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.
Lessee leases — strict self-review fixes
Source: the strict self-review of lessee-leases-purchasing (2026-10-12). Every
Must fix, Risk, Suggestion and Docs item is in scope; the user asked to "fix it
all". Work is split into packages that own DISJOINT files so they can run in
parallel. Phase 2 starts after Phase 1 is merged into the working tree.
Shared rules for every package:
- Read root
AGENTS.md,.ai/lessons.md, and the AGENTS.md /.claude/rulesfor the files you touch. No JSDate; no raw rounding; companyId on every query; Kysely for multi-row writes; log before throw; SPDX header on new files. - Touch ONLY the files your package owns. Other agents edit other areas at the same time, so a typecheck error in a file you do not own is not yours to fix — report it.
- Never touch
apps/erp/app/modules/accounting/ui/useAccountingSubmodules.tsxorpackages/react/src/Button.tsx(the user's uncommitted work). - Red → green: every behaviour fix gets a test that fails without it.
- Do not commit. Leave changes in the working tree; the orchestrator runs the gates and commits.
- Database tests:
export SUPABASE_DB_URL=$(grep -E '^SUPABASE_DB_URL=' .env.local | cut -d= -f2-).
Decisions (made by the orchestrator; do not re-litigate)
- D1 (#9) Lease tables over the API: keep
purchasing_*RLS for Draft editing, and add BEFORE INSERT/UPDATE/DELETE triggers onlease,leaseLine,leasePaymentthat, for an API role (coalesce(current_setting('role', true), 'none') IN ('anon','authenticated'), the idiom incheck_accounting_config_locked), refuse any write whose lease is not Draft — EXCEPT an Active/ended lease's non-accounting fields onlease(name,description,assignee,noticeDate,customFields,updatedAt,updatedBy). For an API role they also refuse any change toleaseLine.classificationOverride,classificationOverrideReason,lesseeClassification,classificationInputs,initialLiability,initialRouAsset,rouAssetCost,status,fixedAssetId, and tolease.status,commencementJournalId. The override route already writes with the service role. Server functions (Kysely, not an API role) are unaffected. - D2 (#6) An asset whose ownership transfers may be "purchased" with no purchase
option: price 0 (Purchase refuses only when neither
purchaseOptionAmountnorownershipTransfers). The UI offers the action for such an asset. - D3 (#2) Modify keeps escalation anniversaries on the ORIGINAL commencement:
rows are generated from
lease.commencementDateand only those from the effective date on are kept; the payment entered in Modify is the payment AT the effective date (the modal pre-fills the current escalated rate). - D4 (#4) Activation refuses a Skipped row due on or after
max(commencementDate, openingAsOf ?? commencementDate);invoiceFromDateis only for payments made before the opening balance. - D5 (#3) The opening balance skips exactly the payments the schedule already
deducted before the as-of date: decide by the schedule month a row belongs to
(
paymentMonthIndex), not bydueDate. - D6 Lease Liabilities current portion = principal of the next 12 schedule lines
after as-of (
slice(0, 12)), not a date window. - D7 Lease owner, notice date, name and notes stay editable after activation
(
update.tsx+LeaseProperties); every ASC 842 term stays Modify-only. - D8 A classification override can be removed while Draft (intent on the classification route; service role write; audit-logged like setting it).
- Not changed (explain in the PR): the GL current/non-current split of the lease liability stays a reporting reclass (the Lease Liabilities report), not a second GL account.
Phase 1 (parallel)
A — post-lease server function
Owns packages/server-functions/src/post-lease/**, new packages/server-functions/src/lib/* files it creates, and the void/journal call sites it rewires in post-rental-agreement/, post-charge/.
- Must: D3 Modify escalation; D5 opening balance; D4 Skipped-row refusal;
Terminate/Purchase refuse a date not after commencement; Cancel refuses once an
asset is not Active or any
leaseEventexists; D2 purchase with ownership transfer at price 0. - Risks: Modify with Skipped rows on/after the date (refuse with a clear 400);
29th–31st leases — generate rows and schedule as months of the ORIGINAL
commencement so later boundaries line up;
purchaseLinesnegative cost →InvalidInputError; removecancel's Draft-delete branch (ERP uses$id.delete); cancel readsjournalPostingStatusdirectly. - Suggestions: drop unused
todayparams; throw instead of?? "Operating"on Active lines; removeas unknown as Json/as LeaseAccountcasts; carryserialNumberonto the purchased fixed asset; extract a shared "void journal as negated copy" helper intosrc/libused bypost-lease,post-chargevoid and the reimbursement void IF behaviour is identical (tests green), and batchvoidLeaseJournalreads (no per-journal query loop). - Tests: mid-month advance fixture before the cutover; escalation + modify;
cancel after purchase refused; terminate asserts journal amounts and gainLoss;
a run-posted row (with
runLineId) for cancel's refusal.
B — purchase invoice posting, legacy, jobs, digest link
Owns packages/server-functions/src/post-purchase-invoice/**,
packages/server-functions/src/activate-accounting/legacy/purchase-invoice.ts,
packages/jobs/**, packages/notifications/**, packages/lib/src/events.ts,
apps/erp/app/routes/api+/link.ts, apps/erp/app/components/Layout/Topbar/Notifications.tsx.
- Must: a capitalized Lease line debits Lease Liabilities with the NOMINAL amount
only; its tax / shipping / header-freight share go to Operating Lease Expense
(Short-Term/variable lines unchanged); compare
nominalBaseCostwithpayment.amountusingequalsfrom the precision API. - Risk: refuse posting unless
payment.purchaseInvoiceLineId === invoiceLine.id. - Suggestion: extract
lease-lines.ts(resolveLeasePostingLine) besideposting-lines.ts, used by posting AND the legacy activate-accounting path (so the legacy path gains the Draft/Cancelled/other-lease/amount checks); type the lease map with DB enums; drop theprojectIdcast. - Digest: carry an explicit document kind in the recurring-invoicing digest
(
documentType), route on it inlink.tsandNotifications.tsx(nopi_sniffing); when a digest mixes sales review items and lease drafts, send the lease drafts as their own notification. - Jobs: named tolerance instead of
> 0.005; one exported ended/billable status constant (define it in jobs; do not edit create-lease-invoices — phase 2 D wires it); moveremindLeaseDatesloading intolease-reminders.ts. - Tests: posting a Lease line per classification, tax on a capitalized line,
amount mismatch, Draft/Cancelled lease, row of another invoice line; void
returns rows to Pending;
stampLeaseRemindersdate-moved guard; expireLeases.
C — database + lease math
Owns packages/database/** (migrations, manifest, run-journals.ts,
accounting-cutover/open-items.ts, generated types), packages/utils/src/lease-math*.ts,
packages/utils/src/lessor-lease*.ts.
- Must: D1 triggers (new migration; event-system / RLS rules per
.claude/rules/authz-manifest.md— triggers are ordinary migration DDL). - Risks: cutover open items add back payments due by the last pre-cutover line
that are Pending or on an invoice not posted before the cutover (and the same
gap in
getLeaseNetInvestmentItemsif confirmed);leasePayment.purchaseInvoiceLineIdrelease — AFTER DELETE trigger onpurchaseInvoiceLinereturning matching rows to Pending (+ CHECK(status = 'Invoiced') = (purchaseInvoiceLineId IS NOT NULL)if the data allows);leasesview:nextDueDateonly for Active/ended (not Draft/Cancelled),classification/paymentAmount/assetCountover Active assets (ended lease: all), oneLEFT JOIN LATERALaggregate instead of correlated subqueries. - Suggestions: CHECKs (
fairValue > 0,economicLifeMonths > 0,purchaseOptionAmount >= 0,annualEscalationPercent > -100); composite FK(leaseLineId, leaseId, companyId)onleasePayment,leaseScheduleLine,leaseEvent(needs a unique(id, leaseId, companyId)onleaseLine);revenueRecognitionSchedulelease pairing CHECK;leaseEvent.fixedAssetIdFK;run-journals.ts: non-optional lease maps and throw when a lessee row's lease or asset is missing; edit20261011163211section 9 to delete only the runs the release emptied (the branch is unmerged). - lease-math: Feb 29
isTermWithinTwelveMonths; a 0-month term refused with a clear message (not "needs a whole number of months"); leftover days get their share of cost (no cost-free final part-month); sharemonthlyRate/ PV withlessor-lease; tests for escalation, initial direct costs, quarterly, leftover days, financeamortizationMonths≠ term, Feb 29, 0 months; remove theas nevercasts in tests. - Run
pnpm db:migratethenpnpm run generate:types.
E — accounting module, report, docs
Owns apps/erp/app/modules/accounting/** (except ui/useAccountingSubmodules.tsx),
apps/erp/app/routes/x+/accounting+/**, apps/erp/app/routes/x+/revenue-recognition-run+/**,
apps/erp/app/routes/x+/reports+/lease-liabilities.tsx, packages/server-functions/src/lib/run-type.ts,
packages/ee/src/accounting/core/models.ts (read only unless needed),
.claude/rules/accounting-sync-handlers.md, apps/erp/app/modules/accounting/AGENTS.md,
.ai/specs/**, docs/content/docs/reference/accounting.mdx.
- Risk: D6; report requires
view: purchasingtoo (or shows a notice); a "lease run not posted through X" banner when the latest posted month is before as-of; use the asset's own end (its Termination/Purchase event date) notlease.endDate; pagegetLeaseLiabilitiesreads and limit them to kept assets. - Suggestions: one client-safe run-type descriptor (filter, label, list path)
replacing
scheduleRowsOfRunType, the three labels and four path ternaries, and the hand-written unions (use the DB enum); fixdeleteRevenueRecognitionRunmessage; move the lease liabilities helpers toaccounting/lease-liabilities.ts; drop the$runId.tsxbreadcrumb cast. - Close checklist text: drop "rent to accrue" from revenue recognition.
- Tests: lease run post → reverse stamps and clears
leaseScheduleLine;getLeaseLiabilitiesmapping; mid-month and leap-year as-of; truncated termination schedule. - Docs: sync-handlers rule (
Lease Run, whatLeasecarries, the sync-policy migration); accounting AGENTS.md (unpost path exists, nav Closing, test scope); spec line 443Lease Run; scope note on2026-07-04-lease-accounting.md.
Phase 2 (after Phase 1)
D — ERP lease routes, service, models, server + row cutter
Owns apps/erp/app/routes/x+/lease+/**, apps/erp/app/routes/x+/purchasing+/leases.tsx,
apps/erp/app/modules/purchasing/purchasing.{service,models,server}.ts (+ tests),
apps/erp/app/routes/api+/mcp+/lib/mcp-blocked-tools.ts,
packages/server-functions/src/create-lease-invoices/**, and the Modify row-cutting
call in post-lease/lifecycle.ts.
- Must: payment add/delete routes require
create/delete; delete checks a row was deleted. - Risks: write + recut in ONE Kysely transaction for all four routes; block or
fix the MCP
upsertLeaseLine/updateLeaserecut gap; invoice-from follows commencement in the Details step (one sharedresolveLeaseDates); page the lease reads past 1000 rows; ISO-date refine on lease date fields + guard refine 3;new.tsxlogs and flashes a failed settings read;$id.invoice.tsxlogs. - D7 (
update.tsxallows the four non-accounting fields while not Draft); D8 remove-override intent. - Suggestions: drop the route-level Draft re-checks; one export of the recut
field sets; setup index →
assets; dropNumber(...)on parsed numerics; early return on no lines;leaseRefusalwithout the cast; deletes report not-found; validator tests (terminate, purchase, classification, malformed date, invoice-from). - Row cutter: ONE
cutLeasePaymentRowsused byregenerateLeasePayments,rollOpenEndedLeasePaymentsand Modify; wire B's status constant into create-lease-invoices.
U — lease screens
Owns apps/erp/app/modules/purchasing/ui/Leases/**, and moves pure lifecycle math
into packages/utils/src/lease-math.ts / a client-safe export shared with
post-lease/lessee.ts.
- Must: header shows Purchase Invoices whenever there are invoices and Invoice Now unless Draft/Cancelled.
- D2 / D3 / D7 / D8 in the UI; the Purchase modal refuses a price the server refuses; the Modify modal warns on a Short-Term asset past 12 months.
- Risks / suggestions: one shared lifecycle math (carrying, add-back, measuring,
journal legs) used by UI and server;
LeaseSummaryreuses the liability helper (moveleaseAssetLiabilitytoutils.ts); payment / asset count over Active assets;LeasePropertiesdrop the dead Draft wiring (keep D7 fields); remove the dead header Activate path; fix the "Assets card" hint; splitLeaseClassificationPreview.tsxand reuseLeaseJournalPreview; renameuseAssetPreviews;<Plural>for the two hand plurals; oneuseLeaseFrequencyLabel; typeLeasePaymentInvoiceLinks.statuswith the enum (drop both casts), drop the other casts; surface real errors frompreviewLesseeClassification; trim unused barrel exports andLeaseListItem; promote one money component shared withRentalMoney.