-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement period value calculation of the invoice
We are calculating the period value of the invoice based on the line items min/max period values.
- Loading branch information
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |