Skip to content

fix(billing): net prior split discounts in legacy commitment history - #5367

Merged
turip merged 5 commits into
openmeterio:mainfrom
arian-gogani:codex/legacy-minimum-spend-net
Oct 27, 2026
Merged

turip merged 5 commits into
openmeterio:mainfrom
arian-gogani:codex/legacy-minimum-spend-net

Conversation

@arian-gogani

@arian-gogani arian-gogani commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

SplitLineHierarchy.SumNetAmount sums persisted prior-line Amount, which is gross, while the caller treats it as net spend when evaluating commitments. The split-line history introduced in #5341 carries gross amount and charges but omits DiscountsTotal, even though the persisted invoice line has that field. A prior line with 100 in gross amount and 40 in discounts therefore contributes 100 where the function promises 60.

This carries DiscountsTotal into the history model and subtracts it in SumNetAmount. #5297 established the net-of-discounts calculation for the separate delta engine; this change addresses the legacy history path.

Verification

  • Focused billing test: before the subtraction, expected 60 and got 100 (exit 1); after, it passes (exit 0).
  • Adapter test: a persisted line with 100 gross and 40 discounts maps to 40 in DiscountsTotal, and SumNetAmount returns 60. Passed.
  • go test ./openmeter/billing/... passed.
  • go vet ./openmeter/billing/... ./test/... passed.

This demonstrates the history sum and adapter mapping. I did not run a database-backed invoice flow or independently execute the maximum-spend commitment path.

Summary by CodeRabbit

  • Bug Fixes
    • Minimum and maximum spend commitments now account for discounts when calculating previously billed amounts across invoice periods, helping ensure charges and minimum-spend top-ups reflect the correct net amount.
  • Documentation
    • Clarified how spend commitments use prior usage after discounts and before taxes and credits.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no actionable issue remains.

Summary

The PR makes legacy commitment history count prior spend after discounts.

  • Progressive commitments count earlier invoice slices after discounts.

No new actionable issues or repository-rule violations were found. Tests were not run during this review.

Reviews (5) · Last reviewed commit: "test(billing): mirror progressive commit..." · Reviewed by Greptile

…t; focused test 100 vs 60 red before, green after; billing tests and vet pass
@arian-gogani
arian-gogani requested a review from a team as a code owner October 26, 2026 15:36
@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 5b1f76f and 5f13ca1.

Files selected for processing: 2
  • test/billing/discount_test.go
  • test/credits_billing_compat/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

Split-line history now retains discounts from database invoice lines. SumNetAmount subtracts those discounts from eligible child amounts before optionally adding charges. Tests cover discount-aware totals and minimum and maximum spend commitments across progressive invoice slices.

Changes: Discounted split-line history

Layer / File(s) Summary
Preserve discounts in history
openmeter/billing/invoicelinesplitgroup.go, openmeter/billing/adapter/invoicelinesplitgroup.go
SplitLineHierarchyLine adds DiscountsTotal. The database mapping copies this value into the hierarchy line.
Subtract discounts from prior net amounts
openmeter/billing/invoicelinesplitgroup.go, openmeter/billing/splitlinehierarchy_test.go, test/billing/discount_test.go, test/credits_billing_compat/discount_test.go, openmeter/billing/README.md
SumNetAmount subtracts discounts from eligible child amounts and still adds charges when requested. Tests cover discounted totals and progressive minimum and maximum spend commitments. The README describes how discounts and commitment amounts are counted across slices.

Priority: Normal

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

Change: Bug fix

Possibly related PRs

Suggested reviewers: turip, borosr

Merge Risk: Minimal · up to 5f13c

The change appears mergeable after normal checks; the database-backed invoice flows have not been independently run.

Pre-merge checks: Passed 4
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: applying prior split discounts when calculating net billing history for legacy commitments.
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
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · 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.

Comment thread openmeter/billing/adapter/invoicelinesplitgroup_net_test.go Outdated
Comment thread test/billing/discount_commitments_test.go Outdated
@turip turip added the release-note/bug-fix Release note: Bug Fixes label Oct 26, 2026

@turip turip left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@turip
turip merged commit 5e0d2a9 into openmeterio:main Oct 27, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants