1 import { BigNumberInput } from "@medusajs/types"
2 import {
3 ApplicationMethodAllocation,
4 ApplicationMethodType,
5 } from "../../promotion"
6 import { MathBN } from "../math"
7 import { MEDUSA_EPSILON } from "../big-number"
8
9 function getPromotionValueForPercentage(promotion, lineItemAmount) {
10 return MathBN.mult(MathBN.div(promotion.value, 100), lineItemAmount)
11 }
12
13 function getPromotionValueForFixed(
14 promotion,
15 lineItemAmount,
16 lineItemsAmount,
17 lineItem
18 ) {
19 if (promotion.allocation === ApplicationMethodAllocation.ACROSS) {
20 const promotionValueForItem = MathBN.mult(
21 MathBN.div(lineItemAmount, lineItemsAmount),
22 promotion.value
23 )
24
25 if (MathBN.lte(promotionValueForItem, lineItemAmount)) {
26 return promotionValueForItem
27 }
28
29 const percentage = MathBN.div(
30 MathBN.mult(lineItemAmount, 100),
31 promotionValueForItem
32 )
33
34 return MathBN.mult(promotionValueForItem, MathBN.div(percentage, 100))
35 }
36
37 // For each allocation, promotion is applied in the scope of the line item.
38 // lineItemAmount will be the total applicable amount for the line item
39 // maximumPromotionAmount is the maximum amount that can be applied to the line item
40 // We need to return the minimum of the two
41 const maximumQuantity = MathBN.min(
42 lineItem.quantity,
43 promotion.max_quantity ?? MathBN.convert(1)
44 )
45
46 const maximumPromotionAmount = MathBN.mult(promotion.value, maximumQuantity)
47
48 return MathBN.min(maximumPromotionAmount, lineItemAmount)
49 }
50
51 export function getPromotionValue(
52 promotion,
53 lineItemAmount,
54 lineItemsAmount,
55 lineItem
56 ) {
57 if (promotion.type === ApplicationMethodType.PERCENTAGE) {
58 return getPromotionValueForPercentage(promotion, lineItemAmount)
59 }
60
61 return getPromotionValueForFixed(
62 promotion,
63 lineItemAmount,
64 lineItemsAmount,
65 lineItem
66 )
67 }
68
69 export function getApplicableQuantity(lineItem, maxQuantity) {
70 if (maxQuantity && lineItem.quantity) {
71 return MathBN.min(lineItem.quantity, maxQuantity)
72 }
73
74 return lineItem.quantity
75 }
76
77 function getLineItemSubtotal(lineItem) {
78 return MathBN.div(lineItem.subtotal, lineItem.quantity)
79 }
80
81 function getLineItemOriginalTotal(lineItem) {
82 return MathBN.div(lineItem.original_total, lineItem.quantity)
83 }
84
85 // Applied amounts are accumulated excl-tax; convert to incl-tax for tax-inclusive promotions before subtracting.
86 function getAppliedValueInPromotionBase(promotion, lineItem) {
87 if (!promotion.is_tax_inclusive) {
88 return promotion.applied_value
89 }
90
91 return MathBN.mult(
92 promotion.applied_value,
93 MathBN.div(lineItem.original_total, lineItem.subtotal)
94 )
95 }
96
97 export function calculateAdjustmentAmountFromPromotion(
98 lineItem,
99 promotion,
100 lineItemsAmount: BigNumberInput = 0
101 ) {
102 /*
103 For a promotion with an across allocation, we consider not only the line item total, but also the total of all other line items in the order.
104
105 We then distribute the promotion value proportionally across the line items based on the total of each line item.
106
107 For example, if the promotion is 100$, and the order total is 400$, and the items are:
108 item1: 250$
109 item2: 150$
110 total: 400$
111
112 The promotion value for the line items would be:
113 item1: 62.5$
114 item2: 37.5$
115 total: 100$
116
117 For the next 100$ promotion, we remove the applied promotions value from the line item total and redistribute the promotion value across the line items based on the updated totals.
118
119 Example:
120 item1: (250 - 62.5) = 187.5
121 item2: (150 - 37.5) = 112.5
122 total: 300
123
124 The promotion value for the line items would be:
125 item1: $62.5
126 item2: $37.5
127 total: 100$
128
129 */
130 if (promotion.allocation === ApplicationMethodAllocation.ACROSS) {
131 const quantity = getApplicableQuantity(lineItem, promotion.max_quantity)
132
133 const lineItemAmount = MathBN.mult(
134 promotion.is_tax_inclusive
135 ? getLineItemOriginalTotal(lineItem)
136 : getLineItemSubtotal(lineItem),
137 quantity
138 )
139 const applicableAmount = MathBN.sub(
140 lineItemAmount,
141 getAppliedValueInPromotionBase(promotion, lineItem)
142 )
143
144 if (MathBN.lte(applicableAmount, MEDUSA_EPSILON)) {
145 return MathBN.convert(0)
146 }
147
148 const promotionValue = getPromotionValue(
149 promotion,
150 applicableAmount,
151 lineItemsAmount,
152 lineItem
153 )
154
155 const returnValue = MathBN.min(promotionValue, applicableAmount)
156 if (MathBN.lte(returnValue, MEDUSA_EPSILON)) {
157 return MathBN.convert(0)
158 }
159
160 return returnValue
161 }
162
163 /*
164 For a promotion with an EACH allocation, we calculate the promotion value on the line item as a whole.
165
166 Example:
167 item1: {
168 subtotal: 200$,
169 unit_price: 50$,
170 quantity: 4,
171 }
172
173 When applying promotions, we need to consider 2 values:
174 1. What is the maximum promotion value?
175 2. What is the maximum promotion we can apply on the line item?
176
177 After applying each promotion, we reduce the maximum promotion that you can add to the line item by the value of the promotions applied.
178
179 We then apply whichever is lower.
180 */
181
182 const remainingItemAmount = MathBN.sub(
183 promotion.is_tax_inclusive ? lineItem.original_total : lineItem.subtotal,
184 getAppliedValueInPromotionBase(promotion, lineItem)
185 )
186
187 const itemAmount = MathBN.div(
188 promotion.is_tax_inclusive ? lineItem.original_total : lineItem.subtotal,
189 lineItem.quantity
190 )
191
192 const maximumPromotionAmount = MathBN.mult(
193 itemAmount,
194 promotion.max_quantity ?? MathBN.convert(1)
195 )
196
197 const applicableAmount = MathBN.min(
198 remainingItemAmount,
199 maximumPromotionAmount
200 )
201
202 if (MathBN.lte(applicableAmount, MEDUSA_EPSILON)) {
203 return MathBN.convert(0)
204 }
205
206 const promotionValue = getPromotionValue(
207 promotion,
208 applicableAmount,
209 lineItemsAmount,
210 lineItem
211 )
212
213 const returnValue = MathBN.min(promotionValue, applicableAmount)
214 if (MathBN.lte(returnValue, MEDUSA_EPSILON)) {
215 return MathBN.convert(0)
216 }
217
218 return returnValue
219 }