fix(dashboard,order): preview pending diff summary (#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
This commit is contained in:
Frane Polić
2025-12-08 09:57:13 +01:00
committed by GitHub
parent fe49b567d6
commit 6176f93ac5
6 changed files with 54 additions and 20 deletions
+6
View File
@@ -0,0 +1,6 @@
---
"@medusajs/dashboard": patch
"@medusajs/order": patch
---
fix(dashboard, order): summary pending diff calculation on preview
@@ -482,7 +482,7 @@ medusaIntegrationTestRunner({
expect.objectContaining({ expect.objectContaining({
transaction_total: 0, transaction_total: 0,
current_order_total: 61, current_order_total: 61,
pending_difference: 11, pending_difference: 61, // item is not yet received
paid_total: 0, paid_total: 0,
refunded_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( result = await api.post(
`/admin/returns/${returnId}/request`, `/admin/returns/${returnId}/request`,
{}, {},
@@ -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 inboundShippingTotal = useMemo(() => {
const method = preview.shipping_methods.find( const method = preview.shipping_methods.find(
(sm) => (sm) =>
@@ -1010,10 +1020,7 @@ export const ClaimCreateForm = ({
{t("orders.claims.refundAmount")} {t("orders.claims.refundAmount")}
</span> </span>
<span className="txt-small font-medium"> <span className="txt-small font-medium">
{getStylizedAmount( {getStylizedAmount(estimatedDifference, order.currency_code)}
preview.summary.pending_difference,
order.currency_code
)}
</span> </span>
</div> </div>
</div> </div>
@@ -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 inboundShippingTotal = useMemo(() => {
const method = preview.shipping_methods.find( const method = preview.shipping_methods.find(
(sm) => (sm) =>
@@ -516,10 +526,7 @@ export const ExchangeCreateForm = ({
{t("orders.exchanges.refundAmount")} {t("orders.exchanges.refundAmount")}
</span> </span>
<span className="txt-small font-medium"> <span className="txt-small font-medium">
{getStylizedAmount( {getStylizedAmount(estimatedDifference, order.currency_code)}
preview.summary.pending_difference,
order.currency_code
)}
</span> </span>
</div> </div>
</div> </div>
@@ -402,6 +402,16 @@ export const ReturnCreateForm = ({
return method?.total || 0 return method?.total || 0
}, [preview.shipping_methods]) }, [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 ( return (
<RouteFocusModal.Form <RouteFocusModal.Form
form={form} form={form}
@@ -707,10 +717,7 @@ export const ReturnCreateForm = ({
{t("orders.returns.estDifference")} {t("orders.returns.estDifference")}
</span> </span>
<span className="txt-small font-medium"> <span className="txt-small font-medium">
{getStylizedAmount( {getStylizedAmount(estimatedDifference, order.currency_code)}
preview.summary.pending_difference,
order.currency_code
)}
</span> </span>
</div> </div>
</div> </div>
@@ -164,7 +164,7 @@ export class OrderChangeProcessing {
private processAction_( private processAction_(
action: InternalOrderChangeEvent, action: InternalOrderChangeEvent,
isReplay = false isReplay = false
): BigNumberInput | void { ): void {
const definedType = OrderChangeProcessing.typeDefinition[action.action] const definedType = OrderChangeProcessing.typeDefinition[action.action]
if (!isPresent(definedType)) { if (!isPresent(definedType)) {
@@ -204,10 +204,11 @@ export class OrderChangeProcessing {
action.amount = calculatedAmount ?? 0 action.amount = calculatedAmount ?? 0
} }
} }
return calculatedAmount
} }
/**
* Only used for order creation.
*/
public getSummary(): OrderSummaryDTO { public getSummary(): OrderSummaryDTO {
const summary = this.summary const summary = this.summary
const orderSummary = { const orderSummary = {
@@ -224,17 +225,17 @@ export class OrderChangeProcessing {
return orderSummary 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 { public getSummaryFromOrder(order: OrderDTO): OrderSummaryDTO {
const summary_ = this.summary const summary_ = this.summary
const total = order.total const total = order.total
// const pendingDifference = MathBN.sub(total, summary_.transaction_total) const pendingDifference = MathBN.sub(total, summary_.transaction_total)
const orderSummary = { const orderSummary = {
transaction_total: new BigNumber(summary_.transaction_total), transaction_total: new BigNumber(summary_.transaction_total),
original_order_total: new BigNumber(summary_.original_order_total), original_order_total: new BigNumber(summary_.original_order_total),
current_order_total: new BigNumber(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), paid_total: new BigNumber(summary_.paid_total),
refunded_total: new BigNumber(summary_.refunded_total), refunded_total: new BigNumber(summary_.refunded_total),
credit_line_total: new BigNumber(summary_.credit_line_total), credit_line_total: new BigNumber(summary_.credit_line_total),
@@ -272,7 +273,7 @@ export function calculateOrderChange({
return { return {
instance: calc, 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), getSummaryFromOrder: (order: OrderDTO) => calc.getSummaryFromOrder(order),
order: calc.getCurrentOrder(), order: calc.getCurrentOrder(),
} }