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

View File

@@ -0,0 +1,6 @@
---
"@medusajs/dashboard": patch
"@medusajs/order": patch
---
fix(dashboard, order): summary pending diff calculation on preview

View File

@@ -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`,
{},

View File

@@ -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")}
</span>
<span className="txt-small font-medium">
{getStylizedAmount(
preview.summary.pending_difference,
order.currency_code
)}
{getStylizedAmount(estimatedDifference, order.currency_code)}
</span>
</div>
</div>

View File

@@ -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")}
</span>
<span className="txt-small font-medium">
{getStylizedAmount(
preview.summary.pending_difference,
order.currency_code
)}
{getStylizedAmount(estimatedDifference, order.currency_code)}
</span>
</div>
</div>

View File

@@ -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 (
<RouteFocusModal.Form
form={form}
@@ -707,10 +717,7 @@ export const ReturnCreateForm = ({
{t("orders.returns.estDifference")}
</span>
<span className="txt-small font-medium">
{getStylizedAmount(
preview.summary.pending_difference,
order.currency_code
)}
{getStylizedAmount(estimatedDifference, order.currency_code)}
</span>
</div>
</div>

View File

@@ -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(),
}