Skip to content

fix(billing): preserve charge invoice discount breakdowns - #5258

Merged
turip merged 1 commit into
mainfrom
feat/preserve-charge-invoice-discounts
Oct 6, 2026
Merged

turip merged 1 commit into
mainfrom
feat/preserve-charge-invoice-discounts

Conversation

@turip

@turip turip commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Overview

Charge-backed invoice detailed lines retained discounted totals but lost their discount breakdowns when mapped from realization runs. Preserve discount facts and stable child references for flat-fee and usage-based charges so billing keeps the breakdown and resource identities through draft edits and collection.

Temporarily defer usage-based charges with configured percentage discounts or maximum spend to period-end invoicing, even when progressive billing is enabled, while discount deltas are unsupported.

Notes for reviewer

  • Regression coverage includes draft discount edits, stable discount identities, negative discount validation, and period-end invoicing of discounted usage.
  • Validation: make test-nocache before rebase; affected-package tests and scoped make lint-go-fast after rebase.

Summary by CodeRabbit

  • New Features
    • Invoice line details now preserve charge discount information, including discount amounts, reasons, and references.
    • Usage-based charges with percentage discounts or maximum-spend caps are invoiced at the end of the service period rather than progressively.
  • Bug Fixes
    • Invoices reject negative discount amounts; zero and positive amounts remain valid.

RetriggerConfidence Score: 4/5

The PR should not merge until usage correction invoices preserve discount breakdowns that reconcile with their detailed totals.

Fix All in Claude CodeFindings

  1. P1 Delta discounts disagree with totals ▶
Fix with agent prompt
### Issue 1
openmeter/billing/charges/usagebased/service/linemapper.go:416
When a usage charge has an earlier realization, delta rating subtracts the earlier totals but keeps the current discount breakdown. If a later run reverses a previously discounted detail, its discount total becomes negative while its discount amount remains positive. This new mapping puts that positive discount on the invoice, so the displayed breakdown does not reconcile with the detail’s totals. The discount facts need to reflect the delta before they are mapped.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

This PR maps charge realization discount facts into billing-managed invoice details and defers progressive invoicing for usage charges with percentage discounts or maximum spend.

  • Adds coverage for discount persistence, identity reuse, validation, and period-end collection.
  • Existing usage realization deltas can still produce discount breakdowns that disagree with their totals.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Current usage rating] --> C[Delta rating]
  B[Earlier realization] --> C
  C --> D[Subtract earlier totals]
  C --> E[Retain cumulative discount facts]
  D --> F[Invoice detail]
  E --> F
  F --> G[Discount breakdown can disagree with totals]
Loading

Reviews (1) · Last reviewed commit: "fix(billing): preserve charge invoice di..."

@turip
turip requested a review from a team as a code owner October 6, 2026 12:15
@coderabbitai

coderabbitai Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: openmeterio/openmeter/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a4bf8552-9ee5-47dc-81ba-ab21bb2444ee
📥 Commits

Reviewing files that changed from the base of the PR and between 9cb8114 and fef60ff.

📒 Files selected for processing (12)
  • openmeter/billing/README.md
  • openmeter/billing/charges/README.md
  • openmeter/billing/charges/flatfee/service/linemapper.go
  • openmeter/billing/charges/flatfee/service/linemapper_test.go
  • openmeter/billing/charges/models/detailedline/model.go
  • openmeter/billing/charges/usagebased/service/lineengine.go
  • openmeter/billing/charges/usagebased/service/lineengine_test.go
  • openmeter/billing/charges/usagebased/service/linemapper.go
  • openmeter/billing/charges/usagebased/service/linemapper_test.go
  • openmeter/billing/invoicedetailedline_test.go
  • test/credits/flatfee_invoice_edit_test.go
  • test/credits/usagebased_invoice_discount_test.go

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

Charge detailed lines now map discount snapshots into billing discounts. Usage-based charges with percentage discounts or maximum-spend commitments defer invoicing until period end, even when progressive billing is enabled.

Changes

Charge Discounts in Invoicing

Layer / File(s) Summary
Map discount snapshots to billing lines
openmeter/billing/README.md, openmeter/billing/charges/models/detailedline/model.go, openmeter/billing/charges/flatfee/service/*, openmeter/billing/charges/usagebased/service/linemapper*, openmeter/billing/invoicedetailedline_test.go
Flat-fee and usage-based detailed-line mapping now carries discount facts into billing-managed discounts. Tests check snapshot preservation and validation of negative discount amounts.
Defer selected usage charges until period end
openmeter/billing/charges/README.md, openmeter/billing/charges/usagebased/service/lineengine*
Percentage discounts and maximum-spend commitments disable partial-period progressive billing. Tests also cover zero values, usage discounts, and minimum-spend commitments.
Verify discount details through invoice lifecycle
test/credits/flatfee_invoice_edit_test.go, test/credits/usagebased_invoice_discount_test.go
Integration tests check discount details during flat-fee invoice edits and usage-based invoicing. They verify discount values and resource identities through invoice collection.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Suggested reviewers: chrisgacsal, rolosp

Merge Risk: ⚪ Minimal · up to fef60

The discount-mapping and period-end invoicing changes appear ready to merge after normal checks; no concrete merge-blocking issue was identified.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: preserving charge discount breakdowns on invoices.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · 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.

@turip turip added release-note/bug-fix Release note: Bug Fixes area/billing labels Oct 6, 2026
@turip
turip enabled auto-merge (squash) October 6, 2026 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants