diff --git a/.changeset/chilly-zoos-beg.md b/.changeset/chilly-zoos-beg.md new file mode 100644 index 0000000000..41ec3f7370 --- /dev/null +++ b/.changeset/chilly-zoos-beg.md @@ -0,0 +1,6 @@ +--- +"@medusajs/dashboard": patch +"@medusajs/order": patch +--- + +fix(dashboard, order): summary pending diff calculation on preview diff --git a/integration-tests/http/__tests__/returns/returns.spec.ts b/integration-tests/http/__tests__/returns/returns.spec.ts index 8fd9784c0a..d65a371394 100644 --- a/integration-tests/http/__tests__/returns/returns.spec.ts +++ b/integration-tests/http/__tests__/returns/returns.spec.ts @@ -482,7 +482,7 @@ medusaIntegrationTestRunner({ expect.objectContaining({ transaction_total: 0, current_order_total: 61, - pending_difference: 11, + pending_difference: 61, // item is not yet received paid_total: 0, refunded_total: 0, }) @@ -710,6 +710,12 @@ medusaIntegrationTestRunner({ }) ) + expect(result.data.order_preview.summary).toEqual( + expect.objectContaining({ + pending_difference: 61 + 1002, // original total + newly added shipping + }) + ) + result = await api.post( `/admin/returns/${returnId}/request`, {}, diff --git a/packages/admin/dashboard/src/routes/orders/order-create-claim/components/claim-create-form/claim-create-form.tsx b/packages/admin/dashboard/src/routes/orders/order-create-claim/components/claim-create-form/claim-create-form.tsx index d7a2f476a8..3f6fde0999 100644 --- a/packages/admin/dashboard/src/routes/orders/order-create-claim/components/claim-create-form/claim-create-form.tsx +++ b/packages/admin/dashboard/src/routes/orders/order-create-claim/components/claim-create-form/claim-create-form.tsx @@ -568,6 +568,16 @@ export const ClaimCreateForm = ({ } }, []) + /** + * For estimated difference show pending difference and subtract the total of inbound items (assume all items will be returned correctly) + * We don't include inbound total in the pending difference because it will be considered returned when the receive flow is completed + */ + const estimatedDifference = + preview.summary.pending_difference - + inboundPreviewItems.reduce((acc, item) => { + return acc + item.total + }, 0) + const inboundShippingTotal = useMemo(() => { const method = preview.shipping_methods.find( (sm) => @@ -1010,10 +1020,7 @@ export const ClaimCreateForm = ({ {t("orders.claims.refundAmount")} - {getStylizedAmount( - preview.summary.pending_difference, - order.currency_code - )} + {getStylizedAmount(estimatedDifference, order.currency_code)} diff --git a/packages/admin/dashboard/src/routes/orders/order-create-exchange/components/exchange-create-form/exchange-create-form.tsx b/packages/admin/dashboard/src/routes/orders/order-create-exchange/components/exchange-create-form/exchange-create-form.tsx index 320d7290b6..786f264077 100644 --- a/packages/admin/dashboard/src/routes/orders/order-create-exchange/components/exchange-create-form/exchange-create-form.tsx +++ b/packages/admin/dashboard/src/routes/orders/order-create-exchange/components/exchange-create-form/exchange-create-form.tsx @@ -272,6 +272,16 @@ export const ExchangeCreateForm = ({ } }, []) + /** + * For estimated difference show pending difference and subtract the total of inbound items (assume all items will be returned correctly) + * We don't include inbound total in the pending difference because it will be considered returned when the receive flow is completed + */ + const estimatedDifference = + preview.summary.pending_difference - + inboundPreviewItems.reduce((acc, item) => { + return acc + item.total + }, 0) + const inboundShippingTotal = useMemo(() => { const method = preview.shipping_methods.find( (sm) => @@ -516,10 +526,7 @@ export const ExchangeCreateForm = ({ {t("orders.exchanges.refundAmount")} - {getStylizedAmount( - preview.summary.pending_difference, - order.currency_code - )} + {getStylizedAmount(estimatedDifference, order.currency_code)} diff --git a/packages/admin/dashboard/src/routes/orders/order-create-return/components/return-create-form/return-create-form.tsx b/packages/admin/dashboard/src/routes/orders/order-create-return/components/return-create-form/return-create-form.tsx index e4a53f9b77..58f3a4d873 100644 --- a/packages/admin/dashboard/src/routes/orders/order-create-return/components/return-create-form/return-create-form.tsx +++ b/packages/admin/dashboard/src/routes/orders/order-create-return/components/return-create-form/return-create-form.tsx @@ -402,6 +402,16 @@ export const ReturnCreateForm = ({ return method?.total || 0 }, [preview.shipping_methods]) + /** + * For estimated difference show pending difference and subtract the total of inbound items (assume all items will be returned correctly) + * We don't include inbound total in the pending difference because it will be considered returned when the receive flow is completed + */ + const estimatedDifference = + preview.summary.pending_difference - + previewItems.reduce((acc, item) => { + return acc + item.total + }, 0) + return ( - {getStylizedAmount( - preview.summary.pending_difference, - order.currency_code - )} + {getStylizedAmount(estimatedDifference, order.currency_code)} diff --git a/packages/modules/order/src/utils/calculate-order-change.ts b/packages/modules/order/src/utils/calculate-order-change.ts index 15d5acf26a..775668185e 100644 --- a/packages/modules/order/src/utils/calculate-order-change.ts +++ b/packages/modules/order/src/utils/calculate-order-change.ts @@ -164,7 +164,7 @@ export class OrderChangeProcessing { private processAction_( action: InternalOrderChangeEvent, isReplay = false - ): BigNumberInput | void { + ): void { const definedType = OrderChangeProcessing.typeDefinition[action.action] if (!isPresent(definedType)) { @@ -204,10 +204,11 @@ export class OrderChangeProcessing { action.amount = calculatedAmount ?? 0 } } - - return calculatedAmount } + /** + * Only used for order creation. + */ public getSummary(): OrderSummaryDTO { const summary = this.summary const orderSummary = { @@ -224,17 +225,17 @@ export class OrderChangeProcessing { return orderSummary } - // Returns the order summary from a calculated order including taxes + // Returns the order summary from a calculated order including taxes <- this is used for order preview flow public getSummaryFromOrder(order: OrderDTO): OrderSummaryDTO { const summary_ = this.summary const total = order.total - // const pendingDifference = MathBN.sub(total, summary_.transaction_total) + const pendingDifference = MathBN.sub(total, summary_.transaction_total) const orderSummary = { transaction_total: new BigNumber(summary_.transaction_total), original_order_total: new BigNumber(summary_.original_order_total), current_order_total: new BigNumber(total), - pending_difference: new BigNumber(summary_.pending_difference), + pending_difference: new BigNumber(pendingDifference), paid_total: new BigNumber(summary_.paid_total), refunded_total: new BigNumber(summary_.refunded_total), credit_line_total: new BigNumber(summary_.credit_line_total), @@ -272,7 +273,7 @@ export function calculateOrderChange({ return { instance: calc, - summary: calc.getSummary(), + summary: calc.getSummary(), // used for order creation, in other flows we call `getSummaryFromOrder` to get values from calculated totals getSummaryFromOrder: (order: OrderDTO) => calc.getSummaryFromOrder(order), order: calc.getCurrentOrder(), }