accounting workflows
This commit is contained in:
+458
-6
@@ -2,6 +2,8 @@
|
||||
session_start();
|
||||
require '../config.php';
|
||||
require '../include_header.php';
|
||||
|
||||
$prefill_billing_id = (int)($_GET['billing_id'] ?? 0);
|
||||
?>
|
||||
|
||||
<body>
|
||||
@@ -13,16 +15,141 @@
|
||||
|
||||
<div class="row mb-6">
|
||||
<div class="col-12">
|
||||
<h1 class="fs-3 mb-1">Payments</h1>
|
||||
<p class="mb-0 text-muted">Cash and bank payments</p>
|
||||
<div class="d-flex justify-content-between align-items-center">
|
||||
<div>
|
||||
<h1 class="fs-3 mb-1">Payments</h1>
|
||||
<p class="mb-0 text-muted">Supplier payments against payment billings</p>
|
||||
</div>
|
||||
<button class="btn btn-primary" onclick="show_payment_form()">
|
||||
<i class="ti ti-plus me-1"></i>New Payment
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="row g-5 d-none" id="payment_form">
|
||||
<div class="col-lg-8">
|
||||
<div class="card mb-5">
|
||||
<div class="card-body p-5">
|
||||
<h2 class="fs-5 mb-4">Payment Information</h2>
|
||||
<div class="row gx-5">
|
||||
<div class="mb-3 col-lg-6">
|
||||
<label class="form-label">Supplier</label>
|
||||
<input type="text" id="contact" class="form-control contact_search"
|
||||
placeholder="Search supplier..." autocomplete="off">
|
||||
<input type="hidden" id="contact_id">
|
||||
</div>
|
||||
<div class="mb-3 col-lg-3">
|
||||
<label class="form-label">Payment Date</label>
|
||||
<input type="text" id="payment_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">Payment 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;">Pay / Use</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="allocation_tbody">
|
||||
<tr><td colspan="5" class="text-center py-5 text-muted">Select a supplier 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 Payment</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_payment()">
|
||||
<i class="ti ti-check me-1"></i>Post Payment
|
||||
</button>
|
||||
<button class="btn btn-light w-100" onclick="hide_payment_form()">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row mt-5" id="payment_list_section">
|
||||
<div class="col-12">
|
||||
<div class="card p-5 text-center text-muted">
|
||||
<i class="ti ti-credit-card fs-1 mb-3 d-block"></i>
|
||||
<p class="mb-0">Payments coming soon.</p>
|
||||
<div class="card">
|
||||
<div class="card-body p-5">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover mb-0 table-centered" id="payment_list">
|
||||
<thead class="table-primary border-light">
|
||||
<tr>
|
||||
<th>Payment #</th>
|
||||
<th>Billing #</th>
|
||||
<th>Date</th>
|
||||
<th>Supplier</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>
|
||||
@@ -32,5 +159,330 @@
|
||||
|
||||
<?php require '../include_ending.php'; ?>
|
||||
|
||||
<script>
|
||||
var open_billings = [];
|
||||
var prefill_billing_id = <?php echo $prefill_billing_id; ?>;
|
||||
var formula_map = {};
|
||||
|
||||
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_payment_form() {
|
||||
$('#payment_form').removeClass('d-none');
|
||||
$('#payment_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: 'payment' },
|
||||
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_payment_form() {
|
||||
$('#payment_form').addClass('d-none');
|
||||
}
|
||||
|
||||
function payment_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_payments() {
|
||||
return ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_payment.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'read',
|
||||
data: { action: 'list' },
|
||||
onSuccess: function(res) {
|
||||
var data = res.output || [];
|
||||
alasql('DROP TABLE IF EXISTS payment_list');
|
||||
alasql('CREATE TABLE payment_list');
|
||||
alasql.tables.payment_list.data = data;
|
||||
$('table#payment_list tfoot').html(generate_pagination('payment_list', data.length));
|
||||
change_page_payment_list(1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function change_page_payment_list(page_num) {
|
||||
var offset = (page_num - 1) * prop_limit;
|
||||
var rows = alasql(`SELECT * FROM payment_list LIMIT ${prop_limit} OFFSET ${offset}`);
|
||||
var body = '';
|
||||
|
||||
if (!rows.length) {
|
||||
$('table#payment_list tbody').html('<tr><td colspan="9" class="text-center py-5 text-muted">No payments 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.payment_number || '')}</td>
|
||||
<td class="py-3">${escape_html(r.billing_number || '—')}</td>
|
||||
<td class="py-3">${r.payment_date ? format_date(r.payment_date) : '<span class="text-muted">—</span>'}</td>
|
||||
<td class="py-3">${escape_html(r.contact_name || '—')}</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">${payment_status_badge(r.status)}</td>
|
||||
<td class="py-3">
|
||||
<a href="<?php echo $server_url?>finance/manage_payment.php?id=${r.id}" title="View payment">
|
||||
<i class="ti ti-eye fs-5"></i>
|
||||
</a>
|
||||
</td>
|
||||
</tr>`;
|
||||
});
|
||||
$('table#payment_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_payment_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 payment billings for this supplier.</td></tr>');
|
||||
recalc_payment();
|
||||
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 === 'supplier_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">Supplier Credit Note</span>' : '<span class="badge bg-primary">Purchase 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 payment_amount"
|
||||
${is_cn ? `min="${-balance}" max="0" value="0"` : `min="0" max="${balance}" value="0"`}
|
||||
step="any" oninput="recalc_payment()">
|
||||
</td>
|
||||
</tr>`;
|
||||
});
|
||||
$('#allocation_tbody').html(body || '<tr><td colspan="5" class="text-center py-5 text-muted">No remaining billing lines.</td></tr>');
|
||||
recalc_payment();
|
||||
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 payment_amount"
|
||||
min="0" max="${balance}" step="any" value="0" oninput="recalc_payment()">
|
||||
</td>
|
||||
</tr>`;
|
||||
});
|
||||
$('#allocation_tbody').html(body);
|
||||
recalc_payment();
|
||||
}
|
||||
|
||||
function recalc_payment() {
|
||||
var purchase_invoice_total = 0;
|
||||
var cn_total = 0;
|
||||
|
||||
$('#allocation_tbody tr[data-billing-id]').each(function() {
|
||||
var amount = parseFloat($(this).find('.payment_amount').val()) || 0;
|
||||
if ($(this).data('doc-id')) {
|
||||
if ($(this).data('doc-type') === 'supplier_credit_note') cn_total += Math.abs(amount);
|
||||
else purchase_invoice_total += Math.abs(amount);
|
||||
} else {
|
||||
purchase_invoice_total += amount;
|
||||
}
|
||||
});
|
||||
|
||||
var payment_total = purchase_invoice_total - cn_total;
|
||||
$('#summary_billing').text(format_number(payment_total, 2));
|
||||
$('#summary_cash').text(format_number(payment_total, 2));
|
||||
}
|
||||
|
||||
function collect_payment_billing() {
|
||||
var selected = [];
|
||||
$('#allocation_tbody tr[data-billing-id]').each(function() {
|
||||
var amount = parseFloat($(this).find('.payment_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('Payment amount exceeds billing balance.');
|
||||
}
|
||||
if ($(this).data('doc-id')) {
|
||||
selected.push({
|
||||
payment_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({
|
||||
payment_billing_id: parseInt($(this).data('billing-id')),
|
||||
amount: abs_amount,
|
||||
});
|
||||
});
|
||||
var billing_ids = [...new Set(selected.map(row => row.payment_billing_id))];
|
||||
if (billing_ids.length > 1) {
|
||||
throw new Error('Please post one payment billing per payment.');
|
||||
}
|
||||
if (!selected.length) return null;
|
||||
if (selected[0].invoice_id) {
|
||||
var purchase_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') === 'supplier_credit_note') cn_total += row.amount;
|
||||
else purchase_invoice_total += row.amount;
|
||||
});
|
||||
if (purchase_invoice_total - cn_total <= 0.0001) {
|
||||
throw new Error('Cash payment amount must be greater than zero.');
|
||||
}
|
||||
return {
|
||||
payment_billing_id: billing_ids[0],
|
||||
amount: purchase_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('Payment amount must be greater than zero.');
|
||||
}
|
||||
return selected[0];
|
||||
}
|
||||
|
||||
function post_payment() {
|
||||
var billing;
|
||||
try {
|
||||
billing = collect_payment_billing();
|
||||
} catch(e) {
|
||||
bootbox.alert(e.message);
|
||||
return;
|
||||
}
|
||||
if (!billing) {
|
||||
bootbox.alert('Please enter a payment amount for one payment billing.');
|
||||
return;
|
||||
}
|
||||
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_payment.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'create',
|
||||
data: {
|
||||
payment_billing_id: billing.payment_billing_id,
|
||||
amount: billing.amount,
|
||||
allocations: billing.allocations ? JSON.stringify(billing.allocations) : '',
|
||||
payment_date: to_iso_date($('#payment_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 (prefill_billing_id > 0 && res.new_id) {
|
||||
window.location.href = '<?php echo $server_url?>finance/manage_payment.php?id=' + res.new_id;
|
||||
return;
|
||||
}
|
||||
hide_payment_form();
|
||||
$('#contact, #contact_id, #notes').val('');
|
||||
$('#allocation_tbody').html('<tr><td colspan="5" class="text-center py-5 text-muted">Select a supplier and load open billings.</td></tr>');
|
||||
load_payments();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$(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');
|
||||
});
|
||||
|
||||
$(async function() {
|
||||
flatpickr('#payment_date', { dateFormat: 'd/m/Y', allowInput: true });
|
||||
load_departments('department_id');
|
||||
await load_formula_map();
|
||||
load_payments();
|
||||
if (prefill_billing_id > 0) {
|
||||
$('#payment_list_section').addClass('d-none');
|
||||
show_payment_form();
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_payment_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>
|
||||
|
||||
Reference in New Issue
Block a user