fix(totals): order item totals (#7867)

This commit is contained in:
Carlos R. L. Rodrigues authored and GitHub committed 2024-06-28 09:34:25 -03:00
1 parent ef259b3386
commit 9f3998393b
6 files changed
+82 -28

No files matched your search

@@ -290,6 +290,14 @@ medusaIntegrationTestRunner({
discount_tax_total: 0, discount_tax_total: 0,
tax_total: 0, tax_total: 0,
original_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: { raw_subtotal: {
value: "50", value: "50",
precision: 20, precision: 20,
@@ -318,6 +326,38 @@ medusaIntegrationTestRunner({
value: "0", value: "0",
precision: 20, 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: { shipping_address: {
@@ -641,12 +641,14 @@ describe("Total calculation", function () {
{ {
unit_price: 50, unit_price: 50,
quantity: 2, quantity: 2,
detail: {
fulfilled_quantity: 2, fulfilled_quantity: 2,
shipped_quantity: 2, shipped_quantity: 2,
return_requested_quantity: 0, return_requested_quantity: 0,
return_received_quantity: 1, return_received_quantity: 1,
return_dismissed_quantity: 1, return_dismissed_quantity: 1,
written_off_quantity: 0, written_off_quantity: 0,
},
tax_lines: [ tax_lines: [
{ {
rate: 10, rate: 10,
@@ -668,12 +670,6 @@ describe("Total calculation", function () {
{ {
unit_price: 50, unit_price: 50,
quantity: 2, 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: [ tax_lines: [
{ {
rate: 10, rate: 10,
@@ -688,6 +684,14 @@ describe("Total calculation", function () {
total: 22, 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, subtotal: 100,
total: 88, total: 88,
original_total: 110, original_total: 110,
+6 -6
View File
@@ -44,12 +44,12 @@ export function decorateCartTotals(
transformPropertiesToBigNumber(cartLike) transformPropertiesToBigNumber(cartLike)
const optionalFields = { const optionalFields = {
fulfilled_quantity: "fulfilled_total", "detail.fulfilled_quantity": "fulfilled_total",
shipped_quantity: "shipped_total", "detail.shipped_quantity": "shipped_total",
return_requested_quantity: "return_requested_total", "detail.return_requested_quantity": "return_requested_total",
return_received_quantity: "return_received_total", "detail.return_received_quantity": "return_received_total",
return_dismissed_quantity: "return_dismissed_total", "detail.return_dismissed_quantity": "return_dismissed_total",
written_off_quantity: "write_off_total", "detail.written_off_quantity": "write_off_total",
} }
const items = (cartLike.items ?? []) as unknown as GetItemTotalInput[] const items = (cartLike.items ?? []) as unknown as GetItemTotalInput[]
@@ -1,5 +1,5 @@
import { AdjustmentLineDTO, BigNumberInput, TaxLineDTO } from "@medusajs/types" import { AdjustmentLineDTO, BigNumberInput, TaxLineDTO } from "@medusajs/types"
import { isDefined } from "../../common" import { isDefined, pickValueFromObject } from "../../common"
import { calculateAdjustmentTotal } from "../adjustment" import { calculateAdjustmentTotal } from "../adjustment"
import { BigNumber } from "../big-number" import { BigNumber } from "../big-number"
import { MathBN } from "../math" import { MathBN } from "../math"
@@ -17,13 +17,14 @@ export interface GetItemTotalInput {
is_tax_inclusive?: boolean is_tax_inclusive?: boolean
tax_lines?: Pick<TaxLineDTO, "rate">[] tax_lines?: Pick<TaxLineDTO, "rate">[]
adjustments?: Pick<AdjustmentLineDTO, "amount">[] adjustments?: Pick<AdjustmentLineDTO, "amount">[]
detail?: {
fulfilled_quantity?: BigNumber fulfilled_quantity: BigNumber
shipped_quantity?: BigNumber shipped_quantity: BigNumber
return_requested_quantity?: BigNumber return_requested_quantity: BigNumber
return_received_quantity?: BigNumber return_received_quantity: BigNumber
return_dismissed_quantity?: BigNumber return_dismissed_quantity: BigNumber
written_off_quantity?: BigNumber written_off_quantity: BigNumber
}
} }
export interface GetItemTotalOutput { export interface GetItemTotalOutput {
@@ -76,10 +77,11 @@ function setRefundableTotal(
totals: GetItemTotalOutput, totals: GetItemTotalOutput,
context: GetLineItemsTotalsContext context: GetLineItemsTotalsContext
) { ) {
const itemDetail = item.detail!
const totalReturnedQuantity = MathBN.sum( const totalReturnedQuantity = MathBN.sum(
item.return_requested_quantity ?? 0, itemDetail.return_requested_quantity ?? 0,
item.return_received_quantity ?? 0, itemDetail.return_received_quantity ?? 0,
item.return_dismissed_quantity ?? 0 itemDetail.return_dismissed_quantity ?? 0
) )
const currentQuantity = MathBN.sub(item.quantity, totalReturnedQuantity) const currentQuantity = MathBN.sub(item.quantity, totalReturnedQuantity)
const discountPerUnit = MathBN.div(discountTotal, item.quantity) const discountPerUnit = MathBN.div(discountTotal, item.quantity)
@@ -152,7 +154,7 @@ function getLineItemTotals(
}) })
totals.tax_total = new BigNumber(taxTotal) totals.tax_total = new BigNumber(taxTotal)
if (isDefined(item.return_requested_quantity)) { if (isDefined(item.detail?.return_requested_quantity)) {
setRefundableTotal(item, discountTotal, totals, context) setRefundableTotal(item, discountTotal, totals, context)
} }
@@ -186,10 +188,18 @@ function getLineItemTotals(
} }
for (const field in optionalFields) { for (const field in optionalFields) {
if (field in item) {
const totalField = optionalFields[field] const totalField = optionalFields[field]
totals[totalField] = new BigNumber(MathBN.mult(totalPerUnit, item[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 return totals
@@ -31,7 +31,7 @@ export const adminOrderRoutesMiddlewares: MiddlewareRoute[] = [
}, },
{ {
method: ["POST"], method: ["POST"],
matcher: "/admin/returns/create-return", matcher: "/admin/returns",
middlewares: [ middlewares: [
validateAndTransformBody(AdminPostReturnsReqSchema), validateAndTransformBody(AdminPostReturnsReqSchema),
validateAndTransformQuery( validateAndTransformQuery(
@@ -7,7 +7,7 @@ import { ReturnsParams, StorePostReturnsReqSchema } from "./validators"
export const storeRegionRoutesMiddlewares: MiddlewareRoute[] = [ export const storeRegionRoutesMiddlewares: MiddlewareRoute[] = [
{ {
method: ["POST"], method: ["POST"],
matcher: "/store/returns/create-return", matcher: "/store/returns",
middlewares: [ middlewares: [
validateAndTransformBody(StorePostReturnsReqSchema), validateAndTransformBody(StorePostReturnsReqSchema),
validateAndTransformQuery( validateAndTransformQuery(