Skip to content

feat(o2c): derive the order's generated plan step as total minus the other steps (#2939) - #2942

Draft
objectstack-fleet[bot] wants to merge 2 commits into
mainfrom
claude/issue-2939-derive-generated-step
Draft

objectstack-fleet[bot] wants to merge 2 commits into
mainfrom
claude/issue-2939-derive-generated-step

Conversation

@objectstack-fleet

@objectstack-fleet objectstack-fleet Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2939

Clause-②: no
Metadata-protocol: none
Metadata-impact: maintainer — a hook changed (billing_step_give_back on crm_billing_milestone is replaced by billing_step_derive_remainder), an action (Prepare Billing) and a flow (sales_order_amount_plan_step) now set or delete the order's generated plan step by derivation. No permission set, sharing rule, field or object is added, removed or renamed.

维护者速读

  • 用户能看到什么变化:按工时计费的订单上,订单确认时自动生成的那条回款计划改为始终等于「订单总金额减去其他回款计划」;开票、删除开票计划、改订单金额后都会自动重算,订单不再因为这些操作误报「回款计划与订单金额不符」。
  • 为什么做:落实您在开票扣减那张合并请求上第四问「选 A 并另开卡」的裁决,用「按结果推算」取代「逐笔加减」,根治删除开票计划后自动计划少一笔、拆分计划会漂移这两个风险。
  • 出错会怎样:受影响的只是这条自动计划的金额;它从不低于已收到的回款,有回款或有发票指向时从不删除,已取消订单和有多条自动计划的订单不动;算错时销售可以直接改回,下一次开票或删除也会重新算对。
  • 要你做什么:确认三点取舍——已有回款或发票的自动计划也参与推算但不低于已回款;销售手工调低但没补齐其他计划的自动计划,下次开票会被重算成剩余金额;只有订单上已有开票计划时,改订单金额才会重算。

What changes

The order's generated plan step (origin = order_confirmation) is now derived: the order's Total Price less every other step of the order. Floors: never below the step's Received, never below 0. At 0 with nothing received and no invoice naming it, it is deleted (ruling B-1 on #2933 kept). When a floor binds, the plan stays above the order and Payment Plan Differs shows it, which is the truth.

Where it runs, and relative to which transaction:

  • Prepare Billing (src/psa/actions/billing.actions.ts): inside the run's ctx.api.transaction, after the stamps and their count check. It reads the whole plan in the unit (so it sees the run's own step) and writes with the compare-and-set of Order to Cash: Prepare Billing reduces the order's automatic full-amount payment plan step, so Payment Plan Differs stays truthful #2934 (multi: true over { id, planned_amount: read }, row count must be 1). A miss throws CONFLICT and rolls back the run; the retry derives again. The per-run reduction is removed, replaced by this derivation.
  • A deleted billing step (src/o2c/objects/billing_milestone.hook.ts): billing_step_give_back is removed and replaced by billing_step_derive_remainder (afterDelete, runAs: 'system'). It runs after the delete has committed, outside its transaction (measured in the pinned engine: after-events wait for the unit's commit), so it reads the plan without the deleted step. Compare-and-set, three attempts, then a ctx.log.warn. Why that is now safe: the value is derived from the plan as it stands, not added to the step, so a missed write is not lost money: the next derivation (a run, a delete, a total change) writes the same value. The give-back's two weaknesses from the feat(o2c): Prepare Billing reduces the order's automatic full-amount step (#2934) #2938 review (after-commit give-up leaving the step short for good; no record of what each run took, so a split plan drifted) are gone. The hook SETS the step to the remainder (so a generated step a person raised by hand above it is lowered, as a run would) and never deletes it.
  • The order's total changes (src/o2c/flows/sales-order-amount-plan-step.flow.ts, the e6 equivalent): a new branch for an order with several steps, exactly one generated and at least one from a billing run. The order's Planned (all steps) less the generated step's own amount is "the other steps", so the flow reads the order after the steps and computes the remainder in two assignment nodes; the edges compare one bound string, so test/flow-variable-conditions.test.ts stays green. The delete is a compare-and-set (multi: true). The write is by id, not a compare-and-set: a multi update that sets an input of the stored Open is the lint warning flow-multi-update-moves-stored-input, which os lint --strict refuses even though the id pins one row. The flow header states the window this leaves (a billing run between the flow's read and write is overwritten, the flag shows it, the next derivation puts it right; a total moves only while Draft). The lone-step path (e2/e6/e7) is unchanged.
  • Cancelled orders: none of the three derives on a cancelled order (the action and hook check the order's status, the flow's new branch is guarded on record.status in e3/e8). The cancel-trim flow owns that plan, and the total stays where it was, so "total − other steps" would rebuild what the trim took off (the extra acceptance point from the Order to Cash: measure whether cancelling an order lets its cleared time and expenses be billed onto the cancelled order #2940 measurement). Prepare Billing itself still bills a cancelled order's project until hotcrm#2940 lands; the billing step it adds leaves the generated step alone, and the docs say so.

Rulings:

  • 2A (a step with money received or an invoice naming it): as the card restates it, it is "not lowered below Received". Derivation makes the original "left untouched" unnecessary for its stated reason (no record of what each run took, so a give-back could not be symmetric): derivation is symmetric by construction. Such a step is now derived with Received as its floor and is never deleted (an invoice-named step with 0 received is kept at 0).
  • 3A (several generated steps): kept, not moot. With two there is no single remainder, so all three paths leave the plan alone.
  • 1A (no backfill): kept. An order billed before Order to Cash: Prepare Billing reduces the order's automatic full-amount payment plan step, so Payment Plan Differs stays truthful #2934 is put right the next time any derivation runs on it (test: 10,000 order with an old 300 billing step, next run of 150 → 9,550).

Rule 11: HookCompareAndSetOptions in src/sales/objects/_hook-api.ts keeps one rostered caller, renamed. The flow's by-id write is explained in its header.

Docs: in-app manuals crm_psa_billing and crm_o2c_collection_plan (en, zh); docs site Time and Expenses and Collection Plan (en, zh-Hans, zh-Hant); docs/architecture/psa-module-plan.md.

Changesets: new .changeset/2939-derive-generated-plan-step.md (minor). DELIBERATE CORRECTION of the unreleased .changeset/2934-billing-reduces-auto-step.md: its give-back, three-attempt and upgrade sentences described behaviour this release no longer ships, so its TO paragraph now ends "In this same release the step is worked out as the rest of the order rather than reduced run by run, and a deleted billing step or a changed total works it out again (#2939, its own entry): read that entry for how it behaves." The FROM paragraph is unchanged.

Tests

test/psa-prepare-billing-order-step.test.ts: 33 cases (was 19), on the booted app through the action door, exact figures. Every earlier case kept, the changed ones updated to the derived figure:

Cases
  • Run: 10,000 order billed 300 then 150 → 9,700 then 9,550, Planned 10,000, no flag, open 10,000 · exactly the order (300) → deleted · past it (450 on 400) → deleted, flag · 1,000 received → still 9,700 · floor binds: 1,000 order, 800 received, billed 300 → 800 (not 700), Planned 1,100, flag · invoice naming it → 9,700; on a 300 order billed 300 → kept at 0, not deleted · two generated steps → untouched, order_step null · CONFLICT on a writer at the compare-and-set → 409, nothing left, retry 9,700 · order billed before Order to Cash: Prepare Billing reduces the order's automatic full-amount payment plan step, so Payment Plan Differs stays truthful #2934 → 9,550 · generated lowered by hand to 6,000 alone → 9,700 · contract project → unchanged.
  • Delete: split plan 6,000 + 4,000 → 5,700, delete → 6,000, rebill → 5,700 · split plan, delete without rebilling: an old 300 step that took nothing, deleted → 6,000 (the give-back gave 6,300) · 9,700 → 10,000 → 9,700 · generated already deleted → nothing recreated · 1,000 received after the run → delete gives 10,000 · two misses → third attempt writes 10,000 · three misses → step keeps 9,400, delete stands, and the next run puts it right (10,000 − 450 = 9,550) · shipped body logs one warning { order, step, remainder: 10000 } · old billing step deleted → 10,000 · a generated step a rep raised by hand to 12,000 beside 300 → SET to 10,000 on delete (not 12,300).
  • Total: raised 10,000 → 12,000 → 11,700, no flag · lowered to 5,000 → 4,700 · lowered to 200 below the 300 billed → deleted, flag · lowered to 1,200 with 1,000 received → floor 1,000, Planned 1,300, flag · rep's split with no billing step → untouched, flag.
  • Cancel: 100 received on billing → generated deleted, billing 100 · nothing received on generated, the surviving billing step deleted → no generated step comes back (pins non-recreation only: the hook never recreates a deleted step, guard or not; the guard is pinned by the next four) · a generated step at 1,000 received survives the cancel (beside billing 100), then: the billing step deleted → stays 1,000 (remainder would be 10,000) · a Prepare Billing run on the cancelled order (served until hotcrm#2940; the case accepts a 400 too) → stays 1,000, order_step null (remainder would be 9,550) · a system write moving the total to 12,000 runs the follow flow → stays 1,000 (remainder would be 11,900) · 1,000 received on generated, nothing on billing → trim lowers it to 1,000 and deletes the billing step; the derivation does not raise it again · nothing received → both deleted, trim run completed.
Ablation (each restored after its run; counts on the final 33-case file)
  • Derivation in Prepare Billing disabled (if (false && orderId)): 13 of 33 red.
  • Hook body returns at once: 7 red (split plan, 9,700→10,000, 1,000 received, two misses, three misses, shipped-body warning, hand-raised 12,000). The "split plan, old step" case stays green under this ablation (no hook leaves 6,000 too); it tells derivation apart from the give-back's 6,300, not from no hook.
  • Hook only raises (if (!(after > before)) return): 1 red, the hand-raised case (expected 12000 to be 10000).
  • Received floor removed in the action (Math.max(target, 0)): 1 red, the floor-binds case.
  • Flow branch disabled (size(...) > 100): 4 red, the four total-change cases.
  • Received floor removed in the flow (DERIVED = 'vars.remainder'): 1 red, the 1,200 case.
  • Cancelled guard removed in Prepare Billing (const plan = order): 1 red, the run on the cancelled order (order_step not null).
  • Cancelled guard removed in the flow (both e3 and e8 terms): 1 red, the system total write (expected 11900 to be 1000).
  • Cancelled guard removed in the hook: 2 red, the delete after cancel (expected 10000 to be 1000) and the trim case (times out at its 15 s settle as the step goes back up).

Slow cases carry their own 15 s budget with a one-line reason, as in d66957a (0.9 to 1.2 s locally).

Acceptance notes

  • Prepare Billing on a cancelled order still runs (hotcrm#2940, waiting on the maintainer, not done here). The derivation stands aside on it (pinned by its case and ablation); the docs and the changeset say a cancelled order's generated step is left alone and that billing still runs until hotcrm#2940 lands. The measurement below hit it once in zh-CN (200), not inspected on the dev server.
  • Found in passing: os lint --strict refuses a flow update_record with multi: true whose filter pins the row's id (flow-multi-update-moves-stored-input), though one row always computes one value. It cost the flow its compare-and-set write; not filed here.

Verification

Booted `objectstack dev` on port 4039: a new 10,000 order confirmed Draft→Confirmed, a T&M project, a consultant's timesheets submitted and approved through the approval, Prepare Billing twice, a billing step deleted, the total raised, payments, cancel

en (zh-CN, accept-language: zh-CN, a second order: the same figures line for line):

confirmed                  {"total":10000,"planned":10000,"plan_differs":null,"steps":[["order_confirmation",10000,0]]}
prepare 2026-11-08 200 {"amount":300,…,"order_step":{"amount":9700,"deleted":false}}
after run 1 (300)          {"total":10000,"planned":10000,"plan_differs":null,"steps":[["billing",300,0],["order_confirmation",9700,0]]}
prepare 2026-11-15 200 {"amount":150,…,"order_step":{"amount":9550,"deleted":false}}
after run 2 (150)          {"total":10000,"planned":10000,"plan_differs":null,"steps":[["billing",150,0],["billing",300,0],["order_confirmation",9550,0]]}
after deleting the 150 step {"total":10000,"planned":10000,"plan_differs":null,"steps":[["billing",300,0],["order_confirmation",9700,0]]}
total raised to 12,000     {"total":12000,"planned":12000,"plan_differs":null,"steps":[["billing",300,0],["order_confirmation",11700,0]]}
1,000 + 100 received       {"total":12000,"planned":12000,"plan_differs":null,"steps":[["billing",300,100],["order_confirmation",11700,1000]]}
cancelled                  {"total":12000,"planned":1100,"plan_differs":null,"steps":[["billing",100,100],["order_confirmation",1000,1000]]}
cancelled, billing deleted {"total":12000,"planned":1000,"plan_differs":null,"steps":[["order_confirmation",1000,1000]]}

Two-minute check: npx vitest run test/psa-prepare-billing-order-step.test.ts, which should show 33 passed.

Read first: src/o2c/flows/sales-order-amount-plan-step.flow.ts (the new branch, its by-id write and the window its header states), src/psa/actions/billing.actions.ts (the derivation block after the count check), src/o2c/objects/billing_milestone.hook.ts (the derivation hook).

Gates on head 50e89cf: pnpm validate / typecheck / lint (os lint --strict, 0 warnings) / hygiene exit 0; full suite as 8 shards exit 0 (745 / 848 / 847 / 870 / 832 / 971 / 773 / 1214 passed).

🤖 Generated with Claude Code

https://claude.ai/code/session_017xRBcUpajguxop2zMyDN9K

…other steps (#2939)

Replace the run-by-run reduction and the after-delete give-back of #2934
with a derivation: the order's one generated step (origin
order_confirmation) is the order's Total Price less every other step,
never below its Received, never below 0, deleted at 0 when nothing is
received on it and no invoice names it.

- Prepare Billing derives it in the run's transaction, compare-and-set
  on the amount read (CONFLICT and rollback on a miss).
- billing_step_give_back becomes billing_step_derive_remainder
  (afterDelete, after commit): it derives from the plan as it stands,
  compare-and-set with three attempts, so a miss is put right by the
  next derivation instead of leaving the step short.
- sales_order_amount_plan_step derives it when the total of an order
  with billing-run steps changes (several steps, exactly one generated).
- A cancelled order is never derived; several generated steps are left
  as they are (ruling 3A); Received is a floor, an invoice or a payment
  keeps the step from being deleted (ruling 2A as the card restates it).

Tests on the booted app with exact figures, manuals (en, zh), docs site
(en, zh-Hans, zh-Hant), changeset; the #2934 changeset is corrected so the
release notes do not describe the give-back that this release replaces.

Claude-Session: https://claude.ai/code/session_017xRBcUpajguxop2zMyDN9K
@vercel

vercel Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
hotcrm Ignored Ignored Dec 4, 2026 12:22pm UTC

Request Review

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

repo:hotcrm@sunhaolin · sunhaolin · session_017xRBcUpajguxop2zMyDN9K

Independent read-only review · head d638bbf · 2026-12-04T11:19Z · reviewer: Opus, substituting for Fable (out of quota until 12-06) · maintainer-class, stays draft

  1. 结论:有条件通过。
  2. 依据:推算公式三处一致(总额 − 其他所有计划,含手工与开票计划,到分,下限取已回款和 0),无重复扣减;Prepare Billing 事务内按「id + 读到金额」条件写入,落空即 CONFLICT 回滚;删除 hook 提交后以系统身份运行,三次落空记警告;flow 仅在「恰好一条自动计划且 ≥1 条开票计划、未取消」时推算;已取消订单三处都跳过;29 个用例精确金额;_hook-api 登记名单已更新,符合规则 11。
  3. 风险:① 实质改了裁决 2A:原话是有回款或发票的自动计划「不动」,本 PR 做成「照算、不低于已回款」;且下限不看已开票——全额发票指向自动计划后开票照样扣小,「不符」不出现,可能重复开票的信号被掩盖。须维护者明确批准。② flow 按 id 直写(lint 误报所致),改总额瞬间恰逢开票会多一笔,下次推算纠正;按 AGENTS.md 应把 lint 误报提给上游。③ hook 注释「余额只增不减」与代码第 83 行不符。④ 测试空白:action 与 flow 的「已取消跳过」无用例无消融;一个取消用例在无自动计划时本就通过,判断不出跳过逻辑;消融说明「27 中 13 红」与 29 个用例对不上。⑤ 手册写「已取消订单不受影响」,但 Order to Cash: measure whether cancelling an order lets its cleared time and expenses be billed onto the cancelled order #2940 修好前仍会在已取消订单上新增开票计划。⑥ 测试分片当时仍在跑。⑦ 改未发布的 Order to Cash: Prepare Billing reduces the order's automatic full-amount payment plan step, so Payment Plan Differs stays truthful #2934 changeset 可接受(同版发布、FROM 未动),但删掉了「有回款或发票就不动」,须随 ① 一并确认。
  4. 类别:维护者类(同意)——替换以系统身份运行的 afterDelete hook,开票、删除、改总额都会自动改写或删除回款计划,并涉及裁决 2A。

待维护者裁决

  1. flow 写入:A 现在按 id 写(窗口很窄,只在草稿订单改总额时,下次推算纠正),同时向上游提 lint 误报,修好后另开小卡改条件写入;B 不做这个分支,等上游。建议 A。
  2. 何时重算:A 只在有开票计划时(现状;开过票后,销售手工调低的自动计划会被改回剩余额,changeset 已写明);B 任何计划变化都重算(覆盖销售拆分);C 销售手改过就不动(要加标记字段)。建议 A。
  3. 裁决 2A:A 照算、下限=已回款(现状);A′ 照算、下限=max(已回款, 该计划已开票额);B 按原话「不动」(开了票的订单长期显示「不符」)。这是改已有裁决,须您明确选;倾向 A′,不想再加改动就选 B。
  4. B-1(减到 0 即删,之后删开票计划补不回来):A 保持(现状,与 B-1 一致,少见,有「不符」提示);B 保留 0 元计划(推翻 B-1)。建议 A。

Seat: items ③④⑤ go back to the dev now. ② goes upstream as a lint card.

Evidence: billing.actions.ts:167, 176, 178, 182, 188; billing_milestone.hook.ts:28, 71, 82-83, 89; sales-order-amount-plan-step.flow.ts:54, 68, 74, 213, 237, 239; test/psa-prepare-billing-order-step.test.ts:213, 496, 511; _hook-api.ts:184-194; objectstack packages/lint/src/rule-explanations/flow-anti-patterns.ts:71-89; #2938 rulings 2026-12-04T08:55Z.

- billing_step_derive_remainder: the comment now says what the code does.
  The step is SET to the remainder, which lowers a generated step a person
  raised by hand above it; it no longer claims the remainder only grows.
  New case pins it (12,000 beside a 300 billing step, delete gives 10,000).
- Cancelled-order guards each have a discriminating case: a generated step
  at 1,000 received survives the cancel, and a deleted billing step, a
  Prepare Billing run on the cancelled order (still served until #2940)
  and a system write moving its total each leave it at 1,000. The earlier
  "nothing received" cancel case is kept and says it pins non-recreation
  only.
- Docs and changeset no longer say a cancelled order is never touched:
  the derivation leaves a cancelled order's generated step alone, and
  Prepare Billing still bills a cancelled order's project until #2940
  lands (linked).

Claude-Session: https://claude.ai/code/session_017xRBcUpajguxop2zMyDN9K
@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

repo:hotcrm@sunhaolin · sunhaolin · session_017xRBcUpajguxop2zMyDN9K

Head update: 50e89cf, review items ③④⑤ fixed, CI all green (2026-12-04T12:34Z)

Stays draft with pm:awaiting-merge. The 4 rulings above are pending.

@objectstack-fleet objectstack-fleet Bot added the pm:awaiting-merge PR only: landing waits on the maintainer's own review and merge (SKILL.md 复核 ②); never on an issue label Dec 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pm:awaiting-merge PR only: landing waits on the maintainer's own review and merge (SKILL.md 复核 ②); never on an issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Order to Cash: derive the order's generated payment plan step as total minus the other steps, instead of adding and subtracting per billing run

2 participants