Skip to content

fix(so): close five sales order audit defects - #631

Open
privatejoel wants to merge 1 commit into
mainfrom
claude/musing-robinson-861e92
Open

privatejoel wants to merge 1 commit into
mainfrom
claude/musing-robinson-861e92

Conversation

@privatejoel

Copy link
Copy Markdown
Collaborator

Summary

This PR fixes five defects that the 2026-10-19 audit of the sales order path found. The plan in docs/SO_EXECUTION_COHESION.md (C-1 to C-38) does not cover them. This PR does not touch the branches of #622 to #630.

  1. A writer could set an approval. PATCH /api/orders/[id] copied approval from the body. A PATCH with {approval:{payloadHash:<current>}} and no status wrote an approval with no approver. Now approval is not in APPROVE_INPUTS. Only the approve branch writes it, with approvedBy set to the approver. POST /api/orders took status, approval and payload_hash from the body and checked only the coarse write verb. Now it always makes a DRAFT with no approval and no hash, and it needs so.write.
    • Callers checked: no server code calls this POST. so-intake.tsx sends DRAFT. The legacy local-storage mirror in anvil-client.js sends status, approval and payload_hash. The server now ignores these fields, so a mirrored order arrives as DRAFT.
  2. Lines stayed editable after approval, and the Tally retry pushed with no check.
    • so-workspace.tsx: lines are editable only in DRAFT, PENDING_REVIEW and BLOCKED. Before, APPROVED, FAILED_TALLY_IMPORT and DUPLICATE were also editable. The unused so.edit_after_approval action is deleted from rbac.ts (nothing read it, and the server did not have it).
    • tally/retry.js: before it pushes a queued row, the drain reads the order in the row's tenant. It skips the row if the order is not found, is cancelled, has no approval, or has a hash that is not the hash on the row. A skipped row gets status = gave_up and last_error = "skipped::<reason>". Its payload_xml stays. If the order read fails, the row stays pending and goes through the usual backoff. The orders update after a push now filters on tenant_id.
  3. The Tally voucher printed RATE 0 when the PO printed no rate. computeLineTax and inventoryEntryXml read line.rate || line.unitPrice, so a rate that the reconciler filled in discounted_unit_price was lost. Both now use soLineRate and soLineQty (_lib/so-line-money.js). voucher_pdf.js gave computeLineTax an object that it built field by field and that did not include _mapped_item, so a GST rate that only the item master had printed as 0%. Also, the PDF read discounted_unit_price first, so it could print a rate that was not on the voucher. It now gives the whole line to computeLineTax and prints soLineQty and soLineRate.
  4. SO intake kept the lines of the previous PO. The intake set the lines only when it got some lines, and "remove" cleared only the document. A new resetExtraction() clears lines, evidence, extraction meta, the large-PO flag and the extracted customer. It runs at the start of each extraction and on remove.
  5. The Excel export returned a 500. orders/export.js selected orders.payment_terms, but no migration makes this column. It is removed from the select and from the Payment Terms fallback. The row still reads salesOrder.payment_terms, then the customer block.

Deploy notes

  • No migration. No new environment variable.
  • POST /api/orders now returns 403 ACTION_FORBIDDEN for finance, procurement and operator. These roles are read-only on sales orders, and no screen shows them a create button.
  • After the deploy, the first retry drain moves queued rows for cancelled, unapproved or changed orders to gave_up with skipped::<reason>. tally/health counts these rows as give-ups.
  • No new requireAction name. so.write is already in SERVER_ACTIONS and in rbac.ts.

Tests

New or changed tests (all behavioural, no source grep):

  • api-so-write-gate.test.js: a writer PATCH with an approval and no status leaves approval null (PENDING_REVIEW and APPROVED). An approve stores approvedBy. POST with a forged status, approval and hash stores DRAFT with null approval and null hash. POST by finance, procurement or operator returns 403 and inserts no row.
  • so-workspace-manual-lines.test.tsx: the add and remove controls show on DRAFT, PENDING_REVIEW and BLOCKED. They do not show on APPROVED, FAILED_TALLY_IMPORT, DUPLICATE or EXPORTED_TO_TALLY, and the read-only notice gives the status.
  • api-tally-mode-b.test.js: the fixtures now queue rows for approved orders with a matching hash. New "order guard" tests: no push for a cancelled order, an order returned for a fix, an order edited after it was queued, or an order of another tenant. The skipped row keeps its XML. A failed order read keeps the row pending. The exported status goes only to the row's tenant.
  • api-tally-build-voucher.test.js: a line with no PO rate and a reconciler rate prints <RATE>120/Nos</RATE> and the correct tax.
  • api-orders-voucher-pdf.test.js: the item master GST rate is used. The PDF prints the PO rate, as the voucher does.
  • so-intake-extraction-reset.test.tsx (new): a second PO with 0 lines, and a removed PO, give an order with no lines from the first PO.
  • api-orders-export.test.js: the fake database now refuses a select that names orders.payment_terms, as the live database does.

Mutation check. I reverted each fix one at a time and ran its test file. All 21 mutations failed their tests:

# Mutation Result
M1 "approval" back in APPROVE_INPUTS killed
M2 POST takes body.status killed
M3 POST takes body.approval killed
M4 POST takes body.payload_hash killed
M5 POST without requireAction so.write killed
M6 APPROVED in LINE_EDIT_STATUSES killed
M7 BLOCKED not in LINE_EDIT_STATUSES killed
M8 retry ignores CANCELLED killed
M9 retry ignores a missing approval killed
M10 retry ignores a changed hash killed
M11 retry order read without tenant filter killed
M12 retry order update without tenant filter killed
M13 retry skips (not retries) on a read error killed
M14 retry skip clears payload_xml killed
M15 computeLineTax reads rate || unitPrice killed
M16 inventoryEntryXml reads rate || unitPrice killed
M17 voucher_pdf gives a hand-picked object to computeLineTax killed
M18 voucher_pdf prints discounted_unit_price first killed
M19 no reset at the start of an extraction killed
M20 remove does not reset killed
M21 export selects payment_terms killed

Gates on this branch:

  • npm ci in the worktree.
  • npm run check: pass.
  • npm run lint: 0 errors. The changed lines add no warnings.
  • npm run build: pass.
  • npm run verify: pass. It rewrote docs/RBAC_AUDIT.md and docs/WRITE_PATH_AUDIT.md with old drift that this PR did not cause. I restored both files.
  • npm run eval:golden: pass, no regressions.
  • NODE_OPTIONS=--no-experimental-webstorage npx vitest run: 627 files, 7390 tests, all pass.

Not covered

  • The server still accepts a result edit on an APPROVED order. The edit clears the approval, but the status stays APPROVED. The lock in this PR is in the workspace only.
  • FAILED_TALLY_IMPORT has no direct transition to DRAFT in ALLOWED_TRANSITIONS. To fix its lines, a user must move it to APPROVED and then return it for correction.
  • The retry guard does not check approval_expires_at. tally/push.js does not check it either.
  • A skipped retry row does not change its tally_voucher_records row.
  • _lib/tally-reconciler.js (the missing_in_tally auto-fix) inserts into tally_retry_queue with columns that do not exist (tally_voucher_record_id, attempt, reason) and without the NOT NULL voucher_type, payload_xml and payload_hash. That insert fails today. This PR does not change it.
  • Remove in the intake does not clear the selected customer. Only the extraction state is cleared.
  • The legacy mirror in anvil-client.js still sends status, approval and payload_hash. The server ignores them. I did not remove them from the client.

🤖 Generated with Claude Code

- A writer cannot set an approval. The PATCH no longer copies
  "approval" from the body; only the approve branch writes it, with
  the approver's id. POST /api/orders makes a DRAFT with no approval
  and no hash, and needs so.write.
- Lines are editable only in DRAFT, PENDING_REVIEW and BLOCKED. The
  unused so.edit_after_approval action is deleted.
- The Tally retry drain pushes a queued row only while its order is
  not cancelled, is approved, and has the hash the row was queued
  with. Otherwise the row leaves the queue with the reason and keeps
  its XML. The order update is scoped to the row's tenant.
- The Tally voucher XML and the voucher PDF read rate and quantity
  through soLineRate/soLineQty. The PDF gives the whole line to
  computeLineTax, so the item master GST rate is used.
- SO intake clears all extraction state at the start of each
  extraction and on remove.
- The Excel export no longer selects orders.payment_terms, which no
  migration creates.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vercel

vercel Bot commented Oct 21, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated
anvil Ready Ready Preview Oct 21, 2026 2:36am UTC

This branch was successfully deployed

1 active deployment
Preview — be385aca Deployed Oct 21, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant