Skip to content

fix(inventory): preserve ledger chain continuity for legacy stock quantities (#1217) - #1218

Open
khaira777 wants to merge 1 commit into
mainfrom
fix/legacy-inventory-precision
Open

khaira777 wants to merge 1 commit into
mainfrom
fix/legacy-inventory-precision

Conversation

@khaira777

Copy link
Copy Markdown
Contributor

Summary

Fixes #1217

When a store possesses legacy stock with more than 8 decimal places (e.g. 10.123456789012), adjusting the stock truncated the persisted stock to 8 decimal places (9.62345679), while the movement delta remained -0.5. In validateInventoryLedgerRows, expectedStockAfter (10.123456789012 - 0.5 = 9.623456789012) diverged from current.stockAfter (9.62345679) by ~1e-9, which exceeded the floating-point tolerance (~2e-14) and reported Inventory movement history contains a broken stock chain. This failed database health checks, backup creations, and restores.

Solution

In main/services/inventory.ts adjustProductStock:

  • Added getDecimalPlaces(value: number): number helper.
  • Dynamically size targetPrecision = Math.max(INVENTORY_QUANTITY_PRECISION, Math.min(14, getDecimalPlaces(stockBefore))).
  • Apply targetPrecision to SQL ROUND(..., targetPrecision) and effectiveQuantityDelta = Number((stockAfter - stockBefore).toFixed(targetPrecision)).
  • Added unit and ledger validation regression tests in tests/inventory-ledger.test.ts verifying that legacy stock with >8 decimals preserves exact ledger chain continuity across stock increases and decreases, passing validateInventoryLedgerDatabase(db) with zero errors.

Verification

  • npm run test:inventory-ledger (60/60 passed)
  • npm run test:backup (passed)
  • npm run test:upgrade-path (passed)
  • npm run lint (0 errors)
  • npm run build (passed)
  • npm run docs:check (passed)

@coderabbitai

coderabbitai Bot commented Oct 18, 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: ba099e82-a05a-4aed-b11b-020b9e820ded

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 force-pushed the fix/legacy-inventory-precision branch from 91b5fd7 to 8803024 Compare October 18, 2026 18:38
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(inventory): preserve ledger chain continuity when adjusting legacy stock with high decimal precision

1 participant