Skip to content

fix(o2c): an invoice recorded from a billing milestone keeps it (#2755) - #2767

Merged
sunhaolin merged 1 commit into
mainfrom
claude/inspiring-elion-643afa
Nov 25, 2026
Merged

sunhaolin merged 1 commit into
mainfrom
claude/inspiring-elion-643afa

Conversation

@sunhaolin

Copy link
Copy Markdown
Collaborator

Description

An invoice created from a billing milestone's Invoices tab → New lost the milestone: the prefilled Billing Milestone was hidden behind "Select Account first", picking the Account emptied it, and the save succeeded with no milestone and no order or contract. The milestone's Invoiced never moved, and nothing said so. This is the invoice half of #2755; the payment half is #2760.

Cause: on crm_invoice, the crm_billing_milestone lookup declared dependsOn: ['crm_account'] and no lookupAutofill. The order and contract lookups on the same object dropped dependsOn for the same reason on #2560. And no rule refused an invoice naming another account's milestone.

Metadata-impact: maintainer — a hook is added and a validation tightened (an invoice naming another account's billing milestone is now refused on save; before, it was saved). No permission set, sharing rule, object or field is added, removed or renamed; no stored data changes.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Related Issues

Related to #2755 (the payment half is #2760; this is the follow-up that PR names)
Related to #2598 (objectstack-ai/objectstack#35050: a prefilled parent does not fire lookupAutofill)

Changes Made

  • src/o2c/objects/invoice.object.ts: crm_billing_milestone drops dependsOn and gains lookupAutofill. The milestone fills the account (overwrite: true), and the order and the contract while the invoice has none (default mode), which is what the server stamps on save.
  • src/o2c/objects/invoice.hook.ts: a new hook invoice_plan_step_guard (priority 90, after invoice_row_stamp at 50, before invoice_figures at 100) refuses an invoice whose billing milestone belongs to another account, in the refusal envelope (VALIDATION_FAILED, reason milestone_named, param milestone). It judges a write that sets the milestone, or that moves the account under a milestone the invoice keeps; an edit of the amount and the paid-amount recompute are not judged.
  • src/o2c/objects/_row-stamp.ts (three lines): an invoice that names its account takes no order or contract from a milestone of another account, so the refusal names the milestone the user chose rather than an order or contract they never saw.
  • Translations of the new message in zh-CN, es-ES, ja-JP; test/refusal-envelope.test.ts count 98 → 99.
  • Tests in test/o2c-collection-plan.test.ts (invoice_plan_step_guard: an invoice's plan step belongs to its account (hotcrm#2755), 7 cases): the lookup carries no dependsOn and the autofill above; an invoice entered from a plan step keeps it and the step counts it; another account's step is refused by name on insert and on update; moving the account under a kept step is refused; a control edit is not judged.
  • Docs: src/o2c/docs/crm_o2c_invoices_payments.md + .zh.md, and content/docs/(product)/order-to-cash/invoices-and-payments.mdx + .zh-Hans + .zh-Hant.
  • Changeset .changeset/2755-invoice-keeps-billing-milestone.md (patch).

The trade-off

Without dependsOn the Billing Milestone picker no longer filters by the account: it lists every account's milestones the user may read. The server refusal replaces the filter, so a wrong pick is caught at save, not in the picker. Picking a milestone also overwrites an Account already chosen on the form with the milestone's account, as picking an order or a contract does.

When the form still carries the order or contract the milestone filled in, invoice_row_stamp (priority 50) refuses first and names that order or contract; the milestone is named once it is out of the way (measured below). The docs and the changeset say so.

Why the order and contract autofill do not overwrite

lookupAutofill with overwrite: true empties its target when the picked record's source field is empty, and a milestone carries an order or a contract, not always both. In the default mode a value already on the form stays, which is also what the server does.

Not worked around

A prefilled parent does not fire lookupAutofill (objectstack-ai/objectstack#35050, tracked on #2598), so on an invoice opened from a milestone's page the Account is still picked by hand. This PR does not work around that. What holds now: the prefilled milestone is visible, survives picking the account, and is saved.

Edges, as they stand

  • An update that names only another account's milestone (no account in the write) on an invoice with no order and no contract takes the milestone's order or contract, and with it the milestone's account. This is the stamp's existing rule for a write naming a contract alone; it is not refused.
  • An invoice that already holds an order or a contract and then picks another account's milestone may be refused by invoice_row_stamp under the order's or the contract's name, before the guard runs.

Testing

  • pnpm verify green locally (validate, typecheck, lint, hygiene, build, test)
  • New tests added (with the guard removed, the four refusal cases fail)
  • Manual testing completed, in the browser against a local dev server on its own port (18.6.0, seed data, zh-Hans UI):

Before (main, 48082b7d)

Door What was done Read back
Milestone "Globex seats on delivery" → Invoices → New Billing Milestone shows "Select Account first". Picked Account Globex Industries: the milestone field came back empty. Saved 1,000 Invoice saved with no milestone and no order. Milestone Invoiced stayed 0; its Invoices tab stayed at 0

After (this branch)

Door What was done Read back
Milestone "Globex seats on delivery" → Invoices → New Opens with Billing Milestone showing the milestone, no "Select Account first"; Account is empty (objectstack#35050). Picked Account Globex Industries: the milestone stayed. Saved 1,000 Invoice carries the milestone and its order (filled by the server). Milestone Invoiced 0 → 1,000; the invoice is listed on the milestone's Invoices tab (count 1)
Invoices list → New All pickers open with Account empty; Billing Milestone is not locked. Picked "Initech Q4 service fee": Account (Initech Solutions) and Contract (CTR-0003) filled. Saved 500 Invoice carries the account, the contract and the milestone; Due Date filled from the contract's terms. Milestone Invoiced 15,000 → 15,500
Cross-account "Initech Q4 service fee" picked (Account and Contract filled), then Account changed to Globex Industries (nothing is cleared). Save Refused, nothing saved: "CTR-0003 belongs to another account…" (the stamp, naming the contract the milestone filled in). Cleared Contract, saved again: refused, "Initech Q4 service fee belongs to another account. Choose a billing milestone of this invoice's account." (shown in zh-Hans). No invoice was created

Conflicts to expect with #2760

Both PRs are cut from main and neither has merged. This branch was kept apart from #2760's rather than stacked on it, so whichever lands second needs a small manual merge:

A trial merge of this tree with #2760's head (b1097378) gives three conflict hunks, one per file:

The five doc pages and the three translation packs are changed by both PRs and merge without a conflict.

Checklist

  • I have added a changeset
  • Metadata changes under src/ declare their class (see the Metadata-impact: line above)
  • English docs changed with their Chinese twins
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Additional Notes

Draft on purpose: a maintainer-class metadata change stays a draft for the maintainer's merge (AGENTS.md, How a green PR lands). The independent review of Who decides a change has not been run on this PR yet. Auto-merge is not armed.

New on a billing milestone's Invoices tab opened an invoice whose Billing
Milestone was prefilled but hidden behind "Select Account first", and
choosing the Account emptied it. The invoice was saved with no milestone,
so the milestone's Invoiced never moved. This is the invoice half of
#2755; the payment half is #2760.

- crm_invoice.crm_billing_milestone drops dependsOn and gains
  lookupAutofill: the account (overwrite), and the order and the contract
  while the invoice has none.
- A new hook, invoice_plan_step_guard, refuses an invoice whose billing
  milestone belongs to another account, by the milestone's name, in the
  refusal envelope. The picker no longer filters by account, so this rule
  replaces the filter, for the form as for an import or an API write.
- The row stamp takes no order or contract from another account's
  milestone when the invoice names its account, so the refusal names the
  milestone.
- Message translated in zh-CN, es-ES and ja-JP; tests, the module manual,
  the site page and a changeset ride along.

A prefilled parent still does not fire lookupAutofill
(objectstack-ai/objectstack#35050, tracked on #2598); that is not worked
around here, so the Account is still chosen by hand from a milestone's
page.
@vercel

vercel Bot commented Nov 24, 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 Nov 24, 2026 7:00am UTC

Request Review

@github-actions github-actions Bot added documentation Improvements or additions to documentation ci/cd CI plumbing and the verification pipeline metadata Declarative metadata — schema, security posture, UI surfaces backend Server-side behaviour — hooks, flows, actions labels Nov 24, 2026
@sunhaolin

Copy link
Copy Markdown
Collaborator Author

Seat record · HOLD · head 971cd977 · 2026-11-24T09:06Z

独立评审已完成。PR 自述为 maintainer 类;评审认为可按 reviewer 类处理,但它同样收紧了一条保存时的规则,并且必须排在 #2760 之后,所以和 #2760 一起交维护者决定;本席位不转 ready、不挂 auto-merge。

  • 改了什么:发票表单的「回款节点」不再依赖「客户」,选节点会带出客户、订单、合同;新增保存校验,节点属于别的客户时拒绝并写明节点名;不涉及权限,不删改字段。
  • 为什么改、谁提的:从发票 / 回款节点的「回款」页签新建回款:父记录被「请先选择客户」盖住、选客户后被清空,保存的回款没有挂到发票上 #2755(p1,本地 UI 测试发现)的发票一半:从回款节点页新建的发票丢了节点,节点的已开票金额不变。
  • 最坏情况与能否回退:少数以前能保存的写入现在被拒(主要是导入或 API 写入的、自带本客户合同却挂了别的客户节点的发票);已有错配发票只改金额或状态不受影响;无迁移,revert 即可。
  • 评审没有验证的:没有在浏览器实测表单;控制台编辑表单是否重新提交未改动的节点;批量更新时 hook 能否拿到原记录;hook 读节点是否受当前用户读权限限制;未运行测试。

Findings

  1. _row-stamp.ts:127(低,本 PR 之前就有)。
  2. 测试 nit。
  3. 文档没有写「客户」在什么情况下需要手工选择。

与 #2760 的关系:行为上不矛盾,文本上冲突(src/o2c/objects/_row-stamp.ts、test/o2c-collection-plan.test.ts、test/refusal-envelope.test.ts)。两个 PR 都把手工维护的拒绝条数从 98 改成 99,都合入后应为 100。#2760 的注释 _row-stamp.ts:112「Payments only: nothing after this hook judges an invoice's step」在本 PR 合入后需要改。后合的一个需要 rebase。

Landing:等待维护者决定;建议 #2760 先合,本 PR rebase 后再合。

@sunhaolin

Copy link
Copy Markdown
Collaborator Author

Seat record · 浏览器实测 · head 971cd977 · 2026-11-24T12:51Z

在本分支的本地服务(独立开发库,zh-CN)上实测,四项全部通过。PR 保持 draft,未改动合并状态。此前 HOLD 记录里"没有在浏览器实测表单""编辑表单是否能保存"两条就此消除。

  • 回款节点 → 发票 → 新建:节点已预填并可见,选客户后仍在;保存成功,发票保留 crm_billing_milestone,服务器补上订单;节点已开票 1000 → 3000。✅
  • 空白表单:客户为空时节点选择框可用(不再提示先选客户);选节点带出客户与合同(该节点没有订单,订单留空)。✅
  • 跨客户:客户改成另一家后保存被拒绝,未保存。先命中既有的合同规则("CTR-0003属于另一个客户…"),清掉合同后命中本 PR 的新规则:"Initech Q1 service fee属于另一个客户。请选择这张发票所属客户的回款节点。" ✅
  • 只改金额的编辑:2000 → 2500 保存成功,节点、订单、客户都保留,节点已开票 3000 → 3500。✅

Nits(不阻塞):

仍未验证:批量更新时 hook 能否拿到原记录;hook 读节点是否受读权限限制(只用管理员测过);en 语言环境。与 #2760 在 _row-stamp.ts 和两个测试文件上有文本冲突,后合并的一方需要 rebase。

@sunhaolin
sunhaolin marked this pull request as ready for review November 25, 2026 00:56
@sunhaolin
sunhaolin added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit d46679b Nov 25, 2026
21 checks passed
sunhaolin added a commit that referenced this pull request Nov 25, 2026
Resolves the conflicts with #2767 (an invoice keeps its billing milestone):
- _row-stamp.ts: one account check for both forms. A payment or an invoice
  that names its account takes nothing from an invoice or a step of another
  account, so payment_plan_step_stamp and invoice_plan_step_guard refuse it
  by its own name.
- refusal-envelope.test.ts: 100 call sites (both new refusals).
- o2c-collection-plan.test.ts: both imports kept.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Server-side behaviour — hooks, flows, actions ci/cd CI plumbing and the verification pipeline documentation Improvements or additions to documentation metadata Declarative metadata — schema, security posture, UI surfaces

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant