@darkpos/pricing 1.0.22 → 1.0.24

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.
@@ -25,7 +25,9 @@ module.exports = ({ utils, modifierActions }) => {
25
25
  );
26
26
  }, 0);
27
27
 
28
- // check validation
28
+ if (modifier.compute.type === 'percentage' && modifier.type !== 'credit')
29
+ return itemsModifiers;
30
+
29
31
  if (math.abs(totalDistributed) !== math.abs(orderModifierTotal)) {
30
32
  const difference = math.sub(orderModifierTotal, totalDistributed);
31
33
  const selectedItem = items.find(
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable no-restricted-syntax */
2
- module.exports = ({ _, utils, modifierActions }) => {
2
+ module.exports = ({ _, utils, actions, modifierActions }) => {
3
3
  const { math } = utils;
4
4
  //
5
5
  const calculateOne = inputItem => {
@@ -66,14 +66,22 @@ module.exports = ({ _, utils, modifierActions }) => {
66
66
 
67
67
  modifiers.push(_modifier);
68
68
  const { type, _computed } = _modifier;
69
- let computedAmount = modifierActions.isIgnoreQuantity(_modifier)
70
- ? _computed.amount
71
- : math.mul(_computed.amount, quantity);
69
+
70
+ const computedAmountCalc = math.mul(_computed.amount, quantity);
71
+ const computedPriceCalc = math.mul(computedPrice * quantity);
72
+
73
+ let computedAmount =
74
+ _modifier.compute.type === 'percentage' ||
75
+ modifierActions.isIgnoreQuantity(_modifier) ||
76
+ math.gt(math.abs(computedAmountCalc), computedPriceCalc)
77
+ ? _computed.amount
78
+ : computedAmountCalc;
79
+
72
80
  // subscription modifiers have fixed amount and can't be multiplied by quantity
73
81
  if (modifierActions.isSubscription(_modifier))
74
82
  computedAmount = _computed.amount;
75
83
 
76
- prvPrice = math.add(prvPrice, computedAmount);
84
+ prvPrice = math.add(prvPrice, math.div(_computed.amount, quantity));
77
85
  prvSort = sort;
78
86
 
79
87
  if (included) {
@@ -90,7 +98,7 @@ module.exports = ({ _, utils, modifierActions }) => {
90
98
 
91
99
  subTotals._actual = math.add(subTotals._simple, subTotals._included);
92
100
 
93
- const total = math.add(subTotals._actual, subTotals._xincluded);
101
+ const total = math.toDecimalPlaces(actions.getTotal({ subTotals }));
94
102
 
95
103
  return {
96
104
  ...item,
@@ -1,12 +1,12 @@
1
- module.exports = ({ utils }) =>
1
+ module.exports = ({ utils, actions }) =>
2
2
  function getBalanceToPay({ orderItems = [] }) {
3
3
  const total = orderItems.reduce(
4
- (t, item) => utils.math.add(t, item && item.total),
4
+ (t, item) => utils.math.add(actions.getTotal(item), t),
5
5
  0
6
6
  );
7
7
  const totalPaid = orderItems.reduce(
8
8
  (t, item) => utils.math.add(t, item && item.totalPaid),
9
9
  0
10
10
  );
11
- return utils.math.sub(total, totalPaid);
11
+ return utils.math.toDecimalPlaces(utils.math.sub(total, totalPaid));
12
12
  };
@@ -2,7 +2,6 @@ module.exports = ({ utils }) => {
2
2
  const { math } = utils;
3
3
  return function getTotal(item) {
4
4
  if (!item || !item.subTotals) return 0;
5
-
6
- return math.add(item.subTotals._simple, item.subTotals._xincluded);
5
+ return math.add(item.subTotals._actual, item.subTotals._xincluded);
7
6
  };
8
7
  };
@@ -16,15 +16,17 @@ module.exports = ({ utils }) => {
16
16
  }
17
17
  }
18
18
 
19
- const total = Object.keys(subTotals).reduce(
20
- (acc, key) => math.add(acc, subTotals[key]),
21
- subTotal
19
+ const total = math.toDecimalPlaces(
20
+ Object.keys(subTotals).reduce(
21
+ (acc, key) => math.add(acc, subTotals[key]),
22
+ subTotal
23
+ )
22
24
  );
23
25
 
24
26
  subTotals = Object.keys(subTotals).reduce(
25
27
  (acc, key) => ({
26
28
  ...acc,
27
- [key]: subTotals[key],
29
+ [key]: math.toDecimalPlaces(subTotals[key]),
28
30
  }),
29
31
  {}
30
32
  );
@@ -1,13 +1,13 @@
1
1
  /**
2
2
  * Get calculated modifier
3
3
  */
4
- module.exports = ({ _, constants, utils, localization, actions }) => {
4
+ module.exports = ({ _, constants, utils, localization, actions }) => {
5
5
  const { math } = utils;
6
6
  const { Modifier } = constants;
7
7
 
8
8
  return function calculate(
9
9
  modifier = {},
10
- options = { price: 0, skip: false }
10
+ options = { price: 0, quantity: 0, skip: false }
11
11
  ) {
12
12
  const { name, compute } = modifier;
13
13
  const { type, action, amount = 0 } = compute || {};
@@ -15,8 +15,16 @@
15
15
  amount: 0,
16
16
  description: '',
17
17
  };
18
- const maxAmount = actions.getProperty(modifier, 'maxAmount');
18
+
19
+ const maxAmountProp = actions.getProperty(modifier, 'maxAmount');
20
+
19
21
  if (!options.skip) {
22
+ const maxAmountCalc = math.mul(options.price, options.quantity);
23
+
24
+ const maxAmount = maxAmountProp
25
+ ? math.min(maxAmountProp, maxAmountCalc)
26
+ : maxAmountCalc;
27
+
20
28
  const multiplier = action === Modifier.Compute.Actions.SUBTRACT ? -1 : 1;
21
29
 
22
30
  if (type === Modifier.Compute.Types.FIXED)
@@ -24,15 +32,16 @@
24
32
 
25
33
  if (type === Modifier.Compute.Types.PERCENTAGE)
26
34
  _computed.amount = math.div(
27
- math.mul(multiplier, options.price, amount),
35
+ math.mul(multiplier, options.price, options.quantity, amount),
28
36
  100
29
37
  );
30
38
 
31
39
  if (!type || action === Modifier.Compute.Actions.OVERRIDE)
32
40
  _computed.amount = 0;
33
41
 
34
- if (maxAmount && math.gt(math.abs(_computed.amount), maxAmount))
35
- _computed.amount = maxAmount;
42
+ if (math.gt(math.abs(_computed.amount), maxAmount)) {
43
+ _computed.amount = maxAmount * multiplier;
44
+ }
36
45
  }
37
46
 
38
47
  const localAmount = localization.formatAmount(_computed.amount);
@@ -17,7 +17,10 @@ module.exports = ({ _, utils, constants, actions }) => {
17
17
 
18
18
  if (type === Modifier.Compute.Types.PERCENTAGE) {
19
19
  const modifierAmountByPercentage = math.div(
20
- math.mul(options.orderTotal, amount),
20
+ math.mul(
21
+ modifier.type === 'credit' ? options.orderTotal : options.itemTotal,
22
+ amount
23
+ ),
21
24
  100
22
25
  );
23
26
  modifierAmount =
@@ -26,14 +29,18 @@ module.exports = ({ _, utils, constants, actions }) => {
26
29
  : modifierAmountByPercentage;
27
30
  }
28
31
 
29
- let amountToApply = 0;
32
+ let amountToApply = modifierAmount;
30
33
 
31
- if (!math.isZero(options.orderTotal))
34
+ if (
35
+ !math.isZero(options.orderTotal) &&
36
+ (type !== Modifier.Compute.Types.PERCENTAGE || modifier.type === 'credit')
37
+ ) {
32
38
  amountToApply = math.div(
33
39
  math.mul(modifierAmount, options.itemTotal),
34
40
  options.orderTotal,
35
41
  options.itemQuantity
36
42
  );
43
+ }
37
44
 
38
45
  const modifierToAdd = actions.create({
39
46
  ..._.cloneDeep(modifier),
@@ -43,6 +50,12 @@ module.exports = ({ _, utils, constants, actions }) => {
43
50
  amount: amountToApply,
44
51
  action: compute.action || Modifier.Compute.Actions.SUBTRACT,
45
52
  },
53
+ properties: {
54
+ ignoreQuantity:
55
+ (modifier.properties && modifier.properties.ignoreQuantity) ||
56
+ type === Modifier.Compute.Types.PERCENTAGE,
57
+ ...(modifier.properties || {}),
58
+ },
46
59
  });
47
60
  if (maxAmount !== null) delete modifierToAdd.properties.maxAmount;
48
61
 
@@ -7,9 +7,11 @@ module.exports = ({ utils }) => {
7
7
  totalSplitedOrder,
8
8
  }) =>
9
9
  totalOriginOrder
10
- ? math.div(
11
- math.mul(modifierAmount, totalSplitedOrder || 0),
12
- totalOriginOrder
10
+ ? math.toDecimalPlaces(
11
+ math.div(
12
+ math.mul(modifierAmount, totalSplitedOrder || 0),
13
+ totalOriginOrder
14
+ )
13
15
  )
14
16
  : 0;
15
17
 
@@ -17,6 +17,7 @@ module.exports = ({ utils }) => {
17
17
  orderSubTotals[key]
18
18
  );
19
19
  }
20
+
20
21
  return {
21
22
  total: math.add(acc.total, order.total),
22
23
  subTotals,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.22",
3
+ "version": "1.0.24",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -19,7 +19,7 @@
19
19
  "access": "public"
20
20
  },
21
21
  "dependencies": {
22
- "@darkpos/utils": "1.0.8",
22
+ "@darkpos/utils": "1.0.9",
23
23
  "lodash": "^4.17.21",
24
24
  "moment-timezone": "^0.5.34"
25
25
  },
@@ -36,5 +36,5 @@
36
36
  "supertest": "^6.2.3",
37
37
  "supervisor": "^0.12.0"
38
38
  },
39
- "gitHead": "82cfaf282b53da5c4343a36daad7e7d16fe4aa2c"
39
+ "gitHead": "a5c31d32fc9c21bc3d643d02e1171438b38f6826"
40
40
  }