fix(utils): avoid inflating refundable_total for tax inclusive pricing (#14237)

* Prevent refundable_total inflation for tax inclusive item pricing

* Add tests

* Add changeset

* Update changeset

* Review changes
This commit is contained in:
Nicolas Gorga
2025-12-09 15:28:22 -03:00
committed by GitHub
parent bff0142e7a
commit 4dbf46f2cb
4 changed files with 48 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/utils": patch
---
fix(utils): avoid inflating refundable_total for tax inclusive pricing

View File

@@ -1267,4 +1267,41 @@ describe("Total calculation", function () {
total: 0,
})
})
it("should calculate refundable_total for tax inclusive items without inflating tax", function () {
const cartTaxInclusive = {
items: [
{
unit_price: 100,
quantity: 2,
is_tax_inclusive: true,
detail: {
fulfilled_quantity: 2,
shipped_quantity: 2,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
tax_lines: [
{
rate: 20,
},
],
adjustments: [
{
amount: 10,
},
],
},
],
}
const serializedTaxInclusive = JSON.parse(
JSON.stringify(decorateCartTotals(cartTaxInclusive))
)
expect(serializedTaxInclusive.items[0].refundable_total).toBe(188)
expect(serializedTaxInclusive.items[0].refundable_total_per_unit).toBe(94)
})
})

View File

@@ -94,6 +94,7 @@ function setRefundableTotal(
)
const taxTotal = calculateTaxTotal({
isTaxInclusive: item.is_tax_inclusive,
taxLines: item.tax_lines || [],
taxableAmount: refundableSubTotal,
})

View File

@@ -3,15 +3,20 @@ import { BigNumber } from "../big-number"
import { MathBN } from "../math"
export function calculateTaxTotal({
isTaxInclusive = false,
taxLines,
taxableAmount,
setTotalField,
}: {
isTaxInclusive?: boolean
taxLines: Pick<TaxLineDTO, "rate">[]
taxableAmount: BigNumberInput
setTotalField?: string
}) {
let taxTotal = MathBN.convert(0)
if (isTaxInclusive) {
return taxTotal
}
for (const taxLine of taxLines) {
const rate = MathBN.div(taxLine.rate, 100)