fix(utils): totals calculation (#8190)
This commit is contained in:
committed by
GitHub
parent
fe5a7b1810
commit
07205e4249
@@ -1,5 +1,4 @@
|
||||
export enum ChangeActionType {
|
||||
CANCEL = "CANCEL",
|
||||
FULFILL_ITEM = "FULFILL_ITEM",
|
||||
CANCEL_ITEM_FULFILLMENT = "CANCEL_ITEM_FULFILLMENT",
|
||||
ITEM_ADD = "ITEM_ADD",
|
||||
|
||||
@@ -111,7 +111,7 @@ describe("Total calculation", function () {
|
||||
tax_lines: [
|
||||
{
|
||||
rate: 10,
|
||||
total: 9,
|
||||
total: 8.9,
|
||||
subtotal: 10,
|
||||
},
|
||||
],
|
||||
@@ -123,24 +123,24 @@ describe("Total calculation", function () {
|
||||
},
|
||||
],
|
||||
subtotal: 100,
|
||||
total: 99,
|
||||
total: 97.9,
|
||||
original_total: 110,
|
||||
discount_total: 10,
|
||||
discount_tax_total: 1,
|
||||
tax_total: 9,
|
||||
discount_total: 11,
|
||||
discount_tax_total: 1.1,
|
||||
tax_total: 8.9,
|
||||
original_tax_total: 10,
|
||||
},
|
||||
],
|
||||
total: 99,
|
||||
total: 97.9,
|
||||
subtotal: 100,
|
||||
tax_total: 9,
|
||||
discount_total: 10,
|
||||
discount_tax_total: 1,
|
||||
original_total: 100,
|
||||
tax_total: 8.9,
|
||||
discount_total: 11,
|
||||
discount_tax_total: 1.1,
|
||||
original_total: 99,
|
||||
original_tax_total: 10,
|
||||
item_total: 99,
|
||||
item_total: 97.9,
|
||||
item_subtotal: 100,
|
||||
item_tax_total: 9,
|
||||
item_tax_total: 8.9,
|
||||
original_item_total: 110,
|
||||
original_item_subtotal: 100,
|
||||
original_item_tax_total: 10,
|
||||
@@ -252,7 +252,7 @@ describe("Total calculation", function () {
|
||||
tax_lines: [
|
||||
{
|
||||
rate: 10,
|
||||
total: 0.7,
|
||||
total: 0.67,
|
||||
subtotal: 1,
|
||||
},
|
||||
],
|
||||
@@ -264,11 +264,11 @@ describe("Total calculation", function () {
|
||||
},
|
||||
],
|
||||
subtotal: 10,
|
||||
total: 7.7,
|
||||
total: 7.37,
|
||||
original_total: 11,
|
||||
discount_total: 3,
|
||||
discount_tax_total: 0.3,
|
||||
tax_total: 0.7,
|
||||
discount_total: 3.3,
|
||||
discount_tax_total: 0.33,
|
||||
tax_total: 0.67,
|
||||
original_tax_total: 1,
|
||||
},
|
||||
],
|
||||
@@ -290,7 +290,7 @@ describe("Total calculation", function () {
|
||||
total: 2,
|
||||
},
|
||||
],
|
||||
subtotal: 10.380952380952381,
|
||||
subtotal: 9.523809523809524,
|
||||
total: 8,
|
||||
original_total: 10,
|
||||
discount_total: 2,
|
||||
@@ -304,7 +304,7 @@ describe("Total calculation", function () {
|
||||
tax_lines: [
|
||||
{
|
||||
rate: 10,
|
||||
total: 0.3,
|
||||
total: 0.28,
|
||||
subtotal: 0.5,
|
||||
},
|
||||
],
|
||||
@@ -316,32 +316,32 @@ describe("Total calculation", function () {
|
||||
},
|
||||
],
|
||||
subtotal: 5,
|
||||
total: 3.3,
|
||||
total: 3.08,
|
||||
original_total: 5.5,
|
||||
discount_total: 2,
|
||||
discount_tax_total: 0.2,
|
||||
tax_total: 0.3,
|
||||
discount_total: 2.2,
|
||||
discount_tax_total: 0.22,
|
||||
tax_total: 0.28,
|
||||
original_tax_total: 0.5,
|
||||
},
|
||||
],
|
||||
total: 104.77186147186147,
|
||||
subtotal: 100.9090909090909,
|
||||
tax_total: 9.562770562770563,
|
||||
discount_total: 17,
|
||||
discount_tax_total: 1.6,
|
||||
original_total: 110.47619047619048,
|
||||
total: 107.445670995671,
|
||||
subtotal: 115.43290043290044,
|
||||
tax_total: 9.512770562770562,
|
||||
discount_total: 17.5,
|
||||
discount_tax_total: 1.65,
|
||||
original_total: 124.5,
|
||||
original_tax_total: 11.067099567099566,
|
||||
item_total: 97.7,
|
||||
item_total: 97.37,
|
||||
item_subtotal: 100.9090909090909,
|
||||
item_tax_total: 8.881818181818181,
|
||||
item_tax_total: 8.851818181818182,
|
||||
original_item_total: 111,
|
||||
original_item_subtotal: 100.9090909090909,
|
||||
original_item_tax_total: 10.090909090909092,
|
||||
shipping_total: 11.3,
|
||||
shipping_subtotal: 15.380952380952381,
|
||||
shipping_tax_total: 0.680952380952381,
|
||||
shipping_total: 11.08,
|
||||
shipping_subtotal: 14.523809523809524,
|
||||
shipping_tax_total: 0.660952380952381,
|
||||
original_shipping_tax_total: 0.9761904761904762,
|
||||
original_shipping_tax_subtotal: 15.380952380952381,
|
||||
original_shipping_subtotal: 14.523809523809524,
|
||||
original_shipping_total: 15.5,
|
||||
})
|
||||
})
|
||||
@@ -567,7 +567,7 @@ describe("Total calculation", function () {
|
||||
tax_lines: [
|
||||
{
|
||||
rate: 10,
|
||||
total: 8,
|
||||
total: 7.8,
|
||||
subtotal: 10,
|
||||
},
|
||||
],
|
||||
@@ -579,11 +579,11 @@ describe("Total calculation", function () {
|
||||
},
|
||||
],
|
||||
subtotal: 100,
|
||||
total: 88,
|
||||
total: 85.8,
|
||||
original_total: 110,
|
||||
discount_total: 20,
|
||||
discount_tax_total: 2,
|
||||
tax_total: 8,
|
||||
discount_total: 22,
|
||||
discount_tax_total: 2.2,
|
||||
tax_total: 7.8,
|
||||
original_tax_total: 10,
|
||||
},
|
||||
],
|
||||
@@ -593,7 +593,7 @@ describe("Total calculation", function () {
|
||||
tax_lines: [
|
||||
{
|
||||
rate: 10,
|
||||
total: 2.3,
|
||||
total: 2.28,
|
||||
subtotal: 2.5,
|
||||
},
|
||||
],
|
||||
@@ -605,32 +605,32 @@ describe("Total calculation", function () {
|
||||
},
|
||||
],
|
||||
subtotal: 25,
|
||||
total: 25.3,
|
||||
total: 25.08,
|
||||
original_total: 27.5,
|
||||
discount_total: 2,
|
||||
discount_tax_total: 0.2,
|
||||
tax_total: 2.3,
|
||||
discount_total: 2.2,
|
||||
discount_tax_total: 0.22,
|
||||
tax_total: 2.28,
|
||||
original_tax_total: 2.5,
|
||||
},
|
||||
],
|
||||
total: 113.6,
|
||||
subtotal: 100,
|
||||
tax_total: 10.3,
|
||||
discount_total: 22,
|
||||
discount_tax_total: 2.2,
|
||||
original_total: 118,
|
||||
total: 110.88,
|
||||
subtotal: 125,
|
||||
tax_total: 10.08,
|
||||
discount_total: 24.2,
|
||||
discount_tax_total: 2.42,
|
||||
original_total: 140.8,
|
||||
original_tax_total: 12.5,
|
||||
item_total: 88,
|
||||
item_total: 85.8,
|
||||
item_subtotal: 100,
|
||||
item_tax_total: 8,
|
||||
item_tax_total: 7.8,
|
||||
original_item_total: 110,
|
||||
original_item_subtotal: 100,
|
||||
original_item_tax_total: 10,
|
||||
shipping_total: 25.3,
|
||||
shipping_total: 25.08,
|
||||
shipping_subtotal: 25,
|
||||
shipping_tax_total: 2.3,
|
||||
shipping_tax_total: 2.28,
|
||||
original_shipping_tax_total: 2.5,
|
||||
original_shipping_tax_subtotal: 25,
|
||||
original_shipping_subtotal: 25,
|
||||
original_shipping_total: 27.5,
|
||||
})
|
||||
})
|
||||
@@ -673,7 +673,7 @@ describe("Total calculation", function () {
|
||||
tax_lines: [
|
||||
{
|
||||
rate: 10,
|
||||
total: 8,
|
||||
total: 7.8,
|
||||
subtotal: 10,
|
||||
},
|
||||
],
|
||||
@@ -693,41 +693,41 @@ describe("Total calculation", function () {
|
||||
written_off_quantity: 1,
|
||||
},
|
||||
subtotal: 100,
|
||||
total: 88,
|
||||
total: 85.8,
|
||||
original_total: 110,
|
||||
discount_total: 20,
|
||||
discount_tax_total: 2,
|
||||
tax_total: 8,
|
||||
discount_total: 22,
|
||||
discount_tax_total: 2.2,
|
||||
tax_total: 7.8,
|
||||
original_tax_total: 10,
|
||||
fulfilled_total: 88,
|
||||
shipped_total: 88,
|
||||
fulfilled_total: 85.8,
|
||||
shipped_total: 85.8,
|
||||
return_requested_total: 0,
|
||||
return_received_total: 44,
|
||||
return_dismissed_total: 44,
|
||||
write_off_total: 44,
|
||||
return_received_total: 42.9,
|
||||
return_dismissed_total: 42.9,
|
||||
write_off_total: 42.9,
|
||||
refundable_total: 0,
|
||||
refundable_total_per_unit: 0,
|
||||
},
|
||||
],
|
||||
total: 88,
|
||||
total: 85.8,
|
||||
subtotal: 100,
|
||||
tax_total: 8,
|
||||
discount_total: 20,
|
||||
discount_tax_total: 2,
|
||||
original_total: 90,
|
||||
tax_total: 7.8,
|
||||
discount_total: 22,
|
||||
discount_tax_total: 2.2,
|
||||
original_total: 88,
|
||||
original_tax_total: 10,
|
||||
item_total: 88,
|
||||
item_total: 85.8,
|
||||
item_subtotal: 100,
|
||||
item_tax_total: 8,
|
||||
item_tax_total: 7.8,
|
||||
original_item_total: 110,
|
||||
original_item_subtotal: 100,
|
||||
original_item_tax_total: 10,
|
||||
fulfilled_total: 88,
|
||||
shipped_total: 88,
|
||||
fulfilled_total: 85.8,
|
||||
shipped_total: 85.8,
|
||||
return_requested_total: 0,
|
||||
return_received_total: 44,
|
||||
return_dismissed_total: 44,
|
||||
write_off_total: 44,
|
||||
return_received_total: 42.9,
|
||||
return_dismissed_total: 42.9,
|
||||
write_off_total: 42.9,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -30,8 +30,10 @@ export function calculateAdjustmentTotal({
|
||||
adj["subtotal"] = new BigNumber(MathBN.sub(adj.amount, taxAmount))
|
||||
adj["total"] = new BigNumber(adj.amount)
|
||||
} else {
|
||||
total = MathBN.add(adj.amount, taxAmount)
|
||||
|
||||
adj["subtotal"] = new BigNumber(adj.amount)
|
||||
adj["total"] = new BigNumber(MathBN.add(adj.amount, taxAmount))
|
||||
adj["total"] = new BigNumber(total)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,10 +91,8 @@ export function decorateCartTotals(
|
||||
let shippingOriginalSubtotal = MathBN.convert(0)
|
||||
|
||||
let shippingTaxTotal = MathBN.convert(0)
|
||||
let shippingTaxSubTotal = MathBN.convert(0)
|
||||
|
||||
let shippingOriginalTaxTotal = MathBN.convert(0)
|
||||
let shippingOriginalTaxSubtotal = MathBN.convert(0)
|
||||
|
||||
const cartItems = items.map((item, index) => {
|
||||
const itemTotals = Object.assign(item, itemsTotals[item.id ?? index] ?? {})
|
||||
@@ -147,6 +145,8 @@ export function decorateCartTotals(
|
||||
|
||||
const methodSubtotal = MathBN.convert(methodTotals.subtotal)
|
||||
|
||||
subtotal = MathBN.add(subtotal, methodSubtotal)
|
||||
|
||||
const methodTotal = MathBN.convert(methodTotals.total)
|
||||
const methodOriginalTotal = MathBN.convert(methodTotals.original_total)
|
||||
const methodTaxTotal = MathBN.convert(methodTotals.tax_total)
|
||||
@@ -175,11 +175,6 @@ export function decorateCartTotals(
|
||||
shippingOriginalTaxTotal,
|
||||
methodOriginalTaxTotal
|
||||
)
|
||||
shippingOriginalTaxSubtotal = MathBN.add(
|
||||
shippingOriginalTaxSubtotal,
|
||||
methodSubtotal
|
||||
)
|
||||
|
||||
discountTotal = MathBN.add(discountTotal, methodDiscountTotal)
|
||||
discountTaxTotal = MathBN.add(discountTaxTotal, methodDiscountTaxTotal)
|
||||
|
||||
@@ -202,7 +197,7 @@ export function decorateCartTotals(
|
||||
)
|
||||
const originalTotal = MathBN.sub(originalTempTotal, discountTotal)
|
||||
// TODO: subtract (cart.gift_card_total + cart.gift_card_tax_total)
|
||||
const tempTotal = MathBN.add(subtotal, shippingTotal, taxTotal)
|
||||
const tempTotal = MathBN.add(subtotal, taxTotal)
|
||||
const total = MathBN.sub(tempTotal, discountTotal)
|
||||
|
||||
const cart = cartLike as any
|
||||
@@ -245,9 +240,7 @@ export function decorateCartTotals(
|
||||
cart.shipping_tax_total = new BigNumber(shippingTaxTotal)
|
||||
|
||||
cart.original_shipping_tax_total = new BigNumber(shippingOriginalTaxTotal)
|
||||
cart.original_shipping_tax_subtotal = new BigNumber(
|
||||
shippingOriginalTaxSubtotal
|
||||
)
|
||||
cart.original_shipping_subtotal = new BigNumber(shippingOriginalSubtotal)
|
||||
cart.original_shipping_total = new BigNumber(shippingOriginalTotal)
|
||||
}
|
||||
|
||||
|
||||
@@ -132,8 +132,8 @@ function getLineItemTotals(
|
||||
unit_price: item.unit_price,
|
||||
|
||||
subtotal: new BigNumber(subtotal),
|
||||
|
||||
total: new BigNumber(total),
|
||||
|
||||
original_total: new BigNumber(subtotal),
|
||||
|
||||
discount_total: new BigNumber(discountTotal),
|
||||
|
||||
@@ -115,7 +115,7 @@ export function getShippingMethodTotals(
|
||||
const isTaxInclusive = context.includeTax ?? shippingMethod.is_tax_inclusive
|
||||
|
||||
if (isTaxInclusive) {
|
||||
const subtotal = MathBN.add(shippingMethod.amount, taxTotal)
|
||||
const subtotal = MathBN.sub(shippingMethod.amount, originalTaxTotal)
|
||||
totals.subtotal = new BigNumber(subtotal)
|
||||
} else {
|
||||
const originalTotal = MathBN.add(
|
||||
|
||||
Reference in New Issue
Block a user