Skip to content

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.

43.2 KB
Raw Blame

Contracts — design interview and Phase A run log

The grill that designed contracts (.ai/specs/implemented/2026-09-22-revenue-recognition-rentals-and-contracts.md Part III), then the execution log of .ai/plans/implemented/2026-09-22-revenue-recognition-rentals-and-contracts.md Part III. Phase B has no run log; its notes are in .ai/plans/implemented/2026-09-22-revenue-recognition-rentals-and-contracts.md Part IV ("Changes during the build").

Design interview (Q1–Q11, U1–U4, G1–G9)

Complete. Q1–Q11 produced the Subscriptions draft; U1–U4 (unify with rental invoice automation) and G1–G9 (generalized AR contracts) re-scoped it. All carried into .ai/specs/implemented/2026-09-22-revenue-recognition-rentals-and-contracts.md Part III.

Resolutions carried into .ai/specs/implemented/2026-09-22-revenue-recognition-rentals-and-contracts.md Part III (originally drafted as 2026-10-02-subscriptions.md). Research: .ai/research/subscription-recurring-invoicing.md.

Settled by the codebase (no question)

  • Foreign currency: Service-line deferral in post-sales-invoice already posts in base (amountBase: charges.amounts.salesRevenueBase), so subscriptions are not restricted to base currency the way rental agreements are.

  • From the Rillet / Stripe API review (.ai/research/subscription-recurring-invoicing.md):

    • Lines carry their own start/end dates, defaulting to the subscription's (Rillet item dates, Stripe per-item periods) — needed for mid-term add-ons and amendments.
    • A change is an amendment line pointing at the line it replaces (amendsLineId, Rillet amending); the replaced line is closed at the effective date, never edited once billed.
    • Adjustment credits are computed from the amount actually billed for the period, not the current rate (Stripe flexible billing mode).
    • An invoice line points at its subscription line and billing period and flags adjustments (Stripe parent.subscription_item_details; Rillet's missing link is the gap to avoid).
    • An upcoming-invoice schedule preview on the subscription before and after activation (Rillet preview-invoice-schedule), computed by the same pure function that cuts periods.
    • The subscription's customer PO reference is copied onto every invoice (salesInvoice.customerReference; Rillet purchase_order_number).
    • Carbon is the system of record for the subscription; "Post and send via Stripe" sends each posted invoice through the existing Connect path (one-off Stripe invoices), never a Stripe Subscription object — two billing engines for one contract would double-bill.
    • Revenue is spread by day over each line's service period (Carbon's existing spreadStraightLine, = Rillet DAILY); an even-per-month pattern (Rillet EVEN_PERIOD) is out of v1.
    • Syncing subscription invoices to Rillet as recurring revenue (today Carbon pushes every product ONE_TIME, include_in_arr_mrr: false) is a follow-up, out of v1.

Resolved with the user

  • Q1 — What can a subscription line bill? — Answer: Service items only. Recurring physical goods (shipping, stock) are recurring sales orders, a different feature; usage-based (metered, in arrears) billing is out of v1.
  • Q2 — Billing frequencies — Answer: Daily, Weekly, Monthly, Quarterly, Annually. (Whether this is the invoice rhythm or the unit the rate is quoted in — see Q2b.)
  • Q2b — Price unit vs invoice rhythm — Answer: Separate (BC "billing base period" vs "billing rhythm"; the rental split of line rateUnit vs agreement billingCycle). Each line carries a rate per Day / Week / Month / Quarter / Year; the subscription is invoiced every Week / Month / Quarter / Year. €10/day invoiced monthly = one invoice per month for the days in it.
  • Q3 — Period alignment — Answer: A choice per subscription, Anniversary by default (periods run from the start date, no proration) or Calendar (aligned to the 1st of the month / quarter / year, first period prorated by days). Research consensus.
    • Settled by research + codebase: an anniversary on the 29th–31st clamps to the month's last day and returns to the anchor day when it exists (Stripe's rule; @internationalized/date .add({ months }) clamps, .claude/rules/date-handling.md).
  • Q4 — Invoice grouping — Answer: One invoice per subscription per run (Stripe, Odoo, Acumatica; rental agreements today). Several services on one invoice = several lines on one subscription. Per-customer consolidation is out of v1.
  • Q5 — Invoice automation — Answer: A per-subscription setting, Draft by default: Draft for review (a person posts and chooses Email / Stripe, as rentals today) / Post and email / Post and send via Stripe (hosted invoice + payment link). The subscription stores the invoice contact, and for Stripe the linked Stripe customer, confirmed once at activation (the existing preflightStripeSend link step).
    • Settled by the codebase: mail already goes out from SMTP_FROM with the poster as replyTo (packages/lib/src/email.server.ts, send-email.ts). An automatic send uses the subscription's sales person as reply-to, falling back to the user who activated it.
  • Q6 — Mid-term changes — Answer: Dated changes, prorated by day (NetSuite change orders, Stripe, Odoo upsell). A quantity or rate change carries an effective date; a period already billed in advance gets a prorated adjustment line on the next invoice (increase = charge, decrease = credit); unbilled and arrears periods are simply priced by day across the change. A billed period is never rewritten (the rental adjustment-row precedent).
  • Q7 — Cancellation — Answer: Cancel asks for an end date, defaulting to the end of the current period (nothing to credit). An earlier date offers a Credit unused time option that credits the unused prepaid days, prorated (Chargebee credit options, F&O issue credit, Stripe cancel_at_period_end as the default).
  • Q7b — Form of a cancellation credit — Answer: A Draft customer credit memo (the existing memo), linked to the subscription, applied or refunded like any memo. Mid-term reduction credits (Q6) still ride the next invoice as negative lines.
  • Q8 — Renewals and escalation — Answer: In v1. A subscription is open-ended or has a term (e.g. 12 months); at term end it either Renews automatically (same term length, every line's rate raised by an optional uplift %) or Ends. Renewal is applied by the daily job as a dated rate change effective the new term's first day (the Q6 mechanism), so a renewed term never rewrites a billed period (NetSuite Extend + Uplift, BC subsequent term + price update, Chargebee contract terms).
  • Q9 — Origin — Answer: Subscriptions are created standalone (like rental agreements), and Create Subscription on a sales order turns chosen Service lines into one.
  • Q9b — Sales-order lines after conversion — Answer: The user picks which Service lines become the subscription; those lines are marked billed-by-subscription and drop out of the order's own invoicing (no double billing). The order's other lines (e.g. a one-off setup fee) invoice from the order as usual.
  • Q10 — Migrating contracts already billed elsewhere — Answer: A subscription has an optional Billed through date. It keeps its real start date and term; periods ending on or before that date are recorded as billed elsewhere (no invoice, no revenue schedule), and the first Carbon invoice is the first period after it (Rillet REVENUE_RECOGNITION_ONLY, F&O stubbing). Deferred revenue already booked in the old system arrives through the opening balance, not through the subscription.
  • Q11 — When an amendment takes effect — Answer: A choice per amendment, From the change date (prorated by day — the Q6 behaviour, and the default) or From the next billing period (no proration). Rillet effective_from AS_OF_AMENDMENT_DATE / END_OF_CURRENT_BILLING_CYCLE; Stripe proration_behavior.

Unification with rental invoice automation (2026-10-02, after the spec was written)

Context: .ai/specs/implemented/2026-09-22-revenue-recognition-rentals-and-contracts.md Part II + its plan (not started, 23 open tasks) independently design the same post / send / hold / notify layer for rentals.

  • U1 — One layer or two? — Answer: Unify. One recurring-invoicing layer: billing sources (rental agreements, subscriptions) generate periods and draft invoices and declare their own holds; ONE shared pipeline (claim, hold checks, post, send, sentAt / sendError, VOID re-bill holds, digest) in packages/jobs/src/invoicing/, one mode list, one Settings → Invoicing page. The rental automation spec + plan are renamed to source-agnostic names before any code exists and still ship first (rentals only); the subscription spec drops its own delivery / post-extraction / sender sections and plugs in as the second source.
  • U2 — Where the mode is set — Answer: One company default for all recurring invoices (companySettings.invoiceAutomation, default Post and Email, on Settings → Invoicing) plus a nullable per-document override (rentalAgreement.invoiceAutomation, subscription.invoiceAutomation; NULL = company default). Supersedes subscription-spec Q5's "per subscription, Draft by default".
  • U3 — Reply-to on automatic emails — Answer: companySettings.accountsReceivableEmail, else the document's owner (sales person, else creator) — the same owner the digest uses. From stays "<Company name>" <DEFAULT_FROM>. Refines rental D13 ("else the agreement creator") and supersedes the subscription spec's "sales person, else activator".
  • U4 — Daily job — Answer: One recurring-billing job: per company (isolated step), draft every source (rental agreements now; subscriptions when built), run the shared automation over all drafted invoices, send ONE digest per owner. The rental-billing cron is renamed now.
  • Settled with U1 (no question): Post and Send via Stripe joins the shared mode list when the Stripe branch is built (subscription work, ALTER TYPE … ADD VALUE), and is then available to rental agreements too — the rental plan does not build Stripe.

Generalized AR contracts (2026-10-02, from Rillet's Contract screens)

Rillet's Contract (General Details → Products → Invoicing → Revenue → Summary): customer + shipping customer, contract type (New Sales …), close date, PO number, start + duration presets (6 mo / 1–3 yr / open-ended / custom) → end date, currency; product lines of any pricing type (One-time / Fixed recurring / Usage) with price × qty, customer-facing description, own dates + go-live, "Over Contract Term" / "Adjust Contract Term", tax, discount (% or $), dimensions, revenue account; invoicing = frequency + first invoice date + payment terms + bill/ship-to + email recipients + an EDITABLE invoice breakdown (dates and totals); revenue = per-line pattern (Daily | Even Period, prorated first & last) + an EDITABLE monthly revenue schedule; summary of invoiced / recognized / deferred per month. Billing and revenue are independent schedules on the same lines (a one-time $1.8M item invoiced once, recognized $300k/month over six months).

  • G1 — AP side — Answer: Nothing on the purchasing side for now (Rillet has none). Prepaid expenses and repeating supplier bills remain a documented gap, out of scope.
  • G2 — Contract scope — Answer: A generalized AR Contract replaces the "Subscription" document: services, SaaS and one-time items on one contract, with independent billing and revenue schedules. Rental agreements stay a separate document (fleet custody, deliver / return, out of service, lease classification) and share the recurring-invoicing layer (U1–U4); merging them is a later option, not blocked by either.
  • G3 — Contract line kinds — Answer: One-time and Recurring lines, Service items only (setup fees, implementation, prepaid licences, platform access, support). Physical goods stay on sales orders (shipping, stock), linkable to the contract. Usage out of v1. Extends Q1.
  • G4 — Invoice schedule — Answer: Computed from frequency + first invoice date (one-time lines on the first invoice), then editable while Draft — move a date, split or merge invoices, bill a one-time line later — with each line's billed total conserved (Rillet "redistribution only").
    • Settled by the model: an open-ended contract's schedule is editable only within the cut horizon; an amendment regenerates the unbilled schedule from its effective date, resetting manual edits after it (the user is warned).
  • G4b — Editable schedule for rentals? — Answer: No. Rentals get the shared read-only "Upcoming invoices" preview; their schedule stays driven by terms, deliveries and returns. Reasons: returns / holdover / rolling periods re-cut the schedule and would overwrite edits; a unit treated as a sale must bill exactly the level payment × whole periods its net investment was valued on (salesTypeRequirementError); rent automation relies on invoices being fully determined by the terms. Revisit narrowly (Rental-treated units on fixed terms) if a real case appears.
  • G5 — Revenue per line — Answer: A revenue pattern per line — Daily or Even per month, prorated first & last (Rillet DAILY / EVEN_PERIOD) — over revenue start / end dates that default to the line's (a go-live date can push the start). A read-only per-month revenue preview and an invoiced / recognized / deferred summary. Hand-edited revenue schedules are out of v1.
    • Settled by the model (supersedes the subscription spec's "plain Service-line deferral, no new posting rules"): revenue follows the contract LINE, not the invoice, so a contract line's revenue schedule is generated at confirmation and posted by the recognition run; invoice posting for a contract line uses the rental revenue model already built (rental-posting.ts planRentalLine: consume accrued Contract Assets first, defer the rest; the run accrues earned-but-unbilled revenue to Contract Assets, synthesizeRentalAccruals), generalized from rental lines to contract lines. This is the "revenue arrangement" of .ai/specs/2026-07-04-revenue-recognition.md Phases 2–3, built from the billing side (no SSP allocation: each line's revenue is its own price).
  • G6 — Contract type — Answer: Types New Sales, Existing, Expansion, Reactivation, Contraction, suggested automatically and editable: first contract for the customer → New Sales; customer whose previous contracts all ended → Reactivation; a renewal → Existing; an amendment that raises / lowers recurring value → Expansion / Contraction. ARR / MRR reporting is a follow-up spec.
    • Settled by the model: the type belongs to the contract AND to each amendment, so an amendment is a small header (contractAmendment: date, reason, type, effective-from) that its replacement lines point at — Rillet's amendment carries amendment_date + reason the same way.
  • G7 — Email recipients — Answer: The document's invoice contact (To), CC the customer's default CC (else the company default CC) — the rental automation plan's rule, kept as the shared-layer rule for rentals and contracts. (All-customer-emails and an "invoice recipient" contact flag were considered and declined.)
  • G8 — Line discounts — Answer: A discount % per line (and per amendment line), shown on invoices as price + discount; a time-limited discount is an amendment that removes it at a date; revenue is the net amount.
  • G9 — Revenue on migrated, already-billed periods — Answer: Carbon recognizes it. A migrated contract carries Billed through (Q10) and Recognize revenue from (the migration date); its revenue schedule from that date releases the migrated Deferred Revenue opening balance month by month, and nothing before the date is touched. Requires the opening balance to sit in Deferred Revenue. Refines Q10 ("deferred revenue … arrives through the opening balance, not through the subscription").
  • Settled by the codebase: tables are customerContract* (no contract* tables exist; only the unrelated contractor), leaving room for a supplier-side contract later; UI label "Contracts".
  • Settled by the codebase: per-line manual dimensions (Rillet) are out of v1 — no Carbon sales document carries them; post-sales-invoice derives journal dimensions at posting.

Handoff (2026-10-02)

  • Split — Answer: Contracts ship as two plans: Phase A (contracts, editable invoice schedule, invoicing via the shared layer, Stripe mode; interim revenue through the existing Service-line deferral) and Phase B (line-level revenue engine). Recorded in the spec's "Delivery phases".
  • Order of work: (1) execute .ai/plans/implemented/2026-09-22-revenue-recognition-rentals-and-contracts.md Part II (builds the shared recurring-invoicing layer, renamed to source-agnostic names, scope unchanged); (2) /plan contracts Phase A from .ai/specs/implemented/2026-09-22-revenue-recognition-rentals-and-contracts.md Part III; (3) /plan Phase B.
  • Not committed at handoff: contracts spec (renamed from subscriptions), rental automation spec + plan renames, rev-rec scope note, this record, the research file additions.

Phase A execution

Plan: .ai/plans/implemented/2026-09-22-revenue-recognition-rentals-and-contracts.md Part III Branch: revenue-recognition-rentals-spec

Task 1 — Baseline (2026-10-03)

  • Merged origin/main (7 commits, incl. architecture hardening #1836) — merge commit 99a32331f2. 32 conflicts, none in the plan's STOP list. Branch routes moved to redirect from @carbon/utils (no-raw-redirect: 0 findings). Generated types/swagger regenerated by pnpm db:migrate; packages/jobs/manifests/schema.json regenerated by pnpm db:check:backups -- --stage (restorable). Committed with --no-verify (the hook would lint all 1314 merged files; resolved files were linted).
  • Environment: .env has a stale SUPABASE_DB_URL (port 54322); the stack runs on 57346 (.env.local). Vitest reads .env, so DB-backed tests need export SUPABASE_DB_URL="$(grep -h '^SUPABASE_DB_URL' .env.local | cut -d= -f2- | tr -d '"')".
  • Typecheck erp, @carbon/jobs, server-functions, database, utils, stripe, ee, documents: 9/9 successful.
  • Tests (with the URL exported): utils 555 ✓, database 314 ✓, server-functions 326 ✓, jobs 888 ✓ (30 skipped), documents 53 ✓.
  • No pre-existing failures.
  • Task 3b: unitPrice left unchanged in the salesInvoices view only as the lines JSON output field (no arithmetic).

Tasks 13 + 13b (one commit: same files)

  • Project dimension: buildSalesPostingLines marks revenue-side legs (Sales, Deferred Revenue, Rental revenue legs); AR, tax, shipping and disposal legs carry no project. The purchase invoice puts its project on AP too, so it was not copied.
  • SalesPostingMetadata.projectId is optional (a required field would break datasets/helpers/posting-journals.ts:170); every post-sales-invoice call site sets it.
  • Escape hatch 13b step 2 reviewed, not triggered in intent: the buyer's purchase invoice is typed by hand at the price it is charged (no mirror code exists), so the seller's intercompany amount uses net merchandise.
  • Shipment line unitPrice uses the generated netUnitPrice.

Task 14

  • buildMemoJournal books one reason line; it was NOT restructured. post-memo-transaction.ts replaces that line with two debits (Deferred Revenue released + Sales remainder) that sum to it. Release capped at the memo's base amount so the journal always balances.
  • Void refusal lives in post-memo/index.ts (not covered by a test).

Task 16

  • @carbon/stripe gained workspace/catalog deps @carbon/files and @internationalized/date (both already in the repo; lockfile +6 lines).
  • The ERP upsertDocument call became a direct document insert with the same fields.

Tasks 7 + 8 (one commit: same module files)

  • @mcp tags: +13 (the plan's list sums to 13; its "+11" was a miscount).
  • contractType optional in customerContractValidator (Task 22 suggests it when unset).
  • getContractInvoiceSchedule returns { invoices, credits } (credits = memo-borne rows).
  • The rental label map gained the Stripe mode here (Task 19's one-liner) so erp compiles after the enum array grew.

Tasks 19 + 20

  • @carbon/form Select has no per-option disabled state, so the Stripe mode stays selectable when Stripe is not connected; the option label says "Connect Stripe in Integrations first" and the action refuses the save.
  • Nav icon LuFileText (LuFileSignature not in react-icons 5.6.0). Ended status uses gray (no "muted" in the palette).

Task 13e

  • unitPrice is base currency, convertedUnitPrice document currency; the discount factor applies to whichever each site already used.
  • Model field discountPercent is .optional() (a .default(0) breaks provider pull code outside the task).
  • FOLLOW-UP (pre-existing, out of scope): the Stripe send uses salesInvoiceLine.unitPrice (base currency) under the invoice's currency code, so a foreign-currency invoice sent via Stripe bills base amounts.

Task 13d

  • Invoice line discount is entered in percent points (INPUT_FORMAT.percentPoints) and the route divides by 100. (The plan's "as quote pricing does" was inaccurate: quotes store the fraction directly.)
  • nonTaxableAddOnCost in SalesInvoiceSummary is NOT missing: it is added after tax, matching the view. No change.
  • FOLLOW-UP: upsertSalesInvoiceLine (@mcp upsert) takes its type from the points validator but writes the value as given; an MCP caller passing 20 is rejected by the 0–1 CHECK rather than stored wrongly. Convert in the service in a later change.

Tasks 9–11 (one commit: one server function)

  • post-customer-contract: confirm, edit-schedule, reset-schedule, amend (+preview), cancel (+preview), revert-cancellation; { update: "sales" }.
  • Choices beyond the plan: a One-time line is changed/ended as a whole (old line ends the day before it starts); a future-dated line changes from its own start; amendment percents are points ÷ 100; discount-ends applies to Recurring lines only; confirming an edited open-ended contract compares totals up to the last persisted period; the credit memo amount rounds to the currency's decimals.
  • Found and fixed (separate commit): reconcile re-cut split installments one by one to the full line amount.

Tasks 21 + 22 (one commit)

  • Notes on an Active contract go through the new updateContractNotes service (@mcp update); the agent's direct table update in the route was moved into it.
  • Header Amend/Cancel are disabled until Tasks 28/29; Confirm/Invoice Now/Revert post to routes built in Tasks 27/29.
  • ContractProject loads active projects client-side into a Combobox (no Project selector exists in ~/components/Form).
  • Notes are edited as plain text in the properties panel (tiptapToText / textToTiptap), as rentals do.

Task 12

  • create-contract-invoices + live-DB test post-customer-contract/contract-lifecycle.test.ts (confirm → draft → amend → cancel → end; renewal with uplift; open-ended horizon roll). Both run (not skipped) and pass.
  • Choices: a no-uplift renewal still copies a line ending on the old contract end (else it drops out of the new term); location = the origin sales order's shipping location, else the company's oldest location; the horizon roll only appends rows after the last persisted period.

Tasks 17 + 18

  • sendPostedInvoiceViaStripe({ client, db, companyId, invoiceId }) (needs db for the mapping write); guards a double send by checking for an existing Stripe invoice link before sending.
  • Owner lookup failure no longer fails the email (logged, returns null). A failed rental step no longer skips the company's contract step.
  • FOLLOW-UP for Task 34: packages/jobs/AGENTS.md still describes recurring-billing / invoice-automate as rentals-only.

Tasks 23–26 (one commit: shared index.ts / types.ts / loader)

  • Line form: Item picker type="Service"; One-time hides the rate unit; Revenue fields collapsed but still submitted; read-only on Active ("Change lines with Amend"). New line defaults: Recurring, rate unit = billing frequency, start/project from the contract.
  • Invoices: unedited Drafts send planned: refs; the server function resolves them (fix commit 56fa0f7b8e, live-DB test). Loader adds held reasons and credit memo numbers (one .in() each). Installment inputs use the rate format (5 dp) so installments can sum exactly.
  • Revenue: project names fetched client-side (embedding project(name) in getContractLines would remove that).

Tasks 15 + 30

  • Create Contract is disabled (not hidden) when ineligible (Carbon "disable, don't hide").
  • Server refuses Cancelled/Closed orders; the status recompute runs only for To Ship and Invoice / To Ship / To Invoice / Completed; deleting a contract (line) recomputes the status of an order the rollup settled.
  • Payment term and invoice party come from salesOrderPayment (the order header has neither).
  • New contract lines from an order default to One-time.

Tasks 28 + 29

  • Previews POST intent=preview; $id.tsx shouldRevalidate skips a preview so the page loader does not re-run per keystroke.
  • Flash messages are text only, so the memo link lives on the contract page (Invoices credit rows), not in the flash.
  • Amendment history infers an ended line (end = amendment − 1 day, no replacement); cancellations are exact via previousState.
  • Preview rows for new lines show "New line" (their ids come from the rolled-back transaction).

Task 31

  • Contract links: invoice line form ("Generated from contract CON…"), invoice header and memo header (secondary button, LuFileText), readable ids read client-side scoped by companyId.

Task 27

  • ESCAPE HATCH TRIGGERED: path.to.api.stripeConnectCustomer (api+/stripe-connect.customer.$invoiceId.ts) and resolveStripeCustomer resolve the billed customer from an invoice; there is no by-customer variant. The contract confirm modal does NOT link a Stripe customer. In Stripe mode it shows whether the billing customer is linked ($id.confirm.tsx loader, getLinkedStripeCustomerId); unlinked → warning + Confirm disabled; the server function refuses too. OPEN DECISION for Brad: build a by-customer Stripe link step.

Task 33 — i18n

  • lingui:extract → 219 new strings × 12 locales = 2628, filled via the translate skill (84 Haiku chunks). Merge: 2628 filled, 0 unmatched, remaining 0. linguito check exit 0. Glossary coverage 100% for all 12 locales.
  • check-glossary.mjs: 1560 enforced violations vs 1456 at HEAD (pre-existing failure); this run added 104 (≈4% of new strings), several apparently inflected forms the checker misses. Reported for review per the skill; repair is the consistency runbook, not a re-run.
  • Gates: lint 34/34; typecheck 10 packages; tests utils 577, database 338, server-functions 331, jobs 894, checks 260, documents 56, ee accounting 1028, erp sales/invoicing 256 — all pass. MCP manifest current; workflow catalog regenerated (commit bc50296243).

Task 35 — browser verification

Run 2026-10-04 (company "Carbon Development", timezone UTC, base USD, accounting enabled, company invoicing default Post; contracts set per-contract where a mode mattered). Dates shifted onto the current month: Acme = CON000001, start 2026-10-01, Monthly / Calendar / Advance, 12 months, renew +5%, Post and Email; Consulting One-time $60,000 2026-10-01..2027-03-31 Even Period, Website Hosting 10 × $40/Month 20% off, Support $1,200/Year. SMTP not configured (RESEND_API_KEY only — no contract had a contact with an email, so nothing was sent). Stripe Connect NOT connected (companyIntegration empty). Screenshots in .context/contracts-*.png. Contracts after CON000001 were created by POSTing the New Contract / Add Line forms' own route actions from the logged-in page (same server path as the forms); confirm, invoice, amend, cancel, split, void and post went through the UI.

  1. FAIL — Draft Acme: Invoices show Oct 1 = $60,420.00 then $420.00 monthly (PASS part); implementation previews $10,000/month Oct–Mar (PASS part); but October reads invoiced $60,420.00 / recognized $15,040.00 / deferred $45,380.00 (expected $10,420 / $50,000), and Apr 2027 onward reads recognized $0 with "Earned, not billed". BUG-1 below. .context/contracts-01-revenue-preview.png, contracts-01-draft.png.
  2. PASS — Split Consulting into 3 × $20,000 (Oct 1 / Nov 1 / Dec 1) accepted: invoices $20,420 × 3, marked Edited. A split totalling $50,000: Split button disabled with "$10,000.00 left to place"; the same split POSTed straight to /schedule left the rows unchanged (server refuses too). Reset Schedule restored the computed schedule. contracts-02-split-refused.png, contracts-02-split-accepted.png.
  3. PASS — Confirm (modal: first invoice Oct 1 $60,420, 12 planned, Post and email). Invoice → AR000019, three Service lines (service windows Oct 1–31 ×2, Oct 1–Mar 31), Submitted via invoice-automate. JE-2026-10-000060 credits Deferred Revenue 60,000 + 320 + 100 = $60,420. Project = ORBSEC Constellation Block 2 (contract) on Consulting / Website Hosting revenue legs, NovaSat Gen-3 (line override) on the Support leg, AR legs carry no Project. Second Invoice → "Nothing is due on this contract yet", still one invoice. Email step: run output "The invoice contact has no email" (no contact set) — delivery PENDING SMTP. contracts-03-confirm-modal.png, contracts-03-active-invoiced.png.
  4. PASS — CON000002, IT Consulting 1 × $10/Day: Oct (31 days) $310.00, Nov $300.00, Feb 2027 $280.00. contracts-04-daily-rate.png.
  5. PASS (preview; the plan's 12 March date unreachable, used 12 Oct — Oct has 31 days, same arithmetic) — Amend Acme, change date Oct 12, Website Hosting 10 → 15: adjustment −$206.45 (Oct 12–31), new line +$309.68 (Oct 12–31), type suggested Expansion. From the next billing period: "Takes effect Nov 1, 2026", no October rows. Not saved on Acme. Saved twice elsewhere: CON000003 (Oct 12, future) and CON000005 (Oct 4) — both "Contract amended". contracts-05-amend-change-date.png, contracts-05-amend-next-period.png.
  6. PASS (today-reachable variant) — CON000004 (platform + support, Oct invoiced $420 as AR000023), Cancel end date Oct 20, Credit unused time: preview "$149.03" (= 420 × 11/31); saved → Draft credit memo CR-2026-10-000003 $149.03 linked to the contract. Posted → JE-2026-10-000066: Deferred Revenue −149.03 (debit, liability), AR −149.03 (credit). Revert Cancellation afterwards refused: "The credit memo has been posted", contract still ends Oct 20. contracts-06-cancel-preview.png, contracts-06-revert-refused.png.
  7. PASS — CON000002's posted AR000020 voided (⋯ → Void → Void Invoice); the schedule row went back to Planned; Invoice → AR000021 Draft, Held, automationHoldReason "Re-billing AR000020, which was voided". contracts-07-rebill-held.png.
  8. PASS — CON000005 (custom end Oct 31, End, Post and Email), Oct invoiced $400 (AR000024); amended Oct 4 10 → 5 seats (−$361.29 adjustment, +$180.65 new) → Invoice → AR000025 Draft −$180.65, held "Includes a credit for a contract change" (not posted). contracts-08-negative-adjustment-held.png.
  9. PENDING (Stripe not connected) — designed refusals verified: CON000006 in Post and Send via Stripe → Confirm modal "No Stripe customer is linked …", Confirm disabled (Task 27 escape hatch). Settings → Invoicing: the option reads "Post and send via Stripe / Connect Stripe in Integrations first", and saving it is refused with that message (companySettings stays Post). The actual send/stamp needs Stripe test mode. contracts-09-stripe-confirm.png, contracts-09-settings-stripe-option.png.
  10. PASS — SO000027 (Electrical Service $300 + Support $1,200, both Service) confirmed → To Invoice. Create Contract with only Support ticked → CON000009 Draft, line salesOrderLineId set, order line invoicedComplete. Invoice the order → AR000029 with ONLY Electrical Service; posted (JE-2026-10-000069 Sales/AR $300) → order Completed. Side finding BUG-3 (stored header subtotal $1,500). contracts-10-contract-from-order.png, contracts-10-order-invoice-draft.png.
  11. FAIL — BUG-2. A contract created in EUR stores exchangeRate 1 (CON000007 → AR000026 posted 320 = 320 at rate 1). Switching currency in the properties panel does fetch 0.9215 (CON000008), but its invoice AR000027 bills the customer €294.88 (unit €36.86) for a contract that shows €40 / €320 per month; base posted $320. contracts-11-eur-contract.png, contracts-11-eur-invoice.png.
  12. PASS — RA000003 Invoice with October already billed → "Nothing is due on this agreement yet"; Add Charge $75 → Invoice → "Drafted 1 rental invoice" AR000028 Draft held "Charges are reviewed before posting" (rental behaviour unchanged). contracts-12-rental-charge-held.png.
  13. PASS — AR000019 Website Hosting line: 10 Each × $40.00, "20% off", $320.00 on screen; PDF row "10 EA 40.00 −20% 320.00", Subtotal 60,420.00 (.context/contracts-13-invoice.pdf). Hand-made AR000030 (Consulting 10 × $40, 20% off) posted: JE-2026-10-000070 Sales $320 / AR $320; list total $320.00. contracts-13-invoice-line-discount.png, contracts-13-invoice-list.png.

Renewal (term end not reachable today): covered by contract-lifecycle.test.ts — pnpm --filter @carbon/server-functions exec vitest run src/post-customer-contract → 3/3 passed (confirm/draft/amend/cancel; renewal with uplift + open-ended roll; first schedule edit). Digest and email delivery: PENDING SMTP.

Bugs found (not fixed)

  • BUG-1 (check 1) Revenue preview lumps an open-ended Recurring line into its start month. Repro: /x/contract/con_C2EHU3BswwbhX8U8eHAiZA/details → Revenue: Website Hosting "Oct 1, 2026 – No end $3,840.00", expand → one row "Oct 2026 $3,840.00"; Support likewise $1,200 in Oct. Cause: lineRevenueDates returns end = revenueEndDate ?? endDate (null for a line with no end) and revenuePreview (packages/utils/src/contract-revenue.ts) treats revenueEnd === null as point-in-time — correct only for One-time lines (spec "The revenue schedule"). A Recurring line should fall back to the contract end date (or the schedule horizon). Preview only — Phase A posts revenue from each invoice line's service-window deferral — but the position table is wrong (deferred too low, "Earned, not billed" from Mar 2027).
  • BUG-2 (check 11) Foreign-currency contracts. (a) insertContract (New Contract form, x+/contract+/new.tsx) never looks up the exchange rate, so a contract created in EUR keeps the column default 1; only the properties-panel currency change (update.tsx) calls getExchangeRate. Repro: New Contract → Currency Euro → Save → customerContract.exchangeRate = 1 → its invoice posts at rate 1 (CON000007 / AR000026). (b) create-contract-invoices writes the contract-currency rate into salesInvoiceLine.unitPrice, which is the BASE price (convertedUnitPrice = unitPrice × exchangeRate), so the customer is billed rate × exchangeRate in the document currency. Repro: CON000008 (EUR @ 0.9215, 10 × €40, 20% off; contract page shows €320/month) → Invoice → /x/sales-invoice/si_HnKuQAiPrDd1trrwEMMzxW shows €36.86 unit and €294.88 total; journal AR/Deferred Revenue $320 (expected €320 document, ≈ $347.26 base). Spec says invoices post in the contract currency with base translation. create-rental-invoices writes unitPrice the same way, so foreign-currency rental agreements likely share (b) — not tested.
  • BUG-3 (check 10, low) Order invoice header subtotal counts contract-billed lines. convert salesOrderToSalesInvoice skips invoicedComplete lines when inserting invoice lines but its uninvoicedSubtotal does not, so AR000029 stores salesInvoice.subtotal/totalAmount 1500 while its only line is $300. Screens, list, view and journal use the computed $300; the stored header columns are stale (note a hand-made invoice also stores subtotal 0, so the stored header is not authoritative elsewhere either).

Observations (minor)

  • Per-contract Invoicing (New Contract form, properties panel) offers "Post and send via Stripe" with no "Connect Stripe" hint and saves it without Stripe connected; Settings refuses the same choice. Confirm then blocks on the missing customer link, so nothing is sent.
  • The Billing Timing help (termId="billing-timing") on the contract form and the order's Create Contract modal reads rental copy: "When each rental billing period falls due … before the unit has been used … after the rent is earned."
  • "Revert Cancellation" stays enabled after the credit memo posts; the server refuses with a toast.
  • Invoice PDF summary prints "Discount (USD) −80.00" above "Subtotal 60,420.00" although line totals are already net, so the discount row reads as if subtracted again.
  • A manual Service line (Consulting) on a hand-made invoice posts through "Post and Ship Invoice" (methodType Purchase to Order) — pre-existing.

Task 35 — re-test after fixes

Run 2026-10-04 against the hot-reloaded dev server (fixes 8ba14cb914 BUG-1, c9847d4796 BUG-2, fbb71af40b BUG-3), isolated agent-browser session reverify35, same company. No product code changed.

  1. PASS (BUG-1) — CON000001 (/x/contract/con_C2EHU3BswwbhX8U8eHAiZA/details, Active). Revenue: Website Hosting still reads "Oct 1, 2026 – No end $3,840.00" but "Show monthly revenue" now lists 12 rows Oct 2026 – Sep 2027 (Oct $326.14, Nov $315.62, Feb $294.58 …), not one October row. Position table runs Oct 2026 – Sep 2027 and ends at deferred $0.00. October reads invoiced $60,420.00 / recognized $10,428.05 / deferred $49,991.95 — not the expected $10,420 / $50,000 because both recurring lines carry revenue method Daily (DB: Website Hosting and Support revenueMethod = Daily, Consulting Even Period): Oct = 10,000 + 3,840 × 31/365 (326.14) + 1,200 × 31/365 (101.92) = 10,428.05. A day-weighted month against flat monthly billing also leaves a few-dollar wobble (Mar deferred $6.90; Aug 2027 "$5.75 Earned, not billed", cumulative daily revenue through Aug 31 = 64,625.75 vs billed 64,620) — arithmetic of the Daily method, not the bug. .context/contracts-retest-01-revenue-spread.png, contracts-retest-01-october.png, contracts-retest-01-position.png.
  2. PASS (BUG-2) — New Contract form (UI): "EUR re-test (Task 35 BUG-2)", Apex Space Research, start Oct 4 2026, Monthly / Anniversary / Advance, Currency Euro → CON000010 (con_DXZEiNyKPuqzUbhAJUbMzN) stored exchangeRate 0.9215 (= the company's exchangeRateOverride EUR rate; CON000007 before the fix stored 1). Add Line (UI): Website Hosting, Recurring, 10 × €40/Month, 20% off → contract shows €320.00/month, 12 × €320.00 invoices. Confirm → Invoice → "Drafted 1 invoice(s); posting 1 automatically" → AR000031 (si_LJjqm7ogtg2UXz6k4KgPyr) Submitted: line $43.41 / €40.00 each, 10, 20% off, €320.00 ($347.26); subtotal/total €320.00 / $347.26. DB: unitPrice 43.40749 (base), convertedUnitPrice 40.000002, header exchangeRate 0.9215, subtotal 347.25992. Journal JE-2026-10-000071 Posted: AR 347.26 / Deferred Revenue 347.26. .context/contracts-retest-02-new-eur-form.png, contracts-retest-02-eur-invoice.png.
  3. PASS (BUG-3) — New SO000028 (so_KmnY5EqG7RrqAEzDTSfUFb, Apex, ref "BUG-3 retest"): Electrical Service 1 × $300 + Support 1 × $1,200 (Rule Violation acknowledged on save/confirm) → Confirm → To Invoice. ⋯ → Create Contract with only Support ticked → Draft contract con_L6Ne3MMgdv5hPxFb1C1St5, Support line invoicedComplete. Order Invoice → AR000032 (si_LygNX69jDR64eh8D1UbDUB) Draft with one line Electrical Service $300; stored salesInvoice.subtotal 300 / totalAmount 300 (AR000029 before the fix: 1500 / 1500); header $300.00, invoice list total $300.00. Left in Draft. .context/contracts-retest-03-order-invoice.png, contracts-retest-03-invoice-list.png.

Fixes after self-review (Brad, 2026-10-04: fix all must-fix items; build the Stripe link; research FX)

# Fix Owner
1 Horizon roll and edited open-ended confirm key on the last RECURRING period end, not a long One-time window A
2 Adjustment for an invoiced split = Σ billed on the key − ideal clipped amount; memo releases every installment's deferral A
3 Revert refuses when an amendment is newer than the cancellation; finds the cancellation by previousState IS NOT NULL A
4 discountEndsOn applied on amend change/add and dropped/applied on renewal copies A
5 A second earlier end credits the difference, not nothing A
13 Amend percent points → round(points / 100) A
+ Split installments must each be > 0; billedThrough must fall on a period end (spec); clear InvalidInputError for amend rateUnit/date conflicts A
6 Create Contract from an order prices from convertedUnitPrice; modal shows the order currency B
9 Cancel with credit requires create: invoicing; revert requires delete: invoicing B
10 Switching an Active contract to a posting mode requires create: invoicing; Stripe mode refused when Stripe is not connected B
11 deleteContractLine loses its @mcp tag (route uses the releasing function) B
+ Invoice Now / Cancel-with-credit buttons honour invoicing permission; billing-timing glossary term covers contracts; status-colors comment placement B
7 Stripe receives document-currency amounts (converted*) C
8 A zero-total invoice in Stripe mode is not sent (no error, not held) C
+ Stripe idempotency key per Carbon invoice; no per-unit rounding of the net price; pure test for toStripeInvoiceLines; PDF summary order Subtotal (gross) → Discount → Tax C
12 Confirm loader logs a swallowed Stripe lookup error D
S Link a Stripe customer from the contract page (by-customer resolve + link) D
FX Research: contract exchange-rate policy E

Fixes after self-review — results (2026-10-04)

  • A (7ba605c5bf): all seven items confirmed and fixed with failing-first tests (schedule 34, server-functions 337 incl. new DB tests). Behaviour notes: credits are billed − clipped period price (same pricing rule as rows); a renewal that un-clips an invoiced period now bills the remainder.
  • B (db7cb8f504): permissions on cancel credit / revert / posting modes; order prices in document currency; deleteContractLine and updateContractInvoiceAutomation are no longer MCP tools.
  • C (cfe04ff7bf): Stripe sends invoice-currency amounts (connect-invoice.ts, tested), skips zero totals, idempotency keys on every write; PDF Subtotal → Discount → Tax.
  • D: Task 27 escape hatch RESOLVED — resolveStripeCustomerForBilling / linkStripeCustomerForBilling (invoice preflight now calls the shared helper), by-customer API route, StripeCustomerPanel loads by invoice or customer, contract confirm modal links before confirming; confirm loader logs a swallowed lookup error. Browser check pending: the shared dev server returns 504 "Outdated Optimize Dep" and needs a restart.