Skip to content

fix(main): apply document tax rounding across transaction documents - #1282

Merged
khaira777 merged 13 commits into
mainfrom
fm/ship-r3-document-tax-rounding
Oct 22, 2026
Merged

khaira777 merged 13 commits into
mainfrom
fm/ship-r3-document-tax-rounding

Conversation

@khaira777

@khaira777 khaira777 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1274

Intent

Fix the document tax rounding defect from the FloCafe 3.13.1 release-readiness audit at 3657545 (Issue #1274):

  • [P2] Apply document tax rounding across the real document (main/services/tax.ts, main/routes/orders.ts, main/services/orders.ts, Issue fix(tax): apply document-scope tax rounding across the full transaction document #1274): Document tax rounding produces incorrect order, bill, and report totals. TaxEngine.calculate supports scope: "document" with largest_remainder allocation, and the signed official-india 1.0.5 pack configures scope: "document", method: "half_up", decimalPlaces: 2, remainderAllocation: "largest_remainder", and 2.5% CGST + 2.5% SGST on standard. However, calculateItemTax (main/services/tax.ts:175-278) calls TaxEngine.calculate once per item with a single synthetic line (lineId: "legacy-item-adapter"), and POST /api/orders (main/routes/orders.ts:1743) sums those per-item results. For two exclusive standard items at 100.10 each, TaxEngine.calculate on the 2-line document produces taxAmount = 10.01, payableTotal = 210.21, CGST = 5.00, SGST = 5.01, whereas FloCafe rounds each item separately and stores/reports tax_amount = 10.02, total = 210.22, CGST = 5.00, SGST = 5.02 across POST /api/tax/preview, POST /api/orders, POST /api/bills/generate, /api/reports/tax-components, and receipt tax-component helpers. Evaluate document-scoped tax packs across the complete transaction document (items and charges) with stable line identities, persist the engine's document-allocated line snapshots/totals, and keep preview, order creation, order/item mutations, bill generation, receipt helpers, and tax-component reports consistent.

What Changed

  • Replaced isolated per-item tax calculation with document-scoped tax calculation via calculateDocumentTaxes(), evaluating all order items and configured charges together in a single tax engine pass with stable line identities for tax previews and order creation.
  • Updated recomputeOrderTotals() to reallocate document rounding remainders across existing item snapshots and configured charge snapshots during order mutations without re-evaluating saved items against the active catalog, persisting updated line snapshots, tax amounts, and totals to order_items.
  • Updated calculateOrderTotals() to filter non-terminal order items and updated architectural documentation for document tax rounding across items, charges, orders, bills, and reports.

Risk Assessment

✅ Low: Unified document-scoped tax rounding is correctly implemented across items and charges with persistent snapshots across all transaction lifecycle phases without defects or extraneous abstractions.

Testing

Successfully verified document tax rounding across the live running application: 6 derived end-to-end scenarios passed against real SQLite and Express services with Playwright browser UI rendering, capturing UI screenshots and structured JSON evidence in ~/.no-mistakes/evidence/01M5GFXD0KH5WDGXY5NMQ16AFT, with 273/273 integration tax tests, 16/16 tax engine unit tests, and 5/5 Playwright settings tax tests passing.

  • Live validation: ✅ go - 6 of 6 scenarios driven live against the product
Scenario Result Live Evidence
Document tax preview evaluates items and charges in a single engine pass under document-scope rounding ✅ pass live ~/.no-mistakes/evidence/01M5GFXD0KH5WDGXY5NMQ16AFT/scenario-1-tax-preview.json
Order creation and bill generation persist document-allocated line snapshots and consistent tax totals ✅ pass live ~/.no-mistakes/evidence/01M5GFXD0KH5WDGXY5NMQ16AFT/scenario-2-order-bill.json
Active order mutations reconcile document tax rounding without catalog re-evaluation drift ✅ pass live ~/.no-mistakes/evidence/01M5GFXD0KH5WDGXY5NMQ16AFT/scenario-3-mutations.json
Untaxed lines and zero-price items do not absorb document rounding remainders ✅ pass live ~/.no-mistakes/evidence/01M5GFXD0KH5WDGXY5NMQ16AFT/scenario-4-boundary-untaxed.json
Bill settlement and tax-components reporting reflect document-allocated component totals ✅ pass live ~/.no-mistakes/evidence/01M5GFXD0KH5WDGXY5NMQ16AFT/scenario-5-settlement-reporting.json
Settings UI renders official tax pack configuration and interactive test calculation ✅ pass live ~/.no-mistakes/evidence/01M5GFXD0KH5WDGXY5NMQ16AFT/tax-settings-document-rounding.png

Tax Configuration and Test Calculation UI Screenshot
Orders and Billing UI Screenshot

Evidence: Scenario 1: Document Tax Preview JSON Evidence
{
  "preview2Items": {
    "items": [
      {
        "product_id": "a7tc0z",
        "product_name": "Rounding Item 1",
        "quantity": 1,
        "unit_price": 100.1,
        "subtotal": 100.1,
        "discount_amount": 0,
        "tax_amount": 5.01,
        "tax_breakdown": [
          {
            "title": "CGST",
            "rate": 2.5,
            "amount": 2.5
          },
          {
            "title": "SGST",
            "rate": 2.5,
            "amount": 2.51
          }
        ],
        "tax_type": "exclusive",
        "total": 105.11
      },
      {
        "product_id": "0qi1z3",
        "product_name": "Rounding Item 2",
        "quantity": 1,
        "unit_price": 100.1,
        "subtotal": 100.1,
        "discount_amount": 0,
        "tax_amount": 5,
        "tax_breakdown": [
          {
            "title": "CGST",
            "rate": 2.5,
            "amount": 2.5
          },
          {
            "title": "SGST",
            "rate": 2.5,
            "amount": 2.5
          }
        ],
        "tax_type": "exclusive",
        "total": 105.1
      }
    ],
    "summary": {
      "subtotal": 200.2,
      "discount_amount": 0,
      "discounted_subtotal": 200.2,
      "tax_amount": 10.01,
      "tax_breakdown": [
        {
          "title": "CGST",
          "rate": 2.5,
          "amount": 5
        },
        {
          "title": "SGST",
          "rate": 2.5,
          "amount": 5.01
        }
      ],
      "packaging_charge": 0,
      "delivery_charge": 0,
      "service_charge": 0,
      "round_off": 0,
      "total": 210.21
    }
  },
  "previewItemAndCharge": {
    "items": [
      {
        "product_id": "a7tc0z",
        "product_name": "Rounding Item 1",
        "quantity": 1,
        "unit_price": 100.1,
        "subtotal": 100.1,
        "discount_amount": 0,
        "tax_amount": 5,
        "tax_breakdown": [
          {
            "title": "CGST",
            "rate": 2.5,
            "amount": 2.5
          },
          {
            "title": "SGST",
            "rate": 2.5,
            "amount": 2.5
          }
        ],
        "tax_type": "exclusive",
        "total": 105.1
      }
    ],
    "summary": {
      "subtotal": 100.1,
      "discount_amount": 0,
      "discounted_subtotal": 100.1,
      "tax_amount": 10.01,
      "tax_breakdown": [
        {
          "title": "CGST",
          "rate": 2.5,
          "amount": 5
        },
        {
          "title": "SGST",
          "rate": 2.5,
          "amount": 5.01
        }
      ],
      "packaging_charge": 100.1,
      "delivery_charge": 0,
      "service_charge": 0,
      "round_off": 0,
      "total": 210.21
    }
  }
}
Evidence: Scenario 2: Order Creation and Bill Snapshots JSON Evidence
{
  "order": {
    "id": 1,
    "order_number": "ORD-20261022-0001",
    "table_id": null,
    "customer_id": null,
    "user_id": "e2e-owner",
    "type": "takeaway",
    "delivery_address": null,
    "expected_payment_method": null,
    "delivery_note": null,
    "guest_count": null,
    "special_instructions": null,
    "packaging_charge": 0,
    "delivery_charge": 0,
    "service_charge": 0,
    "charges_breakdown": null,
    "status": "pending",
    "subtotal": 200.2,
    "tax_amount": 10.01,
    "tax_breakdown": [
      {
        "title": "CGST",
        "rate": 2.5,
        "amount": 5
      },
      {
        "title": "SGST",
        "rate": 2.5,
        "amount": 5.01
      }
    ],
    "tax_snapshot": [
      {
        "packId": "official-india",
        "packVersion": "1.0.0",
        "effectiveFrom": "2026-01-01",
        "taxRounding": {
          "scope": "document",
          "method": "half_up",
          "decimalPlaces": 2,
          "remainderAllocation": "largest_remainder"
        },
        "payableRounding": {
          "increment": "0.01",
          "method": "half_up"
        },
        "appliedRuleIds": [
          "a-sgst",
          "b-cgst"
        ],
        "lines": [
          {
            "lineId": "line-0001:a7tc0z",
            "categoryId": "standard",
            "categorySource": "explicit",
            "taxBehavior": "exclusive",
            "grossAmount": "100.10",
            "taxableBase": "100.10",
            "taxAmount": "5.01",
            "components": [
              {
                "ruleId": "b-cgst",
                "label": "CGST",
                "type": "percent",
                "rate": "2.5",
                "baseRuleIds": [],
                "amount": "2.50",
                "roundingRemainder": "0"
              },
              {
                "ruleId": "a-sgst",
                "label": "SGST",
                "type": "percent",
                "rate": "2.5",
                "baseRuleIds": [],
                "amount": "2.51",
                "roundingRemainder": "0"
              }
            ]
          }
        ],
        "country": "IN",
        "currency": "THB",
        "merchantOverridesApplied": []
      },
      {
        "packId": "official-india",
        "packVersion": "1.0.0",
        "effectiveFrom": "2026-01-01",
        "taxRounding": {
          "scope": "document",
          "method": "half_up",
          "decimalPlaces": 2,
          "remainderAllocation": "largest_remainder"
        },
        "payableRounding": {
          "increment": "0.01",
          "method": "half_up"
        },
        "appliedRuleIds": [
          "a-sgst",
          "b-cgst"
        ],
        "lines": [
          {
            "lineId": "line-0002:0qi1z3",
            "categoryId": "standard",
            "categorySource": "explicit",
            "taxBehavior": "exclusive",
            "grossAmount": "100.10",
            "taxableBase": "100.10",
            "taxAmount": "5.00",
            "components": [
              {
                "ruleId": "b-cgst",
                "label": "CGST",
                "type": "percent",
                "rate": "2.5",
                "baseRuleIds": [],
                "amount": "2.50",
                "roundingRemainder": "0"
              },
              {
                "ruleId": "a-sgst",
                "label": "SGST",
                "type": "percent",
                "rate": "2.5",
                "baseRuleIds": [],
                "amount": "2.50",
                "roundingRemainder": "0"
              }
            ]
          }
        ],
        "country": "IN",
        "currency": "THB",
        "merchantOverridesApplied": []
      }
    ],
    "packaging_tax_category_id": "standard",
    "delivery_tax_category_id": null,
    "service_charge_tax_category_id": null,
    "discount_amount": 0,
    "discount_type": null,
    "discount_value": null,
    "discount_reason": null,
    "round_off": 0,
    "total": 210.21,
    "cooking_started_at": null,
    "ready_at": null,
    "served_at": null,
    "completed_at": null,
    "cancelled_at": null,
    "cancellation_reason": null,
    "created_at": "2026-10-22 00:32:15",
    "updated_at": "2026-10-22 00:32:15",
    "online_platform": null,
    "external_order_id": null,
    "expected_payment_method_id": null,
    "items": [
      {
        "id": 1,
        "order_id": 1,
        "product_id": "a7tc0z",
        "product_name": "Rounding Item 1",
        "product_sku": null,
        "unit_price": 100.1,
        "quantity": 1,
        "inventory_deducted_quantity": 0,
        "inventory_product_id": null,
        "subtotal": 100.1,
        "tax_amount": 5.01,
        "tax_breakdown": [
          {
            "title": "CGST",
            "rate": 2.5,
            "amount": 2.5
          },
          {
            "title": "SGST",
            "rate": 2.5,
            "amount": 2.51
          }
        ],
        "tax_snapshot": {
          "packId": "official-india",
          "packVersion": "1.0.0",
          "effectiveFrom": "2026-01-01",
          "taxRounding": {
            "scope": "document",
            "method": "half_up",
            "decimalPlaces": 2,
            "remainderAllocation": "largest_remainder"
          },
          "payableRounding": {
            "increment": "0.01",
            "method": "half_up"
          },
          "appliedRuleIds": [
            "a-sgst",
            "b-cgst"
          ],
          "lines": [
            {
              "lineId": "line-0001:a7tc0z",
              "categoryId": "standard",
              "categorySource": "explicit",
              "taxBehavior": "exclusive",
              "grossAmount": "100.10",
              "taxableBase": "100.10",
              "taxAmount": "5.01",
              "components": [
                {
                  "ruleId": "b-cgst",
                  "label": "CGST",
                  "type": "percent",
                  "rate": "2.5",
                  "baseRuleIds": [],
                  "amount": "2.50",
                  "roundingRemainder": "0.0025"
                },
                {
                  "ruleId": "a-sgst",
                  "label": "SGST",
                  "type": "percent",
                  "rate": "2.5",
                  "baseRuleIds": [],
                  "amount": "2.51",
                  "roundingRemainder": "-0.0075"
                }
              ]
            }
          ],
          "country": "IN",
          "currency": "THB",
          "merchantOverridesApplied": []
        },
        "tax_type": "exclusive",
        "discount_amount": 0,
        "total": 105.11,
        "variant_selection": null,
        "modifier_selection": null,
        "special_instructions": null,
        "status": "pending",
        "created_at": "2026-10-22 00:32:15",
        "updated_at": "2026-10-22 00:32:15",
        "voided_at": null,
        "recipe_snapshot": null,
        "variant_id": null,
        "inventory_variant_id": null,
        "addons": [],
        "edit_version": 0
      },
      {
        "id": 2,
        "order_id": 1,
        "product_id": "0qi1z3",
        "product_name": "Rounding Item 2",
        "product_sku": null,
        "unit_price": 100.1,
        "quantity": 1,
        "inventory_deducted_quantity": 0,
        "inventory_product_id": null,
        "subtotal": 100.1,
        "tax_amount": 5,
        "tax_breakdown": [
          {
            "title": "CGST",
            "rate": 2.5,
            "amount": 2.5
          },
          {
            "title": "SGST",
            "rate": 2.5,
            "amount": 2.5
          }
        ],
        "tax_snapshot": {
          "packId": "official-india",
          "packVersion": "1.0.0",
          "effectiveFrom": "2026-01-01",
          "taxRounding": {
            "scope": "document",
            "method": "half_up",
            "decimalPlaces": 2,
            "remainderAllocation": "largest_remainder"
          },
          "payableRounding": {
            "increment": "0.01",
            "method": "half_up"
          },
          "appliedRuleIds": [
            "a-sgst",
            "b-cgst"
          ],
          "l

... [7987 bytes truncated] ...

      "effectiveFrom": "2026-01-01",
          "taxRounding": {
            "scope": "document",
            "method": "half_up",
            "decimalPlaces": 2,
            "remainderAllocation": "largest_remainder"
          },
          "payableRounding": {
            "increment": "0.01",
            "method": "half_up"
          },
          "appliedRuleIds": [
            "a-sgst",
            "b-cgst"
          ],
          "lines": [
            {
              "lineId": "line-0002:0qi1z3",
              "categoryId": "standard",
              "categorySource": "explicit",
              "taxBehavior": "exclusive",
              "grossAmount": "100.10",
              "taxableBase": "100.10",
              "taxAmount": "5.00",
              "components": [
                {
                  "ruleId": "b-cgst",
                  "label": "CGST",
                  "type": "percent",
                  "rate": "2.5",
                  "baseRuleIds": [],
                  "amount": "2.50",
                  "roundingRemainder": "0"
                },
                {
                  "ruleId": "a-sgst",
                  "label": "SGST",
                  "type": "percent",
                  "rate": "2.5",
                  "baseRuleIds": [],
                  "amount": "2.50",
                  "roundingRemainder": "0"
                }
              ]
            }
          ],
          "country": "IN",
          "currency": "THB",
          "merchantOverridesApplied": []
        }
      ],
      "packaging_tax_category_id": "standard",
      "delivery_tax_category_id": null,
      "service_charge_tax_category_id": null,
      "discount_amount": 0,
      "discount_type": null,
      "discount_value": null,
      "discount_reason": null,
      "round_off": 0,
      "total": 210.21,
      "cooking_started_at": null,
      "ready_at": null,
      "served_at": null,
      "completed_at": null,
      "cancelled_at": null,
      "cancellation_reason": null,
      "created_at": "2026-10-22 00:32:15",
      "updated_at": "2026-10-22 00:32:15",
      "online_platform": null,
      "external_order_id": null,
      "expected_payment_method_id": null,
      "items": [
        {
          "id": 1,
          "order_id": 1,
          "product_id": "a7tc0z",
          "product_name": "Rounding Item 1",
          "product_sku": null,
          "unit_price": 100.1,
          "quantity": 1,
          "inventory_deducted_quantity": 0,
          "inventory_product_id": null,
          "subtotal": 100.1,
          "tax_amount": 5.01,
          "tax_breakdown": [
            {
              "title": "CGST",
              "rate": 2.5,
              "amount": 2.5
            },
            {
              "title": "SGST",
              "rate": 2.5,
              "amount": 2.51
            }
          ],
          "tax_snapshot": {
            "packId": "official-india",
            "packVersion": "1.0.0",
            "effectiveFrom": "2026-01-01",
            "taxRounding": {
              "scope": "document",
              "method": "half_up",
              "decimalPlaces": 2,
              "remainderAllocation": "largest_remainder"
            },
            "payableRounding": {
              "increment": "0.01",
              "method": "half_up"
            },
            "appliedRuleIds": [
              "a-sgst",
              "b-cgst"
            ],
            "lines": [
              {
                "lineId": "line-0001:a7tc0z",
                "categoryId": "standard",
                "categorySource": "explicit",
                "taxBehavior": "exclusive",
                "grossAmount": "100.10",
                "taxableBase": "100.10",
                "taxAmount": "5.01",
                "components": [
                  {
                    "ruleId": "b-cgst",
                    "label": "CGST",
                    "type": "percent",
                    "rate": "2.5",
                    "baseRuleIds": [],
                    "amount": "2.50",
                    "roundingRemainder": "0.0025"
                  },
                  {
                    "ruleId": "a-sgst",
                    "label": "SGST",
                    "type": "percent",
                    "rate": "2.5",
                    "baseRuleIds": [],
                    "amount": "2.51",
                    "roundingRemainder": "-0.0075"
                  }
                ]
              }
            ],
            "country": "IN",
            "currency": "THB",
            "merchantOverridesApplied": []
          },
          "tax_type": "exclusive",
          "discount_amount": 0,
          "total": 105.11,
          "variant_selection": null,
          "modifier_selection": null,
          "special_instructions": null,
          "status": "pending",
          "created_at": "2026-10-22 00:32:15",
          "updated_at": "2026-10-22 00:32:15",
          "voided_at": null,
          "recipe_snapshot": null,
          "variant_id": null,
          "inventory_variant_id": null,
          "addons": [],
          "edit_version": 0
        },
        {
          "id": 2,
          "order_id": 1,
          "product_id": "0qi1z3",
          "product_name": "Rounding Item 2",
          "product_sku": null,
          "unit_price": 100.1,
          "quantity": 1,
          "inventory_deducted_quantity": 0,
          "inventory_product_id": null,
          "subtotal": 100.1,
          "tax_amount": 5,
          "tax_breakdown": [
            {
              "title": "CGST",
              "rate": 2.5,
              "amount": 2.5
            },
            {
              "title": "SGST",
              "rate": 2.5,
              "amount": 2.5
            }
          ],
          "tax_snapshot": {
            "packId": "official-india",
            "packVersion": "1.0.0",
            "effectiveFrom": "2026-01-01",
            "taxRounding": {
              "scope": "document",
              "method": "half_up",
              "decimalPlaces": 2,
              "remainderAllocation": "largest_remainder"
            },
            "payableRounding": {
              "increment": "0.01",
              "method": "half_up"
            },
            "appliedRuleIds": [
              "a-sgst",
              "b-cgst"
            ],
            "lines": [
              {
                "lineId": "line-0002:0qi1z3",
                "categoryId": "standard",
                "categorySource": "explicit",
                "taxBehavior": "exclusive",
                "grossAmount": "100.10",
                "taxableBase": "100.10",
                "taxAmount": "5.00",
                "components": [
                  {
                    "ruleId": "b-cgst",
                    "label": "CGST",
                    "type": "percent",
                    "rate": "2.5",
                    "baseRuleIds": [],
                    "amount": "2.50",
                    "roundingRemainder": "0.0025"
                  },
                  {
                    "ruleId": "a-sgst",
                    "label": "SGST",
                    "type": "percent",
                    "rate": "2.5",
                    "baseRuleIds": [],
                    "amount": "2.50",
                    "roundingRemainder": "0.0025"
                  }
                ]
              }
            ],
            "country": "IN",
            "currency": "THB",
            "merchantOverridesApplied": []
          },
          "tax_type": "exclusive",
          "discount_amount": 0,
          "total": 105.1,
          "variant_selection": null,
          "modifier_selection": null,
          "special_instructions": null,
          "status": "pending",
          "created_at": "2026-10-22 00:32:15",
          "updated_at": "2026-10-22 00:32:15",
          "voided_at": null,
          "recipe_snapshot": null,
          "variant_id": null,
          "inventory_variant_id": null,
          "addons": [],
          "edit_version": 0
        }
      ]
    }
  },
  "displayComponents": [
    {
      "title": "CGST",
      "rate": 2.5,
      "amount": 5
    },
    {
      "title": "SGST",
      "rate": 2.5,
      "amount": 5.01
    }
  ]
}
  • Evidence: Scenario 3: Order Mutations JSON Evidence (local file: ~/.no-mistakes/evidence/01M5GFXD0KH5WDGXY5NMQ16AFT/scenario-3-mutations.json)
  • Evidence: Scenario 4: Boundary Untaxed Lines and Zero-Price Items JSON Evidence (local file: ~/.no-mistakes/evidence/01M5GFXD0KH5WDGXY5NMQ16AFT/scenario-4-boundary-untaxed.json)
Evidence: Scenario 5: Settlement and Tax Components Reporting JSON Evidence
{
  "settledBill": {
    "id": 1,
    "bill_number": "INV-20261022-0001",
    "order_id": 1,
    "customer_id": null,
    "subtotal": 200.2,
    "tax_amount": 10.01,
    "tax_breakdown": [
      {
        "title": "CGST",
        "rate": 2.5,
        "amount": 5
      },
      {
        "title": "SGST",
        "rate": 2.5,
        "amount": 5.01
      }
    ],
    "tax_snapshot": [
      {
        "packId": "official-india",
        "packVersion": "1.0.0",
        "effectiveFrom": "2026-01-01",
        "taxRounding": {
          "scope": "document",
          "method": "half_up",
          "decimalPlaces": 2,
          "remainderAllocation": "largest_remainder"
        },
        "payableRounding": {
          "increment": "0.01",
          "method": "half_up"
        },
        "appliedRuleIds": [
          "a-sgst",
          "b-cgst"
        ],
        "lines": [
          {
            "lineId": "line-0001:a7tc0z",
            "categoryId": "standard",
            "categorySource": "explicit",
            "taxBehavior": "exclusive",
            "grossAmount": "100.10",
            "taxableBase": "100.10",
            "taxAmount": "5.01",
            "components": [
              {
                "ruleId": "b-cgst",
                "label": "CGST",
                "type": "percent",
                "rate": "2.5",
                "baseRuleIds": [],
                "amount": "2.50",
                "roundingRemainder": "0"
              },
              {
                "ruleId": "a-sgst",
                "label": "SGST",
                "type": "percent",
                "rate": "2.5",
                "baseRuleIds": [],
                "amount": "2.51",
                "roundingRemainder": "0"
              }
            ]
          }
        ],
        "country": "IN",
        "currency": "THB",
        "merchantOverridesApplied": []
      },
      {
        "packId": "official-india",
        "packVersion": "1.0.0",
        "effectiveFrom": "2026-01-01",
        "taxRounding": {
          "scope": "document",
          "method": "half_up",
          "decimalPlaces": 2,
          "remainderAllocation": "largest_remainder"
        },
        "payableRounding": {
          "increment": "0.01",
          "method": "half_up"
        },
        "appliedRuleIds": [
          "a-sgst",
          "b-cgst"
        ],
        "lines": [
          {
            "lineId": "line-0002:0qi1z3",
            "categoryId": "standard",
            "categorySource": "explicit",
            "taxBehavior": "exclusive",
            "grossAmount": "100.10",
            "taxableBase": "100.10",
            "taxAmount": "5.00",
            "components": [
              {
                "ruleId": "b-cgst",
                "label": "CGST",
                "type": "percent",
                "rate": "2.5",
                "baseRuleIds": [],
                "amount": "2.50",
                "roundingRemainder": "0"
              },
              {
                "ruleId": "a-sgst",
                "label": "SGST",
                "type": "percent",
                "rate": "2.5",
                "baseRuleIds": [],
                "amount": "2.50",
                "roundingRemainder": "0"
              }
            ]
          }
        ],
        "country": "IN",
        "currency": "THB",
        "merchantOverridesApplied": []
      }
    ],
    "discount_amount": 0,
    "discount_type": null,
    "discount_value": null,
    "discount_reason": null,
    "delivery_charge": 0,
    "packaging_charge": 0,
    "service_charge": 0,
    "charges_breakdown": null,
    "round_off": 0,
    "total": 210.21,
    "paid_amount": 210.21,
    "balance": 0,
    "payment_status": "paid",
    "payment_details": [
      {
        "method": "cash",
        "cash_session_id": 0,
        "amount": 210.21,
        "requested_amount": 210.21,
        "amount_omitted": false,
        "tendered_amount": 210.21,
        "change_amount": 0,
        "timestamp": "2026-10-22 00:32:18"
      }
    ],
    "paid_at": "2026-10-22 00:32:18",
    "printed_at": null,
    "created_at": "2026-10-22 00:32:15",
    "updated_at": "2026-10-22 00:32:18",
    "split_group_id": null,
    "split_label": null
  },
  "report": {
    "taxComponents": {
      "startDate": "2026-10-22",
      "endDate": "2026-10-22",
      "billCount": 1,
      "taxAmount": 10.01,
      "components": [
        {
          "title": "CGST",
          "rate": 2.5,
          "amount": 5
        },
        {
          "title": "SGST",
          "rate": 2.5,
          "amount": 5.01
        }
      ]
    }
  }
}

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 - 6 of 6 scenarios driven live against the product
Scenario Result Live Evidence
Document tax preview evaluates items and charges in a single engine pass under document-scope rounding ✅ pass live ~/.no-mistakes/evidence/01M5GFXD0KH5WDGXY5NMQ16AFT/scenario-1-tax-preview.json
Order creation and bill generation persist document-allocated line snapshots and consistent tax totals ✅ pass live ~/.no-mistakes/evidence/01M5GFXD0KH5WDGXY5NMQ16AFT/scenario-2-order-bill.json
Active order mutations reconcile document tax rounding without catalog re-evaluation drift ✅ pass live ~/.no-mistakes/evidence/01M5GFXD0KH5WDGXY5NMQ16AFT/scenario-3-mutations.json
Untaxed lines and zero-price items do not absorb document rounding remainders ✅ pass live ~/.no-mistakes/evidence/01M5GFXD0KH5WDGXY5NMQ16AFT/scenario-4-boundary-untaxed.json
Bill settlement and tax-components reporting reflect document-allocated component totals ✅ pass live ~/.no-mistakes/evidence/01M5GFXD0KH5WDGXY5NMQ16AFT/scenario-5-settlement-reporting.json
Settings UI renders official tax pack configuration and interactive test calculation ✅ pass live ~/.no-mistakes/evidence/01M5GFXD0KH5WDGXY5NMQ16AFT/tax-settings-document-rounding.png
  • node tests/e2e-server.cjs (live Main API on :3001, KDS on :3002, Server App on :3003)
  • Playwright Chromium headless browser UI interaction on /settings?tab=tax and /orders
  • cd frontend && npx playwright test e2e/qa-settings-tax.spec.ts
  • npm run test:tax-engine
  • npm run test:integration-tax
  • End-to-end live driver across tax preview, order creation, order mutations, boundary untaxed lines, bill generation, settlement, and tax-components reporting
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

@coderabbitai

coderabbitai Bot commented Oct 21, 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
  • Configuration used: Repository: FreeOpenSourcePOS/FloCafe/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 41f6a433-e9dc-472e-bb0d-b8a8935dffdc

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

Copy link
Copy Markdown
Contributor Author

@greptileai review this at apex

@greptile-apps

greptile-apps Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5 Tier: apex

[Medium impact] The changes since the last review appear safe to merge; no new issue needs a separate comment.

Summary

This PR calculates document-scoped tax across items and configured charges, then saves the allocated amounts for orders, bills, receipts, and reports.

  • Tax rounding now spans items and configured charges across a transaction.

git diff --check passed. This review used source inspection; the app and test suites were not run.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A["Items and configured charges"] --> B["calculateDocumentTaxes"]
  B --> C["Tax preview"]
  B --> D["Saved order tax"]
  E["Order changes"] --> F["Saved item tax + current charge tax"]
  F --> G["recomputeOrderTotals"]
  G --> D
  D --> H["Bills, receipts, and reports"]
Loading

Reviews (5) · Last reviewed commit: "fix(tax): eliminate fractional gross sca..." · Reviewed by Greptile

Comment thread main/services/orders.ts Resolved
Comment thread main/services/orders.ts Resolved
Comment thread main/services/orders.ts Resolved
Comment thread main/services/tax.ts Resolved
Comment thread main/services/orders.ts Resolved
Comment thread frontend/e2e/qa-settings-tax.spec.ts Resolved
Comment thread main/services/orders.ts Resolved
@khaira777
khaira777 force-pushed the fm/ship-r3-document-tax-rounding branch from 15e09b8 to 6d2ec9e Compare October 21, 2026 21:24
@khaira777 khaira777 changed the title fix(main): apply document-scope tax rounding across transaction documents fix(main): evaluate document tax rounding across complete transaction documents Oct 21, 2026
@khaira777

Copy link
Copy Markdown
Contributor Author

@greptileai review this at apex

Comment thread tests/integration-tax.test.ts Resolved
Comment thread tests/integration-tax.test.ts Resolved
Apply document-scope tax rounding across multi-item transactions and charges.

- Add calculateDocumentItemTaxes evaluating all items in a single TaxEngine.calculate call
- Batch calculateConfiguredChargeTaxes to evaluate active charges in one pass
- Call calculateDocumentItemTaxes in calculateTaxPreview, POST /api/orders, and calculateOrderTotals
- Reconcile active document items during order/bill mutations
- Add comprehensive unit and integration tests for document rounding and mutations
- Extend E2E qa-settings-tax spec and capture screenshot

Fixes #1274
…Violated Shared baseline test fixtures in `tests/e2e-server.cjs` must preserve the baseline single-product catalog state (`e2e-product` as the sole product in `e2e-category` with consistent inventory ledger state) so that global POS UI assumptions, selector cardinality (`getByTestId('pos-product-card')`), and database export/import inventory integrity hold across all test suites, while spec-specific test fixtures must be dynamically scoped, isolated, and cleanly torn down within their own test lifecycle. #### Root Cause In commit `72d791f5`, two extra products (`e2e-product-rounding-1` and `e2e-product-rounding-2`) with price 100.10, `stock_quantity: 999`, and `track_inventory: 0` were statically added to `seedPosFixture()` in `tests/e2e-server.cjs` under `e2e-category` solely to support document tax rounding assertions in `frontend/e2e/qa-settings-tax.spec.ts`. This polluted the shared E2E test database across every shard: 1. Any test asserting or clicking `page.getByTestId('pos-product-card')` (e.g. `frontend/e2e/checkout-discounts-disabled.spec.ts:556`, `frontend/e2e/diagnostics-checkout-recovery.spec.ts:33`, and multiple suites across Shards 1, 2, and 3) failed with a Playwright strict mode violation because `pos-product-card` resolved to 3 elements instead of 1. 2. `database-export-import.spec.ts` in Shard 1 failed with `Error: Product stock must match the latest inventory movement history` during snapshot restore verification because the two seeded products had nonzero stock with no corresponding inventory movement history. #### Remediation Across All Sites 1. **Shared Fixture Boundary (`tests/e2e-server.cjs`)**: Removed the static insertion of `e2e-product-rounding-1` and `e2e-product-rounding-2` from `seedPosFixture()`. `e2e-category` now contains solely the baseline `e2e-product`. 2. **Spec-Specific Dynamic Fixture Boundary (`frontend/e2e/qa-settings-tax.spec.ts`)**: Updated the test `document tax rounding applies across transaction documents and preserves rounding consistency` to provision its own isolated category (`Tax Rounding Category`) and products dynamically via `POST /api/categories` and `POST /api/products`, reference their returned IDs for tax preview, order creation, and bill generation, and delete them in the `finally` block before restoring the store country setting. ### Verification Run & Results 1. `git diff --check`: Passed with 0 whitespace errors. 2. `npm run lint`: Passed with 0 errors across backend and frontend. 3. `npm run build`: Backend TypeScript build passed. 4. `npm run build:frontend`: Next.js static build and export passed. 5. `npm run docs:check`: Structure, links, index completeness, and process material checks passed. 6. `npm run test:script-coverage`: Passed (253 reachable test scripts, 311 test files covered). 7. `node scripts/run-test.cjs npx tsx tests/integration-tax.test.ts`: Passed (255/255 passed, 0 failed). 8. `node scripts/run-test.cjs npx tsx tests/tax-engine.test.ts`: Passed (16/16 passed, 0 failed). 9. Playwright E2E suites: - `cd frontend && npx playwright test e2e/qa-settings-tax.spec.ts`: Passed (5/5 passed in 15.5s). - `cd frontend && npx playwright test e2e/checkout-discounts-disabled.spec.ts`: Passed (6/6 passed in 41.4s, resolving the strict mode violation). - `cd frontend && npx playwright test e2e/diagnostics-checkout-recovery.spec.ts`: Passed (5/5 passed in 13.8s, resolving the strict mode violation). Checks not run locally: Full multi-shard browser test matrix across all 4 shards (ran focused failing suites locally due to execution time and environment constraints; CI runner executes all shards)
@khaira777
khaira777 force-pushed the fm/ship-r3-document-tax-rounding branch from b486f6f to e6e9728 Compare October 21, 2026 23:20
@khaira777

Copy link
Copy Markdown
Contributor Author

@greptileai review this at apex

@khaira777 khaira777 changed the title fix(main): evaluate document tax rounding across complete transaction documents chore: update pull request Oct 21, 2026
@khaira777 khaira777 changed the title chore: update pull request fix(tax): apply document tax rounding across transaction documents Oct 21, 2026
Comment thread main/services/orders.ts Resolved
@khaira777 khaira777 changed the title fix(tax): apply document tax rounding across transaction documents fix(main): apply document tax rounding across transaction documents Oct 22, 2026
@khaira777

Copy link
Copy Markdown
Contributor Author

@greptileai review this at apex

1 similar comment
@khaira777

Copy link
Copy Markdown
Contributor Author

@greptileai review this at apex

Comment thread main/services/orders.ts Resolved
Comment thread tests/integration-tax.test.ts Resolved
Comment thread docs/architecture/taxation.md Resolved
@khaira777

Copy link
Copy Markdown
Contributor Author

@greptileai review this at apex

1 similar comment
@khaira777

Copy link
Copy Markdown
Contributor Author

@greptileai review this at apex

@khaira777
khaira777 merged commit 5fb2881 into main Oct 22, 2026
24 checks passed
@khaira777
khaira777 deleted the fm/ship-r3-document-tax-rounding branch October 22, 2026 00:49
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.

fix(tax): apply document-scope tax rounding across the full transaction document

1 participant