|
1
|
import { BigNumberInput, CartLikeWithTotals } from "@medusajs/types" |
|
2
|
import { defaultCurrencies } from "../../defaults/currencies" |
|
3
|
import { BigNumber, getEpsilonFromDecimalPrecision } from "../big-number" |
|
4
|
import { calculateCreditLinesTotal } from "../credit-lines" |
|
5
|
import { GetItemTotalInput, getLineItemsTotals } from "../line-item" |
|
6
|
import { MathBN } from "../math" |
|
7
|
import { |
|
8
|
getShippingMethodsTotals, |
|
9
|
GetShippingMethodTotalInput, |
|
10
|
} from "../shipping-method" |
|
11
|
import { transformPropertiesToBigNumber } from "../transform-properties-to-bignumber" |
|
12
|
|
|
13
|
interface TotalsConfig { |
|
14
|
includeTaxes?: boolean |
|
15
|
} |
|
16
|
|
|
17
|
export interface DecorateCartLikeInputDTO { |
|
18
|
currency_code?: string |
|
19
|
credit_lines?: { |
|
20
|
amount: BigNumberInput |
|
21
|
}[] |
|
22
|
items?: { |
|
23
|
id?: string |
|
24
|
unit_price: BigNumberInput |
|
25
|
is_tax_inclusive?: boolean |
|
26
|
quantity: BigNumberInput |
|
27
|
adjustments?: { amount: BigNumberInput; is_tax_inclusive?: boolean }[] |
|
28
|
tax_lines?: { |
|
29
|
rate: BigNumberInput |
|
30
|
}[] |
|
31
|
}[] |
|
32
|
shipping_methods?: { |
|
33
|
id?: string |
|
34
|
amount: BigNumberInput |
|
35
|
is_tax_inclusive?: boolean |
|
36
|
adjustments?: { amount: BigNumberInput }[] |
|
37
|
tax_lines?: { |
|
38
|
rate: BigNumberInput |
|
39
|
}[] |
|
40
|
}[] |
|
41
|
region?: { |
|
42
|
automatic_taxes?: boolean |
|
43
|
} |
|
44
|
} |
|
45
|
|
|
46
|
export function decorateCartTotals( |
|
47
|
cartLike: DecorateCartLikeInputDTO, |
|
48
|
config: TotalsConfig = {} |
|
49
|
): CartLikeWithTotals { |
|
50
|
transformPropertiesToBigNumber(cartLike) |
|
51
|
|
|
52
|
const optionalFields = { |
|
53
|
"detail.fulfilled_quantity": "fulfilled_total", |
|
54
|
"detail.shipped_quantity": "shipped_total", |
|
55
|
"detail.return_requested_quantity": "return_requested_total", |
|
56
|
"detail.return_received_quantity": "return_received_total", |
|
57
|
"detail.return_dismissed_quantity": "return_dismissed_total", |
|
58
|
"detail.written_off_quantity": "write_off_total", |
|
59
|
} |
|
60
|
|
|
61
|
const creditLines = cartLike.credit_lines ?? [] |
|
62
|
const items = (cartLike.items ?? []) as unknown as GetItemTotalInput[] |
|
63
|
const shippingMethods = (cartLike.shipping_methods ?? |
|
64
|
[]) as unknown as GetShippingMethodTotalInput[] |
|
65
|
|
|
66
|
const includeTax = config?.includeTaxes || cartLike.region?.automatic_taxes |
|
67
|
|
|
68
|
const itemsTotals = getLineItemsTotals(items, { |
|
69
|
includeTax, |
|
70
|
extraQuantityFields: optionalFields, |
|
71
|
}) |
|
72
|
|
|
73
|
const shippingMethodsTotals = getShippingMethodsTotals(shippingMethods, { |
|
74
|
includeTax, |
|
75
|
}) |
|
76
|
|
|
77
|
const extraTotals = {} |
|
78
|
|
|
79
|
// TODO: Remove this once we have a way to calculate the tax rate for credit lines |
|
80
|
const creditLinesSumTax = MathBN.convert(0) |
|
81
|
const creditLinesSumTaxRate = MathBN.div(creditLinesSumTax, 100) |
|
82
|
|
|
83
|
let subtotal = MathBN.convert(0) |
|
84
|
|
|
85
|
let discountTotal = MathBN.convert(0) |
|
86
|
let discountSubtotal = MathBN.convert(0) |
|
87
|
let discountTaxTotal = MathBN.convert(0) |
|
88
|
|
|
89
|
let itemsSubtotal = MathBN.convert(0) |
|
90
|
let itemsTotal = MathBN.convert(0) |
|
91
|
let itemsOriginalTotal = MathBN.convert(0) |
|
92
|
let itemsOriginalSubtotal = MathBN.convert(0) |
|
93
|
let itemsTaxTotal = MathBN.convert(0) |
|
94
|
let itemsOriginalTaxTotal = MathBN.convert(0) |
|
95
|
let itemsDiscountTotal = MathBN.convert(0) |
|
96
|
|
|
97
|
let shippingSubtotal = MathBN.convert(0) |
|
98
|
let shippingTotal = MathBN.convert(0) |
|
99
|
let shippingOriginalTotal = MathBN.convert(0) |
|
100
|
let shippingOriginalSubtotal = MathBN.convert(0) |
|
101
|
let shippingTaxTotal = MathBN.convert(0) |
|
102
|
let shippingOriginalTaxTotal = MathBN.convert(0) |
|
103
|
let shippingDiscountTotal = MathBN.convert(0) |
|
104
|
|
|
105
|
const cartItems = items.map((item, index) => { |
|
106
|
const rawTotals = itemsTotals[item.id ?? index] ?? {} |
|
107
|
const itemTotals = Object.assign(item, rawTotals) |
|
108
|
const itemSubtotal = itemTotals.subtotal |
|
109
|
|
|
110
|
const itemTotal = MathBN.convert(itemTotals.total) |
|
111
|
const itemOriginalTotal = MathBN.convert(itemTotals.original_total) |
|
112
|
|
|
113
|
const itemTaxTotal = MathBN.convert(itemTotals.tax_total) |
|
114
|
const itemOriginalTaxTotal = MathBN.convert(itemTotals.original_tax_total) |
|
115
|
|
|
116
|
const itemDiscountTotal = MathBN.convert(itemTotals.discount_total) |
|
117
|
const itemDiscountSubTotal = MathBN.convert(itemTotals.discount_subtotal) |
|
118
|
|
|
119
|
const itemDiscountTaxTotal = MathBN.convert(itemTotals.discount_tax_total) |
|
120
|
|
|
121
|
subtotal = MathBN.add(subtotal, itemSubtotal) |
|
122
|
|
|
123
|
discountTotal = MathBN.add(discountTotal, itemDiscountTotal) |
|
124
|
discountSubtotal = MathBN.add(discountSubtotal, itemDiscountSubTotal) |
|
125
|
discountTaxTotal = MathBN.add(discountTaxTotal, itemDiscountTaxTotal) |
|
126
|
|
|
127
|
itemsTotal = MathBN.add(itemsTotal, itemTotal) |
|
128
|
itemsOriginalTotal = MathBN.add(itemsOriginalTotal, itemOriginalTotal) |
|
129
|
itemsOriginalSubtotal = MathBN.add(itemsOriginalSubtotal, itemSubtotal) |
|
130
|
itemsSubtotal = MathBN.add(itemsSubtotal, itemSubtotal) |
|
131
|
itemsTaxTotal = MathBN.add(itemsTaxTotal, itemTaxTotal) |
|
132
|
itemsOriginalTaxTotal = MathBN.add( |
|
133
|
itemsOriginalTaxTotal, |
|
134
|
itemOriginalTaxTotal |
|
135
|
) |
|
136
|
itemsDiscountTotal = MathBN.add(itemsDiscountTotal, itemDiscountTotal) |
|
137
|
|
|
138
|
for (const key of Object.values(optionalFields)) { |
|
139
|
if (key in itemTotals) { |
|
140
|
extraTotals[key] ??= MathBN.convert(0) |
|
141
|
extraTotals[key] = MathBN.add(extraTotals[key], itemTotals[key] ?? 0) |
|
142
|
} |
|
143
|
} |
|
144
|
|
|
145
|
return itemTotals |
|
146
|
}) |
|
147
|
|
|
148
|
const cartShippingMethods = shippingMethods.map((shippingMethod, index) => { |
|
149
|
const shippingMethodTotals = Object.assign( |
|
150
|
shippingMethod, |
|
151
|
shippingMethodsTotals[shippingMethod.id ?? index] ?? {} |
|
152
|
) |
|
153
|
|
|
154
|
subtotal = MathBN.add(subtotal, shippingMethodTotals.subtotal) |
|
155
|
|
|
156
|
discountTotal = MathBN.add( |
|
157
|
discountTotal, |
|
158
|
shippingMethodTotals.discount_total |
|
159
|
) |
|
160
|
discountSubtotal = MathBN.add( |
|
161
|
discountSubtotal, |
|
162
|
shippingMethodTotals.discount_subtotal |
|
163
|
) |
|
164
|
discountTaxTotal = MathBN.add( |
|
165
|
discountTaxTotal, |
|
166
|
shippingMethodTotals.discount_tax_total |
|
167
|
) |
|
168
|
|
|
169
|
shippingSubtotal = MathBN.add( |
|
170
|
shippingSubtotal, |
|
171
|
shippingMethodTotals.subtotal |
|
172
|
) |
|
173
|
shippingTotal = MathBN.add(shippingTotal, shippingMethodTotals.total) |
|
174
|
shippingOriginalTotal = MathBN.add( |
|
175
|
shippingOriginalTotal, |
|
176
|
shippingMethodTotals.original_total |
|
177
|
) |
|
178
|
shippingOriginalSubtotal = MathBN.add( |
|
179
|
shippingOriginalSubtotal, |
|
180
|
shippingMethodTotals.subtotal |
|
181
|
) |
|
182
|
shippingTaxTotal = MathBN.add( |
|
183
|
shippingTaxTotal, |
|
184
|
shippingMethodTotals.tax_total |
|
185
|
) |
|
186
|
shippingOriginalTaxTotal = MathBN.add( |
|
187
|
shippingOriginalTaxTotal, |
|
188
|
shippingMethodTotals.original_tax_total |
|
189
|
) |
|
190
|
shippingDiscountTotal = MathBN.add( |
|
191
|
shippingDiscountTotal, |
|
192
|
shippingMethodTotals.discount_total |
|
193
|
) |
|
194
|
|
|
195
|
return shippingMethodTotals |
|
196
|
}) |
|
197
|
|
|
198
|
const { creditLinesTotal, creditLinesSubtotal, creditLinesTaxTotal } = |
|
199
|
calculateCreditLinesTotal({ |
|
200
|
creditLines, |
|
201
|
includesTax: false, |
|
202
|
taxRate: creditLinesSumTaxRate, |
|
203
|
currencyCode: cartLike.currency_code, |
|
204
|
}) |
|
205
|
|
|
206
|
const taxTotal = MathBN.add(itemsTaxTotal, shippingTaxTotal) |
|
207
|
|
|
208
|
const originalTaxTotal = MathBN.add( |
|
209
|
itemsOriginalTaxTotal, |
|
210
|
shippingOriginalTaxTotal |
|
211
|
) |
|
212
|
|
|
213
|
// TODO: Gift Card calculations |
|
214
|
const originalTotal = MathBN.add(itemsOriginalTotal, shippingOriginalTotal) |
|
215
|
const originalSubtotal = MathBN.add( |
|
216
|
itemsOriginalSubtotal, |
|
217
|
shippingOriginalSubtotal |
|
218
|
) |
|
219
|
|
|
220
|
const tempTotal = MathBN.add(subtotal, taxTotal) |
|
221
|
const total = MathBN.sub(tempTotal, discountSubtotal, creditLinesTotal) |
|
222
|
|
|
223
|
const cart = cartLike as any |
|
224
|
|
|
225
|
cart.total = new BigNumber(total) |
|
226
|
cart.subtotal = new BigNumber(subtotal) |
|
227
|
cart.tax_total = new BigNumber(taxTotal) |
|
228
|
|
|
229
|
cart.discount_total = new BigNumber(discountTotal) |
|
230
|
cart.discount_subtotal = new BigNumber(discountSubtotal) |
|
231
|
cart.discount_tax_total = new BigNumber(discountTaxTotal) |
|
232
|
|
|
233
|
cart.credit_line_total = new BigNumber(creditLinesTotal) |
|
234
|
cart.credit_line_subtotal = new BigNumber(creditLinesSubtotal) |
|
235
|
cart.credit_line_tax_total = new BigNumber(creditLinesTaxTotal) |
|
236
|
|
|
237
|
// cart.gift_card_total = giftCardTotal.total || 0 |
|
238
|
// cart.gift_card_tax_total = giftCardTotal.tax_total || 0 |
|
239
|
|
|
240
|
cart.original_total = new BigNumber(originalTotal) |
|
241
|
cart.original_subtotal = new BigNumber(originalSubtotal) |
|
242
|
cart.original_tax_total = new BigNumber(originalTaxTotal) |
|
243
|
|
|
244
|
// cart.original_gift_card_total = |
|
245
|
// cart.original_gift_card_tax_total = |
|
246
|
|
|
247
|
if (cartLike.items) { |
|
248
|
cart.items = cartItems |
|
249
|
cart.item_total = new BigNumber(itemsTotal) |
|
250
|
cart.item_subtotal = new BigNumber(itemsSubtotal) |
|
251
|
cart.item_tax_total = new BigNumber(itemsTaxTotal) |
|
252
|
cart.item_discount_total = new BigNumber(itemsDiscountTotal) |
|
253
|
|
|
254
|
cart.original_item_total = new BigNumber(itemsOriginalTotal) |
|
255
|
cart.original_item_subtotal = new BigNumber(itemsOriginalSubtotal) |
|
256
|
cart.original_item_tax_total = new BigNumber(itemsOriginalTaxTotal) |
|
257
|
|
|
258
|
for (const key of Object.keys(extraTotals)) { |
|
259
|
cart[key] = new BigNumber(extraTotals[key]) |
|
260
|
} |
|
261
|
} |
|
262
|
|
|
263
|
if (cart.shipping_methods) { |
|
264
|
cart.shipping_methods = cartShippingMethods |
|
265
|
cart.shipping_total = new BigNumber(shippingTotal) |
|
266
|
cart.shipping_subtotal = new BigNumber(shippingSubtotal) |
|
267
|
cart.shipping_tax_total = new BigNumber(shippingTaxTotal) |
|
268
|
cart.shipping_discount_total = new BigNumber(shippingDiscountTotal) |
|
269
|
|
|
270
|
cart.original_shipping_tax_total = new BigNumber(shippingOriginalTaxTotal) |
|
271
|
cart.original_shipping_subtotal = new BigNumber(shippingOriginalSubtotal) |
|
272
|
cart.original_shipping_total = new BigNumber(shippingOriginalTotal) |
|
273
|
} |
|
274
|
|
|
275
|
// Calculate pending return total |
|
276
|
if (cart.summary) { |
|
277
|
const pendingReturnTotal = MathBN.sum( |
|
278
|
0, |
|
279
|
...(cart.items?.map((item) => item.return_requested_total ?? 0) ?? [0]) |
|
280
|
) |
|
281
|
|
|
282
|
const upperCurCode = cart.currency_code?.toUpperCase() as string |
|
283
|
const currencyEpsilon = getEpsilonFromDecimalPrecision( |
|
284
|
defaultCurrencies[upperCurCode]?.decimal_digits |
|
285
|
) |
|
286
|
|
|
287
|
const pendingDifference = new BigNumber( |
|
288
|
MathBN.sub( |
|
289
|
MathBN.sub(cart.total, pendingReturnTotal), |
|
290
|
cart.summary?.transaction_total ?? 0 |
|
291
|
) |
|
292
|
) |
|
293
|
|
|
294
|
cart.summary.pending_difference = MathBN.lte( |
|
295
|
MathBN.abs(pendingDifference), |
|
296
|
currencyEpsilon |
|
297
|
) |
|
298
|
? MathBN.convert(0) |
|
299
|
: pendingDifference |
|
300
|
} |
|
301
|
|
|
302
|
return cart |
|
303
|
} |