Skip to content

Commit 99c55df

Browse files
committed
chore: prior run values
1 parent eb760ff commit 99c55df

13 files changed

Lines changed: 423 additions & 191 deletions

File tree

openmeter/billing/charges/usagebased/rating.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var _ rating.StandardLineAccessor = (*RateableIntent)(nil)
1616
type RateableIntent struct {
1717
Intent
1818

19+
ServicePeriod timeutil.ClosedPeriod
1920
MeterValue alpacadecimal.Decimal
2021
CreditsApplied billing.CreditsApplied
2122
}

openmeter/billing/charges/usagebased/service/creditheninvoice.go

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -248,16 +248,15 @@ func (s *CreditThenInvoiceStateMachine) startInvoiceCreatedRun(
248248
}
249249

250250
result, err := s.Runs.CreateRatedRun(ctx, usagebasedrun.CreateRatedRunInput{
251-
Charge: s.Charge,
252-
CustomerOverride: s.CustomerOverride,
253-
FeatureMeter: s.FeatureMeter,
254-
Type: runType,
255-
StoredAtLT: storedAtLT,
256-
ServicePeriodTo: servicePeriodTo,
257-
LineID: lo.ToPtr(input.LineID),
258-
IgnoreMinimumCommitment: ignoreMinimumCommitmentForRunType(runType),
259-
CreditAllocation: usagebasedrun.CreditAllocationAvailable,
260-
CurrencyCalculator: s.CurrencyCalculator,
251+
Charge: s.Charge,
252+
CustomerOverride: s.CustomerOverride,
253+
FeatureMeter: s.FeatureMeter,
254+
Type: runType,
255+
StoredAtLT: storedAtLT,
256+
ServicePeriodTo: servicePeriodTo,
257+
LineID: lo.ToPtr(input.LineID),
258+
CreditAllocation: usagebasedrun.CreditAllocationAvailable,
259+
CurrencyCalculator: s.CurrencyCalculator,
261260
})
262261
if err != nil {
263262
return err
@@ -275,12 +274,6 @@ func (s *CreditThenInvoiceStateMachine) StartFinalInvoiceRun(ctx context.Context
275274
return s.startInvoiceCreatedRun(ctx, input, usagebased.RealizationRunTypeFinalRealization)
276275
}
277276

278-
func ignoreMinimumCommitmentForRunType(runType usagebased.RealizationRunType) bool {
279-
// Partial invoice runs are interim cumulative checkpoints. Minimum commitment is billed only on the
280-
// final realization, so partial runs must suppress it during both creation and later snapshotting.
281-
return runType == usagebased.RealizationRunTypePartialInvoice
282-
}
283-
284277
func resolveInvoiceCreatedTrigger(charge usagebased.Charge, billedPeriod timeutil.ClosedPeriod) meta.Trigger {
285278
if meta.NormalizeTimestamp(billedPeriod.To).Equal(meta.NormalizeTimestamp(charge.Intent.ServicePeriod.To)) {
286279
return meta.TriggerFinalInvoiceCreated
@@ -309,17 +302,15 @@ func (s *CreditThenInvoiceStateMachine) SnapshotInvoiceUsage(ctx context.Context
309302

310303
storedAtLT := meta.NormalizeTimestamp(currentRun.StoredAtLT)
311304

312-
ratingResult, err := s.Rater.GetDetailedLinesForUsage(ctx, usagebasedrating.GetDetailedLinesForUsageInput{
313-
Charge: s.Charge,
314-
PriorRuns: s.Charge.Realizations.Without(currentRun.ID),
315-
Customer: s.CustomerOverride,
316-
FeatureMeter: s.FeatureMeter,
317-
ServicePeriodTo: currentRun.ServicePeriodTo,
318-
StoredAtLT: storedAtLT,
319-
IgnoreMinimumCommitment: ignoreMinimumCommitmentForRunType(currentRun.Type),
305+
ratingResult, err := s.Rater.GetDetailedRatingForUsage(ctx, usagebasedrating.GetDetailedRatingForUsageInput{
306+
Charge: s.Charge,
307+
StoredAtLT: storedAtLT,
308+
ServicePeriodTo: currentRun.ServicePeriodTo,
309+
Customer: s.CustomerOverride,
310+
FeatureMeter: s.FeatureMeter,
320311
})
321312
if err != nil {
322-
return fmt.Errorf("get rating for usage: %w", err)
313+
return fmt.Errorf("get detailed rating for usage: %w", err)
323314
}
324315

325316
currentTotals := ratingResult.Totals.RoundToPrecision(s.CurrencyCalculator)

openmeter/billing/charges/usagebased/service/creditheninvoice_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,3 @@ func TestResolveInvoiceCreatedTrigger(t *testing.T) {
5858
require.Equal(t, meta.TriggerFinalInvoiceCreated, trigger)
5959
})
6060
}
61-
62-
func TestIgnoreMinimumCommitmentForRunType(t *testing.T) {
63-
t.Run("partial invoice run", func(t *testing.T) {
64-
require.True(t, ignoreMinimumCommitmentForRunType(usagebased.RealizationRunTypePartialInvoice))
65-
})
66-
67-
t.Run("final realization run", func(t *testing.T) {
68-
require.False(t, ignoreMinimumCommitmentForRunType(usagebased.RealizationRunTypeFinalRealization))
69-
})
70-
}

openmeter/billing/charges/usagebased/service/creditsonly.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,15 @@ func (s *CreditsOnlyStateMachine) FinalizeRealizationRun(ctx context.Context) er
184184

185185
storedAtLT := meta.NormalizeTimestamp(currentRun.StoredAtLT)
186186

187-
ratingResult, err := s.Rater.GetDetailedLinesForUsage(ctx, usagebasedrating.GetDetailedLinesForUsageInput{
187+
ratingResult, err := s.Rater.GetDetailedRatingForUsage(ctx, usagebasedrating.GetDetailedRatingForUsageInput{
188188
Charge: s.Charge,
189-
PriorRuns: s.Charge.Realizations.Without(currentRun.ID),
189+
StoredAtLT: storedAtLT,
190+
ServicePeriodTo: currentRun.ServicePeriodTo,
190191
Customer: s.CustomerOverride,
191192
FeatureMeter: s.FeatureMeter,
192-
ServicePeriodTo: currentRun.ServicePeriodTo,
193-
StoredAtLT: storedAtLT,
194193
})
195194
if err != nil {
196-
return fmt.Errorf("get rating for usage: %w", err)
195+
return fmt.Errorf("get detailed rating for usage: %w", err)
197196
}
198197

199198
currentTotals := ratingResult.Totals.RoundToPrecision(s.CurrencyCalculator)
@@ -219,12 +218,18 @@ func (s *CreditsOnlyStateMachine) FinalizeRealizationRun(ctx context.Context) er
219218
}
220219
currentRun.DetailedLines = mo.Some(runDetailedLines)
221220

222-
if _, err := s.Adapter.UpdateRealizationRun(ctx, usagebased.UpdateRealizationRunInput{
221+
currentRunBase, err := s.Adapter.UpdateRealizationRun(ctx, usagebased.UpdateRealizationRunInput{
223222
ID: currentRun.ID,
224223
StoredAtLT: mo.Some(storedAtLT),
225224
MeteredQuantity: mo.Some(ratingResult.Quantity),
226225
Totals: mo.Some(currentTotals),
227-
}); err != nil {
226+
})
227+
if err != nil {
228+
return fmt.Errorf("update realization run: %w", err)
229+
}
230+
currentRun.RealizationRunBase = currentRunBase
231+
232+
if err := s.Charge.Realizations.SetRealizationRun(currentRun); err != nil {
228233
return fmt.Errorf("update realization run: %w", err)
229234
}
230235

0 commit comments

Comments
 (0)