Skip to content

fix(billing): stamp manual invoice edits in service diff - #4591

Merged
turip merged 3 commits into
mainfrom
feat/invoice-edit-etoe-manual-flatfee
Jun 28, 2026
Merged

turip merged 3 commits into
mainfrom
feat/invoice-edit-etoe-manual-flatfee

Conversation

@turip

@turip turip commented Jun 28, 2026 •

Copy link
Copy Markdown
Member

Summary

  • move mutable invoice-line diff validation back into the service-level differ
  • stamp API-created invoice lines as manual before line-engine dispatch, while leaving system-created lines untouched
  • keep API update/delete ownership stamping after line-engine inspection
  • tighten credit-then-invoice sync and e2e coverage for manual flat-fee overrides

Root Cause

API-created charge-backed invoice lines could reach line engines with caller-supplied ownership because ownership stamping had drifted into the apply path. The credit-then-invoice update tests also masked part of the behavior by setting ManagedBy inside the API edit closure.

Validation

  • direnv exec . make lint-go-fast
  • direnv exec . make test-nocache

Summary by CodeRabbit

  • New Features
    • Enhanced flat-fee invoice line edits under manual-approval so customer-facing fields (e.g., service period, invoice date, price, taxes, discounts) can be updated while preserving the intended edited-line behavior.
    • Added support for including deleted charges via status filtering.
  • Bug Fixes
    • Corrected invoice line ownership/lifecycle handling for API create, update, and delete edits, including manual override behavior.
    • Improved validation and merge behavior for period changes, progressive usage discounts, and gathering/standard line edits.
  • Documentation
    • Updated billing guidance for mutable invoice line edits and manual override behavior.
  • Tests
    • Added/expanded unit and end-to-end coverage for invoice override editing and charge listing behavior.

@turip
turip requested a review from a team as a code owner June 28, 2026 18:56
@coderabbitai

coderabbitai Bot commented Jun 28, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 18897a26-266b-41d2-a917-13a7a6d05d39

📥 Commits

Reviewing files that changed from the base of the PR and between e5b263c and b40ea05.

📒 Files selected for processing (1)
  • e2e/billinginvoice_override_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • e2e/billinginvoice_override_test.go

📝 Walkthrough

Walkthrough

This PR changes invoice-line edit ownership stamping, legacy override validation, and lifecycle-controller mapping for flat-fee manual overrides. It also updates related helpers, tests, and billing guidance, plus one e2e flow covering invoice replacement and charge ownership.

Changes

Flat-fee manual override edit flow

Layer / File(s) Summary
Merge and legacy override validation
openmeter/billing/httpdriver/invoiceline.go, openmeter/billing/lineengine/engine.go, .agents/skills/billing/SKILL.md, .agents/skills/charges/SKILL.md
Invoice-line merge helpers stop setting ManagedBy on replace updates, adjust tax and rate-card merge behavior, and replace split-line validation with legacy override checks that distinguish flat-fee and usage-based period changes. Billing guidance was updated to match the new stamping and override behavior.
Service stamping and charge API mapping
openmeter/billing/service/invoiceupdate.go, openmeter/billing/service/stdinvoice.go, openmeter/billing/service/gatheringinvoice.go, api/v3/handlers/customers/charges/convert.go, api/v3/handlers/customers/charges/list.go
Mutable invoice diffs now use ChangeSource, API-created lines are stamped after routing, nil generic lines are rejected, and charge lifecycle-controller conversion now supports a manual-override option. Charge listing also includes deleted results when the status filter requests deleted.
Unit tests and service fixtures
openmeter/billing/httpdriver/invoiceline_test.go, openmeter/billing/lineengine/engine_test.go, openmeter/billing/service/invoiceupdate_test.go, openmeter/billing/service/lineengine_test.go, openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go
New and updated tests cover HTTP merge behavior, legacy override validation, diff stamping, created-line managed-by tracking, and subscription-sync assertions around manual edit ownership.
E2E flow and client helpers
e2e/billinginvoice_override_test.go, e2e/v3helpers_test.go
A new e2e test covers billing profile setup, credits, subscription cancellation, invoice replacement, charge ownership, and credit totals. New v3 helpers support billing, charge, credit, subscription, and pagination flows used by the test.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • openmeterio/openmeter#4552: It also changes the mutable invoice-line edit path through openmeter/billing/service/invoiceupdate.go and openmeter/billing/lineengine/engine.go.
  • openmeterio/openmeter#4586: It overlaps with the same manual override and flat-fee edit flow, including line ownership and sync behavior.
  • openmeterio/openmeter#4364: It touches the same sync_credittheninvoice_test.go manual-edit ownership assertions.

Suggested labels

area/billing

Suggested reviewers

  • tothandras
  • borbelyr-kong
  • GAlexIHU
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 17.65% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ 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 matches the main change: moving manual invoice-edit stamping into the billing service diff path.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/invoice-edit-etoe-manual-flatfee

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 the release-note/bug-fix Release note: Bug Fixes label Jun 28, 2026
@greptile-apps

greptile-apps Bot commented Jun 28, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR updates manual invoice-line edit handling in billing. The main changes are:

  • Moves API-created invoice-line ownership stamping into the service diff path.
  • Re-stamps line-engine create outputs before rebuilding invoices.
  • Preserves manual flat-fee overrides through charge sync and charge API conversion.
  • Adds deleted-charge listing support.
  • Expands unit and e2e coverage for invoice override flows.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
openmeter/billing/service/invoiceupdate.go Moves API-created line ownership stamping into diff construction and keeps created engine outputs manual after dispatch.
openmeter/billing/lineengine/engine.go Updates legacy invoice-line override validation so flat-fee manual overrides can change periods while stricter split-line and usage-based checks remain.
api/v3/handlers/customers/charges/convert.go Reports override-backed charges as manually controlled in the charge API response.
openmeter/billing/httpdriver/invoiceline.go Moves mutable-line ownership and validation responsibilities out of API merge helpers and into service and line-engine paths.

Reviews (2): Last reviewed commit: "test(e2e): tighten charge override asser..." | Re-trigger Greptile

}
req.StatusIn = statuses
// TODO: Also add a deleted_at filter
req.IncludeDeleted = slices.Contains(statuses, meta.ChargeStatusDeleted)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Deleted Rows Broaden Mixed Filters

When filter[status][oeq] contains both active and deleted, this flips IncludeDeleted for the whole repository query. That removes the normal deleted_at IS NULL guard for the active branch too, so soft-deleted override rows whose status is still active can be returned as active charges.

Prompt To Fix With AI
This is a comment left during a code review.
Path: api/v3/handlers/customers/charges/list.go
Line: 110

Comment:
**Deleted Rows Broaden Mixed Filters**

When `filter[status][oeq]` contains both `active` and `deleted`, this flips `IncludeDeleted` for the whole repository query. That removes the normal `deleted_at IS NULL` guard for the active branch too, so soft-deleted override rows whose status is still active can be returned as active charges.

How can I resolve this? If you propose a fix, please make it concise.

Fix in Claude Code Fix in Codex

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@e2e/billinginvoice_override_test.go`:
- Around line 670-675: The helper comment on
assertFlatFeeChargeLifecycleController is stale and misleading because the
lifecycle contract now treats LifecycleController as manual whenever an override
layer exists, regardless of the underlying subscription-managed sync behavior.
Update the comment to describe the observable override/controller behavior
instead of tying it to invoice-line ManagedBy ownership, keeping the wording
aligned with the helper’s assertions in assertFlatFeeChargeLifecycleController
and the flatFee LifecycleController field.
- Around line 643-668: `listChargesByName` currently overwrites entries when two
charges have the same name, so add a uniqueness check before writing into the
map. In the helper that switches on `charge.Discriminator()` and builds `out`,
assert that the derived key (`flatFee.Name` or `usageBased.Name`) is not already
present and fail the test immediately if it is. This should be done inside
`listChargesByName` so the e2e catches duplicate charge emission instead of
silently passing.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 1fea0538-6cee-44a2-b95e-ace064388170

📥 Commits

Reviewing files that changed from the base of the PR and between 8edef47 and e5b263c.

📒 Files selected for processing (17)
  • .agents/skills/billing/SKILL.md
  • .agents/skills/charges/SKILL.md
  • api/v3/handlers/customers/charges/convert.go
  • api/v3/handlers/customers/charges/list.go
  • e2e/billinginvoice_override_test.go
  • e2e/v3helpers_test.go
  • openmeter/billing/httpdriver/invoiceline.go
  • openmeter/billing/httpdriver/invoiceline_test.go
  • openmeter/billing/invoiceline.go
  • openmeter/billing/lineengine/engine.go
  • openmeter/billing/lineengine/engine_test.go
  • openmeter/billing/service/gatheringinvoice.go
  • openmeter/billing/service/invoiceupdate.go
  • openmeter/billing/service/invoiceupdate_test.go
  • openmeter/billing/service/lineengine_test.go
  • openmeter/billing/service/stdinvoice.go
  • openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go

Comment thread e2e/billinginvoice_override_test.go Resolved
Comment thread e2e/billinginvoice_override_test.go Resolved
@turip
turip merged commit 33cf406 into main Jun 28, 2026
26 checks passed
@turip
turip deleted the feat/invoice-edit-etoe-manual-flatfee branch June 28, 2026 20:08
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