Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions openmeter/billing/invoiceline.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,70 @@ func (i *Line) DisassociateChildren() {
}
}

// helper functions for generating new lines
type NewFlatFeeLineInput struct {
ID string
CreatedAt time.Time
UpdatedAt time.Time

Namespace string
Period Period
InvoiceAt time.Time

InvoiceID string

Name string
Metadata map[string]string
Description *string

Currency currencyx.Code

ManagedBy InvoiceLineManagedBy

PerUnitAmount alpacadecimal.Decimal
PaymentTerm productcatalog.PaymentTermType
// TODO: Is this needed?
// Category FlatFeeCategory
Quantity alpacadecimal.Decimal

RateCardDiscounts Discounts
}

func NewFlatFeeLine(input NewFlatFeeLineInput) *Line {
return &Line{
LineBase: LineBase{
Namespace: input.Namespace,
ID: input.ID,
CreatedAt: input.CreatedAt,
UpdatedAt: input.UpdatedAt,

Period: input.Period,
InvoiceAt: input.InvoiceAt,
InvoiceID: input.InvoiceID,

Name: input.Name,
Metadata: input.Metadata,
Description: input.Description,

Status: InvoiceLineStatusValid,

Type: InvoiceLineTypeFee,

ManagedBy: lo.CoalesceOrEmpty(input.ManagedBy, SystemManagedLine),

Currency: input.Currency,
RateCardDiscounts: input.RateCardDiscounts,
},
FlatFee: &FlatFeeLine{
PerUnitAmount: input.PerUnitAmount,
PaymentTerm: input.PaymentTerm,
// Category: lo.CoalesceOrEmpty(input.Category, FlatFeeCategoryRegular),
Category: FlatFeeCategoryRegular,
Quantity: input.Quantity,
},
}
}

// TODO[OM-1016]: For events we need a json marshaler
type LineChildren struct {
mo.Option[[]*Line]
Expand Down
43 changes: 18 additions & 25 deletions openmeter/notification/internal/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,32 +185,25 @@ func (t *TestEventGenerator) newTestInvoicePayload(ctx context.Context, namespac
Number: lo.ToPtr("TEST-INV-1"),
Currency: currencyx.Code(currency.USD),
Lines: billing.NewLineChildren([]*billing.Line{
{
LineBase: billing.LineBase{
Namespace: namespace,
ID: ulid.Make().String(),
CreatedAt: now,
UpdatedAt: now,

ManagedBy: billing.ManuallyManagedLine,

Name: "test flat fee",
Type: billing.InvoiceLineTypeFee,
Period: billing.Period{
Start: now.Add(-time.Hour * 24 * 30),
End: now,
},
InvoiceAt: now,

Status: billing.InvoiceLineStatusValid,
},
FlatFee: &billing.FlatFeeLine{
PerUnitAmount: alpacadecimal.NewFromInt(1000),
Quantity: alpacadecimal.NewFromInt(1),
PaymentTerm: productcatalog.InAdvancePaymentTerm,
Category: billing.FlatFeeCategoryRegular,
billing.NewFlatFeeLine(billing.NewFlatFeeLineInput{
Namespace: namespace,
ID: ulid.Make().String(),
CreatedAt: now,
UpdatedAt: now,

ManagedBy: billing.ManuallyManagedLine,

Name: "test flat fee",
Period: billing.Period{
Start: now.Add(-time.Hour * 24 * 30),
End: now,
},
},
InvoiceAt: now,

PerUnitAmount: alpacadecimal.NewFromInt(1000),
Quantity: alpacadecimal.NewFromInt(1),
PaymentTerm: productcatalog.InAdvancePaymentTerm,
}),
}),
})
if err != nil {
Expand Down
48 changes: 18 additions & 30 deletions test/app/custominvoicing/invocing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,18 @@ func (s *CustomInvoicingTestSuite) TestInvoicingFlowHooksEnabled() {
Customer: customerEntity.GetID(),
Currency: currencyx.Code(currency.HUF),
Lines: []*billing.Line{
{
LineBase: billing.LineBase{
Period: billing.Period{Start: periodStart, End: periodEnd},
billing.NewFlatFeeLine(billing.NewFlatFeeLineInput{
Period: billing.Period{Start: periodStart, End: periodEnd},

InvoiceAt: issueAt,
ManagedBy: billing.ManuallyManagedLine,
InvoiceAt: issueAt,
ManagedBy: billing.ManuallyManagedLine,

Type: billing.InvoiceLineTypeFee,
Name: "Test item - HUF",

Name: "Test item - HUF",
},
FlatFee: &billing.FlatFeeLine{
PerUnitAmount: alpacadecimal.NewFromFloat(200),
Quantity: alpacadecimal.NewFromFloat(3),
Category: billing.FlatFeeCategoryRegular,
PaymentTerm: productcatalog.InAdvancePaymentTerm,
},
},
PerUnitAmount: alpacadecimal.NewFromFloat(200),
Quantity: alpacadecimal.NewFromFloat(3),
PaymentTerm: productcatalog.InAdvancePaymentTerm,
}),
{
LineBase: billing.LineBase{
Period: billing.Period{Start: periodStart, End: periodEnd},
Expand Down Expand Up @@ -357,24 +351,18 @@ func (s *CustomInvoicingTestSuite) TestInvoicingFlowPaymentStatusOnly() {
Customer: customerEntity.GetID(),
Currency: currencyx.Code(currency.HUF),
Lines: []*billing.Line{
{
LineBase: billing.LineBase{
Period: billing.Period{Start: periodStart, End: periodEnd},
billing.NewFlatFeeLine(billing.NewFlatFeeLineInput{
Period: billing.Period{Start: periodStart, End: periodEnd},

InvoiceAt: issueAt,
ManagedBy: billing.ManuallyManagedLine,
InvoiceAt: issueAt,
ManagedBy: billing.ManuallyManagedLine,

Type: billing.InvoiceLineTypeFee,
Name: "Test item - HUF",

Name: "Test item - HUF",
},
FlatFee: &billing.FlatFeeLine{
PerUnitAmount: alpacadecimal.NewFromFloat(200),
Quantity: alpacadecimal.NewFromFloat(3),
Category: billing.FlatFeeCategoryRegular,
PaymentTerm: productcatalog.InAdvancePaymentTerm,
},
},
PerUnitAmount: alpacadecimal.NewFromFloat(200),
Quantity: alpacadecimal.NewFromFloat(3),
PaymentTerm: productcatalog.InAdvancePaymentTerm,
}),
},
})
s.NoError(err, "failed to create pending invoice lines")
Expand Down
39 changes: 14 additions & 25 deletions test/billing/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,21 +250,15 @@ func (s *CollectionTestSuite) TestCollectionFlowWithFlatFeeOnly() {
Customer: customer.GetID(),
Currency: currencyx.Code(currency.USD),
Lines: []*billing.Line{
{
LineBase: billing.LineBase{
Period: billing.Period{Start: periodStart, End: periodEnd},
InvoiceAt: periodStart,
ManagedBy: billing.ManuallyManagedLine,
Type: billing.InvoiceLineTypeFee,
Name: "Flat fee",
},
FlatFee: &billing.FlatFeeLine{
PerUnitAmount: alpacadecimal.NewFromFloat(10),
Quantity: alpacadecimal.NewFromFloat(1),
PaymentTerm: productcatalog.InAdvancePaymentTerm,
Category: billing.FlatFeeCategoryRegular,
},
},
billing.NewFlatFeeLine(billing.NewFlatFeeLineInput{
Period: billing.Period{Start: periodStart, End: periodEnd},
InvoiceAt: periodStart,
Name: "Flat fee",

PerUnitAmount: alpacadecimal.NewFromFloat(10),
Quantity: alpacadecimal.NewFromFloat(1),
PaymentTerm: productcatalog.InAdvancePaymentTerm,
}),
},
})
s.NoError(err)
Expand Down Expand Up @@ -362,25 +356,20 @@ func (s *CollectionTestSuite) TestCollectionFlowWithFlatFeeEditing() {
End: periodEnd.Add(time.Hour * 2),
}

invoice.Lines.Append(&billing.Line{
LineBase: billing.LineBase{
invoice.Lines.Append(
billing.NewFlatFeeLine(billing.NewFlatFeeLineInput{
Namespace: namespace,
Currency: currencyx.Code(currency.USD),
InvoiceID: invoice.ID,
Status: billing.InvoiceLineStatusValid,
Period: linePeriod,
InvoiceAt: linePeriod.End,
ManagedBy: billing.ManuallyManagedLine,
Type: billing.InvoiceLineTypeFee,
Name: "Flat fee",
},
FlatFee: &billing.FlatFeeLine{

PerUnitAmount: alpacadecimal.NewFromFloat(10),
Quantity: alpacadecimal.NewFromFloat(1),
PaymentTerm: productcatalog.InArrearsPaymentTerm,
Category: billing.FlatFeeCategoryRegular,
},
})
}),
)
return nil
},
})
Expand Down
Loading
Loading