536 lines
20 KiB
PHP
536 lines
20 KiB
PHP
<?php
|
|
session_start();
|
|
require '../config.php';
|
|
require '../include_header.php';
|
|
|
|
$prefill_billing_id = (int)($_GET['billing_id'] ?? 0);
|
|
?>
|
|
|
|
<body>
|
|
<?php require '../include_topbar.php'; ?>
|
|
<?php require '../include_sidebar_ac.php'; ?>
|
|
|
|
<main id="content" class="content py-15">
|
|
<div class="container-fluid">
|
|
|
|
<div class="row mb-6">
|
|
<div class="col-12">
|
|
<div class="d-flex justify-content-between align-items-center">
|
|
<div>
|
|
<h1 class="fs-3 mb-1">Receipts</h1>
|
|
<p class="mb-0 text-muted">Customer payments against receipt billings</p>
|
|
</div>
|
|
<button class="btn btn-primary" onclick="show_receipt_form()">
|
|
<i class="ti ti-plus me-1"></i>New Receipt
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row g-5 d-none" id="receipt_form">
|
|
<div class="col-lg-8">
|
|
<div class="card mb-5">
|
|
<div class="card-body p-5">
|
|
<h2 class="fs-5 mb-4">Receipt Information</h2>
|
|
<div class="row gx-5">
|
|
<div class="mb-3 col-lg-6">
|
|
<label class="form-label">Customer</label>
|
|
<input type="text" id="contact" class="form-control contact_search"
|
|
placeholder="Search customer..." autocomplete="off">
|
|
<input type="hidden" id="contact_id">
|
|
</div>
|
|
<div class="mb-3 col-lg-3">
|
|
<label class="form-label">Receipt Date</label>
|
|
<input type="text" id="receipt_date" class="form-control" placeholder="DD/MM/YYYY">
|
|
</div>
|
|
<div class="mb-3 col-lg-3">
|
|
<label class="form-label">Method</label>
|
|
<select id="payment_method" class="form-select">
|
|
<option value="bank_transfer">Bank Transfer</option>
|
|
<option value="cash">Cash</option>
|
|
<option value="cheque">Cheque</option>
|
|
<option value="card">Card</option>
|
|
<option value="other">Other</option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3 col-lg-4">
|
|
<label class="form-label">GL Formula</label>
|
|
<select id="formula_id" class="form-select">
|
|
<option value="">— None —</option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3 col-lg-4">
|
|
<label class="form-label">Department</label>
|
|
<select id="department_id" class="form-select">
|
|
<option value="0">— No Department —</option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3 col-lg-12">
|
|
<label class="form-label">Notes</label>
|
|
<textarea id="notes" class="form-control" rows="2"></textarea>
|
|
</div>
|
|
</div>
|
|
<button class="btn btn-outline-secondary" onclick="load_open_billings()">
|
|
<i class="ti ti-search me-1"></i>Load Open Billings
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-body p-5">
|
|
<h2 class="fs-5 mb-4">Receipt Billings</h2>
|
|
<div class="table-responsive">
|
|
<table class="table mb-0" id="allocation_table">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>Document / Billing</th>
|
|
<th>Type / Date</th>
|
|
<th>Department</th>
|
|
<th>Balance</th>
|
|
<th style="width:150px;">Receive / Use</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="allocation_tbody">
|
|
<tr><td colspan="5" class="text-center py-5 text-muted">Select a customer and load open billings.</td></tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-lg-4">
|
|
<div class="card mb-5">
|
|
<div class="card-body p-4">
|
|
<h2 class="fs-5 mb-3">Summary</h2>
|
|
<div class="d-flex justify-content-between mb-2">
|
|
<span class="text-muted">Selected Billings</span>
|
|
<span id="summary_billing">0.00</span>
|
|
</div>
|
|
<hr>
|
|
<div class="d-flex justify-content-between fw-bold fs-5">
|
|
<span>Cash Receipt</span>
|
|
<span id="summary_cash">0.00</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-body p-4 d-flex flex-column gap-2">
|
|
<button class="btn btn-primary w-100" onclick="post_receipt()">
|
|
<i class="ti ti-check me-1"></i>Post Receipt
|
|
</button>
|
|
<button class="btn btn-light w-100" onclick="hide_receipt_form()">Cancel</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row mt-5" id="receipt_list_section">
|
|
<div class="col-12 mb-4">
|
|
<div class="card">
|
|
<div class="card-body p-4">
|
|
<div class="row g-3 align-items-end">
|
|
<div class="col-lg-4">
|
|
<div class="position-relative">
|
|
<input type="text" id="search_input" class="form-control ps-9"
|
|
placeholder="Search receipt number or customer...">
|
|
<span class="position-absolute top-25 ms-4">
|
|
<i class="ti ti-search text-muted"></i>
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<div class="col-lg-2">
|
|
<select id="filter_dept" class="form-select">
|
|
<option value="">All Departments</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-lg-2">
|
|
<button class="btn btn-secondary w-100" onclick="load_receipts()">
|
|
<i class="ti ti-search me-1"></i>Search
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-12">
|
|
<div class="card">
|
|
<div class="card-body p-5">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover mb-0 table-centered" id="receipt_list">
|
|
<thead class="table-primary border-light">
|
|
<tr>
|
|
<th>Receipt #</th>
|
|
<th>Billing #</th>
|
|
<th>Date</th>
|
|
<th>Customer</th>
|
|
<th>Department</th>
|
|
<th>Method</th>
|
|
<th>Amount</th>
|
|
<th>GL Formula</th>
|
|
<th>Status</th>
|
|
<th>Action</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody></tbody>
|
|
<tfoot></tfoot>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</main>
|
|
|
|
<?php require '../include_ending.php'; ?>
|
|
|
|
<script>
|
|
|
|
const can_delete = ['admin', 'owner'].includes(user_role);
|
|
|
|
var open_billings = [];
|
|
var prefill_billing_id = <?php echo $prefill_billing_id; ?>;
|
|
var formula_map = {};
|
|
var all_receipts = [];
|
|
|
|
function load_formula_map() {
|
|
return ajax_request({
|
|
url: '<?php echo $server_url?>accounting/api/engine/account_formula.php',
|
|
autoPrepare: true,
|
|
checkRequired: 0,
|
|
action: 'read',
|
|
data: {},
|
|
onSuccess: function(res) {
|
|
formula_map = {};
|
|
$.each(res.output || [], function(i, f) { formula_map[f.id] = f.formula_name; });
|
|
}
|
|
});
|
|
}
|
|
|
|
function show_receipt_form() {
|
|
$('#receipt_form').removeClass('d-none');
|
|
$('#receipt_date').val(flatpickr.formatDate(new Date(), 'd/m/Y'));
|
|
ajax_request({
|
|
url: server_url + 'accounting/api/engine/account_formula.php',
|
|
autoPrepare: true,
|
|
checkRequired: 0,
|
|
action: 'get_by_type',
|
|
data: { document_type: 'receipt' },
|
|
onSuccess: function(fres) {
|
|
var opts = '<option value="">— None —</option>';
|
|
$.each(fres.output || [], function(i, f) {
|
|
opts += '<option value="' + f.id + '">' + escape_html(f.formula_name) + '</option>';
|
|
});
|
|
$('#formula_id').html(opts);
|
|
}
|
|
});
|
|
}
|
|
|
|
function hide_receipt_form() {
|
|
$('#receipt_form').addClass('d-none');
|
|
}
|
|
|
|
function receipt_status_badge(status) {
|
|
return String(status) === '4'
|
|
? '<span class="badge bg-light text-dark">Void</span>'
|
|
: '<span class="badge bg-success">Posted</span>';
|
|
}
|
|
|
|
function method_label(value) {
|
|
return String(value || '').replace(/_/g, ' ').replace(/\b\w/g, function(c) {
|
|
return c.toUpperCase();
|
|
});
|
|
}
|
|
|
|
function load_receipts() {
|
|
return ajax_request({
|
|
url: '<?php echo $server_url?>finance/api/engine/manage_receipt.php',
|
|
autoPrepare: true,
|
|
checkRequired: 0,
|
|
action: 'read',
|
|
data: { action: 'list' },
|
|
onSuccess: function(res) {
|
|
var data = res.output || [];
|
|
alasql('DROP TABLE IF EXISTS receipt_list');
|
|
alasql('CREATE TABLE receipt_list');
|
|
alasql.tables.receipt_list.data = data;
|
|
$('table#receipt_list tfoot').html(generate_pagination('receipt_list', data.length));
|
|
change_page_receipt_list(1);
|
|
}
|
|
});
|
|
}
|
|
|
|
function change_page_receipt_list(page_num) {
|
|
var offset = (page_num - 1) * prop_limit;
|
|
var rows = alasql(`SELECT * FROM receipt_list LIMIT ${prop_limit} OFFSET ${offset}`);
|
|
var body = '';
|
|
|
|
if (!rows.length) {
|
|
$('table#receipt_list tbody').html('<tr><td colspan="10" class="text-center py-5 text-muted">No receipts found.</td></tr>');
|
|
return;
|
|
}
|
|
|
|
$.each(rows, function(i, r) {
|
|
var formula_name = r.formula_id && formula_map[r.formula_id]
|
|
? escape_html(formula_map[r.formula_id])
|
|
: '<span class="text-muted">—</span>';
|
|
body += `<tr>
|
|
<td class="py-3 fw-semibold">${escape_html(r.receipt_number || '')}</td>
|
|
<td class="py-3">${escape_html(r.billing_number || '—')}</td>
|
|
<td class="py-3">${r.receipt_date ? format_date(r.receipt_date) : '<span class="text-muted">—</span>'}</td>
|
|
<td class="py-3">${escape_html(r.contact_name || '—')}</td>
|
|
<td class="py-3">${get_dept_label(r.department_id)}</td>
|
|
<td class="py-3">${escape_html(method_label(r.payment_method) || '—')}</td>
|
|
<td class="py-3 fw-semibold">${format_number(r.amount, 2)}</td>
|
|
<td class="py-3 small">${formula_name}</td>
|
|
<td class="py-3">${receipt_status_badge(r.status)}</td>
|
|
<td class="py-3">
|
|
<a href="<?php echo $server_url?>finance/manage_receipt.php?id=${r.id}" title="View receipt">
|
|
<i class="ti ti-eye fs-5"></i>
|
|
</a>
|
|
${can_delete ? `<a href="javascript:void(0);" class="link-danger ms-2" title="Delete" onclick="delete_receipt(${r.id})"><i class="ti ti-trash fs-5"></i></a>` : ''}
|
|
</td>
|
|
</tr>`;
|
|
});
|
|
$('table#receipt_list tbody').html(body);
|
|
}
|
|
|
|
function load_open_billings() {
|
|
var contact_id = parseInt($('#contact_id').val() || $('#contact').attr('data-id')) || 0;
|
|
|
|
ajax_request({
|
|
url: '<?php echo $server_url?>finance/api/engine/manage_receipt_billing.php',
|
|
autoPrepare: true,
|
|
checkRequired: 0,
|
|
action: 'open_billings',
|
|
data: { contact_id: contact_id },
|
|
onSuccess: function(res) {
|
|
open_billings = res.output || [];
|
|
render_open_billings();
|
|
}
|
|
});
|
|
}
|
|
|
|
function render_open_billings() {
|
|
var body = '';
|
|
if (!open_billings.length) {
|
|
$('#allocation_tbody').html('<tr><td colspan="5" class="text-center py-5 text-muted">No open receipt billings for this contact.</td></tr>');
|
|
recalc_receipt();
|
|
return;
|
|
}
|
|
|
|
if (open_billings.length === 1 && Array.isArray(open_billings[0].allocations)) {
|
|
var billing = open_billings[0];
|
|
$.each(billing.allocations || [], function(i, line) {
|
|
var balance = parseFloat(line.balance_amount || 0);
|
|
if (balance <= 0.0001) return;
|
|
var is_cn = line.doc_type === 'credit_note';
|
|
var display_balance = is_cn ? -balance : balance;
|
|
body += `<tr data-billing-id="${billing.id}" data-doc-id="${line.invoice_id}" data-doc-type="${line.doc_type}" data-department-id="${line.department_id || 0}" data-balance="${balance}">
|
|
<td>
|
|
<div class="fw-semibold">${escape_html(line.invoice_number || '')}</div>
|
|
<div class="small text-muted">${escape_html(billing.billing_number || '')}</div>
|
|
</td>
|
|
<td>${is_cn ? '<span class="badge bg-warning text-dark">Credit Note</span>' : '<span class="badge bg-primary">Invoice</span>'}</td>
|
|
<td>${get_dept_label(line.department_id)}</td>
|
|
<td class="fw-semibold ${is_cn ? 'text-danger' : ''}">${format_number(display_balance, 2)}</td>
|
|
<td>
|
|
<input type="number" class="form-control form-control-sm receipt_amount"
|
|
${is_cn ? `min="${-balance}" max="0" value="0"` : `min="0" max="${balance}" value="0"`}
|
|
step="any" oninput="recalc_receipt()">
|
|
</td>
|
|
</tr>`;
|
|
});
|
|
$('#allocation_tbody').html(body || '<tr><td colspan="5" class="text-center py-5 text-muted">No remaining billing lines.</td></tr>');
|
|
recalc_receipt();
|
|
return;
|
|
}
|
|
|
|
$.each(open_billings, function(i, d) {
|
|
var balance = parseFloat(d.balance_amount || 0);
|
|
body += `<tr data-billing-id="${d.id}" data-balance="${balance}">
|
|
<td>
|
|
<div class="fw-semibold">${escape_html(d.billing_number || '')}</div>
|
|
<div class="small text-muted">Amount Due ${format_number(d.amount, 2)}</div>
|
|
</td>
|
|
<td>${d.billing_date ? format_date(d.billing_date) : '<span class="text-muted">—</span>'}</td>
|
|
<td><span class="text-muted">—</span></td>
|
|
<td class="fw-semibold">${format_number(balance, 2)}</td>
|
|
<td>
|
|
<input type="number" class="form-control form-control-sm receipt_amount"
|
|
min="0" max="${balance}" step="any" value="0" oninput="recalc_receipt()">
|
|
</td>
|
|
</tr>`;
|
|
});
|
|
$('#allocation_tbody').html(body);
|
|
recalc_receipt();
|
|
}
|
|
|
|
function recalc_receipt() {
|
|
var invoice_total = 0;
|
|
var cn_total = 0;
|
|
|
|
$('#allocation_tbody tr[data-billing-id]').each(function() {
|
|
var amount = parseFloat($(this).find('.receipt_amount').val()) || 0;
|
|
if ($(this).data('doc-id')) {
|
|
if ($(this).data('doc-type') === 'credit_note') cn_total += Math.abs(amount);
|
|
else invoice_total += Math.abs(amount);
|
|
} else {
|
|
invoice_total += amount;
|
|
}
|
|
});
|
|
|
|
var receipt_total = invoice_total - cn_total;
|
|
$('#summary_billing').text(format_number(receipt_total, 2));
|
|
$('#summary_cash').text(format_number(receipt_total, 2));
|
|
}
|
|
|
|
function collect_receipt_billing() {
|
|
var selected = [];
|
|
$('#allocation_tbody tr[data-billing-id]').each(function() {
|
|
var amount = parseFloat($(this).find('.receipt_amount').val()) || 0;
|
|
var balance = parseFloat($(this).data('balance')) || 0;
|
|
var abs_amount = Math.abs(amount);
|
|
if (abs_amount <= 0) return;
|
|
if (abs_amount - balance > 0.0001) {
|
|
throw new Error('Receipt amount exceeds billing balance.');
|
|
}
|
|
if ($(this).data('doc-id')) {
|
|
selected.push({
|
|
receipt_billing_id: parseInt($(this).data('billing-id')),
|
|
invoice_id: parseInt($(this).data('doc-id')),
|
|
department_id: parseInt($(this).data('department-id')) || 0,
|
|
amount: abs_amount,
|
|
});
|
|
return;
|
|
}
|
|
selected.push({
|
|
receipt_billing_id: parseInt($(this).data('billing-id')),
|
|
amount: abs_amount,
|
|
});
|
|
});
|
|
var billing_ids = [...new Set(selected.map(row => row.receipt_billing_id))];
|
|
if (billing_ids.length > 1) {
|
|
throw new Error('Please post one receipt billing per receipt.');
|
|
}
|
|
if (!selected.length) return null;
|
|
if (selected[0].invoice_id) {
|
|
var invoice_total = 0;
|
|
var cn_total = 0;
|
|
selected.forEach(function(row) {
|
|
var $tr = $(`#allocation_tbody tr[data-doc-id="${row.invoice_id}"]`);
|
|
if ($tr.data('doc-type') === 'credit_note') cn_total += row.amount;
|
|
else invoice_total += row.amount;
|
|
});
|
|
if (invoice_total - cn_total <= 0.0001) {
|
|
throw new Error('Cash receipt amount must be greater than zero.');
|
|
}
|
|
return {
|
|
receipt_billing_id: billing_ids[0],
|
|
amount: invoice_total - cn_total,
|
|
allocations: selected.map(row => ({ invoice_id: row.invoice_id, department_id: row.department_id || 0, amount: row.amount })),
|
|
};
|
|
}
|
|
if (selected[0].amount <= 0.0001) {
|
|
throw new Error('Receipt amount must be greater than zero.');
|
|
}
|
|
return selected[0];
|
|
}
|
|
|
|
function post_receipt() {
|
|
var billing;
|
|
try {
|
|
billing = collect_receipt_billing();
|
|
} catch(e) {
|
|
bootbox.alert(e.message);
|
|
return;
|
|
}
|
|
if (!billing) {
|
|
bootbox.alert('Please enter a receipt amount for one receipt billing.');
|
|
return;
|
|
}
|
|
|
|
ajax_request({
|
|
url: '<?php echo $server_url?>finance/api/engine/manage_receipt.php',
|
|
autoPrepare: true,
|
|
checkRequired: 0,
|
|
action: 'create',
|
|
data: {
|
|
receipt_billing_id: billing.receipt_billing_id,
|
|
amount: billing.amount,
|
|
allocations: billing.allocations ? JSON.stringify(billing.allocations) : '',
|
|
receipt_date: to_iso_date($('#receipt_date').val()),
|
|
payment_method: $('#payment_method').val(),
|
|
formula_id: $('#formula_id').val() || null,
|
|
department_id: $('#department_id').val() || 0,
|
|
notes: $('#notes').val(),
|
|
},
|
|
onSuccess: function(res) {
|
|
if (res.new_id) {
|
|
window.location.href = '<?php echo $server_url?>finance/manage_receipt.php?id=' + res.new_id;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
$(document).on('contact:selected', '#contact', function(e, contact) {
|
|
$('#contact_id').val(contact.id);
|
|
$('#contact').attr('data-id', contact.id);
|
|
});
|
|
|
|
$(document).on('input', '#contact', function() {
|
|
$('#contact_id').val('');
|
|
$(this).removeAttr('data-id');
|
|
});
|
|
|
|
function delete_receipt(id) {
|
|
bootbox.confirm({
|
|
message: 'Delete this receipt? The GL entry and invoice allocations will be reversed.',
|
|
buttons: { confirm: { label: 'Delete', className: 'btn-danger' }, cancel: { label: 'Back', className: 'btn-secondary' } },
|
|
callback: function(result) {
|
|
if (!result) return;
|
|
ajax_request({
|
|
url: '<?php echo $server_url?>finance/api/engine/delete_receipt.php',
|
|
autoPrepare: false,
|
|
checkRequired: 0,
|
|
action: 'delete',
|
|
data: { id: id },
|
|
onSuccess: function() { load_receipts(); }
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
$(async function() {
|
|
flatpickr('#receipt_date', { dateFormat: 'd/m/Y', allowInput: true });
|
|
load_departments('department_id');
|
|
await load_formula_map();
|
|
load_receipts();
|
|
if (prefill_billing_id > 0) {
|
|
$('#receipt_list_section').addClass('d-none');
|
|
show_receipt_form();
|
|
ajax_request({
|
|
url: '<?php echo $server_url?>finance/api/engine/manage_receipt_billing.php',
|
|
autoPrepare: true,
|
|
checkRequired: 0,
|
|
queueLock: false,
|
|
action: 'retrieve',
|
|
data: { id: prefill_billing_id },
|
|
onSuccess: function(res) {
|
|
var billing = res.output || {};
|
|
$('#contact').val(billing.contact_name || '').attr('data-id', billing.contact_id || '');
|
|
$('#contact_id').val(billing.contact_id || '');
|
|
open_billings = parseFloat(billing.balance_amount || 0) > 0.0001 ? [billing] : [];
|
|
render_open_billings();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|