// Status change logic for Paid Amount field const togglePaidAmount = (statusId, containerId) => { const statusEl = document.getElementById(statusId); const containerEl = document.getElementById(containerId); if (statusEl && containerEl) { statusEl.addEventListener('change', function() { if (this.value === 'partially_paid') { containerEl.style.display = 'block'; } else { containerEl.style.display = 'none'; } }); } }; togglePaidAmount('add_status', 'addPaidAmountContainer'); togglePaidAmount('edit_status', 'editPaidAmountContainer'); // Pay Invoice Logic document.querySelectorAll('.pay-invoice-btn').forEach(btn => { btn.addEventListener('click', function() { const id = this.getAttribute('data-id'); const total = parseFloat(this.getAttribute('data-total')); const paid = parseFloat(this.getAttribute('data-paid') || 0); const remaining = total - paid; document.getElementById('pay_invoice_id').value = id; document.getElementById('pay_invoice_total').value = total.toFixed(3); document.getElementById('pay_remaining_amount').value = remaining.toFixed(3); document.getElementById('pay_amount').value = remaining.toFixed(3); document.getElementById('pay_amount').max = remaining.toFixed(3); }); }); // Show receipt modal if needed if (typeof showReceipt === 'function') { showReceipt(= $rid ?>); } else { setTimeout(() => { if (typeof showReceipt === 'function') showReceipt(= $rid ?>); }, 500); } function showReceipt(paymentId) { fetch(`index.php?action=get_payment_details&payment_id=${paymentId}`) .then(res => res.json()) .then(data => { if (!data) return; document.getElementById('receiptNo').textContent = 'RCP-' + data.id.toString().padStart(5, '0'); document.getElementById('receiptDate').textContent = data.payment_date; document.getElementById('receiptCustomer').textContent = data.customer_name || '---'; document.getElementById('receiptInvNo').textContent = (data.inv_type === 'purchase' ? 'PUR-' : 'INV-') + data.inv_id.toString().padStart(5, '0'); document.getElementById('receiptMethod').textContent = data.payment_method; document.getElementById('receiptAmount').textContent = parseFloat(data.amount).toFixed(3); document.getElementById('receiptAmountWords').textContent = data.amount_words; const outletEl = document.getElementById('receiptOutletName'); if (outletEl) { outletEl.textContent = data.outlet_name ? (data.outlet_name) : ''; outletEl.style.display = data.outlet_name ? 'block' : 'none'; } // Update labels for Purchase vs Sale const partyLabel = document.getElementById('receiptPartyLabel'); const againstLabel = document.getElementById('receiptAgainstLabel'); const receiptTitle = document.querySelector('#receiptModal .modal-title'); const receiptH4 = document.querySelector('.receipt-container h4.letter-spacing-2'); if (data.inv_type === 'purchase') { partyLabel.textContent = 'Paid To'; partyLabel.setAttribute('data-en', 'Paid To'); partyLabel.setAttribute('data-ar', 'صرف إلى'); againstLabel.textContent = 'Against Purchase'; againstLabel.setAttribute('data-en', 'Against Purchase'); againstLabel.setAttribute('data-ar', 'مقابل شراء'); receiptTitle.textContent = 'Payment Voucher'; receiptTitle.setAttribute('data-en', 'Payment Voucher'); receiptTitle.setAttribute('data-ar', 'سند صرف'); receiptH4.textContent = 'PAYMENT VOUCHER'; receiptH4.setAttribute('data-en', 'PAYMENT VOUCHER'); receiptH4.setAttribute('data-ar', 'سند صرف'); } else { partyLabel.textContent = 'Received From'; partyLabel.setAttribute('data-en', 'Received From'); partyLabel.setAttribute('data-ar', 'وصلنا من'); againstLabel.textContent = 'Against Invoice'; againstLabel.setAttribute('data-en', 'Against Invoice'); againstLabel.setAttribute('data-ar', 'مقابل فاتورة'); receiptTitle.textContent = 'Payment Receipt'; receiptTitle.setAttribute('data-en', 'Payment Receipt'); receiptTitle.setAttribute('data-ar', 'سند قبض'); receiptH4.textContent = 'PAYMENT RECEIPT'; receiptH4.setAttribute('data-en', 'PAYMENT RECEIPT'); receiptH4.setAttribute('data-ar', 'سند قبض'); } const notesContainer = document.getElementById('receiptNotesContainer'); if (data.notes) { document.getElementById('receiptNotes').textContent = data.notes; notesContainer.style.display = 'block'; } else { notesContainer.style.display = 'none'; } const receiptModal = new bootstrap.Modal(document.getElementById('receiptModal')); receiptModal.show(); }); }; document.querySelectorAll('.view-payments-btn').forEach(btn => { btn.addEventListener('click', function() { const invoiceId = this.getAttribute('data-id'); const tbody = document.getElementById('paymentsTableBody'); tbody.innerHTML = '