Skip to content

Commit

Permalink
feat: implement period value calculation of the invoice
Browse files Browse the repository at this point in the history
We are calculating the period value of the invoice based on the line items
min/max period values.
  • Loading branch information
turip committed Jan 21, 2025
1 parent e19abec commit 1f3da33
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions openmeter/billing/service/invoicecalc/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
var InvoiceCalculations = []Calculation{
DraftUntilIfMissing,
RecalculateDetailedLinesAndTotals,
CalculateInvoicePeriod,
}

type Calculation func(*billing.Invoice, CalculatorDependencies) error
Expand Down
36 changes: 36 additions & 0 deletions openmeter/billing/service/invoicecalc/period.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package invoicecalc

import "github.com/openmeterio/openmeter/openmeter/billing"

// CalculateInvoicePeriod calculates the period of the invoice based on the lines.
func CalculateInvoicePeriod(invoice *billing.Invoice, deps CalculatorDependencies) error {
var period *billing.Period

for _, line := range invoice.Lines.OrEmpty() {
if line.DeletedAt != nil || line.Status != billing.InvoiceLineStatusValid {
continue
}

if period == nil {
period = &billing.Period{
Start: line.Period.Start,
End: line.Period.End,
}
continue
}

if line.Period.Start.Before(period.Start) {
period.Start = line.Period.Start
}

if line.Period.End.After(period.End) {
period.End = line.Period.End
}
}

if period != nil {
invoice.Period = period
}

return nil
}

0 comments on commit 1f3da33

Please sign in to comment.