File size: 2,709 Bytes
cee2387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/dialect"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
	"entgo.io/ent/schema/mixin"

	"github.com/openmeterio/openmeter/openmeter/productcatalog"
	"github.com/openmeterio/openmeter/openmeter/taxcode"
	"github.com/openmeterio/openmeter/pkg/framework/entutils"
)

// Tax code stores information about an entity's tax code
type TaxCode struct {
	ent.Schema
}

func (TaxCode) Mixin() []ent.Mixin {
	return []ent.Mixin{
		entutils.UniqueResourceMixin{},
		entutils.AnnotationsMixin{},
	}
}

func (TaxCode) Fields() []ent.Field {
	return []ent.Field{
		field.String("app_mappings").
			GoType(&taxcode.TaxCodeAppMappings{}).
			ValueScanner(TaxCodeAppMappingsValueScanner).
			SchemaType(map[string]string{
				dialect.Postgres: "jsonb",
			}).
			Optional().
			Nillable(),
	}
}

func (TaxCode) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("namespace", "key").
			Annotations(
				entsql.IndexWhere("deleted_at IS NULL"),
			).
			Unique(),
	}
}

func (TaxCode) Edges() []ent.Edge {
	return []ent.Edge{
		edge.To("billing_workflow_configs", BillingWorkflowConfig.Type),
		edge.To("billing_customer_overrides", BillingCustomerOverride.Type),
		edge.To("billing_invoice_lines", BillingInvoiceLine.Type),
		edge.To("subscription_items", SubscriptionItem.Type),
		edge.To("plan_rate_cards", PlanRateCard.Type),
		edge.To("addon_rate_cards", AddonRateCard.Type),
		edge.To("charge_flat_fees", ChargeFlatFee.Type),
		edge.To("charge_flat_fee_overrides", ChargeFlatFeeOverride.Type),
		edge.To("charge_usage_based", ChargeUsageBased.Type),
		edge.To("charge_usage_based_overrides", ChargeUsageBasedOverride.Type),
		edge.To("charge_credit_purchases", ChargeCreditPurchase.Type),
		edge.To("organization_default_invoicing", OrganizationDefaultTaxCodes.Type).
			StorageKey(edge.Symbol("org_dtc_invoicing_tax_code_fk")),
		edge.To("organization_default_credit_grant", OrganizationDefaultTaxCodes.Type).
			StorageKey(edge.Symbol("org_dtc_credit_grant_tax_code_fk")),
	}
}

var TaxCodeAppMappingsValueScanner = entutils.JSONStringValueScanner[*taxcode.TaxCodeAppMappings]()

// TaxMixin adds tax_code_id and tax_behavior fields to a schema.
type TaxMixin struct {
	mixin.Schema
}

func (TaxMixin) Fields() []ent.Field {
	return []ent.Field{
		field.String("tax_code_id").
			Optional().
			Nillable().
			SchemaType(map[string]string{
				dialect.Postgres: "char(26)",
			}),
		field.Enum("tax_behavior").
			GoType(productcatalog.TaxBehavior("")).
			Optional().
			Nillable(),
	}
}

func (TaxMixin) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("tax_code_id"),
	}
}