Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
Refactor: Credit/Debit Memos → payment-shaped "Credits & Debits"
Goal
Stop modeling a memo as "a salesInvoice/purchaseInvoice row (invoiceType) with line
items." Model it like a payment: a party-level, non-cash settlement document =
party + amount + reason GL account + applications to invoices. No line builder, no
items/assets. One nav concept (a customer or supplier can have credits or debits),
sitting beside Payments — not buried in the AR/AP groups.
Working principle (per branch state)
The branch's payment/memo migrations (20260622*, 20260623*) are unmerged and not in
production, so we edit them in place and re-run a DB reset — no new add-then-remove
migrations. All schema changes below are edits to those existing files.
DECISIONS (locked)
- Parallel
memotable (Option B) — a credit/debit memo isn't literally a "payment". - Separate numbering — own sequences (
CR-…for credits,DR-…for debits), notPAY-. - Expose all four party×direction combos.
Target model (Option B: memo table)
- New
memotable mirroringpaymentbut withreasonAccount(FK→account, the returns/allowance/adjustment GL) instead ofbankAccount. Columns:id,memoId(seq),companyId,customerIdXORsupplierId(party → AR vs AP control),direction memoDirection(Credit|Debit),amount,reasonAccount,currencyCode,exchangeRate,status memoStatus(Draft|Posted|Voided),memoDate,postingDate,journalId, posted/voided audit,reference,notes, audit,customFields. - The four combos by control-account sign:
Combo Party Direction Effect Journal AR Credit Memo Customer Credit AR ↓ (owe us less) DR reason / CR AR AR Debit Memo Customer Debit AR ↑ (owe us more) DR AR / CR reason AP Debit Memo Supplier Debit AP ↓ (we owe less) DR AP / CR reason AP Credit Memo Supplier Credit AP ↑ (we owe more) DR reason / CR AP - Numbering: two sequences (
creditMemo→CR-%{yyyy}-%{mm}-,debitMemo→DR-%{yyyy}-%{mm}-), seeded inseed.data.ts+ migration backfill (replacing the retiredsalesCreditMemo/purchaseDebitMemo).
⚠️ The one place "all four" diverges from copy-payments: reducers vs increasers
Two combos reduce the party balance (AR Credit, AP Debit) — these behave like payments: they have an apply table and settle open invoices, or sit as an on-account credit. The other two increase the balance (AR Debit, AP Credit) — these are new open items the counterparty must settle (like a mini-invoice). Consequences:
- The apply table only renders for the reducer direction at creation.
- An increaser memo is itself a settleable open item, so
invoiceSettlement.targetmust generalize to point at an invoice or a memo (addtargetMemoId), so a payment or a reducer-memo can settle a debit/credit memo. (The reducer's apply list = open invoices + opposite-direction open memos.) - The subledger/tie-out signs each open memo: reducers −, increasers +.
This is the real added scope of exposing all four. It's the right general model (it's how Oracle AR/AP net any two open items), just more than a literal payments copy.
Settlement primitive
invoiceSettlementsource:paymentIdXORmemoId(replaces the retiredsourceSalesInvoiceId/sourcePurchaseInvoiceId).invoiceSettlementtarget:targetSalesInvoiceIdXORtargetPurchaseInvoiceIdXORtargetMemoId(new — lets credits/payments settle an increaser memo).
Subledger / GL consequences (important)
- Memos are no longer invoice rows, so they leave the
salesInvoices/purchaseInvoicesviews. The "memo signed negative as an open invoice" logic in the tie-out RPCs is removed; instead memos are subtracted as unapplied credits — the same way the tie-out already subtracts unapplied payment cash. (Cleaner and symmetric.) - Posting (
post-payment): forkind!='Cash', book DR reasonAccount / CR AR (Credit) or DR AP / CR reasonAccount (Debit) instead of the bank line. The per-application control/FX logic is unchanged. (Discount/write-off: memos don't carry them — keep the existinginvoiceSettlementcheck that gates discount/write-off to cash payments.)
Work plan
1. Schema (edit existing migrations, then reset)
20260622143012_ar-ap-payments.sql:- Remove
invoiceTypecolumns +salesInvoiceType/purchaseInvoiceTypeenums. - Remove
invoiceSettlement.sourceSalesInvoiceId/sourcePurchaseInvoiceId+ their checks; the source is justpaymentId(+ keep target sales/purchase XOR). - Add
payment.kind,payment.reasonAccount, makebankAccountnullable + a check (kind='Cash'⇒ bankAccount NOT NULL; else reasonAccount NOT NULL). - Repurpose the
salesCreditMemo/purchaseDebitMemosequences → a single memo sequence (or reuse thepaymentsequence; decide numbering, e.g.CR-/DR-by kind). - Keep the
'Credit Memo'/'Debit Memo'journalEntrySourceTypevalues for posting.
- Remove
20260623151204_invoice-balance-from-line-total.sql: drop the memo-settlement branch that readsourceSalesInvoiceIdmemos; balance = total − settled(payments only).20260623154712_tie-out-aging-from-view-total.sql: remove memo-as-negative-invoice signing; subtract unapplied memo credits alongside unapplied payment cash.seed.data.ts: drop the CM/DM sequences (or replace with the memo sequence).
2. Edge function post-payment
- Branch on
kind: cash → existing DR bank/CR control; credit/debit → DR/CRreasonAccountvs control. Reuse the signed-debit balance check + FX plug unchanged.
3. Service + validators (invoicing.service.ts / invoicing.models.ts)
- Extend
paymentValidator:kind,reasonAccount, makebankAccountconditionally required (refine onkind). RetirecreditMemoValidator/debitMemoValidator,creditMemoStatusType/debitMemoStatusType,isCreditMemoLocked/isDebitMemoLocked. getPayments: addkindfilter. AddgetMemos=getPaymentsfiltered to Credit/Debit (or just akindfilter param). RetiregetCreditMemos/getDebitMemos.insertSalesInvoice/insertPurchaseInvoice: remove theinvoiceTypememo branch.
4. UI (reuse, don't copy)
- Reuse
PaymentApplyTableverbatim (apply mechanics identical). PaymentForm: addkind; when memo, swap the Bank Account field for a Reason (GL account) field, hide cash-only bits. One component, conditional fields.- A
CreditsTable(or reusePaymentsTablewith akindfilter) for the list. - Detail route reuses
PaymentHeader/PaymentSummary/PaymentApplications/apply table.
5. Routes
- New
x+/credits+/(or reusepayments+with akindquery) +invoicing+/credits.tsxlist. Mirror the payment routes (new/$id/post/void/delete/applications.set). - Path helpers: add
credit*/memo*(or generalize the payment helpers with a kind).
6. Navigation
- One "Credits & Debits" entry beside Payments (its own group), filterable by party type + direction — mirroring the Payments list filters.
- Remove "Credit Memos" (AR group) and "Debit Memos" (AP group) entries.
7. RETIRE (delete)
x+/credit-memo+/(14 files),x+/debit-memo+/(14 files),invoicing+/credit-memos.tsx,invoicing+/debit-memos.tsx.ui/CreditMemo/(12 files),ui/DebitMemo/(12 files).creditMemo*+debitMemo*path helpers.- The retired validators/enums/service fns above.
8. Verify (reset + the SQL harness from the stress test)
crbnreset (picks up edited migrations + seed).- Re-run the controlled rolled-back SQL scenarios: derived balance/status, FX generated column, tie-out variance 0 with an unapplied memo credit (the new subledger path), AR + AP. Then a UI pass: create a credit (customer + amount + reason), apply, post.
Risks / open questions
- Numbering: keep separate memo numbers (CR-/DR-) or fold into PAY-? (Decision 1-adjacent.)
- Itemized returns are explicitly out of scope here (separate return/RMA concern).
- AR debit / AP credit memos (the other two combos) come free once
kind+party is the model — confirm we want all four exposed now or just the two we have today. - Editing the merged-in
20260623*migrations: they were authored by other work this branch absorbed — make sure reshaping them doesn't break their non-memo intent.