Skip to content

fix(psa): refuse an expense report's submission on a locked, closed or cancelled project (#2764) - #2773

Draft
sunhaolin wants to merge 3 commits into
mainfrom
claude/fix-2764-locked-project-report-submit-54f6ce
Draft

sunhaolin wants to merge 3 commits into
mainfrom
claude/fix-2764-locked-project-report-submit-54f6ce

Conversation

@sunhaolin

@sunhaolin sunhaolin commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #2764

What was wrong

On a project with Lock Time and Expenses ticked (crm_project.time_entry_locked), or on a Closed or Cancelled project, Submit on a draft expense report (submit_expense_report) was accepted: the click answered "Expense report … submitted for approval.", and a moment later the flow expense_report_project_approval (turn_back_locked / turn_back_over) set the report back to Draft and left a notice in the inbox. A timesheet on the same project is refused at the click with the reason (src/psa/objects/timesheet.hook.ts).

Which option, and why

Option A of the issue: the submission is refused at the click. The report stays in Draft, the person reads why and what to do, and no approval request or notice is created.

How

The expense package knows nothing of projects, and no hook may attach to another package's object (ADR-0130 R4), so the refusal is three validation rules contributed by Project Delivery to crm_expense_report through the objectExtensions it already uses for the report's Project field (src/psa/extensions/expense-project.extension.ts):

Rule Refuses the write that makes the report Submitted when its project is
expense_report_project_closed Closed
expense_report_project_cancelled Cancelled
expense_report_project_locked locked for time and expenses (and neither Closed nor Cancelled, so one reason is given at a time)

Each is a conditional rule (when: status becomes submitted and the report has a project; then: one hop through record.crm_project), the shape contract_same_account on crm_sales_order already uses. The engine evaluates them on every write, so the Submit button, the stage path and a plain PATCH are all covered, for every caller (an administrator is not exempt, as with the timesheet). A Draft is never refused: a report on such a project can still be written, changed and deleted. With Project Delivery disabled the rules leave with the field. No platform change was needed.

One behaviour this adds beyond the issue, please confirm it: the rules' when treats a create as a write that makes the report Submitted (previous == null), so a report created or imported directly as Submitted through the API on a locked, Closed or Cancelled project is now refused too. Before, it was accepted without that check: the turn-back flow is record-after-update and never saw a create. I kept it because the question "may this report be Submitted on this project?" has the same answer whichever write asks it, and the state machine (expense_report_status_progression) has no initialStates, so a create as Submitted is otherwise open. No seed data creates expense reports, so no seed is affected.

Every read through the project carries has() (has(record.crm_project.status), has(record.crm_project.time_entry_locked)), per AGENTS.md "Validation predicates must be TOTAL" (#630) and as the flow's own OVER / LOCKED tests do: a project row read without either key leaves the rule answerable instead of unevaluable (which would refuse the write). test/expense-report-project-rules-total.test.ts runs the three rules through the engine's evaluateValidationRules with such rows and pins no "failed to evaluate" warning, admission on an open project, and each refusal still given when only its own key is present.

The messages are translated in all four locales (src/psa/translations/{en,zh-CN,es-ES,ja-JP}.ts, objects.crm_expense_report._validations.*). They sit in Project Delivery's packs, beside the label of the Project field it contributes to the same object, so they leave with the rules.

One guard changed, please look at it: test/i18n-package-ownership.test.ts (#2352) let a module's pack carry a row for another package's object only when the row labelled fields the module contributes through objectExtensions (#2416). This is the first time a module contributes validation rules that way, so the allowance now also covers the messages of the rules it contributes (the rule's name and the name of the rule inside a conditional), and nothing else: the object's own label, a field or a rule the module does not contribute is still a stray. A positive control pins that Project Delivery words exactly its six rule names in every locale.

The turn-back flow stays as the backstop

src/psa/flows/expense-report-project-approval.flow.ts keeps turn_back_locked / turn_back_over unchanged (only its header comment changed). They are now reachable only when a project is closed or locked between the rule's read and the flow's read. A test opens that window (it holds the flow, submits on an open project, changes the project, then runs the flow) and pins that the report still ends in Draft with the notice.

What Submit for Reimbursement and Submit for a Trip now do

submit_for_reimbursement and submit_trip_for_reimbursement share one script (src/expense/actions/expense.actions.ts). It groups the selected expenses into one draft report per project and submits each. It now catches the rule's refusal per report and carries on:

  • reports of open projects and of no project are submitted as before;
  • the report of a locked, Closed or Cancelled project is kept in Draft in My Reports with its expenses on it;
  • if any report was refused, the click ends with HTTP 400 VALIDATION_FAILED and a message that names each refused report with the rule's reason, then says how many other reports were submitted. Before, it reported success for all of them and the flow turned the refused ones back.

The expense package still does not import or name anything of PSA: it only reacts to a VALIDATION_FAILED from the update.

Known limits

  1. The refusal does not name the project. On @objectstack/* 18.6.0 a rule's message is fixed text (no record values are interpolated), so it says "This report's project …" where the timesheet's hook names the project. The report page shows the project beside it.
  2. The sentences around the reason in the two bulk actions are English ("Expense report ER-0002 was not submitted: … It stays in Draft in My Reports …"); the reason inside them is in the caller's language. A list refusal cannot be expressed through the action refusal catalogue on 18.6.0. That is zh-CN:「提交报销」「按出差申请报销」的拒绝原因是英文 #2759, blocked by refusals: one throw answers one declared sentence — an action that refuses a selection for several reasons at once cannot list them in the person's language (after #34273) objectstack#36877, and is not worked around here.

Measured

Booted stack (@objectstack/verify), test/psa-approvals.test.ts:

  • POST /api/v1/actions/crm_expense_report/submit_expense_report on a locked, Closed or Cancelled project → 400 {"success":false,"error":{"code":"VALIDATION_FAILED","message":…}}; with accept-language: zh-CN the message is the Chinese sentence. Afterwards the report and its lines are draft, there are 0 turned-back notices, 0 approval requests and no flow run.
  • A plain status write is refused the same way for the consultant and for the administrator.
  • The reason exists in en, zh-CN, es-ES and ja-JP for all three rules.
  • The draft can still be edited and deleted, and submits once the project is unlocked.
  • Bulk actions, four cases (submit_for_reimbursement over a locked and over a Closed project, submit_trip_for_reimbursement over a locked and over a Cancelled one), each with one expense on the refusing project, one on an open project and one on no project: 400 VALIDATION_FAILED, message Expense report <number> was not submitted: <reason in zh-CN> It stays in Draft in My Reports, with its expenses on it. 2 other reports were submitted.; the refused report and its expense are draft and no flow ran on it; the other two reports are submitted, each with one open approval request.
  • The backstop, for a locked and for a Closed project: with the flow held, the submission passes the rule on an open project, the project then locks or closes, and the flow run owed to that write turns the report and its lines back to Draft with the matching notice and no approval request.

Browser, my own dev server on port 4002, dev login button (Dev Admin), UI in zh-CN:

Door Project Request What the screen showed
Expense list, bulk 提交报销 (1 expense) locked submit_for_reimbursement → 400 result dialog "操作未对所选记录全部完成。…" with "Expense report ER-0001 was not submitted: 这张报销单所属的项目已锁定工时与费用,因此不能提交。先保留为草稿,待项目经理在项目上取消勾选“锁定工时与费用”后再提交。 It stays in Draft in My Reports, with its expenses on it. No report was submitted."
Report page, 提交 button locked submit_expense_report → 400 red error toast with the Chinese locked sentence; no "已提交审批"; status stays 草稿
Report page, stage path 移至已提交 → 移动 locked PATCH /api/v1/data/crm_expense_report/:id → 400 red inline banner with the same sentence; status stays 草稿
Report page, 提交 button Closed 400 red error toast "这张报销单所属的项目已关闭,不再接受费用,因此不能提交。请删除这张报销单,把其中的费用改挂到进行中的项目,再重新提交。"
Report page, 提交 button Cancelled 400 red error toast "这张报销单所属的项目已取消,不再接受费用,因此不能提交。…"
Expense list, bulk 提交报销 (2 expenses, 2 locked projects) locked 400 dialog names ER-0002 and ER-0003, each with the Chinese reason, then "They stay in Draft in My Reports, with their expenses on them. No report was submitted."
Report page, 提交 button, after the lock was unticked open submit_expense_report → 200 green toast "报销单 2026-11-24 已提交审批。"; status 已提交, approval pending

After the locked-project refusals: sys_notification 0 rows, sys_approval_request 0 rows, the report and its expense draft, the expense still on the report.

Docs

  • In-app manuals: src/expense/docs/crm_expense_recording.md / .zh.md, src/psa/docs/crm_psa_approvals_budget.md / .zh.md.
  • Site: content/docs/(operate)/administration/automation, content/docs/(product)/expenses/index, content/docs/(product)/projects/time-and-expenses, each in en, zh-Hans and zh-Hant.
  • docs/feature-inventory.md, docs/architecture/psa-module-plan.md, docs/architecture/expense-module-plan.md.
  • Changeset: .changeset/2764-report-submit-refused-on-locked-project.md (patch).

Possible conflicts

Two open sibling changes touch files this PR touches; whichever lands second needs a rebase:

Checks

pnpm verify locally at bad5b98c (this branch with origin/main af7025cd merged in): validate, typecheck, lint, hygiene and build green. The test step was run on a machine at load average ~150: 322 of 334 files passed, and the 29 failures in 12 files (case assignment, quotes, forecasts, seeds and others, most far from this change) were 5 s timeouts and transaction-already-complete faults from the load. Re-run alone, 8 of those files passed outright and the last 4 (flow-billing-handoff, global-actions, psa-approvals, psa-budget-versions) failed only on the 5 s timeout and passed whole with --testTimeout=60000 (109 tests). CI is the clean run.

Metadata-impact: maintainer — a validation rule tightened (three rules contributed by Project Delivery to crm_expense_report refuse a submission that was accepted and then turned back; no change to who can see or change what)

…r cancelled project (#2764)

Submit on a draft expense report of a project with Lock Time and Expenses
ticked, or of a Closed or Cancelled project, answered "submitted for
approval" and the flow expense_report_project_approval then set the report
back to Draft with a notice. A timesheet on the same project was refused at
the click.

Project Delivery now contributes three validation rules to
crm_expense_report through objectExtensions (ADR-0130 R4: no hook on another
package's object). They refuse the write that makes the report Submitted,
with the reason and what to do, in en, zh-CN, es-ES and ja-JP. A Draft is
never refused. The flow keeps its turn-back branches as the backstop for a
project that locks or closes between the rule's read and the flow's.

Submit for Reimbursement and Submit Against a Travel Request submit the
reports of open projects and of no project, keep the refused report in
Draft, and end the click with an error naming it, where they reported
success for all.

The locale-pack ownership guard (#2352) now also lets a module word the
rules it contributes to another package's object, as it already let it
label the fields it contributes.

Fixes #2764
@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 25, 2026 2:53am 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 f74252e6 · 2026-11-25

独立评审(只读子代理,xhigh,未参与改动)结论:通过(有 nit),建议合并前补第 1 条守卫。Metadata-impact 同意 maintainer — 校验规则收紧(不涉及权限、共享或删数据),PR 保持 draft 交维护者合并;本席位不转 ready、不挂 auto-merge。

  • 改了什么:PSA 通过 objectExtensions 给 crm_expense_report 加三条 conditional 校验规则:在已关闭、已取消或已锁定的项目上提交报销单直接以 400 VALIDATION_FAILED 拒绝,不再先提交成功再被流程退回草稿;流程退回分支保留兜底。批量提交时被拒的那张留在草稿、其余照常提交,最后逐张说明。四语提示、三语文档与测试同步。
  • 为什么改、谁提的:锁定项目上提交报销单:提示「已提交审批」,报销单却被退回草稿 #2764(本地 UI 测试发现):工时单在锁定项目上直接被拒,报销单却「先成功再退回」,两者不一致。
  • 最坏情况与能否回退:本该能交的报销单被误拒(见第 1 条);不删数据、不改权限;撤掉三条规则与 bulk 的 try/catch 即回退,流程兜底仍在。
  • 评审没有验证的:Mongo / memory 驱动下项目记录缺 time_entry_locked 时的实际表现;浏览器界面未复跑。

Findings

  1. src/psa/extensions/expense-project.extension.ts:124,129,136(低,本 PR):then 里跨一跳读 record.crm_project.status / .time_entry_locked 没有 has() 守卫,与 AGENTS.md「每个 record.x 读取都要守卫」及流程里的写法不一致;在返回缺失键的驱动上谓词无法求值会误拒。先例 contract_same_account 同样写法,故不阻塞,建议补上。
  2. src/expense/actions/expense.actions.ts:111-120(低,PR 已自述):批量拒绝提示是英文片段包住本地化原因,不是每语一句整话;由 zh-CN:「提交报销」「按出差申请报销」的拒绝原因是英文 #2759 跟踪,受 refusals: one throw answers one declared sentence — an action that refuses a selection for several reasons at once cannot list them in the person's language (after #34273) objectstack#36877 阻塞。
  3. nit:docs/feature-inventory.md:290 EXP-004 仍写「全部或全不」,批量现可部分成功;src/expense/actions/expense.actions.ts:149 注释 "before anything is written" 与 155 行新段落冲突。
  4. nit:PR 正文第 34 行动作名写作 "Submit Against a Travel Request",实际标签为 "Submit for a Trip"(文档写对了)。
  5. nit(行为变化):expense-project.extension.ts:78 的 previous == null 让新建或导入时即为 submitted 的报销单也被拒——以前只在更新时触发的流程看不到这种插入,建议 changeset 提一句。
  6. 与 fix(expense): Mark as Paid refuses in the caller's language (#2763) #2765 无冲突:fix(expense): Mark as Paid refuses in the caller's language (#2763) #2765 改 expense.actions.ts 287–350 行,本 PR 改 79–160 行。

已核实:规则条件与流程 OVER / LOCKED 分支一致;在途报销单不受影响;驳回后重交被拦;表单直改状态与管理员写入均被覆盖;四语齐全;en / zh-Hans / zh-Hant 文档与中英手册已更新;测试覆盖 issue 列出的每种提交方式与兜底路径。

…2764)

Review follow-ups on #2773:
- has() on record.crm_project.status / .time_entry_locked in the three
  rules (AGENTS.md #630), pinned by test/expense-report-project-rules-total.test.ts
  through evaluateValidationRules, including a report created as Submitted.
- EXP-004 in docs/feature-inventory.md: a bulk submit can now partly succeed.
- expense.actions.ts: the 'before anything is written' comment names its
  one exception.
- Changeset: the trip action is Submit for a Trip; a report created or
  imported directly as Submitted is refused too.

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

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