@darkpos/pricing 1.0.31 → 1.0.33

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.
Files changed (31) hide show
  1. package/__TEST__/item/addIndirectModifier.test.js +192 -0
  2. package/__TEST__/modifier/calculate.test.js +35 -4
  3. package/__TEST__/modifier/createIndirectModifier.test.js +166 -0
  4. package/__TEST__/modifier/getFixedModifiersTotal.test.js +52 -0
  5. package/__TEST__/modifier/getSplittedModifiers.test.js +134 -0
  6. package/__TEST__/modifier/isComputedOverride.test.js +38 -0
  7. package/__TEST__/order/addItem.test.js +57 -53
  8. package/__TEST__/order/adjustFixedModifiersDifference.test.js +117 -0
  9. package/__TEST__/order/order.test.js +47 -0
  10. package/__TEST__/order/split.test.js +356 -0
  11. package/lib/item/addIndirectModifier.js +5 -2
  12. package/lib/item/calculate.js +5 -1
  13. package/lib/item/getItemsTotalQuantity.js +7 -0
  14. package/lib/item/getItemsTotalWeight.js +7 -0
  15. package/lib/item/getTotalPieces.js +1 -1
  16. package/lib/item/getTotalQuantity.js +5 -0
  17. package/lib/item/getTotalWeight.js +7 -0
  18. package/lib/item/index.js +8 -0
  19. package/lib/modifier/calculate.js +5 -2
  20. package/lib/modifier/createIndirectModifier.js +4 -3
  21. package/lib/modifier/getFixedModifiersTotal.js +5 -4
  22. package/lib/modifier/getSplittedModifiers.js +5 -7
  23. package/lib/modifier/index.js +0 -2
  24. package/lib/modifier/isComputedOverride.js +3 -2
  25. package/lib/modifier/utils.js +10 -0
  26. package/lib/order/addItemModifier.js +9 -4
  27. package/lib/order/addModifier.js +6 -4
  28. package/lib/order/adjustFixedModifiersDifference.js +10 -9
  29. package/lib/order/splitByDepartments.js +37 -7
  30. package/package.json +2 -2
  31. package/lib/modifier/includePiecesInQuantity.js +0 -10
@@ -2,6 +2,8 @@
2
2
  module.exports = ({ _, utils, actions, modifierActions }) => {
3
3
  const { math } = utils;
4
4
  //
5
+ const { getComputeModField } = require('../modifier/utils');
6
+
5
7
  const calculateOne = inputItem => {
6
8
  const item = _.cloneDeep(inputItem);
7
9
  if (!item) return item;
@@ -70,8 +72,10 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
70
72
  const computedAmountCalc = math.mul(_computed.amount, quantity);
71
73
  const computedPriceCalc = math.mul(computedPrice * quantity);
72
74
 
75
+ // adding this computeField because now the compute from a modifier can be null
76
+ const computeField = getComputeModField(_modifier);
73
77
  let computedAmount =
74
- _modifier.compute.type === 'percentage' ||
78
+ computeField.type === 'percentage' ||
75
79
  modifierActions.isIgnoreQuantity(_modifier) ||
76
80
  (!modifierActions.isFixedAdd(_modifier) &&
77
81
  math.gt(math.abs(computedAmountCalc), computedPriceCalc))
@@ -0,0 +1,7 @@
1
+ module.exports = ({ actions }) =>
2
+ function getItemsTotalPieces(items) {
3
+ return items.reduce(
4
+ (total, item) => total + actions.getTotalQuantity(item),
5
+ 0
6
+ );
7
+ };
@@ -0,0 +1,7 @@
1
+ module.exports = ({ actions }) =>
2
+ function getItemsTotalWeight(items) {
3
+ return items.reduce(
4
+ (total, item) => total + actions.getTotalWeight(item),
5
+ 0
6
+ );
7
+ };
@@ -2,6 +2,6 @@ module.exports = ({ utils }) => {
2
2
  const { math } = utils;
3
3
  return function getTotalPieces(item) {
4
4
  if (!item) return 0;
5
- return math.mul(item.quantity || 1, item.pieces || 1);
5
+ return math.mul(item.quantity || 0, item.pieces || 0);
6
6
  };
7
7
  };
@@ -0,0 +1,5 @@
1
+ module.exports = () =>
2
+ function getQuantity(item) {
3
+ if (!item || !item.quantity) return 0;
4
+ return item.quantity;
5
+ };
@@ -0,0 +1,7 @@
1
+ module.exports = ({ utils }) => {
2
+ const { math } = utils;
3
+ return function getTotalWeight(item) {
4
+ if (!item) return 0;
5
+ return math.mul(item.quantity || 0, item.weight || 0);
6
+ };
7
+ };
package/lib/item/index.js CHANGED
@@ -1,5 +1,9 @@
1
1
  const getItemsTotalPieces = require('./getItemsTotalPieces');
2
2
  const getTotalPieces = require('./getTotalPieces');
3
+ const getItemsTotalQuantity = require('./getItemsTotalQuantity');
4
+ const getTotalQuantity = require('./getTotalQuantity');
5
+ const getItemsTotalWeight = require('./getItemsTotalWeight');
6
+ const getTotalWeight = require('./getTotalWeight');
3
7
  const removeModifier = require('./removeModifier');
4
8
  const removeModifiers = require('./removeModifiers');
5
9
  const findOriginalItem = require('./findOriginalItem');
@@ -45,6 +49,10 @@ const itemActions = (deps = {}) => {
45
49
  };
46
50
 
47
51
  const freezedActions = Object.freeze({
52
+ getItemsTotalWeight: getItemsTotalWeight(innerDeps),
53
+ getTotalWeight: getTotalWeight(innerDeps),
54
+ getItemsTotalQuantity: getItemsTotalQuantity(innerDeps),
55
+ getTotalQuantity: getTotalQuantity(innerDeps),
48
56
  getItemsTotalPieces: getItemsTotalPieces(innerDeps),
49
57
  getTotalPieces: getTotalPieces(innerDeps),
50
58
  removeModifier: removeModifier(innerDeps),
@@ -1,3 +1,5 @@
1
+ const { getComputeModField } = require('./utils');
2
+
1
3
  /**
2
4
  * Get calculated modifier
3
5
  */
@@ -9,8 +11,9 @@ module.exports = ({ _, constants, utils, localization, actions }) => {
9
11
  modifier = {},
10
12
  options = { price: 0, quantity: 0, skip: false }
11
13
  ) {
12
- const { name, compute } = modifier;
13
- const { type, action, amount = 0 } = compute || {};
14
+ const { name } = modifier;
15
+ const compute = getComputeModField(modifier);
16
+ const { type, action, amount = 0 } = compute;
14
17
  const _computed = {
15
18
  amount: 0,
16
19
  description: '',
@@ -4,15 +4,16 @@
4
4
  module.exports = ({ _, utils, constants, actions }) => {
5
5
  const { math } = utils;
6
6
  const { Modifier } = constants;
7
-
7
+ const { getComputeModField } = require('./utils');
8
8
  return function createIndirectModifier(
9
9
  modifier,
10
10
  options = { orderTotal: 0, itemQuantity: 1, itemTotal: 0 }
11
11
  ) {
12
12
  const maxAmount = actions.getProperty(modifier, 'maxAmount');
13
13
 
14
- const compute = modifier.compute || {};
15
- const { type, amount = 0 } = compute;
14
+ const compute = getComputeModField(modifier);
15
+ const type = _.get(compute, 'type', '');
16
+ const amount = _.get(compute, 'amount', 0);
16
17
  let modifierAmount = amount;
17
18
 
18
19
  if (type === Modifier.Compute.Types.PERCENTAGE) {
@@ -1,12 +1,13 @@
1
1
  module.exports = ({ utils }) => {
2
2
  const { math } = utils;
3
-
3
+ const { getComputeModField } = require('./utils');
4
4
  return function getFixedModifiersTotal(modifiers) {
5
5
  if (!Array.isArray(modifiers)) return 0;
6
6
 
7
- const fixedModifiers = modifiers.filter(
8
- mod => mod.compute.type === 'fixed'
9
- );
7
+ const fixedModifiers = modifiers.filter(mod => {
8
+ const compute = getComputeModField(mod);
9
+ return compute.type === 'fixed';
10
+ });
10
11
 
11
12
  return fixedModifiers.reduce(
12
13
  (acc, modifier) => math.add(acc, modifier.compute.amount || 0),
@@ -1,5 +1,6 @@
1
1
  module.exports = ({ utils }) => {
2
2
  const { math } = utils;
3
+ const { getComputeModField } = require('./utils');
3
4
 
4
5
  const divideModifierAmount = ({
5
6
  totalOriginOrder,
@@ -25,10 +26,11 @@ module.exports = ({ utils }) => {
25
26
  totalOriginOrder,
26
27
  totalSplitedOrder,
27
28
  });
29
+ const compute = getComputeModField(modifier);
28
30
  return {
29
31
  ...modifier,
30
32
  compute: {
31
- ...modifier.compute,
33
+ ...compute,
32
34
  },
33
35
  properties: {
34
36
  ...modifier.properties,
@@ -63,7 +65,7 @@ module.exports = ({ utils }) => {
63
65
  ) {
64
66
  return modifiers.map(each => {
65
67
  const modifier = { ...each };
66
-
68
+ const compute = getComputeModField(modifier);
67
69
  if (modifier.type === 'credit') {
68
70
  return splitCreditModifier({
69
71
  modifier,
@@ -72,11 +74,7 @@ module.exports = ({ utils }) => {
72
74
  });
73
75
  }
74
76
 
75
- if (
76
- modifier.compute &&
77
- modifier.compute.type === 'fixed' &&
78
- modifier.compute.amount
79
- ) {
77
+ if (compute && compute.type === 'fixed' && compute.amount) {
80
78
  return splitFixedModifier({
81
79
  modifier,
82
80
  totalOriginOrder,
@@ -76,7 +76,6 @@ const hasModifier = require('./hasModifier');
76
76
  const hasRelatedItems = require('./hasRelatedItems');
77
77
  const hasFixedModifier = require('./hasFixedModifier');
78
78
  const hasCreditModifier = require('./hasCreditModifier');
79
- const includePiecesInQuantity = require('./includePiecesInQuantity');
80
79
  const includesInGroup = require('./includesInGroup');
81
80
  const isTrackUsageSubscription = require('./isTrackUsageSubscription');
82
81
  const isDirect = require('./isDirect');
@@ -218,7 +217,6 @@ const modifierActions = (deps = {}) => {
218
217
  hasAttributes: hasAttributes(innerDeps),
219
218
  hasFixedModifier: hasFixedModifier(innerDeps),
220
219
  hasCreditModifier: hasCreditModifier(innerDeps),
221
- includePiecesInQuantity: includePiecesInQuantity(innerDeps),
222
220
  includesInGroup: includesInGroup(innerDeps),
223
221
  isTrackUsageSubscription: isTrackUsageSubscription(innerDeps),
224
222
  isDirect: isDirect(innerDeps),
@@ -1,7 +1,8 @@
1
- module.exports = ({ actions, constants }) => {
1
+ module.exports = ({ constants }) => {
2
2
  const { Modifier } = constants;
3
3
  return function isComputedOverride(modifier) {
4
- const computeAction = modifier && modifier.compute && modifier.compute.action;
4
+ const computeAction =
5
+ modifier && modifier.compute && modifier.compute.action;
5
6
  return computeAction === Modifier.Attributes.OVERRIDE;
6
7
  };
7
8
  };
@@ -0,0 +1,10 @@
1
+ const _ = require('lodash');
2
+
3
+ const getComputeModField = modifier =>
4
+ _.isNull(modifier.compute) || !modifier.compute
5
+ ? { type: '', amount: 0, action: '' }
6
+ : modifier.compute;
7
+
8
+ module.exports = {
9
+ getComputeModField,
10
+ };
@@ -1,9 +1,10 @@
1
1
  module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
2
2
  const { math } = utils;
3
+ const { getComputeModField } = require('../modifier/utils');
3
4
 
4
5
  const getPrice = ({ item, modifier }) => {
5
- const amount =
6
- (modifier && modifier.compute && modifier.compute.amount) || 0;
6
+ const compute = getComputeModField(modifier);
7
+ const amount = (modifier && compute && compute.amount) || 0;
7
8
  const { price = 0 } = item;
8
9
  if (modifierActions.isMultiplier(modifier)) return math.mul(price, amount);
9
10
  return amount;
@@ -64,6 +65,7 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
64
65
  },
65
66
  }) => {
66
67
  let item = { ...itemProp };
68
+ const compute = getComputeModField(modifier);
67
69
  if (hasBehaivoralFields(modifier)) return item;
68
70
  // check conditions
69
71
  if (!areConditionsMet(item, modifier.conditions)) return item;
@@ -91,7 +93,7 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
91
93
  if (modifierActions.isAmountOverride(modifier)) {
92
94
  item = {
93
95
  ...item,
94
- price: modifier.compute.amount,
96
+ price: compute.amount,
95
97
  };
96
98
  }
97
99
 
@@ -99,7 +101,10 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
99
101
  item.quantity = modifier.compute.amount || 1;
100
102
  }
101
103
 
102
- if (modifierActions.isPriceOverride(modifier) || modifierActions.isComputedOverride(modifier)) {
104
+ if (
105
+ modifierActions.isPriceOverride(modifier) ||
106
+ modifierActions.isComputedOverride(modifier)
107
+ ) {
103
108
  item.price = getPrice({ modifier, item });
104
109
  } else {
105
110
  item.price = itemActions.getItemPrice({
@@ -1,11 +1,12 @@
1
- module.exports = ({ actions, modifierActions }) =>
2
- function addModifier({ order, modifier }) {
1
+ module.exports = ({ actions, modifierActions }) => {
2
+ const { getComputeModField } = require('../modifier/utils');
3
+ return function addModifier({ order, modifier }) {
3
4
  if (!modifier) return order;
4
5
  const { modifiers = [] } = order;
5
-
6
6
  if (modifierActions.isFixedDiscount(modifier)) {
7
+ const compute = getComputeModField(modifier);
7
8
  const orderDue = actions.calculateDue(order);
8
- if (modifier.compute.amount > orderDue) {
9
+ if (compute.amount > orderDue) {
9
10
  const err = new Error();
10
11
  err.valid = false;
11
12
  err.orderDue = orderDue;
@@ -19,3 +20,4 @@ module.exports = ({ actions, modifierActions }) =>
19
20
  modifiers: [...modifiers, { ...modifier }],
20
21
  };
21
22
  };
23
+ };
@@ -1,6 +1,6 @@
1
- module.exports = ({ utils, actions, modifierActions }) => {
1
+ module.exports = ({ utils, actions, modifierActions, _ }) => {
2
2
  const { math } = utils;
3
-
3
+ const { getComputeModField } = require('../modifier/utils');
4
4
  return function adjustFixedModifiersDifference({ subOrders, difference }) {
5
5
  const selectedSubOrderIndex = subOrders.findIndex(
6
6
  subOrder =>
@@ -15,15 +15,16 @@ module.exports = ({ utils, actions, modifierActions }) => {
15
15
  };
16
16
 
17
17
  const modifierToUpdateIdx = subOrderToUpdate.modifiers.findIndex(
18
- mod => mod.compute.type === 'fixed'
18
+ mod => mod.compute && mod.compute.type === 'fixed'
19
19
  );
20
- const prevAmount =
21
- subOrderToUpdate.modifiers[modifierToUpdateIdx].compute.amount;
22
-
23
- subOrderToUpdate.modifiers[modifierToUpdateIdx].compute.amount = math.add(
24
- prevAmount,
25
- difference
20
+ const prevCompute = getComputeModField(
21
+ subOrderToUpdate.modifiers[modifierToUpdateIdx]
26
22
  );
23
+ const prevAmount = _.get(prevCompute, 'compute.amount', 0);
24
+ if (modifierToUpdateIdx !== -1) {
25
+ subOrderToUpdate.modifiers[modifierToUpdateIdx].compute.amount =
26
+ math.add(prevAmount, difference);
27
+ }
27
28
  subOrders.splice(
28
29
  selectedSubOrderIndex,
29
30
  1,
@@ -80,6 +80,9 @@ module.exports = ({ utils, _, itemActions, modifierActions, settings }) => {
80
80
  return (deps.length && deps[0].name) || 'other';
81
81
  };
82
82
 
83
+ const isSplitByPieces = splitUnit => splitUnit === 'pieces';
84
+ const isSplitByWeight = splitUnit => splitUnit === 'weight';
85
+
83
86
  return function splitByDepartments(order) {
84
87
  const { items } = order;
85
88
  const itemsByDepartments = _.groupBy(items, getDepartmentName);
@@ -94,11 +97,37 @@ module.exports = ({ utils, _, itemActions, modifierActions, settings }) => {
94
97
  const departmentName = getDepartmentName(newItems[0]);
95
98
  const maxItems = modifierActions.getDepartmentMaxItems(department);
96
99
 
97
- if (
98
- department &&
99
- maxItems &&
100
- itemActions.getItemsTotalPieces(newItems) > maxItems
101
- ) {
100
+ const autoSplit = _.get(
101
+ department || {},
102
+ 'properties.department.autoSplit',
103
+ false
104
+ );
105
+
106
+ const splitUnit = _.get(
107
+ department || {},
108
+ 'properties.department.splitUnit',
109
+ 'quantity'
110
+ );
111
+
112
+ const getItemsTotalCount = itemsParam => {
113
+ if (isSplitByPieces(splitUnit))
114
+ return itemActions.getItemsTotalPieces(itemsParam);
115
+ if (isSplitByWeight(splitUnit))
116
+ return itemActions.getItemsTotalWeight(itemsParam);
117
+ return itemActions.getItemsTotalQuantity(itemsParam);
118
+ };
119
+
120
+ const getItemTotalCount = itemParam => {
121
+ if (isSplitByPieces(splitUnit))
122
+ return itemActions.getTotalPieces(itemParam);
123
+ if (isSplitByWeight(splitUnit))
124
+ return itemActions.getTotalWeight(itemParam);
125
+ return itemActions.getTotalQuantity(itemParam);
126
+ };
127
+
128
+ const totalCount = getItemsTotalCount(newItems);
129
+
130
+ if (department && autoSplit && maxItems && totalCount > maxItems) {
102
131
  newItems.forEach(newItem => {
103
132
  for (let j = 0; j < newItem.quantity; j += 1) {
104
133
  const item = {
@@ -106,11 +135,12 @@ module.exports = ({ utils, _, itemActions, modifierActions, settings }) => {
106
135
  quantity: 1,
107
136
  _id: helpers.getObjectID(),
108
137
  };
109
- const totalPieces = itemActions.getTotalPieces(item);
138
+ const totalCountPerItem = getItemTotalCount(item);
139
+
110
140
  const index = splitOrders.findIndex(
111
141
  newOrder =>
112
142
  newOrder.items &&
113
- itemActions.getItemsTotalPieces(newOrder.items) + totalPieces <=
143
+ getItemsTotalCount(newOrder.items) + totalCountPerItem <=
114
144
  maxItems &&
115
145
  departmentName === getDepartmentName(newOrder.items[0])
116
146
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.31",
3
+ "version": "1.0.33",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -36,5 +36,5 @@
36
36
  "supertest": "^6.2.3",
37
37
  "supervisor": "^0.12.0"
38
38
  },
39
- "gitHead": "a864feac96c47884de2da753f60ea69856bcb914"
39
+ "gitHead": "fed12242f23f92ff8cad9122afd770e6569f627a"
40
40
  }
@@ -1,10 +0,0 @@
1
- module.exports = () =>
2
- function includePiecesInQuantity(modifier) {
3
- return (
4
- (modifier &&
5
- modifier.properties &&
6
- modifier.properties.department &&
7
- modifier.properties.department.includePieces) ||
8
- false
9
- );
10
- };