Skip to content

fix(bills): harden split-check transaction and minor-unit allocation - #269

Closed
khaira777 wants to merge 3 commits into
mainfrom
fix/253-split-check-allocation
Closed

khaira777 wants to merge 3 commits into
mainfrom
fix/253-split-check-allocation

Conversation

@khaira777

@khaira777 khaira777 commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #253

Audit Findings & Verification

This PR addresses FloCafe audit issue #253. Following empirical testing on origin/main, the issue was PARTIALLY CONFIRMED:

CONFIRMED:

  1. Claim C (Rounding Underflow): Proportional rounding via Math.round() could cause cumulative rounded allocations to exceed the source total, producing negative amounts on the final guest check (e.g. $0.02 split across 4 checks produced $0.01, $0.01, $0.00, -$0.01 for Guest 4).
  2. Claim D (void_adjustment Exposure): Queries filtering status NOT IN ('cancelled', 'voided') failed to exclude synthetic void_adjustment rows, forcing POS operators to allocate non-physical void reversal lines across guest checks.
  3. Claim A (Transaction-Boundary Hardening): Mutable DB-derived split state was validated outside the transaction. This was a structural transaction-boundary weakness. Validation, item normalization, allocation reads, and persistence are now unified inside withTxn().

NOT REPRODUCED:

  1. Claim B (Concurrent Race Condition): FloCafe runs on a single Node.js event loop with synchronous better-sqlite3 execution. Simultaneous HTTP requests are strictly serialized by Node.js, so concurrent HTTP requests do not create duplicate split groups.
  2. Claim E (Repeat-Safety Failure): Sequential duplicate requests hit split_group_id / bill count checks and are cleanly rejected with HTTP 409 Conflict (This check has already been split).

Summary of Changes

  • Single Transaction-Local Validation: Removed duplicated pre-transaction DB reads and normalization. All DB-derived validation, item mapping (txnItemById), check validations, normalized allocations (txnNormalized), weights, and bill_items insertions are performed 100% authoritatively inside withTxn().
  • Largest-Remainder Integer Allocation: Replaced floating-point Math.round() logic with integer minor-unit largest-remainder (Hamilton/Hare-Niemeyer) allocation algorithm, guaranteeing exact reconciliation without negative allocations.
  • Support for Nested & Flat Tax Breakdowns: Handles both nested TaxBreakdown[][] (array of per-item component arrays) and legacy flat TaxBreakdown[] arrays. Reconciles component allocations to check tax amounts and source breakdown totals exactly in minor units while preserving original JSON structures.
  • void_adjustment Exclusion: Updated SQL queries in main/routes/bills.ts and item filtering in frontend/src/components/pos/SplitCheckModal.tsx to exclude void_adjustment rows.
  • Regression Test Expansion: Added 8 comprehensive deterministic test scenarios A-H to tests/payment-methods-split-checks.test.ts (79/79 assertions passing).

@khaira777 khaira777 self-assigned this Aug 12, 2026
@khaira777 khaira777 changed the title fix(bills): harden split-check transaction and minor unit allocation (Fixes #253) fix(bills): harden split-check transaction and minor-unit allocation Aug 12, 2026
@khaira777 khaira777 added bug area:backend Backend (Express/SQLite) area:frontend Frontend (React/Next.js) labels Aug 12, 2026
@khaira777
khaira777 marked this pull request as ready for review August 12, 2026 22:38
@khaira777
khaira777 requested a review from itsbkm as a code owner August 12, 2026 22:38
@khaira777
khaira777 marked this pull request as draft August 13, 2026 04:21
@khaira777

Copy link
Copy Markdown
Contributor Author

PR #269 is intentionally marked draft while the independent verification is addressed.

The core transaction-boundary, integer allocation, void_adjustment exclusion, and repeat/concurrency behavior were independently verified. One residual defect remains: when a split bill contains both categorized item tax snapshots and configured charge-tax snapshots (for example packaging/delivery/service tax), each child bill's total reconciles but the receipt/thermal tax components can misattribute item tax versus charge tax. Please fix this on the existing fix/253-split-check-allocation branch and keep the PR scoped to #253.

Required follow-up:

  • Allocate/persist split-specific item and charge tax snapshots in minor units, or make each per-bill resolver consume the already allocated child breakdown consistently.
  • Add a behavioral regression using categorized item tax plus a configured charge tax.
  • Assert each child receipt/print tax component, not only total tax, and exact aggregate reconciliation.
  • Preserve the current largest-remainder non-negative allocation and void_adjustment exclusion.
  • Re-run focused split tests, the full suite, backend/frontend lint and builds, and CI/no-mistakes validation against current main.
  • Do not merge this PR. Report the updated green PR when complete.

@khaira777

Copy link
Copy Markdown
Contributor Author

Superseded by the completed implementation in #295. Closing this draft to avoid duplicate delivery.

@khaira777 khaira777 closed this Aug 13, 2026
@khaira777
khaira777 deleted the fix/253-split-check-allocation branch August 13, 2026 23:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:backend Backend (Express/SQLite) area:frontend Frontend (React/Next.js)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(checkout): make split checks atomic and allocation-safe

1 participant