Lump-sum fallback when divided line price can't round-trip#42
Merged
Conversation
Stripe's transform_quantity (e.g. divide_by 50, round up) charges ceil(qty / divide_by) × unit_amount, which often isn't a clean 2-decimal multiple of line.Quantity. Today we naively compute Amount/Quantity and let num.Amount.Divide round to the currency subunit, then GOBL re-multiplies to a sum that no longer matches Stripe's amount — inflating the tax base and producing a totals.rounding residue large enough to mask real errors. After computing the per-unit price, check that quantity × price round-trips back to line.Amount at currency-subunit precision. When it doesn't, collapse the line to (quantity=1, price=line.Amount) so the GOBL tax base reconciles with Stripe exactly. The Stripe-supplied item description carries the per-unit narrative either way, so no information is lost. Tiered billing always takes the lump-sum path (no honest single per-unit price across tier breakpoints). Zero-quantity zero-amount lines preserve the (qty=0, price=Price.UnitAmount) display for readability. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates Stripe→GOBL line conversion to ensure line tax bases reconcile exactly with Stripe totals by falling back to a lump-sum (qty=1, price=line.Amount) representation whenever a derived per-unit price cannot round-trip back to the original line amount (notably with transform_quantity and similar cases). It applies the same round-trip logic to credit note lines and refreshes tests and a golden example output accordingly.
Changes:
- Add round-trip verification for derived per-unit prices; fall back to lump-sum when
qty × price != amount(invoice + credit note). - Treat tiered invoice pricing as lump-sum unconditionally; preserve readable display for
(qty=0, amount=0)invoice lines when a unit price exists. - Add targeted unit tests and regenerate the
gobl_quantity_rounding.jsongolden output.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
lines.go |
Introduces (quantity, price) resolution with round-trip validation and lump-sum fallback for invoices and credit notes. |
lines_test.go |
Adds unit tests covering clean per-unit, transform_quantity/proration mismatches, tiered billing, and zero-quantity edge cases. |
examples/stripe.gobl/out/gobl_quantity_rounding.json |
Updates golden output to reflect lump-sum fallback and removal of invoice-level rounding residue. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
price = line.Amount / quantityat currency-subunit precision, verify thatquantity × priceround-trips back toline.Amount. When it doesn't, collapse the line to(quantity=1, price=line.Amount)so the GOBL tax base reconciles with Stripe exactly. Tiered billing takes the lump-sum path unconditionally (no honest single per-unit price across tier breakpoints). Zero-quantity zero-amount lines keep the readable(qty=0, price=Price.UnitAmount)display.transform_quantity(e.g.divide_by=50, round=up): on a real PL invoice withQuantity=751, Amount=3520.00 PLN, the old3520/751 = 4.69rounded down, then751 × 4.69 = 3522.19, inflating the line by +2.19 PLN and leaving arounding: -2.69 PLNresidue at the invoice level. The fix eliminates the residue."751 users × Additional Active Users (at 220.00 zł per 50 users / month)") is preserved verbatim on lump-sum lines, so the per-unit narrative is never lost — only the synthetic per-unit price the data wouldn't support.Test plan
transform_quantity, proration mismatch, tiered billing, zero-quantity zero-amount, zero-quantity nil-price; plus credit-note clean / non-reconciling / zero-quantity variants.examples/stripe.gobl/out/gobl_quantity_rounding.jsongolden — line 6 (metered204 × 147 ≠ 30000) collapses to lump sum; the priortotals.rounding: 0.12residue disappears.test/invoice.json:totals.payable == "17724.30"matches Stripe's reported total exactly, with noroundingfield.go test ./...green.🤖 Generated with Claude Code