@darkpos/pricing 1.0.81 → 1.0.83

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.
@@ -1667,4 +1667,192 @@ describe('Modifier actions', () => {
1667
1667
 
1668
1668
  expect(currentOrder.total).toEqual(495);
1669
1669
  });
1670
+
1671
+ test('CU-86dve295v should split item based on modifier.maxItemQuantity=1, and spread modifiers accordingly. Having Percentage Modifier', () => {
1672
+ const order = {
1673
+ id: 'ord-456',
1674
+ items: [],
1675
+ modifiers: [],
1676
+ };
1677
+
1678
+ const mod10Percent = {
1679
+ _id: 1,
1680
+ compute: {
1681
+ amount: 10,
1682
+ action: 'subtract',
1683
+ type: 'percentage',
1684
+ },
1685
+ modifierId: '111',
1686
+ };
1687
+
1688
+ const modifier = {
1689
+ _id: 2,
1690
+ properties: {
1691
+ limits: { maxItemQuantity: 1 },
1692
+ },
1693
+ modifierId: '222',
1694
+ };
1695
+
1696
+ let currentOrder = { ...order };
1697
+
1698
+ currentOrder = pricingService.order.addItem({
1699
+ order,
1700
+ item: {
1701
+ quantity: 10,
1702
+ itemId: '222',
1703
+ price: 10,
1704
+ modifiers: [],
1705
+ subTotals: {},
1706
+ },
1707
+ }).updatedOrder;
1708
+
1709
+ currentOrder = pricingService.order.addItemModifier({
1710
+ order: currentOrder,
1711
+ modifier: mod10Percent,
1712
+ itemIndex: 0,
1713
+ });
1714
+
1715
+ currentOrder = pricingService.order.calculate(currentOrder);
1716
+ expect(currentOrder.total).toEqual(99);
1717
+
1718
+ currentOrder = pricingService.order.addItemModifier({
1719
+ order: currentOrder,
1720
+ modifier,
1721
+ itemIndex: 0,
1722
+ });
1723
+
1724
+ currentOrder = pricingService.order.calculate(currentOrder);
1725
+
1726
+ expect(currentOrder.items.length).toEqual(1);
1727
+
1728
+ expect(currentOrder.total).toEqual(99);
1729
+
1730
+ currentOrder = pricingService.order.splitItems({
1731
+ order: currentOrder,
1732
+ });
1733
+
1734
+ currentOrder.items.forEach(item => {
1735
+ expect(item.quantity).toEqual(1);
1736
+ expect(item.price).toEqual(10);
1737
+ expect(item.modifiers[0]._computed.amount).toEqual(0);
1738
+ expect(item.modifiers[1]._computed.amount).toEqual(-0.1);
1739
+ });
1740
+
1741
+ expect(currentOrder.total).toEqual(99);
1742
+ });
1743
+
1744
+ test('Should allow adding a modifier more than once to an item if isQuantityMultiplier={false} QTY={2} required=true', () => {
1745
+ const order = {
1746
+ id: 'ord-123',
1747
+ items: [],
1748
+ modifiers: [],
1749
+ };
1750
+ const modifier = {
1751
+ _id: 1,
1752
+ compute: {
1753
+ amount: 10,
1754
+ action: 'subtract',
1755
+ type: 'fixed',
1756
+ },
1757
+ required: true,
1758
+ };
1759
+ order.items.push({
1760
+ quantity: 2,
1761
+ itemId: '123',
1762
+ price: 100,
1763
+ modifiers: [],
1764
+ });
1765
+
1766
+ const conditionsBag = [];
1767
+
1768
+ const updatedOrder = pricingService.order.addItemModifier({
1769
+ order,
1770
+ modifier,
1771
+ itemIndex: 0,
1772
+ onConditionsNotMet: bag => {
1773
+ bag.forEach(condition => {
1774
+ conditionsBag.push(condition);
1775
+ });
1776
+ },
1777
+ });
1778
+
1779
+ let error = '';
1780
+ const updatedOrder2 = pricingService.order.addItemModifier({
1781
+ order: { ...updatedOrder },
1782
+ modifier,
1783
+ itemIndex: 0,
1784
+ onConditionsNotMet: bag => {
1785
+ bag.forEach(condition => {
1786
+ conditionsBag.push(condition);
1787
+ });
1788
+ },
1789
+ onError: errorMessage => {
1790
+ error = errorMessage;
1791
+ },
1792
+ });
1793
+
1794
+ expect(conditionsBag).toEqual([]);
1795
+ expect(error).toEqual('');
1796
+
1797
+ expect(updatedOrder2.items[0].modifiers.length).toEqual(2);
1798
+ });
1799
+
1800
+ test('Should not allow adding a modifier more than once to an item if isQuantityMultiplier={false} QTY={2} required=true', () => {
1801
+ const order = {
1802
+ id: 'ord-123',
1803
+ items: [],
1804
+ modifiers: [],
1805
+ };
1806
+ const modifier = {
1807
+ _id: 1,
1808
+ compute: {
1809
+ amount: 10,
1810
+ action: 'subtract',
1811
+ type: 'fixed',
1812
+ },
1813
+ properties: {
1814
+ isQuantityMultiplier: true,
1815
+ },
1816
+ required: true,
1817
+ };
1818
+ order.items.push({
1819
+ quantity: 2,
1820
+ itemId: '123',
1821
+ price: 100,
1822
+ modifiers: [],
1823
+ });
1824
+
1825
+ const conditionsBag = [];
1826
+
1827
+ const updatedOrder = pricingService.order.addItemModifier({
1828
+ order,
1829
+ modifier,
1830
+ itemIndex: 0,
1831
+ onConditionsNotMet: bag => {
1832
+ bag.forEach(condition => {
1833
+ conditionsBag.push(condition);
1834
+ });
1835
+ },
1836
+ });
1837
+
1838
+ let error = '';
1839
+ const updatedOrder2 = pricingService.order.addItemModifier({
1840
+ order: { ...updatedOrder },
1841
+ modifier,
1842
+ itemIndex: 0,
1843
+ onConditionsNotMet: bag => {
1844
+ bag.forEach(condition => {
1845
+ conditionsBag.push(condition);
1846
+ });
1847
+ },
1848
+ onError: errorMessage => {
1849
+ error = errorMessage;
1850
+ },
1851
+ });
1852
+
1853
+ expect(conditionsBag).toEqual([]);
1854
+ expect(error).toEqual('modifier.has.reached.the.maximum.amount.of.applies');
1855
+
1856
+ expect(updatedOrder2.items[0].modifiers.length).toEqual(1);
1857
+ });
1670
1858
  });
@@ -10,9 +10,11 @@ module.exports = ({ actions, modifierActions }) =>
10
10
  modifiers: modifierActions.getSplittedModifiers(
11
11
  parentItem.modifiers,
12
12
  parentSubTotal,
13
- subTotal
13
+ subTotal,
14
+ subItems.length
14
15
  ),
15
16
  };
17
+
16
18
  return actions.calculate(newItem);
17
19
  });
18
20
  };
@@ -1,4 +1,4 @@
1
- module.exports = ({ utils }) => {
1
+ module.exports = ({ utils, actions }) => {
2
2
  const { math } = utils;
3
3
  const { getComputeModField } = require('./utils');
4
4
 
@@ -47,7 +47,12 @@ module.exports = ({ utils }) => {
47
47
  };
48
48
  };
49
49
 
50
- return function getSplittedModifiers(modifiers, totalOrigin, totalSplited) {
50
+ return function getSplittedModifiers(
51
+ modifiers,
52
+ totalOrigin,
53
+ totalSplited,
54
+ itemsLength
55
+ ) {
51
56
  return modifiers.map(each => {
52
57
  const modifier = { ...each };
53
58
  const compute = getComputeModField(modifier);
@@ -66,6 +71,22 @@ module.exports = ({ utils }) => {
66
71
  totalSplited,
67
72
  });
68
73
  }
74
+
75
+ if (
76
+ compute &&
77
+ compute.type === 'percentage' &&
78
+ compute.amount &&
79
+ !actions.isQuantityMultiplier(modifier)
80
+ ) {
81
+ return {
82
+ ...modifier,
83
+ compute: {
84
+ ...modifier.compute,
85
+ amount: math.div(modifier.compute.amount, itemsLength),
86
+ },
87
+ };
88
+ }
89
+
69
90
  return modifier;
70
91
  });
71
92
  };
@@ -184,11 +184,7 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
184
184
  const contains =
185
185
  !_.isEmpty(item.modifiers) &&
186
186
  modifierActions.contains(
187
- item.modifiers.filter(
188
- mod =>
189
- !modifierActions.isRequired(mod) &&
190
- !modifierActions.isGroupOfModifiers(mod)
191
- ),
187
+ item.modifiers.filter(mod => !modifierActions.isGroupOfModifiers(mod)),
192
188
  modifier
193
189
  );
194
190
 
@@ -40,7 +40,14 @@ module.exports = ({ actions, modifierActions, itemActions, utils, _ }) => {
40
40
  originalItem,
41
41
  }) {
42
42
  const order = _.cloneDeep(orderProp);
43
- if (!order || itemIndex < 0 || !modifier) return order;
43
+
44
+ if (
45
+ !order ||
46
+ itemIndex < 0 ||
47
+ !modifier ||
48
+ modifierActions.isRequired(modifier)
49
+ )
50
+ return order;
44
51
 
45
52
  let item = actions.getSelectedItem({ order, itemIndex });
46
53
  const customer = actions.getCustomer(order);
@@ -8,7 +8,8 @@ module.exports = ({ actions, modifierActions }) =>
8
8
  modifiers: modifierActions.getSplittedModifiers(
9
9
  parentOrder.modifiers,
10
10
  parentOrder.subTotal,
11
- subTotal
11
+ subTotal,
12
+ subOrders.length
12
13
  ),
13
14
  };
14
15
  return actions.calculate(newOrder);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.81",
3
+ "version": "1.0.83",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -51,5 +51,5 @@
51
51
  "supertest": "^6.2.3",
52
52
  "supervisor": "^0.12.0"
53
53
  },
54
- "gitHead": "41babe2d2f61ea0fac42d1f76c1c98a3afde8507"
54
+ "gitHead": "d52fea81582a64b1ca707af442d41981ed6ce78d"
55
55
  }