Skip to content

Commit

Permalink
feat: Tiered price's first bucket's flat fee is always billed
Browse files Browse the repository at this point in the history
  • Loading branch information
turip committed Jan 20, 2025
1 parent dcd6850 commit a2ade61
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion openmeter/billing/service/lineservice/usagebasedline.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ func tieredPriceCalculator(in tieredPriceCalculatorInput) error {

previousTierQty := alpacadecimal.Zero
for idx, tier := range in.TieredPrice.WithSortedTiers().Tiers {
if previousTierQty.GreaterThanOrEqual(in.ToQty) {
if previousTierQty.GreaterThan(in.ToQty) {
// We already have enough data to bill for this tiered price
break
}
Expand Down
27 changes: 23 additions & 4 deletions openmeter/billing/service/lineservice/usagebasedline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,15 @@ func TestTieredGraduatedCalculation(t *testing.T) {
usage: featureUsageResponse{
LinePeriodQty: alpacadecimal.NewFromFloat(0),
},
expect: newDetailedLinesInput{},
expect: newDetailedLinesInput{
{
Name: "feature: flat price for tier 1",
PerUnitAmount: alpacadecimal.NewFromFloat(100),
Quantity: alpacadecimal.NewFromFloat(1),
ChildUniqueReferenceID: "graduated-tiered-1-flat-price",
PaymentTerm: productcatalog.InArrearsPaymentTerm,
},
},
})
})

Expand Down Expand Up @@ -931,9 +939,16 @@ func TestTieredGraduatedCalculation(t *testing.T) {
LinePeriodQty: alpacadecimal.NewFromFloat(0),
},
expect: newDetailedLinesInput{
{
Name: "feature: flat price for tier 1",
PerUnitAmount: alpacadecimal.NewFromFloat(100),
Quantity: alpacadecimal.NewFromFloat(1),
ChildUniqueReferenceID: "graduated-tiered-1-flat-price",
PaymentTerm: productcatalog.InArrearsPaymentTerm,
},
{
Name: "feature: minimum spend",
PerUnitAmount: alpacadecimal.NewFromFloat(1000),
PerUnitAmount: alpacadecimal.NewFromFloat(900),
Quantity: alpacadecimal.NewFromFloat(1),
ChildUniqueReferenceID: GraduatedMinSpendChildUniqueReferenceID,
PaymentTerm: productcatalog.InArrearsPaymentTerm,
Expand Down Expand Up @@ -1293,8 +1308,9 @@ func TestTieredPriceCalculator(t *testing.T) {
}

t.Run("totals, no usage", func(t *testing.T) {
// If there's no usage in the first tier we need to bill the flat fee regardless.
totalAmount := getTotalAmountForGraduatedTieredPrice(t, alpacadecimal.NewFromFloat(0), testIn)
require.Equal(t, alpacadecimal.NewFromFloat(0), totalAmount)
require.Equal(t, alpacadecimal.NewFromFloat(100), totalAmount)
})

t.Run("totals, usage in tier 1", func(t *testing.T) {
Expand All @@ -1306,7 +1322,10 @@ func TestTieredPriceCalculator(t *testing.T) {
})

t.Run("totals, usage in tier 2", func(t *testing.T) {
totalAmount := getTotalAmountForGraduatedTieredPrice(t, alpacadecimal.NewFromFloat(7), testIn)
totalAmount := getTotalAmountForGraduatedTieredPrice(t, alpacadecimal.NewFromFloat(5.001), testIn)
require.Equal(t, alpacadecimal.NewFromFloat(100+50), totalAmount)

totalAmount = getTotalAmountForGraduatedTieredPrice(t, alpacadecimal.NewFromFloat(7), testIn)
require.Equal(t, alpacadecimal.NewFromFloat(100+50), totalAmount)
})

Expand Down

0 comments on commit a2ade61

Please sign in to comment.