@darkpos/pricing 1.0.31 → 1.0.33
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/addIndirectModifier.test.js +192 -0
- package/__TEST__/modifier/calculate.test.js +35 -4
- package/__TEST__/modifier/createIndirectModifier.test.js +166 -0
- package/__TEST__/modifier/getFixedModifiersTotal.test.js +52 -0
- package/__TEST__/modifier/getSplittedModifiers.test.js +134 -0
- package/__TEST__/modifier/isComputedOverride.test.js +38 -0
- package/__TEST__/order/addItem.test.js +57 -53
- package/__TEST__/order/adjustFixedModifiersDifference.test.js +117 -0
- package/__TEST__/order/order.test.js +47 -0
- package/__TEST__/order/split.test.js +356 -0
- package/lib/item/addIndirectModifier.js +5 -2
- package/lib/item/calculate.js +5 -1
- package/lib/item/getItemsTotalQuantity.js +7 -0
- package/lib/item/getItemsTotalWeight.js +7 -0
- package/lib/item/getTotalPieces.js +1 -1
- package/lib/item/getTotalQuantity.js +5 -0
- package/lib/item/getTotalWeight.js +7 -0
- package/lib/item/index.js +8 -0
- package/lib/modifier/calculate.js +5 -2
- package/lib/modifier/createIndirectModifier.js +4 -3
- package/lib/modifier/getFixedModifiersTotal.js +5 -4
- package/lib/modifier/getSplittedModifiers.js +5 -7
- package/lib/modifier/index.js +0 -2
- package/lib/modifier/isComputedOverride.js +3 -2
- package/lib/modifier/utils.js +10 -0
- package/lib/order/addItemModifier.js +9 -4
- package/lib/order/addModifier.js +6 -4
- package/lib/order/adjustFixedModifiersDifference.js +10 -9
- package/lib/order/splitByDepartments.js +37 -7
- package/package.json +2 -2
- package/lib/modifier/includePiecesInQuantity.js +0 -10
package/lib/item/calculate.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
3
3
|
const { math } = utils;
|
|
4
4
|
//
|
|
5
|
+
const { getComputeModField } = require('../modifier/utils');
|
|
6
|
+
|
|
5
7
|
const calculateOne = inputItem => {
|
|
6
8
|
const item = _.cloneDeep(inputItem);
|
|
7
9
|
if (!item) return item;
|
|
@@ -70,8 +72,10 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
70
72
|
const computedAmountCalc = math.mul(_computed.amount, quantity);
|
|
71
73
|
const computedPriceCalc = math.mul(computedPrice * quantity);
|
|
72
74
|
|
|
75
|
+
// adding this computeField because now the compute from a modifier can be null
|
|
76
|
+
const computeField = getComputeModField(_modifier);
|
|
73
77
|
let computedAmount =
|
|
74
|
-
|
|
78
|
+
computeField.type === 'percentage' ||
|
|
75
79
|
modifierActions.isIgnoreQuantity(_modifier) ||
|
|
76
80
|
(!modifierActions.isFixedAdd(_modifier) &&
|
|
77
81
|
math.gt(math.abs(computedAmountCalc), computedPriceCalc))
|
package/lib/item/index.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
const getItemsTotalPieces = require('./getItemsTotalPieces');
|
|
2
2
|
const getTotalPieces = require('./getTotalPieces');
|
|
3
|
+
const getItemsTotalQuantity = require('./getItemsTotalQuantity');
|
|
4
|
+
const getTotalQuantity = require('./getTotalQuantity');
|
|
5
|
+
const getItemsTotalWeight = require('./getItemsTotalWeight');
|
|
6
|
+
const getTotalWeight = require('./getTotalWeight');
|
|
3
7
|
const removeModifier = require('./removeModifier');
|
|
4
8
|
const removeModifiers = require('./removeModifiers');
|
|
5
9
|
const findOriginalItem = require('./findOriginalItem');
|
|
@@ -45,6 +49,10 @@ const itemActions = (deps = {}) => {
|
|
|
45
49
|
};
|
|
46
50
|
|
|
47
51
|
const freezedActions = Object.freeze({
|
|
52
|
+
getItemsTotalWeight: getItemsTotalWeight(innerDeps),
|
|
53
|
+
getTotalWeight: getTotalWeight(innerDeps),
|
|
54
|
+
getItemsTotalQuantity: getItemsTotalQuantity(innerDeps),
|
|
55
|
+
getTotalQuantity: getTotalQuantity(innerDeps),
|
|
48
56
|
getItemsTotalPieces: getItemsTotalPieces(innerDeps),
|
|
49
57
|
getTotalPieces: getTotalPieces(innerDeps),
|
|
50
58
|
removeModifier: removeModifier(innerDeps),
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const { getComputeModField } = require('./utils');
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Get calculated modifier
|
|
3
5
|
*/
|
|
@@ -9,8 +11,9 @@ module.exports = ({ _, constants, utils, localization, actions }) => {
|
|
|
9
11
|
modifier = {},
|
|
10
12
|
options = { price: 0, quantity: 0, skip: false }
|
|
11
13
|
) {
|
|
12
|
-
const { name
|
|
13
|
-
const
|
|
14
|
+
const { name } = modifier;
|
|
15
|
+
const compute = getComputeModField(modifier);
|
|
16
|
+
const { type, action, amount = 0 } = compute;
|
|
14
17
|
const _computed = {
|
|
15
18
|
amount: 0,
|
|
16
19
|
description: '',
|
|
@@ -4,15 +4,16 @@
|
|
|
4
4
|
module.exports = ({ _, utils, constants, actions }) => {
|
|
5
5
|
const { math } = utils;
|
|
6
6
|
const { Modifier } = constants;
|
|
7
|
-
|
|
7
|
+
const { getComputeModField } = require('./utils');
|
|
8
8
|
return function createIndirectModifier(
|
|
9
9
|
modifier,
|
|
10
10
|
options = { orderTotal: 0, itemQuantity: 1, itemTotal: 0 }
|
|
11
11
|
) {
|
|
12
12
|
const maxAmount = actions.getProperty(modifier, 'maxAmount');
|
|
13
13
|
|
|
14
|
-
const compute = modifier
|
|
15
|
-
const
|
|
14
|
+
const compute = getComputeModField(modifier);
|
|
15
|
+
const type = _.get(compute, 'type', '');
|
|
16
|
+
const amount = _.get(compute, 'amount', 0);
|
|
16
17
|
let modifierAmount = amount;
|
|
17
18
|
|
|
18
19
|
if (type === Modifier.Compute.Types.PERCENTAGE) {
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
module.exports = ({ utils }) => {
|
|
2
2
|
const { math } = utils;
|
|
3
|
-
|
|
3
|
+
const { getComputeModField } = require('./utils');
|
|
4
4
|
return function getFixedModifiersTotal(modifiers) {
|
|
5
5
|
if (!Array.isArray(modifiers)) return 0;
|
|
6
6
|
|
|
7
|
-
const fixedModifiers = modifiers.filter(
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
const fixedModifiers = modifiers.filter(mod => {
|
|
8
|
+
const compute = getComputeModField(mod);
|
|
9
|
+
return compute.type === 'fixed';
|
|
10
|
+
});
|
|
10
11
|
|
|
11
12
|
return fixedModifiers.reduce(
|
|
12
13
|
(acc, modifier) => math.add(acc, modifier.compute.amount || 0),
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
module.exports = ({ utils }) => {
|
|
2
2
|
const { math } = utils;
|
|
3
|
+
const { getComputeModField } = require('./utils');
|
|
3
4
|
|
|
4
5
|
const divideModifierAmount = ({
|
|
5
6
|
totalOriginOrder,
|
|
@@ -25,10 +26,11 @@ module.exports = ({ utils }) => {
|
|
|
25
26
|
totalOriginOrder,
|
|
26
27
|
totalSplitedOrder,
|
|
27
28
|
});
|
|
29
|
+
const compute = getComputeModField(modifier);
|
|
28
30
|
return {
|
|
29
31
|
...modifier,
|
|
30
32
|
compute: {
|
|
31
|
-
...
|
|
33
|
+
...compute,
|
|
32
34
|
},
|
|
33
35
|
properties: {
|
|
34
36
|
...modifier.properties,
|
|
@@ -63,7 +65,7 @@ module.exports = ({ utils }) => {
|
|
|
63
65
|
) {
|
|
64
66
|
return modifiers.map(each => {
|
|
65
67
|
const modifier = { ...each };
|
|
66
|
-
|
|
68
|
+
const compute = getComputeModField(modifier);
|
|
67
69
|
if (modifier.type === 'credit') {
|
|
68
70
|
return splitCreditModifier({
|
|
69
71
|
modifier,
|
|
@@ -72,11 +74,7 @@ module.exports = ({ utils }) => {
|
|
|
72
74
|
});
|
|
73
75
|
}
|
|
74
76
|
|
|
75
|
-
if (
|
|
76
|
-
modifier.compute &&
|
|
77
|
-
modifier.compute.type === 'fixed' &&
|
|
78
|
-
modifier.compute.amount
|
|
79
|
-
) {
|
|
77
|
+
if (compute && compute.type === 'fixed' && compute.amount) {
|
|
80
78
|
return splitFixedModifier({
|
|
81
79
|
modifier,
|
|
82
80
|
totalOriginOrder,
|
package/lib/modifier/index.js
CHANGED
|
@@ -76,7 +76,6 @@ const hasModifier = require('./hasModifier');
|
|
|
76
76
|
const hasRelatedItems = require('./hasRelatedItems');
|
|
77
77
|
const hasFixedModifier = require('./hasFixedModifier');
|
|
78
78
|
const hasCreditModifier = require('./hasCreditModifier');
|
|
79
|
-
const includePiecesInQuantity = require('./includePiecesInQuantity');
|
|
80
79
|
const includesInGroup = require('./includesInGroup');
|
|
81
80
|
const isTrackUsageSubscription = require('./isTrackUsageSubscription');
|
|
82
81
|
const isDirect = require('./isDirect');
|
|
@@ -218,7 +217,6 @@ const modifierActions = (deps = {}) => {
|
|
|
218
217
|
hasAttributes: hasAttributes(innerDeps),
|
|
219
218
|
hasFixedModifier: hasFixedModifier(innerDeps),
|
|
220
219
|
hasCreditModifier: hasCreditModifier(innerDeps),
|
|
221
|
-
includePiecesInQuantity: includePiecesInQuantity(innerDeps),
|
|
222
220
|
includesInGroup: includesInGroup(innerDeps),
|
|
223
221
|
isTrackUsageSubscription: isTrackUsageSubscription(innerDeps),
|
|
224
222
|
isDirect: isDirect(innerDeps),
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
module.exports = ({
|
|
1
|
+
module.exports = ({ constants }) => {
|
|
2
2
|
const { Modifier } = constants;
|
|
3
3
|
return function isComputedOverride(modifier) {
|
|
4
|
-
const computeAction =
|
|
4
|
+
const computeAction =
|
|
5
|
+
modifier && modifier.compute && modifier.compute.action;
|
|
5
6
|
return computeAction === Modifier.Attributes.OVERRIDE;
|
|
6
7
|
};
|
|
7
8
|
};
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
|
|
2
2
|
const { math } = utils;
|
|
3
|
+
const { getComputeModField } = require('../modifier/utils');
|
|
3
4
|
|
|
4
5
|
const getPrice = ({ item, modifier }) => {
|
|
5
|
-
const
|
|
6
|
-
|
|
6
|
+
const compute = getComputeModField(modifier);
|
|
7
|
+
const amount = (modifier && compute && compute.amount) || 0;
|
|
7
8
|
const { price = 0 } = item;
|
|
8
9
|
if (modifierActions.isMultiplier(modifier)) return math.mul(price, amount);
|
|
9
10
|
return amount;
|
|
@@ -64,6 +65,7 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
|
|
|
64
65
|
},
|
|
65
66
|
}) => {
|
|
66
67
|
let item = { ...itemProp };
|
|
68
|
+
const compute = getComputeModField(modifier);
|
|
67
69
|
if (hasBehaivoralFields(modifier)) return item;
|
|
68
70
|
// check conditions
|
|
69
71
|
if (!areConditionsMet(item, modifier.conditions)) return item;
|
|
@@ -91,7 +93,7 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
|
|
|
91
93
|
if (modifierActions.isAmountOverride(modifier)) {
|
|
92
94
|
item = {
|
|
93
95
|
...item,
|
|
94
|
-
price:
|
|
96
|
+
price: compute.amount,
|
|
95
97
|
};
|
|
96
98
|
}
|
|
97
99
|
|
|
@@ -99,7 +101,10 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
|
|
|
99
101
|
item.quantity = modifier.compute.amount || 1;
|
|
100
102
|
}
|
|
101
103
|
|
|
102
|
-
if (
|
|
104
|
+
if (
|
|
105
|
+
modifierActions.isPriceOverride(modifier) ||
|
|
106
|
+
modifierActions.isComputedOverride(modifier)
|
|
107
|
+
) {
|
|
103
108
|
item.price = getPrice({ modifier, item });
|
|
104
109
|
} else {
|
|
105
110
|
item.price = itemActions.getItemPrice({
|
package/lib/order/addModifier.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
module.exports = ({ actions, modifierActions }) =>
|
|
2
|
-
|
|
1
|
+
module.exports = ({ actions, modifierActions }) => {
|
|
2
|
+
const { getComputeModField } = require('../modifier/utils');
|
|
3
|
+
return function addModifier({ order, modifier }) {
|
|
3
4
|
if (!modifier) return order;
|
|
4
5
|
const { modifiers = [] } = order;
|
|
5
|
-
|
|
6
6
|
if (modifierActions.isFixedDiscount(modifier)) {
|
|
7
|
+
const compute = getComputeModField(modifier);
|
|
7
8
|
const orderDue = actions.calculateDue(order);
|
|
8
|
-
if (
|
|
9
|
+
if (compute.amount > orderDue) {
|
|
9
10
|
const err = new Error();
|
|
10
11
|
err.valid = false;
|
|
11
12
|
err.orderDue = orderDue;
|
|
@@ -19,3 +20,4 @@ module.exports = ({ actions, modifierActions }) =>
|
|
|
19
20
|
modifiers: [...modifiers, { ...modifier }],
|
|
20
21
|
};
|
|
21
22
|
};
|
|
23
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
module.exports = ({ utils, actions, modifierActions }) => {
|
|
1
|
+
module.exports = ({ utils, actions, modifierActions, _ }) => {
|
|
2
2
|
const { math } = utils;
|
|
3
|
-
|
|
3
|
+
const { getComputeModField } = require('../modifier/utils');
|
|
4
4
|
return function adjustFixedModifiersDifference({ subOrders, difference }) {
|
|
5
5
|
const selectedSubOrderIndex = subOrders.findIndex(
|
|
6
6
|
subOrder =>
|
|
@@ -15,15 +15,16 @@ module.exports = ({ utils, actions, modifierActions }) => {
|
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
const modifierToUpdateIdx = subOrderToUpdate.modifiers.findIndex(
|
|
18
|
-
mod => mod.compute.type === 'fixed'
|
|
18
|
+
mod => mod.compute && mod.compute.type === 'fixed'
|
|
19
19
|
);
|
|
20
|
-
const
|
|
21
|
-
subOrderToUpdate.modifiers[modifierToUpdateIdx]
|
|
22
|
-
|
|
23
|
-
subOrderToUpdate.modifiers[modifierToUpdateIdx].compute.amount = math.add(
|
|
24
|
-
prevAmount,
|
|
25
|
-
difference
|
|
20
|
+
const prevCompute = getComputeModField(
|
|
21
|
+
subOrderToUpdate.modifiers[modifierToUpdateIdx]
|
|
26
22
|
);
|
|
23
|
+
const prevAmount = _.get(prevCompute, 'compute.amount', 0);
|
|
24
|
+
if (modifierToUpdateIdx !== -1) {
|
|
25
|
+
subOrderToUpdate.modifiers[modifierToUpdateIdx].compute.amount =
|
|
26
|
+
math.add(prevAmount, difference);
|
|
27
|
+
}
|
|
27
28
|
subOrders.splice(
|
|
28
29
|
selectedSubOrderIndex,
|
|
29
30
|
1,
|
|
@@ -80,6 +80,9 @@ module.exports = ({ utils, _, itemActions, modifierActions, settings }) => {
|
|
|
80
80
|
return (deps.length && deps[0].name) || 'other';
|
|
81
81
|
};
|
|
82
82
|
|
|
83
|
+
const isSplitByPieces = splitUnit => splitUnit === 'pieces';
|
|
84
|
+
const isSplitByWeight = splitUnit => splitUnit === 'weight';
|
|
85
|
+
|
|
83
86
|
return function splitByDepartments(order) {
|
|
84
87
|
const { items } = order;
|
|
85
88
|
const itemsByDepartments = _.groupBy(items, getDepartmentName);
|
|
@@ -94,11 +97,37 @@ module.exports = ({ utils, _, itemActions, modifierActions, settings }) => {
|
|
|
94
97
|
const departmentName = getDepartmentName(newItems[0]);
|
|
95
98
|
const maxItems = modifierActions.getDepartmentMaxItems(department);
|
|
96
99
|
|
|
97
|
-
|
|
98
|
-
department
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
)
|
|
100
|
+
const autoSplit = _.get(
|
|
101
|
+
department || {},
|
|
102
|
+
'properties.department.autoSplit',
|
|
103
|
+
false
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
const splitUnit = _.get(
|
|
107
|
+
department || {},
|
|
108
|
+
'properties.department.splitUnit',
|
|
109
|
+
'quantity'
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
const getItemsTotalCount = itemsParam => {
|
|
113
|
+
if (isSplitByPieces(splitUnit))
|
|
114
|
+
return itemActions.getItemsTotalPieces(itemsParam);
|
|
115
|
+
if (isSplitByWeight(splitUnit))
|
|
116
|
+
return itemActions.getItemsTotalWeight(itemsParam);
|
|
117
|
+
return itemActions.getItemsTotalQuantity(itemsParam);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const getItemTotalCount = itemParam => {
|
|
121
|
+
if (isSplitByPieces(splitUnit))
|
|
122
|
+
return itemActions.getTotalPieces(itemParam);
|
|
123
|
+
if (isSplitByWeight(splitUnit))
|
|
124
|
+
return itemActions.getTotalWeight(itemParam);
|
|
125
|
+
return itemActions.getTotalQuantity(itemParam);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const totalCount = getItemsTotalCount(newItems);
|
|
129
|
+
|
|
130
|
+
if (department && autoSplit && maxItems && totalCount > maxItems) {
|
|
102
131
|
newItems.forEach(newItem => {
|
|
103
132
|
for (let j = 0; j < newItem.quantity; j += 1) {
|
|
104
133
|
const item = {
|
|
@@ -106,11 +135,12 @@ module.exports = ({ utils, _, itemActions, modifierActions, settings }) => {
|
|
|
106
135
|
quantity: 1,
|
|
107
136
|
_id: helpers.getObjectID(),
|
|
108
137
|
};
|
|
109
|
-
const
|
|
138
|
+
const totalCountPerItem = getItemTotalCount(item);
|
|
139
|
+
|
|
110
140
|
const index = splitOrders.findIndex(
|
|
111
141
|
newOrder =>
|
|
112
142
|
newOrder.items &&
|
|
113
|
-
|
|
143
|
+
getItemsTotalCount(newOrder.items) + totalCountPerItem <=
|
|
114
144
|
maxItems &&
|
|
115
145
|
departmentName === getDepartmentName(newOrder.items[0])
|
|
116
146
|
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/pricing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.33",
|
|
4
4
|
"description": "Pricing calculator",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"license": "ISC",
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
"supertest": "^6.2.3",
|
|
37
37
|
"supervisor": "^0.12.0"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "fed12242f23f92ff8cad9122afd770e6569f627a"
|
|
40
40
|
}
|