-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(billing): add collectionAt to invoices (#2175)
- Loading branch information
1 parent
28c3129
commit 602c35a
Showing
19 changed files
with
480 additions
and
23 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
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,62 @@ | ||
package billingservice | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/samber/lo" | ||
|
||
"github.com/openmeterio/openmeter/openmeter/billing" | ||
) | ||
|
||
// UpdateInvoiceCollectionAt updates the collectionAt attribute of the invoice with gathering type | ||
// using the customers collection configuration. It returns true if the attribute has been updated. | ||
// The collectionAt is calculated by adding the collection interval (from CollectionConfig) to the earliest invoicedAt | ||
// timestamp of the invoice lines on the gathering invoice. | ||
func UpdateInvoiceCollectionAt(invoice *billing.Invoice, collection billing.CollectionConfig) bool { | ||
if invoice == nil || invoice.Status != billing.InvoiceStatusGathering { | ||
return false | ||
} | ||
|
||
var invoiceAt time.Time | ||
|
||
// Find the invoice lint with the earliest invoiceAt attribute | ||
invoice.Lines.ForEach(func(v []*billing.Line) { | ||
for _, line := range v { | ||
if line == nil || line.Status != billing.InvoiceLineStatusValid { | ||
continue | ||
} | ||
|
||
if line.DeletedAt != nil { | ||
continue | ||
} | ||
|
||
if invoiceAt.IsZero() { | ||
invoiceAt = line.InvoiceAt | ||
continue | ||
} | ||
|
||
if line.InvoiceAt.Before(invoiceAt) { | ||
invoiceAt = line.InvoiceAt | ||
} | ||
} | ||
}) | ||
|
||
if invoiceAt.IsZero() { | ||
return false | ||
} | ||
|
||
interval, ok := collection.Interval.Duration() | ||
if !ok { | ||
return false | ||
} | ||
|
||
collectionAt := invoiceAt.Add(interval) | ||
|
||
if lo.FromPtr(invoice.CollectionAt).Equal(collectionAt) { | ||
return false | ||
} | ||
|
||
invoice.CollectionAt = &collectionAt | ||
|
||
return true | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.