Skip to content

Architecture: Central pricing/adjustment service & financial ledger boundary #369

Open

Description

@khaira777

Problem

Order pricing, discounts, charges, split checks, and payment tender validation are currently distributed across main/routes/bills.ts, main/routes/orders.ts, shared/charges.ts, and main/services/tax.ts. Upcoming features - Promotion Engine (#359), Customer Types (#633), Combos (#366), Tipping (#364), Gift Cards (#363), and Payment-Method Surcharges - all intersect this boundary. Additionally, shared/charges.ts currently evaluates percentage fees independently against net or gross item subtotal only (no compound fee-on-fee ordering, Fork Audit TAX-002), and main/services/tax.ts only maps 3 hardcoded charge tax kinds (packaging, delivery, service_charge).

Codebase Anchor

  • main/routes/bills.ts:96-99,175,485-491,548-556,1753-2045 (inline payment tender validation, rounding, split checks split_group_id, manual discount endpoints, payment_idempotency).
  • shared/charges.ts:14-50,273-316 (ChargeDefinition, calculateAppliedCharges - integer minor-unit math, currently only net | gross basis) & main/services/charges.ts:90-140 (resolveOrderCharges retaining applied snapshots).
  • main/services/tax-engine.ts:209-236 (already decoupled with Decimal.js and tax-on-tax baseRuleIds) & main/services/tax.ts:71-85,282-301 (ChargeTaxKind limited to 3 standard charge kinds).
  • main/services/refund.ts (already decoupled refund ceiling & amount_cents minor units), main/routes/cash-sessions.ts & main/routes/cash-closures.ts.

Core Invariants & Constraints

  • Backend authority (Core Invariant Add AppX tile assets to resolve Windows Store certification failure (10.1.1.11) #5): All order totals, adjustments, compound charges, taxes, and tender allocations remain strictly backend-authoritative.
  • Applied charge snapshots (docs/reference/product-invariants.md:186-216): Disabling or editing a charge/promotion definition must never mutate existing open or historical orders.
  • Zero regression on existing flows: Existing manual discounts, tax packs, split checks, and refund ceilings must pass all current suites untouched (npm run test:charges-engine, npm run test:refunds, tax suites).

Scope / Acceptance Criteria

  • Phase 1 - Payment Tender Extraction (absorbed from #282): Extract payment tender validation, split allocation, rounding remainder distribution, and multi-tender reconciliation out of main/routes/bills.ts into main/services/payment-tender.ts with unit/boundary tests independent of dining/table assumptions, plus tax-snapshot contract tests.
  • Phase 2 - Central Adjustment & Compound Fee Boundary (includes Fork TAX-002): Introduce main/services/pricing.ts owning the single calculation pipeline (items -> order_adjustments snapshot -> charges [with optional ordered/compound fee-on-fee basis in shared/charges.ts] -> tax engine [supporting arbitrary custom charge tax categories in main/services/tax.ts] -> payable total) so #359, #633, #366, #364, and payment surcharges plug into one module.
  • Phase 3 - Financial Ledger Boundary: Define the clean write interface feeding payments, refunds (#278), gift cards (#363), tips (#364), and cash sessions (#279) without four disconnected accounting tables.
  • All tests wired into package.json and passing (npm test).

Concrete Design Decisions to Lock Before Coding

  1. Incremental rollout: Confirm Phase 1 (payment-tender.ts extraction, zero schema change) ships first as a pure refactor PR before Phase 2 adds order_adjustments and compound charge ordering.
  2. Compound fee ordering (TAX-002): Confirm whether compound charges use an explicit sort_order + calculation_basis: 'net' | 'gross' | 'post_charges' (preventing circular fee dependencies) and how receipts display compound basis clarity.

Out of Scope

  • Replacing all existing REAL SQLite columns in one shot (tracked separately under #285 persistence boundary).
  • Payment-gateway SDKs or card-surcharge UI/legal policy (tracked separately in Payment-Method Surcharges design issue, gated on Phase 1).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:backendenhancementpriority:highstate:needs-designtype:design

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions