@darkpos/pricing 1.0.43 → 1.0.45
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 -78
- package/__TEST__/mocks/addItemMock.js +18729 -19357
- package/__TEST__/mocks/partially-paid/order-modifiers.json +48 -51
- package/__TEST__/mocks/partially-paid/order-partially-paid.json +893 -860
- package/__TEST__/mocks/scripts/calculate-partially-paid/index.js +2 -2
- package/__TEST__/mocks/scripts/calculate-unpaid/index.js +2 -2
- package/__TEST__/mocks/unpaid/order-modifiers.json +48 -52
- package/__TEST__/modifier/calculate.test.js +1 -0
- package/__TEST__/modifier/getMatchTagsModifiers.test.js +72 -18
- package/__TEST__/modifier/getModifierIndex.test.js +10 -7
- package/__TEST__/modifier/getRecommendedModifiers.test.js +6 -4
- package/__TEST__/modifier/hasAttribute.test.js +11 -5
- package/__TEST__/modifier/hasMatchTags.test.js +33 -11
- package/__TEST__/modifier/sort.test.js +1 -1
- package/__TEST__/order/conditionsNotMet.test.js +133 -0
- package/__TEST__/order/manualSplit.test.js +104 -0
- package/__TEST__/order/order-payment-modifier.test.js +1117 -0
- package/__TEST__/order/order.test.js +53 -105
- package/__TEST__/order/pickEndDate.test.js +7 -153
- package/__TEST__/order/validateConditionsCalculate.test.js +396 -0
- package/lib/constants/index.js +1 -1
- package/lib/index.js +11 -2
- package/lib/item/calculate.js +93 -18
- package/lib/item/getAmounts.js +14 -0
- package/lib/item/getBalance.js +0 -1
- package/lib/item/getItemModifiersDescription.js +1 -0
- package/lib/item/getItemsBalance.js +9 -0
- package/lib/item/getItemsTotals.js +27 -0
- package/lib/item/getTotal.js +3 -1
- package/lib/item/hasPaymentMethodType.js +17 -0
- package/lib/item/hasPaymentModifierWithPaymentId.js +9 -0
- package/lib/item/index.js +15 -8
- package/lib/item/removePaymentModifiersByPaymentId.js +13 -0
- package/lib/modifier/areConditionsMet.js +61 -0
- 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/getChildren.js +8 -0
- package/lib/modifier/getComputedAmount.js +9 -0
- package/lib/modifier/hasPaymentMethodType.js +14 -0
- package/lib/modifier/index.js +33 -9
- 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/isValid.js +12 -0
- package/lib/modifier/sort.js +28 -0
- package/lib/modifier/validate.js +14 -0
- package/lib/modifier/validateDateDaysDiff.js +30 -0
- package/lib/modifier/validateInArr.js +12 -0
- package/lib/modifier/validateNumberCondition.js +20 -0
- package/lib/modifier/validateRequiredModifiers.js +16 -0
- package/lib/order/addItemModifier.js +72 -28
- package/lib/order/applyPayment.js +61 -0
- package/lib/order/calculate.js +45 -22
- package/lib/order/getBalance.js +0 -1
- package/lib/order/getOrdersBalance.js +8 -0
- package/lib/order/index.js +9 -10
- package/lib/order/manualSplit.js +34 -0
- package/lib/order/manualSplitByQuantity.js +50 -0
- package/lib/store/getRecommendedEndDate.js +13 -0
- package/lib/store/index.js +25 -0
- package/package.json +6 -4
- package/lib/item/getBalanceToPay.js +0 -12
- package/lib/item/getTotals.js +0 -40
- package/lib/item/markModifiersAsLocked.js +0 -11
- package/lib/modifier/createPaymentModifier.js +0 -12
- package/lib/modifier/findByPaymentMethod.js +0 -10
- package/lib/modifier/findByPaymentType.js +0 -10
- package/lib/modifier/getLockedModifiers.js +0 -5
- package/lib/order/markModifiersAsLocked.js +0 -14
- package/lib/order/removeModifiersWithPaymentMethods.js +0 -29
- package/lib/order/removeModifiersWithPaymentTypes.js +0 -27
- package/lib/{order → store}/pickEndDate.js +2 -2
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module.exports = ({ actions, _ }) =>
|
|
2
|
+
function validateRequiredModifiers(modifiers, requiredModifiers, operand) {
|
|
3
|
+
if (_.isEmpty(requiredModifiers) && !operand) return true;
|
|
4
|
+
switch (operand) {
|
|
5
|
+
case '$in':
|
|
6
|
+
return requiredModifiers.some(each =>
|
|
7
|
+
actions.contains(modifiers, each)
|
|
8
|
+
);
|
|
9
|
+
case '$nin':
|
|
10
|
+
return requiredModifiers.every(
|
|
11
|
+
each => !actions.contains(modifiers, each)
|
|
12
|
+
);
|
|
13
|
+
default:
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
@@ -27,31 +27,71 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
|
|
|
27
27
|
return false;
|
|
28
28
|
};
|
|
29
29
|
|
|
30
|
-
const validateItemPiecesCondition = (itemPieces, minPieces) => {
|
|
31
|
-
if (!minPieces) return true;
|
|
32
|
-
return minPieces <= itemPieces;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const validateItemQtyCondition = (itemQuantity, minQuantity) => {
|
|
36
|
-
if (!minQuantity) return true;
|
|
37
|
-
return minQuantity <= itemQuantity;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
const validateRequiredModifiers = (modifiers, requiredModifiers) => {
|
|
41
|
-
if (_.isEmpty(modifiers) || _.isEmpty(requiredModifiers)) return true;
|
|
42
|
-
return requiredModifiers.every(each =>
|
|
43
|
-
modifierActions.contains(modifiers, each)
|
|
44
|
-
);
|
|
45
|
-
};
|
|
46
|
-
|
|
47
30
|
const areConditionsMet = (item, conditions) => {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
validateRequiredModifiers(item.modifiers, conditions.modifiers)
|
|
31
|
+
const conditionsBag = [];
|
|
32
|
+
if (!conditions || !conditions.rules) return conditionsBag;
|
|
33
|
+
// Find conditions with itemPieces as key
|
|
34
|
+
const itemPiecesConditions = conditions.rules.filter(
|
|
35
|
+
each => each.key === 'itemPieces'
|
|
54
36
|
);
|
|
37
|
+
if (itemPiecesConditions.length > 0) {
|
|
38
|
+
itemPiecesConditions.forEach(each => {
|
|
39
|
+
if (
|
|
40
|
+
!modifierActions.validateNumberCondition(
|
|
41
|
+
item.pieces,
|
|
42
|
+
each.value,
|
|
43
|
+
each.operand
|
|
44
|
+
)
|
|
45
|
+
) {
|
|
46
|
+
conditionsBag.push({
|
|
47
|
+
name: `${each.operand}.itemPieces`,
|
|
48
|
+
value: each.value,
|
|
49
|
+
current: item.pieces,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
const itemQtyConditions = conditions.rules.filter(
|
|
55
|
+
each => each.key === 'itemQuantity'
|
|
56
|
+
);
|
|
57
|
+
if (itemQtyConditions.length > 0) {
|
|
58
|
+
itemQtyConditions.forEach(each => {
|
|
59
|
+
if (
|
|
60
|
+
!modifierActions.validateNumberCondition(
|
|
61
|
+
item.quantity,
|
|
62
|
+
each.value,
|
|
63
|
+
each.operand
|
|
64
|
+
)
|
|
65
|
+
) {
|
|
66
|
+
conditionsBag.push({
|
|
67
|
+
name: `${each.operand}.itemQuantity`,
|
|
68
|
+
value: each.value,
|
|
69
|
+
current: item.quantity,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
const requiredModifiersConditions = conditions.rules.filter(
|
|
75
|
+
each => each.key === 'modifiers'
|
|
76
|
+
);
|
|
77
|
+
if (requiredModifiersConditions.length > 0) {
|
|
78
|
+
requiredModifiersConditions.forEach(each => {
|
|
79
|
+
if (
|
|
80
|
+
!modifierActions.validateRequiredModifiers(
|
|
81
|
+
item.modifiers,
|
|
82
|
+
each.value,
|
|
83
|
+
each.operand
|
|
84
|
+
)
|
|
85
|
+
) {
|
|
86
|
+
conditionsBag.push({
|
|
87
|
+
name: `${each.operand}.modifiers`,
|
|
88
|
+
value: each.value,
|
|
89
|
+
current: item.modifiers,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return conditionsBag;
|
|
55
95
|
};
|
|
56
96
|
|
|
57
97
|
const addModifier = ({
|
|
@@ -63,19 +103,21 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
|
|
|
63
103
|
items: [],
|
|
64
104
|
itemModifiers: [],
|
|
65
105
|
},
|
|
106
|
+
onConditionsNotMet,
|
|
66
107
|
}) => {
|
|
67
108
|
const modifier = _modifier; // to avoid no param reassign lint rule
|
|
68
109
|
let item = { ...itemProp };
|
|
69
110
|
const compute = getComputeModField(modifier);
|
|
70
111
|
if (hasBehaivoralFields(modifier)) return item;
|
|
71
|
-
|
|
72
|
-
if (!areConditionsMet(item, modifier.conditions)) return item;
|
|
112
|
+
const conditionsBag = areConditionsMet(item, modifier.conditions);
|
|
73
113
|
const isAmountOverride = modifierActions.isAmountOverride(modifier);
|
|
74
114
|
const isPriceOverride = modifierActions.isPriceOverride(modifier);
|
|
75
|
-
|
|
76
|
-
|
|
115
|
+
// is the modifier suggestion required
|
|
116
|
+
if (conditionsBag.length > 0 && !modifierActions.isRequired(modifier)) {
|
|
117
|
+
if (onConditionsNotMet) onConditionsNotMet(conditionsBag);
|
|
118
|
+
return item;
|
|
119
|
+
}
|
|
77
120
|
const modifierToAdd = modifierActions.mutateModifier(modifier);
|
|
78
|
-
|
|
79
121
|
const modifierIndex = item.modifiers.findIndex(
|
|
80
122
|
ieach => ieach.modifierId === modifier._id
|
|
81
123
|
);
|
|
@@ -122,6 +164,7 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
|
|
|
122
164
|
modifier,
|
|
123
165
|
itemIndex,
|
|
124
166
|
cache,
|
|
167
|
+
onConditionsNotMet,
|
|
125
168
|
}) {
|
|
126
169
|
let order = _.cloneDeep(orderProp);
|
|
127
170
|
if (!order || itemIndex < 0 || !modifier) return order;
|
|
@@ -166,6 +209,7 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
|
|
|
166
209
|
modifier,
|
|
167
210
|
cache,
|
|
168
211
|
customer,
|
|
212
|
+
onConditionsNotMet,
|
|
169
213
|
});
|
|
170
214
|
|
|
171
215
|
// Recursive Rules:
|
|
@@ -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,37 +2,47 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* return calculated Order
|
|
4
4
|
*/
|
|
5
|
-
module.exports = ({ _, actions, itemActions, modifierActions }) =>
|
|
6
|
-
function calculateorder(inputOrder) {
|
|
5
|
+
module.exports = ({ _, actions, itemActions, modifierActions, utils }) =>
|
|
6
|
+
function calculateorder(inputOrder, opts = {}) {
|
|
7
7
|
if (!inputOrder) return inputOrder;
|
|
8
8
|
const order = _.cloneDeep(inputOrder);
|
|
9
9
|
const { items = [], orders = [] } = order;
|
|
10
|
-
|
|
11
10
|
if (!items.length) {
|
|
12
11
|
if (!orders.length)
|
|
13
12
|
return { ...order, subTotals: {}, subTotal: 0, total: 0 };
|
|
14
13
|
|
|
15
14
|
return { ...order, ...actions.getTotals(order.orders) };
|
|
16
15
|
}
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
const startRequestDate = order.start ? order.start.requestDate : null;
|
|
17
|
+
const endRequestDate = order.end ? order.end.requestDate : null;
|
|
18
|
+
const options = {
|
|
19
|
+
...opts,
|
|
20
|
+
startRequestDate,
|
|
21
|
+
endRequestDate,
|
|
22
|
+
};
|
|
23
|
+
const sortedOrderModifiers = modifierActions.sort(order.modifiers || []);
|
|
21
24
|
|
|
22
25
|
let itemsWNIM = items.map(item =>
|
|
23
26
|
itemActions.removeModifiers({ item, modifiers: sortedOrderModifiers })
|
|
24
27
|
);
|
|
25
28
|
|
|
26
|
-
itemsWNIM = itemActions.calculate(itemsWNIM
|
|
29
|
+
itemsWNIM = itemActions.calculate(itemsWNIM, {
|
|
30
|
+
...options,
|
|
31
|
+
lockPaymentModifiers: !!(
|
|
32
|
+
options.lockPaymentModifiers && !sortedOrderModifiers.length
|
|
33
|
+
),
|
|
34
|
+
});
|
|
27
35
|
|
|
28
36
|
const newOrder = {
|
|
29
37
|
...order,
|
|
30
|
-
...itemActions.
|
|
38
|
+
...itemActions.getItemsTotals(itemsWNIM),
|
|
31
39
|
items: itemsWNIM,
|
|
32
40
|
};
|
|
33
41
|
|
|
34
42
|
if (!sortedOrderModifiers.length) return newOrder;
|
|
35
43
|
|
|
44
|
+
itemsWNIM = itemActions.calculate(itemsWNIM, {});
|
|
45
|
+
|
|
36
46
|
let tempItems = itemsWNIM || items;
|
|
37
47
|
let itemsWIM = _.cloneDeep(tempItems);
|
|
38
48
|
let prvSort = null;
|
|
@@ -55,26 +65,19 @@ module.exports = ({ _, actions, itemActions, modifierActions }) =>
|
|
|
55
65
|
} else {
|
|
56
66
|
tempItems = itemsWIM;
|
|
57
67
|
}
|
|
58
|
-
tempItems = itemActions.calculate(tempItems);
|
|
68
|
+
tempItems = itemActions.calculate(tempItems, options);
|
|
59
69
|
}
|
|
60
70
|
|
|
61
71
|
if (isCredit) {
|
|
62
|
-
const calculatedItemWIM = itemActions.calculate(itemsWIM);
|
|
63
|
-
computedTotal = itemActions.
|
|
72
|
+
const calculatedItemWIM = itemActions.calculate(itemsWIM, options);
|
|
73
|
+
computedTotal = itemActions.getItemsTotals(calculatedItemWIM).total;
|
|
64
74
|
}
|
|
65
75
|
|
|
66
76
|
if (modifierActions.hasItems({ modifier })) {
|
|
67
77
|
tempItems = itemActions.getItems(tempItems, modifier.items);
|
|
68
78
|
}
|
|
69
79
|
|
|
70
|
-
|
|
71
|
-
itemActions.removeModifiers({
|
|
72
|
-
item: tempItem,
|
|
73
|
-
modifiers: modifierActions.getLockedModifiers(tempItem.modifiers),
|
|
74
|
-
})
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
computedTotal = itemActions.getTotals(tempItems).total;
|
|
80
|
+
computedTotal = itemActions.getItemsTotals(tempItems).total;
|
|
78
81
|
|
|
79
82
|
const itemsModifiers = itemActions.addIndirectModifier({
|
|
80
83
|
orderTotal: computedTotal,
|
|
@@ -92,11 +95,31 @@ module.exports = ({ _, actions, itemActions, modifierActions }) =>
|
|
|
92
95
|
prvSort = sort;
|
|
93
96
|
}
|
|
94
97
|
|
|
95
|
-
const calculatedItemWIM = itemActions.calculate(itemsWIM);
|
|
98
|
+
const calculatedItemWIM = itemActions.calculate(itemsWIM, options);
|
|
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
|
+
}
|
|
96
119
|
|
|
97
120
|
return {
|
|
98
121
|
...order,
|
|
99
|
-
...itemActions.
|
|
122
|
+
...itemActions.getItemsTotals(calculatedItemWIM),
|
|
100
123
|
items: calculatedItemWIM,
|
|
101
124
|
};
|
|
102
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');
|
|
@@ -72,7 +70,6 @@ const getItemIndex = require('./getItemIndex');
|
|
|
72
70
|
const getRelatedItems = require('./getRelatedItems');
|
|
73
71
|
const getItemByItemId = require('./getItemByItemId');
|
|
74
72
|
const getScheduleByCustomer = require('./getScheduleByCustomer');
|
|
75
|
-
const pickEndDate = require('./pickEndDate');
|
|
76
73
|
const getAppliedCredit = require('./getAppliedCredit');
|
|
77
74
|
const addCreditModifier = require('./addCreditModifier');
|
|
78
75
|
const adjustCreditModifiersDifference = require('./adjustCreditModifiersDifference');
|
|
@@ -83,11 +80,14 @@ const splitAndCalculate = require('./splitAndCalculate');
|
|
|
83
80
|
const spreadModifiers = require('./spreadModifiers');
|
|
84
81
|
const validateCreditModifiersTotal = require('./validateCreditModifiersTotal');
|
|
85
82
|
const validateFixedModifiersTotal = require('./validateFixedModifiersTotal');
|
|
86
|
-
const markModifiersAsLocked = require('./markModifiersAsLocked');
|
|
87
83
|
const removeModifiers = require('./removeModifiers');
|
|
88
84
|
const addModifiers = require('./addModifiers');
|
|
89
85
|
const createSuborder = require('./createSuborder');
|
|
90
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');
|
|
91
91
|
|
|
92
92
|
const orderActions = (deps = {}) => {
|
|
93
93
|
const actions = {};
|
|
@@ -157,9 +157,6 @@ const orderActions = (deps = {}) => {
|
|
|
157
157
|
getCustomerSubscriptions: getCustomerSubscriptions(innerDeps),
|
|
158
158
|
getCustomerSubscriptionsByItem: getCustomerSubscriptionsByItem(innerDeps),
|
|
159
159
|
hasRemainingSubscription: hasRemainingSubscription(innerDeps),
|
|
160
|
-
removeModifiersWithPaymentMethods:
|
|
161
|
-
removeModifiersWithPaymentMethods(innerDeps),
|
|
162
|
-
removeModifiersWithPaymentTypes: removeModifiersWithPaymentTypes(innerDeps),
|
|
163
160
|
hasItem: hasItem(innerDeps),
|
|
164
161
|
getNumberOfItems: getNumberOfItems(innerDeps),
|
|
165
162
|
isOpen: isOpen(innerDeps),
|
|
@@ -172,7 +169,6 @@ const orderActions = (deps = {}) => {
|
|
|
172
169
|
getItemIndex: getItemIndex(innerDeps),
|
|
173
170
|
getRelatedItems: getRelatedItems(innerDeps),
|
|
174
171
|
getScheduleByCustomer: getScheduleByCustomer(innerDeps),
|
|
175
|
-
pickEndDate: pickEndDate(innerDeps),
|
|
176
172
|
getAppliedCredit: getAppliedCredit(innerDeps),
|
|
177
173
|
addCreditModifier: addCreditModifier(innerDeps),
|
|
178
174
|
adjustCreditModifiersDifference: adjustCreditModifiersDifference(innerDeps),
|
|
@@ -183,11 +179,14 @@ const orderActions = (deps = {}) => {
|
|
|
183
179
|
spreadModifiers: spreadModifiers(innerDeps),
|
|
184
180
|
validateCreditModifiersTotal: validateCreditModifiersTotal(innerDeps),
|
|
185
181
|
validateFixedModifiersTotal: validateFixedModifiersTotal(innerDeps),
|
|
186
|
-
markModifiersAsLocked: markModifiersAsLocked(innerDeps),
|
|
187
182
|
removeModifiers: removeModifiers(innerDeps),
|
|
188
183
|
addModifiers: addModifiers(innerDeps),
|
|
189
184
|
createSuborder: createSuborder(innerDeps),
|
|
190
185
|
getSuborder: getSuborder(innerDeps),
|
|
186
|
+
manualSplit: manualSplit(innerDeps),
|
|
187
|
+
manualSplitByQuantity: manualSplitByQuantity(innerDeps),
|
|
188
|
+
applyPayment: applyPayment(innerDeps),
|
|
189
|
+
getOrdersBalance: getOrdersBalance(innerDeps),
|
|
191
190
|
});
|
|
192
191
|
|
|
193
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
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module.exports = ({ actions, settings }) =>
|
|
2
|
+
function getRecommendedEndDate() {
|
|
3
|
+
const defaultSchedule =
|
|
4
|
+
settings.order &&
|
|
5
|
+
settings.order.endDate &&
|
|
6
|
+
settings.order.endDate.default;
|
|
7
|
+
|
|
8
|
+
return defaultSchedule
|
|
9
|
+
? actions.pickEndDate({
|
|
10
|
+
...defaultSchedule,
|
|
11
|
+
})
|
|
12
|
+
: null;
|
|
13
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//
|
|
2
|
+
const pickEndDate = require('./pickEndDate');
|
|
3
|
+
const getRecommendedEndDate = require('./getRecommendedEndDate');
|
|
4
|
+
|
|
5
|
+
const storeActions = (deps = {}) => {
|
|
6
|
+
const actions = {};
|
|
7
|
+
|
|
8
|
+
const innerDeps = {
|
|
9
|
+
...deps,
|
|
10
|
+
actions,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const freezedActions = Object.freeze({
|
|
14
|
+
pickEndDate: pickEndDate(innerDeps),
|
|
15
|
+
getRecommendedEndDate: getRecommendedEndDate(innerDeps),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
Object.keys(freezedActions).forEach(actionName => {
|
|
19
|
+
actions[actionName] = freezedActions[actionName];
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return freezedActions;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
module.exports = storeActions;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/pricing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.45",
|
|
4
4
|
"description": "Pricing calculator",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"license": "ISC",
|
|
@@ -12,8 +12,10 @@
|
|
|
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
|
+
"lint": "eslint --quiet lib/"
|
|
17
19
|
},
|
|
18
20
|
"publishConfig": {
|
|
19
21
|
"access": "public"
|
|
@@ -36,5 +38,5 @@
|
|
|
36
38
|
"supertest": "^6.2.3",
|
|
37
39
|
"supervisor": "^0.12.0"
|
|
38
40
|
},
|
|
39
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "3bfaa3655ef26d0dc0fb60bdc40caee1e3693ccb"
|
|
40
42
|
}
|
|
@@ -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
|
-
};
|
package/lib/item/getTotals.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-restricted-syntax */
|
|
2
|
-
|
|
3
|
-
module.exports = ({ utils }) => {
|
|
4
|
-
const { math } = utils;
|
|
5
|
-
return function getTotals(items) {
|
|
6
|
-
let subTotals = {};
|
|
7
|
-
let subTotal = 0;
|
|
8
|
-
|
|
9
|
-
for (const item of items) {
|
|
10
|
-
subTotal = math.add(subTotal, item.subTotals._actual);
|
|
11
|
-
const keys = Object.keys(item.subTotals).filter(
|
|
12
|
-
key => !key.includes('_')
|
|
13
|
-
);
|
|
14
|
-
for (const key of keys) {
|
|
15
|
-
subTotals[key] = math.add(subTotals[key] || 0, item.subTotals[key]);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const total = math.toDecimalPlaces(
|
|
20
|
-
Object.keys(subTotals).reduce(
|
|
21
|
-
(acc, key) => math.add(acc, subTotals[key]),
|
|
22
|
-
subTotal
|
|
23
|
-
)
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
-
subTotals = Object.keys(subTotals).reduce(
|
|
27
|
-
(acc, key) => ({
|
|
28
|
-
...acc,
|
|
29
|
-
[key]: math.toDecimalPlaces(subTotals[key]),
|
|
30
|
-
}),
|
|
31
|
-
{}
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
return {
|
|
35
|
-
subTotals,
|
|
36
|
-
subTotal,
|
|
37
|
-
total,
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
module.exports = () =>
|
|
2
|
-
function markModifiersAsLocked({ item, modifierIds }) {
|
|
3
|
-
if (!item || !Array.isArray(item.modifiers)) return {};
|
|
4
|
-
|
|
5
|
-
return {
|
|
6
|
-
...item,
|
|
7
|
-
modifiers: item.modifiers.map(mod =>
|
|
8
|
-
modifierIds.includes(mod._parentId) ? { ...mod, locked: true } : mod
|
|
9
|
-
),
|
|
10
|
-
};
|
|
11
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
module.exports = ({ actions }) =>
|
|
2
|
-
function findByPaymentMethod({ modifiers, paymentMethod = '' }) {
|
|
3
|
-
if (!Array.isArray(modifiers) || !paymentMethod) return null;
|
|
4
|
-
|
|
5
|
-
return modifiers.find(
|
|
6
|
-
each =>
|
|
7
|
-
actions.isPaymentMethodModifier(each) &&
|
|
8
|
-
each.conditions.paymentMethods.includes(paymentMethod)
|
|
9
|
-
);
|
|
10
|
-
};
|