Repository navigation
feat(invoices): add discountAmount so an invoice can show a discount - #122
Merged
Merged
Conversation
Invoice lines cannot be negative and there was no discount field, so a discounted order had to be collapsed into one line at the reduced price. discountAmount is an optional invoice-level figure in cents, bounded by the subtotal: amount = subtotal - discountAmount + taxAmount. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0122PV8TJ7ELGdAX7H2hdrRU
Contributor
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
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.
Problem
items[].unitAmounthasmin: 0andamountwassubtotal + taxAmount, so an invoice could not show a discount. A host invoicing an order with a coupon had to collapse the goods into one line at the reduced price.Change
New optional invoice field
discountAmount(cents, default0,min: 0):discountAmountlarger thansubtotalis refused withDiscount cannot exceed the invoice subtotal, on create and on any update that changes the lines or the discount. A discount therefore cannot push the total below zero. A discount equal to the subtotal is allowed (total = tax).discountAmountis refused by field validation.unitAmountkeepsmin: 0; lines are unchanged.Invoicetype gainsdiscountAmount?: number | null.Why a field and not negative lines
amount, which the host passes (normallyinvoice.amount). Refund limits follow the payment. So neither design touches the provider code, and the choice is about the invoice model only.taxAmount. A second one of the same shape is the smallest addition and reads the same way.unitAmountwould change what an existing field accepts and whatsubtotalmeans (today always the sum of non-negative lines). Anything a host built on those assumptions (rendering, exports, tax on the subtotal) would see negative rows without having opted in.Compatibility
Additive. An invoice without
discountAmountis calculated exactly as before. Hosts that manage their database schema with migrations need one for the newdiscount_amountcolumn on the invoices table.Unchanged and outside this PR:
taxAmounthas no lower bound, so a negative tax larger than the subtotal can still produce a negativeamount, as it could before.Dev app
The demo invoice page shows a "Discount" row, and the demo invoice route no longer replaces a stored total of
0with a recomputed one.Tests
dev/invoice-discount.spec.ts(sqlite, Local API, test provider): the formula, no-discount parity, discount equal to subtotal, refusal above subtotal, negative discount, negative line still refused, partial updates, and a payment for the discounted total that marks the invoice paid and is refunded up to that total. No real Mollie or Stripe calls were made.🤖 Generated with Claude Code
https://claude.ai/code/session_0122PV8TJ7ELGdAX7H2hdrRU