Repository navigation
Description
Summary
FloCafe currently has two different subtotals that order-tax rescaling runs against, and which one is used depends on the call site rather than on a single rule. The order-total recomputation made this explicit in main/services/orders.ts so the divergence would stop being invisible, but the underlying money-formula question is still unanswered. This issue records the divergence, the sites, the conditions under which the two disagree in practice, and what a decision needs to settle. It is deliberately a decision issue, not a bug report: the current behaviour is long-standing and no one has yet established that it produces wrong numbers in a reachable case.
What the divergence is
recomputeOrderTotals in main/services/orders.ts selects its denominator from an explicit subtotalBasis union:
export type OrderSubtotalBasis =
| { subtotalBasis?: 'fresh-items' }
| { subtotalBasis: 'stored-order'; storedSubtotal: number };and applies it here:
const subtotal = input.subtotalBasis === 'stored-order' ? input.storedSubtotal : totals.subtotal;
const discountedSubtotal = Math.max(0, subtotal - discountAmount);
...
taxRatio = subtotal > 0 ? discountedSubtotal / subtotal : 1;
taxAmount = Number((totals.totalTax * taxRatio).toFixed(decimals));The numerator (totals.totalTax, totals.exclusiveTax, and every per-item tax breakdown) always comes from a fresh sum of the order's current non-terminal items via calculateOrderTotals. The denominator comes either from that same fresh sum (fresh-items, the default) or from a persisted column (stored-order). When those two disagree, the tax ratio is a ratio of a fresh number to a stale one.
The call sites
There are six call sites for recomputeOrderTotals in main/. Four use the fresh sum and two pass a stored column:
| Site | Basis | Value passed | Rewrites the stored column? |
|---|---|---|---|
main/routes/orders.ts:931 |
fresh-items (default) |
- | yes, orders.ts:943/:947 |
main/routes/orders.ts:1597 |
fresh-items (default) |
- | yes, orders.ts:1607 |
main/routes/orders.ts:1822 |
fresh-items (default) |
- | yes, orders.ts:1836/:1845 |
main/routes/orders.ts:1994 |
fresh-items (default) |
- | yes, orders.ts:2004 |
main/routes/orders.ts:1388 |
stored-order |
currentOrder.subtotal |
no |
main/routes/bills.ts:2237 |
stored-order |
bill.subtotal |
n/a, separate bills row |
main/routes/orders.ts:1388 is the order-level discount edit. Its own comment records the important detail: unlike every other site, it does not rewrite orders.subtotal. So on that path the stored column is the value as of the previous write while the item tax base is the value as of right now.
main/routes/bills.ts:2237 is the bill settlement path. It passes the bill's own subtotal and also uses taxScaling: 'always', which rounds tax even with no discount applied. Treating the bill as a frozen settlement document with its own subtotal is defensible on its own terms; the open question is whether that is the intent or just historical behaviour.
Why this can matter
The two sites agree whenever the stored column is current, which is the common case, and that is presumably why this has never surfaced as a report. They diverge when the stored column is stale, and the reachable ways to get there are:
- The discount edit itself. On
orders.ts:1388the column is deliberately not rewritten, so any change to the order's items between the last write and the discount edit makes it stale by exactly the size of that change. - Terminal-item transitions. Cancelling, restoring, or refunding an item changes which items count toward the fresh sum. The other four sites rewrite the column as part of the same transaction, so they stay consistent; the discount site does not.
- Partial failure or a crash between statements in a multi-statement mutation, leaving the column behind the item rows.
- A restored backup, where a restored
orders.subtotalmay predate the item rows it is restored alongside.
The consequence is directional, not symmetric. Because the numerator is always fresh, a stored subtotal that is too large produces a taxRatio that is too small and under-charges tax; a stored subtotal that is too small produces a ratio that is too large and over-charges. There is a sharper edge in the clamped branch: if the stale stored subtotal is smaller than the discount, Math.max(0, subtotal - discountAmount) yields 0, so taxRatio becomes 0 and the entire order's tax is zeroed. That last case is a hypothesis derived from reading the code, not a reproduced result, and confirming or refuting it should be part of whichever way this is decided.
What a decision needs to settle
- Is the fresh item sum authoritative for tax rescaling? If yes, both
stored-ordercall sites change, and the bill settlement path needs a stated reason for being the exception, if it remains one. - What is the bill's role? A settlement document that is frozen at its own subtotal is a coherent model, but it has to be stated as the rule rather than inherited as an artifact of which column the code happened to read.
- What happens to historical orders? Orders whose stored subtotal predates the change would recompute to a different tax figure under the fresh-sum rule. Whether existing bills and reports are restated, left as recorded, or recomputed on read is a separate call with accounting consequences.
- Does the stale-low path need a guard independent of the basis decision? The zeroed-tax branch above is reachable only when the stored subtotal is stale, but a clamp that silently produces zero tax is worth an explicit decision either way.
Recommendation
Decide in favour of the fresh item sum for order mutation paths, because the numerator is already fresh there and a mixed numerator/denominator is the actual defect, and keep the bill's own subtotal as an explicit, documented exception for the settlement boundary. Before changing anything, reproduce the stale-low case in a test so the fix is proven against a failure rather than against a reading, and decide the historical-orders question with an accountant rather than a refactorer.
Notes on provenance
The explicit subtotalBasis parameter and these site comments were introduced by the route-registrar extraction (PR #874, commit ee216563), which deliberately did not normalize the two bases because doing so is a money-formula change rather than a refactor. The fresh-items default means any future call site silently gets the fresh sum, which is the safer default but also means the divergence will not be caught if a new site copies the stored-order pattern without the reasoning attached.
Activity
khaira777 commentedon Sep 26, 2026
Captain decision recorded, and implementation under way.
The fresh item sum is authoritative for tax rescaling everywhere; the stored-column split and its type are removed rather than left dormant; the order and unpaid-bill discount paths heal the stored subtotal inside the same transaction that already writes the row, so healing is never partial; a paid bill stays immutable; no history is migrated or backfilled, because recorded transactions are audit-trail evidence and closed cash drawers must still reconcile against them; and the shared recomputation clamps the effective discount to the subtotal it is deducted from so the ratio can never wipe out an order's tax.
The sharp case is now a required test rather than a hypothesis: a stored subtotal that has fallen below the discount currently zeroes the order's tax entirely, and must leave tax standing after this.
A worker is building it in its own pull request, with the characterisation committed and shown failing first, plus engineering, product, security and quality reviews of its own diff before it reports ready. The full four-platform matrix is the release gate, because this is a money path.
I will link the pull request here the moment it is open.