automate and view gl entries
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -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
@@ -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
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user