@darkpos/pricing 1.0.53 → 1.0.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__TEST__/item/addIndirectModifier.test.js +3 -3
- package/__TEST__/item.test.js +28 -1
- package/__TEST__/mocks/addItemMock.js +2 -2
- package/__TEST__/mocks/order-credit.json +1 -1
- package/__TEST__/mocks/partially-paid/order-partially-paid.json +2 -2
- package/__TEST__/mocks/unpaid/order-not-paid.json +1 -1
- package/__TEST__/modifier/createIndirectModifier.test.js +26 -91
- package/__TEST__/modifier/hasModifier.test.js +2 -2
- package/__TEST__/modifier.test.js +112 -0
- package/__TEST__/order/order-payment-modifier.test.js +2 -2
- package/__TEST__/order/order.test.js +452 -7
- package/lib/item/addIndirectModifier.js +1 -1
- package/lib/item/calculate.js +11 -28
- package/lib/item/index.js +2 -0
- package/lib/item/removeModifiersByQuantity.js +30 -0
- package/lib/modifier/calculate.js +27 -6
- package/lib/modifier/createFeeModifier.js +1 -1
- package/lib/modifier/createIndirectModifier.js +6 -7
- package/lib/modifier/index.js +6 -2
- package/lib/modifier/isCompute.js +10 -0
- package/lib/modifier/isFixedSubtract.js +4 -0
- package/lib/modifier/isQuantityMultiplier.js +5 -0
- package/lib/order/addItemModifier.js +11 -0
- package/package.json +4 -2
- package/__TEST__/mocks/scripts/calculate-partially-paid/index.js +0 -27
- package/__TEST__/mocks/scripts/calculate-unpaid/index.js +0 -28
- package/__TEST__/mocks/scripts/order-to-string.js +0 -18
- package/lib/modifier/isIgnoreQuantity.js +0 -5
package/lib/item/index.js
CHANGED
|
@@ -46,6 +46,7 @@ const getTotalsDifference = require('./getTotalsDifference');
|
|
|
46
46
|
const getTotalNeareastDifference = require('./getTotalNeareastDifference');
|
|
47
47
|
const getNoteTags = require('./getNoteTags');
|
|
48
48
|
const getBasePrice = require('./getBasePrice');
|
|
49
|
+
const removeModifiersByQuantity = require('./removeModifiersByQuantity');
|
|
49
50
|
|
|
50
51
|
const itemActions = (deps = {}) => {
|
|
51
52
|
const actions = {};
|
|
@@ -105,6 +106,7 @@ const itemActions = (deps = {}) => {
|
|
|
105
106
|
getTotalNeareastDifference: getTotalNeareastDifference(innerDeps),
|
|
106
107
|
getNoteTags: getNoteTags(innerDeps),
|
|
107
108
|
getBasePrice: getBasePrice(innerDeps),
|
|
109
|
+
removeModifiersByQuantity: removeModifiersByQuantity(innerDeps),
|
|
108
110
|
});
|
|
109
111
|
|
|
110
112
|
Object.keys(freezedActions).forEach(actionName => {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
module.exports = ({ modifierActions }) =>
|
|
2
|
+
function removeModifiersByQuantity(item) {
|
|
3
|
+
if (!item || !Array.isArray(item.modifiers)) {
|
|
4
|
+
return item;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const modifierCounts = {};
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
...item,
|
|
11
|
+
modifiers: item.modifiers.filter(modifier => {
|
|
12
|
+
if (
|
|
13
|
+
modifierActions.isPaymentModifier(modifier) ||
|
|
14
|
+
modifierActions.isCalculatedPaymentModifier(modifier) ||
|
|
15
|
+
!modifier.modifierId ||
|
|
16
|
+
!modifierActions.isDirect(modifier)
|
|
17
|
+
)
|
|
18
|
+
return true;
|
|
19
|
+
|
|
20
|
+
const count = modifierCounts[modifier.modifierId] || 0;
|
|
21
|
+
|
|
22
|
+
if (count < item.quantity) {
|
|
23
|
+
modifierCounts[modifier.modifierId] = count + 1;
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return false;
|
|
28
|
+
}),
|
|
29
|
+
};
|
|
30
|
+
};
|
|
@@ -14,12 +14,14 @@ module.exports = ({ _, constants, utils, localization, actions }) => {
|
|
|
14
14
|
const modifier = _modifier;
|
|
15
15
|
const { name } = modifier;
|
|
16
16
|
const compute = getComputeModField(modifier);
|
|
17
|
-
const { type,
|
|
17
|
+
const { type, amount: computeAmount = 0 } = compute;
|
|
18
18
|
const _computed = {
|
|
19
19
|
amount: 0,
|
|
20
20
|
description: '',
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
+
const amount = computeAmount;
|
|
24
|
+
|
|
23
25
|
const maxAmountProp = actions.getProperty(modifier, 'maxAmount');
|
|
24
26
|
|
|
25
27
|
if (!options.skip) {
|
|
@@ -32,22 +34,41 @@ module.exports = ({ _, constants, utils, localization, actions }) => {
|
|
|
32
34
|
? math.min(maxAmountProp, maxAmountCalc)
|
|
33
35
|
: maxAmountCalc;
|
|
34
36
|
|
|
35
|
-
const multiplier =
|
|
37
|
+
const multiplier = actions.isSubtract(_modifier) ? -1 : 1;
|
|
36
38
|
|
|
37
|
-
if (type === Modifier.Compute.Types.FIXED)
|
|
39
|
+
if (type === Modifier.Compute.Types.FIXED) {
|
|
38
40
|
_computed.amount = math.mul(multiplier, amount);
|
|
41
|
+
}
|
|
39
42
|
|
|
40
|
-
if (type === Modifier.Compute.Types.PERCENTAGE)
|
|
43
|
+
if (type === Modifier.Compute.Types.PERCENTAGE) {
|
|
41
44
|
_computed.amount = math.div(
|
|
42
|
-
math.mul(multiplier, options.price,
|
|
45
|
+
math.mul(multiplier, options.price, amount),
|
|
43
46
|
100
|
|
44
47
|
);
|
|
48
|
+
}
|
|
45
49
|
|
|
46
50
|
if (!type) _computed.amount = 0;
|
|
47
51
|
|
|
48
52
|
if (math.gt(math.abs(_computed.amount), maxAmount)) {
|
|
49
53
|
_computed.amount = maxAmount * multiplier;
|
|
50
54
|
}
|
|
55
|
+
|
|
56
|
+
if (
|
|
57
|
+
(!actions.isOverride(_modifier) &&
|
|
58
|
+
actions.isQuantityMultiplier(_modifier)) ||
|
|
59
|
+
(actions.isAmountOverride(_modifier) && actions.isMultiplier(_modifier))
|
|
60
|
+
) {
|
|
61
|
+
_computed.amount = math.mul(_computed.amount, options.quantity);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (
|
|
65
|
+
actions.isSubtract(_modifier) &&
|
|
66
|
+
typeof _computed.amount === 'number' &&
|
|
67
|
+
typeof options.maxDiscountAmount === 'number' &&
|
|
68
|
+
math.gt(math.abs(_computed.amount), options.maxDiscountAmount)
|
|
69
|
+
) {
|
|
70
|
+
_computed.amount = math.mul(options.maxDiscountAmount, -1);
|
|
71
|
+
}
|
|
51
72
|
}
|
|
52
73
|
|
|
53
74
|
const localAmount =
|
|
@@ -71,7 +92,7 @@ module.exports = ({ _, constants, utils, localization, actions }) => {
|
|
|
71
92
|
|
|
72
93
|
if (actions.isAmountOverride(modifier) && isMultiplier) {
|
|
73
94
|
_computed.description = `${name} (${localization.formatAmount(
|
|
74
|
-
|
|
95
|
+
_computed.amount
|
|
75
96
|
)})`;
|
|
76
97
|
}
|
|
77
98
|
|
|
@@ -36,10 +36,9 @@ module.exports = ({ _, utils, constants, actions }) => {
|
|
|
36
36
|
!math.isZero(options.orderTotal) &&
|
|
37
37
|
(type !== Modifier.Compute.Types.PERCENTAGE || modifier.type === 'credit')
|
|
38
38
|
) {
|
|
39
|
-
const modififierQuantity =
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
: options.itemQuantity;
|
|
39
|
+
const modififierQuantity = !actions.isQuantityMultiplier(modifier)
|
|
40
|
+
? 1
|
|
41
|
+
: options.itemQuantity;
|
|
43
42
|
|
|
44
43
|
amountToApply = math.div(
|
|
45
44
|
math.mul(modifierAmount, options.itemTotal),
|
|
@@ -57,10 +56,10 @@ module.exports = ({ _, utils, constants, actions }) => {
|
|
|
57
56
|
action: compute.action || Modifier.Compute.Actions.SUBTRACT,
|
|
58
57
|
},
|
|
59
58
|
properties: {
|
|
60
|
-
ignoreQuantity:
|
|
61
|
-
(modifier.properties && modifier.properties.ignoreQuantity) ||
|
|
62
|
-
type === Modifier.Compute.Types.PERCENTAGE,
|
|
63
59
|
...(modifier.properties || {}),
|
|
60
|
+
isQuantityMultiplier:
|
|
61
|
+
type !== Modifier.Compute.Types.PERCENTAGE &&
|
|
62
|
+
actions.isQuantityMultiplier(modifier),
|
|
64
63
|
},
|
|
65
64
|
});
|
|
66
65
|
if (maxAmount !== null) delete modifierToAdd.properties.maxAmount;
|
package/lib/modifier/index.js
CHANGED
|
@@ -103,7 +103,6 @@ const isAmountOverride = require('./isAmountOverride');
|
|
|
103
103
|
const isValid = require('./isValid');
|
|
104
104
|
const isDiscount = require('./isDiscount');
|
|
105
105
|
const isHidden = require('./isHidden');
|
|
106
|
-
const isIgnoreQuantity = require('./isIgnoreQuantity');
|
|
107
106
|
const isGratuity = require('./isGratuity');
|
|
108
107
|
const isPaymentMethodModifier = require('./isPaymentMethodModifier');
|
|
109
108
|
const isPaymentTypeModifier = require('./isPaymentTypeModifier');
|
|
@@ -148,6 +147,9 @@ const isExtractCalculatedValue = require('./isExtractCalculatedValue');
|
|
|
148
147
|
const getPriceWithoutModifier = require('./getPriceWithoutModifier');
|
|
149
148
|
const isGroup = require('./isGroup');
|
|
150
149
|
const isOptionsSelectedOverride = require('./isOptionsSelectedOverride');
|
|
150
|
+
const isFixedSubtract = require('./isFixedSubtract');
|
|
151
|
+
const isQuantityMultiplier = require('./isQuantityMultiplier');
|
|
152
|
+
const isCompute = require('./isCompute');
|
|
151
153
|
|
|
152
154
|
const modifierActions = (deps = {}) => {
|
|
153
155
|
const actions = {};
|
|
@@ -262,7 +264,6 @@ const modifierActions = (deps = {}) => {
|
|
|
262
264
|
isValid: isValid(innerDeps),
|
|
263
265
|
isDiscount: isDiscount(innerDeps),
|
|
264
266
|
isHidden: isHidden(innerDeps),
|
|
265
|
-
isIgnoreQuantity: isIgnoreQuantity(innerDeps),
|
|
266
267
|
isGratuity: isGratuity(innerDeps),
|
|
267
268
|
isPaymentMethodModifier: isPaymentMethodModifier(innerDeps),
|
|
268
269
|
isPaymentTypeModifier: isPaymentTypeModifier(innerDeps),
|
|
@@ -309,6 +310,9 @@ const modifierActions = (deps = {}) => {
|
|
|
309
310
|
getPriceWithoutModifier: getPriceWithoutModifier(innerDeps),
|
|
310
311
|
isGroup: isGroup(innerDeps),
|
|
311
312
|
isOptionsSelectedOverride: isOptionsSelectedOverride(innerDeps),
|
|
313
|
+
isFixedSubtract: isFixedSubtract(innerDeps),
|
|
314
|
+
isQuantityMultiplier: isQuantityMultiplier(innerDeps),
|
|
315
|
+
isCompute: isCompute(innerDeps),
|
|
312
316
|
});
|
|
313
317
|
|
|
314
318
|
Object.keys(freezedActions).forEach(actionName => {
|
|
@@ -177,6 +177,7 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
|
|
|
177
177
|
itemIndex,
|
|
178
178
|
cache,
|
|
179
179
|
onConditionsNotMet,
|
|
180
|
+
onError,
|
|
180
181
|
}) {
|
|
181
182
|
let order = _.cloneDeep(orderProp);
|
|
182
183
|
if (!order || itemIndex < 0 || !modifier) return order;
|
|
@@ -223,6 +224,16 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
|
|
|
223
224
|
return order;
|
|
224
225
|
}
|
|
225
226
|
|
|
227
|
+
if (
|
|
228
|
+
contains &&
|
|
229
|
+
modifierActions.isCompute(modifier) &&
|
|
230
|
+
!modifierActions.isQuantityMultiplier(modifier)
|
|
231
|
+
) {
|
|
232
|
+
if (onError)
|
|
233
|
+
onError('modifier.has.reached.the.maximum.amount.of.applies');
|
|
234
|
+
return order;
|
|
235
|
+
}
|
|
236
|
+
|
|
226
237
|
item = addModifier({
|
|
227
238
|
item,
|
|
228
239
|
modifier,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/pricing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.55",
|
|
4
4
|
"description": "Pricing calculator",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"license": "ISC",
|
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
"test:item": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/item.test.js",
|
|
20
20
|
"test:split": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/split.test.js",
|
|
21
21
|
"test:getModifierTags": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/item/getModifierTags.test.js",
|
|
22
|
+
"test:createIndirectModifier": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/modifier/createIndirectModifier.test.js",
|
|
23
|
+
"test:conditionsNotMet": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/conditionsNotMet.test.js",
|
|
22
24
|
"lint": "eslint --quiet lib/"
|
|
23
25
|
},
|
|
24
26
|
"publishConfig": {
|
|
@@ -42,5 +44,5 @@
|
|
|
42
44
|
"supertest": "^6.2.3",
|
|
43
45
|
"supervisor": "^0.12.0"
|
|
44
46
|
},
|
|
45
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "4341a5b5f555e6148a83ba4dba73bb7c908a02f5"
|
|
46
48
|
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
const usePricing = require('../../../../index');
|
|
2
|
-
|
|
3
|
-
const orderToStr = require('../order-to-string');
|
|
4
|
-
|
|
5
|
-
const orderPartiallyPaid = require('../../partially-paid/order-partially-paid.json');
|
|
6
|
-
const orderModifiers = require('../../partially-paid/order-modifiers.json');
|
|
7
|
-
const inputItems = require('../../partially-paid/input-items.json');
|
|
8
|
-
|
|
9
|
-
const pricingService = usePricing();
|
|
10
|
-
|
|
11
|
-
const paymentModifiers = orderModifiers.map(orderMod =>
|
|
12
|
-
pricingService.modifier.duplicate({
|
|
13
|
-
orderMod,
|
|
14
|
-
items: inputItems,
|
|
15
|
-
})
|
|
16
|
-
);
|
|
17
|
-
|
|
18
|
-
let order = { ...orderPartiallyPaid };
|
|
19
|
-
|
|
20
|
-
order = pricingService.order.addModifiers({
|
|
21
|
-
modifiers: paymentModifiers,
|
|
22
|
-
order,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
const resultedOrder = pricingService.order.calculate(order);
|
|
26
|
-
|
|
27
|
-
orderToStr(resultedOrder);
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
const usePricing = require('../../../../index');
|
|
2
|
-
|
|
3
|
-
const orderToStr = require('../order-to-string');
|
|
4
|
-
|
|
5
|
-
const orderUnpaid = require('../../unpaid/order-not-paid.json');
|
|
6
|
-
const orderModifiers = require('../../unpaid/order-modifiers.json');
|
|
7
|
-
const inputItems = require('../../unpaid/input-items.json');
|
|
8
|
-
|
|
9
|
-
const pricingService = usePricing();
|
|
10
|
-
|
|
11
|
-
const paymentModifiers = orderModifiers.map(orderMod =>
|
|
12
|
-
pricingService.modifier.duplicate({
|
|
13
|
-
...orderMod,
|
|
14
|
-
items: inputItems,
|
|
15
|
-
})
|
|
16
|
-
);
|
|
17
|
-
|
|
18
|
-
let order = { ...orderUnpaid };
|
|
19
|
-
|
|
20
|
-
if (paymentModifiers.length > 0)
|
|
21
|
-
order = pricingService.order.addModifiers({
|
|
22
|
-
modifiers: paymentModifiers,
|
|
23
|
-
order,
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
const resultedOrder = pricingService.order.calculate(order);
|
|
27
|
-
|
|
28
|
-
orderToStr(resultedOrder);
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
module.exports = function orderToString(order) {
|
|
2
|
-
const { total, totalPaid, items } = order;
|
|
3
|
-
|
|
4
|
-
console.log({ total, totalPaid });
|
|
5
|
-
items.map(item => {
|
|
6
|
-
console.log('****');
|
|
7
|
-
console.log({
|
|
8
|
-
name: item.name,
|
|
9
|
-
price: item.price,
|
|
10
|
-
total: item.total,
|
|
11
|
-
totalPaid: item.totalPaid,
|
|
12
|
-
subtotals: item.subTotals,
|
|
13
|
-
});
|
|
14
|
-
item.modifiers.map(mod => {
|
|
15
|
-
console.log('modifier _computed', mod._computed);
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
|
-
};
|