feat(dashboard,core-flows,types,utils,medusa): Order cancelations will refund payments (#10667)

* feat(order, types): Add Credit Line to order module

* chore: add action to inject credit lines

* WIP

* chore: add fixes + observe

* chore: fix balances

* chore: add canceled badge

* chore: fix i18n schema

* chore: remove redunddant query

* chore: add changeset

* chore: add credit lines for all cancel cases

* chore: add accounting total

* chore: address review & cleanup
This commit is contained in:
Riqwan Thamir
2025-01-07 07:56:28 +01:00
committed by GitHub
parent 99a06102a2
commit 47594192b7
27 changed files with 1241 additions and 1666 deletions

View File

@@ -11,7 +11,9 @@ OrderChangeProcessing.registerActionType(ChangeActionType.CREDIT_LINE_ADD, {
existing = {
id: action.reference_id!,
order_id: currentOrder.id,
amount: action.amount as number,
amount: action.amount!,
reference: action.reference,
reference_id: action.reference_id,
}
creditLines.push(existing)

View File

@@ -1,6 +1,7 @@
import { BigNumberInput, OrderSummaryDTO } from "@medusajs/framework/types"
import {
BigNumber,
ChangeActionType,
MathBN,
isPresent,
transformPropertiesToBigNumber,
@@ -63,8 +64,6 @@ export class OrderChangeProcessing {
MathBN.convert(0)
)
const currentOrderTotal = MathBN.add(this.order.total ?? 0, creditLineTotal)
for (const tr of transactions) {
if (MathBN.lt(tr.amount, 0)) {
refunded = MathBN.add(refunded, MathBN.abs(tr.amount))
@@ -79,12 +78,13 @@ export class OrderChangeProcessing {
this.summary = {
pending_difference: 0,
difference_sum: 0,
current_order_total: currentOrderTotal,
current_order_total: this.order.total ?? 0,
original_order_total: this.order.total ?? 0,
transaction_total: transactionTotal,
paid_total: paid,
refunded_total: refunded,
credit_line_total: creditLineTotal,
accounting_total: MathBN.sub(this.order.total ?? 0, creditLineTotal),
}
}
@@ -102,6 +102,11 @@ export class OrderChangeProcessing {
}
public processActions() {
let creditLineTotal = (this.order.credit_lines || []).reduce(
(acc, creditLine) => MathBN.add(acc, creditLine.amount),
MathBN.convert(0)
)
for (const action of this.actions) {
this.processAction_(action)
}
@@ -128,30 +133,37 @@ export class OrderChangeProcessing {
)
}
if (!this.isEventDone(action) && !action.change_id) {
summary.difference_sum = MathBN.add(summary.difference_sum, amount)
if (action.action === ChangeActionType.CREDIT_LINE_ADD) {
creditLineTotal = MathBN.add(creditLineTotal, amount)
} else {
if (!this.isEventDone(action) && !action.change_id) {
summary.difference_sum = MathBN.add(summary.difference_sum, amount)
}
summary.current_order_total = MathBN.add(
summary.current_order_total,
amount
)
}
const creditLineTotal = (this.order.credit_lines || []).reduce(
(acc, creditLine) => MathBN.add(acc, creditLine.amount),
MathBN.convert(0)
)
summary.credit_line_total = creditLineTotal
summary.current_order_total = MathBN.add(
summary.current_order_total,
amount
)
}
const groupSum = MathBN.add(...Object.values(this.groupTotal))
summary.difference_sum = MathBN.add(summary.difference_sum, groupSum)
summary.credit_line_total = creditLineTotal
summary.accounting_total = MathBN.sub(
summary.current_order_total,
creditLineTotal
)
summary.transaction_total = MathBN.sum(
...this.transactions.map((tr) => tr.amount)
)
summary.current_order_total = MathBN.sub(
summary.current_order_total,
creditLineTotal
)
summary.pending_difference = MathBN.sub(
summary.current_order_total,
summary.transaction_total
@@ -216,6 +228,7 @@ export class OrderChangeProcessing {
paid_total: new BigNumber(summary.paid_total),
refunded_total: new BigNumber(summary.refunded_total),
credit_line_total: new BigNumber(summary.credit_line_total),
accounting_total: new BigNumber(summary.accounting_total),
} as unknown as OrderSummaryDTO
return orderSummary