''' // Main javascript file for Opulent POS document.addEventListener('DOMContentLoaded', () => { const page = document.body.dataset.page; // --- Logic for Cashier Checkout Page --- if (page === 'cashier_checkout') { // --- Element Selectors --- const barcodeInput = document.getElementById('barcode-scanner-input'); const productSearchInput = document.getElementById('product-search'); const productGrid = document.getElementById('product-grid'); const productGridPlaceholder = document.getElementById('product-grid-placeholder'); const cartItemsContainer = document.getElementById('cart-items'); const cartPlaceholder = document.getElementById('cart-placeholder'); const cartItemCount = document.getElementById('cart-item-count'); const cartSubtotal = document.getElementById('cart-subtotal'); const cartTax = document.getElementById('cart-tax'); const cartTotal = document.getElementById('cart-total'); const completeSaleBtn = document.getElementById('complete-sale-btn'); const cancelSaleBtn = document.getElementById('cancel-sale-btn'); const printLastInvoiceBtn = document.getElementById('print-last-invoice-btn'); // --- State Management --- let cart = JSON.parse(localStorage.getItem('cart')) || {}; // --- Utility Functions --- const debounce = (func, delay) => { let timeout; return function(...args) { const context = this; clearTimeout(timeout); timeout = setTimeout(() => func.apply(context, args), delay); }; }; const formatCurrency = (amount) => `PKR ${parseFloat(amount).toFixed(2)}`; // --- API Communication --- const searchProducts = async (query) => { if (query.length < 2) { productGrid.innerHTML = ''; productGridPlaceholder.style.display = 'block'; return; } try { const response = await fetch(`api/search_products.php?q=${encodeURIComponent(query)}`); if (!response.ok) throw new Error('Network response was not ok'); const products = await response.json(); renderProductGrid(products); } catch (error) { console.error('Error fetching products:', error); productGrid.innerHTML = '
Could not fetch products.
'; } }; const findProductByBarcode = async (barcode) => { try { const response = await fetch(`api/search_products.php?q=${encodeURIComponent(barcode)}&exact=true`); if (!response.ok) throw new Error('Network response was not ok'); const products = await response.json(); if (products.length > 0) { addToCart(products[0]); return true; } return false; } catch (error) { console.error('Error fetching product by barcode:', error); return false; } }; // --- Rendering Functions --- const renderProductGrid = (products) => { productGrid.innerHTML = ''; if (products.length === 0) { productGridPlaceholder.innerHTML = 'No products found.
'; productGridPlaceholder.style.display = 'block'; return; } productGridPlaceholder.style.display = 'none'; products.forEach(product => { const productCard = document.createElement('div'); productCard.className = 'col'; productCard.innerHTML = `${formatCurrency(product.price)}
Loading...
'; saleDetailsModal.show(); try { const response = await fetch(`api/get_sale_details.php?id=${saleId}`); if (!response.ok) throw new Error('Failed to fetch details.'); const sale = await response.json(); let itemsHtml = 'Cashier: ${sale.cashier_name || 'N/A'}
Date: ${new Date(sale.created_at).toLocaleString()}
Total: PKR ${parseFloat(sale.total_amount).toFixed(2)}
Error: ${error.message}
`; } } }); } }); ''