@darkpos/pricing 1.0.107 → 1.0.108
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/__TEST__/item.test.js
CHANGED
|
@@ -1557,4 +1557,136 @@ describe('Item actions', () => {
|
|
|
1557
1557
|
'No Clean (0 Unit @ $0.00/Unit)'
|
|
1558
1558
|
);
|
|
1559
1559
|
});
|
|
1560
|
+
|
|
1561
|
+
test('Should not apply modifier value when extractCalculatedValue is true but modifier is invalid', () => {
|
|
1562
|
+
const prepayDiscount50Percent = {
|
|
1563
|
+
compute: {
|
|
1564
|
+
amount: 50,
|
|
1565
|
+
type: 'percentage',
|
|
1566
|
+
action: 'subtract',
|
|
1567
|
+
},
|
|
1568
|
+
properties: {
|
|
1569
|
+
extractCalculatedValue: true,
|
|
1570
|
+
},
|
|
1571
|
+
conditions: {
|
|
1572
|
+
valid: null,
|
|
1573
|
+
rules: [
|
|
1574
|
+
{
|
|
1575
|
+
key: 'payment',
|
|
1576
|
+
value: 'prepay',
|
|
1577
|
+
operand: '$in',
|
|
1578
|
+
},
|
|
1579
|
+
],
|
|
1580
|
+
},
|
|
1581
|
+
name: 'percentageDiscount50',
|
|
1582
|
+
type: 'discount',
|
|
1583
|
+
};
|
|
1584
|
+
|
|
1585
|
+
const orderItem = {
|
|
1586
|
+
price: 7.59,
|
|
1587
|
+
quantity: 1,
|
|
1588
|
+
modifiers: [prepayDiscount50Percent],
|
|
1589
|
+
};
|
|
1590
|
+
const order = {
|
|
1591
|
+
items: [orderItem],
|
|
1592
|
+
};
|
|
1593
|
+
|
|
1594
|
+
const calculatedOrder = pricingService.order.calculate(order);
|
|
1595
|
+
|
|
1596
|
+
expect(calculatedOrder.total).toBe(7.59);
|
|
1597
|
+
expect(calculatedOrder.items[0].price).toBe(7.59);
|
|
1598
|
+
expect(calculatedOrder.items[0].total).toBe(7.59);
|
|
1599
|
+
expect(calculatedOrder.items[0].modifiers.length).toBe(1);
|
|
1600
|
+
expect(calculatedOrder.items[0].modifiers[0]._computed).toMatchObject({
|
|
1601
|
+
amount: 0,
|
|
1602
|
+
description: 'percentageDiscount50',
|
|
1603
|
+
});
|
|
1604
|
+
});
|
|
1605
|
+
|
|
1606
|
+
test('Should apply modifier value when extractCalculatedValue is true and modifier is valid', () => {
|
|
1607
|
+
const prepayDiscount50Percent = {
|
|
1608
|
+
compute: {
|
|
1609
|
+
amount: 50,
|
|
1610
|
+
type: 'percentage',
|
|
1611
|
+
action: 'subtract',
|
|
1612
|
+
},
|
|
1613
|
+
properties: {
|
|
1614
|
+
extractCalculatedValue: true,
|
|
1615
|
+
},
|
|
1616
|
+
name: 'percentageDiscount50',
|
|
1617
|
+
type: 'discount',
|
|
1618
|
+
};
|
|
1619
|
+
|
|
1620
|
+
const orderItem = {
|
|
1621
|
+
price: 7.59,
|
|
1622
|
+
quantity: 1,
|
|
1623
|
+
modifiers: [prepayDiscount50Percent],
|
|
1624
|
+
};
|
|
1625
|
+
const order = {
|
|
1626
|
+
items: [orderItem],
|
|
1627
|
+
};
|
|
1628
|
+
|
|
1629
|
+
const calculatedOrder = pricingService.order.calculate(order);
|
|
1630
|
+
|
|
1631
|
+
expect(calculatedOrder.total).toBe(7.59);
|
|
1632
|
+
expect(calculatedOrder.items[0].price).toBe(7.59);
|
|
1633
|
+
expect(calculatedOrder.items[0].total).toBe(7.59);
|
|
1634
|
+
expect(calculatedOrder.items[0].modifiers.length).toBe(1);
|
|
1635
|
+
expect(calculatedOrder.items[0].modifiers[0]._computed).toMatchObject({
|
|
1636
|
+
amount: -7.59,
|
|
1637
|
+
description: 'percentageDiscount50 (-$7.59)',
|
|
1638
|
+
});
|
|
1639
|
+
});
|
|
1640
|
+
|
|
1641
|
+
test('Should apply modifier value when extractCalculatedValue is true and modifier is valid (being also a payment modifier)', () => {
|
|
1642
|
+
const prepayDiscount50Percent = {
|
|
1643
|
+
compute: {
|
|
1644
|
+
amount: 50,
|
|
1645
|
+
type: 'percentage',
|
|
1646
|
+
action: 'subtract',
|
|
1647
|
+
},
|
|
1648
|
+
properties: {
|
|
1649
|
+
extractCalculatedValue: true,
|
|
1650
|
+
},
|
|
1651
|
+
conditions: {
|
|
1652
|
+
valid: null,
|
|
1653
|
+
rules: [
|
|
1654
|
+
{
|
|
1655
|
+
key: 'payment',
|
|
1656
|
+
value: 'prepay',
|
|
1657
|
+
operand: '$in',
|
|
1658
|
+
},
|
|
1659
|
+
],
|
|
1660
|
+
},
|
|
1661
|
+
name: 'percentageDiscount50',
|
|
1662
|
+
type: 'discount',
|
|
1663
|
+
};
|
|
1664
|
+
|
|
1665
|
+
const orderItem = {
|
|
1666
|
+
price: 7.59,
|
|
1667
|
+
quantity: 1,
|
|
1668
|
+
modifiers: [prepayDiscount50Percent],
|
|
1669
|
+
};
|
|
1670
|
+
const order = {
|
|
1671
|
+
items: [orderItem],
|
|
1672
|
+
};
|
|
1673
|
+
|
|
1674
|
+
const calculatedOrder = pricingService.order.calculate(order, {
|
|
1675
|
+
isPrepay: true,
|
|
1676
|
+
});
|
|
1677
|
+
|
|
1678
|
+
expect(calculatedOrder.total).toBe(7.59);
|
|
1679
|
+
expect(calculatedOrder.items[0].price).toBe(7.59);
|
|
1680
|
+
expect(calculatedOrder.items[0].total).toBe(7.59);
|
|
1681
|
+
expect(calculatedOrder.items[0].modifiers.length).toBe(2);
|
|
1682
|
+
|
|
1683
|
+
expect(calculatedOrder.items[0].modifiers[0]._computed).toMatchObject({
|
|
1684
|
+
amount: 0,
|
|
1685
|
+
description: 'percentageDiscount50',
|
|
1686
|
+
});
|
|
1687
|
+
expect(calculatedOrder.items[0].modifiers[1]._computed).toMatchObject({
|
|
1688
|
+
amount: -7.59,
|
|
1689
|
+
description: '$7.59 discount (-$7.59)',
|
|
1690
|
+
});
|
|
1691
|
+
});
|
|
1560
1692
|
});
|
package/lib/item/calculate.js
CHANGED
|
@@ -40,10 +40,6 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
40
40
|
item.properties.overwrittenQuantity = undefined;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
const price = parseFloat(actions.getPriceWithoutModifiers({ item }));
|
|
44
|
-
let quantity = parseInt(item.quantity, 10);
|
|
45
|
-
subTotals._simple = math.mul(price, quantity);
|
|
46
|
-
|
|
47
43
|
const modifiers = [];
|
|
48
44
|
let { modifiers: itemModifiersPrev = [] } =
|
|
49
45
|
actions.removeModifiersByQuantity(item);
|
|
@@ -103,6 +99,15 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
103
99
|
);
|
|
104
100
|
});
|
|
105
101
|
|
|
102
|
+
const price = parseFloat(
|
|
103
|
+
actions.getPriceWithoutModifiers({
|
|
104
|
+
price: item.price,
|
|
105
|
+
modifiers: validatedModifiers,
|
|
106
|
+
})
|
|
107
|
+
);
|
|
108
|
+
let quantity = parseInt(item.quantity, 10);
|
|
109
|
+
subTotals._simple = math.mul(price, quantity);
|
|
110
|
+
|
|
106
111
|
const modifiersToNotCompute = actions.getModifiersToNotCompute({
|
|
107
112
|
item,
|
|
108
113
|
validatedModifiers,
|
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
module.exports = ({ actions, modifierActions }) =>
|
|
2
2
|
/* eslint-disable no-restricted-syntax */
|
|
3
|
-
function getPriceWithoutModifiers({
|
|
4
|
-
const basePrice = actions.getBasePrice(
|
|
3
|
+
function getPriceWithoutModifiers({ price, modifiers }) {
|
|
4
|
+
const basePrice = actions.getBasePrice({ price });
|
|
5
5
|
|
|
6
|
-
if (
|
|
7
|
-
!item ||
|
|
8
|
-
!Array.isArray(item.modifiers) ||
|
|
9
|
-
typeof basePrice !== 'number'
|
|
10
|
-
)
|
|
6
|
+
if (!Array.isArray(modifiers) || typeof basePrice !== 'number')
|
|
11
7
|
return basePrice;
|
|
12
8
|
|
|
13
|
-
const calculatedModifiers =
|
|
14
|
-
|
|
9
|
+
const calculatedModifiers = modifiers.filter(
|
|
10
|
+
modifier =>
|
|
11
|
+
modifierActions.isValid(modifier) &&
|
|
12
|
+
modifierActions.isExtractCalculatedValue({ modifier })
|
|
15
13
|
);
|
|
16
14
|
|
|
17
15
|
if (calculatedModifiers.length === 0) return basePrice;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/pricing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.108",
|
|
4
4
|
"description": "Pricing calculator",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"license": "ISC",
|
|
@@ -54,5 +54,5 @@
|
|
|
54
54
|
"supertest": "^6.2.3",
|
|
55
55
|
"supervisor": "^0.12.0"
|
|
56
56
|
},
|
|
57
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "263ab257a926d73b861d12ae2ccfdb46b7d162ee"
|
|
58
58
|
}
|