// Edit Invoice Logic document.querySelectorAll('.edit-invoice-btn').forEach(btn => { btn.addEventListener('click', function() { const data = JSON.parse(this.dataset.json); document.getElementById('edit_invoice_id').value = data.id; document.getElementById('edit_customer_id').value = data.customer_id; document.getElementById('edit_invoice_date').value = data.invoice_date; document.getElementById('edit_due_date').value = data.due_date || ''; document.getElementById('edit_payment_type').value = data.payment_type || 'cash'; document.getElementById('edit_status').value = data.status || 'unpaid'; document.getElementById('edit_paid_amount').value = parseFloat(data.paid_amount || 0).toFixed(3); if (data.status === 'partially_paid') { document.getElementById('editPaidAmountContainer').style.display = 'block'; } else { document.getElementById('editPaidAmountContainer').style.display = 'none'; } const tableBody = document.getElementById('editInvoiceItemsTableBody'); tableBody.innerHTML = ''; data.items.forEach(item => { // We need more data than what's in invoice_items (like SKU and names, but we have them from the join in PHP) // The dataset-json already contains name_en, name_ar etc because of the PHP logic at line 1093 const itemMeta = { id: item.item_id, name_en: item.name_en, name_ar: item.name_ar, sku: '', // Optional, or fetch if needed vat_rate: 0 // Will be handled if we have it in the join }; // Fetch current item details to get VAT rate if possible, or use stored if available // For simplicity, let's assume we want to use the item's current VAT rate or store it. // Looking at the join at line 1093, it doesn't fetch vat_rate. Let's fix that in PHP too. addItemToTable({ id: item.item_id, name_en: item.name_en, name_ar: item.name_ar, sku: '', vat_rate: item.vat_rate || 0 // We'll add this to PHP join }, tableBody, null, null, document.getElementById('edit_grandTotal'), document.getElementById('edit_subtotal'), document.getElementById('edit_totalVat'), { quantity: item.quantity, unit_price: item.unit_price }); }); }); }); // View and Print Invoice Logic document.addEventListener('click', function(e) { if (e.target.closest('.view-invoice-btn')) { const btn = e.target.closest('.view-invoice-btn'); const data = JSON.parse(btn.dataset.json); if (window.viewAndPrintA4Invoice) { window.viewAndPrintA4Invoice(data, false); } } if (e.target.closest('.print-a4-btn')) { const btn = e.target.closest('.print-a4-btn'); const data = JSON.parse(btn.dataset.json); if (window.viewAndPrintA4Invoice) { window.viewAndPrintA4Invoice(data, true); } } }); // Return Logic (General for Sales and Purchase) const setupReturnLogic = (selectId, containerId, tbodyId, totalDisplayId, submitBtnId, type = "sale") => { const select = document.getElementById(selectId); if (!select) return; const calculateTotal = function() { let total = 0; document.querySelectorAll('#' + tbodyId + ' tr').forEach(row => { const qtyInput = row.querySelector('.return-qty-input'); if (!qtyInput) return; const qty = parseFloat(qtyInput.value) || 0; const price = parseFloat(qtyInput.dataset.price) || 0; const lineTotal = qty * price; const lineTotalDisplay = row.querySelector('.line-total'); if (lineTotalDisplay) { lineTotalDisplay.innerText = lineTotal.toFixed(3); } total += lineTotal; }); const totalDisplay = document.getElementById(totalDisplayId); if (totalDisplay) { totalDisplay.innerText = 'OMR ' + total.toFixed(3); } const submitBtn = document.getElementById(submitBtnId); if (submitBtn) { submitBtn.disabled = total <= 0; } }; const handleInvoiceChange = async function() { const invoiceId = select.value; const container = document.getElementById(containerId); const tbody = document.getElementById(tbodyId); const submitBtn = document.getElementById(submitBtnId); if (!invoiceId) { if (container) container.style.display = 'none'; if (submitBtn) submitBtn.disabled = true; return; } if (tbody) { tbody.innerHTML = '