@darkpos/pricing 1.0.81 → 1.0.84
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__/modifier.test.js +188 -0
- package/__TEST__/order/order.test.js +28 -0
- package/lib/item/calculate.js +9 -13
- package/lib/item/getTotals.js +27 -0
- package/lib/item/index.js +4 -0
- package/lib/item/patchItem.js +31 -0
- package/lib/item/splitAndCalculate.js +3 -1
- package/lib/modifier/calculate.js +9 -38
- package/lib/modifier/createDescription.js +40 -0
- package/lib/modifier/getSplittedModifiers.js +23 -2
- package/lib/modifier/index.js +4 -0
- package/lib/modifier/patchModifier.js +21 -0
- package/lib/order/addItemModifier.js +1 -5
- package/lib/order/calculate.js +23 -4
- package/lib/order/removeItemModifier.js +8 -1
- package/lib/order/splitAndCalculate.js +2 -1
- package/package.json +2 -2
|
@@ -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
|
});
|
|
@@ -3863,4 +3863,32 @@ describe('Order actions', () => {
|
|
|
3863
3863
|
|
|
3864
3864
|
expect(currentOrder.modifiers.length).toBe(2);
|
|
3865
3865
|
});
|
|
3866
|
+
test('Get calculated Order, one item with correct rounded amount in modifier description', () => {
|
|
3867
|
+
const discountModifier = {
|
|
3868
|
+
compute: {
|
|
3869
|
+
amount: 30,
|
|
3870
|
+
type: 'percentage',
|
|
3871
|
+
action: 'subtract',
|
|
3872
|
+
},
|
|
3873
|
+
name: 'modifier1',
|
|
3874
|
+
type: 'discount',
|
|
3875
|
+
};
|
|
3876
|
+
|
|
3877
|
+
const orderItem = {
|
|
3878
|
+
price: 3.65,
|
|
3879
|
+
quantity: 1,
|
|
3880
|
+
modifiers: [discountModifier],
|
|
3881
|
+
};
|
|
3882
|
+
const order = { items: [orderItem] };
|
|
3883
|
+
const newOrder = pricingService.order.calculate(order);
|
|
3884
|
+
|
|
3885
|
+
expect(newOrder).toHaveProperty('total', 2.56);
|
|
3886
|
+
expect(newOrder.items[0].modifiers[0]._computed.amount).toEqual(-1.09);
|
|
3887
|
+
expect(newOrder.items[0].modifiers[0]._computed.description).toEqual(
|
|
3888
|
+
'modifier1 (-$1.09)'
|
|
3889
|
+
);
|
|
3890
|
+
expect(newOrder).toHaveProperty('subTotals', {
|
|
3891
|
+
discount: -1.09,
|
|
3892
|
+
});
|
|
3893
|
+
});
|
|
3866
3894
|
});
|
package/lib/item/calculate.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
3
3
|
const { math } = utils;
|
|
4
4
|
//
|
|
5
|
+
|
|
5
6
|
const calculateOne = (inputItem, opts = {}) => {
|
|
6
7
|
const item = _.cloneDeep(inputItem);
|
|
7
8
|
const amountToPay =
|
|
@@ -17,7 +18,7 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
17
18
|
|
|
18
19
|
if (!item) return item;
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
let subTotals = {
|
|
21
22
|
_included: 0,
|
|
22
23
|
_xincluded: 0,
|
|
23
24
|
_direct: 0,
|
|
@@ -169,18 +170,13 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
169
170
|
prvPrice = math.add(prvPrice, math.div(_computed.amount, quantity));
|
|
170
171
|
prvSort = sort;
|
|
171
172
|
|
|
172
|
-
|
|
173
|
-
subTotals
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
subTotals._direct = math.add(subTotals._direct, computedAmount);
|
|
180
|
-
else subTotals._xdirect = math.add(subTotals._xdirect, computedAmount);
|
|
181
|
-
|
|
182
|
-
subTotals._actual = math.add(subTotals._simple, subTotals._included);
|
|
183
|
-
total = actions.getTotal({ subTotals });
|
|
173
|
+
({ subTotals, total } = actions.getTotals({
|
|
174
|
+
subTotals,
|
|
175
|
+
amountToAdd: computedAmount,
|
|
176
|
+
included,
|
|
177
|
+
type,
|
|
178
|
+
direct,
|
|
179
|
+
}));
|
|
184
180
|
}
|
|
185
181
|
}
|
|
186
182
|
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module.exports = ({ utils, actions }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
return function getTotals({
|
|
4
|
+
subTotals: subTotalsParam,
|
|
5
|
+
amountToAdd,
|
|
6
|
+
included,
|
|
7
|
+
type,
|
|
8
|
+
direct,
|
|
9
|
+
}) {
|
|
10
|
+
const subTotals = { ...subTotalsParam };
|
|
11
|
+
if (included) {
|
|
12
|
+
subTotals._included = math.add(subTotals._included, amountToAdd);
|
|
13
|
+
} else {
|
|
14
|
+
subTotals._xincluded = math.add(subTotals._xincluded, amountToAdd);
|
|
15
|
+
subTotals[type] = math.add(subTotals[type] || 0, amountToAdd);
|
|
16
|
+
}
|
|
17
|
+
if (direct) subTotals._direct = math.add(subTotals._direct, amountToAdd);
|
|
18
|
+
else subTotals._xdirect = math.add(subTotals._xdirect, amountToAdd);
|
|
19
|
+
|
|
20
|
+
subTotals._actual = math.add(subTotals._simple, subTotals._included);
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
subTotals,
|
|
24
|
+
total: actions.getTotal({ subTotals }),
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
};
|
package/lib/item/index.js
CHANGED
|
@@ -59,6 +59,8 @@ const adjustFixedModifiersDifference = require('./adjustFixedModifiersDifference
|
|
|
59
59
|
const validateCreditModifiersTotal = require('./validateCreditModifiersTotal');
|
|
60
60
|
const adjustCreditModifiersDifference = require('./adjustCreditModifiersDifference');
|
|
61
61
|
const getSubtotal = require('./getSubtotal');
|
|
62
|
+
const getTotals = require('./getTotals');
|
|
63
|
+
const patchItem = require('./patchItem');
|
|
62
64
|
|
|
63
65
|
const itemActions = (deps = {}) => {
|
|
64
66
|
const actions = {};
|
|
@@ -131,6 +133,8 @@ const itemActions = (deps = {}) => {
|
|
|
131
133
|
validateCreditModifiersTotal: validateCreditModifiersTotal(innerDeps),
|
|
132
134
|
adjustCreditModifiersDifference: adjustCreditModifiersDifference(innerDeps),
|
|
133
135
|
getSubtotal: getSubtotal(innerDeps),
|
|
136
|
+
getTotals: getTotals(innerDeps),
|
|
137
|
+
patchItem: patchItem(innerDeps),
|
|
134
138
|
});
|
|
135
139
|
|
|
136
140
|
Object.keys(freezedActions).forEach(actionName => {
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module.exports = ({ actions, modifierActions }) =>
|
|
2
|
+
function patchItem({ item, difference }) {
|
|
3
|
+
if (!item) return undefined;
|
|
4
|
+
|
|
5
|
+
const localItem = { ...item };
|
|
6
|
+
|
|
7
|
+
const modifierIndex = item.modifiers.findIndex(
|
|
8
|
+
mod => modifierActions.isPercentage(mod) || !modifierActions.isDirect(mod)
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
localItem.modifiers[modifierIndex] = modifierActions.patchModifier({
|
|
12
|
+
mod: localItem.modifiers[modifierIndex],
|
|
13
|
+
difference,
|
|
14
|
+
price: actions.getBasePrice(localItem) || localItem.price,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const { included, type, direct } = localItem.modifiers[modifierIndex];
|
|
18
|
+
|
|
19
|
+
const result = actions.getTotals({
|
|
20
|
+
subTotals: localItem.subTotals,
|
|
21
|
+
amountToAdd: difference,
|
|
22
|
+
included,
|
|
23
|
+
type,
|
|
24
|
+
direct,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
localItem.subTotals = result.subTotals;
|
|
28
|
+
localItem.total = result.total;
|
|
29
|
+
|
|
30
|
+
return localItem;
|
|
31
|
+
};
|
|
@@ -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
|
};
|
|
@@ -3,7 +3,7 @@ const { getComputeModField } = require('./utils');
|
|
|
3
3
|
/**
|
|
4
4
|
* Get calculated modifier
|
|
5
5
|
*/
|
|
6
|
-
module.exports = ({ _, constants, utils,
|
|
6
|
+
module.exports = ({ _, constants, utils, actions }) => {
|
|
7
7
|
const { math } = utils;
|
|
8
8
|
const { Modifier } = constants;
|
|
9
9
|
|
|
@@ -12,7 +12,6 @@ module.exports = ({ _, constants, utils, localization, actions }) => {
|
|
|
12
12
|
options = { price: 0, quantity: 0, skip: false, basePrice: 0 }
|
|
13
13
|
) {
|
|
14
14
|
const modifier = _modifier;
|
|
15
|
-
const { name } = modifier;
|
|
16
15
|
const compute = getComputeModField(modifier);
|
|
17
16
|
const { type, amount: computeAmount = 0 } = compute;
|
|
18
17
|
const _computed = {
|
|
@@ -71,42 +70,14 @@ module.exports = ({ _, constants, utils, localization, actions }) => {
|
|
|
71
70
|
}
|
|
72
71
|
}
|
|
73
72
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
options.basePrice || options.price
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
const isMultiplier = actions.isMultiplier(modifier);
|
|
84
|
-
|
|
85
|
-
_computed.description = `${name}${localAmount ? ` (${localAmount})` : ''}`;
|
|
86
|
-
|
|
87
|
-
if (actions.isPriceOverride(modifier)) {
|
|
88
|
-
_computed.description = isMultiplier
|
|
89
|
-
? `${name} (${compute.amount} Unit @ ${localBasePrice}/Unit)`
|
|
90
|
-
: `${name} (${localization.formatAmount(compute.amount)}/Unit)`;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (actions.isAmountOverride(modifier) && isMultiplier) {
|
|
94
|
-
_computed.description = `${name} (${localization.formatAmount(
|
|
95
|
-
_computed.amount
|
|
96
|
-
)})`;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (actions.isOptionsSelectedOverride(modifier)) {
|
|
100
|
-
const selectedOverrideOptions = _.get(
|
|
101
|
-
modifier,
|
|
102
|
-
'properties.override.selected'
|
|
103
|
-
);
|
|
104
|
-
_computed.description = isMultiplier
|
|
105
|
-
? `${name} (${compute.amount} Unit @ ${localBasePrice}/${selectedOverrideOptions.selectedUnit})`
|
|
106
|
-
: `${name} (${localization.formatAmount(compute.amount)}/${
|
|
107
|
-
selectedOverrideOptions.selectedUnit
|
|
108
|
-
})`;
|
|
109
|
-
}
|
|
73
|
+
_computed.description = actions.createDescription({
|
|
74
|
+
modifier: {
|
|
75
|
+
...modifier,
|
|
76
|
+
compute,
|
|
77
|
+
_computed,
|
|
78
|
+
},
|
|
79
|
+
price: options.basePrice || options.price,
|
|
80
|
+
});
|
|
110
81
|
|
|
111
82
|
return { ..._.cloneDeep(modifier), _computed };
|
|
112
83
|
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
module.exports = ({ actions, localization, _ }) =>
|
|
2
|
+
function createDescription({ modifier, price }) {
|
|
3
|
+
const { _computed, compute, name } = modifier;
|
|
4
|
+
|
|
5
|
+
let description = '';
|
|
6
|
+
const localAmount =
|
|
7
|
+
!!_computed.amount && Number(_computed.amount) !== 0
|
|
8
|
+
? `${localization.formatAmount(_computed.amount)}`
|
|
9
|
+
: '';
|
|
10
|
+
|
|
11
|
+
const localBasePrice = localization.formatAmount(price || 0);
|
|
12
|
+
|
|
13
|
+
const isMultiplier = actions.isMultiplier(modifier);
|
|
14
|
+
|
|
15
|
+
description = `${name}${localAmount ? ` (${localAmount})` : ''}`;
|
|
16
|
+
|
|
17
|
+
if (actions.isPriceOverride(modifier)) {
|
|
18
|
+
description = isMultiplier
|
|
19
|
+
? `${name} (${compute.amount} Unit @ ${localBasePrice}/Unit)`
|
|
20
|
+
: `${name} (${localization.formatAmount(compute.amount)}/Unit)`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (actions.isAmountOverride(modifier) && isMultiplier) {
|
|
24
|
+
description = `${name} (${localization.formatAmount(_computed.amount)})`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (actions.isOptionsSelectedOverride(modifier)) {
|
|
28
|
+
const selectedOverrideOptions = _.get(
|
|
29
|
+
modifier,
|
|
30
|
+
'properties.override.selected'
|
|
31
|
+
);
|
|
32
|
+
description = isMultiplier
|
|
33
|
+
? `${name} (${compute.amount} Unit @ ${localBasePrice}/${selectedOverrideOptions.selectedUnit})`
|
|
34
|
+
: `${name} (${localization.formatAmount(compute.amount)}/${
|
|
35
|
+
selectedOverrideOptions.selectedUnit
|
|
36
|
+
})`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return description;
|
|
40
|
+
};
|
|
@@ -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(
|
|
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
|
};
|
package/lib/modifier/index.js
CHANGED
|
@@ -161,6 +161,8 @@ const isLimits = require('./isLimits');
|
|
|
161
161
|
const isItemSet = require('./isItemSet');
|
|
162
162
|
const isOrderUseValid = require('./isOrderUseValid');
|
|
163
163
|
const getMaxItemQuantity = require('./getMaxItemQuantity');
|
|
164
|
+
const createDescription = require('./createDescription');
|
|
165
|
+
const patchModifier = require('./patchModifier');
|
|
164
166
|
|
|
165
167
|
const modifierActions = (deps = {}) => {
|
|
166
168
|
const actions = {};
|
|
@@ -335,6 +337,8 @@ const modifierActions = (deps = {}) => {
|
|
|
335
337
|
getMaxItemQuantity: getMaxItemQuantity(innerDeps),
|
|
336
338
|
getFixedModifiersTotalEntities: getFixedModifiersTotalEntities(innerDeps),
|
|
337
339
|
getCreditModifiersTotalEntities: getCreditModifiersTotalEntities(innerDeps),
|
|
340
|
+
createDescription: createDescription(innerDeps),
|
|
341
|
+
patchModifier: patchModifier(innerDeps),
|
|
338
342
|
});
|
|
339
343
|
|
|
340
344
|
Object.keys(freezedActions).forEach(actionName => {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module.exports = ({ utils, actions }) =>
|
|
2
|
+
function patchModifier({ mod, difference, price }) {
|
|
3
|
+
if (!mod || !mod._computed) return mod;
|
|
4
|
+
|
|
5
|
+
const modifier = { ...mod };
|
|
6
|
+
|
|
7
|
+
modifier._computed.amount = utils.math.add(
|
|
8
|
+
modifier._computed.amount,
|
|
9
|
+
difference
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
modifier._computed.description = actions.createDescription({
|
|
13
|
+
modifier: {
|
|
14
|
+
...modifier,
|
|
15
|
+
_computed: modifier._computed,
|
|
16
|
+
},
|
|
17
|
+
price,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return modifier;
|
|
21
|
+
};
|
|
@@ -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
|
|
package/lib/order/calculate.js
CHANGED
|
@@ -108,19 +108,38 @@ module.exports = ({
|
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
const addToItemTotal = difference => {
|
|
111
|
-
if (difference
|
|
111
|
+
if (difference !== 0 && calculatedItems.length) {
|
|
112
112
|
calculatedItems[0].total = utils.math.toDecimalPlaces(
|
|
113
113
|
utils.math.add(calculatedItems[0].total, difference)
|
|
114
114
|
);
|
|
115
115
|
}
|
|
116
116
|
};
|
|
117
117
|
|
|
118
|
-
if (calculatedItems.length >
|
|
119
|
-
|
|
118
|
+
if (calculatedItems.length > 0) {
|
|
119
|
+
let difference = itemActions.getTotalsDifference({
|
|
120
120
|
items: calculatedItems,
|
|
121
121
|
});
|
|
122
122
|
|
|
123
|
-
|
|
123
|
+
if (difference < 0) {
|
|
124
|
+
difference = difference < -0.005 ? 0 : utils.math.abs(difference);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const itemIndex = calculatedItems.findIndex(
|
|
128
|
+
item =>
|
|
129
|
+
!itemActions.isFullyPaid(item) &&
|
|
130
|
+
item.modifiers.some(
|
|
131
|
+
mod =>
|
|
132
|
+
modifierActions.isPercentage(mod) ||
|
|
133
|
+
!modifierActions.isDirect(mod)
|
|
134
|
+
)
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
if (itemIndex >= 0 && difference >= 0.005) {
|
|
138
|
+
calculatedItems[itemIndex] = itemActions.patchItem({
|
|
139
|
+
item: calculatedItems[itemIndex],
|
|
140
|
+
difference,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
124
143
|
}
|
|
125
144
|
|
|
126
145
|
if (storeActions.isNeareastMultiple() && calculatedItems.length > 0) {
|
|
@@ -40,7 +40,14 @@ module.exports = ({ actions, modifierActions, itemActions, utils, _ }) => {
|
|
|
40
40
|
originalItem,
|
|
41
41
|
}) {
|
|
42
42
|
const order = _.cloneDeep(orderProp);
|
|
43
|
-
|
|
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);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/pricing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.84",
|
|
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": "
|
|
54
|
+
"gitHead": "d15a98cdefcf27cdaf41a8096a6c6c7dcdfcc13f"
|
|
55
55
|
}
|