This fixes the discount_ calculation logic and promotion tax inclusiveness calculation (#12960)

* This fixes the discount_ calculation logic

* This fixes the adjustment to be handled as a subtotal value in every calculation and applies the tax inclusive logic on the promotion value itself

* Added some testcases and revoked some changes to improve testing output

* Fixed a test case based on feedback

* Corrected promotion/admin test cases

* Corrected cart/store test case

* Improved cart/store test cases for more robust promotion testing considering tax inclusion flags

* Remove unnessary changes as adjustments now automatically are subtotals and therefore the tax inclusive flag does not need to be applied again

* Remove adjustments->is_tax_inclusive usage everywhere

* Migration script to remove is_tax_inclusive in cart line item adjustment

* Forgot to adjust one more testcase

* Corrections based on fPolic feedback

* Refactored PR to consider feedback from oliver

* Added more testcases for promotion in cart

---------

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
scherddel
2025-07-31 13:27:43 +02:00
committed by GitHub
parent 75320e744f
commit 1bdf602f1c
11 changed files with 1076 additions and 119 deletions

View File

@@ -124,8 +124,8 @@ describe("Total calculation", function () {
adjustments: [
{
amount: 10,
total: 11,
subtotal: 10,
total: 11,
},
],
subtotal: 100,
@@ -244,16 +244,16 @@ describe("Total calculation", function () {
adjustments: [
{
amount: 9,
subtotal: 8.181818181818182,
total: 9,
subtotal: 9,
total: 9.9,
},
],
subtotal: 90,
total: 89.1,
original_total: 99,
discount_total: 9,
discount_total: 9.9,
discount_subtotal: 9,
discount_tax_total: 0.8181818181818182,
discount_tax_total: 0.9,
tax_total: 8.1,
original_tax_total: 9,
},
@@ -298,17 +298,17 @@ describe("Total calculation", function () {
adjustments: [
{
amount: 9,
subtotal: 8.181818181818182,
total: 9,
subtotal: 9,
total: 9.9,
},
],
amount: 99,
subtotal: 90,
total: 89.1,
original_total: 99,
discount_total: 9,
discount_total: 9.9,
discount_subtotal: 9,
discount_tax_total: 0.8181818181818182,
discount_tax_total: 0.9,
tax_total: 8.1,
original_tax_total: 9,
},
@@ -342,9 +342,9 @@ describe("Total calculation", function () {
total: 191.4,
subtotal: 198,
tax_total: 17.4,
discount_total: 24.6,
discount_total: 26.4,
discount_subtotal: 24,
discount_tax_total: 2.2363636363636363,
discount_tax_total: 2.4,
original_total: 217.8,
original_tax_total: 19.8,
item_total: 95.7,
@@ -562,7 +562,7 @@ describe("Total calculation", function () {
* TAX INCLUSIVE CART
*
* Total price -> 120 tax inclusive
* Fixed discount -> 10 tax inclusive
* Fixed discount -> 10 tax inclusive total (which results in a subtotal of 8.33 of the discount)
* Tax rate -> 20%
*/
@@ -574,8 +574,7 @@ describe("Total calculation", function () {
is_tax_inclusive: true,
adjustments: [
{
amount: 10,
is_tax_inclusive: true,
amount: 8.333333333333334,
},
],
tax_lines: [
@@ -615,8 +614,7 @@ describe("Total calculation", function () {
],
adjustments: [
{
is_tax_inclusive: true,
amount: 10, // <- amount is tax inclusive so it's equal to total
amount: 8.333333333333334,
subtotal: 8.333333333333334,
total: 10,
},
@@ -625,7 +623,7 @@ describe("Total calculation", function () {
],
subtotal: 100,
tax_total: 18.333333333333332,
total: 110, // total is 120 - 10 tax inclusive discount
total: 110,
original_item_subtotal: 100,
original_item_tax_total: 20,
@@ -882,4 +880,338 @@ describe("Total calculation", function () {
credit_line_total: 40,
})
})
it("should calculate carts with items + taxes + adjustments", function () {
const cart = {
items: [
{
unit_price: 119,
quantity: 1,
tax_lines: [
{
rate: 19,
},
],
adjustments: [
{
amount: 119,
},
],
},
],
}
const serialized = JSON.parse(JSON.stringify(decorateCartTotals(cart)))
expect(serialized).toEqual({
items: [
{
unit_price: 119,
quantity: 1,
tax_lines: [
{
rate: 19,
subtotal: 22.61,
},
],
adjustments: [
{
amount: 119,
subtotal: 119,
total: 141.61,
},
],
subtotal: 119,
total: 0,
original_total: 141.61,
discount_total: 141.61,
discount_subtotal: 119,
discount_tax_total: 22.61,
tax_total: 0,
original_tax_total: 22.61,
},
],
total: 0,
subtotal: 119,
tax_total: 0,
discount_total: 141.61,
discount_subtotal: 119,
discount_tax_total: 22.61,
original_total: 141.61,
original_tax_total: 22.61,
item_total: 0,
item_subtotal: 119,
item_tax_total: 0,
original_item_total: 141.61,
original_item_subtotal: 119,
original_item_tax_total: 22.61,
credit_line_subtotal: 0,
credit_line_tax_total: 0,
credit_line_total: 0,
})
})
it("should calculate carts with items + taxes with is_tax_inclusive", function () {
const cartWithTax = {
items: [
{
unit_price: 119,
quantity: 1,
is_tax_inclusive: true,
tax_lines: [
{
rate: 19,
},
],
},
],
}
const cartWithoutTax = {
items: [
{
unit_price: 119,
quantity: 1,
is_tax_inclusive: false,
tax_lines: [
{
rate: 19,
},
],
},
],
}
const cartMixed = {
items: [...cartWithTax.items, ...cartWithoutTax.items],
}
const serializedWith = JSON.parse(
JSON.stringify(decorateCartTotals(cartWithTax))
)
const serializedWithout = JSON.parse(
JSON.stringify(decorateCartTotals(cartWithoutTax))
)
const serializedMixed = JSON.parse(
JSON.stringify(decorateCartTotals(cartMixed))
)
expect(serializedWith).toEqual({
credit_line_subtotal: 0,
credit_line_tax_total: 0,
credit_line_total: 0,
discount_subtotal: 0,
discount_tax_total: 0,
discount_total: 0,
item_subtotal: 100,
item_tax_total: 19,
item_total: 119,
items: [
{
discount_subtotal: 0,
discount_tax_total: 0,
discount_total: 0,
is_tax_inclusive: true,
original_tax_total: 19,
original_total: 119,
quantity: 1,
subtotal: 100,
tax_lines: [
{
rate: 19,
subtotal: 19,
total: 19,
},
],
tax_total: 19,
total: 119,
unit_price: 119,
},
],
original_item_subtotal: 100,
original_item_tax_total: 19,
original_item_total: 119,
original_tax_total: 19,
original_total: 119,
subtotal: 100,
tax_total: 19,
total: 119,
})
expect(serializedWithout).toEqual({
credit_line_subtotal: 0,
credit_line_tax_total: 0,
credit_line_total: 0,
discount_subtotal: 0,
discount_tax_total: 0,
discount_total: 0,
item_subtotal: 119,
item_tax_total: 22.61,
item_total: 141.61,
items: [
{
discount_subtotal: 0,
discount_tax_total: 0,
discount_total: 0,
is_tax_inclusive: false,
original_tax_total: 22.61,
original_total: 141.61,
quantity: 1,
subtotal: 119,
tax_lines: [
{
rate: 19,
subtotal: 22.61,
total: 22.61,
},
],
tax_total: 22.61,
total: 141.61,
unit_price: 119,
},
],
original_item_subtotal: 119,
original_item_tax_total: 22.61,
original_item_total: 141.61,
original_tax_total: 22.61,
original_total: 141.61,
subtotal: 119,
tax_total: 22.61,
total: 141.61,
})
expect(serializedMixed).toEqual({
credit_line_subtotal: 0,
credit_line_tax_total: 0,
credit_line_total: 0,
discount_subtotal: 0,
discount_tax_total: 0,
discount_total: 0,
item_subtotal: 219,
item_tax_total: 41.61,
item_total: 260.61,
items: [
{
discount_subtotal: 0,
discount_tax_total: 0,
discount_total: 0,
is_tax_inclusive: true,
original_tax_total: 19,
original_total: 119,
quantity: 1,
subtotal: 100,
tax_lines: [
{
rate: 19,
subtotal: 19,
total: 19,
},
],
tax_total: 19,
total: 119,
unit_price: 119,
},
{
discount_subtotal: 0,
discount_tax_total: 0,
discount_total: 0,
is_tax_inclusive: false,
original_tax_total: 22.61,
original_total: 141.61,
quantity: 1,
subtotal: 119,
tax_lines: [
{
rate: 19,
subtotal: 22.61,
total: 22.61,
},
],
tax_total: 22.61,
total: 141.61,
unit_price: 119,
},
],
original_item_subtotal: 219,
original_item_tax_total: 41.61,
original_item_total: 260.61,
original_tax_total: 41.61,
original_total: 260.61,
subtotal: 219,
tax_total: 41.61,
total: 260.61,
})
})
it("should calculate tax inclusive carts with items + taxes with tax inclusive adjustments", function () {
const cart = {
items: [
{
unit_price: 119,
quantity: 1,
is_tax_inclusive: true,
adjustments: [
{
amount: 100,
},
],
tax_lines: [
{
rate: 19,
},
],
},
],
}
const serialized = JSON.parse(JSON.stringify(decorateCartTotals(cart)))
expect(serialized).toEqual({
credit_line_subtotal: 0,
credit_line_tax_total: 0,
credit_line_total: 0,
discount_subtotal: 100,
discount_tax_total: 19,
discount_total: 119,
item_subtotal: 100,
item_tax_total: 0,
item_total: 0,
items: [
{
adjustments: [
{
amount: 100,
subtotal: 100,
total: 119,
},
],
discount_subtotal: 100,
discount_tax_total: 19,
discount_total: 119,
is_tax_inclusive: true,
original_tax_total: 19,
original_total: 119,
quantity: 1,
subtotal: 100,
tax_lines: [
{
rate: 19,
subtotal: 19,
},
],
tax_total: 0,
total: 0,
unit_price: 119,
},
],
original_item_subtotal: 100,
original_item_tax_total: 19,
original_item_total: 119,
original_tax_total: 19,
original_total: 119,
subtotal: 100,
tax_total: 0,
total: 0,
})
})
})

View File

@@ -5,11 +5,9 @@ import { MathBN } from "../math"
export function calculateAdjustmentTotal({
adjustments,
includesTax,
taxRate,
}: {
adjustments: Pick<AdjustmentLineDTO, "amount" | "is_tax_inclusive">[]
includesTax?: boolean
taxRate?: BigNumberInput
}) {
// the sum of all adjustment amounts excluding tax
@@ -24,35 +22,22 @@ export function calculateAdjustmentTotal({
continue
}
const adjustmentAmount = MathBN.convert(adj.amount)
const adjustmentSubtotal =
isDefined(taxRate) && adj.is_tax_inclusive
? MathBN.div(adj.amount, MathBN.add(1, taxRate))
: adj.amount
if (adj.is_tax_inclusive && isDefined(taxRate)) {
adjustmentsSubtotal = MathBN.add(
adjustmentsSubtotal,
MathBN.div(adjustmentAmount, MathBN.add(1, taxRate))
)
} else {
adjustmentsSubtotal = MathBN.add(adjustmentsSubtotal, adjustmentAmount)
}
const adjustmentTaxTotal = isDefined(taxRate)
? MathBN.mult(adjustmentSubtotal, taxRate)
: 0
const adjustmentTotal = MathBN.add(adjustmentSubtotal, adjustmentTaxTotal)
if (isDefined(taxRate)) {
const adjustmentSubtotal = includesTax
? MathBN.div(adjustmentAmount, MathBN.add(1, taxRate))
: adjustmentAmount
adjustmentsSubtotal = MathBN.add(adjustmentsSubtotal, adjustmentSubtotal)
adjustmentsTaxTotal = MathBN.add(adjustmentsTaxTotal, adjustmentTaxTotal)
adjustmentsTotal = MathBN.add(adjustmentsTotal, adjustmentTotal)
const adjustmentTaxTotal = MathBN.mult(adjustmentSubtotal, taxRate)
const adjustmentTotal = MathBN.add(adjustmentSubtotal, adjustmentTaxTotal)
adj["subtotal"] = new BigNumber(adjustmentSubtotal)
adj["total"] = new BigNumber(adjustmentTotal)
adjustmentsTotal = MathBN.add(adjustmentsTotal, adjustmentTotal)
adjustmentsTaxTotal = MathBN.add(adjustmentsTaxTotal, adjustmentTaxTotal)
} else {
adj["subtotal"] = new BigNumber(adjustmentAmount)
adj["adjustmentAmount"] = new BigNumber(adjustmentAmount)
adjustmentsTotal = MathBN.add(adjustmentsTotal, adjustmentAmount)
}
adj["subtotal"] = new BigNumber(adjustmentsSubtotal)
adj["total"] = new BigNumber(adjustmentsTotal)
}
return {

View File

@@ -16,7 +16,7 @@ export interface GetItemTotalInput {
quantity: BigNumber
is_tax_inclusive?: boolean
tax_lines?: Pick<TaxLineDTO, "rate">[]
adjustments?: Pick<AdjustmentLineDTO, "amount">[]
adjustments?: Pick<AdjustmentLineDTO, "amount" | "is_tax_inclusive">[]
detail?: {
fulfilled_quantity: BigNumber
delivered_quantity: BigNumber
@@ -133,7 +133,6 @@ function getLineItemTotals(
adjustmentsTaxTotal: discountTaxTotal,
} = calculateAdjustmentTotal({
adjustments: item.adjustments || [],
includesTax: isTaxInclusive,
taxRate: sumTaxRate,
})

View File

@@ -5,23 +5,23 @@ import {
} from "../../promotion"
import { MathBN } from "../math"
function getPromotionValueForPercentage(promotion, lineItemTotal) {
return MathBN.mult(MathBN.div(promotion.value, 100), lineItemTotal)
function getPromotionValueForPercentage(promotion, lineItemAmount) {
return MathBN.mult(MathBN.div(promotion.value, 100), lineItemAmount)
}
function getPromotionValueForFixed(promotion, itemTotal, allItemsTotal) {
function getPromotionValueForFixed(promotion, lineItemAmount, lineItemsAmount) {
if (promotion.allocation === ApplicationMethodAllocation.ACROSS) {
const promotionValueForItem = MathBN.mult(
MathBN.div(itemTotal, allItemsTotal),
MathBN.div(lineItemAmount, lineItemsAmount),
promotion.value
)
if (MathBN.lte(promotionValueForItem, itemTotal)) {
if (MathBN.lte(promotionValueForItem, lineItemAmount)) {
return promotionValueForItem
}
const percentage = MathBN.div(
MathBN.mult(itemTotal, 100),
MathBN.mult(lineItemAmount, 100),
promotionValueForItem
)
@@ -30,16 +30,15 @@ function getPromotionValueForFixed(promotion, itemTotal, allItemsTotal) {
MathBN.div(percentage, 100)
).precision(4)
}
return promotion.value
}
export function getPromotionValue(promotion, lineItemTotal, lineItemsTotal) {
export function getPromotionValue(promotion, lineItemAmount, lineItemsAmount) {
if (promotion.type === ApplicationMethodType.PERCENTAGE) {
return getPromotionValueForPercentage(promotion, lineItemTotal)
return getPromotionValueForPercentage(promotion, lineItemAmount)
}
return getPromotionValueForFixed(promotion, lineItemTotal, lineItemsTotal)
return getPromotionValueForFixed(promotion, lineItemAmount, lineItemsAmount)
}
export function getApplicableQuantity(lineItem, maxQuantity) {
@@ -50,14 +49,18 @@ export function getApplicableQuantity(lineItem, maxQuantity) {
return lineItem.quantity
}
function getLineItemUnitPrice(lineItem) {
function getLineItemSubtotal(lineItem) {
return MathBN.div(lineItem.subtotal, lineItem.quantity)
}
function getLineItemTotal(lineItem) {
return MathBN.div(lineItem.total, lineItem.quantity)
}
export function calculateAdjustmentAmountFromPromotion(
lineItem,
promotion,
lineItemsTotal: BigNumberInput = 0
lineItemsAmount: BigNumberInput = 0
) {
/*
For a promotion with an across allocation, we consider not only the line item total, but also the total of all other line items in the order.
@@ -89,20 +92,26 @@ export function calculateAdjustmentAmountFromPromotion(
*/
if (promotion.allocation === ApplicationMethodAllocation.ACROSS) {
const quantity = getApplicableQuantity(lineItem, promotion.max_quantity)
const lineItemTotal = MathBN.mult(getLineItemUnitPrice(lineItem), quantity)
const applicableTotal = MathBN.sub(lineItemTotal, promotion.applied_value)
if (MathBN.lte(applicableTotal, 0)) {
return applicableTotal
const lineItemAmount = MathBN.mult(
promotion.is_tax_inclusive
? getLineItemTotal(lineItem)
: getLineItemSubtotal(lineItem),
quantity
)
const applicableAmount = MathBN.sub(lineItemAmount, promotion.applied_value)
if (MathBN.lte(applicableAmount, 0)) {
return applicableAmount
}
const promotionValue = getPromotionValue(
promotion,
applicableTotal,
lineItemsTotal
applicableAmount,
lineItemsAmount
)
return MathBN.min(promotionValue, applicableTotal)
return MathBN.min(promotionValue, applicableAmount)
}
/*
@@ -124,26 +133,32 @@ export function calculateAdjustmentAmountFromPromotion(
We then apply whichever is lower.
*/
const remainingItemTotal = MathBN.sub(
lineItem.subtotal,
const remainingItemAmount = MathBN.sub(
promotion.is_tax_inclusive ? lineItem.total : lineItem.subtotal,
promotion.applied_value
)
const unitPrice = MathBN.div(lineItem.subtotal, lineItem.quantity)
const maximumPromotionTotal = MathBN.mult(
unitPrice,
const itemAmount = MathBN.div(
promotion.is_tax_inclusive ? lineItem.total : lineItem.subtotal,
lineItem.quantity
)
const maximumPromotionAmount = MathBN.mult(
itemAmount,
promotion.max_quantity ?? MathBN.convert(1)
)
const applicableTotal = MathBN.min(remainingItemTotal, maximumPromotionTotal)
const applicableAmount = MathBN.min(
remainingItemAmount,
maximumPromotionAmount
)
if (MathBN.lte(applicableTotal, 0)) {
if (MathBN.lte(applicableAmount, 0)) {
return MathBN.convert(0)
}
const promotionValue = getPromotionValue(
promotion,
applicableTotal,
lineItemsTotal
applicableAmount,
lineItemsAmount
)
return MathBN.min(promotionValue, applicableTotal)
return MathBN.min(promotionValue, applicableAmount)
}

View File

@@ -72,7 +72,6 @@ export function getShippingMethodTotals(
adjustmentsTaxTotal: discountsTaxTotal,
} = calculateAdjustmentTotal({
adjustments: shippingMethod.adjustments || [],
includesTax: isTaxInclusive,
taxRate: sumTaxRate,
})