Skip to content

Commit 9a70063

Browse files
authored
fix: correctly set tax rate on custom line items (#329)
1 parent 283488d commit 9a70063

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

.changeset/twelve-icons-take.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@labdigital/commercetools-mock": patch
3+
---
4+
5+
fix: implement tax rate on custom line items

src/repositories/cart/helpers.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ export const createCustomLineItemFromDraft = (
150150
)
151151
: undefined;
152152

153-
// Get the tax category to calculate taxed price
154153
let taxCategory: TaxCategory | undefined = undefined;
155154
if (taxCategoryRef) {
156155
try {
@@ -167,7 +166,6 @@ export const createCustomLineItemFromDraft = (
167166
centAmount: (draft.money.centAmount ?? 0) * quantity,
168167
});
169168

170-
// Calculate taxed price if tax category is available
171169
const taxedPrice = taxCategory
172170
? calculateTaxedPrice(
173171
totalPrice.centAmount,
@@ -177,6 +175,12 @@ export const createCustomLineItemFromDraft = (
177175
)
178176
: undefined;
179177

178+
const taxRate = taxCategory
179+
? taxCategory.rates.find(
180+
(rate) => !rate.country || rate.country === country,
181+
) || taxCategory.rates[0]
182+
: undefined;
183+
180184
return {
181185
id: uuidv4(),
182186
key: draft.key,
@@ -186,6 +190,7 @@ export const createCustomLineItemFromDraft = (
186190
quantity: draft.quantity ?? 1,
187191
state: [],
188192
taxCategory: taxCategoryRef,
193+
taxRate,
189194
taxedPrice,
190195
custom: createCustomFields(draft.custom, projectKey, storage),
191196
discountedPricePerQuantity: [],

src/repositories/cart/index.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,5 +293,10 @@ describe("Cart repository", () => {
293293
expect(customLineItem.taxedPrice?.totalTax?.centAmount).toBe(210);
294294
expect(customLineItem.taxedPrice?.taxPortions).toHaveLength(1);
295295
expect(customLineItem.taxedPrice?.taxPortions[0].rate).toBe(0.21);
296+
expect(customLineItem.taxRate).toBeDefined();
297+
expect(customLineItem.taxRate?.amount).toBe(0.21);
298+
expect(customLineItem.taxRate?.name).toBe("Standard VAT");
299+
expect(customLineItem.taxRate?.includedInPrice).toBe(false);
300+
expect(customLineItem.taxRate?.country).toBe("NL");
296301
});
297302
});

src/services/cart.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,8 @@ describe("Cart Update Actions", () => {
13991399
expect(customLineItem.totalPrice.centAmount).toBe(1000);
14001400
expect(customLineItem.taxCategory.id).toBe(taxCategory.id);
14011401
expect(customLineItem.taxedPrice).toBeDefined();
1402+
expect(customLineItem.taxRate).toBeDefined();
1403+
expect(customLineItem.taxRate.amount).toBe(0.21);
14021404
expect(customLineItem.id).toBeDefined();
14031405
expect(customLineItem.custom).toBeDefined();
14041406
expect(customLineItem.custom.fields.description).toBe(

0 commit comments

Comments
 (0)