@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
package/lib/item/calculate.js
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
|
+
const { getComputeModField } = require('../modifier/utils');
|
|
2
|
+
|
|
1
3
|
/* eslint-disable no-restricted-syntax */
|
|
2
4
|
module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
3
5
|
const { math } = utils;
|
|
4
6
|
//
|
|
5
|
-
const { getComputeModField } = require('../modifier/utils');
|
|
6
7
|
const calculateOne = (inputItem, opts = {}) => {
|
|
7
8
|
const item = _.cloneDeep(inputItem);
|
|
9
|
+
const amountToPay =
|
|
10
|
+
typeof opts.amountToPay === 'number' ? opts.amountToPay : 0;
|
|
8
11
|
const paymentMethod = opts.paymentMethod || null;
|
|
12
|
+
const paymentType = opts.paymentType || null;
|
|
9
13
|
const startRequestDate = opts.startRequestDate || null;
|
|
10
14
|
const endRequestDate = opts.endRequestDate || null;
|
|
15
|
+
const lockPaymentModifiers = !!opts.lockPaymentModifiers;
|
|
16
|
+
const { paymentId } = opts;
|
|
17
|
+
|
|
11
18
|
if (!item) return item;
|
|
12
19
|
|
|
13
20
|
const subTotals = {
|
|
@@ -29,7 +36,6 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
29
36
|
const validatedModifiers = itemModifiers.map(each =>
|
|
30
37
|
modifierActions.validate(each, {
|
|
31
38
|
item,
|
|
32
|
-
paymentMethod,
|
|
33
39
|
startRequestDate,
|
|
34
40
|
endRequestDate,
|
|
35
41
|
})
|
|
@@ -47,20 +53,47 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
47
53
|
modifierActions.isValid(each)
|
|
48
54
|
);
|
|
49
55
|
|
|
56
|
+
const paymentModifiersToCompute = validatedModifiers
|
|
57
|
+
.filter(
|
|
58
|
+
modifier =>
|
|
59
|
+
modifierActions.isPaymentModifier(modifier) &&
|
|
60
|
+
modifierActions.hasPaymentMethodType({
|
|
61
|
+
paymentModifier: modifier,
|
|
62
|
+
paymentMethod,
|
|
63
|
+
paymentType,
|
|
64
|
+
})
|
|
65
|
+
)
|
|
66
|
+
.filter(paymentModifier => {
|
|
67
|
+
const childModifiers = modifierActions.getChildren({
|
|
68
|
+
parentModifier: paymentModifier,
|
|
69
|
+
modifiers: modifiersToCompute,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
!modifierActions.isFixed(paymentModifier) ||
|
|
74
|
+
childModifiers.length === 0
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
50
78
|
if (amountOverride) modifiersToCompute.push(amountOverride);
|
|
51
79
|
|
|
52
80
|
const modifiersToNotCompute = validatedModifiers
|
|
53
81
|
.filter(
|
|
54
82
|
each =>
|
|
83
|
+
modifierActions.isPaymentModifier(each) ||
|
|
55
84
|
!modifiersToCompute.find(
|
|
56
85
|
ceach => ceach.modifierId === each.modifierId
|
|
57
86
|
)
|
|
58
87
|
)
|
|
59
88
|
.map(each => modifierActions.calculate(each, { skip: true }));
|
|
60
89
|
|
|
61
|
-
if (modifiersToCompute.length) {
|
|
90
|
+
if (modifiersToCompute.length || paymentModifiersToCompute.length) {
|
|
62
91
|
// sort modifiers based on sort
|
|
63
|
-
const sortedModifiers = modifierActions.sort(
|
|
92
|
+
const sortedModifiers = modifierActions.sort([
|
|
93
|
+
...modifiersToCompute,
|
|
94
|
+
...paymentModifiersToCompute,
|
|
95
|
+
]);
|
|
96
|
+
|
|
64
97
|
let computedPrice = price;
|
|
65
98
|
let prvPrice = 0;
|
|
66
99
|
let prvSort;
|
|
@@ -76,12 +109,37 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
76
109
|
prvSort = null;
|
|
77
110
|
}
|
|
78
111
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
112
|
+
let _modifier = modifier;
|
|
113
|
+
|
|
114
|
+
if (modifierActions.isPaymentModifier(modifier)) {
|
|
115
|
+
_modifier = modifierActions.calculatePaymentModifier({
|
|
116
|
+
paymentModifier: modifier,
|
|
117
|
+
amountToPay,
|
|
118
|
+
itemBalance:
|
|
119
|
+
typeof item.totalPaid === 'number' && item.totalPaid > 0
|
|
120
|
+
? actions.getItemsBalance({ items: [item] })
|
|
121
|
+
: math.sub(computedPrice, item.totalPaid),
|
|
122
|
+
paymentMethod,
|
|
123
|
+
paymentType,
|
|
124
|
+
paymentId,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
_modifier = modifierActions.calculate(
|
|
129
|
+
{ ..._modifier },
|
|
130
|
+
{
|
|
131
|
+
price: computedPrice,
|
|
132
|
+
quantity,
|
|
133
|
+
}
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
if (
|
|
137
|
+
!modifierActions.isPaymentModifier(modifier) ||
|
|
138
|
+
lockPaymentModifiers
|
|
139
|
+
) {
|
|
140
|
+
modifiers.push(_modifier);
|
|
141
|
+
}
|
|
83
142
|
|
|
84
|
-
modifiers.push(_modifier);
|
|
85
143
|
const { type, _computed } = _modifier;
|
|
86
144
|
|
|
87
145
|
const computedAmountCalc = math.mul(_computed.amount, quantity);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module.exports = ({ utils }) =>
|
|
2
|
+
function getAmounts(item, amount) {
|
|
3
|
+
const { status = {}, total, totalPaid } = item;
|
|
4
|
+
const totalDue = utils.math.sub(total, totalPaid);
|
|
5
|
+
const remainingAmount = utils.math.sub(amount, totalDue);
|
|
6
|
+
|
|
7
|
+
if (status && status.paid && status.paid.value)
|
|
8
|
+
return { amountToAssign: 0, remainingAmount: amount, isPaid: true };
|
|
9
|
+
|
|
10
|
+
if (amount >= totalDue)
|
|
11
|
+
return { amountToAssign: totalDue, remainingAmount, isPaid: true };
|
|
12
|
+
|
|
13
|
+
return { amountToAssign: amount, remainingAmount: 0, isPaid: false };
|
|
14
|
+
};
|
package/lib/item/getBalance.js
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module.exports = ({ modifierActions }) =>
|
|
2
|
+
function getInvalidRequiredModifiers({ item }) {
|
|
3
|
+
if (!item || !Array.isArray(item.modifiers)) return true;
|
|
4
|
+
const invalidModifiers = [];
|
|
5
|
+
|
|
6
|
+
item.modifiers.forEach(modifier => {
|
|
7
|
+
if (
|
|
8
|
+
modifierActions.isRequired(modifier) &&
|
|
9
|
+
modifierActions.isGroupOfModifiers(modifier) &&
|
|
10
|
+
!item.modifiers
|
|
11
|
+
.filter(mod => mod._id !== modifier._id)
|
|
12
|
+
.some(itemModifier =>
|
|
13
|
+
modifierActions.isRelatedModifier({
|
|
14
|
+
_id: itemModifier.modifierId || itemModifier._id,
|
|
15
|
+
modifier,
|
|
16
|
+
})
|
|
17
|
+
)
|
|
18
|
+
) {
|
|
19
|
+
invalidModifiers.push(modifier);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
return invalidModifiers;
|
|
24
|
+
};
|
|
@@ -10,7 +10,8 @@ module.exports = ({ modifierActions, _, settings }) =>
|
|
|
10
10
|
modifierActions.isValid(each) &&
|
|
11
11
|
!modifierActions.isAmountOverride(each) &&
|
|
12
12
|
!modifierActions.isDepartment(each) &&
|
|
13
|
-
modifierActions.isDirect(each)
|
|
13
|
+
modifierActions.isDirect(each) &&
|
|
14
|
+
!modifierActions.isGroupOfModifiers(each)
|
|
14
15
|
)
|
|
15
16
|
.map(modifier => {
|
|
16
17
|
const { name, _id, compute = {} } = modifier;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/* eslint-disable no-restricted-syntax */
|
|
2
|
+
|
|
3
|
+
module.exports = ({ utils }) => {
|
|
4
|
+
const { math } = utils;
|
|
5
|
+
return function getItemsTotals(items) {
|
|
6
|
+
const subTotals = {};
|
|
7
|
+
let subTotal = 0;
|
|
8
|
+
let total = 0;
|
|
9
|
+
|
|
10
|
+
for (const item of items) {
|
|
11
|
+
subTotal = math.add(subTotal, item.subTotals._actual);
|
|
12
|
+
const keys = Object.keys(item.subTotals).filter(
|
|
13
|
+
key => !key.includes('_')
|
|
14
|
+
);
|
|
15
|
+
for (const key of keys) {
|
|
16
|
+
subTotals[key] = math.add(subTotals[key], item.subTotals[key]);
|
|
17
|
+
}
|
|
18
|
+
total = math.add(total, item.total);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
subTotals,
|
|
23
|
+
subTotal,
|
|
24
|
+
total,
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
module.exports = ({ modifierActions }) =>
|
|
2
|
+
function getPipeModifiers({ item }) {
|
|
3
|
+
if (!item || !Array.isArray(item.modifiers)) return [];
|
|
4
|
+
return item.modifiers.filter(
|
|
5
|
+
modifier =>
|
|
6
|
+
modifierActions.isGroupOfModifiers(modifier) &&
|
|
7
|
+
modifierActions.isRequired(modifier) &&
|
|
8
|
+
modifierActions.enableAutoPopup(modifier)
|
|
9
|
+
);
|
|
10
|
+
};
|
package/lib/item/getTotal.js
CHANGED
|
@@ -2,6 +2,8 @@ module.exports = ({ utils }) => {
|
|
|
2
2
|
const { math } = utils;
|
|
3
3
|
return function getTotal(item) {
|
|
4
4
|
if (!item || !item.subTotals) return 0;
|
|
5
|
-
return math.
|
|
5
|
+
return math.toDecimalPlaces(
|
|
6
|
+
math.add(item.subTotals._actual, item.subTotals._xincluded)
|
|
7
|
+
);
|
|
6
8
|
};
|
|
7
9
|
};
|
package/lib/item/hasModifier.js
CHANGED
|
@@ -1,15 +1,27 @@
|
|
|
1
|
-
module.exports = () =>
|
|
1
|
+
module.exports = ({ modifierActions }) =>
|
|
2
2
|
function hasModifier({ item, modifier }) {
|
|
3
3
|
if (!item || !modifier || !item.modifiers) return false;
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
if (modifierActions.isGroupOfModifiers(modifier)) {
|
|
6
|
+
const relatedModifiers = modifier.properties.group.modifiers;
|
|
7
|
+
return [
|
|
8
|
+
modifierActions.removeGroupData({ modifier }),
|
|
9
|
+
...relatedModifiers,
|
|
10
|
+
].some(relatedModifier =>
|
|
11
|
+
hasModifier({ item, modifier: relatedModifier })
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const itemModifier = item.modifiers
|
|
16
|
+
.filter(itemMod => !modifierActions.isGroupOfModifiers(itemMod))
|
|
17
|
+
.find(
|
|
18
|
+
each =>
|
|
19
|
+
(each.modifierId &&
|
|
20
|
+
(each.modifierId === modifier.modifierId ||
|
|
21
|
+
each.modifierId === modifier._id)) ||
|
|
22
|
+
(typeof each.modifier === 'object' &&
|
|
23
|
+
each.modifier._id === modifier.modifierId)
|
|
24
|
+
);
|
|
13
25
|
|
|
14
26
|
return !!itemModifier;
|
|
15
27
|
};
|
package/lib/item/hasModifiers.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
module.exports = () =>
|
|
1
|
+
module.exports = ({ actions }) =>
|
|
2
2
|
function hasModifiers({ item, modifiers }) {
|
|
3
3
|
if (!item || !modifiers || !item.modifiers) return false;
|
|
4
4
|
|
|
5
|
-
return
|
|
6
|
-
|
|
7
|
-
!!modifiers.find(each => each._id === itemModifier.modifierId)
|
|
5
|
+
return modifiers.some(each =>
|
|
6
|
+
actions.hasModifier({ item, modifier: each })
|
|
8
7
|
);
|
|
9
8
|
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module.exports = ({ modifierActions }) =>
|
|
2
|
+
function hasPaymentMethodType({ item, paymentMethod, paymentType }) {
|
|
3
|
+
if (
|
|
4
|
+
!item ||
|
|
5
|
+
!Array.isArray(item.modifiers) ||
|
|
6
|
+
(!paymentMethod && !paymentType)
|
|
7
|
+
)
|
|
8
|
+
return false;
|
|
9
|
+
|
|
10
|
+
return item.modifiers.some(modifier =>
|
|
11
|
+
modifierActions.hasPaymentMethodType({
|
|
12
|
+
paymentModifier: modifier,
|
|
13
|
+
paymentMethod,
|
|
14
|
+
paymentType,
|
|
15
|
+
})
|
|
16
|
+
);
|
|
17
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
module.exports = ({ modifierActions }) =>
|
|
2
|
+
function hasPaymentModifierWithPaymentId({ item, paymentId }) {
|
|
3
|
+
if (!item || !Array.isArray(item.modifiers) || !paymentId) return false;
|
|
4
|
+
|
|
5
|
+
return item.modifiers.some(
|
|
6
|
+
modifier =>
|
|
7
|
+
modifierActions.getProperty(modifier, 'paymentId') === paymentId
|
|
8
|
+
);
|
|
9
|
+
};
|
package/lib/item/index.js
CHANGED
|
@@ -9,7 +9,6 @@ const removeModifiers = require('./removeModifiers');
|
|
|
9
9
|
const findOriginalItem = require('./findOriginalItem');
|
|
10
10
|
const create = require('./create');
|
|
11
11
|
const hasModifier = require('./hasModifier');
|
|
12
|
-
const isSelected = require('./isSelected');
|
|
13
12
|
const hasModifiers = require('./hasModifiers');
|
|
14
13
|
const addIndirectModifier = require('./addIndirectModifier');
|
|
15
14
|
const calculate = require('./calculate');
|
|
@@ -20,10 +19,9 @@ const removeModifierById = require('./removeModifierById');
|
|
|
20
19
|
const getDiscountModifiers = require('./getDiscountModifiers');
|
|
21
20
|
const getTotal = require('./getTotal');
|
|
22
21
|
const getPendingIndex = require('./getPendingIndex');
|
|
23
|
-
const
|
|
22
|
+
const getItemsTotals = require('./getItemsTotals');
|
|
24
23
|
const getNotIncludedModifiers = require('./getNotIncludedModifiers');
|
|
25
24
|
const hasModifierWithValue = require('./hasModifierWithValue');
|
|
26
|
-
const getBalanceToPay = require('./getBalanceToPay');
|
|
27
25
|
const getItemsTotalPaid = require('./getItemsTotalPaid');
|
|
28
26
|
const getItemModifiersDescription = require('./getItemModifiersDescription');
|
|
29
27
|
const isRelatedItem = require('./isRelatedItem');
|
|
@@ -31,12 +29,18 @@ const getParentItem = require('./getParentItem');
|
|
|
31
29
|
const isParentIncluded = require('./isParentIncluded');
|
|
32
30
|
const hasCreateSubscription = require('./hasCreateSubscription');
|
|
33
31
|
const getItemModifiers = require('./getItemModifiers');
|
|
34
|
-
const
|
|
35
|
-
const getBalance = require('./getBalance');
|
|
32
|
+
const getItemsBalance = require('./getItemsBalance');
|
|
36
33
|
const isFullyPaid = require('./isFullyPaid');
|
|
37
34
|
const getTotalPrice = require('./getTotalPrice');
|
|
38
35
|
const removePaymentModifiers = require('./removePaymentModifiers');
|
|
39
36
|
const getItems = require('./getItems');
|
|
37
|
+
const getAmounts = require('./getAmounts');
|
|
38
|
+
const hasPaymentMethodType = require('./hasPaymentMethodType');
|
|
39
|
+
const removePaymentModifiersByPaymentId = require('./removePaymentModifiersByPaymentId');
|
|
40
|
+
const hasPaymentModifierWithPaymentId = require('./hasPaymentModifierWithPaymentId');
|
|
41
|
+
const getBalance = require('./getBalance');
|
|
42
|
+
const getPipeModifiers = require('./getPipeModifiers');
|
|
43
|
+
const getInvalidRequiredModifiers = require('./getInvalidRequiredModifiers');
|
|
40
44
|
|
|
41
45
|
const itemActions = (deps = {}) => {
|
|
42
46
|
const actions = {};
|
|
@@ -58,7 +62,6 @@ const itemActions = (deps = {}) => {
|
|
|
58
62
|
findOriginalItem: findOriginalItem(innerDeps),
|
|
59
63
|
create: create(innerDeps),
|
|
60
64
|
hasModifier: hasModifier(innerDeps),
|
|
61
|
-
isSelected: isSelected(innerDeps),
|
|
62
65
|
hasModifiers: hasModifiers(innerDeps),
|
|
63
66
|
addIndirectModifier: addIndirectModifier(innerDeps),
|
|
64
67
|
calculate: calculate(innerDeps),
|
|
@@ -69,10 +72,9 @@ const itemActions = (deps = {}) => {
|
|
|
69
72
|
getDiscountModifiers: getDiscountModifiers(innerDeps),
|
|
70
73
|
getTotal: getTotal(innerDeps),
|
|
71
74
|
getPendingIndex: getPendingIndex(innerDeps),
|
|
72
|
-
|
|
75
|
+
getItemsTotals: getItemsTotals(innerDeps),
|
|
73
76
|
getNotIncludedModifiers: getNotIncludedModifiers(innerDeps),
|
|
74
77
|
hasModifierWithValue: hasModifierWithValue(innerDeps),
|
|
75
|
-
getBalanceToPay: getBalanceToPay(innerDeps),
|
|
76
78
|
getItemsTotalPaid: getItemsTotalPaid(innerDeps),
|
|
77
79
|
getItemModifiersDescription: getItemModifiersDescription(innerDeps),
|
|
78
80
|
isRelatedItem: isRelatedItem(innerDeps),
|
|
@@ -80,12 +82,19 @@ const itemActions = (deps = {}) => {
|
|
|
80
82
|
isParentIncluded: isParentIncluded(innerDeps),
|
|
81
83
|
hasCreateSubscription: hasCreateSubscription(innerDeps),
|
|
82
84
|
getItemModifiers: getItemModifiers(innerDeps),
|
|
83
|
-
|
|
84
|
-
getBalance: getBalance(innerDeps),
|
|
85
|
+
getItemsBalance: getItemsBalance(innerDeps),
|
|
85
86
|
isFullyPaid: isFullyPaid(innerDeps),
|
|
86
87
|
getTotalPrice: getTotalPrice(innerDeps),
|
|
87
88
|
removePaymentModifiers: removePaymentModifiers(innerDeps),
|
|
88
89
|
getItems: getItems(innerDeps),
|
|
90
|
+
getAmounts: getAmounts(innerDeps),
|
|
91
|
+
hasPaymentMethodType: hasPaymentMethodType(innerDeps),
|
|
92
|
+
removePaymentModifiersByPaymentId:
|
|
93
|
+
removePaymentModifiersByPaymentId(innerDeps),
|
|
94
|
+
hasPaymentModifierWithPaymentId: hasPaymentModifierWithPaymentId(innerDeps),
|
|
95
|
+
getBalance: getBalance(innerDeps),
|
|
96
|
+
getPipeModifiers: getPipeModifiers(innerDeps),
|
|
97
|
+
getInvalidRequiredModifiers: getInvalidRequiredModifiers(innerDeps),
|
|
89
98
|
});
|
|
90
99
|
|
|
91
100
|
Object.keys(freezedActions).forEach(actionName => {
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module.exports = ({ modifierActions }) =>
|
|
2
|
+
function removePaymentModifiersByPaymentId({ item, paymentId }) {
|
|
3
|
+
if (!item || !Array.isArray(item.modifiers) || !paymentId) return item;
|
|
4
|
+
|
|
5
|
+
return {
|
|
6
|
+
...item,
|
|
7
|
+
modifiers: item.modifiers.filter(
|
|
8
|
+
modifier =>
|
|
9
|
+
!modifierActions.getProperty(modifier, 'paymentId') ||
|
|
10
|
+
modifierActions.getProperty(modifier, 'paymentId') !== paymentId
|
|
11
|
+
),
|
|
12
|
+
};
|
|
13
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
module.exports = ({ actions }) => {
|
|
2
2
|
const modifierConditionPass = (
|
|
3
3
|
modifier,
|
|
4
|
-
{ item,
|
|
4
|
+
{ item, startRequestDate, endRequestDate }
|
|
5
5
|
) =>
|
|
6
6
|
modifier.conditions && Array.isArray(modifier.conditions.rules)
|
|
7
7
|
? modifier.conditions.rules.every(condition => {
|
|
@@ -32,17 +32,9 @@ module.exports = ({ actions }) => {
|
|
|
32
32
|
condition.operand
|
|
33
33
|
);
|
|
34
34
|
case 'paymentMethods':
|
|
35
|
-
return
|
|
36
|
-
paymentMethod ? paymentMethod.key : null,
|
|
37
|
-
condition.value,
|
|
38
|
-
condition.operand
|
|
39
|
-
);
|
|
35
|
+
return false;
|
|
40
36
|
case 'paymentTypes':
|
|
41
|
-
return
|
|
42
|
-
paymentMethod ? paymentMethod.type : null,
|
|
43
|
-
condition.value,
|
|
44
|
-
condition.operand
|
|
45
|
-
);
|
|
37
|
+
return false;
|
|
46
38
|
default:
|
|
47
39
|
return false;
|
|
48
40
|
}
|
|
@@ -52,7 +44,6 @@ module.exports = ({ actions }) => {
|
|
|
52
44
|
function areConditionsMet(modifier, opts) {
|
|
53
45
|
const {
|
|
54
46
|
item = null,
|
|
55
|
-
paymentMethod = null,
|
|
56
47
|
endRequestDate = null,
|
|
57
48
|
startRequestDate = null,
|
|
58
49
|
} = opts;
|
|
@@ -61,7 +52,6 @@ module.exports = ({ actions }) => {
|
|
|
61
52
|
modifier &&
|
|
62
53
|
modifierConditionPass(modifier, {
|
|
63
54
|
item,
|
|
64
|
-
paymentMethod,
|
|
65
55
|
startRequestDate,
|
|
66
56
|
endRequestDate,
|
|
67
57
|
})
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module.exports = ({ utils, actions, constants }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
|
|
4
|
+
return function calculatePaymentDiscountModifier({
|
|
5
|
+
paymentModifier,
|
|
6
|
+
amountToPay,
|
|
7
|
+
}) {
|
|
8
|
+
let amount = 0;
|
|
9
|
+
|
|
10
|
+
if (actions.isFixed(paymentModifier)) {
|
|
11
|
+
amount = paymentModifier.compute.amount;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (actions.isPercentage(paymentModifier)) {
|
|
15
|
+
const discountAmount = math.div(paymentModifier.compute.amount, 100);
|
|
16
|
+
|
|
17
|
+
amount = math.sub(
|
|
18
|
+
math.div(amountToPay, math.sub(1, discountAmount)),
|
|
19
|
+
amountToPay
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return actions.createDiscountModifier({
|
|
24
|
+
amount,
|
|
25
|
+
type: constants.Modifier.Compute.Types.FIXED,
|
|
26
|
+
modifierId: paymentModifier.modifierId,
|
|
27
|
+
_id: paymentModifier._id,
|
|
28
|
+
properties: paymentModifier.properties,
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module.exports = ({ utils, actions, constants }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
|
|
4
|
+
return function calculatePaymentDiscountModifier({
|
|
5
|
+
paymentModifier,
|
|
6
|
+
amountToPay,
|
|
7
|
+
}) {
|
|
8
|
+
let amount = 0;
|
|
9
|
+
|
|
10
|
+
if (actions.isFixed(paymentModifier)) {
|
|
11
|
+
amount = paymentModifier.compute.amount;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (actions.isPercentage(paymentModifier)) {
|
|
15
|
+
const feeAmount = math.div(paymentModifier.compute.amount, 100);
|
|
16
|
+
amount = math.mul(amountToPay, feeAmount);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return actions.createFeeModifier({
|
|
20
|
+
amount,
|
|
21
|
+
type: constants.Modifier.Compute.Types.FIXED,
|
|
22
|
+
modifierId: paymentModifier.modifierId,
|
|
23
|
+
_id: paymentModifier._id,
|
|
24
|
+
properties: paymentModifier.properties,
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
module.exports = ({ actions, utils }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
|
|
4
|
+
return function calculatePaymentFeeModifier({
|
|
5
|
+
paymentModifier: paymentModifierParam,
|
|
6
|
+
amountToPay: amountToPayParam,
|
|
7
|
+
itemBalance: itemBalanceParam,
|
|
8
|
+
paymentMethod,
|
|
9
|
+
paymentType,
|
|
10
|
+
paymentId,
|
|
11
|
+
}) {
|
|
12
|
+
if (
|
|
13
|
+
!actions.hasPaymentMethodType({
|
|
14
|
+
paymentModifier: paymentModifierParam,
|
|
15
|
+
paymentMethod,
|
|
16
|
+
paymentType,
|
|
17
|
+
})
|
|
18
|
+
)
|
|
19
|
+
return undefined;
|
|
20
|
+
|
|
21
|
+
const paymentModifier = {
|
|
22
|
+
...paymentModifierParam,
|
|
23
|
+
properties: {
|
|
24
|
+
...(paymentModifierParam.properties || {}),
|
|
25
|
+
isCalculatedPaymentModifier: true,
|
|
26
|
+
paymentId,
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const itemBalance =
|
|
31
|
+
typeof itemBalanceParam === 'number' ? itemBalanceParam : 0;
|
|
32
|
+
|
|
33
|
+
const calculatedPaymentMod = actions.calculate(paymentModifier, {
|
|
34
|
+
price: itemBalance,
|
|
35
|
+
quantity: 1,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const balanceIncludingPaymentModifier = math.add(
|
|
39
|
+
itemBalance,
|
|
40
|
+
calculatedPaymentMod._computed.amount
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
let amountToPay =
|
|
44
|
+
typeof amountToPayParam === 'number' ? amountToPayParam : 0;
|
|
45
|
+
|
|
46
|
+
amountToPay =
|
|
47
|
+
amountToPay && amountToPay < balanceIncludingPaymentModifier
|
|
48
|
+
? amountToPay
|
|
49
|
+
: balanceIncludingPaymentModifier;
|
|
50
|
+
|
|
51
|
+
if (actions.isDiscount(paymentModifier)) {
|
|
52
|
+
return actions.calculatePaymentDiscountModifier({
|
|
53
|
+
amountToPay,
|
|
54
|
+
paymentModifier,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (actions.isAdd(paymentModifier)) {
|
|
59
|
+
return actions.calculatePaymentFeeModifier({
|
|
60
|
+
amountToPay,
|
|
61
|
+
paymentModifier,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return undefined;
|
|
66
|
+
};
|
|
67
|
+
};
|
|
@@ -2,7 +2,7 @@ module.exports = ({ constants, actions, localization }) => {
|
|
|
2
2
|
const { Modifier } = constants;
|
|
3
3
|
|
|
4
4
|
return function createDiscountModifier(params) {
|
|
5
|
-
const { amount, type, properties } = params;
|
|
5
|
+
const { amount, type, properties, ...rest } = params;
|
|
6
6
|
const { formatAmount } = localization;
|
|
7
7
|
|
|
8
8
|
const name =
|
|
@@ -21,6 +21,7 @@ module.exports = ({ constants, actions, localization }) => {
|
|
|
21
21
|
properties: {
|
|
22
22
|
...properties,
|
|
23
23
|
},
|
|
24
|
+
...rest,
|
|
24
25
|
});
|
|
25
26
|
};
|
|
26
27
|
};
|
|
@@ -2,7 +2,7 @@ module.exports = ({ constants, actions, localization }) => {
|
|
|
2
2
|
const { Modifier } = constants;
|
|
3
3
|
|
|
4
4
|
return function createFeeModifier(params) {
|
|
5
|
-
const { amount, type, properties } = params;
|
|
5
|
+
const { amount, type, properties, ...rest } = params;
|
|
6
6
|
const { formatAmount } = localization;
|
|
7
7
|
|
|
8
8
|
const name =
|
|
@@ -22,6 +22,7 @@ module.exports = ({ constants, actions, localization }) => {
|
|
|
22
22
|
ignoreQuantity: true,
|
|
23
23
|
...properties,
|
|
24
24
|
},
|
|
25
|
+
...rest,
|
|
25
26
|
});
|
|
26
27
|
};
|
|
27
28
|
};
|
|
@@ -19,7 +19,11 @@ module.exports = ({ actions }) => {
|
|
|
19
19
|
return arr;
|
|
20
20
|
}, []);
|
|
21
21
|
|
|
22
|
-
if (actions.includesInGroup(modifier))
|
|
22
|
+
if (actions.includesInGroup(modifier))
|
|
23
|
+
groupModifiers.unshift({
|
|
24
|
+
...actions.removeGroupData({ modifier }),
|
|
25
|
+
required: false,
|
|
26
|
+
});
|
|
23
27
|
|
|
24
28
|
return groupModifiers;
|
|
25
29
|
};
|