fix(invoices): compute invoice money in Decimal (money migration phase 2)#341
Open
snowsky wants to merge 2 commits into
Open
fix(invoices): compute invoice money in Decimal (money migration phase 2)#341snowsky wants to merge 2 commits into
snowsky wants to merge 2 commits into
Conversation
Extend the existing money helpers with line_amount, subtotal_from_items, compute_discount and invoice_total. They compute in Decimal (ROUND_HALF_UP, 2dp) and return float for the Numeric(asdecimal=False) columns, so invoice totals no longer accumulate binary-float drift. Adds unit tests (TestInvoiceTotals).
Route subtotal/discount/total and per-line amounts through the Decimal money helpers in create, clone and update, and round the paid_amount increment/decrease math (current_paid sum, incremental, and the payment-removal subtractions) so a $33.33+$33.33+$33.34 reversal no longer leaves a residual penny. The ORM still returns float (Numeric asdecimal=False), so no other arithmetic is affected.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Phase 2 of the invoice money migration (plan:
docs/todos/invoice-money-decimal-migration-plan.md). Phase 1 (#340) moved storage toNUMERIC(15,4); this phase fixes the actual float-drift correctness bug in the calculations.Course-correction from the original plan
Phase 1 said Phase 2 would flip the columns to
asdecimal=True. An audit of the codebase found 18Decimal × floatarithmetic sites across 7 files (payments, discounts, cashflow, subscriptions, inventory) that would raiseTypeErrorunder that flip — HIGH risk. So instead this PR does "Decimal math at the edges": compute totals inDecimaland round (ROUND_HALF_UP, 2dp) at the calculation/storage boundaries, returningfloatfor theNumeric(asdecimal=False)columns. This fixes the drift where it actually happens without destabilizing the rest of the codebase. (A futureasdecimal=Truehardening pass would first need those 18 sites wrapped.)Changes
core/utils/money.py— extended the existing Decimal helper (reused, not duplicated) withline_amount,subtotal_from_items,compute_discount,invoice_total. Each computes inDecimaland returnsfloat.core/routers/invoices/crud.py:current_paidviasum_money, and the incremental + payment-removal subtractions are Decimal-rounded — fixes the residual-penny bug (e.g. removing$33.33 + $33.33 + $33.34).Tests
api/tests/test_money.py— extended withTestInvoiceTotals(line amounts, per-line-rounded subtotal, percentage/fixed/capped discounts, totals floored at zero, float-drift elimination). 18 money tests pass locally.Not in this PR
float(ORM returns float viaasdecimal=False), so no UI break.amountso the displayed total matches the backend to the cent.calculate-discount,discount_rule_service) still use float; they should be aligned withcompute_discountin Phase 3 for display parity.asdecimal=Trueflip (see above).Verification
Helpers covered by unit tests;
crud.pycompiles. Full create/update/clone round-trip needs the stack up — flagging for review.