@darkpos/pricing 1.0.44 → 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 -85
- package/__TEST__/mocks/scripts/calculate-partially-paid/index.js +2 -2
- package/__TEST__/mocks/scripts/calculate-unpaid/index.js +2 -2
- 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/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 +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/getChildren.js +8 -0
- package/lib/modifier/getComputedAmount.js +9 -0
- package/lib/modifier/hasPaymentMethodType.js +14 -0
- package/lib/modifier/index.js +19 -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/sort.js +28 -0
- 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 +6 -4
- package/lib/item/getBalanceToPay.js +0 -12
- package/lib/item/getTotals.js +0 -40
- 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,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
|
+
};
|
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
|
};
|
|
@@ -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
|
@@ -20,10 +20,9 @@ const removeModifierById = require('./removeModifierById');
|
|
|
20
20
|
const getDiscountModifiers = require('./getDiscountModifiers');
|
|
21
21
|
const getTotal = require('./getTotal');
|
|
22
22
|
const getPendingIndex = require('./getPendingIndex');
|
|
23
|
-
const
|
|
23
|
+
const getItemsTotals = require('./getItemsTotals');
|
|
24
24
|
const getNotIncludedModifiers = require('./getNotIncludedModifiers');
|
|
25
25
|
const hasModifierWithValue = require('./hasModifierWithValue');
|
|
26
|
-
const getBalanceToPay = require('./getBalanceToPay');
|
|
27
26
|
const getItemsTotalPaid = require('./getItemsTotalPaid');
|
|
28
27
|
const getItemModifiersDescription = require('./getItemModifiersDescription');
|
|
29
28
|
const isRelatedItem = require('./isRelatedItem');
|
|
@@ -31,12 +30,16 @@ const getParentItem = require('./getParentItem');
|
|
|
31
30
|
const isParentIncluded = require('./isParentIncluded');
|
|
32
31
|
const hasCreateSubscription = require('./hasCreateSubscription');
|
|
33
32
|
const getItemModifiers = require('./getItemModifiers');
|
|
34
|
-
const
|
|
35
|
-
const getBalance = require('./getBalance');
|
|
33
|
+
const getItemsBalance = require('./getItemsBalance');
|
|
36
34
|
const isFullyPaid = require('./isFullyPaid');
|
|
37
35
|
const getTotalPrice = require('./getTotalPrice');
|
|
38
36
|
const removePaymentModifiers = require('./removePaymentModifiers');
|
|
39
37
|
const getItems = require('./getItems');
|
|
38
|
+
const getAmounts = require('./getAmounts');
|
|
39
|
+
const hasPaymentMethodType = require('./hasPaymentMethodType');
|
|
40
|
+
const removePaymentModifiersByPaymentId = require('./removePaymentModifiersByPaymentId');
|
|
41
|
+
const hasPaymentModifierWithPaymentId = require('./hasPaymentModifierWithPaymentId');
|
|
42
|
+
const getBalance = require('./getBalance');
|
|
40
43
|
|
|
41
44
|
const itemActions = (deps = {}) => {
|
|
42
45
|
const actions = {};
|
|
@@ -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,17 @@ 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),
|
|
89
96
|
});
|
|
90
97
|
|
|
91
98
|
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
|
};
|
|
@@ -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,6 @@ const mutateModifier = require('./mutateModifier');
|
|
|
127
126
|
const isFixedDiscount = require('./isFixedDiscount');
|
|
128
127
|
const removeLocked = require('./removeLocked');
|
|
129
128
|
const hasItems = require('./hasItems');
|
|
130
|
-
const getLockedModifiers = require('./getLockedModifiers');
|
|
131
129
|
const getGroupedModifiers = require('./getGroupedModifiers');
|
|
132
130
|
const getNotesToModifierTags = require('./getNotesToModifierTags');
|
|
133
131
|
const isOptionsOverride = require('./isOptionsOverride');
|
|
@@ -137,6 +135,15 @@ const validateNumberCondition = require('./validateNumberCondition');
|
|
|
137
135
|
const validateRequiredModifiers = require('./validateRequiredModifiers');
|
|
138
136
|
const validateDateDaysDiff = require('./validateDateDaysDiff');
|
|
139
137
|
const validateInArr = require('./validateInArr');
|
|
138
|
+
const isPercentage = require('./isPercentage');
|
|
139
|
+
const getChildren = require('./getChildren');
|
|
140
|
+
const getComputedAmount = require('./getComputedAmount');
|
|
141
|
+
const hasPaymentMethodType = require('./hasPaymentMethodType');
|
|
142
|
+
const calculatePaymentModifier = require('./calculatePaymentModifier');
|
|
143
|
+
const calculatePaymentDiscountModifier = require('./calculatePaymentDiscountModifier');
|
|
144
|
+
const calculatePaymentFeeModifier = require('./calculatePaymentFeeModifier');
|
|
145
|
+
const isCalculatedPaymentModifier = require('./isCalculatedPaymentModifier');
|
|
146
|
+
const isChild = require('./isChild');
|
|
140
147
|
|
|
141
148
|
const modifierActions = (deps = {}) => {
|
|
142
149
|
const actions = {};
|
|
@@ -265,13 +272,11 @@ const modifierActions = (deps = {}) => {
|
|
|
265
272
|
purifyModifiers: purifyModifiers(innerDeps),
|
|
266
273
|
sort: sort(innerDeps),
|
|
267
274
|
isPaymentModifier: isPaymentModifier(innerDeps),
|
|
268
|
-
createPaymentModifier: createPaymentModifier(innerDeps),
|
|
269
275
|
isSubtract: isSubtract(innerDeps),
|
|
270
276
|
isFixed: isFixed(innerDeps),
|
|
271
277
|
isFixedDiscount: isFixedDiscount(innerDeps),
|
|
272
278
|
removeLocked: removeLocked(innerDeps),
|
|
273
279
|
hasItems: hasItems(innerDeps),
|
|
274
|
-
getLockedModifiers: getLockedModifiers(innerDeps),
|
|
275
280
|
isFixedAdd: isFixedAdd(innerDeps),
|
|
276
281
|
isFee: isFee(innerDeps),
|
|
277
282
|
isAdd: isAdd(innerDeps),
|
|
@@ -286,6 +291,16 @@ const modifierActions = (deps = {}) => {
|
|
|
286
291
|
validateDateDaysDiff: validateDateDaysDiff(innerDeps),
|
|
287
292
|
validateInArr: validateInArr(innerDeps),
|
|
288
293
|
areConditionsMet: areConditionsMet(innerDeps),
|
|
294
|
+
isPercentage: isPercentage(innerDeps),
|
|
295
|
+
getChildren: getChildren(innerDeps),
|
|
296
|
+
getComputedAmount: getComputedAmount(innerDeps),
|
|
297
|
+
hasPaymentMethodType: hasPaymentMethodType(innerDeps),
|
|
298
|
+
calculatePaymentModifier: calculatePaymentModifier(innerDeps),
|
|
299
|
+
calculatePaymentDiscountModifier:
|
|
300
|
+
calculatePaymentDiscountModifier(innerDeps),
|
|
301
|
+
calculatePaymentFeeModifier: calculatePaymentFeeModifier(innerDeps),
|
|
302
|
+
isCalculatedPaymentModifier: isCalculatedPaymentModifier(innerDeps),
|
|
303
|
+
isChild: isChild(innerDeps),
|
|
289
304
|
});
|
|
290
305
|
|
|
291
306
|
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
|
};
|