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