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:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/utils": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(utils): avoid inflating refundable_total for tax inclusive pricing
|
||||||
@@ -1267,4 +1267,41 @@ describe("Total calculation", function () {
|
|||||||
total: 0,
|
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)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -94,6 +94,7 @@ function setRefundableTotal(
|
|||||||
)
|
)
|
||||||
|
|
||||||
const taxTotal = calculateTaxTotal({
|
const taxTotal = calculateTaxTotal({
|
||||||
|
isTaxInclusive: item.is_tax_inclusive,
|
||||||
taxLines: item.tax_lines || [],
|
taxLines: item.tax_lines || [],
|
||||||
taxableAmount: refundableSubTotal,
|
taxableAmount: refundableSubTotal,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,15 +3,20 @@ import { BigNumber } from "../big-number"
|
|||||||
import { MathBN } from "../math"
|
import { MathBN } from "../math"
|
||||||
|
|
||||||
export function calculateTaxTotal({
|
export function calculateTaxTotal({
|
||||||
|
isTaxInclusive = false,
|
||||||
taxLines,
|
taxLines,
|
||||||
taxableAmount,
|
taxableAmount,
|
||||||
setTotalField,
|
setTotalField,
|
||||||
}: {
|
}: {
|
||||||
|
isTaxInclusive?: boolean
|
||||||
taxLines: Pick<TaxLineDTO, "rate">[]
|
taxLines: Pick<TaxLineDTO, "rate">[]
|
||||||
taxableAmount: BigNumberInput
|
taxableAmount: BigNumberInput
|
||||||
setTotalField?: string
|
setTotalField?: string
|
||||||
}) {
|
}) {
|
||||||
let taxTotal = MathBN.convert(0)
|
let taxTotal = MathBN.convert(0)
|
||||||
|
if (isTaxInclusive) {
|
||||||
|
return taxTotal
|
||||||
|
}
|
||||||
|
|
||||||
for (const taxLine of taxLines) {
|
for (const taxLine of taxLines) {
|
||||||
const rate = MathBN.div(taxLine.rate, 100)
|
const rate = MathBN.div(taxLine.rate, 100)
|
||||||
|
|||||||
Reference in New Issue
Block a user