File size: 1,309 Bytes
16f1328
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// File: tasks/pricing/source.js
// Source JavaScript code for the pricing migration task.

const TAX_RATES_BPS = {
    "US-CA": 875,
    "US-TX": 625,
    "US-NY": 800,
    UK: 2000,
};

function taxRateBps(regionId) {
    return TAX_RATES_BPS[regionId] ?? 0;
}

function subtotal(order) {
    return order.items.reduce(
        (total, item) => total + item.unitPrice * item.quantity,
        0,
    );
}

function couponDiscount(order) {
    const sub = subtotal(order);
    const rawDiscount = order.coupons.reduce(
        (total, coupon) => total + Math.floor((sub * coupon.discountPercent) / 100),
        0,
    );
    return Math.min(rawDiscount, Math.floor(sub / 2));
}

function loyaltyDiscount(order) {
    const sub = subtotal(order);
    return Math.min(order.loyaltyPoints, Math.floor(sub / 10));
}

function totalDiscount(order) {
    return couponDiscount(order) + loyaltyDiscount(order);
}

function tax(order) {
    const afterDiscount = subtotal(order) - totalDiscount(order);
    return Math.floor((afterDiscount * taxRateBps(order.regionId)) / 10000);
}

function finalPrice(order) {
    return subtotal(order) - totalDiscount(order) + tax(order);
}

module.exports = {
    subtotal,
    taxRateBps,
    couponDiscount,
    loyaltyDiscount,
    totalDiscount,
    tax,
    finalPrice,
};