@darkpos/pricing 1.0.52 → 1.0.53

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.
@@ -2620,4 +2620,45 @@ describe('Order actions', () => {
2620
2620
  _actual: 4.65,
2621
2621
  });
2622
2622
  });
2623
+
2624
+ test('CU-86dvd3dbp Get calculated Order, multiple items and indirect modifiers #2 toNeareastMultiple (No Order Modifiers)', () => {
2625
+ const pricingService2 = usePricing({
2626
+ store: { _settings: { localization: { nearestMultiple: '0.5' } } },
2627
+ });
2628
+
2629
+ const item1 = {
2630
+ _id: 1,
2631
+ price: 15.99,
2632
+ quantity: 1,
2633
+ };
2634
+ const item2 = {
2635
+ _id: 2,
2636
+ price: 4.65,
2637
+ quantity: 1,
2638
+ };
2639
+
2640
+ const order = { items: [item1, item2] };
2641
+ const newOrder = pricingService2.order.calculate(order);
2642
+ expect(newOrder).toHaveProperty('total', 21);
2643
+ expect(newOrder).toHaveProperty('subTotal', 20.64);
2644
+ expect(newOrder).toHaveProperty('subTotals', {});
2645
+ expect(newOrder.items[0]).toHaveProperty('total', 16.35);
2646
+ expect(newOrder.items[0]).toHaveProperty('subTotals', {
2647
+ _included: 0,
2648
+ _xincluded: 0,
2649
+ _direct: 0,
2650
+ _xdirect: 0,
2651
+ _simple: 15.99,
2652
+ _actual: 15.99,
2653
+ });
2654
+ expect(newOrder.items[1]).toHaveProperty('total', 4.65);
2655
+ expect(newOrder.items[1]).toHaveProperty('subTotals', {
2656
+ _included: 0,
2657
+ _xincluded: 0,
2658
+ _direct: 0,
2659
+ _xdirect: 0,
2660
+ _simple: 4.65,
2661
+ _actual: 4.65,
2662
+ });
2663
+ });
2623
2664
  });
@@ -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.53",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -42,5 +42,5 @@
42
42
  "supertest": "^6.2.3",
43
43
  "supervisor": "^0.12.0"
44
44
  },
45
- "gitHead": "43e5d458336e6d49ffbcf8af2a715428364a4df5"
45
+ "gitHead": "ad5528b8430430e2a34e124218dc4ac1d3da1681"
46
46
  }