38471-vm/pages/sales_purchases_payment_receipt_script.php
2026-05-03 04:30:55 +00:00

159 lines
7.9 KiB
PHP

// 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
<?php if (isset($_SESSION['trigger_receipt_modal'])):
$rid = (int)$_SESSION['show_receipt_id'];
unset($_SESSION['trigger_receipt_modal']);
?>
if (typeof showReceipt === 'function') {
showReceipt(<?= $rid ?>);
} else {
setTimeout(() => { if (typeof showReceipt === 'function') showReceipt(<?= $rid ?>); }, 500);
}
<?php endif; ?>
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 = '<tr><td colspan="5" class="text-center" data-en="Loading..." data-ar="جاري التحميل...">Loading...</td></tr>';
fetch(`index.php?action=get_payments&invoice_id=${invoiceId}`)
.then(res => res.json())
.then(data => {
tbody.innerHTML = '';
if (data.length === 0) {
tbody.innerHTML = '<tr><td colspan="5" class="text-center" data-en="No payments found." data-ar="لا توجد مدفوعات.">No payments found.</td></tr>';
return;
}
data.forEach(p => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>RCP-${p.id.toString().padStart(5, '0')}</td>
<td>${p.payment_date}</td>
<td>${p.payment_method}</td>
<td class="text-end fw-bold">OMR ${parseFloat(p.amount).toFixed(3)}</td>
<td class="text-end">
<button class="btn btn-sm btn-outline-primary" onclick="showReceipt(${p.id})">
<i class="bi bi-printer"></i>
</button>
</td>
`;
tbody.appendChild(tr);
});
});
});
});
window.printReceipt = function() {
const content = document.getElementById('printableReceipt').innerHTML;
const originalContent = document.body.innerHTML;
document.body.innerHTML = content;
window.print();
document.body.innerHTML = originalContent;
window.location.reload();
};