1 import { ITaxModuleService } from "@medusajs/types"
2
3 export const setupTaxStructure = async (service: ITaxModuleService) => {
4 // Setup for this specific test
5 //
6 // Using the following structure to setup tests.
7 // US - default 2%
8 // - Region: CA - default 5%
9 // - Override: Reduced rate (for 3 product ids): 3%
10 // - Override: Reduced rate (for product type): 1%
11 // - Region: NY - default: 6%
12 // - Region: FL - default: 4%
13 //
14 // Denmark - default 25%
15 //
16 // Germany - default 19%
17 // - Override: Reduced Rate (for product type) - 7%
18 //
19 // Canada - default 5%
20 // - Override: Reduced rate (for product id) - 3%
21 // - Override: Reduced rate (for product type) - 3.5%
22 // - Region: QC - default 2%
23 // - Override: Reduced rate (for same product type as country reduced rate): 1%
24 // - Region: BC - default 2%
25 //
26 const [us, dk, de, ca] = await service.createTaxRegions([
27 {
28 country_code: "US",
29 provider_id: "tp_system",
30 default_tax_rate: { name: "US Default Rate", rate: 2, code: "US_DEF" },
31 },
32 {
33 country_code: "DK",
34 provider_id: "tp_system",
35 default_tax_rate: {
36 name: "Denmark Default Rate",
37 rate: 25,
38 code: "DK_DEF",
39 },
40 },
41 {
42 country_code: "DE",
43 provider_id: "tp_system",
44 default_tax_rate: {
45 code: "DE19",
46 name: "Germany Default Rate",
47 rate: 19,
48 },
49 },
50 {
51 country_code: "CA",
52 provider_id: "tp_system",
53 default_tax_rate: {
54 name: "Canada Default Rate",
55 rate: 5,
56 code: "CA_DEF",
57 },
58 },
59 ])
60
61 // Create province regions within the US
62 const [cal, ny, fl, qc, bc] = await service.createTaxRegions([
63 {
64 country_code: "US",
65 province_code: "CA",
66 parent_id: us.id,
67 default_tax_rate: {
68 rate: 5,
69 name: "CA Default Rate",
70 code: "CADEFAULT",
71 },
72 },
73 {
74 country_code: "US",
75 province_code: "NY",
76 parent_id: us.id,
77 default_tax_rate: {
78 rate: 6,
79 name: "NY Default Rate",
80 code: "NYDEFAULT",
81 },
82 },
83 {
84 country_code: "US",
85 province_code: "FL",
86 parent_id: us.id,
87 default_tax_rate: {
88 rate: 4,
89 name: "FL Default Rate",
90 code: "FLDEFAULT",
91 },
92 },
93 {
94 country_code: "CA",
95 province_code: "QC",
96 parent_id: ca.id,
97 default_tax_rate: {
98 rate: 2,
99 name: "QC Default Rate",
100 code: "QCDEFAULT",
101 },
102 },
103 {
104 country_code: "CA",
105 province_code: "BC",
106 parent_id: ca.id,
107 default_tax_rate: {
108 rate: 2,
109 name: "BC Default Rate",
110 code: "BCDEFAULT",
111 },
112 },
113 ])
114
115 const [calProd, calType, deType, canProd, canType, qcType] =
116 await service.createTaxRates([
117 {
118 tax_region_id: cal.id,
119 name: "CA Reduced Rate for Products",
120 rate: 3,
121 code: "CAREDUCE_PROD",
122 },
123 {
124 tax_region_id: cal.id,
125 name: "CA Reduced Rate for Product Type",
126 rate: 1,
127 code: "CAREDUCE_TYPE",
128 },
129 {
130 tax_region_id: de.id,
131 name: "Germany Reduced Rate for Product Type",
132 rate: 7,
133 code: "DEREDUCE_TYPE",
134 },
135 {
136 tax_region_id: ca.id,
137 name: "Canada Reduced Rate for Product",
138 rate: 3,
139 code: "CAREDUCE_PROD_CA",
140 },
141 {
142 tax_region_id: ca.id,
143 name: "Canada Reduced Rate for Product Type",
144 rate: 3.5,
145 code: "CAREDUCE_TYPE_CA",
146 },
147 {
148 tax_region_id: qc.id,
149 name: "QC Reduced Rate for Product Type",
150 rate: 1,
151 code: "QCREDUCE_TYPE",
152 },
153 ])
154
155 // Create tax rate rules for specific products and product types
156 await service.createTaxRateRules([
157 {
158 reference: "product",
159 reference_id: "product_id_1",
160 tax_rate_id: calProd.id,
161 },
162 {
163 reference: "product",
164 reference_id: "product_id_2",
165 tax_rate_id: calProd.id,
166 },
167 {
168 reference: "product",
169 reference_id: "product_id_3",
170 tax_rate_id: calProd.id,
171 },
172 {
173 reference: "product_type",
174 reference_id: "product_type_id_1",
175 tax_rate_id: calType.id,
176 },
177 {
178 reference: "product_type",
179 reference_id: "product_type_id_2",
180 tax_rate_id: deType.id,
181 },
182 {
183 reference: "product",
184 reference_id: "product_id_4",
185 tax_rate_id: canProd.id,
186 },
187 {
188 reference: "product_type",
189 reference_id: "product_type_id_3",
190 tax_rate_id: canType.id,
191 },
192 {
193 reference: "product_type",
194 reference_id: "product_type_id_3",
195 tax_rate_id: qcType.id,
196 },
197 ])
198
199 return {
200 us: {
201 country: us,
202 children: {
203 cal: {
204 province: cal,
205 overrides: {
206 calProd,
207 calType,
208 },
209 },
210 ny: {
211 province: ny,
212 overrides: {},
213 },
214 fl: {
215 province: fl,
216 overrides: {},
217 },
218 },
219 overrides: {},
220 },
221 dk: {
222 country: dk,
223 children: {},
224 overrides: {},
225 },
226 de: {
227 country: de,
228 children: {},
229 overrides: {
230 deType,
231 },
232 },
233 ca: {
234 country: ca,
235 children: {
236 qc: {
237 province: qc,
238 overrides: {
239 qcType,
240 },
241 },
242 bc: {
243 province: bc,
244 overrides: {},
245 },
246 },
247 overrides: {
248 canProd,
249 canType,
250 },
251 },
252 }
253 }