@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.
- package/__TEST__/order/order.test.js +41 -0
- package/lib/order/calculate.js +62 -59
- package/package.json +2 -2
|
@@ -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
|
});
|
package/lib/order/calculate.js
CHANGED
|
@@ -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 (
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
84
|
+
if (modifierActions.hasItems({ modifier })) {
|
|
85
|
+
tempItems = itemActions.getItems(tempItems, modifier.items);
|
|
86
|
+
}
|
|
82
87
|
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
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 &&
|
|
109
|
-
|
|
110
|
-
utils.math.add(
|
|
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 (
|
|
118
|
+
if (calculatedItems.length > 1) {
|
|
116
119
|
const difference = itemActions.getTotalsDifference({
|
|
117
|
-
items:
|
|
120
|
+
items: calculatedItems,
|
|
118
121
|
});
|
|
119
122
|
|
|
120
123
|
addToItemTotal(difference);
|
|
121
124
|
}
|
|
122
125
|
|
|
123
|
-
if (storeActions.isNeareastMultiple() &&
|
|
126
|
+
if (storeActions.isNeareastMultiple() && calculatedItems.length > 0) {
|
|
124
127
|
const difference = itemActions.getTotalNeareastDifference({
|
|
125
|
-
items:
|
|
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(
|
|
134
|
-
items:
|
|
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.
|
|
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": "
|
|
45
|
+
"gitHead": "ad5528b8430430e2a34e124218dc4ac1d3da1681"
|
|
46
46
|
}
|