Auto-post expenses and invoice payments to accounts ledger

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-16 12:04:15 +07:00
parent d75fe6ed95
commit 3a095851e9
9 changed files with 430 additions and 70 deletions
+8 -2
View File
@@ -259,7 +259,8 @@ export async function postInvoicePaymentTransaction(
total: invoices.total,
currency: invoices.currency,
issueDate: invoices.issueDate,
invoiceNumber: invoices.invoiceNumber
invoiceNumber: invoices.invoiceNumber,
direction: invoices.direction
})
.from(invoices)
.where(eq(invoices.id, invoiceId))
@@ -273,6 +274,11 @@ export async function postInvoicePaymentTransaction(
.limit(1);
if (!acct) throw new Error(`postInvoicePaymentTransaction: account ${paymentAccountId} not found`);
// outgoing = we billed a customer → cash in (credit).
// incoming = we owe a supplier → cash out (debit).
const sign = inv.direction === 'outgoing' ? 1 : -1;
const signedAmount = sign * Number(inv.total);
await dbOrTx
.delete(companyAccountTransactions)
.where(eq(companyAccountTransactions.sourceInvoiceId, invoiceId));
@@ -281,7 +287,7 @@ export async function postInvoicePaymentTransaction(
accountId: paymentAccountId,
companyId: acct.companyId,
type: 'invoice_payment',
amount: Number(inv.total).toFixed(2),
amount: signedAmount.toFixed(2),
currency: inv.currency,
occurredAt: new Date(inv.issueDate),
description: `Invoice ${inv.invoiceNumber}`,