1 import { AdjustmentLineDTO, TaxLineDTO } from "@medusajs/types"
2 import { calculateAdjustmentTotal } from "../adjustment"
3 import { BigNumber } from "../big-number"
4 import { MathBN } from "../math"
5 import { calculateTaxTotal } from "../tax"
6
7 interface GetShippingMethodsTotalsContext {
8 includeTax?: boolean
9 }
10
11 export interface GetShippingMethodTotalInput {
12 id: string
13 amount: BigNumber
14 is_tax_inclusive?: boolean
15 tax_lines?: TaxLineDTO[]
16 adjustments?: Pick<AdjustmentLineDTO, "amount">[]
17 }
18
19 export interface GetShippingMethodTotalOutput {
20 amount: BigNumber
21
22 subtotal: BigNumber
23 original_subtotal: BigNumber
24
25 total: BigNumber
26 original_total: BigNumber
27
28 discount_total: BigNumber
29 discount_subtotal: BigNumber
30 discount_tax_total: BigNumber
31
32 tax_total: BigNumber
33 original_tax_total: BigNumber
34 }
35
36 export function getShippingMethodsTotals(
37 shippingMethods: GetShippingMethodTotalInput[],
38 context: GetShippingMethodsTotalsContext
39 ): Record<string, GetShippingMethodTotalOutput> {
40 const shippingMethodsTotals = {}
41
42 let index = 0
43 for (const shippingMethod of shippingMethods) {
44 shippingMethodsTotals[shippingMethod.id ?? index] = getShippingMethodTotals(
45 shippingMethod,
46 context
47 )
48 index++
49 }
50
51 return shippingMethodsTotals
52 }
53
54 export function getShippingMethodTotals(
55 shippingMethod: GetShippingMethodTotalInput,
56 context: GetShippingMethodsTotalsContext
57 ) {
58 const isTaxInclusive = shippingMethod.is_tax_inclusive ?? context.includeTax
59
60 const shippingMethodAmount = MathBN.convert(shippingMethod.amount)
61 const sumTax = MathBN.sum(
62 ...(shippingMethod.tax_lines?.map((taxLine) => taxLine.rate) ?? [])
63 )
64 const sumTaxRate = MathBN.div(sumTax, 100)
65
66 const subtotal = isTaxInclusive
67 ? MathBN.div(shippingMethodAmount, MathBN.add(1, sumTaxRate))
68 : shippingMethodAmount
69
70 const {
71 adjustmentsTotal: discountsTotal,
72 adjustmentsSubtotal: discountsSubtotal,
73 adjustmentsTaxTotal: discountsTaxTotal,
74 } = calculateAdjustmentTotal({
75 adjustments: shippingMethod.adjustments || [],
76 taxRate: sumTaxRate,
77 })
78
79 const taxLines = shippingMethod.tax_lines || []
80
81 const taxTotal = calculateTaxTotal({
82 taxLines,
83 taxableAmount: MathBN.sub(subtotal, discountsSubtotal),
84 setTotalField: "total",
85 })
86
87 const originalTaxTotal = calculateTaxTotal({
88 taxLines,
89 taxableAmount: subtotal,
90 setTotalField: "subtotal",
91 })
92
93 const totals: GetShippingMethodTotalOutput = {
94 amount: new BigNumber(shippingMethodAmount),
95
96 subtotal: new BigNumber(subtotal),
97 total: new BigNumber(
98 MathBN.sum(MathBN.sub(subtotal, discountsSubtotal), taxTotal)
99 ),
100 original_subtotal: new BigNumber(
101 MathBN.sub(
102 isTaxInclusive
103 ? shippingMethodAmount
104 : MathBN.add(subtotal, originalTaxTotal),
105 originalTaxTotal
106 )
107 ),
108 original_total: new BigNumber(
109 isTaxInclusive
110 ? shippingMethodAmount
111 : MathBN.add(subtotal, originalTaxTotal)
112 ),
113
114 discount_total: new BigNumber(discountsTotal),
115 discount_subtotal: new BigNumber(discountsSubtotal),
116 discount_tax_total: new BigNumber(discountsTaxTotal),
117
118 tax_total: new BigNumber(taxTotal),
119 original_tax_total: new BigNumber(originalTaxTotal),
120 }
121
122 return totals
123 }