@darkpos/pricing 1.0.52 → 1.0.54

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.
@@ -1,5 +1,3 @@
1
- const { getComputeModField } = require('../modifier/utils');
2
-
3
1
  /* eslint-disable no-restricted-syntax */
4
2
  module.exports = ({ _, utils, actions, modifierActions }) => {
5
3
  const { math } = utils;
@@ -94,6 +92,9 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
94
92
  })
95
93
  );
96
94
 
95
+ let computedPrice = price;
96
+ let total = math.mul(computedPrice, quantity);
97
+
97
98
  if (modifiersToCompute.length || paymentModifiersToCompute.length) {
98
99
  // sort modifiers based on sort
99
100
  const sortedModifiers = modifierActions.sort([
@@ -101,7 +102,6 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
101
102
  ...paymentModifiersToCompute,
102
103
  ]);
103
104
 
104
- let computedPrice = price;
105
105
  let prvPrice = 0;
106
106
  let prvSort;
107
107
 
@@ -138,6 +138,7 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
138
138
  price: computedPrice,
139
139
  quantity,
140
140
  basePrice: actions.getBasePrice(item),
141
+ maxDiscountAmount: total,
141
142
  }
142
143
  );
143
144
 
@@ -150,29 +151,7 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
150
151
 
151
152
  const { type, _computed } = _modifier;
152
153
 
153
- const computedAmountCalc = math.mul(_computed.amount, quantity);
154
- const computedPriceCalc = math.mul(computedPrice * quantity);
155
-
156
- // adding this computeField because now the compute from a modifier can be null
157
- const computeField = getComputeModField(_modifier);
158
-
159
- const isPercentage = computeField.type === 'percentage';
160
- const isIgnoreQuantity = modifierActions.isIgnoreQuantity(_modifier);
161
- const isNotFixedAddAndAmountGtPrice =
162
- !modifierActions.isFixedAdd(_modifier) &&
163
- math.gt(math.abs(computedAmountCalc), computedPriceCalc);
164
-
165
- const isAmountOverrideAndNotMultiplier =
166
- modifierActions.isAmountOverride(_modifier) &&
167
- !modifierActions.isMultiplier(_modifier);
168
-
169
- let computedAmount =
170
- isPercentage ||
171
- isIgnoreQuantity ||
172
- isNotFixedAddAndAmountGtPrice ||
173
- isAmountOverrideAndNotMultiplier
174
- ? _computed.amount
175
- : computedAmountCalc;
154
+ let computedAmount = _computed.amount;
176
155
 
177
156
  // subscription modifiers have fixed amount and can't be multiplied by quantity
178
157
  if (modifierActions.isSubscription(_modifier))
@@ -190,12 +169,15 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
190
169
  if (direct)
191
170
  subTotals._direct = math.add(subTotals._direct, computedAmount);
192
171
  else subTotals._xdirect = math.add(subTotals._xdirect, computedAmount);
172
+
173
+ subTotals._actual = math.add(subTotals._simple, subTotals._included);
174
+ total = actions.getTotal({ subTotals });
193
175
  }
194
176
  }
195
177
 
196
178
  subTotals._actual = math.add(subTotals._simple, subTotals._included);
197
179
 
198
- const total = actions.getTotal({ subTotals });
180
+ total = actions.getTotal({ subTotals });
199
181
 
200
182
  return {
201
183
  ...item,
@@ -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, action, amount = 0 } = compute;
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 = action === Modifier.Compute.Actions.SUBTRACT ? -1 : 1;
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, options.quantity, amount),
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
- math.mul(_computed.amount, options.quantity)
95
+ _computed.amount
75
96
  )})`;
76
97
  }
77
98
 
@@ -19,7 +19,7 @@ module.exports = ({ constants, actions, localization }) => {
19
19
  type: Modifier.Types.FEE,
20
20
  name: `${name} ${Modifier.Types.FEE}`,
21
21
  properties: {
22
- ignoreQuantity: true,
22
+ isQuantityMultiplier: false,
23
23
  ...properties,
24
24
  },
25
25
  ...rest,
@@ -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
- modifier.properties && modifier.properties.ignoreQuantity
41
- ? 1
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;
@@ -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,8 @@ 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');
151
152
 
152
153
  const modifierActions = (deps = {}) => {
153
154
  const actions = {};
@@ -262,7 +263,6 @@ const modifierActions = (deps = {}) => {
262
263
  isValid: isValid(innerDeps),
263
264
  isDiscount: isDiscount(innerDeps),
264
265
  isHidden: isHidden(innerDeps),
265
- isIgnoreQuantity: isIgnoreQuantity(innerDeps),
266
266
  isGratuity: isGratuity(innerDeps),
267
267
  isPaymentMethodModifier: isPaymentMethodModifier(innerDeps),
268
268
  isPaymentTypeModifier: isPaymentTypeModifier(innerDeps),
@@ -309,6 +309,8 @@ const modifierActions = (deps = {}) => {
309
309
  getPriceWithoutModifier: getPriceWithoutModifier(innerDeps),
310
310
  isGroup: isGroup(innerDeps),
311
311
  isOptionsSelectedOverride: isOptionsSelectedOverride(innerDeps),
312
+ isFixedSubtract: isFixedSubtract(innerDeps),
313
+ isQuantityMultiplier: isQuantityMultiplier(innerDeps),
312
314
  });
313
315
 
314
316
  Object.keys(freezedActions).forEach(actionName => {
@@ -0,0 +1,4 @@
1
+ module.exports = ({ actions }) =>
2
+ function isFixedSubtract(modifier) {
3
+ return actions.isSubtract(modifier) && actions.isFixed(modifier);
4
+ };
@@ -0,0 +1,5 @@
1
+ module.exports = () =>
2
+ function isQuantityMultiplier(modifier) {
3
+ if (!modifier || !modifier.properties) return false;
4
+ return !!modifier.properties.isQuantityMultiplier;
5
+ };
@@ -40,89 +40,92 @@ module.exports = ({
40
40
  ),
41
41
  });
42
42
 
43
+ let calculatedItems = itemsWNIM || [];
44
+
43
45
  const newOrder = {
44
46
  ...order,
45
47
  ...itemActions.getItemsTotals(itemsWNIM),
46
48
  items: itemsWNIM,
47
49
  };
48
50
 
49
- if (!sortedOrderModifiers.length) return newOrder;
50
-
51
- itemsWNIM = itemActions.calculate(itemsWNIM, {});
52
-
53
- let tempItems = itemsWNIM || items;
54
- let itemsWIM = _.cloneDeep(tempItems);
55
- let prvSort = null;
56
-
57
- let computedTotal = newOrder.total;
58
-
59
- for (const modifier of sortedOrderModifiers) {
60
- const isCredit = modifierActions.isCredit(modifier);
61
- const sort = modifierActions.getProperty(modifier, 'sort');
51
+ if (sortedOrderModifiers.length > 0) {
52
+ itemsWNIM = itemActions.calculate(itemsWNIM, {});
53
+
54
+ let tempItems = itemsWNIM || items;
55
+ let itemsWIM = _.cloneDeep(tempItems);
56
+ let prvSort = null;
57
+
58
+ let computedTotal = newOrder.total;
59
+
60
+ for (const modifier of sortedOrderModifiers) {
61
+ const isCredit = modifierActions.isCredit(modifier);
62
+ const sort = modifierActions.getProperty(modifier, 'sort');
63
+
64
+ if (prvSort !== sort) {
65
+ if (sort) {
66
+ tempItems = itemsWIM.map(item => ({
67
+ ...item,
68
+ modifiers: modifierActions.getModifiersByMaxSort(
69
+ item.modifiers,
70
+ modifier.properties.sort
71
+ ),
72
+ }));
73
+ } else {
74
+ tempItems = itemsWIM;
75
+ }
76
+ tempItems = itemActions.calculate(tempItems, options);
77
+ }
62
78
 
63
- if (prvSort !== sort) {
64
- if (sort) {
65
- tempItems = itemsWIM.map(item => ({
66
- ...item,
67
- modifiers: modifierActions.getModifiersByMaxSort(
68
- item.modifiers,
69
- modifier.properties.sort
70
- ),
71
- }));
72
- } else {
73
- tempItems = itemsWIM;
79
+ if (isCredit) {
80
+ const calculatedItemWIM = itemActions.calculate(itemsWIM, options);
81
+ computedTotal = itemActions.getItemsTotals(calculatedItemWIM).total;
74
82
  }
75
- tempItems = itemActions.calculate(tempItems, options);
76
- }
77
83
 
78
- if (isCredit) {
79
- const calculatedItemWIM = itemActions.calculate(itemsWIM, options);
80
- computedTotal = itemActions.getItemsTotals(calculatedItemWIM).total;
81
- }
84
+ if (modifierActions.hasItems({ modifier })) {
85
+ tempItems = itemActions.getItems(tempItems, modifier.items);
86
+ }
82
87
 
83
- if (modifierActions.hasItems({ modifier })) {
84
- tempItems = itemActions.getItems(tempItems, modifier.items);
88
+ computedTotal = itemActions.getItemsTotals(tempItems).total;
89
+
90
+ const itemsModifiers = itemActions.addIndirectModifier({
91
+ orderTotal: computedTotal,
92
+ items: tempItems,
93
+ modifier,
94
+ });
95
+
96
+ // adding the modifiers to items
97
+ itemsWIM = itemsWIM.map(item => ({
98
+ ...item,
99
+ modifiers: [
100
+ ...(item.modifiers || []),
101
+ itemsModifiers[item._id],
102
+ ].filter(Boolean),
103
+ }));
104
+ prvSort = sort;
85
105
  }
86
106
 
87
- computedTotal = itemActions.getItemsTotals(tempItems).total;
88
-
89
- const itemsModifiers = itemActions.addIndirectModifier({
90
- orderTotal: computedTotal,
91
- items: tempItems,
92
- modifier,
93
- });
94
-
95
- // adding the modifiers to items
96
- itemsWIM = itemsWIM.map(item => ({
97
- ...item,
98
- modifiers: [...(item.modifiers || []), itemsModifiers[item._id]].filter(
99
- Boolean
100
- ),
101
- }));
102
- prvSort = sort;
107
+ calculatedItems = itemActions.calculate(itemsWIM, options);
103
108
  }
104
109
 
105
- const calculatedItemWIM = itemActions.calculate(itemsWIM, options);
106
-
107
110
  const addToItemTotal = difference => {
108
- if (difference > 0 && calculatedItemWIM.length) {
109
- calculatedItemWIM[0].total = utils.math.toDecimalPlaces(
110
- utils.math.add(calculatedItemWIM[0].total, difference)
111
+ if (difference > 0 && calculatedItems.length) {
112
+ calculatedItems[0].total = utils.math.toDecimalPlaces(
113
+ utils.math.add(calculatedItems[0].total, difference)
111
114
  );
112
115
  }
113
116
  };
114
117
 
115
- if (calculatedItemWIM.length > 1) {
118
+ if (calculatedItems.length > 1) {
116
119
  const difference = itemActions.getTotalsDifference({
117
- items: calculatedItemWIM,
120
+ items: calculatedItems,
118
121
  });
119
122
 
120
123
  addToItemTotal(difference);
121
124
  }
122
125
 
123
- if (storeActions.isNeareastMultiple() && calculatedItemWIM.length > 0) {
126
+ if (storeActions.isNeareastMultiple() && calculatedItems.length > 0) {
124
127
  const difference = itemActions.getTotalNeareastDifference({
125
- items: calculatedItemWIM,
128
+ items: calculatedItems,
126
129
  });
127
130
 
128
131
  if (difference > 0) addToItemTotal(difference);
@@ -130,7 +133,7 @@ module.exports = ({
130
133
 
131
134
  return {
132
135
  ...order,
133
- ...itemActions.getItemsTotals(calculatedItemWIM),
134
- items: calculatedItemWIM,
136
+ ...itemActions.getItemsTotals(calculatedItems),
137
+ items: calculatedItems,
135
138
  };
136
139
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.52",
3
+ "version": "1.0.54",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -19,6 +19,7 @@
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",
22
23
  "lint": "eslint --quiet lib/"
23
24
  },
24
25
  "publishConfig": {
@@ -42,5 +43,5 @@
42
43
  "supertest": "^6.2.3",
43
44
  "supervisor": "^0.12.0"
44
45
  },
45
- "gitHead": "43e5d458336e6d49ffbcf8af2a715428364a4df5"
46
+ "gitHead": "8bba2c9f52f54528ea2d0dece82406ef9fe69cb3"
46
47
  }
@@ -1,5 +0,0 @@
1
- module.exports = () =>
2
- function isIgnoreQuantity(modifier) {
3
- if (!modifier || !modifier.properties) return false;
4
- return modifier.properties.ignoreQuantity;
5
- };