Skip to content

Commit 6176f93

Browse files
authored
fix(dashboard,order): preview pending diff summary (medusajs#14221)
* wip: preview pending diff summary * fix: return test * fix: rm return calc from processAction_ * chore: cleanup * chore: changeset * feat: add estimated diff for return,claim,exchange forms * chore: changeset
1 parent fe49b56 commit 6176f93

File tree

6 files changed

+54
-20
lines changed

6 files changed

+54
-20
lines changed

.changeset/chilly-zoos-beg.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@medusajs/dashboard": patch
3+
"@medusajs/order": patch
4+
---
5+
6+
fix(dashboard, order): summary pending diff calculation on preview

integration-tests/http/__tests__/returns/returns.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ medusaIntegrationTestRunner({
482482
expect.objectContaining({
483483
transaction_total: 0,
484484
current_order_total: 61,
485-
pending_difference: 11,
485+
pending_difference: 61, // item is not yet received
486486
paid_total: 0,
487487
refunded_total: 0,
488488
})
@@ -710,6 +710,12 @@ medusaIntegrationTestRunner({
710710
})
711711
)
712712

713+
expect(result.data.order_preview.summary).toEqual(
714+
expect.objectContaining({
715+
pending_difference: 61 + 1002, // original total + newly added shipping
716+
})
717+
)
718+
713719
result = await api.post(
714720
`/admin/returns/${returnId}/request`,
715721
{},

packages/admin/dashboard/src/routes/orders/order-create-claim/components/claim-create-form/claim-create-form.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,16 @@ export const ClaimCreateForm = ({
568568
}
569569
}, [])
570570

571+
/**
572+
* For estimated difference show pending difference and subtract the total of inbound items (assume all items will be returned correctly)
573+
* We don't include inbound total in the pending difference because it will be considered returned when the receive flow is completed
574+
*/
575+
const estimatedDifference =
576+
preview.summary.pending_difference -
577+
inboundPreviewItems.reduce((acc, item) => {
578+
return acc + item.total
579+
}, 0)
580+
571581
const inboundShippingTotal = useMemo(() => {
572582
const method = preview.shipping_methods.find(
573583
(sm) =>
@@ -1010,10 +1020,7 @@ export const ClaimCreateForm = ({
10101020
{t("orders.claims.refundAmount")}
10111021
</span>
10121022
<span className="txt-small font-medium">
1013-
{getStylizedAmount(
1014-
preview.summary.pending_difference,
1015-
order.currency_code
1016-
)}
1023+
{getStylizedAmount(estimatedDifference, order.currency_code)}
10171024
</span>
10181025
</div>
10191026
</div>

packages/admin/dashboard/src/routes/orders/order-create-exchange/components/exchange-create-form/exchange-create-form.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,16 @@ export const ExchangeCreateForm = ({
272272
}
273273
}, [])
274274

275+
/**
276+
* For estimated difference show pending difference and subtract the total of inbound items (assume all items will be returned correctly)
277+
* We don't include inbound total in the pending difference because it will be considered returned when the receive flow is completed
278+
*/
279+
const estimatedDifference =
280+
preview.summary.pending_difference -
281+
inboundPreviewItems.reduce((acc, item) => {
282+
return acc + item.total
283+
}, 0)
284+
275285
const inboundShippingTotal = useMemo(() => {
276286
const method = preview.shipping_methods.find(
277287
(sm) =>
@@ -516,10 +526,7 @@ export const ExchangeCreateForm = ({
516526
{t("orders.exchanges.refundAmount")}
517527
</span>
518528
<span className="txt-small font-medium">
519-
{getStylizedAmount(
520-
preview.summary.pending_difference,
521-
order.currency_code
522-
)}
529+
{getStylizedAmount(estimatedDifference, order.currency_code)}
523530
</span>
524531
</div>
525532
</div>

packages/admin/dashboard/src/routes/orders/order-create-return/components/return-create-form/return-create-form.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,16 @@ export const ReturnCreateForm = ({
402402
return method?.total || 0
403403
}, [preview.shipping_methods])
404404

405+
/**
406+
* For estimated difference show pending difference and subtract the total of inbound items (assume all items will be returned correctly)
407+
* We don't include inbound total in the pending difference because it will be considered returned when the receive flow is completed
408+
*/
409+
const estimatedDifference =
410+
preview.summary.pending_difference -
411+
previewItems.reduce((acc, item) => {
412+
return acc + item.total
413+
}, 0)
414+
405415
return (
406416
<RouteFocusModal.Form
407417
form={form}
@@ -707,10 +717,7 @@ export const ReturnCreateForm = ({
707717
{t("orders.returns.estDifference")}
708718
</span>
709719
<span className="txt-small font-medium">
710-
{getStylizedAmount(
711-
preview.summary.pending_difference,
712-
order.currency_code
713-
)}
720+
{getStylizedAmount(estimatedDifference, order.currency_code)}
714721
</span>
715722
</div>
716723
</div>

packages/modules/order/src/utils/calculate-order-change.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export class OrderChangeProcessing {
164164
private processAction_(
165165
action: InternalOrderChangeEvent,
166166
isReplay = false
167-
): BigNumberInput | void {
167+
): void {
168168
const definedType = OrderChangeProcessing.typeDefinition[action.action]
169169

170170
if (!isPresent(definedType)) {
@@ -204,10 +204,11 @@ export class OrderChangeProcessing {
204204
action.amount = calculatedAmount ?? 0
205205
}
206206
}
207-
208-
return calculatedAmount
209207
}
210208

209+
/**
210+
* Only used for order creation.
211+
*/
211212
public getSummary(): OrderSummaryDTO {
212213
const summary = this.summary
213214
const orderSummary = {
@@ -224,17 +225,17 @@ export class OrderChangeProcessing {
224225
return orderSummary
225226
}
226227

227-
// Returns the order summary from a calculated order including taxes
228+
// Returns the order summary from a calculated order including taxes <- this is used for order preview flow
228229
public getSummaryFromOrder(order: OrderDTO): OrderSummaryDTO {
229230
const summary_ = this.summary
230231
const total = order.total
231-
// const pendingDifference = MathBN.sub(total, summary_.transaction_total)
232+
const pendingDifference = MathBN.sub(total, summary_.transaction_total)
232233

233234
const orderSummary = {
234235
transaction_total: new BigNumber(summary_.transaction_total),
235236
original_order_total: new BigNumber(summary_.original_order_total),
236237
current_order_total: new BigNumber(total),
237-
pending_difference: new BigNumber(summary_.pending_difference),
238+
pending_difference: new BigNumber(pendingDifference),
238239
paid_total: new BigNumber(summary_.paid_total),
239240
refunded_total: new BigNumber(summary_.refunded_total),
240241
credit_line_total: new BigNumber(summary_.credit_line_total),
@@ -272,7 +273,7 @@ export function calculateOrderChange({
272273

273274
return {
274275
instance: calc,
275-
summary: calc.getSummary(),
276+
summary: calc.getSummary(), // used for order creation, in other flows we call `getSummaryFromOrder` to get values from calculated totals
276277
getSummaryFromOrder: (order: OrderDTO) => calc.getSummaryFromOrder(order),
277278
order: calc.getCurrentOrder(),
278279
}

0 commit comments

Comments
 (0)