Skip to content

Commit 4dbf46f

Browse files
authored
fix(utils): avoid inflating refundable_total for tax inclusive pricing (medusajs#14237)
* Prevent refundable_total inflation for tax inclusive item pricing * Add tests * Add changeset * Update changeset * Review changes
1 parent bff0142 commit 4dbf46f

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

.changeset/better-memes-move.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/utils": patch
3+
---
4+
5+
fix(utils): avoid inflating refundable_total for tax inclusive pricing

packages/core/utils/src/totals/__tests__/totals.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,4 +1267,41 @@ describe("Total calculation", function () {
12671267
total: 0,
12681268
})
12691269
})
1270+
1271+
it("should calculate refundable_total for tax inclusive items without inflating tax", function () {
1272+
const cartTaxInclusive = {
1273+
items: [
1274+
{
1275+
unit_price: 100,
1276+
quantity: 2,
1277+
is_tax_inclusive: true,
1278+
detail: {
1279+
fulfilled_quantity: 2,
1280+
shipped_quantity: 2,
1281+
return_requested_quantity: 0,
1282+
return_received_quantity: 0,
1283+
return_dismissed_quantity: 0,
1284+
written_off_quantity: 0,
1285+
},
1286+
tax_lines: [
1287+
{
1288+
rate: 20,
1289+
},
1290+
],
1291+
adjustments: [
1292+
{
1293+
amount: 10,
1294+
},
1295+
],
1296+
},
1297+
],
1298+
}
1299+
1300+
const serializedTaxInclusive = JSON.parse(
1301+
JSON.stringify(decorateCartTotals(cartTaxInclusive))
1302+
)
1303+
1304+
expect(serializedTaxInclusive.items[0].refundable_total).toBe(188)
1305+
expect(serializedTaxInclusive.items[0].refundable_total_per_unit).toBe(94)
1306+
})
12701307
})

packages/core/utils/src/totals/line-item/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ function setRefundableTotal(
9494
)
9595

9696
const taxTotal = calculateTaxTotal({
97+
isTaxInclusive: item.is_tax_inclusive,
9798
taxLines: item.tax_lines || [],
9899
taxableAmount: refundableSubTotal,
99100
})

packages/core/utils/src/totals/tax/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ import { BigNumber } from "../big-number"
33
import { MathBN } from "../math"
44

55
export function calculateTaxTotal({
6+
isTaxInclusive = false,
67
taxLines,
78
taxableAmount,
89
setTotalField,
910
}: {
11+
isTaxInclusive?: boolean
1012
taxLines: Pick<TaxLineDTO, "rate">[]
1113
taxableAmount: BigNumberInput
1214
setTotalField?: string
1315
}) {
1416
let taxTotal = MathBN.convert(0)
17+
if (isTaxInclusive) {
18+
return taxTotal
19+
}
1520

1621
for (const taxLine of taxLines) {
1722
const rate = MathBN.div(taxLine.rate, 100)

0 commit comments

Comments
 (0)