chore: totals refundable_total (#7857)

This commit is contained in:
Carlos R. L. Rodrigues
2024-06-27 13:56:55 -03:00
committed by GitHub
parent 91098877c2
commit 7602fe8b61
4 changed files with 72 additions and 5 deletions
@@ -158,6 +158,8 @@ export interface BaseOrderLineItem {
tax_total: number tax_total: number
discount_total: number discount_total: number
discount_tax_total: number discount_tax_total: number
refundable_total: number
refundable_total_per_unit: number
} }
export interface BaseOrderItemDetail { export interface BaseOrderItemDetail {
+21
View File
@@ -477,6 +477,17 @@ export interface OrderLineItemTotalsDTO {
*/ */
discount_tax_total: BigNumberValue 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. * 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. * The raw discount tax total of the order line item.
*/ */
raw_discount_tax_total: BigNumberRawValue 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 { export interface OrderLineItemDTO extends OrderLineItemTotalsDTO {
@@ -643,7 +643,7 @@ describe("Total calculation", function () {
quantity: 2, quantity: 2,
fulfilled_quantity: 2, fulfilled_quantity: 2,
shipped_quantity: 2, shipped_quantity: 2,
return_requested_quantity: 2, return_requested_quantity: 0,
return_received_quantity: 1, return_received_quantity: 1,
return_dismissed_quantity: 1, return_dismissed_quantity: 1,
written_off_quantity: 0, written_off_quantity: 0,
@@ -670,7 +670,7 @@ describe("Total calculation", function () {
quantity: 2, quantity: 2,
fulfilled_quantity: 2, fulfilled_quantity: 2,
shipped_quantity: 2, shipped_quantity: 2,
return_requested_quantity: 2, return_requested_quantity: 0,
return_received_quantity: 1, return_received_quantity: 1,
return_dismissed_quantity: 1, return_dismissed_quantity: 1,
written_off_quantity: 0, written_off_quantity: 0,
@@ -697,10 +697,12 @@ describe("Total calculation", function () {
original_tax_total: 10, original_tax_total: 10,
fulfilled_total: 88, fulfilled_total: 88,
shipped_total: 88, shipped_total: 88,
return_requested_total: 88, return_requested_total: 0,
return_received_total: 44, return_received_total: 44,
return_dismissed_total: 44, return_dismissed_total: 44,
write_off_total: 0, write_off_total: 0,
refundable_total: 0,
refundable_total_per_unit: 0,
}, },
], ],
total: 88, total: 88,
@@ -718,7 +720,7 @@ describe("Total calculation", function () {
original_item_tax_total: 10, original_item_tax_total: 10,
fulfilled_total: 88, fulfilled_total: 88,
shipped_total: 88, shipped_total: 88,
return_requested_total: 88, return_requested_total: 0,
return_received_total: 44, return_received_total: 44,
return_dismissed_total: 44, return_dismissed_total: 44,
write_off_total: 0, write_off_total: 0,
@@ -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 { calculateAdjustmentTotal } from "../adjustment"
import { BigNumber } from "../big-number" import { BigNumber } from "../big-number"
import { MathBN } from "../math" import { MathBN } from "../math"
@@ -37,6 +38,9 @@ export interface GetItemTotalOutput {
discount_total: BigNumber discount_total: BigNumber
discount_tax_total: BigNumber discount_tax_total: BigNumber
refundable_total?: BigNumber
refundable_total_per_unit?: BigNumber
tax_total: BigNumber tax_total: BigNumber
original_tax_total: BigNumber original_tax_total: BigNumber
@@ -66,6 +70,40 @@ export function getLineItemsTotals(
return itemsTotals 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( function getLineItemTotals(
item: GetItemTotalInput, item: GetItemTotalInput,
context: GetLineItemsTotalsContext context: GetLineItemsTotalsContext
@@ -114,6 +152,10 @@ function getLineItemTotals(
}) })
totals.tax_total = new BigNumber(taxTotal) totals.tax_total = new BigNumber(taxTotal)
if (isDefined(item.return_requested_quantity)) {
setRefundableTotal(item, discountTotal, totals, context)
}
const originalTaxTotal = calculateTaxTotal({ const originalTaxTotal = calculateTaxTotal({
taxLines: item.tax_lines || [], taxLines: item.tax_lines || [],
includesTax: context.includeTax, includesTax: context.includeTax,