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