@darkpos/pricing 1.0.44 → 1.0.46
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/getItemsModifierDescription.test.js +75 -85
- package/__TEST__/mocks/scripts/calculate-partially-paid/index.js +2 -2
- package/__TEST__/mocks/scripts/calculate-unpaid/index.js +2 -2
- package/__TEST__/modifier/hasModifier.test.js +1626 -0
- package/__TEST__/order/addItem.test.js +2 -4
- package/__TEST__/order/manualSplit.test.js +104 -0
- package/__TEST__/order/order-payment-modifier.test.js +1117 -0
- package/__TEST__/order/order.test.js +50 -104
- package/__TEST__/order/pickEndDate.test.js +7 -153
- package/__TEST__/order/validateConditionsCalculate.test.js +42 -43
- package/lib/item/calculate.js +67 -9
- package/lib/item/getAmounts.js +14 -0
- package/lib/item/getBalance.js +0 -1
- package/lib/item/getInvalidRequiredModifiers.js +24 -0
- package/lib/item/getItemModifiersDescription.js +2 -1
- package/lib/item/getItemsBalance.js +9 -0
- package/lib/item/getItemsTotals.js +27 -0
- package/lib/item/getPipeModifiers.js +10 -0
- package/lib/item/getTotal.js +3 -1
- package/lib/item/hasModifier.js +21 -9
- package/lib/item/hasModifiers.js +3 -4
- package/lib/item/hasPaymentMethodType.js +17 -0
- package/lib/item/hasPaymentModifierWithPaymentId.js +9 -0
- package/lib/item/index.js +19 -10
- package/lib/item/removePaymentModifiersByPaymentId.js +13 -0
- package/lib/modifier/areConditionsMet.js +3 -13
- package/lib/modifier/calculatePaymentDiscountModifier.js +31 -0
- package/lib/modifier/calculatePaymentFeeModifier.js +27 -0
- package/lib/modifier/calculatePaymentModifier.js +67 -0
- package/lib/modifier/createDiscountModifier.js +2 -1
- package/lib/modifier/createFeeModifier.js +2 -1
- package/lib/modifier/duplicate.js +1 -1
- package/lib/modifier/getChildren.js +8 -0
- package/lib/modifier/getComputedAmount.js +9 -0
- package/lib/modifier/getGroupRelatedModifiers.js +5 -1
- package/lib/modifier/getItemModifiers.js +1 -3
- package/lib/modifier/hasPaymentMethodType.js +14 -0
- package/lib/modifier/index.js +23 -4
- package/lib/modifier/isCalculatedPaymentModifier.js +4 -0
- package/lib/modifier/isChild.js +6 -0
- package/lib/modifier/isDiscount.js +6 -2
- package/lib/modifier/isFee.js +5 -2
- package/lib/modifier/isPaymentMethodModifier.js +4 -1
- package/lib/modifier/isPaymentTypeModifier.js +4 -1
- package/lib/modifier/isPercentage.js +10 -0
- package/lib/modifier/isRelatedModifier.js +13 -0
- package/lib/modifier/removeGroupData.js +20 -0
- package/lib/modifier/sort.js +28 -0
- package/lib/order/addItemModifier.js +8 -1
- package/lib/order/applyPayment.js +61 -0
- package/lib/order/calculate.js +34 -16
- package/lib/order/getBalance.js +0 -1
- package/lib/order/getOrdersBalance.js +8 -0
- package/lib/order/index.js +9 -8
- package/lib/order/manualSplit.js +34 -0
- package/lib/order/manualSplitByQuantity.js +50 -0
- package/lib/store/pickEndDate.js +2 -2
- package/package.json +7 -4
- package/lib/item/getBalanceToPay.js +0 -12
- package/lib/item/getTotals.js +0 -40
- package/lib/item/isSelected.js +0 -13
- package/lib/item/markModifiersAsLocked.js +0 -13
- package/lib/modifier/createPaymentModifier.js +0 -12
- package/lib/modifier/getLockedModifiers.js +0 -5
- package/lib/order/markModifiersAsLocked.js +0 -14
- package/lib/order/removeModifiersWithPaymentMethods.js +0 -28
- package/lib/order/removeModifiersWithPaymentTypes.js +0 -26
|
@@ -6,9 +6,7 @@ module.exports = ({ actions }) =>
|
|
|
6
6
|
});
|
|
7
7
|
|
|
8
8
|
const enabledModifiers = matchedModifiers.filter(
|
|
9
|
-
each =>
|
|
10
|
-
(actions.isDefault(each) || actions.isRequired(each)) &&
|
|
11
|
-
!(actions.isGroupOfValues(each) || actions.isGroupOfModifiers(each))
|
|
9
|
+
each => actions.isDefault(each) || actions.isRequired(each)
|
|
12
10
|
);
|
|
13
11
|
|
|
14
12
|
const relatedModifiers = enabledModifiers
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module.exports = ({ actions }) =>
|
|
2
|
+
function hasPaymentMethodType({
|
|
3
|
+
paymentModifier,
|
|
4
|
+
paymentMethod,
|
|
5
|
+
paymentType,
|
|
6
|
+
}) {
|
|
7
|
+
return (
|
|
8
|
+
actions.isPaymentModifier(paymentModifier) &&
|
|
9
|
+
paymentModifier.conditions.rules.some(
|
|
10
|
+
rule =>
|
|
11
|
+
rule.value.includes(paymentMethod) || rule.value.includes(paymentType)
|
|
12
|
+
)
|
|
13
|
+
);
|
|
14
|
+
};
|
package/lib/modifier/index.js
CHANGED
|
@@ -117,7 +117,6 @@ const isCredit = require('./isCredit');
|
|
|
117
117
|
const purifyModifiers = require('./purifyModifiers');
|
|
118
118
|
const sort = require('./sort');
|
|
119
119
|
const isPaymentModifier = require('./isPaymentModifier');
|
|
120
|
-
const createPaymentModifier = require('./createPaymentModifier');
|
|
121
120
|
const isSubtract = require('./isSubtract');
|
|
122
121
|
const isFixed = require('./isFixed');
|
|
123
122
|
const isFixedAdd = require('./isFixedAdd');
|
|
@@ -127,7 +126,8 @@ const mutateModifier = require('./mutateModifier');
|
|
|
127
126
|
const isFixedDiscount = require('./isFixedDiscount');
|
|
128
127
|
const removeLocked = require('./removeLocked');
|
|
129
128
|
const hasItems = require('./hasItems');
|
|
130
|
-
const
|
|
129
|
+
const removeGroupData = require('./removeGroupData');
|
|
130
|
+
const isRelatedModifier = require('./isRelatedModifier');
|
|
131
131
|
const getGroupedModifiers = require('./getGroupedModifiers');
|
|
132
132
|
const getNotesToModifierTags = require('./getNotesToModifierTags');
|
|
133
133
|
const isOptionsOverride = require('./isOptionsOverride');
|
|
@@ -137,6 +137,15 @@ const validateNumberCondition = require('./validateNumberCondition');
|
|
|
137
137
|
const validateRequiredModifiers = require('./validateRequiredModifiers');
|
|
138
138
|
const validateDateDaysDiff = require('./validateDateDaysDiff');
|
|
139
139
|
const validateInArr = require('./validateInArr');
|
|
140
|
+
const isPercentage = require('./isPercentage');
|
|
141
|
+
const getChildren = require('./getChildren');
|
|
142
|
+
const getComputedAmount = require('./getComputedAmount');
|
|
143
|
+
const hasPaymentMethodType = require('./hasPaymentMethodType');
|
|
144
|
+
const calculatePaymentModifier = require('./calculatePaymentModifier');
|
|
145
|
+
const calculatePaymentDiscountModifier = require('./calculatePaymentDiscountModifier');
|
|
146
|
+
const calculatePaymentFeeModifier = require('./calculatePaymentFeeModifier');
|
|
147
|
+
const isCalculatedPaymentModifier = require('./isCalculatedPaymentModifier');
|
|
148
|
+
const isChild = require('./isChild');
|
|
140
149
|
|
|
141
150
|
const modifierActions = (deps = {}) => {
|
|
142
151
|
const actions = {};
|
|
@@ -265,17 +274,17 @@ const modifierActions = (deps = {}) => {
|
|
|
265
274
|
purifyModifiers: purifyModifiers(innerDeps),
|
|
266
275
|
sort: sort(innerDeps),
|
|
267
276
|
isPaymentModifier: isPaymentModifier(innerDeps),
|
|
268
|
-
createPaymentModifier: createPaymentModifier(innerDeps),
|
|
269
277
|
isSubtract: isSubtract(innerDeps),
|
|
270
278
|
isFixed: isFixed(innerDeps),
|
|
271
279
|
isFixedDiscount: isFixedDiscount(innerDeps),
|
|
272
280
|
removeLocked: removeLocked(innerDeps),
|
|
273
281
|
hasItems: hasItems(innerDeps),
|
|
274
|
-
getLockedModifiers: getLockedModifiers(innerDeps),
|
|
275
282
|
isFixedAdd: isFixedAdd(innerDeps),
|
|
276
283
|
isFee: isFee(innerDeps),
|
|
277
284
|
isAdd: isAdd(innerDeps),
|
|
278
285
|
mutateModifier: mutateModifier(innerDeps),
|
|
286
|
+
removeGroupData: removeGroupData(innerDeps),
|
|
287
|
+
isRelatedModifier: isRelatedModifier(innerDeps),
|
|
279
288
|
getGroupedModifiers: getGroupedModifiers(innerDeps),
|
|
280
289
|
getNotesToModifierTags: getNotesToModifierTags(innerDeps),
|
|
281
290
|
isOptionsOverride: isOptionsOverride(innerDeps),
|
|
@@ -286,6 +295,16 @@ const modifierActions = (deps = {}) => {
|
|
|
286
295
|
validateDateDaysDiff: validateDateDaysDiff(innerDeps),
|
|
287
296
|
validateInArr: validateInArr(innerDeps),
|
|
288
297
|
areConditionsMet: areConditionsMet(innerDeps),
|
|
298
|
+
isPercentage: isPercentage(innerDeps),
|
|
299
|
+
getChildren: getChildren(innerDeps),
|
|
300
|
+
getComputedAmount: getComputedAmount(innerDeps),
|
|
301
|
+
hasPaymentMethodType: hasPaymentMethodType(innerDeps),
|
|
302
|
+
calculatePaymentModifier: calculatePaymentModifier(innerDeps),
|
|
303
|
+
calculatePaymentDiscountModifier:
|
|
304
|
+
calculatePaymentDiscountModifier(innerDeps),
|
|
305
|
+
calculatePaymentFeeModifier: calculatePaymentFeeModifier(innerDeps),
|
|
306
|
+
isCalculatedPaymentModifier: isCalculatedPaymentModifier(innerDeps),
|
|
307
|
+
isChild: isChild(innerDeps),
|
|
289
308
|
});
|
|
290
309
|
|
|
291
310
|
Object.keys(freezedActions).forEach(actionName => {
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
module.exports = ({ constants }) => {
|
|
1
|
+
module.exports = ({ constants, actions }) => {
|
|
2
2
|
const { Modifier } = constants;
|
|
3
3
|
return function isDiscount(modifier) {
|
|
4
|
-
return !!(
|
|
4
|
+
return !!(
|
|
5
|
+
modifier &&
|
|
6
|
+
(modifier.type === Modifier.Types.DISCOUNT ||
|
|
7
|
+
actions.isSubtract(modifier))
|
|
8
|
+
);
|
|
5
9
|
};
|
|
6
10
|
};
|
package/lib/modifier/isFee.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
module.exports = ({ constants }) => {
|
|
1
|
+
module.exports = ({ constants, actions }) => {
|
|
2
2
|
const { Modifier } = constants;
|
|
3
3
|
return function isFee(modifier) {
|
|
4
|
-
return !!(
|
|
4
|
+
return !!(
|
|
5
|
+
modifier &&
|
|
6
|
+
(modifier.type === Modifier.Types.FEE || actions.isAdd(modifier))
|
|
7
|
+
);
|
|
5
8
|
};
|
|
6
9
|
};
|
|
@@ -3,6 +3,9 @@ module.exports = () =>
|
|
|
3
3
|
return !!(
|
|
4
4
|
modifier &&
|
|
5
5
|
modifier.conditions &&
|
|
6
|
-
modifier.conditions.
|
|
6
|
+
Array.isArray(modifier.conditions.rules) &&
|
|
7
|
+
modifier.conditions.rules.some(
|
|
8
|
+
condition => condition.key === 'paymentMethods'
|
|
9
|
+
)
|
|
7
10
|
);
|
|
8
11
|
};
|
|
@@ -3,6 +3,9 @@ module.exports = () =>
|
|
|
3
3
|
return !!(
|
|
4
4
|
modifier &&
|
|
5
5
|
modifier.conditions &&
|
|
6
|
-
modifier.conditions.
|
|
6
|
+
Array.isArray(modifier.conditions.rules) &&
|
|
7
|
+
modifier.conditions.rules.some(
|
|
8
|
+
condition => condition.key === 'paymentTypes'
|
|
9
|
+
)
|
|
7
10
|
);
|
|
8
11
|
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module.exports = ({ actions }) =>
|
|
2
|
+
function isRelatedModifier({ _id, modifier }) {
|
|
3
|
+
if (!modifier || !_id) return false;
|
|
4
|
+
|
|
5
|
+
if (actions.isGroupOfModifiers(modifier)) {
|
|
6
|
+
const relatedModifiers = modifier.properties.group.modifiers;
|
|
7
|
+
return [actions.removeGroupData({ modifier }), ...relatedModifiers].some(
|
|
8
|
+
relatedModifier => isRelatedModifier({ _id, modifier: relatedModifier })
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return modifier._id === _id || modifier.modifierId === _id;
|
|
13
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module.exports = ({ constants }) => {
|
|
2
|
+
const { Modifier } = constants;
|
|
3
|
+
|
|
4
|
+
return function removeGroupData({ modifier }) {
|
|
5
|
+
if (!modifier) return modifier;
|
|
6
|
+
|
|
7
|
+
return {
|
|
8
|
+
...modifier,
|
|
9
|
+
properties:
|
|
10
|
+
modifier.properties && modifier.properties.group
|
|
11
|
+
? { ...modifier.properties, group: null }
|
|
12
|
+
: modifier.properties,
|
|
13
|
+
attributes: Array.isArray(modifier.attributes)
|
|
14
|
+
? modifier.attributes.filter(
|
|
15
|
+
attribute => attribute !== Modifier.Attributes.GROUP
|
|
16
|
+
)
|
|
17
|
+
: modifier.attributes,
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
};
|
package/lib/modifier/sort.js
CHANGED
|
@@ -6,6 +6,34 @@ module.exports = ({ actions }) => {
|
|
|
6
6
|
const aVal = actions.getProperty(a, key);
|
|
7
7
|
const bVal = actions.getProperty(b, key);
|
|
8
8
|
|
|
9
|
+
const aIsPaymentModifier = actions.isPaymentModifier(a);
|
|
10
|
+
const bIsPaymentModifier = actions.isPaymentModifier(b);
|
|
11
|
+
const aIsCalculatedPaymentModifier =
|
|
12
|
+
actions.isCalculatedPaymentModifier(a);
|
|
13
|
+
const bIsCalculatedPaymentModifier =
|
|
14
|
+
actions.isCalculatedPaymentModifier(b);
|
|
15
|
+
|
|
16
|
+
if (aIsCalculatedPaymentModifier && bIsPaymentModifier && aVal === null) {
|
|
17
|
+
return -1;
|
|
18
|
+
}
|
|
19
|
+
if (bIsCalculatedPaymentModifier && aIsPaymentModifier && bVal === null) {
|
|
20
|
+
return -1;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (
|
|
24
|
+
(aIsPaymentModifier || aIsCalculatedPaymentModifier) &&
|
|
25
|
+
aVal === null
|
|
26
|
+
) {
|
|
27
|
+
return 1;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (
|
|
31
|
+
(bIsPaymentModifier || bIsCalculatedPaymentModifier) &&
|
|
32
|
+
bVal === null
|
|
33
|
+
) {
|
|
34
|
+
return 1;
|
|
35
|
+
}
|
|
36
|
+
|
|
9
37
|
if (aVal == null && bVal === null) {
|
|
10
38
|
return 0;
|
|
11
39
|
}
|
|
@@ -173,7 +173,14 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
|
|
|
173
173
|
const customer = actions.getCustomer(order);
|
|
174
174
|
const contains =
|
|
175
175
|
!_.isEmpty(item.modifiers) &&
|
|
176
|
-
modifierActions.contains(
|
|
176
|
+
modifierActions.contains(
|
|
177
|
+
item.modifiers.filter(
|
|
178
|
+
mod =>
|
|
179
|
+
!modifierActions.isRequired(mod) &&
|
|
180
|
+
!modifierActions.isGroupOfModifiers(mod)
|
|
181
|
+
),
|
|
182
|
+
modifier
|
|
183
|
+
);
|
|
177
184
|
|
|
178
185
|
const usingCount = contains
|
|
179
186
|
? modifierActions.getUsingCount(item.modifiers, modifier)
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
const options = {
|
|
3
|
+
"5d9f80ae6065e52e04686e03": {
|
|
4
|
+
amount: 10,
|
|
5
|
+
items: {
|
|
6
|
+
"5d9f80dc6065e52e04686e3c": {
|
|
7
|
+
status: { picked: { value: true } },
|
|
8
|
+
total: 0,
|
|
9
|
+
totalPaid: 5,
|
|
10
|
+
},
|
|
11
|
+
"5d9f80dc6065e52e04686e3c": {
|
|
12
|
+
status: { picked: { value: true } },
|
|
13
|
+
total: 0,
|
|
14
|
+
totalPaid: 5,
|
|
15
|
+
},
|
|
16
|
+
"5d9f80dc6065e52e04686e3c": {
|
|
17
|
+
status: { picked: { value: true } },
|
|
18
|
+
total: 0,
|
|
19
|
+
totalPaid: 5,
|
|
20
|
+
},
|
|
21
|
+
"5d9f80dc6065e52e04686e3c": {
|
|
22
|
+
status: { picked: { value: true } },
|
|
23
|
+
total: 0,
|
|
24
|
+
totalPaid: 5,
|
|
25
|
+
},
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
*/
|
|
30
|
+
module.exports = ({ utils, itemActions }) =>
|
|
31
|
+
function applyPayment(options, amountToProcess = 0) {
|
|
32
|
+
const optionsWithAmount = options;
|
|
33
|
+
let amount = amountToProcess;
|
|
34
|
+
|
|
35
|
+
Object.keys(options).forEach(orderId => {
|
|
36
|
+
const order = options[orderId];
|
|
37
|
+
const { items } = order;
|
|
38
|
+
|
|
39
|
+
Object.keys(items).forEach(itemId => {
|
|
40
|
+
const item = items[itemId];
|
|
41
|
+
|
|
42
|
+
const { amountToAssign, remainingAmount, isPaid } =
|
|
43
|
+
itemActions.getAmounts(item, amount);
|
|
44
|
+
amount = remainingAmount;
|
|
45
|
+
|
|
46
|
+
// assign amount to item
|
|
47
|
+
items[itemId] = {
|
|
48
|
+
...item,
|
|
49
|
+
orderId,
|
|
50
|
+
amount: amountToAssign,
|
|
51
|
+
totalPaid: utils.math.add(item.totalPaid, amountToAssign),
|
|
52
|
+
status: {
|
|
53
|
+
...item.status,
|
|
54
|
+
paid: { value: isPaid, date: new Date() },
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return optionsWithAmount;
|
|
61
|
+
};
|
package/lib/order/calculate.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* return calculated Order
|
|
4
4
|
*/
|
|
5
|
-
module.exports = ({ _, actions, itemActions, modifierActions }) =>
|
|
5
|
+
module.exports = ({ _, actions, itemActions, modifierActions, utils }) =>
|
|
6
6
|
function calculateorder(inputOrder, opts = {}) {
|
|
7
7
|
if (!inputOrder) return inputOrder;
|
|
8
8
|
const order = _.cloneDeep(inputOrder);
|
|
@@ -20,24 +20,29 @@ module.exports = ({ _, actions, itemActions, modifierActions }) =>
|
|
|
20
20
|
startRequestDate,
|
|
21
21
|
endRequestDate,
|
|
22
22
|
};
|
|
23
|
-
const sortedOrderModifiers = modifierActions.sort(
|
|
24
|
-
modifierActions.removeLocked(order.modifiers)
|
|
25
|
-
);
|
|
23
|
+
const sortedOrderModifiers = modifierActions.sort(order.modifiers || []);
|
|
26
24
|
|
|
27
25
|
let itemsWNIM = items.map(item =>
|
|
28
26
|
itemActions.removeModifiers({ item, modifiers: sortedOrderModifiers })
|
|
29
27
|
);
|
|
30
28
|
|
|
31
|
-
itemsWNIM = itemActions.calculate(itemsWNIM,
|
|
29
|
+
itemsWNIM = itemActions.calculate(itemsWNIM, {
|
|
30
|
+
...options,
|
|
31
|
+
lockPaymentModifiers: !!(
|
|
32
|
+
options.lockPaymentModifiers && !sortedOrderModifiers.length
|
|
33
|
+
),
|
|
34
|
+
});
|
|
32
35
|
|
|
33
36
|
const newOrder = {
|
|
34
37
|
...order,
|
|
35
|
-
...itemActions.
|
|
38
|
+
...itemActions.getItemsTotals(itemsWNIM),
|
|
36
39
|
items: itemsWNIM,
|
|
37
40
|
};
|
|
38
41
|
|
|
39
42
|
if (!sortedOrderModifiers.length) return newOrder;
|
|
40
43
|
|
|
44
|
+
itemsWNIM = itemActions.calculate(itemsWNIM, {});
|
|
45
|
+
|
|
41
46
|
let tempItems = itemsWNIM || items;
|
|
42
47
|
let itemsWIM = _.cloneDeep(tempItems);
|
|
43
48
|
let prvSort = null;
|
|
@@ -65,21 +70,14 @@ module.exports = ({ _, actions, itemActions, modifierActions }) =>
|
|
|
65
70
|
|
|
66
71
|
if (isCredit) {
|
|
67
72
|
const calculatedItemWIM = itemActions.calculate(itemsWIM, options);
|
|
68
|
-
computedTotal = itemActions.
|
|
73
|
+
computedTotal = itemActions.getItemsTotals(calculatedItemWIM).total;
|
|
69
74
|
}
|
|
70
75
|
|
|
71
76
|
if (modifierActions.hasItems({ modifier })) {
|
|
72
77
|
tempItems = itemActions.getItems(tempItems, modifier.items);
|
|
73
78
|
}
|
|
74
79
|
|
|
75
|
-
|
|
76
|
-
itemActions.removeModifiers({
|
|
77
|
-
item: tempItem,
|
|
78
|
-
modifiers: modifierActions.getLockedModifiers(tempItem.modifiers),
|
|
79
|
-
})
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
computedTotal = itemActions.getTotals(tempItems).total;
|
|
80
|
+
computedTotal = itemActions.getItemsTotals(tempItems).total;
|
|
83
81
|
|
|
84
82
|
const itemsModifiers = itemActions.addIndirectModifier({
|
|
85
83
|
orderTotal: computedTotal,
|
|
@@ -99,9 +97,29 @@ module.exports = ({ _, actions, itemActions, modifierActions }) =>
|
|
|
99
97
|
|
|
100
98
|
const calculatedItemWIM = itemActions.calculate(itemsWIM, options);
|
|
101
99
|
|
|
100
|
+
if (calculatedItemWIM.length > 1) {
|
|
101
|
+
const { total, subTotalsTotal } = calculatedItemWIM.reduce(
|
|
102
|
+
(acc, item) => ({
|
|
103
|
+
total: utils.math.add(acc.total, item.total),
|
|
104
|
+
subTotalsTotal: utils.math.add(
|
|
105
|
+
acc.subTotalsTotal,
|
|
106
|
+
utils.math.add(item.subTotals._actual, item.subTotals._xincluded)
|
|
107
|
+
),
|
|
108
|
+
}),
|
|
109
|
+
{ total: 0, subTotalsTotal: 0 }
|
|
110
|
+
);
|
|
111
|
+
const difference = utils.math.sub(subTotalsTotal, total);
|
|
112
|
+
|
|
113
|
+
if (difference > 0) {
|
|
114
|
+
calculatedItemWIM[0].total = utils.math.toDecimalPlaces(
|
|
115
|
+
utils.math.add(calculatedItemWIM[0].total, difference)
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
102
120
|
return {
|
|
103
121
|
...order,
|
|
104
|
-
...itemActions.
|
|
122
|
+
...itemActions.getItemsTotals(calculatedItemWIM),
|
|
105
123
|
items: calculatedItemWIM,
|
|
106
124
|
};
|
|
107
125
|
};
|
package/lib/order/getBalance.js
CHANGED
package/lib/order/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
//
|
|
2
2
|
const getTotalPieces = require('./getTotalPieces');
|
|
3
3
|
const getStartDate = require('./getStartDate');
|
|
4
|
-
const
|
|
4
|
+
const getOrdersBalance = require('./getOrdersBalance');
|
|
5
5
|
const getEndDate = require('./getEndDate');
|
|
6
6
|
const isNew = require('./isNew');
|
|
7
7
|
const getSelectedItem = require('./getSelectedItem');
|
|
@@ -57,8 +57,6 @@ const removeApplyModifier = require('./removeApplyModifier');
|
|
|
57
57
|
const getCustomerSubscriptions = require('./getCustomerSubscriptions');
|
|
58
58
|
const getCustomerSubscriptionsByItem = require('./getCustomerSubscriptionsByItem');
|
|
59
59
|
const hasRemainingSubscription = require('./hasRemainingSubscription');
|
|
60
|
-
const removeModifiersWithPaymentMethods = require('./removeModifiersWithPaymentMethods');
|
|
61
|
-
const removeModifiersWithPaymentTypes = require('./removeModifiersWithPaymentTypes');
|
|
62
60
|
const getItemsToPay = require('./getItemsToPay');
|
|
63
61
|
const hasItem = require('./hasItem');
|
|
64
62
|
const getNumberOfItems = require('./getNumberOfItems');
|
|
@@ -82,11 +80,14 @@ const splitAndCalculate = require('./splitAndCalculate');
|
|
|
82
80
|
const spreadModifiers = require('./spreadModifiers');
|
|
83
81
|
const validateCreditModifiersTotal = require('./validateCreditModifiersTotal');
|
|
84
82
|
const validateFixedModifiersTotal = require('./validateFixedModifiersTotal');
|
|
85
|
-
const markModifiersAsLocked = require('./markModifiersAsLocked');
|
|
86
83
|
const removeModifiers = require('./removeModifiers');
|
|
87
84
|
const addModifiers = require('./addModifiers');
|
|
88
85
|
const createSuborder = require('./createSuborder');
|
|
89
86
|
const getSuborder = require('./getSuborder');
|
|
87
|
+
const manualSplit = require('./manualSplit');
|
|
88
|
+
const manualSplitByQuantity = require('./manualSplitByQuantity');
|
|
89
|
+
const applyPayment = require('./applyPayment');
|
|
90
|
+
const getBalance = require('./getBalance');
|
|
90
91
|
|
|
91
92
|
const orderActions = (deps = {}) => {
|
|
92
93
|
const actions = {};
|
|
@@ -156,9 +157,6 @@ const orderActions = (deps = {}) => {
|
|
|
156
157
|
getCustomerSubscriptions: getCustomerSubscriptions(innerDeps),
|
|
157
158
|
getCustomerSubscriptionsByItem: getCustomerSubscriptionsByItem(innerDeps),
|
|
158
159
|
hasRemainingSubscription: hasRemainingSubscription(innerDeps),
|
|
159
|
-
removeModifiersWithPaymentMethods:
|
|
160
|
-
removeModifiersWithPaymentMethods(innerDeps),
|
|
161
|
-
removeModifiersWithPaymentTypes: removeModifiersWithPaymentTypes(innerDeps),
|
|
162
160
|
hasItem: hasItem(innerDeps),
|
|
163
161
|
getNumberOfItems: getNumberOfItems(innerDeps),
|
|
164
162
|
isOpen: isOpen(innerDeps),
|
|
@@ -181,11 +179,14 @@ const orderActions = (deps = {}) => {
|
|
|
181
179
|
spreadModifiers: spreadModifiers(innerDeps),
|
|
182
180
|
validateCreditModifiersTotal: validateCreditModifiersTotal(innerDeps),
|
|
183
181
|
validateFixedModifiersTotal: validateFixedModifiersTotal(innerDeps),
|
|
184
|
-
markModifiersAsLocked: markModifiersAsLocked(innerDeps),
|
|
185
182
|
removeModifiers: removeModifiers(innerDeps),
|
|
186
183
|
addModifiers: addModifiers(innerDeps),
|
|
187
184
|
createSuborder: createSuborder(innerDeps),
|
|
188
185
|
getSuborder: getSuborder(innerDeps),
|
|
186
|
+
manualSplit: manualSplit(innerDeps),
|
|
187
|
+
manualSplitByQuantity: manualSplitByQuantity(innerDeps),
|
|
188
|
+
applyPayment: applyPayment(innerDeps),
|
|
189
|
+
getOrdersBalance: getOrdersBalance(innerDeps),
|
|
189
190
|
});
|
|
190
191
|
|
|
191
192
|
Object.keys(freezedActions).forEach(actionName => {
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module.exports = ({ actions }) =>
|
|
2
|
+
function manualSplit(
|
|
3
|
+
{ orderIndex, itemIndex },
|
|
4
|
+
destinationIndex,
|
|
5
|
+
currentOrder,
|
|
6
|
+
subOrders,
|
|
7
|
+
itemId,
|
|
8
|
+
splitQuantity
|
|
9
|
+
) {
|
|
10
|
+
let _subOrders = subOrders;
|
|
11
|
+
|
|
12
|
+
if (
|
|
13
|
+
orderIndex == null ||
|
|
14
|
+
itemIndex == null ||
|
|
15
|
+
subOrders == null ||
|
|
16
|
+
destinationIndex == null
|
|
17
|
+
)
|
|
18
|
+
return _subOrders;
|
|
19
|
+
if (!subOrders[destinationIndex] || !subOrders[orderIndex])
|
|
20
|
+
return _subOrders;
|
|
21
|
+
if (splitQuantity) {
|
|
22
|
+
_subOrders = actions.manualSplitByQuantity(
|
|
23
|
+
{ orderIndex, itemIndex },
|
|
24
|
+
destinationIndex,
|
|
25
|
+
currentOrder,
|
|
26
|
+
subOrders,
|
|
27
|
+
itemId
|
|
28
|
+
);
|
|
29
|
+
} else {
|
|
30
|
+
const item = _subOrders[orderIndex].items.splice(itemIndex, 1)[0];
|
|
31
|
+
if (item) _subOrders[destinationIndex].items.unshift(item);
|
|
32
|
+
}
|
|
33
|
+
return _subOrders.map(subOrder => actions.calculate(subOrder));
|
|
34
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module.exports = () =>
|
|
2
|
+
function splitByQuantity(
|
|
3
|
+
{ orderIndex, itemIndex },
|
|
4
|
+
destinationIndex,
|
|
5
|
+
currentOrder,
|
|
6
|
+
subOrders,
|
|
7
|
+
itemId
|
|
8
|
+
) {
|
|
9
|
+
const _subOrders = subOrders;
|
|
10
|
+
const currentItem = currentOrder.items.find(item => item._id === itemId);
|
|
11
|
+
const currentSuborder = subOrders[Number(orderIndex)];
|
|
12
|
+
const currentSuborderItem = (currentSuborder.items || []).find(
|
|
13
|
+
item => item._id === itemId
|
|
14
|
+
);
|
|
15
|
+
const destinationSuborder = subOrders[Number(destinationIndex)];
|
|
16
|
+
let destinationItem = (destinationSuborder.items || []).find(
|
|
17
|
+
item => item._id === itemId
|
|
18
|
+
);
|
|
19
|
+
if (orderIndex !== destinationIndex) {
|
|
20
|
+
for (let i = 0; i <= subOrders.length; i += 1) {
|
|
21
|
+
if (i === orderIndex) {
|
|
22
|
+
if (currentSuborderItem && currentSuborder && currentSuborder.items) {
|
|
23
|
+
// react breaks if I don't create a clone to reassign the quantity
|
|
24
|
+
const _currentSuborderItem = { ...currentSuborderItem };
|
|
25
|
+
_currentSuborderItem.quantity =
|
|
26
|
+
Number(_currentSuborderItem.quantity) - 1;
|
|
27
|
+
currentSuborder.items[Number(itemIndex)] = _currentSuborderItem;
|
|
28
|
+
if (Number(_currentSuborderItem.quantity) <= 0) {
|
|
29
|
+
currentSuborder.items.splice(Number(itemIndex), 1);
|
|
30
|
+
}
|
|
31
|
+
_subOrders[Number(orderIndex)] = currentSuborder;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (i === destinationIndex) {
|
|
36
|
+
// case 1: if the suborder has no items
|
|
37
|
+
if (!destinationItem) {
|
|
38
|
+
destinationItem = { ...currentItem, quantity: 1 };
|
|
39
|
+
destinationSuborder.items =
|
|
40
|
+
destinationSuborder.items.concat(destinationItem);
|
|
41
|
+
} else {
|
|
42
|
+
// case 2: if the suborder has items
|
|
43
|
+
destinationItem.quantity = Number(destinationItem.quantity) + 1;
|
|
44
|
+
}
|
|
45
|
+
_subOrders[Number(destinationIndex)] = destinationSuborder;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return _subOrders;
|
|
50
|
+
};
|
package/lib/store/pickEndDate.js
CHANGED
|
@@ -46,8 +46,6 @@ module.exports = ({ settings, _, moment }) => {
|
|
|
46
46
|
/**
|
|
47
47
|
* setting the hour and minute so we can compare with closedDays
|
|
48
48
|
*/
|
|
49
|
-
if (hour !== undefined) endDateTZ.set('hour', hour);
|
|
50
|
-
if (minute !== undefined) endDateTZ.set('minute', minute);
|
|
51
49
|
closedDays.forEach(closed => {
|
|
52
50
|
const closedDay = moment(closed.date);
|
|
53
51
|
closedDay.set('hour', hour);
|
|
@@ -56,6 +54,8 @@ module.exports = ({ settings, _, moment }) => {
|
|
|
56
54
|
endDateTZ.add(1, 'days');
|
|
57
55
|
}
|
|
58
56
|
});
|
|
57
|
+
if (hour !== undefined) endDateTZ.set('hour', hour);
|
|
58
|
+
if (minute !== undefined) endDateTZ.set('minute', minute);
|
|
59
59
|
|
|
60
60
|
return moment.utc(endDateTZ).format();
|
|
61
61
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/pricing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.46",
|
|
4
4
|
"description": "Pricing calculator",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"license": "ISC",
|
|
@@ -12,8 +12,11 @@
|
|
|
12
12
|
"main": "index.js",
|
|
13
13
|
"scripts": {
|
|
14
14
|
"test": "jest --runInBand --detectOpenHandles --logHeapUsage --verbose --forceExit ./__TEST__",
|
|
15
|
-
"test:me": "jest --runInBand --detectOpenHandles --logHeapUsage
|
|
16
|
-
"
|
|
15
|
+
"test:me": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/order-payment-modifier.test.js",
|
|
16
|
+
"test:order": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/order.test.js",
|
|
17
|
+
"test:validateConditions": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/validateConditionsCalculate.test.js",
|
|
18
|
+
"test:hasModifier": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/modifier/hasModifier.test.js",
|
|
19
|
+
"lint": "eslint --quiet lib/"
|
|
17
20
|
},
|
|
18
21
|
"publishConfig": {
|
|
19
22
|
"access": "public"
|
|
@@ -36,5 +39,5 @@
|
|
|
36
39
|
"supertest": "^6.2.3",
|
|
37
40
|
"supervisor": "^0.12.0"
|
|
38
41
|
},
|
|
39
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "e048578555dc3d2147bbc4e5b4e0d55809261dcc"
|
|
40
43
|
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
module.exports = ({ utils, actions }) =>
|
|
2
|
-
function getBalanceToPay({ orderItems = [] }) {
|
|
3
|
-
const total = orderItems.reduce(
|
|
4
|
-
(t, item) => utils.math.add(actions.getTotal(item), t),
|
|
5
|
-
0
|
|
6
|
-
);
|
|
7
|
-
const totalPaid = orderItems.reduce(
|
|
8
|
-
(t, item) => utils.math.add(t, item && item.totalPaid),
|
|
9
|
-
0
|
|
10
|
-
);
|
|
11
|
-
return utils.math.toDecimalPlaces(utils.math.sub(total, totalPaid));
|
|
12
|
-
};
|