chore(promotion): big number calc (#7537)
This commit is contained in:
1 parent
61977bd392
commit
096372463e
12 files changed
+194
-148
No files matched your search
@@ -1,3 +1,5 @@
|
||||
import { BigNumberInput } from "../../totals"
|
||||
|
||||
/**
|
||||
* A compute action informs you what adjustment must be made to a cart item or shipping method.
|
||||
*/
|
||||
@@ -48,7 +50,7 @@ export interface AddItemAdjustmentAction {
|
||||
/**
|
||||
* The amount to remove off the item's total.
|
||||
*/
|
||||
amount: number
|
||||
amount: BigNumberInput
|
||||
|
||||
/**
|
||||
* The promotion's code.
|
||||
@@ -103,7 +105,7 @@ export interface AddShippingMethodAdjustment {
|
||||
/**
|
||||
* The amount to remove off the shipping method's total.
|
||||
*/
|
||||
amount: number
|
||||
amount: BigNumberInput
|
||||
|
||||
/**
|
||||
* The promotion's code.
|
||||
@@ -163,12 +165,12 @@ export interface ComputeActionItemLine extends Record<string, unknown> {
|
||||
/**
|
||||
* The quantity of the line item.
|
||||
*/
|
||||
quantity: number
|
||||
quantity: BigNumberInput
|
||||
|
||||
/**
|
||||
* The subtotal of the line item.
|
||||
*/
|
||||
subtotal: number
|
||||
subtotal: BigNumberInput
|
||||
|
||||
/**
|
||||
* The adjustments applied before on the line item.
|
||||
@@ -188,7 +190,7 @@ export interface ComputeActionShippingLine extends Record<string, unknown> {
|
||||
/**
|
||||
* The subtotal of the shipping method.
|
||||
*/
|
||||
subtotal: number
|
||||
subtotal: BigNumberInput
|
||||
|
||||
/**
|
||||
* The adjustments applied before on the shipping method.
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
import { BigNumberInput } from "@medusajs/types"
|
||||
import {
|
||||
ApplicationMethodAllocation,
|
||||
ApplicationMethodType,
|
||||
} from "../../promotion"
|
||||
import { MathBN } from "../math"
|
||||
|
||||
function getPromotionValueForPercentage(promotion, lineItemTotal) {
|
||||
return (promotion.value / 100) * lineItemTotal
|
||||
return MathBN.mult(MathBN.div(promotion.value, 100), lineItemTotal)
|
||||
}
|
||||
|
||||
function getPromotionValueForFixed(promotion, lineItemTotal, lineItemsTotal) {
|
||||
if (promotion.allocation === ApplicationMethodAllocation.ACROSS) {
|
||||
return (lineItemTotal / lineItemsTotal) * promotion.value
|
||||
return MathBN.mult(
|
||||
MathBN.div(lineItemTotal, lineItemsTotal),
|
||||
promotion.value
|
||||
)
|
||||
}
|
||||
|
||||
return promotion.value
|
||||
@@ -25,26 +30,26 @@ export function getPromotionValue(promotion, lineItemTotal, lineItemsTotal) {
|
||||
|
||||
export function getApplicableQuantity(lineItem, maxQuantity) {
|
||||
if (maxQuantity && lineItem.quantity) {
|
||||
return Math.min(lineItem.quantity, maxQuantity)
|
||||
return MathBN.min(lineItem.quantity, maxQuantity)
|
||||
}
|
||||
|
||||
return lineItem.quantity
|
||||
}
|
||||
|
||||
function getLineItemUnitPrice(lineItem) {
|
||||
return lineItem.subtotal / lineItem.quantity
|
||||
return MathBN.div(lineItem.subtotal, lineItem.quantity)
|
||||
}
|
||||
|
||||
export function calculateAdjustmentAmountFromPromotion(
|
||||
lineItem,
|
||||
promotion,
|
||||
lineItemsTotal = 0
|
||||
lineItemsTotal: BigNumberInput = 0
|
||||
) {
|
||||
const quantity = getApplicableQuantity(lineItem, promotion.max_quantity)
|
||||
const lineItemTotal = getLineItemUnitPrice(lineItem) * quantity
|
||||
const applicableTotal = lineItemTotal - promotion.applied_value
|
||||
const lineItemTotal = MathBN.mult(getLineItemUnitPrice(lineItem), quantity)
|
||||
const applicableTotal = MathBN.sub(lineItemTotal, promotion.applied_value)
|
||||
|
||||
if (applicableTotal <= 0) {
|
||||
if (MathBN.lte(applicableTotal, 0)) {
|
||||
return applicableTotal
|
||||
}
|
||||
|
||||
@@ -54,5 +59,5 @@ export function calculateAdjustmentAmountFromPromotion(
|
||||
lineItemsTotal
|
||||
)
|
||||
|
||||
return Math.min(promotionValue, applicableTotal)
|
||||
return MathBN.min(promotionValue, applicableTotal)
|
||||
}
|
||||
Reference in new issue
Block a user