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:
@@ -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}`,
|
||||
|
||||
Reference in New Issue
Block a user