Skip to content

Conversation

@groyoh
Copy link
Contributor

@groyoh groyoh commented Dec 4, 2025

Context

When a wallet has limitations (restricted to specific billable metrics or fee types), the ongoing balance calculation was incorrectly summing all draft invoice amounts instead of only the fees that match the wallet's limitations. This caused the ongoing balance to be understated, as it included fees that should not be applicable to the wallet.

Description

The wallet ongoing balance is calculated as: balance - current_usage - draft_invoices_amount + already_billed_amount. Previously, draft_invoices_amount was calculated using customer.invoices.draft.sum(:total_amount_cents), which ignored wallet limitations entirely.

This PR introduces Wallets::Balance::CalculateApplicableFeesService that filters fees by billable metric limitations and fee type limitations when applicable.

The service is now used by:

  • RefreshOngoingService - for real-time balance refresh
  • RefreshOngoingUsageService - for clock job balance refresh
  • AppliedPrepaidCreditsService - for applying prepaid credits to invoices

@groyoh groyoh force-pushed the fix/wallet-draft-invoice branch from b4a8410 to 612db53 Compare December 4, 2025 10:09
@groyoh groyoh marked this pull request as draft December 8, 2025 10:34
@groyoh
Copy link
Contributor Author

groyoh commented Dec 8, 2025

Converting back to draft as it will conflict with "multiple wallets" initiative. So we'll merge "multiple wallets" first and then rebase this one.

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.

3 participants