automate and view gl entries

This commit is contained in:
Thanakorn S
2026-05-23 16:46:35 +07:00
parent 5affae1fc5
commit 54f3f11fd2
12 changed files with 551 additions and 26 deletions
+103 -4
View File
@@ -355,10 +355,16 @@
<td class="py-3 fw-semibold">${format_number(inv.grand_total, 2)}</td>
<td class="py-3 small">${formula_name}</td>
<td class="py-3">${invoice_status_badge(inv.status, inv.due_date, inv.payment_state, inv.doc_type)}</td>
<td class="py-3">
<a href="<?php echo $server_url?>revenue/manage_invoice.php?id=${inv.id}" title="View">
<td class="py-3 text-nowrap">
<a href="<?php echo $server_url?>revenue/manage_invoice.php?id=${inv.id}" title="View" class="me-2">
<i class="ti ti-eye fs-5"></i>
</a>
<a href="javascript:;" title="View GL Entry" class="me-2" onclick="show_gl_modal('invoice',${inv.id})">
<i class="ti ti-book fs-5 text-secondary"></i>
</a>
<a href="javascript:;" title="Delete" class="link-danger" onclick="delete_invoice(${inv.id},'${escape_html(inv.invoice_number)}')">
<i class="ti ti-trash fs-5"></i>
</a>
</td>
</tr>`;
});
@@ -397,16 +403,93 @@
<td class="py-3 fw-semibold text-danger">${format_number(cn.grand_total, 2)}</td>
<td class="py-3 small">${formula_name}</td>
<td class="py-3">${invoice_status_badge(cn.status, cn.due_date, cn.payment_state, cn.doc_type)}</td>
<td class="py-3">
<a href="<?php echo $server_url?>revenue/manage_invoice.php?id=${cn.id}" title="View">
<td class="py-3 text-nowrap">
<a href="<?php echo $server_url?>revenue/manage_invoice.php?id=${cn.id}" title="View" class="me-2">
<i class="ti ti-eye fs-5"></i>
</a>
<a href="javascript:;" title="View GL Entry" class="me-2" onclick="show_gl_modal('credit_note',${cn.id})">
<i class="ti ti-book fs-5 text-secondary"></i>
</a>
<a href="javascript:;" title="Delete" class="link-danger" onclick="delete_invoice(${cn.id},'${escape_html(cn.invoice_number)}')">
<i class="ti ti-trash fs-5"></i>
</a>
</td>
</tr>`;
});
$('table#cn_list tbody').html(body);
}
function delete_invoice(id, doc_number) {
bootbox.confirm({
message: '<strong>Delete ' + escape_html(doc_number) + '?</strong><br>This cannot be undone. The document number will be freed for reuse.',
buttons: {
confirm: { label: 'Delete', className: 'btn-danger' },
cancel: { label: 'Back', className: 'btn-secondary' }
},
callback: function(result) {
if (!result) return;
ajax_request({
url: server_url + 'order/api/engine/delete_invoice.php',
autoPrepare: true, checkRequired: 0, action: 'delete',
data: { id: id },
onSuccess: function() { retrieve_invoices(); }
});
}
});
}
var _GL_SOURCE_LABELS = { invoice:'Invoice', credit_note:'Credit Note', purchase_invoice:'Purchase Invoice', supplier_credit_note:'Supplier Credit Note', receipt:'Receipt', payment:'Payment', manual:'Manual', reversal:'Reversal' };
var _GL_SOURCE_BADGE = { invoice:'bg-success-subtle text-success', credit_note:'bg-warning-subtle text-warning', purchase_invoice:'bg-primary-subtle text-primary', supplier_credit_note:'bg-secondary-subtle text-secondary', receipt:'bg-info-subtle text-info', payment:'bg-danger-subtle text-danger', manual:'bg-dark-subtle text-dark', reversal:'bg-danger-subtle text-danger' };
function _gl_source_badge(t) { return '<span class="badge ' + (_GL_SOURCE_BADGE[t]||'bg-light text-dark') + '">' + (_GL_SOURCE_LABELS[t]||t) + '</span>'; }
function show_gl_modal(doc_type, source_id) {
$('#gl-modal-body').html('<div class="text-center text-muted py-4"><i class="ti ti-loader ti-spin me-1"></i>Loading…</div>');
$('#glModal').modal('show');
ajax_request({
url: server_url + 'accounting/api/engine/get_gl_by_source.php',
autoPrepare: true, checkRequired: 0, noLoading: true, action: 'read',
data: { doc_type: doc_type, source_id: source_id },
onSuccess: function(res) {
if (!res.output) {
$('#gl-modal-body').html('<p class="text-muted text-center py-4">No GL entry posted for this document.</p>');
return;
}
var h = res.output.header, lines = res.output.lines || [];
var total_debit = 0, total_credit = 0, lines_html = '';
$.each(lines, function(i, l) {
total_debit += parseFloat(l.debit) || 0;
total_credit += parseFloat(l.credit) || 0;
var dept = l.dept_code ? escape_html(l.dept_code + (l.dept_name ? ' - ' + l.dept_name : '')) : '<span class="text-muted">—</span>';
lines_html += '<tr>' +
'<td class="fw-semibold">' + escape_html(l.account_code) + '</td>' +
'<td>' + escape_html(l.account_name) + '</td>' +
'<td>' + dept + '</td>' +
'<td class="text-end">' + (parseFloat(l.debit) ? format_number(l.debit, 2) : '<span class="text-muted">—</span>') + '</td>' +
'<td class="text-end">' + (parseFloat(l.credit) ? format_number(l.credit, 2) : '<span class="text-muted">—</span>') + '</td>' +
'<td class="text-muted small">' + escape_html(l.description || '—') + '</td></tr>';
});
lines_html += '<tr class="table-light fw-semibold"><td colspan="3">Total</td>' +
'<td class="text-end">' + format_number(total_debit, 2) + '</td>' +
'<td class="text-end">' + format_number(total_credit, 2) + '</td><td></td></tr>';
var balanced = Math.abs(total_debit - total_credit) < 0.01;
var bal_badge = balanced ? '<span class="badge bg-success-subtle text-success ms-2">Balanced</span>' : '<span class="badge bg-danger-subtle text-danger ms-2">Unbalanced</span>';
$('#gl-modal-body').html(
'<div class="row g-3 mb-4 text-sm">' +
'<div class="col-sm-4"><div class="text-muted small">Source</div><div>' + _gl_source_badge(h.source_type) + '</div></div>' +
'<div class="col-sm-4"><div class="text-muted small">Period</div><div class="fw-semibold">' + escape_html(h.period) + '</div></div>' +
'<div class="col-sm-4"><div class="text-muted small">Formula</div><div>' + escape_html(h.formula_name || '—') + '</div></div>' +
'<div class="col-sm-4"><div class="text-muted small">Posted At</div><div class="text-muted small">' + escape_html(h.posted_at) + '</div></div>' +
'<div class="col-sm-4"><div class="text-muted small">Updated At</div><div class="text-muted small">' + escape_html(h.updated_at_fmt) + '</div></div>' +
'</div>' +
'<div class="d-flex align-items-center mb-2"><span class="fw-semibold">Journal Lines</span>' + bal_badge + '</div>' +
'<table class="table table-sm table-hover mb-0 table-centered">' +
'<thead class="table-light"><tr><th>Account Code</th><th>Account Name</th><th>Department</th><th class="text-end">Debit</th><th class="text-end">Credit</th><th>Description</th></tr></thead>' +
'<tbody>' + lines_html + '</tbody></table>'
);
}
});
}
$(async function() {
try {
await load_dept_cache();
@@ -426,5 +509,21 @@
});
</script>
<!-- GL Entry Modal -->
<div class="modal fade" id="glModal" tabindex="-1">
<div class="modal-dialog modal-lg modal-dialog-scrollable">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="gl-modal-title"><i class="ti ti-book me-2"></i>GL Entry</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body" id="gl-modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</body>
</html>