automate and view gl entries

This commit is contained in:
Thanakorn S
2026-05-23 16:46:35 +07:00
parent 363da054e0
commit eddb10aa22
12 changed files with 551 additions and 26 deletions
@@ -0,0 +1,34 @@
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require_once '../../../assets/utils/classes_ac/GlManager.php';
require_once '../../../assets/utils/classes_ac/GlQueryManager.php';
$doc_type = trim((string)($data['doc_type'] ?? ''));
$source_id = (int)($data['source_id'] ?? 0);
if (!$doc_type || $source_id <= 0) {
$answer['message'] = 'doc_type and source_id required.';
exit(json_encode($answer));
}
try {
$gl = new GlManager($pdo2, $company_id);
$entry = $gl->getBySource($doc_type, $source_id);
if (!$entry) {
$answer['success'] = 1;
$answer['output'] = null;
exit(json_encode($answer));
}
$query = new GlQueryManager($pdo2, $company_id);
$detail = $query->getJournalDetail((int)$entry['id']);
$answer['success'] = 1;
$answer['output'] = $detail;
} catch (Exception $e) {
$answer['message'] = $e->getMessage();
}
exit(json_encode($answer));
@@ -201,7 +201,7 @@ class GlQueryManager
) AS product_mapping_missing"
: ", 0 AS product_mapping_missing";
$where = ['i.company_id = :cid', 'i.doc_type = :doc_type', 'i.status != 4'];
$where = ['i.company_id = :cid', 'i.doc_type = :doc_type', 'i.status NOT IN (0, 4)'];
$params = [':cid' => $this->companyId, ':doc_type' => $doc_type, ':source_type' => $doc_type];
if ($date_from) { $where[] = 'i.issued_date >= :date_from'; $params[':date_from'] = $date_from; }
if ($date_to) { $where[] = 'i.issued_date <= :date_to'; $params[':date_to'] = $date_to; }
+92 -4
View File
@@ -345,10 +345,16 @@
<td class="py-3">${inv.due_date ? format_date(inv.due_date) : '<span class="text-muted">—</span>'}</td>
<td class="py-3 small">${formula_name}</td>
<td class="py-3">${invoice_status_badge(inv.status, inv.due_date)}</td>
<td class="py-3">
<a href="<?php echo $server_url?>expense/manage_purchase_invoice.php?id=${inv.id}" title="View">
<td class="py-3 text-nowrap">
<a href="<?php echo $server_url?>expense/manage_purchase_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('purchase_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>`;
});
@@ -387,10 +393,16 @@
<td class="py-3 fw-semibold text-info">${format_number(scn.grand_total, 2)}</td>
<td class="py-3 small">${formula_name}</td>
<td class="py-3">${invoice_status_badge(scn.status, null)}</td>
<td class="py-3">
<a href="<?php echo $server_url?>expense/manage_purchase_invoice.php?id=${scn.id}" title="View">
<td class="py-3 text-nowrap">
<a href="<?php echo $server_url?>expense/manage_purchase_invoice.php?id=${scn.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('supplier_credit_note',${scn.id})">
<i class="ti ti-book fs-5 text-secondary"></i>
</a>
<a href="javascript:;" title="Delete" class="link-danger" onclick="delete_invoice(${scn.id},'${escape_html(scn.invoice_number)}')">
<i class="ti ti-trash fs-5"></i>
</a>
</td>
</tr>`;
});
@@ -410,7 +422,83 @@
await retrieve_invoices();
}
});
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>'
);
}
});
}
</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"><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>
+24
View File
@@ -54,6 +54,30 @@
(new UsageGuard($pdo1, $company_id, $packages))->increment();
require_once '../../../assets/utils/notify_node.php';
notify_node('payment_posted', ['id' => $new_id], $company_id);
// Auto-post GL
try {
require_once '../../../assets/utils/classes_ac/posting/BasePosting.php';
require_once '../../../assets/utils/classes_ac/posting/PaymentPosting.php';
$sth = $pdo2->prepare("SELECT formula_id FROM td_payment WHERE company_id = :cid AND id = :id LIMIT 1");
$sth->execute([':cid' => $company_id, ':id' => $new_id]);
$row = $sth->fetch(PDO::FETCH_ASSOC);
$posting = new PaymentPosting($pdo2, $company_id);
$built = $posting->build($new_id, $row['formula_id'] ?: null);
$guard = new PostingWindowGuard($pdo1, $company_id);
$gl = new GlManager($pdo2, $company_id, $guard);
$pdo2->beginTransaction();
$gl->post('payment', $new_id, $built['formula_id'], $built['period'], $built['lines'], ['journal_date' => $built['doc_date']]);
$pdo2->commit();
notify_node('gl_posted', ['doc_type' => 'payment', 'id' => $new_id, 'action' => 'posted'], $company_id);
$answer['gl_posted'] = true;
} catch (Exception $e) {
if ($pdo2->inTransaction()) $pdo2->rollBack();
$answer['gl_posted'] = false;
$answer['gl_message'] = $e->getMessage();
error_log("auto_gl [payment#{$new_id}]: " . $e->getMessage());
}
exit(json_encode($answer));
}
+24
View File
@@ -54,6 +54,30 @@
(new UsageGuard($pdo1, $company_id, $packages))->increment();
require_once '../../../assets/utils/notify_node.php';
notify_node('receipt_posted', ['id' => $new_id], $company_id);
// Auto-post GL
try {
require_once '../../../assets/utils/classes_ac/posting/BasePosting.php';
require_once '../../../assets/utils/classes_ac/posting/ReceiptPosting.php';
$sth = $pdo2->prepare("SELECT formula_id FROM td_receipt WHERE company_id = :cid AND id = :id LIMIT 1");
$sth->execute([':cid' => $company_id, ':id' => $new_id]);
$row = $sth->fetch(PDO::FETCH_ASSOC);
$posting = new ReceiptPosting($pdo2, $company_id);
$built = $posting->build($new_id, $row['formula_id'] ?: null);
$guard = new PostingWindowGuard($pdo1, $company_id);
$gl = new GlManager($pdo2, $company_id, $guard);
$pdo2->beginTransaction();
$gl->post('receipt', $new_id, $built['formula_id'], $built['period'], $built['lines'], ['journal_date' => $built['doc_date']]);
$pdo2->commit();
notify_node('gl_posted', ['doc_type' => 'receipt', 'id' => $new_id, 'action' => 'posted'], $company_id);
$answer['gl_posted'] = true;
} catch (Exception $e) {
if ($pdo2->inTransaction()) $pdo2->rollBack();
$answer['gl_posted'] = false;
$answer['gl_message'] = $e->getMessage();
error_log("auto_gl [receipt#{$new_id}]: " . $e->getMessage());
}
exit(json_encode($answer));
}
+78 -8
View File
@@ -257,11 +257,14 @@
<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">
<td class="py-3 text-nowrap">
<a href="<?php echo $server_url?>finance/manage_payment.php?id=${r.id}" title="View payment" class="me-2">
<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_payment(${r.id})"><i class="ti ti-trash fs-5"></i></a>` : ''}
<a href="javascript:;" title="View GL Entry" class="me-2" onclick="show_gl_modal('payment',${r.id})">
<i class="ti ti-book fs-5 text-secondary"></i>
</a>
${can_delete ? `<a href="javascript:void(0);" class="link-danger" title="Delete" onclick="delete_payment(${r.id})"><i class="ti ti-trash fs-5"></i></a>` : ''}
</td>
</tr>`;
});
@@ -439,14 +442,24 @@
notes: $('#notes').val(),
},
onSuccess: function(res) {
if (res.gl_posted === true) {
toastr.success('GL entry posted automatically.', 'GL Posted');
} else if (res.gl_posted === false) {
toastr.warning('Payment posted, but GL posting failed: ' + (res.gl_message || 'Unknown error'), 'GL Skipped', { timeOut: 8000 });
}
var delay = res.gl_posted === false ? 3000 : 800;
if (prefill_billing_id > 0 && res.new_id) {
window.location.href = '<?php echo $server_url?>finance/manage_payment.php?id=' + res.new_id;
setTimeout(function() {
window.location.href = '<?php echo $server_url?>finance/manage_payment.php?id=' + res.new_id;
}, delay);
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();
setTimeout(function() {
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();
}, delay);
}
});
}
@@ -504,7 +517,64 @@
});
}
});
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>'
);
}
});
}
</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"><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>
+71 -4
View File
@@ -287,11 +287,14 @@
<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">
<td class="py-3 text-nowrap">
<a href="<?php echo $server_url?>finance/manage_receipt.php?id=${r.id}" title="View receipt" class="me-2">
<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>` : ''}
<a href="javascript:;" title="View GL Entry" class="me-2" onclick="show_gl_modal('receipt',${r.id})">
<i class="ti ti-book fs-5 text-secondary"></i>
</a>
${can_delete ? `<a href="javascript:void(0);" class="link-danger" title="Delete" onclick="delete_receipt(${r.id})"><i class="ti ti-trash fs-5"></i></a>` : ''}
</td>
</tr>`;
});
@@ -470,7 +473,14 @@
},
onSuccess: function(res) {
if (res.new_id) {
window.location.href = '<?php echo $server_url?>finance/manage_receipt.php?id=' + res.new_id;
if (res.gl_posted === true) {
toastr.success('GL entry posted automatically.', 'GL Posted');
} else if (res.gl_posted === false) {
toastr.warning('Receipt posted, but GL posting failed: ' + (res.gl_message || 'Unknown error'), 'GL Skipped', { timeOut: 8000 });
}
setTimeout(function() {
window.location.href = '<?php echo $server_url?>finance/manage_receipt.php?id=' + res.new_id;
}, res.gl_posted === false ? 3000 : 800);
}
}
});
@@ -529,7 +539,64 @@
});
}
});
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>'
);
}
});
}
</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"><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>
+45
View File
@@ -13,6 +13,13 @@
exit(json_encode($answer));
}
$posting_map = [
'invoice' => 'InvoicePosting',
'credit_note' => 'CreditNotePosting',
'purchase_invoice' => 'PurchaseInvoicePosting',
'supplier_credit_note' => 'SupplierCreditNotePosting',
];
try {
dbTransaction($pdo2, function($pdo) use ($id, $due_date, $company_id, $logging) {
$inv = new InvoiceManager($pdo, $company_id);
@@ -22,6 +29,44 @@
$answer['success'] = 1;
$answer['message'] = 'Invoice issued.';
// Auto-post GL for this document
$sth = $pdo2->prepare("SELECT doc_type, formula_id FROM td_invoice WHERE company_id = :cid AND id = :id LIMIT 1");
$sth->execute([':cid' => $company_id, ':id' => $id]);
$doc = $sth->fetch(PDO::FETCH_ASSOC);
if ($doc && isset($posting_map[$doc['doc_type']])) {
try {
$class_name = $posting_map[$doc['doc_type']];
require_once '../../../assets/utils/classes_ac/posting/BasePosting.php';
require_once '../../../assets/utils/classes_ac/posting/' . $class_name . '.php';
require_once '../../../assets/utils/notify_node.php';
$posting = new $class_name($pdo2, $company_id);
$built = $posting->build($id, $doc['formula_id'] ?: null);
$guard = new PostingWindowGuard($pdo1, $company_id);
$gl = new GlManager($pdo2, $company_id, $guard);
$meta = ['journal_date' => $built['doc_date'] ?? null];
$existing = $gl->getBySource($doc['doc_type'], $id);
$pdo2->beginTransaction();
if ($existing) {
$gl->replace($doc['doc_type'], $id, $built['formula_id'], $built['period'], $built['lines'], $meta);
} else {
$gl->post($doc['doc_type'], $id, $built['formula_id'], $built['period'], $built['lines'], $meta);
}
$pdo2->commit();
notify_node('gl_posted', ['doc_type' => $doc['doc_type'], 'id' => $id, 'action' => 'posted'], $company_id);
$answer['gl_posted'] = true;
} catch (Exception $e) {
if ($pdo2->inTransaction()) $pdo2->rollBack();
$answer['gl_posted'] = false;
$answer['gl_message'] = $e->getMessage();
error_log("auto_gl [{$doc['doc_type']}#{$id}]: " . $e->getMessage());
}
}
} catch (PDOException $e) {
$answer['message'] = 'Database error, please try again.';
http_response_code(500);
+63 -3
View File
@@ -256,11 +256,14 @@
<td class="py-3 fw-semibold">${format_number(inv.grand_total, 2)}</td>
<td class="py-3">${inv.due_date ? format_date(inv.due_date) : '<span class="text-muted">—</span>'}</td>
<td class="py-3">${invoice_status_badge(inv.status, inv.due_date)}</td>
<td class="py-3">
<a href="<?php echo $server_url?>po/manage_purchase_invoice.php?id=${inv.id}" title="View">
<td class="py-3 text-nowrap">
<a href="<?php echo $server_url?>po/manage_purchase_invoice.php?id=${inv.id}" title="View" class="me-2">
<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_invoice(${inv.id})"><i class="ti ti-trash fs-5"></i></a>` : ''}
<a href="javascript:;" title="View GL Entry" class="me-2" onclick="show_gl_modal('${inv.doc_type}',${inv.id})">
<i class="ti ti-book fs-5 text-secondary"></i>
</a>
${can_delete ? `<a href="javascript:void(0);" class="link-danger" title="Delete" onclick="delete_invoice(${inv.id})"><i class="ti ti-trash fs-5"></i></a>` : ''}
</td>
</tr>`;
});
@@ -286,6 +289,47 @@
});
}
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() {
await load_dept_cache();
populate_dept_filter('filter_dept');
@@ -297,5 +341,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"><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>
+8 -1
View File
@@ -364,7 +364,14 @@
invoice_id: invoice_id,
due_date: is_dn ? '' : due_date.split('/').reverse().join('-')
},
onSuccess: function() { retrieve_invoice(); }
onSuccess: function(res) {
retrieve_invoice();
if (res.gl_posted === true) {
toastr.success('GL entry posted automatically.', 'GL Posted');
} else if (res.gl_posted === false) {
toastr.warning('Document issued, but GL posting failed: ' + (res.gl_message || 'Unknown error'), 'GL Skipped', { timeOut: 8000 });
}
}
});
}
});
+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>
+8 -1
View File
@@ -337,7 +337,14 @@
id: invoice_id,
due_date: due_date ? due_date.split('/').reverse().join('-') : ''
},
onSuccess: function() { retrieve_invoice(); }
onSuccess: function(res) {
retrieve_invoice();
if (res.gl_posted === true) {
toastr.success('GL entry posted automatically.', 'GL Posted');
} else if (res.gl_posted === false) {
toastr.warning('Invoice issued, but GL posting failed: ' + (res.gl_message || 'Unknown error'), 'GL Skipped', { timeOut: 8000 });
}
}
});
}
});