Skip to content

Architecture [Needs Review]: Integer minor-unit Money foundation (main/core/money.ts) #285

Open

Description

@khaira777

Problem

Legacy catalog and transactional columns (products.price, orders.total, bills.subtotal/tax_amount/total) are stored as SQLite REAL floats and manipulated with JS floating-point numbers in several older routes, creating drift risk across 0-decimal (JPY/KRW), 2-decimal (USD/INR), and 3-decimal (KWD/BHD/OMR) currencies. Per maintainer guidance, FloCafe does not need a disruptive big-bang rewrite of every table, but it does need a single canonical minor-unit Money helper and clear persistence/calculation boundary rules for all new and refactored financial services (#369, #364, #363, #1342).

Codebase Anchor

  • Existing minor-unit & exact-decimal foundations:
    • main/countries.ts (getCurrencyFractionDigits, getCurrencyMinorUnitFactor - supports 0, 2, and 3 decimal currencies).
    • shared/charges.ts:8-11,276-306 (integer minor-unit calculation using tenant fraction digits).
    • main/services/tax-engine.ts:10-144 (Decimal.js with ROUND_HALF_UP / ROUND_HALF_EVEN / FLOOR / CEILING).
    • main/services/refund.ts (refunds.amount_cents minor-unit storage per docs/reference/product-invariants.md:278-283) and main/routes/cash-sessions.ts / main/routes/cash-closures.ts (*_cents integer columns).
    • main/routes/bills.ts:96-99,175,548-556 (inline minor-unit rounding and split remainder distribution).

Core Invariants & Constraints

  • Currency precision agnosticism (docs/reference/product-invariants.md:278-283): Never hardcode * 100; always derive factor (1, 100, 1000) from getCurrencyMinorUnitFactor(currency).
  • Data safety (Core Invariant fix(windows): add Express path-rewriting middleware for Next.js static exports #2): Legacy REAL columns on existing installs must stay backward-compatible across migrations without rounding distortion on historical bills.

Scope / Acceptance Criteria

  • Extract a single shared minor-unit helper (shared/money.ts) consolidating toMinorUnits, fromMinorUnits, roundMinor, percentageMinorUnits, and penny-safe allocateMinor (largest-remainder / proportional split) so shared/charges.ts, main/services/payment-tender.ts (#369), and frontend previews never duplicate rounding math.
  • Document the persistence boundary contract in docs/architecture/: all new financial columns store INTEGER minor units (named *_minor or *_cents per convention), while legacy REAL columns convert at the service boundary via toMinorUnits(..., factor) and round deterministically on write.
  • Add exhaustive rounding and penny-safe split tests across 0-, 2-, and 3-decimal currencies wired into package.json.

Concrete Design Decisions to Lock Before Coding

  1. Rounding default: Confirm ROUND_HALF_UP vs ROUND_HALF_EVEN outside tax packs (tax packs already carry per-pack RoundingMethod).
  2. Legacy column migration: Confirm legacy orders/bills REAL columns stay as-is (boundary-converted) rather than adding dual shadow columns.

Out of Scope

  • Migrating existing products.price, orders.total, or bills.total SQLite column types in place.

Activity

  1. khaira777 commented on Aug 16, 2026

    @khaira777
    ContributorAuthor

    Verdict: VALID BUT NEEDS RESCOPING (architecture review warranted)

    Findings:

    • Money is stored as REAL floats (main/db.ts:4015 price REAL; order/bill totals :4166-4205 REAL) and manipulated with JS numbers in places — the float-drift concern is real.
    • The tax engine already uses decimal.js with configurable rounding including half_even (main/services/tax-engine.ts:10, :117-118; main/tax-packs/types.ts:3 RoundingMethod), and penny-safe split math with minor-unit rounding + remainder distribution already exists (main/routes/bills.ts:96-99, :175, :548-556).

    Suggested scope: don't replace every money path with a library in one change. Start with (a) minor-unit conversion at persistence/calculation boundaries (store cents as INTEGER or round at write via Decimal), (b) centralize rounding/split helpers by reusing the existing bills.ts math, (c) a reconciliation test. Maintainer decision needed on how far to take it (full library vs boundary-only). Related: #282 (tender service), #280 (REAL stock_quantity), #278.

  2. added
    enhancement
    area:backend
    on Aug 16, 2026
  3. added
    type:design
    state:needs-design
    on Sep 21, 2026
  4. itsbkm commented on Sep 21, 2026

    @itsbkm
    Contributor

    Checked current state: no main/core/money.ts exists yet, so this issue's scope is unchanged from when it was filed - still relevant as-is. Per the stabilization plan, narrowing to specifically the persistence/calculation boundary question (not a full monetary-type migration) is the recommended framing. Labeling type:design / state:needs-design.

  5. added
    priority:normal
    on Nov 28, 2026
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:normalstate:needs-designtype:design

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions