From 7602fe8b61d5ec65d92c81446c4a344d0a652032 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Thu, 27 Jun 2024 13:56:55 -0300 Subject: [PATCH] chore: totals refundable_total (#7857) --- packages/core/types/src/http/order/common.ts | 2 + packages/core/types/src/order/common.ts | 21 +++++++++ .../core/utils/src/totals/__tests__/totals.ts | 10 +++-- .../core/utils/src/totals/line-item/index.ts | 44 ++++++++++++++++++- 4 files changed, 72 insertions(+), 5 deletions(-) diff --git a/packages/core/types/src/http/order/common.ts b/packages/core/types/src/http/order/common.ts index f3f6b1b34f..8f3467a67d 100644 --- a/packages/core/types/src/http/order/common.ts +++ b/packages/core/types/src/http/order/common.ts @@ -158,6 +158,8 @@ export interface BaseOrderLineItem { tax_total: number discount_total: number discount_tax_total: number + refundable_total: number + refundable_total_per_unit: number } export interface BaseOrderItemDetail { diff --git a/packages/core/types/src/order/common.ts b/packages/core/types/src/order/common.ts index edd5326b6e..0b1c9674f4 100644 --- a/packages/core/types/src/order/common.ts +++ b/packages/core/types/src/order/common.ts @@ -477,6 +477,17 @@ export interface OrderLineItemTotalsDTO { */ discount_tax_total: BigNumberValue + /** + * The refundable total of the order line item. + */ + refundable_total: BigNumberValue + + /** + * The refundable total per unit of the order line item. + */ + + refundable_total_per_unit: BigNumberValue + /** * The raw original total of the order line item. */ @@ -531,6 +542,16 @@ export interface OrderLineItemTotalsDTO { * The raw discount tax total of the order line item. */ raw_discount_tax_total: BigNumberRawValue + + /** + * The raw refundable total of the order line item.. + */ + raw_refundable_total: BigNumberRawValue + + /** + * The raw refundable total per unit of the order line item. + */ + raw_refundable_total_per_unit: BigNumberRawValue } export interface OrderLineItemDTO extends OrderLineItemTotalsDTO { diff --git a/packages/core/utils/src/totals/__tests__/totals.ts b/packages/core/utils/src/totals/__tests__/totals.ts index 8b19688fb4..8d8ea02e36 100644 --- a/packages/core/utils/src/totals/__tests__/totals.ts +++ b/packages/core/utils/src/totals/__tests__/totals.ts @@ -643,7 +643,7 @@ describe("Total calculation", function () { quantity: 2, fulfilled_quantity: 2, shipped_quantity: 2, - return_requested_quantity: 2, + return_requested_quantity: 0, return_received_quantity: 1, return_dismissed_quantity: 1, written_off_quantity: 0, @@ -670,7 +670,7 @@ describe("Total calculation", function () { quantity: 2, fulfilled_quantity: 2, shipped_quantity: 2, - return_requested_quantity: 2, + return_requested_quantity: 0, return_received_quantity: 1, return_dismissed_quantity: 1, written_off_quantity: 0, @@ -697,10 +697,12 @@ describe("Total calculation", function () { original_tax_total: 10, fulfilled_total: 88, shipped_total: 88, - return_requested_total: 88, + return_requested_total: 0, return_received_total: 44, return_dismissed_total: 44, write_off_total: 0, + refundable_total: 0, + refundable_total_per_unit: 0, }, ], total: 88, @@ -718,7 +720,7 @@ describe("Total calculation", function () { original_item_tax_total: 10, fulfilled_total: 88, shipped_total: 88, - return_requested_total: 88, + return_requested_total: 0, return_received_total: 44, return_dismissed_total: 44, write_off_total: 0, diff --git a/packages/core/utils/src/totals/line-item/index.ts b/packages/core/utils/src/totals/line-item/index.ts index b45faa7a13..8b9f579e16 100644 --- a/packages/core/utils/src/totals/line-item/index.ts +++ b/packages/core/utils/src/totals/line-item/index.ts @@ -1,4 +1,5 @@ -import { AdjustmentLineDTO, TaxLineDTO } from "@medusajs/types" +import { AdjustmentLineDTO, BigNumberInput, TaxLineDTO } from "@medusajs/types" +import { isDefined } from "../../common" import { calculateAdjustmentTotal } from "../adjustment" import { BigNumber } from "../big-number" import { MathBN } from "../math" @@ -37,6 +38,9 @@ export interface GetItemTotalOutput { discount_total: BigNumber discount_tax_total: BigNumber + refundable_total?: BigNumber + refundable_total_per_unit?: BigNumber + tax_total: BigNumber original_tax_total: BigNumber @@ -66,6 +70,40 @@ export function getLineItemsTotals( return itemsTotals } +function setRefundableTotal( + item: GetItemTotalInput, + discountTotal: BigNumberInput, + totals: GetItemTotalOutput, + context: GetLineItemsTotalsContext +) { + const totalReturnedQuantity = MathBN.sum( + item.return_requested_quantity ?? 0, + item.return_received_quantity ?? 0, + item.return_dismissed_quantity ?? 0 + ) + const currentQuantity = MathBN.sub(item.quantity, totalReturnedQuantity) + const discountPerUnit = MathBN.div(discountTotal, item.quantity) + + const refundableSubTotal = MathBN.sub( + MathBN.mult(currentQuantity, item.unit_price), + MathBN.mult(currentQuantity, discountPerUnit) + ) + + const taxTotal = calculateTaxTotal({ + taxLines: item.tax_lines || [], + includesTax: context.includeTax, + taxableAmount: refundableSubTotal, + }) + const refundableTotal = MathBN.add(refundableSubTotal, taxTotal) + + totals.refundable_total_per_unit = new BigNumber( + MathBN.eq(currentQuantity, 0) + ? 0 + : MathBN.div(refundableTotal, currentQuantity) + ) + totals.refundable_total = new BigNumber(refundableTotal) +} + function getLineItemTotals( item: GetItemTotalInput, context: GetLineItemsTotalsContext @@ -114,6 +152,10 @@ function getLineItemTotals( }) totals.tax_total = new BigNumber(taxTotal) + if (isDefined(item.return_requested_quantity)) { + setRefundableTotal(item, discountTotal, totals, context) + } + const originalTaxTotal = calculateTaxTotal({ taxLines: item.tax_lines || [], includesTax: context.includeTax,