Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make tax codes and inclusion configurable in billing profiles #2156

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
1,364 changes: 682 additions & 682 deletions api/api.gen.go

Large diffs are not rendered by default.

1,388 changes: 694 additions & 694 deletions api/client/go/client.gen.go

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions api/client/javascript/src/client/schemas.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2223,10 +2223,10 @@ export interface components {
* @default false
*/
progressiveBilling?: boolean
/** @description Default tax behavior for the invoice.
/** @description Default tax configuration to apply to the invoices.
*
* Each line can override the tax behavior. If not set, the provider's defaults are used. */
taxBehavior?: components['schemas']['TaxBehavior']
defaultTaxConfig?: components['schemas']['TaxConfig']
}
/**
* Workflow payment settings
Expand Down Expand Up @@ -4751,10 +4751,10 @@ export interface components {
* @example P1D
*/
dueAfter?: string
/** @description Default tax behavior for the invoice.
/** @description Default tax configuration to apply to the invoices.
*
* Each line can override the tax behavior. If not set, the provider's defaults are used. */
taxBehavior?: components['schemas']['TaxBehavior']
defaultTaxConfig?: components['schemas']['TaxConfig']
}
/** @description InvoiceWorkflowReplaceUpdate represents the update model for an invoice workflow.
*
Expand Down
12 changes: 6 additions & 6 deletions api/openapi.cloud.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8720,11 +8720,11 @@ components:
type: boolean
description: Should progressive billing be allowed for this workflow?
default: false
taxBehavior:
defaultTaxConfig:
allOf:
- $ref: '#/components/schemas/TaxBehavior'
- $ref: '#/components/schemas/TaxConfig'
description: |-
Default tax behavior for the invoice.
Default tax configuration to apply to the invoices.

Each line can override the tax behavior. If not set, the provider's defaults are used.
description: BillingWorkflowInvoicingSettings represents the invoice settings for a billing workflow
Expand Down Expand Up @@ -12332,11 +12332,11 @@ components:
description: The period after which the invoice is due.
example: P1D
default: P7D
taxBehavior:
defaultTaxConfig:
allOf:
- $ref: '#/components/schemas/TaxBehavior'
- $ref: '#/components/schemas/TaxConfig'
description: |-
Default tax behavior for the invoice.
Default tax configuration to apply to the invoices.

Each line can override the tax behavior. If not set, the provider's defaults are used.
description: InvoiceWorkflowInvoicingSettingsReplaceUpdate represents the update model for the invoicing settings of an invoice workflow.
Expand Down
12 changes: 6 additions & 6 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8407,11 +8407,11 @@ components:
type: boolean
description: Should progressive billing be allowed for this workflow?
default: false
taxBehavior:
defaultTaxConfig:
allOf:
- $ref: '#/components/schemas/TaxBehavior'
- $ref: '#/components/schemas/TaxConfig'
description: |-
Default tax behavior for the invoice.
Default tax configuration to apply to the invoices.

Each line can override the tax behavior. If not set, the provider's defaults are used.
description: BillingWorkflowInvoicingSettings represents the invoice settings for a billing workflow
Expand Down Expand Up @@ -12098,11 +12098,11 @@ components:
description: The period after which the invoice is due.
example: P1D
default: P7D
taxBehavior:
defaultTaxConfig:
allOf:
- $ref: '#/components/schemas/TaxBehavior'
- $ref: '#/components/schemas/TaxConfig'
description: |-
Default tax behavior for the invoice.
Default tax configuration to apply to the invoices.

Each line can override the tax behavior. If not set, the provider's defaults are used.
description: InvoiceWorkflowInvoicingSettingsReplaceUpdate represents the update model for the invoicing settings of an invoice workflow.
Expand Down
4 changes: 2 additions & 2 deletions api/spec/src/billing/profile.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,12 @@ model BillingWorkflowInvoicingSettings {
progressiveBilling?: boolean = false;

/**
* Default tax behavior for the invoice.
* Default tax configuration to apply to the invoices.
*
* Each line can override the tax behavior. If not set, the provider's defaults are used.
*/
@visibility(Lifecycle.Read, Lifecycle.Create, Lifecycle.Update)
taxBehavior?: OpenMeter.ProductCatalog.TaxBehavior;
defaultTaxConfig?: OpenMeter.ProductCatalog.TaxConfig;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions openmeter/billing/adapter/customeroverride.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (a *adapter) CreateCustomerOverride(ctx context.Context, input billing.Crea
SetNillableInvoiceDueAfter(input.Invoicing.DueAfter.ISOStringPtrOrNil()).
SetNillableInvoiceCollectionMethod(input.Payment.CollectionMethod).
SetNillableInvoiceProgressiveBilling(input.Invoicing.ProgressiveBilling).
SetNillableInvoiceTaxBehavior(input.Invoicing.TaxBehavior).
SetNillableInvoiceDefaultTaxConfig(input.Invoicing.DefaultTaxConfig).
Save(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -73,7 +73,7 @@ func (a *adapter) UpdateCustomerOverride(ctx context.Context, input billing.Upda
SetOrClearInvoiceDueAfter(input.Invoicing.DueAfter.ISOStringPtrOrNil()).
SetOrClearInvoiceCollectionMethod(input.Payment.CollectionMethod).
SetOrClearInvoiceProgressiveBilling(input.Invoicing.ProgressiveBilling).
SetOrClearInvoiceTaxBehavior(input.Invoicing.TaxBehavior)
SetOrClearInvoiceDefaultTaxConfig(input.Invoicing.DefaultTaxConfig)

if input.ResetDeletedAt {
update = update.ClearDeletedAt()
Expand Down Expand Up @@ -265,7 +265,7 @@ func mapCustomerOverrideFromDB(dbOverride *db.BillingCustomerOverride) (*billing
DraftPeriod: draftPeriod,
DueAfter: dueAfter,
ProgressiveBilling: dbOverride.InvoiceProgressiveBilling,
TaxBehavior: dbOverride.InvoiceTaxBehavior,
DefaultTaxConfig: lo.EmptyableToPtr(dbOverride.InvoiceDefaultTaxConfig),
},

Payment: billing.PaymentOverrideConfig{
Expand Down
8 changes: 5 additions & 3 deletions openmeter/billing/adapter/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"github.com/samber/lo"

"github.com/openmeterio/openmeter/api"
appentitybase "github.com/openmeterio/openmeter/openmeter/app/entity/base"
"github.com/openmeterio/openmeter/openmeter/billing"
Expand Down Expand Up @@ -82,7 +84,7 @@ func (a *adapter) createWorkflowConfig(ctx context.Context, ns string, input bil
SetInvoiceDueAfter(input.Invoicing.DueAfter.ISOString()).
SetInvoiceCollectionMethod(input.Payment.CollectionMethod).
SetInvoiceProgressiveBilling(input.Invoicing.ProgressiveBilling).
SetNillableInvoiceTaxBehavior(input.Invoicing.TaxBehavior).
SetNillableInvoiceDefaultTaxSettings(input.Invoicing.DefaultTaxConfig).
Save(ctx)
}

Expand Down Expand Up @@ -313,7 +315,7 @@ func (a *adapter) updateWorkflowConfig(ctx context.Context, ns string, id string
SetInvoiceDueAfter(input.Invoicing.DueAfter.ISOString()).
SetInvoiceCollectionMethod(input.Payment.CollectionMethod).
SetInvoiceProgressiveBilling(input.Invoicing.ProgressiveBilling).
SetOrClearInvoiceTaxBehavior(input.Invoicing.TaxBehavior).
SetOrClearInvoiceDefaultTaxSettings(input.Invoicing.DefaultTaxConfig).
Save(ctx)
}

Expand Down Expand Up @@ -406,7 +408,7 @@ func mapWorkflowConfigFromDB(dbWC *db.BillingWorkflowConfig) (billing.WorkflowCo
DraftPeriod: draftPeriod,
DueAfter: dueAfter,
ProgressiveBilling: dbWC.InvoiceProgressiveBilling,
TaxBehavior: dbWC.InvoiceTaxBehavior,
DefaultTaxConfig: lo.EmptyableToPtr(dbWC.InvoiceDefaultTaxSettings),
},

Payment: billing.PaymentConfig{
Expand Down
16 changes: 8 additions & 8 deletions openmeter/billing/customeroverride.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ func (c *CollectionOverrideConfig) Validate() error {
}

type InvoicingOverrideConfig struct {
AutoAdvance *bool `json:"autoAdvance,omitempty"`
DraftPeriod *datex.Period `json:"draftPeriod,omitempty"`
DueAfter *datex.Period `json:"dueAfter,omitempty"`
ProgressiveBilling *bool `json:"progressiveBilling,omitempty"`
TaxBehavior *productcatalog.TaxBehavior `json:"taxBehavior,omitempty"`
AutoAdvance *bool `json:"autoAdvance,omitempty"`
DraftPeriod *datex.Period `json:"draftPeriod,omitempty"`
DueAfter *datex.Period `json:"dueAfter,omitempty"`
ProgressiveBilling *bool `json:"progressiveBilling,omitempty"`
DefaultTaxConfig *productcatalog.TaxConfig `json:"defaultTaxConfig,omitempty"`
}

func (c *InvoicingOverrideConfig) Validate() error {
Expand All @@ -97,9 +97,9 @@ func (c *InvoicingOverrideConfig) Validate() error {
return fmt.Errorf("draft period must be greater or equal to 0")
}

if c.TaxBehavior != nil {
if err := c.TaxBehavior.Validate(); err != nil {
return fmt.Errorf("invalid tax behavior: %s", *c.TaxBehavior)
if c.DefaultTaxConfig != nil {
if err := c.DefaultTaxConfig.Validate(); err != nil {
return fmt.Errorf("invalid default tax config: %w", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion openmeter/billing/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var DefaultWorkflowConfig = WorkflowConfig{
DraftPeriod: lo.Must(datex.ISOString("P1D").Parse()),
DueAfter: lo.Must(datex.ISOString("P1W").Parse()),
ProgressiveBilling: false,
TaxBehavior: nil,
DefaultTaxConfig: nil,
},
Payment: PaymentConfig{
CollectionMethod: CollectionMethodChargeAutomatically,
Expand Down
7 changes: 3 additions & 4 deletions openmeter/billing/httpdriver/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/openmeterio/openmeter/api"
appentitybase "github.com/openmeterio/openmeter/openmeter/app/entity/base"
"github.com/openmeterio/openmeter/openmeter/billing"
"github.com/openmeterio/openmeter/openmeter/productcatalog"
"github.com/openmeterio/openmeter/pkg/datex"
"github.com/openmeterio/openmeter/pkg/defaultx"
"github.com/openmeterio/openmeter/pkg/framework/commonhttp"
Expand Down Expand Up @@ -395,7 +394,7 @@ func fromAPIBillingWorkflow(i api.BillingWorkflow) (billing.WorkflowConfig, erro
DraftPeriod: draftPeriod,
DueAfter: dueAfter,
ProgressiveBilling: lo.FromPtrOr(i.Invoicing.ProgressiveBilling, def.Invoicing.ProgressiveBilling),
TaxBehavior: (*productcatalog.TaxBehavior)(i.Invoicing.TaxBehavior),
DefaultTaxConfig: mapTaxConfigToEntity(i.Invoicing.DefaultTaxConfig),
},

Payment: billing.PaymentConfig{
Expand Down Expand Up @@ -578,7 +577,7 @@ func mapWorkflowConfigToAPI(c billing.WorkflowConfig) api.BillingWorkflow {
DraftPeriod: lo.EmptyableToPtr(c.Invoicing.DraftPeriod.String()),
DueAfter: lo.EmptyableToPtr(c.Invoicing.DueAfter.String()),
ProgressiveBilling: lo.ToPtr(c.Invoicing.ProgressiveBilling),
TaxBehavior: (*api.TaxBehavior)(c.Invoicing.TaxBehavior),
DefaultTaxConfig: mapTaxConfigToAPI(c.Invoicing.DefaultTaxConfig),
},

Payment: &api.BillingWorkflowPaymentSettings{
Expand All @@ -601,7 +600,7 @@ func mapWorkflowConfigSettingsToAPI(c billing.WorkflowConfig) api.BillingWorkflo
DraftPeriod: lo.EmptyableToPtr(c.Invoicing.DraftPeriod.String()),
DueAfter: lo.EmptyableToPtr(c.Invoicing.DueAfter.String()),
ProgressiveBilling: lo.ToPtr(c.Invoicing.ProgressiveBilling),
TaxBehavior: (*api.TaxBehavior)(c.Invoicing.TaxBehavior),
DefaultTaxConfig: mapTaxConfigToAPI(c.Invoicing.DefaultTaxConfig),
},

Payment: &api.BillingWorkflowPaymentSettings{
Expand Down
14 changes: 3 additions & 11 deletions openmeter/billing/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/openmeterio/openmeter/api"
customerentity "github.com/openmeterio/openmeter/openmeter/customer/entity"
"github.com/openmeterio/openmeter/openmeter/productcatalog"
"github.com/openmeterio/openmeter/pkg/clock"
"github.com/openmeterio/openmeter/pkg/currencyx"
"github.com/openmeterio/openmeter/pkg/models"
Expand Down Expand Up @@ -397,21 +398,12 @@ func (i *Invoice) getLeafLines() []*Line {
// unless the line already has a tax behavior set.
func (i *Invoice) GetLeafLinesWithConsolidatedTaxBehavior() []*Line {
leafLines := i.getLeafLines()
if i.Workflow.Config.Invoicing.TaxBehavior == nil {
if i.Workflow.Config.Invoicing.DefaultTaxConfig == nil {
return leafLines
}

return lo.Map(leafLines, func(line *Line, _ int) *Line {
if line.TaxConfig == nil {
line.TaxConfig = &TaxConfig{
Behavior: i.Workflow.Config.Invoicing.TaxBehavior,
}
return line
}

if line.TaxConfig.Behavior == nil {
line.TaxConfig.Behavior = i.Workflow.Config.Invoicing.TaxBehavior
}
line.TaxConfig = productcatalog.MergeTaxConfigs(i.Workflow.Config.Invoicing.DefaultTaxConfig, line.TaxConfig)
return line
})
}
Expand Down
16 changes: 8 additions & 8 deletions openmeter/billing/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ func (c *CollectionConfig) Validate() error {

// InvoiceConfig groups fields related to invoice settings.
type InvoicingConfig struct {
AutoAdvance bool `json:"autoAdvance,omitempty"`
DraftPeriod datex.Period `json:"draftPeriod,omitempty"`
DueAfter datex.Period `json:"dueAfter,omitempty"`
ProgressiveBilling bool `json:"progressiveBilling,omitempty"`
TaxBehavior *productcatalog.TaxBehavior `json:"taxBehavior,omitempty"`
AutoAdvance bool `json:"autoAdvance,omitempty"`
DraftPeriod datex.Period `json:"draftPeriod,omitempty"`
DueAfter datex.Period `json:"dueAfter,omitempty"`
ProgressiveBilling bool `json:"progressiveBilling,omitempty"`
DefaultTaxConfig *productcatalog.TaxConfig `json:"defaultTaxConfig,omitempty"`
}

func (c *InvoicingConfig) Validate() error {
Expand All @@ -110,8 +110,8 @@ func (c *InvoicingConfig) Validate() error {
return fmt.Errorf("due after must be greater or equal to 0")
}

if c.TaxBehavior != nil {
if err := c.TaxBehavior.Validate(); err != nil {
if c.DefaultTaxConfig != nil {
if err := c.DefaultTaxConfig.Validate(); err != nil {
return fmt.Errorf("invalid tax behavior: %w", err)
}
}
Expand Down Expand Up @@ -285,7 +285,7 @@ func (p Profile) Merge(o *CustomerOverride) Profile {
DraftPeriod: lo.FromPtrOr(o.Invoicing.DraftPeriod, p.WorkflowConfig.Invoicing.DraftPeriod),
DueAfter: lo.FromPtrOr(o.Invoicing.DueAfter, p.WorkflowConfig.Invoicing.DueAfter),
ProgressiveBilling: lo.FromPtrOr(o.Invoicing.ProgressiveBilling, p.WorkflowConfig.Invoicing.ProgressiveBilling),
TaxBehavior: lo.CoalesceOrEmpty(o.Invoicing.TaxBehavior, p.WorkflowConfig.Invoicing.TaxBehavior),
DefaultTaxConfig: productcatalog.MergeTaxConfigs(o.Invoicing.DefaultTaxConfig, p.WorkflowConfig.Invoicing.DefaultTaxConfig),
}

p.WorkflowConfig.Payment = PaymentConfig{
Expand Down
28 changes: 15 additions & 13 deletions openmeter/ent/db/billingcustomeroverride.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading