Skip to content

fix: preserve currency precision and saved tax-pack version on customer change - #1324

Merged
khaira777 merged 3 commits into
mainfrom
fm/ship-currency-and-tax-audit-fixes
Oct 23, 2026
Merged

khaira777 merged 3 commits into
mainfrom
fm/ship-currency-and-tax-audit-fixes

Conversation

@khaira777

Copy link
Copy Markdown
Contributor

Intent

Verify and fix the currency-precision and saved-tax-rule regressions from the merged-PR release audit, plus check 3-decimal currencies (such as KWD, BHD, OMR, JOD, TND) and 0-decimal / Japan (JP / JPY / community-japan.json) calculations so everything is accurate:

  1. Preserve currency precision in frontend/src/lib/cart-price.ts: Cart price overrides (clampCartPriceOverride) and cart subtotal helpers (frontend/src/store/cart.ts, frontend/src/hooks/use-tax-preview.ts, frontend/src/lib/cart-order-item.ts) currently round to 2 decimal places (Math.round(n * 100) / 100), which truncates 3-decimal currencies like KWD (1.234 becomes 1.23, and 0.004 rounds to 0 and becomes a free line) and allows fractional overrides on 0-decimal currencies like JPY.
  2. Preserve saved tax rules in main/routes/orders.ts: Attaching or changing a customer via PATCH /api/orders/:id/customer after a country tax-pack version update (1.0.0 to 1.0.1) currently recalculates order_items against the newly active pack version instead of the order item's saved packVersion, changing an unpaid bill's tax from 50 to 100 and overwriting tax_snapshot.packVersion from "1.0.0" to "1.0.1".
  3. Ensure 3-decimal currencies (KWD, BHD, OMR, JOD, TND) and 0-decimal / Japan (JP / JPY / community-japan.json) tax and payable-rounding calculations honor the currency's fraction digits (resolvePayableIncrement in main/services/tax-engine.ts when pack.currency === 'XXX' and getCurrencyFractionDigits(currency) !== configuredDecimals).

What Changed

  • Updated frontend/src/lib/cart-price.ts (resolveActiveCurrencyDecimals, roundMoney, clampCartPriceOverride, normalizeOverridePrice, and cartLineUnitPrice) to round cart prices and overrides using the active currency's fraction digits (supporting 0-decimal and 3-decimal currencies instead of fixed 2-decimal rounding).
  • Updated PATCH /api/orders/:id/customer in main/routes/orders.ts and main/services/tax.ts to skip item tax re-evaluation when customer B2B/state tax context is unchanged, resolve each item's saved packId and packVersion via getCountryPackByVersion, and accept snake_case tax_registration_number on customer records.
  • Updated resolvePayableIncrement in main/services/tax-engine.ts to use the configured currency's minor-unit increment whenever a currency: 'XXX' pack's increment decimals differ from the currency's fraction digits, fell back to taxRounding.decimalPlaces in allocateDocumentTaxRounding, and updated architecture docs and regression tests.

Risk Assessment

✅ Low: The changes are well-bounded and directly address currency-precision rounding in cart price overrides/subtotals, historical tax-pack version preservation on customer updates, and 0-/3-decimal payable increment resolution with end-to-end and unit test coverage.

Testing

Ran the targeted automated suites (test:cart-price-override, test:order-totals-recompute, test:tax-engine, test:currency, test:item-price-override, test:integration-tax, and test:community-tax-packs), built the backend and static frontend, and stood up an isolated Express + SQLite POS instance driven by headless Chromium and live HTTP requests. Verified and captured screenshots and API/database transcripts for KWD 3-decimal POS cart price overrides (1.234 KWD and the 0.004 KWD boundary), JPY 0-decimal fractional override rounding (1080.7 -> 1081 JPY) and Japan community tax pack calculations (1,100 JPY dine-in + 1,080 JPY takeaway = 2,180 JPY with 180 JPY tax), saved packVersion 1.0.0 preservation on PATCH /api/orders/:id/customer after a v1.0.1 pack upgrade for both B2C and inter-state B2B customers, and 3-decimal (KWD, BHD, OMR, JOD, TND) and 0-decimal (JPY, KRW, VND, COP) tax preview and billing calculations; all scenarios passed.

  • Live validation: ✅ go - 4 of 4 scenarios driven live against the product
Scenario Result Live Evidence
In a KWD (3-decimal) store, overriding a cart item price in the POS browser UI to 1.234 KWD and to the 0.004 KWD (four fils) boundary preserves all 3 decimal places in the cart UI, submitted order, an… ✅ pass live ~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/scenario1-kwd-pos-cart-1234.png, ~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/scenario1-kwd-pos-cart-00…
In a JP / JPY (0-decimal) store with community-japan.json active, entering a fractional cart price override (1080.7 JPY) in the POS browser UI rounds to whole yen (1,081 JPY), and placing a dual-rate… ✅ pass live ~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/scenario2-jpy-pos-cart-fractional-override.png, ~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/scenario2-…
Attaching an ordinary B2C customer or switching to an inter-state B2B customer via PATCH /api/orders/:id/customer after upgrading the active country tax pack from v1.0.0 (5% tax) to v1.0.1 (10% tax) p… ✅ pass live ~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/live-validation-results.json (scenario3_customer_patch_pack_version_preservation) and npm run test:order-totals-recompute
All 3-decimal currencies (KWD, BHD, OMR, JOD, TND) and 0-decimal currencies (JPY, KRW, VND, COP) honor their currency fraction digits for tax calculation and payable rounding on the generic XXX tax pa… ✅ pass live ~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/live-validation-results.json (scenario4_multi_currency_live_api) and npm run test:tax-engine

KWD 3-decimal POS price override modal (1.234 KWD)
KWD 3-decimal POS cart with 1.234 KWD override applied
KWD 3-decimal POS cart with 0.004 KWD (four fils) boundary override applied
JPY 0-decimal POS cart rounding fractional 1080.7 override to 1,081 JPY
Japan community pack POS cart (1,100 JPY dine-in + 1,080 JPY takeaway = 2,180 JPY)

Evidence: Live API and database verification transcript across all 4 scenarios
{
  "baseUrl": "http://127.0.0.1:51515",
  "scenarios": {
    "scenario1_kwd_browser_override": {
      "cartFooterText1234": "0/200\n\nItems\n1\nSubtotal\n‏١٫٢٣٤ د.ك.‏\nPlace Order\nSpace",
      "order1234": {
        "id": 1,
        "subtotal": 1.234,
        "tax_amount": 0.062,
        "total": 1.296
      },
      "bill1234": {
        "id": 1,
        "subtotal": 1.234,
        "tax_amount": 0.062,
        "total": 1.296
      },
      "cartFooterText0004": "0/200\n\nItems\n1\nSubtotal\n‏٠٫٠٠٤ د.ك.‏\nPlace Order\nSpace",
      "order0004": {
        "id": 2,
        "subtotal": 0.004,
        "tax_amount": 0,
        "total": 0.004
      },
      "bill0004": {
        "id": 2,
        "subtotal": 0.004,
        "tax_amount": 0,
        "total": 0.004
      },
      "screenshots": [
        "~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/scenario1-kwd-override-modal-1234.png",
        "~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/scenario1-kwd-pos-cart-1234.png",
        "~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/scenario1-kwd-pos-cart-0004.png"
      ]
    },
    "scenario2_jpy_browser_and_japan_pack": {
      "fractionalOverrideInput": 1080.7,
      "cartFooterTextAfterOverride": "0/200\n\nItems\n1\nSubtotal\n¥1,081\nPlace Order\nSpace",
      "overriddenOrder": {
        "id": 3,
        "subtotal": 1081,
        "tax_amount": 98,
        "total": 1081
      },
      "japanDualRateOrder": {
        "id": 4,
        "subtotal": 2180,
        "tax_amount": 180,
        "total": 2180
      },
      "japanDualRateBill": {
        "id": 3,
        "subtotal": 2180,
        "tax_amount": 180,
        "total": 2180,
        "round_off": 0
      },
      "screenshots": [
        "~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/scenario2-jpy-pos-cart-fractional-override.png",
        "~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/scenario2-jpy-pos-cart-japan-pack.png"
      ]
    },
    "scenario3_customer_patch_pack_version_preservation": {
      "orderId": 5,
      "billId": 4,
      "afterB2cAttach": {
        "orderTax": 50,
        "orderTotal": 1050,
        "billTax": 50,
        "billTotal": 1050,
        "packVersion": "1.0.0"
      },
      "afterB2bSwitch": {
        "orderTax": 50,
        "orderTotal": 1050,
        "billTax": 50,
        "billTotal": 1050,
        "packVersion": "1.0.0",
        "taxBreakdown": [
          [
            {
              "title": "Tax C",
              "rate": 5,
              "amount": 50
            }
          ]
        ]
      }
    },
    "scenario4_multi_currency_live_api": {
      "threeDecimalCurrencies": {
        "KWD": {
          "country": "KW",
          "preview": {
            "subtotal": 1.234,
            "discount_amount": 0,
            "discounted_subtotal": 1.234,
            "tax_amount": 0.062,
            "tax_breakdown": [
              {
                "title": "VAT",
                "rate": 5,
                "amount": 0.062
              }
            ],
            "packaging_charge": 0,
            "delivery_charge": 0,
            "service_charge": 0,
            "round_off": 0,
            "total": 1.296
          },
          "bill": {
            "subtotal": 1.234,
            "tax_amount": 0.062,
            "total": 1.296,
            "round_off": 0
          }
        },
        "BHD": {
          "country": "BH",
          "preview": {
            "subtotal": 1.234,
            "discount_amount": 0,
            "discounted_subtotal": 1.234,
            "tax_amount": 0.062,
            "tax_breakdown": [
              {
                "title": "VAT",
                "rate": 5,
                "amount": 0.062
              }
            ],
            "packaging_charge": 0,
            "delivery_charge": 0,
            "service_charge": 0,
            "round_off": 0,
            "total": 1.296
          },
          "bill": {
            "subtotal": 1.234,
            "tax_amount": 0.062,
            "total": 1.296,
            "round_off": 0
          }
        },
        "OMR": {
          "country": "OM",
          "preview": {
            "subtotal": 1.234,
            "discount_amount": 0,
            "discounted_subtotal": 1.234,
            "tax_amount": 0.062,
            "tax_breakdown": [
              {
                "title": "VAT",
                "rate": 5,
                "amount": 0.062
              }
            ],
            "packaging_charge": 0,
            "delivery_charge": 0,
            "service_charge": 0,
            "round_off": 0,
            "total": 1.296
          },
          "bill": {
            "subtotal": 1.234,
            "tax_amount": 0.062,
            "total": 1.296,
            "round_off": 0
          }
        },
        "JOD": {
          "country": "JO",
          "preview": {
            "subtotal": 1.234,
            "discount_amount": 0,
            "discounted_subtotal": 1.234,
            "tax_amount": 0.062,
            "tax_breakdown": [
              {
                "title": "VAT",
                "rate": 5,
                "amount": 0.062
              }
            ],
            "packaging_charge": 0,
            "delivery_charge": 0,
            "service_charge": 0,
            "round_off": 0,
            "total": 1.296
          },
          "bill": {
            "subtotal": 1.234,
            "tax_amount": 0.062,
            "total": 1.296,
            "round_off": 0
          }
        },
        "TND": {
          "country": "TN",
          "preview": {
            "subtotal": 1.234,
            "discount_amount": 0,
            "discounted_subtotal": 1.234,
            "tax_amount": 0.062,
            "tax_breakdown": [
              {
                "title": "VAT",
                "rate": 5,
                "amount": 0.062
              }
            ],
            "packaging_charge": 0,
            "delivery_charge": 0,
            "service_charge": 0,
            "round_off": 0,
            "total": 1.296
          },
          "bill": {
            "subtotal": 1.234,
            "tax_amount": 0.062,
            "total": 1.296,
            "round_off": 0
          }
        }
      },
      "zeroDecimalCurrencies": {
        "JPY": {
          "country": "JP",
          "preview": {
            "subtotal": 105,
            "discount_amount": 0,
            "discounted_subtotal": 105,
            "tax_amount": 5,
            "tax_breakdown": [
              {
                "title": "VAT",
                "rate": 5,
                "amount": 5
              }
            ],
            "packaging_charge": 0,
            "delivery_charge": 0,
            "service_charge": 0,
            "round_off": 0,
            "total": 110
          },
          "bill": {
            "subtotal": 105,
            "tax_amount": 5,
            "total": 110,
            "round_off": 0
          }
        },
        "KRW": {
          "country": "KR",
          "preview": {
            "subtotal": 105,
            "discount_amount": 0,
            "discounted_subtotal": 105,
            "tax_amount": 5,
            "tax_breakdown": [
              {
                "title": "VAT",
                "rate": 5,
                "amount": 5
              }
            ],
            "packaging_charge": 0,
            "delivery_charge": 0,
            "service_charge": 0,
            "round_off": 0,
            "total": 110
          },
          "bill": {
            "subtotal": 105,
            "tax_amount": 5,
            "total": 110,
            "round_off": 0
          }
        },
        "VND": {
          "country": "VN",
          "preview": {
            "subtotal": 105,
            "discount_amount": 0,
            "discounted_subtotal": 105,
            "tax_amount": 5,
            "tax_breakdown": [
              {
                "title": "VAT",
                "rate": 5,
                "amount": 5
              }
            ],
            "packaging_charge": 0,
            "delivery_charge": 0,
            "service_charge": 0,
            "round_off": 0,
            "total": 110
          },
          "bill": {
            "subtotal": 105,
            "tax_amount": 5,
            "total": 110,
            "round_off": 0
          }
        },
        "COP": {
          "country": "CO",
          "preview": {
            "subtotal": 105,
            "discount_amount": 0,
            "discounted_subtotal": 105,
            "tax_amount": 5,
            "tax_breakdown": [
              {
                "title": "VAT",
                "rate": 5,
                "amount": 5
              }
            ],
            "packaging_charge": 0,
            "delivery_charge": 0,
            "service_charge": 0,
            "round_off": 0,
            "total": 110
          },
          "bill": {
            "subtotal": 105,
            "tax_amount": 5,
            "total": 110,
            "round_off": 0
          }
        }
      }
    }
  }
}

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 4 of 4 scenarios driven live against the product
Scenario Result Live Evidence
In a KWD (3-decimal) store, overriding a cart item price in the POS browser UI to 1.234 KWD and to the 0.004 KWD (four fils) boundary preserves all 3 decimal places in the cart UI, submitted order, an… ✅ pass live ~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/scenario1-kwd-pos-cart-1234.png, ~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/scenario1-kwd-pos-cart-00…
In a JP / JPY (0-decimal) store with community-japan.json active, entering a fractional cart price override (1080.7 JPY) in the POS browser UI rounds to whole yen (1,081 JPY), and placing a dual-rate… ✅ pass live ~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/scenario2-jpy-pos-cart-fractional-override.png, ~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/scenario2-…
Attaching an ordinary B2C customer or switching to an inter-state B2B customer via PATCH /api/orders/:id/customer after upgrading the active country tax pack from v1.0.0 (5% tax) to v1.0.1 (10% tax) p… ✅ pass live ~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/live-validation-results.json (scenario3_customer_patch_pack_version_preservation) and npm run test:order-totals-recompute
All 3-decimal currencies (KWD, BHD, OMR, JOD, TND) and 0-decimal currencies (JPY, KRW, VND, COP) honor their currency fraction digits for tax calculation and payable rounding on the generic XXX tax pa… ✅ pass live ~/.no-mistakes/evidence/01M5KJHBSX8T46VV3P1MBRDKHW/live-validation-results.json (scenario4_multi_currency_live_api) and npm run test:tax-engine
  • npm run test:cart-price-override
  • npm run test:order-totals-recompute
  • npm run test:tax-engine
  • npm run test:currency
  • npm run test:item-price-override
  • npm run test:integration-tax
  • npm run test:community-tax-packs
  • npm run build && npm run build:frontend
  • Live Playwright Chromium + Express/SQLite validation across KWD (1.234 and 0.004 overrides), JPY (1080.7 fractional override and community-japan.json 10%/8% inclusive tax), PATCH /api/orders/:id/customer after tax pack v1.0.0 -> v1.0.1 upgrade, and KWD/BHD/OMR/JOD/TND/JPY/KRW/VND/COP tax preview, order, and bill endpoints
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@khaira777
khaira777 merged commit efeb10f into main Oct 23, 2026
23 checks passed
@khaira777
khaira777 deleted the fm/ship-currency-and-tax-audit-fixes branch October 23, 2026 06:04
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