Skip to content

feat(invoices): add discountAmount so an invoice can show a discount - #122

Merged
bvdaakster merged 1 commit into
mainfrom
feat/invoice-discount-amount
Nov 28, 2026
Merged

bvdaakster merged 1 commit into
mainfrom
feat/invoice-discount-amount

Conversation

@bvdaakster

Copy link
Copy Markdown
Contributor

Problem

items[].unitAmount has min: 0 and amount was subtotal + 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, default 0, min: 0):

amount = subtotal − discountAmount + taxAmount
  • A discountAmount larger than subtotal is refused with Discount 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).
  • A negative discountAmount is refused by field validation.
  • unitAmount keeps min: 0; lines are unchanged.
  • Invoice type gains discountAmount?: number | null.

Why a field and not negative lines

  • Nothing in the plugin sends invoice lines to a provider. Mollie, Stripe and the test provider are charged the payment's own amount, which the host passes (normally invoice.amount). Refund limits follow the payment. So neither design touches the provider code, and the choice is about the invoice model only.
  • The invoice already has one invoice-level money figure next to the lines, taxAmount. A second one of the same shape is the smallest addition and reads the same way.
  • Allowing negative unitAmount would change what an existing field accepts and what subtotal means (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.
  • "Total never below zero" is a single bound on one field here. With negative lines it needs a cross-line rule that fails far from the line that caused it.

Compatibility

Additive. An invoice without discountAmount is calculated exactly as before. Hosts that manage their database schema with migrations need one for the new discount_amount column on the invoices table.

Unchanged and outside this PR: taxAmount has no lower bound, so a negative tax larger than the subtotal can still produce a negative amount, 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 0 with 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

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
@claude

claude Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@bvdaakster
bvdaakster merged commit ae75c83 into main Nov 28, 2026
9 checks passed
@bvdaakster
bvdaakster deleted the feat/invoice-discount-amount branch November 28, 2026 13:54
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.

1 participant