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.
Several assets per lease — implementation plan
Spec: .ai/specs/2026-10-11-lessee-leases.md → Addendum (A1–A8)
Research: .ai/research/2026-10-11-lessee-lease-accounting.md → Addendum
Closest sibling: rental agreement (rentalAgreement + rentalAgreementLine; setup step Units = RentalUnitsGrid)
Review findings folded in: .ai/runs/2026-10-11-lessee-leases-review.md (M1–M17)
Progress
- W1: Migration —
leaseLine, line ids on payments / schedule / events,leasesview (M17), RLS, types - W2: Lease math — month counting from the 29th–31st (M15), Short-Term test on the full term (M16)
- W3: ERP models, service, server — line CRUD, rows per line, payment and invoice-line guards (M10, M11, M14)
- W4:
post-leaseactivate, cancel, opening balance per line (M7, M8) - W5:
post-leasemodify per lease, terminate and purchase per line (M3–M6) - W6: Invoices and posting per line
- W7: Run journals, cutover open items, Lease Liabilities report per line
- W8: Daily job — expiry and reminder stamps (M1, M2)
- W9: UI — Assets step, Payments and Accounting per asset, lease page per asset, route fixes (M9, M12, M13), dead Draft UI removed
- W10: Docs, AGENTS.md, translations, full verification, browser test
Dependencies
W1 first (types). W2 independent. W3–W8 need W1. W9 needs W3–W5. W10 last.
W1: Migration
Files: new migration lease-lines; packages/database/src/authz/manifest.ts; audit.config.ts; packages/jobs/src/backups/id-refs.ts; types.ts (generated).
- Create enum
leaseLineStatus(Active,Terminated,Purchased,Expired). A Draft lease's lines areActive; the lease status says Draft. - Create
leaseLine(id('lsln'),companyId,leaseIdFK cascade,descriptionNOT NULL,serialNumber,locationId,paymentAmountNUMERIC NOT NULL DEFAULT 0 CHECK ≥ 0,fairValue,economicLifeMonths,ownershipTransfers,specializedAsset,purchaseOptionAmount,purchaseOptionReasonablyCertain,status,lesseeClassification,classificationInputs,classificationOverride+ reason CHECK,initialLiability,initialRouAsset,rouAssetCost,fixedAssetIdFK set null,sortOrder, audit columns). - Backfill one line per lease from the header's asset columns (
description=lease.name). - Add
leaseLineId(FK cascade) toleasePayment,leaseScheduleLine,leaseEvent; backfill from the lease's one line;NOT NULLon all three: a Modification writes one event per asset. Replace the payment unique index with(companyId, leaseLineId, dueDate, paymentType)and the schedule unique with(companyId, leaseLineId, periodDate). - Drop the moved header columns (
paymentAmount,fairValue,economicLifeMonths,ownershipTransfers,specializedAsset,purchaseOptionAmount,purchaseOptionReasonablyCertain,lesseeClassification,classificationInputs,classificationOverride,classificationOverrideReason,initialLiability,initialRouAsset,rouAssetCost,fixedAssetId). AddCHECK ("discountRate" >= 0). - Recreate
leases:supplierName,assetCount,paymentAmount(sum of lines),classification(the one shared value, elseMixed),nextDueDate,currentLiability= per Active line, the closing liability of its lastleaseScheduleLinewithpostedAtset, else itsinitialLiability; Draft, Cancelled and ended lines count 0 (M17). - Authz:
leaseLinelikeleasePayment; generate the RLS migration. Audit entityleaseaddsleaseLine.
Verify: pnpm db:migrate && pnpm run generate:types; psql shows one line per existing lease and no NULL leaseLineId.
W2: Lease math
monthsBefore/monthsThrough/scheduleSpan/leaseMonthStartingcount lease months withaddMonths(clamped) — one helperleaseMonthIndex(start, date).classifyLesseeLeaseShort-Term test:endDate <= addMonths(commencement, 12) − 1 day, passed as a flag, not rounded-down months.- Tests: start on the 31st (Advance and Arrears points), 12 months + 15 days is not Short-Term.
W3: ERP models, service, server
leaseValidatorloses the asset fields;leaseLineValidator(description, serial, location, payment amount, fair value, economic life, switches, purchase option).- Service:
getLeaseLines,upsertLeaseLine(Draft only),deleteLeaseLine(Draft only);upsertLeasePaymentmatches the row's ownleaseId(M10) and sets Pending / Skipped frominvoiceFromDate; payments carryleaseLineId. regenerateLeasePaymentscuts rows per line with an amount above 0 (M14); a line with no amount has no rows.- Invoicing: strip
leasePaymentIdfrom a new line and keep the stored one on update (M11).
W4: post-lease activate, cancel, opening
- Activate per line: PV, classification (per line override), schedule and run rows per line; one journal per lease with legs per line (
documentLineReference= line id). Refuse a lease with no line or no row. - Opening path: per line; rows due before the as-of date become
Skipped(M8); schedule lines before the date getpostedAt. - Cancel voids every
Leasejournal of the lease (M7) and refuses after an Invoiced row or a run-posted row.
W5: Modify, Terminate, Purchase
preparerefuses a date inside a payment period (M6) and loads carrying amounts per line.- Modify (lease): new end date, rate, frequency, escalation and a payment amount per Active line; remeasure each line; one journal.
- Terminate (line): payload gets
leaseLineId; delete that line's Pending rows after the date and its option row, counting the option in the carrying liability (M4); lease becomes Terminated when no Active line is left. - Purchase (line): reuse an Invoiced option row or refuse cleanly (M5); price not counted twice (M3); fix the test.
W6–W8
- W6: invoice line description names the asset; posting reads the line's classification; refuse a Lease line of a Cancelled lease.
- W7: run journal lessee rows tag the line; cutover open items and the report per line.
- W8: expiry when every line's last
closingRouAssetis 0 (M1); stampr.datewhere unchanged (M2).
W9: UI
- Wizard steps Details → Assets → Payments → Accounting → Review (
leaseSetupSteps). Assets =LeaseAssetsGridcopied fromRentalUnitsGrid(add, delete, cells: description, serial, location, payment, fair value, economic life). - Payments: terms + rows grid with an Asset column. Accounting: classification per asset (like
RentalAgreementAccounting's unit grid) + switches per asset. - Lease page: Assets card with per-asset Terminate / Purchase; Modify takes a payment per asset.
- M9 (
$id.statusrefuses a Draft), M12/M13 (requestReferrer), remove the Draft branch ofLeasePaymentsand the$id.payments.$paymentIdedit route.