@darkpos/pricing 1.0.91 → 1.0.93
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/conditionsNotMet.test.js +124 -0
- package/__TEST__/order/getParentTotals.test.js +79 -0
- package/__TEST__/order/order-payment-modifier.test.js +332 -17
- package/__TEST__/order/order.test.js +60 -6
- package/__TEST__/order/pickEndDate.test.js +111 -25
- package/lib/item/calculate.js +10 -1
- package/lib/item/getTotalsDifference.js +7 -1
- package/lib/item/hasPaymentMethodType.js +7 -1
- package/lib/modifier/areConditionsMet.js +3 -0
- package/lib/modifier/calculatePaymentModifier.js +2 -0
- package/lib/modifier/hasPaymentMethodType.js +21 -6
- package/lib/modifier/index.js +2 -0
- package/lib/modifier/isPaymentModifier.js +2 -1
- package/lib/modifier/isPercentage.js +1 -1
- package/lib/modifier/isPrepayModifier.js +11 -0
- package/lib/modifier/sort.js +16 -27
- package/lib/order/calculate.js +2 -7
- package/lib/order/getTotals.js +21 -22
- package/lib/order/index.js +0 -2
- package/lib/order/splitItems.js +2 -1
- package/lib/store/getScheduleByCustomer.js +25 -0
- package/lib/store/index.js +6 -4
- package/lib/store/pickEndDate.js +40 -6
- package/lib/store/pickEndDateByCustomer.js +5 -0
- package/package.json +3 -2
- package/__TEST__/order/getRecommendedEndDate.test.js +0 -96
- package/lib/order/getScheduleByCustomer.js +0 -19
- package/lib/store/getRecommendedEndDate.js +0 -25
|
@@ -1960,7 +1960,7 @@ describe('Order actions', () => {
|
|
|
1960
1960
|
expect(newOrder).toHaveProperty('total', 710.36);
|
|
1961
1961
|
expect(newOrder).toHaveProperty('subTotal', 775.6);
|
|
1962
1962
|
expect(newOrder).toHaveProperty('subTotals', {
|
|
1963
|
-
percentage: -116.
|
|
1963
|
+
percentage: -116.344,
|
|
1964
1964
|
tax: 51.09265,
|
|
1965
1965
|
});
|
|
1966
1966
|
expect(newOrder.items[0]).toHaveProperty('total', 65.85);
|
|
@@ -1971,7 +1971,7 @@ describe('Order actions', () => {
|
|
|
1971
1971
|
expect(newOrder.items[5]).toHaveProperty('total', 109.91);
|
|
1972
1972
|
expect(newOrder.items[0].modifiers[0]._computed).toHaveProperty(
|
|
1973
1973
|
'amount',
|
|
1974
|
-
-10.
|
|
1974
|
+
-10.789
|
|
1975
1975
|
);
|
|
1976
1976
|
expect(newOrder.items[0].modifiers[1]._computed).toHaveProperty(
|
|
1977
1977
|
'amount',
|
|
@@ -2065,17 +2065,17 @@ describe('Order actions', () => {
|
|
|
2065
2065
|
|
|
2066
2066
|
const newOrder = pricingService.order.calculate(order);
|
|
2067
2067
|
|
|
2068
|
-
expect(newOrder).toHaveProperty('total', 137.
|
|
2068
|
+
expect(newOrder).toHaveProperty('total', 137.92);
|
|
2069
2069
|
expect(newOrder).toHaveProperty('subTotal', 128);
|
|
2070
2070
|
expect(newOrder).toHaveProperty('subTotals', {
|
|
2071
|
-
tax: 9.
|
|
2071
|
+
tax: 9.916,
|
|
2072
2072
|
});
|
|
2073
|
-
expect(newOrder.items[0]).toHaveProperty('total', 40.
|
|
2073
|
+
expect(newOrder.items[0]).toHaveProperty('total', 40.94);
|
|
2074
2074
|
expect(newOrder.items[1]).toHaveProperty('total', 45.26);
|
|
2075
2075
|
expect(newOrder.items[2]).toHaveProperty('total', 51.72);
|
|
2076
2076
|
expect(newOrder.items[0].modifiers[0]._computed).toHaveProperty(
|
|
2077
2077
|
'amount',
|
|
2078
|
-
2.
|
|
2078
|
+
2.941
|
|
2079
2079
|
);
|
|
2080
2080
|
|
|
2081
2081
|
expect(newOrder.items[1].modifiers[0]._computed).toHaveProperty(
|
|
@@ -3965,4 +3965,58 @@ describe('Order actions', () => {
|
|
|
3965
3965
|
expect(newOrder.items[0].total).toBe(25);
|
|
3966
3966
|
expect(newOrder.items[0].status).toMatchObject({});
|
|
3967
3967
|
});
|
|
3968
|
+
|
|
3969
|
+
test('Calculate order with 7% tax, expect correct decimals and rounding', () => {
|
|
3970
|
+
const taxModifier = {
|
|
3971
|
+
compute: {
|
|
3972
|
+
amount: 7,
|
|
3973
|
+
type: 'percentage',
|
|
3974
|
+
action: 'add',
|
|
3975
|
+
},
|
|
3976
|
+
name: 'tax 7%',
|
|
3977
|
+
type: 'tax',
|
|
3978
|
+
};
|
|
3979
|
+
|
|
3980
|
+
const orderItem1 = {
|
|
3981
|
+
price: 4.5,
|
|
3982
|
+
quantity: 1,
|
|
3983
|
+
modifiers: [taxModifier],
|
|
3984
|
+
};
|
|
3985
|
+
const orderItem2 = {
|
|
3986
|
+
price: 4.25,
|
|
3987
|
+
quantity: 1,
|
|
3988
|
+
modifiers: [taxModifier],
|
|
3989
|
+
};
|
|
3990
|
+
const orderItem3 = {
|
|
3991
|
+
price: 6.95,
|
|
3992
|
+
quantity: 1,
|
|
3993
|
+
modifiers: [taxModifier],
|
|
3994
|
+
};
|
|
3995
|
+
const order = { items: [orderItem1, orderItem2, orderItem3] };
|
|
3996
|
+
const newOrder = pricingService.order.calculate(order);
|
|
3997
|
+
|
|
3998
|
+
expect(newOrder).toHaveProperty('total', 16.8);
|
|
3999
|
+
expect(newOrder).toHaveProperty('subTotal', 15.7);
|
|
4000
|
+
expect(newOrder).toHaveProperty('subTotals', {
|
|
4001
|
+
tax: 1.095,
|
|
4002
|
+
});
|
|
4003
|
+
|
|
4004
|
+
expect(newOrder.items[0].modifiers[0]._computed.amount).toEqual(0.311);
|
|
4005
|
+
expect(newOrder.items[0].modifiers[0]._computed.description).toEqual(
|
|
4006
|
+
'tax 7% ($0.31)'
|
|
4007
|
+
);
|
|
4008
|
+
expect(newOrder.items[0].total).toEqual(4.81);
|
|
4009
|
+
|
|
4010
|
+
expect(newOrder.items[1].modifiers[0]._computed.amount).toEqual(0.2975);
|
|
4011
|
+
expect(newOrder.items[1].modifiers[0]._computed.description).toEqual(
|
|
4012
|
+
'tax 7% ($0.30)'
|
|
4013
|
+
);
|
|
4014
|
+
expect(newOrder.items[1].total).toEqual(4.55);
|
|
4015
|
+
|
|
4016
|
+
expect(newOrder.items[2].modifiers[0]._computed.amount).toEqual(0.4865);
|
|
4017
|
+
expect(newOrder.items[2].modifiers[0]._computed.description).toEqual(
|
|
4018
|
+
'tax 7% ($0.49)'
|
|
4019
|
+
);
|
|
4020
|
+
expect(newOrder.items[2].total).toEqual(7.44);
|
|
4021
|
+
});
|
|
3968
4022
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const moment = require('moment-timezone');
|
|
2
2
|
const usePricing = require('../../lib/index');
|
|
3
3
|
|
|
4
|
-
const
|
|
4
|
+
const getDefaultSettings = schedules => ({
|
|
5
5
|
store: {
|
|
6
6
|
_settings: {
|
|
7
7
|
schedule: {
|
|
@@ -11,6 +11,20 @@ const pricingService = usePricing({
|
|
|
11
11
|
},
|
|
12
12
|
],
|
|
13
13
|
},
|
|
14
|
+
order: {
|
|
15
|
+
schedules: schedules || [
|
|
16
|
+
{
|
|
17
|
+
_id: '682373ef8256034ae1f2c195',
|
|
18
|
+
dayOfWeek: [2],
|
|
19
|
+
recommended: 'auto_recommended',
|
|
20
|
+
addDays: 1,
|
|
21
|
+
cutDay: 1,
|
|
22
|
+
skipDays: undefined,
|
|
23
|
+
readyHour: null,
|
|
24
|
+
cutHour: null,
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
},
|
|
14
28
|
},
|
|
15
29
|
},
|
|
16
30
|
});
|
|
@@ -24,64 +38,68 @@ describe('pickEndDate function', () => {
|
|
|
24
38
|
readyHour: { hour: 17, minute: 0 },
|
|
25
39
|
cutDay: 2,
|
|
26
40
|
};
|
|
41
|
+
const pricingServiceTest = usePricing(getDefaultSettings([schedule]));
|
|
27
42
|
|
|
28
43
|
const now = moment('2024-08-25T15:00:00Z').tz('America/New_York');
|
|
29
44
|
jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
|
|
30
45
|
|
|
31
|
-
const result =
|
|
46
|
+
const result = pricingServiceTest.store.pickEndDate();
|
|
32
47
|
|
|
33
|
-
expect(result).toBe('2024-08-26T21:00:00Z');
|
|
48
|
+
expect(result).toBe('2024-08-26T21:00:00Z');
|
|
34
49
|
});
|
|
35
50
|
|
|
36
51
|
test('Order falls on a closed day', () => {
|
|
37
52
|
const schedule = {
|
|
38
53
|
addDays: 1,
|
|
39
54
|
readyHour: { hour: 14, minute: 0 },
|
|
40
|
-
skipDays: [7],
|
|
55
|
+
skipDays: [7],
|
|
41
56
|
cutHour: { hour: 16, minute: 0 },
|
|
42
57
|
cutDay: 2,
|
|
43
58
|
};
|
|
44
59
|
|
|
45
|
-
const now = moment('2024-08-27T15:00:00Z').tz('America/New_York');
|
|
60
|
+
const now = moment('2024-08-27T15:00:00Z').tz('America/New_York');
|
|
46
61
|
|
|
47
62
|
jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
|
|
48
|
-
const
|
|
63
|
+
const pricingServiceTest = usePricing(getDefaultSettings([schedule]));
|
|
64
|
+
const result = pricingServiceTest.store.pickEndDate();
|
|
49
65
|
|
|
50
|
-
expect(result).toBe('2024-08-28T18:00:00Z');
|
|
66
|
+
expect(result).toBe('2024-08-28T18:00:00Z');
|
|
51
67
|
});
|
|
52
68
|
|
|
53
69
|
test('Get endDate if today is friday and Saturday and Sunday are skip days', () => {
|
|
54
70
|
const schedule = {
|
|
55
71
|
addDays: 1,
|
|
56
72
|
readyHour: { hour: 17, minute: 0 },
|
|
57
|
-
skipDays: [6, 7],
|
|
73
|
+
skipDays: [6, 7],
|
|
58
74
|
cutHour: { hour: 16, minute: 0 },
|
|
59
75
|
cutDay: 5,
|
|
60
76
|
};
|
|
61
77
|
|
|
62
|
-
const now = moment('2024-08-23T15:00:00Z').tz('America/New_York');
|
|
78
|
+
const now = moment('2024-08-23T15:00:00Z').tz('America/New_York');
|
|
63
79
|
|
|
64
80
|
jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
|
|
65
|
-
const
|
|
81
|
+
const pricingServiceTest = usePricing(getDefaultSettings([schedule]));
|
|
82
|
+
const result = pricingServiceTest.store.pickEndDate();
|
|
66
83
|
|
|
67
|
-
expect(result).toBe('2024-08-26T21:00:00Z');
|
|
84
|
+
expect(result).toBe('2024-08-26T21:00:00Z');
|
|
68
85
|
});
|
|
69
86
|
|
|
70
87
|
test('Order time before cut-off hour', () => {
|
|
71
88
|
const schedule = {
|
|
72
|
-
addDays: 0,
|
|
89
|
+
addDays: 0,
|
|
73
90
|
readyHour: { hour: 17, minute: 0 },
|
|
74
91
|
skipDays: [],
|
|
75
|
-
cutHour: { hour: 16, minute: 0 },
|
|
92
|
+
cutHour: { hour: 16, minute: 0 },
|
|
76
93
|
cutDay: 5,
|
|
77
94
|
};
|
|
78
95
|
|
|
79
|
-
const now = moment('2024-08-25T15:00:00Z').tz('America/New_York');
|
|
96
|
+
const now = moment('2024-08-25T15:00:00Z').tz('America/New_York');
|
|
80
97
|
|
|
81
98
|
jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
|
|
82
|
-
const
|
|
99
|
+
const pricingServiceTest = usePricing(getDefaultSettings([schedule]));
|
|
100
|
+
const result = pricingServiceTest.store.pickEndDate();
|
|
83
101
|
|
|
84
|
-
expect(result).toBe('2024-08-25T21:00:00Z');
|
|
102
|
+
expect(result).toBe('2024-08-25T21:00:00Z');
|
|
85
103
|
});
|
|
86
104
|
|
|
87
105
|
test('Order time after cut-off hour', () => {
|
|
@@ -97,12 +115,13 @@ describe('pickEndDate function', () => {
|
|
|
97
115
|
recommended: 'nothing_recommended',
|
|
98
116
|
};
|
|
99
117
|
|
|
100
|
-
const now = moment().tz('America/New_York');
|
|
118
|
+
const now = moment().tz('America/New_York');
|
|
101
119
|
|
|
102
120
|
jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
|
|
103
|
-
const
|
|
121
|
+
const pricingServiceTest = usePricing(getDefaultSettings([schedule]));
|
|
122
|
+
const result = pricingServiceTest.store.pickEndDate();
|
|
104
123
|
|
|
105
|
-
expect(result).toBe('2024-08-31T21:00:00Z');
|
|
124
|
+
expect(result).toBe('2024-08-31T21:00:00Z');
|
|
106
125
|
});
|
|
107
126
|
|
|
108
127
|
test('Get EndDate, there are skip dates in between', () => {
|
|
@@ -117,9 +136,10 @@ describe('pickEndDate function', () => {
|
|
|
117
136
|
const now = moment('2024-08-25T15:00:00Z').tz('America/New_York');
|
|
118
137
|
jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
|
|
119
138
|
|
|
120
|
-
const
|
|
139
|
+
const pricingServiceTest = usePricing(getDefaultSettings([schedule]));
|
|
140
|
+
const result = pricingServiceTest.store.pickEndDate();
|
|
121
141
|
|
|
122
|
-
expect(result).toBe('2024-08-26T21:00:00Z');
|
|
142
|
+
expect(result).toBe('2024-08-26T21:00:00Z');
|
|
123
143
|
});
|
|
124
144
|
|
|
125
145
|
test('Get EndDate, Now date is an skip day', () => {
|
|
@@ -134,10 +154,12 @@ describe('pickEndDate function', () => {
|
|
|
134
154
|
const now = moment('2024-09-01T15:00:00Z').tz('America/New_York');
|
|
135
155
|
jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
|
|
136
156
|
|
|
137
|
-
const
|
|
157
|
+
const pricingServiceTest = usePricing(getDefaultSettings([schedule]));
|
|
158
|
+
const result = pricingServiceTest.store.pickEndDate();
|
|
138
159
|
|
|
139
|
-
expect(result).toBe('2024-09-05T00:00:00Z');
|
|
160
|
+
expect(result).toBe('2024-09-05T00:00:00Z');
|
|
140
161
|
});
|
|
162
|
+
|
|
141
163
|
test('Get EndDate, undefined skipdays', () => {
|
|
142
164
|
const schedule = {
|
|
143
165
|
addDays: 1,
|
|
@@ -149,8 +171,72 @@ describe('pickEndDate function', () => {
|
|
|
149
171
|
const now = moment('2025-05-13T15:00:00Z').tz('America/New_York');
|
|
150
172
|
jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
|
|
151
173
|
|
|
152
|
-
const
|
|
174
|
+
const pricingServiceTest = usePricing(getDefaultSettings([schedule]));
|
|
175
|
+
const result = pricingServiceTest.store.pickEndDate();
|
|
176
|
+
|
|
177
|
+
expect(result).toBe('2025-05-15T00:00:00Z');
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test('pickEndDate, skipDays undefined', () => {
|
|
181
|
+
const pricingServiceSkipDays = usePricing(getDefaultSettings());
|
|
182
|
+
const now = moment('2025-05-13T15:00:00Z').tz('America/New_York');
|
|
183
|
+
jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
|
|
184
|
+
|
|
185
|
+
const result = pricingServiceSkipDays.store.pickEndDate();
|
|
186
|
+
expect(result).toBe('2025-05-14T15:00:00Z');
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('pickEndDate - getRecommendedEndDate, skipDays with dayOfWeek undefined', () => {
|
|
190
|
+
const pricingServiceSkipDays2 = usePricing({
|
|
191
|
+
store: {
|
|
192
|
+
_settings: {
|
|
193
|
+
order: {
|
|
194
|
+
endDate: {
|
|
195
|
+
readySchedule: [
|
|
196
|
+
{
|
|
197
|
+
_id: '682373ef8256034ae1f2c195',
|
|
198
|
+
dayOfWeek: null,
|
|
199
|
+
recommended: 'auto_recommended',
|
|
200
|
+
addDays: 1,
|
|
201
|
+
cutDay: 1,
|
|
202
|
+
skipDays: [1],
|
|
203
|
+
readyHour: null,
|
|
204
|
+
cutHour: null,
|
|
205
|
+
},
|
|
206
|
+
],
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
const now = moment('2025-05-13T15:00:00Z').tz('America/New_York');
|
|
213
|
+
jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
|
|
214
|
+
|
|
215
|
+
const result = pricingServiceSkipDays2.store.pickEndDate();
|
|
216
|
+
|
|
217
|
+
expect(result).toBe('2025-05-13T15:00:00Z');
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test('pickEndDate - getRecommendedEndDate, skipDays with one day', () => {
|
|
221
|
+
const pricingServiceSkipDays3 = usePricing(
|
|
222
|
+
getDefaultSettings([
|
|
223
|
+
{
|
|
224
|
+
_id: '682373ef8256034ae1f2c195',
|
|
225
|
+
dayOfWeek: [2],
|
|
226
|
+
recommended: 'auto_recommended',
|
|
227
|
+
addDays: 1,
|
|
228
|
+
cutDay: 1,
|
|
229
|
+
skipDays: [1],
|
|
230
|
+
readyHour: null,
|
|
231
|
+
cutHour: null,
|
|
232
|
+
},
|
|
233
|
+
])
|
|
234
|
+
);
|
|
235
|
+
const now = moment('2025-05-13T15:00:00Z').tz('America/New_York');
|
|
236
|
+
jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
|
|
237
|
+
|
|
238
|
+
const result = pricingServiceSkipDays3.store.pickEndDate();
|
|
153
239
|
|
|
154
|
-
expect(result).toBe('2025-05-
|
|
240
|
+
expect(result).toBe('2025-05-14T15:00:00Z');
|
|
155
241
|
});
|
|
156
242
|
});
|
package/lib/item/calculate.js
CHANGED
|
@@ -9,6 +9,7 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
9
9
|
typeof opts.amountToPay === 'number' ? opts.amountToPay : 0;
|
|
10
10
|
const paymentMethod = opts.paymentMethod || null;
|
|
11
11
|
const paymentType = opts.paymentType || null;
|
|
12
|
+
const isPrepay = opts.isPrepay || false;
|
|
12
13
|
const startRequestDate = opts.startRequestDate || null;
|
|
13
14
|
const endRequestDate = opts.endRequestDate || null;
|
|
14
15
|
const lockPaymentModifiers = !!opts.lockPaymentModifiers;
|
|
@@ -70,6 +71,7 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
70
71
|
paymentModifier: modifier,
|
|
71
72
|
paymentMethod,
|
|
72
73
|
paymentType,
|
|
74
|
+
isPrepay,
|
|
73
75
|
})
|
|
74
76
|
)
|
|
75
77
|
.filter(paymentModifier => {
|
|
@@ -116,6 +118,8 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
116
118
|
let prvPrice = 0;
|
|
117
119
|
let prvSort;
|
|
118
120
|
|
|
121
|
+
let accumulatedAmount = 0;
|
|
122
|
+
|
|
119
123
|
for (const modifier of sortedModifiers) {
|
|
120
124
|
const included = modifierActions.isIncluded(modifier);
|
|
121
125
|
const direct = modifierActions.isDirect(modifier);
|
|
@@ -132,7 +136,10 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
132
136
|
if (modifierActions.isPaymentModifier(modifier)) {
|
|
133
137
|
_modifier = modifierActions.calculatePaymentModifier({
|
|
134
138
|
paymentModifier: modifier,
|
|
135
|
-
amountToPay
|
|
139
|
+
amountToPay:
|
|
140
|
+
accumulatedAmount < 0 && amountToPay > 0
|
|
141
|
+
? math.add(amountToPay, math.abs(accumulatedAmount))
|
|
142
|
+
: amountToPay,
|
|
136
143
|
itemBalance:
|
|
137
144
|
typeof item.totalPaid === 'number' && item.totalPaid > 0
|
|
138
145
|
? actions.getItemsBalance({ items: [item] })
|
|
@@ -140,6 +147,7 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
140
147
|
paymentMethod,
|
|
141
148
|
paymentType,
|
|
142
149
|
paymentId,
|
|
150
|
+
isPrepay,
|
|
143
151
|
});
|
|
144
152
|
}
|
|
145
153
|
|
|
@@ -168,6 +176,7 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
168
176
|
computedAmount = _computed.amount;
|
|
169
177
|
|
|
170
178
|
prvPrice = math.add(prvPrice, math.div(_computed.amount, quantity));
|
|
179
|
+
accumulatedAmount = math.add(accumulatedAmount, _computed.amount);
|
|
171
180
|
prvSort = sort;
|
|
172
181
|
|
|
173
182
|
({ subTotals, total } = actions.getTotals({
|
|
@@ -10,5 +10,11 @@ module.exports = ({ utils }) =>
|
|
|
10
10
|
}),
|
|
11
11
|
{ total: 0, subTotalsTotal: 0 }
|
|
12
12
|
);
|
|
13
|
-
|
|
13
|
+
const difference = utils.math.sub(subTotalsTotal, total);
|
|
14
|
+
|
|
15
|
+
if (difference < -0.005) return -0.004;
|
|
16
|
+
if (difference === -0.005) return 0.005;
|
|
17
|
+
if (difference > 0.005) return 0.004;
|
|
18
|
+
if (difference === 0.005) return -0.005;
|
|
19
|
+
return 0;
|
|
14
20
|
};
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
module.exports = ({ modifierActions }) =>
|
|
2
|
-
function hasPaymentMethodType({
|
|
2
|
+
function hasPaymentMethodType({
|
|
3
|
+
item,
|
|
4
|
+
paymentMethod,
|
|
5
|
+
paymentType,
|
|
6
|
+
isPrepay,
|
|
7
|
+
}) {
|
|
3
8
|
if (
|
|
4
9
|
!item ||
|
|
5
10
|
!Array.isArray(item.modifiers) ||
|
|
@@ -12,6 +17,7 @@ module.exports = ({ modifierActions }) =>
|
|
|
12
17
|
paymentModifier: modifier,
|
|
13
18
|
paymentMethod,
|
|
14
19
|
paymentType,
|
|
20
|
+
isPrepay,
|
|
15
21
|
})
|
|
16
22
|
);
|
|
17
23
|
};
|
|
@@ -8,12 +8,14 @@ module.exports = ({ actions, utils }) => {
|
|
|
8
8
|
paymentMethod,
|
|
9
9
|
paymentType,
|
|
10
10
|
paymentId,
|
|
11
|
+
isPrepay,
|
|
11
12
|
}) {
|
|
12
13
|
if (
|
|
13
14
|
!actions.hasPaymentMethodType({
|
|
14
15
|
paymentModifier: paymentModifierParam,
|
|
15
16
|
paymentMethod,
|
|
16
17
|
paymentType,
|
|
18
|
+
isPrepay,
|
|
17
19
|
})
|
|
18
20
|
)
|
|
19
21
|
return undefined;
|
|
@@ -3,12 +3,27 @@ module.exports = ({ actions }) =>
|
|
|
3
3
|
paymentModifier,
|
|
4
4
|
paymentMethod,
|
|
5
5
|
paymentType,
|
|
6
|
+
isPrepay,
|
|
6
7
|
}) {
|
|
7
|
-
return
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
if (!actions.isPaymentModifier(paymentModifier)) return false;
|
|
9
|
+
|
|
10
|
+
if (actions.isPrepayModifier(paymentModifier) && !isPrepay) return false;
|
|
11
|
+
|
|
12
|
+
if (
|
|
13
|
+
actions.isPaymentTypeModifier(paymentModifier) &&
|
|
14
|
+
!paymentModifier.conditions.rules.some(rule =>
|
|
15
|
+
rule.value.includes(paymentType)
|
|
12
16
|
)
|
|
13
|
-
)
|
|
17
|
+
)
|
|
18
|
+
return false;
|
|
19
|
+
|
|
20
|
+
if (
|
|
21
|
+
actions.isPaymentMethodModifier(paymentModifier) &&
|
|
22
|
+
!paymentModifier.conditions.rules.some(rule =>
|
|
23
|
+
rule.value.includes(paymentMethod)
|
|
24
|
+
)
|
|
25
|
+
)
|
|
26
|
+
return false;
|
|
27
|
+
|
|
28
|
+
return true;
|
|
14
29
|
};
|
package/lib/modifier/index.js
CHANGED
|
@@ -163,6 +163,7 @@ const isOrderUseValid = require('./isOrderUseValid');
|
|
|
163
163
|
const getMaxItemQuantity = require('./getMaxItemQuantity');
|
|
164
164
|
const createDescription = require('./createDescription');
|
|
165
165
|
const patchModifier = require('./patchModifier');
|
|
166
|
+
const isPrepayModifier = require('./isPrepayModifier');
|
|
166
167
|
|
|
167
168
|
const modifierActions = (deps = {}) => {
|
|
168
169
|
const actions = {};
|
|
@@ -339,6 +340,7 @@ const modifierActions = (deps = {}) => {
|
|
|
339
340
|
getCreditModifiersTotalEntities: getCreditModifiersTotalEntities(innerDeps),
|
|
340
341
|
createDescription: createDescription(innerDeps),
|
|
341
342
|
patchModifier: patchModifier(innerDeps),
|
|
343
|
+
isPrepayModifier: isPrepayModifier(innerDeps),
|
|
342
344
|
});
|
|
343
345
|
|
|
344
346
|
Object.keys(freezedActions).forEach(actionName => {
|
|
@@ -2,6 +2,7 @@ module.exports = ({ actions }) =>
|
|
|
2
2
|
function isPaymentModifier(modifier) {
|
|
3
3
|
return (
|
|
4
4
|
actions.isPaymentTypeModifier(modifier) ||
|
|
5
|
-
actions.isPaymentMethodModifier(modifier)
|
|
5
|
+
actions.isPaymentMethodModifier(modifier) ||
|
|
6
|
+
actions.isPrepayModifier(modifier)
|
|
6
7
|
);
|
|
7
8
|
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
module.exports = () =>
|
|
2
|
+
function isPrepayModifier(modifier) {
|
|
3
|
+
return !!(
|
|
4
|
+
modifier &&
|
|
5
|
+
modifier.conditions &&
|
|
6
|
+
Array.isArray(modifier.conditions.rules) &&
|
|
7
|
+
modifier.conditions.rules.some(
|
|
8
|
+
condition => condition.key === 'payment' && condition.value === 'prepay'
|
|
9
|
+
)
|
|
10
|
+
);
|
|
11
|
+
};
|
package/lib/modifier/sort.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
module.exports = ({ actions }) => {
|
|
3
3
|
function compareValues(key, order = 'asc') {
|
|
4
4
|
return function innerSort(a, b) {
|
|
5
|
-
// property doesn't exist on either object
|
|
6
5
|
const aVal = actions.getProperty(a, key);
|
|
7
6
|
const bVal = actions.getProperty(b, key);
|
|
8
7
|
|
|
@@ -13,38 +12,27 @@ module.exports = ({ actions }) => {
|
|
|
13
12
|
const bIsCalculatedPaymentModifier =
|
|
14
13
|
actions.isCalculatedPaymentModifier(b);
|
|
15
14
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
if (bIsCalculatedPaymentModifier && aIsPaymentModifier && bVal === null) {
|
|
20
|
-
return -1;
|
|
21
|
-
}
|
|
15
|
+
const aIsPercentageNoSort = actions.isPercentage(a) && aVal == null;
|
|
16
|
+
const bIsPercentageNoSort = actions.isPercentage(b) && bVal == null;
|
|
22
17
|
|
|
23
|
-
if (
|
|
24
|
-
(aIsPaymentModifier || aIsCalculatedPaymentModifier) &&
|
|
25
|
-
aVal === null
|
|
26
|
-
) {
|
|
18
|
+
if (bIsPaymentModifier && aIsPercentageNoSort && !bIsPercentageNoSort)
|
|
27
19
|
return 1;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
(bIsPaymentModifier || bIsCalculatedPaymentModifier) &&
|
|
32
|
-
bVal === null
|
|
33
|
-
) {
|
|
34
|
-
return 1;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (aVal == null && bVal === null) {
|
|
38
|
-
return 0;
|
|
39
|
-
}
|
|
20
|
+
if (aIsPaymentModifier && !aIsPercentageNoSort && bIsPercentageNoSort)
|
|
21
|
+
return -1;
|
|
40
22
|
|
|
41
|
-
if (aVal
|
|
23
|
+
if (aIsCalculatedPaymentModifier && bIsPaymentModifier && aVal === null)
|
|
24
|
+
return -1;
|
|
25
|
+
if (bIsCalculatedPaymentModifier && aIsPaymentModifier && bVal === null)
|
|
42
26
|
return -1;
|
|
43
|
-
}
|
|
44
27
|
|
|
45
|
-
if (
|
|
28
|
+
if ((aIsPaymentModifier || aIsCalculatedPaymentModifier) && aVal === null)
|
|
29
|
+
return 1;
|
|
30
|
+
if ((bIsPaymentModifier || bIsCalculatedPaymentModifier) && bVal === null)
|
|
46
31
|
return 1;
|
|
47
|
-
|
|
32
|
+
|
|
33
|
+
if (aVal == null && bVal === null) return 0;
|
|
34
|
+
if (aVal == null) return -1;
|
|
35
|
+
if (bVal === null) return 1;
|
|
48
36
|
|
|
49
37
|
const varA = typeof aVal === 'string' ? aVal.toUpperCase() : aVal;
|
|
50
38
|
const varB = typeof bVal === 'string' ? bVal.toUpperCase() : bVal;
|
|
@@ -52,6 +40,7 @@ module.exports = ({ actions }) => {
|
|
|
52
40
|
let comparison = 0;
|
|
53
41
|
if (varA > varB) comparison = 1;
|
|
54
42
|
else if (varA < varB) comparison = -1;
|
|
43
|
+
|
|
55
44
|
return order === 'desc' ? comparison * -1 : comparison;
|
|
56
45
|
};
|
|
57
46
|
}
|
package/lib/order/calculate.js
CHANGED
|
@@ -114,16 +114,11 @@ module.exports = ({
|
|
|
114
114
|
);
|
|
115
115
|
}
|
|
116
116
|
};
|
|
117
|
-
|
|
118
117
|
if (calculatedItems.length > 0) {
|
|
119
|
-
|
|
118
|
+
const difference = itemActions.getTotalsDifference({
|
|
120
119
|
items: calculatedItems,
|
|
121
120
|
});
|
|
122
121
|
|
|
123
|
-
if (difference < 0) {
|
|
124
|
-
difference = difference < -0.005 ? 0 : utils.math.abs(difference);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
122
|
const itemIndex = calculatedItems.findIndex(
|
|
128
123
|
item =>
|
|
129
124
|
!itemActions.isFullyPaid(item) &&
|
|
@@ -134,7 +129,7 @@ module.exports = ({
|
|
|
134
129
|
)
|
|
135
130
|
);
|
|
136
131
|
|
|
137
|
-
if (itemIndex >= 0 && difference
|
|
132
|
+
if (itemIndex >= 0 && difference !== 0) {
|
|
138
133
|
calculatedItems[itemIndex] = itemActions.patchItem({
|
|
139
134
|
item: calculatedItems[itemIndex],
|
|
140
135
|
difference,
|
package/lib/order/getTotals.js
CHANGED
|
@@ -1,29 +1,28 @@
|
|
|
1
1
|
module.exports = ({ utils }) => {
|
|
2
2
|
const { math } = utils;
|
|
3
|
+
|
|
3
4
|
return function getTotals(orders) {
|
|
4
|
-
if (!Array.isArray(orders))
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
acc.subTotals[key] || 0,
|
|
17
|
-
orderSubTotals[key]
|
|
18
|
-
);
|
|
5
|
+
if (!Array.isArray(orders)) {
|
|
6
|
+
return { total: 0, subTotal: 0, subTotals: {} };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
return orders.reduce(
|
|
10
|
+
(acc, order) => {
|
|
11
|
+
const { subTotals: orderSubTotals, subTotal = 0, total = 0 } = order;
|
|
12
|
+
|
|
13
|
+
if (orderSubTotals) {
|
|
14
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
15
|
+
for (const [key, value] of Object.entries(orderSubTotals)) {
|
|
16
|
+
acc.subTotals[key] = math.add(acc.subTotals[key] || 0, value);
|
|
19
17
|
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
acc.total = math.add(acc.total, total);
|
|
21
|
+
acc.subTotal = math.add(acc.subTotal, subTotal);
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
},
|
|
26
|
-
{ total: 0, subTotals: {} }
|
|
27
|
-
);
|
|
23
|
+
return acc;
|
|
24
|
+
},
|
|
25
|
+
{ total: 0, subTotal: 0, subTotals: {} }
|
|
26
|
+
);
|
|
28
27
|
};
|
|
29
28
|
};
|