fix(totals): order item totals (#7867)
This commit is contained in:
committed by
GitHub
parent
ef259b3386
commit
9f3998393b
@@ -290,6 +290,14 @@ medusaIntegrationTestRunner({
|
||||
discount_tax_total: 0,
|
||||
tax_total: 0,
|
||||
original_tax_total: 0,
|
||||
refundable_total: 50,
|
||||
refundable_total_per_unit: 50,
|
||||
fulfilled_total: 0,
|
||||
return_dismissed_total: 0,
|
||||
return_received_total: 0,
|
||||
return_requested_total: 0,
|
||||
shipped_total: 0,
|
||||
write_off_total: 0,
|
||||
raw_subtotal: {
|
||||
value: "50",
|
||||
precision: 20,
|
||||
@@ -318,6 +326,38 @@ medusaIntegrationTestRunner({
|
||||
value: "0",
|
||||
precision: 20,
|
||||
},
|
||||
raw_refundable_total: {
|
||||
precision: 20,
|
||||
value: "49.999999999999999995",
|
||||
},
|
||||
raw_refundable_total_per_unit: {
|
||||
precision: 20,
|
||||
value: "49.999999999999999995",
|
||||
},
|
||||
raw_fulfilled_total: {
|
||||
precision: 20,
|
||||
value: "0",
|
||||
},
|
||||
raw_return_dismissed_total: {
|
||||
precision: 20,
|
||||
value: "0",
|
||||
},
|
||||
raw_return_received_total: {
|
||||
precision: 20,
|
||||
value: "0",
|
||||
},
|
||||
raw_return_requested_total: {
|
||||
precision: 20,
|
||||
value: "0",
|
||||
},
|
||||
raw_shipped_total: {
|
||||
precision: 20,
|
||||
value: "0",
|
||||
},
|
||||
raw_write_off_total: {
|
||||
precision: 20,
|
||||
value: "0",
|
||||
},
|
||||
},
|
||||
],
|
||||
shipping_address: {
|
||||
|
||||
@@ -641,12 +641,14 @@ describe("Total calculation", function () {
|
||||
{
|
||||
unit_price: 50,
|
||||
quantity: 2,
|
||||
fulfilled_quantity: 2,
|
||||
shipped_quantity: 2,
|
||||
return_requested_quantity: 0,
|
||||
return_received_quantity: 1,
|
||||
return_dismissed_quantity: 1,
|
||||
written_off_quantity: 0,
|
||||
detail: {
|
||||
fulfilled_quantity: 2,
|
||||
shipped_quantity: 2,
|
||||
return_requested_quantity: 0,
|
||||
return_received_quantity: 1,
|
||||
return_dismissed_quantity: 1,
|
||||
written_off_quantity: 0,
|
||||
},
|
||||
tax_lines: [
|
||||
{
|
||||
rate: 10,
|
||||
@@ -668,12 +670,6 @@ describe("Total calculation", function () {
|
||||
{
|
||||
unit_price: 50,
|
||||
quantity: 2,
|
||||
fulfilled_quantity: 2,
|
||||
shipped_quantity: 2,
|
||||
return_requested_quantity: 0,
|
||||
return_received_quantity: 1,
|
||||
return_dismissed_quantity: 1,
|
||||
written_off_quantity: 0,
|
||||
tax_lines: [
|
||||
{
|
||||
rate: 10,
|
||||
@@ -688,6 +684,14 @@ describe("Total calculation", function () {
|
||||
total: 22,
|
||||
},
|
||||
],
|
||||
detail: {
|
||||
fulfilled_quantity: 2,
|
||||
return_dismissed_quantity: 1,
|
||||
return_received_quantity: 1,
|
||||
return_requested_quantity: 0,
|
||||
shipped_quantity: 2,
|
||||
written_off_quantity: 0,
|
||||
},
|
||||
subtotal: 100,
|
||||
total: 88,
|
||||
original_total: 110,
|
||||
|
||||
@@ -44,12 +44,12 @@ export function decorateCartTotals(
|
||||
transformPropertiesToBigNumber(cartLike)
|
||||
|
||||
const optionalFields = {
|
||||
fulfilled_quantity: "fulfilled_total",
|
||||
shipped_quantity: "shipped_total",
|
||||
return_requested_quantity: "return_requested_total",
|
||||
return_received_quantity: "return_received_total",
|
||||
return_dismissed_quantity: "return_dismissed_total",
|
||||
written_off_quantity: "write_off_total",
|
||||
"detail.fulfilled_quantity": "fulfilled_total",
|
||||
"detail.shipped_quantity": "shipped_total",
|
||||
"detail.return_requested_quantity": "return_requested_total",
|
||||
"detail.return_received_quantity": "return_received_total",
|
||||
"detail.return_dismissed_quantity": "return_dismissed_total",
|
||||
"detail.written_off_quantity": "write_off_total",
|
||||
}
|
||||
|
||||
const items = (cartLike.items ?? []) as unknown as GetItemTotalInput[]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { AdjustmentLineDTO, BigNumberInput, TaxLineDTO } from "@medusajs/types"
|
||||
import { isDefined } from "../../common"
|
||||
import { isDefined, pickValueFromObject } from "../../common"
|
||||
import { calculateAdjustmentTotal } from "../adjustment"
|
||||
import { BigNumber } from "../big-number"
|
||||
import { MathBN } from "../math"
|
||||
@@ -17,13 +17,14 @@ export interface GetItemTotalInput {
|
||||
is_tax_inclusive?: boolean
|
||||
tax_lines?: Pick<TaxLineDTO, "rate">[]
|
||||
adjustments?: Pick<AdjustmentLineDTO, "amount">[]
|
||||
|
||||
fulfilled_quantity?: BigNumber
|
||||
shipped_quantity?: BigNumber
|
||||
return_requested_quantity?: BigNumber
|
||||
return_received_quantity?: BigNumber
|
||||
return_dismissed_quantity?: BigNumber
|
||||
written_off_quantity?: BigNumber
|
||||
detail?: {
|
||||
fulfilled_quantity: BigNumber
|
||||
shipped_quantity: BigNumber
|
||||
return_requested_quantity: BigNumber
|
||||
return_received_quantity: BigNumber
|
||||
return_dismissed_quantity: BigNumber
|
||||
written_off_quantity: BigNumber
|
||||
}
|
||||
}
|
||||
|
||||
export interface GetItemTotalOutput {
|
||||
@@ -76,10 +77,11 @@ function setRefundableTotal(
|
||||
totals: GetItemTotalOutput,
|
||||
context: GetLineItemsTotalsContext
|
||||
) {
|
||||
const itemDetail = item.detail!
|
||||
const totalReturnedQuantity = MathBN.sum(
|
||||
item.return_requested_quantity ?? 0,
|
||||
item.return_received_quantity ?? 0,
|
||||
item.return_dismissed_quantity ?? 0
|
||||
itemDetail.return_requested_quantity ?? 0,
|
||||
itemDetail.return_received_quantity ?? 0,
|
||||
itemDetail.return_dismissed_quantity ?? 0
|
||||
)
|
||||
const currentQuantity = MathBN.sub(item.quantity, totalReturnedQuantity)
|
||||
const discountPerUnit = MathBN.div(discountTotal, item.quantity)
|
||||
@@ -152,7 +154,7 @@ function getLineItemTotals(
|
||||
})
|
||||
totals.tax_total = new BigNumber(taxTotal)
|
||||
|
||||
if (isDefined(item.return_requested_quantity)) {
|
||||
if (isDefined(item.detail?.return_requested_quantity)) {
|
||||
setRefundableTotal(item, discountTotal, totals, context)
|
||||
}
|
||||
|
||||
@@ -186,10 +188,18 @@ function getLineItemTotals(
|
||||
}
|
||||
|
||||
for (const field in optionalFields) {
|
||||
if (field in item) {
|
||||
const totalField = optionalFields[field]
|
||||
totals[totalField] = new BigNumber(MathBN.mult(totalPerUnit, item[field]))
|
||||
const totalField = optionalFields[field]
|
||||
|
||||
let target = item[totalField]
|
||||
if (field.includes(".")) {
|
||||
target = pickValueFromObject(field, item)
|
||||
}
|
||||
|
||||
if (!isDefined(target)) {
|
||||
continue
|
||||
}
|
||||
|
||||
totals[totalField] = new BigNumber(MathBN.mult(totalPerUnit, target))
|
||||
}
|
||||
|
||||
return totals
|
||||
|
||||
@@ -31,7 +31,7 @@ export const adminOrderRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/returns/create-return",
|
||||
matcher: "/admin/returns",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminPostReturnsReqSchema),
|
||||
validateAndTransformQuery(
|
||||
|
||||
@@ -7,7 +7,7 @@ import { ReturnsParams, StorePostReturnsReqSchema } from "./validators"
|
||||
export const storeRegionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/store/returns/create-return",
|
||||
matcher: "/store/returns",
|
||||
middlewares: [
|
||||
validateAndTransformBody(StorePostReturnsReqSchema),
|
||||
validateAndTransformQuery(
|
||||
|
||||
Reference in New Issue
Block a user