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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
Feature run: Reclassify payment discount accounts (customer contra-revenue, supplier contra-cost)
- Date: 2026-09-08
- Mode: fully-autonomous
- Request: "should we change customerPaymentDiscountAccount from an expense account to a contra revenue account?" → move forward as a feature. Cleanest structure, going-forward only (existing accounts/journals are throwaway test data — no back-compat migration for posted history). Include the supplier side. Keep both as dedicated accounts (reclassify, don't consolidate onto salesDiscountAccount).
- Phase plan: research [skip — done, .ai/research/customer-payment-discount-classification.md] · spec [run — crosses accounting + integrations + seed/chart-of-accounts; GL posting logic] · plan [run] · execute [run] · test [skip — user chose unit-tests-only; journal correctness verified via unit tests on build-payment-journal + db:check:datasets, no browser e2e] · self-review [run]
Decisions
- Design shape (pre-spec, delegated by user "do the cleanest thing"): reclassify both dedicated accounts rather than consolidate. customerPaymentDiscountAccount → contra-revenue (kept distinct from salesDiscountAccount 4020); supplierPaymentDiscountAccount → contra-cost/COGS reduction. Rationale: symmetric with the supplier side (which stays a dedicated account), resolves the memo(4020)-vs-payment(7030) divergence by making both customer paths contra-revenue, and preserves the dedicated early-payment-discount reporting line that SAP/NetSuite/BC all keep. — 2026-09-08
- Going-forward only for POSTED JOURNAL HISTORY: no migration rewrites already-posted journal lines. — 2026-09-08
- (User follow-up 2026-09-08) DO write a migration that updates the EXISTING chart-of-accounts rows (7030, 7020) classification and the default-account mappings, so existing companies' charts are corrected — not only new-company seed data. Posted journal history still untouched. — 2026-09-08
Autonomous spec resolutions (reviewed by human at PR, not mid-loop)
- Q1 classification: 7030 → class Revenue / accountType Income / parent
revenue(mirror 4020 Sales Discounts). 7020 → class Expense / accountType "Cost of Goods Sold" / parentcogs(contra-COGS). Keep numbers 7020/7030 and names. — codebase precedent (4020) + research. - Q1b renumbering to 4xxx/5xxx: DECLINED (out of scope). Numbers are labels; renumbering ripples into seed keys + integration mappings for cosmetic gain, and reclassify-in-place keeps accountDefault FK-by-id intact (no remap). Recorded as considered-and-declined.
- Q2 post-payment sign: adopt post-memo's class-driven pattern. index.ts resolves the selected discount account's
class; builder uses accountTypeFromClass(class) for the discount line instead of hardcoded "expense". SIDE unchanged (balance uses side, not accountType, so journal stays balanced). Move accountTypeFromClass from build-memo-journal.ts → lib/utils.ts; import in both builders. Root-cause fix, not a second hardcode. - Q3 post-memo: NO code change — already resolves reason-account class dynamically (index.ts:255-274). Reclassifying 7020 flows through automatically.
- Q4 lib/utils.ts debit/credit: unchanged; already handle revenue+expense. Only which abstract type the discount line uses changes (now class-derived).
- Q5 migration: new migration (reset-chart-of-accounts.sql is applied — never edit) UPDATEs account rows per company matched by number IN ('7020','7030'), setting class/accountType/incomeBalance/parentId (resolve new parent group id via name+isGroup subquery). Idempotent (fixed target state). accountDefault NOT remapped (same rows). Also edit seed.data.ts 7020/7030 records for new companies. Gates: db:migrate, generate:types, db:check:datasets, db:check:backups.
- Q6 (surfaced while grounding) integration sync payload (integrations.$id.tsx): unaffected — mapping is by account id, which does not change. No code change; verify in execute.
- Q7 (surfaced) companies that CUSTOMIZED 7020/7030 or repointed the default: out of scope — user confirmed existing accounting is throwaway test data; migration matches by number 7020/7030. Documented limitation.
- Plan gate (🛑) auto-approved (autonomous mode): plan is grounded in verified account schema (companyGroupId tenant col, parentId/isGroup, enum values), covers every spec AC, uses only scoped typecheck, and leaves DB apply to the user. No human blocker. — 2026-09-08
Phase log
- research: complete — .ai/research/customer-payment-discount-classification.md (SAP/NetSuite/BC/QuickBooks/Xero + ASC 606/IFRS 15; codebase grounding)
- spec: complete — .ai/specs/2026-09-08-payment-discount-account-classification.md (all OQs resolved autonomously; spec gate passed)
- plan: complete — .ai/plans/2026-09-08-payment-discount-account-classification.md (7 tasks; verified account schema uses companyGroupId not companyId, parentId/isGroup, enum values)
- execute: complete — all 7 tasks. Edge-function tests 41 passed (34 post-payment incl. new AP contra-COGS + flipped AR contra-revenue; 7 post-memo). deno check on changed files: 0 new errors (only 6 pre-existing postgres/index.ts env errors). @carbon/database typecheck green. Migration 20260909014032 created. Verification deviations: (a) deno test run with --no-check due to a pre-existing typecheck failure in the unmodified lib/postgres/index.ts; (b) db:check:datasets + generate:types + db:migrate + db:check:backups are DB-dependent and the local stack is down → USER STEP (migration makes no schema change, so generate:types is a no-op). NOT committed (per no-auto-commit; feature stops at gated PR).
- self-review: complete — no Must-fix. Risks: silent "expense" fallback if a caller omits class; confirm sync/reports read the flipped AR discount line by natural balance (runtime); DB gates still need local run; deno test used --no-check for a pre-existing postgres/index.ts typecheck failure. Suggestions: throw when discount>0 but no class; sync supplier default too. Docs: nothing stale; optional lessons entry + move spec to implemented/ on merge.
Follow-up (2026-09-08, after user screenshot)
- UI fix: AccountDefaultsForm "Customer Payment Discounts" field badgeType "Expense" → "Revenue". badgeType drives both the class badge AND the account-picker filter (accountOptions[badgeType], filtered by account.class), so with the account reclassified to Revenue the Expense-filtered picker went empty (user's screenshot). Supplier stays "Expense" (7020 is still class Expense/COGS). One-line change; type-safe ("Revenue" already used by 5 other fields). Spec UI Changes section corrected (was wrongly "None"). Branch is now
customer-payment-discount-account.
Follow-up 2 (2026-09-08, renumber): reclass + renumber into correct blocks
- User: a Revenue/COGS account shouldn't keep a 7xxx number. Renumbered 7030→4040 (Revenue block) and 7020→5080 (COGS block).
- KEY FINDING: a fresh
crbn resetseeds the chart from seed.data.ts via bootstrap.ts (path b), NOT from reset-chart-of-accounts.sql's DO block — on a wiped reset there are zero companyGroups when migrations run, so that DO block inserts nothing. So seed.data.ts is the authoritative edit for reset; editing the old reset-chart migration is inert AND a never-edit violation → left untouched. - Edits: (1) seed.data.ts — moved the two records into the revenue/cogs blocks, renumbered 4040/5080 (key==number kept for the seed-company subsidiary path which keys its id-map by number), updated accountDefaults mapping to 4040/5080. (2) forward migration 20260909014032 — folded renumber into the existing reclassify UPDATEs (SET number + class/type/parent), matched by OLD number, idempotent, COALESCE parent guard, scoped by companyGroupId; accountDefault untouched (FK by id survives renumber). (3) reset-chart-of-accounts.sql — NOT edited.
- Verified: no 7020/7030 refs remain anywhere in source (only the migration's WHERE on old numbers + comments); @carbon/database typecheck green; 4040/5080 confirmed free in both seed + reset-chart. Edge-function posting code unaffected (keys off account id/class, not number) — no test change. AccountDefaultsForm badgeType fix (customer→Revenue) from follow-up 1 still correct.
- Spec updated (target table, design-decision row reversed, data-model, ACs, OQs, changelog).
- STILL NOT applied/committed. User to
crbn reset(fresh dev company gets 4040/5080 from seed.data.ts) orpnpm db:migrateon an existing DB (renumber migration runs).
Outcome
- Feature implemented on branch
customer-payment-discount-account(uncommitted, per no-auto-commit). 6 source files + 1 migration (20260909014032) + .ai research/spec/plan/run. Edge-function tests 41 passed. NOT committed — feature run stops at a gated PR; awaiting user to commit (via /check-and-commit) and run the DB-dependent gates (pnpm db:migrate, db:check:datasets, db:check:backups).