@darkpos/pricing 1.0.83 → 1.0.84

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.
@@ -3863,4 +3863,32 @@ describe('Order actions', () => {
3863
3863
 
3864
3864
  expect(currentOrder.modifiers.length).toBe(2);
3865
3865
  });
3866
+ test('Get calculated Order, one item with correct rounded amount in modifier description', () => {
3867
+ const discountModifier = {
3868
+ compute: {
3869
+ amount: 30,
3870
+ type: 'percentage',
3871
+ action: 'subtract',
3872
+ },
3873
+ name: 'modifier1',
3874
+ type: 'discount',
3875
+ };
3876
+
3877
+ const orderItem = {
3878
+ price: 3.65,
3879
+ quantity: 1,
3880
+ modifiers: [discountModifier],
3881
+ };
3882
+ const order = { items: [orderItem] };
3883
+ const newOrder = pricingService.order.calculate(order);
3884
+
3885
+ expect(newOrder).toHaveProperty('total', 2.56);
3886
+ expect(newOrder.items[0].modifiers[0]._computed.amount).toEqual(-1.09);
3887
+ expect(newOrder.items[0].modifiers[0]._computed.description).toEqual(
3888
+ 'modifier1 (-$1.09)'
3889
+ );
3890
+ expect(newOrder).toHaveProperty('subTotals', {
3891
+ discount: -1.09,
3892
+ });
3893
+ });
3866
3894
  });
@@ -2,6 +2,7 @@
2
2
  module.exports = ({ _, utils, actions, modifierActions }) => {
3
3
  const { math } = utils;
4
4
  //
5
+
5
6
  const calculateOne = (inputItem, opts = {}) => {
6
7
  const item = _.cloneDeep(inputItem);
7
8
  const amountToPay =
@@ -17,7 +18,7 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
17
18
 
18
19
  if (!item) return item;
19
20
 
20
- const subTotals = {
21
+ let subTotals = {
21
22
  _included: 0,
22
23
  _xincluded: 0,
23
24
  _direct: 0,
@@ -169,18 +170,13 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
169
170
  prvPrice = math.add(prvPrice, math.div(_computed.amount, quantity));
170
171
  prvSort = sort;
171
172
 
172
- if (included) {
173
- subTotals._included = math.add(subTotals._included, computedAmount);
174
- } else {
175
- subTotals._xincluded = math.add(subTotals._xincluded, computedAmount);
176
- subTotals[type] = math.add(subTotals[type] || 0, computedAmount);
177
- }
178
- if (direct)
179
- subTotals._direct = math.add(subTotals._direct, computedAmount);
180
- else subTotals._xdirect = math.add(subTotals._xdirect, computedAmount);
181
-
182
- subTotals._actual = math.add(subTotals._simple, subTotals._included);
183
- total = actions.getTotal({ subTotals });
173
+ ({ subTotals, total } = actions.getTotals({
174
+ subTotals,
175
+ amountToAdd: computedAmount,
176
+ included,
177
+ type,
178
+ direct,
179
+ }));
184
180
  }
185
181
  }
186
182
 
@@ -0,0 +1,27 @@
1
+ module.exports = ({ utils, actions }) => {
2
+ const { math } = utils;
3
+ return function getTotals({
4
+ subTotals: subTotalsParam,
5
+ amountToAdd,
6
+ included,
7
+ type,
8
+ direct,
9
+ }) {
10
+ const subTotals = { ...subTotalsParam };
11
+ if (included) {
12
+ subTotals._included = math.add(subTotals._included, amountToAdd);
13
+ } else {
14
+ subTotals._xincluded = math.add(subTotals._xincluded, amountToAdd);
15
+ subTotals[type] = math.add(subTotals[type] || 0, amountToAdd);
16
+ }
17
+ if (direct) subTotals._direct = math.add(subTotals._direct, amountToAdd);
18
+ else subTotals._xdirect = math.add(subTotals._xdirect, amountToAdd);
19
+
20
+ subTotals._actual = math.add(subTotals._simple, subTotals._included);
21
+
22
+ return {
23
+ subTotals,
24
+ total: actions.getTotal({ subTotals }),
25
+ };
26
+ };
27
+ };
package/lib/item/index.js CHANGED
@@ -59,6 +59,8 @@ const adjustFixedModifiersDifference = require('./adjustFixedModifiersDifference
59
59
  const validateCreditModifiersTotal = require('./validateCreditModifiersTotal');
60
60
  const adjustCreditModifiersDifference = require('./adjustCreditModifiersDifference');
61
61
  const getSubtotal = require('./getSubtotal');
62
+ const getTotals = require('./getTotals');
63
+ const patchItem = require('./patchItem');
62
64
 
63
65
  const itemActions = (deps = {}) => {
64
66
  const actions = {};
@@ -131,6 +133,8 @@ const itemActions = (deps = {}) => {
131
133
  validateCreditModifiersTotal: validateCreditModifiersTotal(innerDeps),
132
134
  adjustCreditModifiersDifference: adjustCreditModifiersDifference(innerDeps),
133
135
  getSubtotal: getSubtotal(innerDeps),
136
+ getTotals: getTotals(innerDeps),
137
+ patchItem: patchItem(innerDeps),
134
138
  });
135
139
 
136
140
  Object.keys(freezedActions).forEach(actionName => {
@@ -0,0 +1,31 @@
1
+ module.exports = ({ actions, modifierActions }) =>
2
+ function patchItem({ item, difference }) {
3
+ if (!item) return undefined;
4
+
5
+ const localItem = { ...item };
6
+
7
+ const modifierIndex = item.modifiers.findIndex(
8
+ mod => modifierActions.isPercentage(mod) || !modifierActions.isDirect(mod)
9
+ );
10
+
11
+ localItem.modifiers[modifierIndex] = modifierActions.patchModifier({
12
+ mod: localItem.modifiers[modifierIndex],
13
+ difference,
14
+ price: actions.getBasePrice(localItem) || localItem.price,
15
+ });
16
+
17
+ const { included, type, direct } = localItem.modifiers[modifierIndex];
18
+
19
+ const result = actions.getTotals({
20
+ subTotals: localItem.subTotals,
21
+ amountToAdd: difference,
22
+ included,
23
+ type,
24
+ direct,
25
+ });
26
+
27
+ localItem.subTotals = result.subTotals;
28
+ localItem.total = result.total;
29
+
30
+ return localItem;
31
+ };
@@ -3,7 +3,7 @@ const { getComputeModField } = require('./utils');
3
3
  /**
4
4
  * Get calculated modifier
5
5
  */
6
- module.exports = ({ _, constants, utils, localization, actions }) => {
6
+ module.exports = ({ _, constants, utils, actions }) => {
7
7
  const { math } = utils;
8
8
  const { Modifier } = constants;
9
9
 
@@ -12,7 +12,6 @@ module.exports = ({ _, constants, utils, localization, actions }) => {
12
12
  options = { price: 0, quantity: 0, skip: false, basePrice: 0 }
13
13
  ) {
14
14
  const modifier = _modifier;
15
- const { name } = modifier;
16
15
  const compute = getComputeModField(modifier);
17
16
  const { type, amount: computeAmount = 0 } = compute;
18
17
  const _computed = {
@@ -71,42 +70,14 @@ module.exports = ({ _, constants, utils, localization, actions }) => {
71
70
  }
72
71
  }
73
72
 
74
- const localAmount =
75
- !!_computed.amount && Number(_computed.amount) !== 0
76
- ? `${localization.formatAmount(_computed.amount)}`
77
- : '';
78
-
79
- const localBasePrice = localization.formatAmount(
80
- options.basePrice || options.price || 0
81
- );
82
-
83
- const isMultiplier = actions.isMultiplier(modifier);
84
-
85
- _computed.description = `${name}${localAmount ? ` (${localAmount})` : ''}`;
86
-
87
- if (actions.isPriceOverride(modifier)) {
88
- _computed.description = isMultiplier
89
- ? `${name} (${compute.amount} Unit @ ${localBasePrice}/Unit)`
90
- : `${name} (${localization.formatAmount(compute.amount)}/Unit)`;
91
- }
92
-
93
- if (actions.isAmountOverride(modifier) && isMultiplier) {
94
- _computed.description = `${name} (${localization.formatAmount(
95
- _computed.amount
96
- )})`;
97
- }
98
-
99
- if (actions.isOptionsSelectedOverride(modifier)) {
100
- const selectedOverrideOptions = _.get(
101
- modifier,
102
- 'properties.override.selected'
103
- );
104
- _computed.description = isMultiplier
105
- ? `${name} (${compute.amount} Unit @ ${localBasePrice}/${selectedOverrideOptions.selectedUnit})`
106
- : `${name} (${localization.formatAmount(compute.amount)}/${
107
- selectedOverrideOptions.selectedUnit
108
- })`;
109
- }
73
+ _computed.description = actions.createDescription({
74
+ modifier: {
75
+ ...modifier,
76
+ compute,
77
+ _computed,
78
+ },
79
+ price: options.basePrice || options.price,
80
+ });
110
81
 
111
82
  return { ..._.cloneDeep(modifier), _computed };
112
83
  };
@@ -0,0 +1,40 @@
1
+ module.exports = ({ actions, localization, _ }) =>
2
+ function createDescription({ modifier, price }) {
3
+ const { _computed, compute, name } = modifier;
4
+
5
+ let description = '';
6
+ const localAmount =
7
+ !!_computed.amount && Number(_computed.amount) !== 0
8
+ ? `${localization.formatAmount(_computed.amount)}`
9
+ : '';
10
+
11
+ const localBasePrice = localization.formatAmount(price || 0);
12
+
13
+ const isMultiplier = actions.isMultiplier(modifier);
14
+
15
+ description = `${name}${localAmount ? ` (${localAmount})` : ''}`;
16
+
17
+ if (actions.isPriceOverride(modifier)) {
18
+ description = isMultiplier
19
+ ? `${name} (${compute.amount} Unit @ ${localBasePrice}/Unit)`
20
+ : `${name} (${localization.formatAmount(compute.amount)}/Unit)`;
21
+ }
22
+
23
+ if (actions.isAmountOverride(modifier) && isMultiplier) {
24
+ description = `${name} (${localization.formatAmount(_computed.amount)})`;
25
+ }
26
+
27
+ if (actions.isOptionsSelectedOverride(modifier)) {
28
+ const selectedOverrideOptions = _.get(
29
+ modifier,
30
+ 'properties.override.selected'
31
+ );
32
+ description = isMultiplier
33
+ ? `${name} (${compute.amount} Unit @ ${localBasePrice}/${selectedOverrideOptions.selectedUnit})`
34
+ : `${name} (${localization.formatAmount(compute.amount)}/${
35
+ selectedOverrideOptions.selectedUnit
36
+ })`;
37
+ }
38
+
39
+ return description;
40
+ };
@@ -161,6 +161,8 @@ const isLimits = require('./isLimits');
161
161
  const isItemSet = require('./isItemSet');
162
162
  const isOrderUseValid = require('./isOrderUseValid');
163
163
  const getMaxItemQuantity = require('./getMaxItemQuantity');
164
+ const createDescription = require('./createDescription');
165
+ const patchModifier = require('./patchModifier');
164
166
 
165
167
  const modifierActions = (deps = {}) => {
166
168
  const actions = {};
@@ -335,6 +337,8 @@ const modifierActions = (deps = {}) => {
335
337
  getMaxItemQuantity: getMaxItemQuantity(innerDeps),
336
338
  getFixedModifiersTotalEntities: getFixedModifiersTotalEntities(innerDeps),
337
339
  getCreditModifiersTotalEntities: getCreditModifiersTotalEntities(innerDeps),
340
+ createDescription: createDescription(innerDeps),
341
+ patchModifier: patchModifier(innerDeps),
338
342
  });
339
343
 
340
344
  Object.keys(freezedActions).forEach(actionName => {
@@ -0,0 +1,21 @@
1
+ module.exports = ({ utils, actions }) =>
2
+ function patchModifier({ mod, difference, price }) {
3
+ if (!mod || !mod._computed) return mod;
4
+
5
+ const modifier = { ...mod };
6
+
7
+ modifier._computed.amount = utils.math.add(
8
+ modifier._computed.amount,
9
+ difference
10
+ );
11
+
12
+ modifier._computed.description = actions.createDescription({
13
+ modifier: {
14
+ ...modifier,
15
+ _computed: modifier._computed,
16
+ },
17
+ price,
18
+ });
19
+
20
+ return modifier;
21
+ };
@@ -108,19 +108,38 @@ module.exports = ({
108
108
  }
109
109
 
110
110
  const addToItemTotal = difference => {
111
- if (difference > 0 && calculatedItems.length) {
111
+ if (difference !== 0 && calculatedItems.length) {
112
112
  calculatedItems[0].total = utils.math.toDecimalPlaces(
113
113
  utils.math.add(calculatedItems[0].total, difference)
114
114
  );
115
115
  }
116
116
  };
117
117
 
118
- if (calculatedItems.length > 1) {
119
- const difference = itemActions.getTotalsDifference({
118
+ if (calculatedItems.length > 0) {
119
+ let difference = itemActions.getTotalsDifference({
120
120
  items: calculatedItems,
121
121
  });
122
122
 
123
- addToItemTotal(difference);
123
+ if (difference < 0) {
124
+ difference = difference < -0.005 ? 0 : utils.math.abs(difference);
125
+ }
126
+
127
+ const itemIndex = calculatedItems.findIndex(
128
+ item =>
129
+ !itemActions.isFullyPaid(item) &&
130
+ item.modifiers.some(
131
+ mod =>
132
+ modifierActions.isPercentage(mod) ||
133
+ !modifierActions.isDirect(mod)
134
+ )
135
+ );
136
+
137
+ if (itemIndex >= 0 && difference >= 0.005) {
138
+ calculatedItems[itemIndex] = itemActions.patchItem({
139
+ item: calculatedItems[itemIndex],
140
+ difference,
141
+ });
142
+ }
124
143
  }
125
144
 
126
145
  if (storeActions.isNeareastMultiple() && calculatedItems.length > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.83",
3
+ "version": "1.0.84",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -51,5 +51,5 @@
51
51
  "supertest": "^6.2.3",
52
52
  "supervisor": "^0.12.0"
53
53
  },
54
- "gitHead": "d52fea81582a64b1ca707af442d41981ed6ce78d"
54
+ "gitHead": "d15a98cdefcf27cdaf41a8096a6c6c7dcdfcc13f"
55
55
  }