Repository navigation
feat(o2c): derive the order's generated plan step as total minus the other steps (#2939) - #2942
objectstack-fleet[bot] wants to merge 2 commits into
Conversation
…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
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
|
repo:hotcrm@sunhaolin · sunhaolin · session_017xRBcUpajguxop2zMyDN9K Independent read-only review · head
待维护者裁决
Seat: items ③④⑤ go back to the dev now. ② goes upstream as a lint card. Evidence: |
- 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
|
repo:hotcrm@sunhaolin · sunhaolin · session_017xRBcUpajguxop2zMyDN9K Head update:
Stays draft with |
Closes #2939
Clause-②: no
Metadata-protocol: none
Metadata-impact: maintainer — a hook changed (
billing_step_give_backoncrm_billing_milestoneis replaced bybilling_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.维护者速读
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:
src/psa/actions/billing.actions.ts): inside the run'sctx.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: trueover{ 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.src/o2c/objects/billing_milestone.hook.ts):billing_step_give_backis removed and replaced bybilling_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 actx.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.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 twoassignmentnodes; the edges compare one bound string, sotest/flow-variable-conditions.test.tsstays green. The delete is a compare-and-set (multi: true). The write is by id, not a compare-and-set: amultiupdate that sets an input of the stored Open is the lint warningflow-multi-update-moves-stored-input, whichos lint --strictrefuses 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.record.statusin 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:
Rule 11:
HookCompareAndSetOptionsinsrc/sales/objects/_hook-api.tskeeps one rostered caller, renamed. The flow's by-id write is explained in its header.Docs: in-app manuals
crm_psa_billingandcrm_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
order_stepnull · 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.{ 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).order_stepnull (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 runcompleted.Ablation (each restored after its run; counts on the final 33-case file)
if (false && orderId)): 13 of 33 red.if (!(after > before)) return): 1 red, the hand-raised case (expected 12000 to be 10000).Math.max(target, 0)): 1 red, the floor-binds case.size(...) > 100): 4 red, the four total-change cases.DERIVED = 'vars.remainder'): 1 red, the 1,200 case.const plan = order): 1 red, the run on the cancelled order (order_stepnot null).expected 11900 to be 1000).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
os lint --strictrefuses a flowupdate_recordwithmulti: truewhose filter pins the row'sid(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):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) /hygieneexit 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