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:
@@ -51,22 +51,43 @@ describe("Action: Credit Line Add", function () {
|
||||
"credit_line_total": 0
|
||||
}
|
||||
|
||||
Upon adding a credit line, the order total and the pending difference will increase making it possible for the merchant
|
||||
to request the customer for a payment for an arbitrary reason, or prepare the order balance sheet to then allow
|
||||
the merchant to provide a refund.
|
||||
Upon adding a credit line, the current order total will decrease with the difference_sum going in
|
||||
the negatives making it possible for the merchant to balance the order to then enable a refund.
|
||||
|
||||
{
|
||||
"transaction_total": 0,
|
||||
"original_order_total": 30,
|
||||
"current_order_total": 60,
|
||||
"pending_difference": 60,
|
||||
"difference_sum": 30,
|
||||
"pending_difference": 0,
|
||||
"difference_sum": -30,
|
||||
"paid_total": 0,
|
||||
"refunded_total": 0,
|
||||
"credit_line_total": 30
|
||||
}
|
||||
*/
|
||||
it("should add credit lines", function () {
|
||||
const changesWithoutActions = calculateOrderChange({
|
||||
order: originalOrder,
|
||||
actions: [],
|
||||
options: { addActionReferenceToObject: true },
|
||||
})
|
||||
|
||||
const changesWithoutActionsJSON = JSON.parse(
|
||||
JSON.stringify(changesWithoutActions.summary)
|
||||
)
|
||||
|
||||
expect(changesWithoutActionsJSON).toEqual({
|
||||
transaction_total: 0,
|
||||
original_order_total: 30,
|
||||
current_order_total: 30,
|
||||
pending_difference: 30,
|
||||
difference_sum: 0,
|
||||
paid_total: 0,
|
||||
refunded_total: 0,
|
||||
credit_line_total: 0,
|
||||
accounting_total: 30,
|
||||
})
|
||||
|
||||
const actions = [
|
||||
{
|
||||
action: ChangeActionType.CREDIT_LINE_ADD,
|
||||
@@ -87,12 +108,13 @@ describe("Action: Credit Line Add", function () {
|
||||
expect(sumToJSON).toEqual({
|
||||
transaction_total: 0,
|
||||
original_order_total: 30,
|
||||
current_order_total: 60,
|
||||
pending_difference: 60,
|
||||
difference_sum: 30,
|
||||
current_order_total: 0,
|
||||
pending_difference: 0,
|
||||
difference_sum: 0,
|
||||
paid_total: 0,
|
||||
refunded_total: 0,
|
||||
credit_line_total: 30,
|
||||
accounting_total: 0,
|
||||
})
|
||||
|
||||
originalOrder.credit_lines.push({
|
||||
@@ -123,12 +145,13 @@ describe("Action: Credit Line Add", function () {
|
||||
expect(sumToJSONSecond).toEqual({
|
||||
transaction_total: 0,
|
||||
original_order_total: 30,
|
||||
current_order_total: 70,
|
||||
pending_difference: 70,
|
||||
difference_sum: 30,
|
||||
current_order_total: -10,
|
||||
pending_difference: -10,
|
||||
difference_sum: 0,
|
||||
paid_total: 0,
|
||||
refunded_total: 0,
|
||||
credit_line_total: 40,
|
||||
accounting_total: -10,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -123,6 +123,7 @@ describe("Order Exchange - Actions", function () {
|
||||
paid_total: 0,
|
||||
refunded_total: 0,
|
||||
credit_line_total: 0,
|
||||
accounting_total: 312.5,
|
||||
})
|
||||
|
||||
const toJson = JSON.parse(JSON.stringify(changes.order.items))
|
||||
|
||||
@@ -3059,6 +3059,7 @@ export default class OrderModuleService<
|
||||
transformPropertiesToBigNumber(trxs)
|
||||
|
||||
const op = isRemoved ? MathBN.sub : MathBN.add
|
||||
|
||||
for (const trx of trxs) {
|
||||
if (MathBN.gt(trx.amount, 0)) {
|
||||
summary.totals.paid_total = new BigNumber(
|
||||
|
||||
@@ -84,6 +84,7 @@ export interface OrderSummaryCalculated {
|
||||
paid_total: BigNumberInput
|
||||
refunded_total: BigNumberInput
|
||||
credit_line_total: BigNumberInput
|
||||
accounting_total: BigNumberInput
|
||||
}
|
||||
|
||||
export interface OrderTransaction {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user