@darkpos/pricing 1.0.43 → 1.0.45
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__/item/getItemsModifierDescription.test.js +75 -78
- package/__TEST__/mocks/addItemMock.js +18729 -19357
- package/__TEST__/mocks/partially-paid/order-modifiers.json +48 -51
- package/__TEST__/mocks/partially-paid/order-partially-paid.json +893 -860
- package/__TEST__/mocks/scripts/calculate-partially-paid/index.js +2 -2
- package/__TEST__/mocks/scripts/calculate-unpaid/index.js +2 -2
- package/__TEST__/mocks/unpaid/order-modifiers.json +48 -52
- package/__TEST__/modifier/calculate.test.js +1 -0
- package/__TEST__/modifier/getMatchTagsModifiers.test.js +72 -18
- package/__TEST__/modifier/getModifierIndex.test.js +10 -7
- package/__TEST__/modifier/getRecommendedModifiers.test.js +6 -4
- package/__TEST__/modifier/hasAttribute.test.js +11 -5
- package/__TEST__/modifier/hasMatchTags.test.js +33 -11
- package/__TEST__/modifier/sort.test.js +1 -1
- package/__TEST__/order/conditionsNotMet.test.js +133 -0
- package/__TEST__/order/manualSplit.test.js +104 -0
- package/__TEST__/order/order-payment-modifier.test.js +1117 -0
- package/__TEST__/order/order.test.js +53 -105
- package/__TEST__/order/pickEndDate.test.js +7 -153
- package/__TEST__/order/validateConditionsCalculate.test.js +396 -0
- package/lib/constants/index.js +1 -1
- package/lib/index.js +11 -2
- package/lib/item/calculate.js +93 -18
- package/lib/item/getAmounts.js +14 -0
- package/lib/item/getBalance.js +0 -1
- package/lib/item/getItemModifiersDescription.js +1 -0
- package/lib/item/getItemsBalance.js +9 -0
- package/lib/item/getItemsTotals.js +27 -0
- package/lib/item/getTotal.js +3 -1
- package/lib/item/hasPaymentMethodType.js +17 -0
- package/lib/item/hasPaymentModifierWithPaymentId.js +9 -0
- package/lib/item/index.js +15 -8
- package/lib/item/removePaymentModifiersByPaymentId.js +13 -0
- package/lib/modifier/areConditionsMet.js +61 -0
- package/lib/modifier/calculatePaymentDiscountModifier.js +31 -0
- package/lib/modifier/calculatePaymentFeeModifier.js +27 -0
- package/lib/modifier/calculatePaymentModifier.js +67 -0
- package/lib/modifier/createDiscountModifier.js +2 -1
- package/lib/modifier/createFeeModifier.js +2 -1
- package/lib/modifier/getChildren.js +8 -0
- package/lib/modifier/getComputedAmount.js +9 -0
- package/lib/modifier/hasPaymentMethodType.js +14 -0
- package/lib/modifier/index.js +33 -9
- package/lib/modifier/isCalculatedPaymentModifier.js +4 -0
- package/lib/modifier/isChild.js +6 -0
- package/lib/modifier/isDiscount.js +6 -2
- package/lib/modifier/isFee.js +5 -2
- package/lib/modifier/isPaymentMethodModifier.js +4 -1
- package/lib/modifier/isPaymentTypeModifier.js +4 -1
- package/lib/modifier/isPercentage.js +10 -0
- package/lib/modifier/isValid.js +12 -0
- package/lib/modifier/sort.js +28 -0
- package/lib/modifier/validate.js +14 -0
- package/lib/modifier/validateDateDaysDiff.js +30 -0
- package/lib/modifier/validateInArr.js +12 -0
- package/lib/modifier/validateNumberCondition.js +20 -0
- package/lib/modifier/validateRequiredModifiers.js +16 -0
- package/lib/order/addItemModifier.js +72 -28
- package/lib/order/applyPayment.js +61 -0
- package/lib/order/calculate.js +45 -22
- package/lib/order/getBalance.js +0 -1
- package/lib/order/getOrdersBalance.js +8 -0
- package/lib/order/index.js +9 -10
- package/lib/order/manualSplit.js +34 -0
- package/lib/order/manualSplitByQuantity.js +50 -0
- package/lib/store/getRecommendedEndDate.js +13 -0
- package/lib/store/index.js +25 -0
- package/package.json +6 -4
- package/lib/item/getBalanceToPay.js +0 -12
- package/lib/item/getTotals.js +0 -40
- package/lib/item/markModifiersAsLocked.js +0 -11
- package/lib/modifier/createPaymentModifier.js +0 -12
- package/lib/modifier/findByPaymentMethod.js +0 -10
- package/lib/modifier/findByPaymentType.js +0 -10
- package/lib/modifier/getLockedModifiers.js +0 -5
- package/lib/order/markModifiersAsLocked.js +0 -14
- package/lib/order/removeModifiersWithPaymentMethods.js +0 -29
- package/lib/order/removeModifiersWithPaymentTypes.js +0 -27
- package/lib/{order → store}/pickEndDate.js +2 -2
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
const usePricing = require('../../index');
|
|
2
|
+
|
|
3
|
+
const pricingService = usePricing();
|
|
4
|
+
|
|
5
|
+
describe('Conditions not met for the item', () => {
|
|
6
|
+
test('#1: Item pieces condition is not met, item price remains the same', () => {
|
|
7
|
+
const order = {
|
|
8
|
+
id: 'ord-123',
|
|
9
|
+
items: [],
|
|
10
|
+
modifiers: [],
|
|
11
|
+
};
|
|
12
|
+
const item = {
|
|
13
|
+
quantity: 1,
|
|
14
|
+
pieces: 1,
|
|
15
|
+
itemId: '123',
|
|
16
|
+
price: 100,
|
|
17
|
+
modifiers: [
|
|
18
|
+
{
|
|
19
|
+
compute: {
|
|
20
|
+
amount: 30,
|
|
21
|
+
type: 'fixed',
|
|
22
|
+
action: 'substract',
|
|
23
|
+
},
|
|
24
|
+
conditions: {
|
|
25
|
+
rules: [
|
|
26
|
+
{
|
|
27
|
+
key: 'itemPieces',
|
|
28
|
+
value: 5,
|
|
29
|
+
operand: '$gt',
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
};
|
|
36
|
+
order.items.push(item);
|
|
37
|
+
const { calculate } = pricingService.order;
|
|
38
|
+
// Calculate the order
|
|
39
|
+
const calculatedOrder = calculate(order);
|
|
40
|
+
expect(calculatedOrder).toHaveProperty('total', 100);
|
|
41
|
+
expect(calculatedOrder).toHaveProperty('subTotal', 100);
|
|
42
|
+
});
|
|
43
|
+
test('#2: Item pieces condition is met, item total is updated', () => {
|
|
44
|
+
const order = {
|
|
45
|
+
id: 'ord-123',
|
|
46
|
+
items: [],
|
|
47
|
+
modifiers: [],
|
|
48
|
+
};
|
|
49
|
+
const item = {
|
|
50
|
+
quantity: 1,
|
|
51
|
+
pieces: 6,
|
|
52
|
+
itemId: '123',
|
|
53
|
+
price: 100,
|
|
54
|
+
modifiers: [
|
|
55
|
+
{
|
|
56
|
+
compute: {
|
|
57
|
+
amount: 30,
|
|
58
|
+
type: 'fixed',
|
|
59
|
+
action: 'subtract',
|
|
60
|
+
},
|
|
61
|
+
conditions: {
|
|
62
|
+
rules: [
|
|
63
|
+
{
|
|
64
|
+
key: 'itemPieces',
|
|
65
|
+
value: 5,
|
|
66
|
+
operand: '$gt',
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
};
|
|
73
|
+
order.items.push(item);
|
|
74
|
+
const { calculate } = pricingService.order;
|
|
75
|
+
// Calculate the order
|
|
76
|
+
|
|
77
|
+
const calculatedOrder = calculate(order);
|
|
78
|
+
expect(calculatedOrder).toHaveProperty('total', 70);
|
|
79
|
+
expect(calculatedOrder).toHaveProperty('subTotal', 100);
|
|
80
|
+
});
|
|
81
|
+
test('#3: Required modifiers condition is not met, item price remains the same', () => {
|
|
82
|
+
const order = {
|
|
83
|
+
id: 'ord-123',
|
|
84
|
+
items: [],
|
|
85
|
+
modifiers: [],
|
|
86
|
+
};
|
|
87
|
+
const item = {
|
|
88
|
+
quantity: 1,
|
|
89
|
+
itemId: '123',
|
|
90
|
+
price: 100,
|
|
91
|
+
modifiers: [],
|
|
92
|
+
};
|
|
93
|
+
order.items.push(item);
|
|
94
|
+
const modifier = {
|
|
95
|
+
conditions: {
|
|
96
|
+
rules: [
|
|
97
|
+
{
|
|
98
|
+
key: 'modifiers',
|
|
99
|
+
value: [{ _id: 'mod-1' }],
|
|
100
|
+
operand: '$in',
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
},
|
|
104
|
+
compute: {
|
|
105
|
+
amount: 30,
|
|
106
|
+
type: 'fixed',
|
|
107
|
+
action: 'subtract',
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
item.modifiers.push(modifier);
|
|
111
|
+
const { calculate } = pricingService.order;
|
|
112
|
+
// Calculate the order
|
|
113
|
+
const calculatedOrder = calculate(order);
|
|
114
|
+
expect(calculatedOrder).toHaveProperty('total', 100);
|
|
115
|
+
expect(calculatedOrder).toHaveProperty('subTotal', 100);
|
|
116
|
+
});
|
|
117
|
+
test('#4: Required modifiers condition is met, item total is updated', () => {
|
|
118
|
+
const order = {
|
|
119
|
+
id: 'ord-123',
|
|
120
|
+
items: [],
|
|
121
|
+
modifiers: [],
|
|
122
|
+
};
|
|
123
|
+
const item = {
|
|
124
|
+
quantity: 1,
|
|
125
|
+
itemId: '123',
|
|
126
|
+
price: 100,
|
|
127
|
+
modifiers: [],
|
|
128
|
+
};
|
|
129
|
+
const modifierToValidate = {
|
|
130
|
+
_id: 'mod-1',
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const modifier = {
|
|
134
|
+
conditions: {
|
|
135
|
+
rules: [
|
|
136
|
+
{
|
|
137
|
+
key: 'modifiers',
|
|
138
|
+
value: [{ modifierId: 'mod-1' }],
|
|
139
|
+
operand: '$in',
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
},
|
|
143
|
+
compute: {
|
|
144
|
+
amount: 30,
|
|
145
|
+
type: 'fixed',
|
|
146
|
+
action: 'subtract',
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
item.modifiers.push(modifierToValidate);
|
|
150
|
+
item.modifiers.push(modifier);
|
|
151
|
+
order.items.push(item);
|
|
152
|
+
const { calculate } = pricingService.order;
|
|
153
|
+
const calculatedOrder = calculate(order);
|
|
154
|
+
expect(calculatedOrder).toHaveProperty('total', 70);
|
|
155
|
+
expect(calculatedOrder).toHaveProperty('subTotal', 100);
|
|
156
|
+
});
|
|
157
|
+
test('#5: endDateDays condition is not met, item price remains the same', () => {
|
|
158
|
+
const order = {
|
|
159
|
+
id: 'ord-123',
|
|
160
|
+
items: [],
|
|
161
|
+
modifiers: [],
|
|
162
|
+
start: {
|
|
163
|
+
requestDate: new Date('2020-01-01'),
|
|
164
|
+
},
|
|
165
|
+
end: {
|
|
166
|
+
requestDate: new Date('2020-01-02'),
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
const item = {
|
|
170
|
+
quantity: 1,
|
|
171
|
+
itemId: '123',
|
|
172
|
+
price: 100,
|
|
173
|
+
modifiers: [],
|
|
174
|
+
};
|
|
175
|
+
order.items.push(item);
|
|
176
|
+
const modifier = {
|
|
177
|
+
conditions: {
|
|
178
|
+
rules: [
|
|
179
|
+
{
|
|
180
|
+
key: 'endDateDays',
|
|
181
|
+
value: 5,
|
|
182
|
+
operand: '$gt',
|
|
183
|
+
},
|
|
184
|
+
],
|
|
185
|
+
},
|
|
186
|
+
compute: {
|
|
187
|
+
amount: 30,
|
|
188
|
+
type: 'fixed',
|
|
189
|
+
action: 'subtract',
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
item.modifiers.push(modifier);
|
|
193
|
+
const { calculate } = pricingService.order;
|
|
194
|
+
// Calculate the order
|
|
195
|
+
const calculatedOrder = calculate(order);
|
|
196
|
+
expect(calculatedOrder).toHaveProperty('total', 100);
|
|
197
|
+
expect(calculatedOrder).toHaveProperty('subTotal', 100);
|
|
198
|
+
});
|
|
199
|
+
test('#6: endDateDays condition is met, item total is updated', () => {
|
|
200
|
+
const order = {
|
|
201
|
+
id: 'ord-123',
|
|
202
|
+
items: [],
|
|
203
|
+
modifiers: [],
|
|
204
|
+
start: {
|
|
205
|
+
requestDate: new Date('2020-01-01'),
|
|
206
|
+
},
|
|
207
|
+
end: {
|
|
208
|
+
requestDate: new Date('2020-01-08'),
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
const item = {
|
|
212
|
+
quantity: 1,
|
|
213
|
+
itemId: '123',
|
|
214
|
+
price: 100,
|
|
215
|
+
modifiers: [],
|
|
216
|
+
};
|
|
217
|
+
order.items.push(item);
|
|
218
|
+
const modifier = {
|
|
219
|
+
compute: {
|
|
220
|
+
amount: 30,
|
|
221
|
+
type: 'fixed',
|
|
222
|
+
action: 'subtract',
|
|
223
|
+
},
|
|
224
|
+
conditions: {
|
|
225
|
+
rules: [
|
|
226
|
+
{
|
|
227
|
+
key: 'endDateDays',
|
|
228
|
+
value: 5,
|
|
229
|
+
operand: '$gt',
|
|
230
|
+
},
|
|
231
|
+
],
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
item.modifiers.push(modifier);
|
|
235
|
+
const { calculate } = pricingService.order;
|
|
236
|
+
// Calculate the order
|
|
237
|
+
const calculatedOrder = calculate(order);
|
|
238
|
+
expect(calculatedOrder).toHaveProperty('total', 70);
|
|
239
|
+
expect(calculatedOrder).toHaveProperty('subTotal', 100);
|
|
240
|
+
});
|
|
241
|
+
// Do tests for paymentMethods and paymentTypes
|
|
242
|
+
test('#7: paymentMethods condition is not met, item price remains the same', () => {
|
|
243
|
+
const order = {
|
|
244
|
+
id: 'ord-123',
|
|
245
|
+
items: [],
|
|
246
|
+
modifiers: [],
|
|
247
|
+
};
|
|
248
|
+
const item = {
|
|
249
|
+
quantity: 1,
|
|
250
|
+
itemId: '123',
|
|
251
|
+
price: 100,
|
|
252
|
+
modifiers: [],
|
|
253
|
+
total: 100,
|
|
254
|
+
};
|
|
255
|
+
order.items.push(item);
|
|
256
|
+
const modifier = {
|
|
257
|
+
conditions: {
|
|
258
|
+
rules: [
|
|
259
|
+
{
|
|
260
|
+
key: 'paymentMethods',
|
|
261
|
+
value: ['cash'],
|
|
262
|
+
operand: '$in',
|
|
263
|
+
},
|
|
264
|
+
],
|
|
265
|
+
_id: 'abcd',
|
|
266
|
+
modifierId: '123',
|
|
267
|
+
},
|
|
268
|
+
compute: {
|
|
269
|
+
amount: 30,
|
|
270
|
+
type: 'fixed',
|
|
271
|
+
action: 'subtract',
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
item.modifiers.push(modifier);
|
|
275
|
+
const { calculate } = pricingService.order;
|
|
276
|
+
const calculatedOrder = calculate(order, { paymentType: 'credit' });
|
|
277
|
+
expect(calculatedOrder).toHaveProperty('total', 100);
|
|
278
|
+
expect(calculatedOrder).toHaveProperty('subTotal', 100);
|
|
279
|
+
});
|
|
280
|
+
test('#8: paymentMethods condition is met, item total is updated', () => {
|
|
281
|
+
const order = {
|
|
282
|
+
id: 'ord-123',
|
|
283
|
+
items: [],
|
|
284
|
+
modifiers: [],
|
|
285
|
+
};
|
|
286
|
+
const item = {
|
|
287
|
+
quantity: 1,
|
|
288
|
+
itemId: '123',
|
|
289
|
+
price: 100,
|
|
290
|
+
modifiers: [],
|
|
291
|
+
total: 100,
|
|
292
|
+
};
|
|
293
|
+
order.items.push(item);
|
|
294
|
+
const modifier = {
|
|
295
|
+
conditions: {
|
|
296
|
+
rules: [
|
|
297
|
+
{
|
|
298
|
+
key: 'paymentMethods',
|
|
299
|
+
value: ['cash'],
|
|
300
|
+
operand: '$in',
|
|
301
|
+
},
|
|
302
|
+
],
|
|
303
|
+
},
|
|
304
|
+
compute: {
|
|
305
|
+
amount: 30,
|
|
306
|
+
type: 'fixed',
|
|
307
|
+
action: 'subtract',
|
|
308
|
+
},
|
|
309
|
+
_id: '123',
|
|
310
|
+
modifierId: 'abc',
|
|
311
|
+
};
|
|
312
|
+
item.modifiers.push(modifier);
|
|
313
|
+
const { calculate } = pricingService.order;
|
|
314
|
+
|
|
315
|
+
const calculatedOrder = calculate(order, { paymentMethod: 'cash' });
|
|
316
|
+
expect(calculatedOrder).toHaveProperty('total', 70);
|
|
317
|
+
expect(calculatedOrder).toHaveProperty('subTotal', 100);
|
|
318
|
+
});
|
|
319
|
+
test('#9: paymentTypes condition is not met, item price remains the same', () => {
|
|
320
|
+
const order = {
|
|
321
|
+
_id: 'ord-123',
|
|
322
|
+
items: [],
|
|
323
|
+
};
|
|
324
|
+
const item = {
|
|
325
|
+
quantity: 1,
|
|
326
|
+
itemId: '123',
|
|
327
|
+
price: 100,
|
|
328
|
+
modifiers: [],
|
|
329
|
+
};
|
|
330
|
+
order.items.push(item);
|
|
331
|
+
const modifier = {
|
|
332
|
+
conditions: {
|
|
333
|
+
rules: [
|
|
334
|
+
{
|
|
335
|
+
key: 'paymentTypes',
|
|
336
|
+
value: ['cash'],
|
|
337
|
+
operand: '$in',
|
|
338
|
+
},
|
|
339
|
+
],
|
|
340
|
+
},
|
|
341
|
+
compute: {
|
|
342
|
+
amount: 30,
|
|
343
|
+
type: 'fixed',
|
|
344
|
+
action: 'subtract',
|
|
345
|
+
},
|
|
346
|
+
_id: '111',
|
|
347
|
+
modifierId: 'ccc',
|
|
348
|
+
};
|
|
349
|
+
item.modifiers.push(modifier);
|
|
350
|
+
const { calculate } = pricingService.order;
|
|
351
|
+
|
|
352
|
+
// Calculate the order
|
|
353
|
+
const calculatedOrder = calculate(order, { paymentType: 'credit' });
|
|
354
|
+
expect(calculatedOrder).toHaveProperty('total', 100);
|
|
355
|
+
expect(calculatedOrder).toHaveProperty('subTotal', 100);
|
|
356
|
+
});
|
|
357
|
+
test('#10: paymentTypes condition is met, item total is updated', () => {
|
|
358
|
+
const item = {
|
|
359
|
+
quantity: 1,
|
|
360
|
+
itemId: '123',
|
|
361
|
+
price: 100,
|
|
362
|
+
modifiers: [
|
|
363
|
+
{
|
|
364
|
+
conditions: {
|
|
365
|
+
rules: [
|
|
366
|
+
{
|
|
367
|
+
key: 'paymentTypes',
|
|
368
|
+
value: ['cash'],
|
|
369
|
+
operand: '$in',
|
|
370
|
+
},
|
|
371
|
+
],
|
|
372
|
+
},
|
|
373
|
+
compute: {
|
|
374
|
+
amount: 30,
|
|
375
|
+
type: 'fixed',
|
|
376
|
+
action: 'subtract',
|
|
377
|
+
},
|
|
378
|
+
_id: '123',
|
|
379
|
+
modifierId: 'bcd',
|
|
380
|
+
},
|
|
381
|
+
],
|
|
382
|
+
total: 100,
|
|
383
|
+
};
|
|
384
|
+
const { calculate } = pricingService.order;
|
|
385
|
+
|
|
386
|
+
const calculatedOrder = calculate(
|
|
387
|
+
{
|
|
388
|
+
_id: 'ord-123',
|
|
389
|
+
items: [item],
|
|
390
|
+
},
|
|
391
|
+
{ paymentType: 'cash' }
|
|
392
|
+
);
|
|
393
|
+
expect(calculatedOrder).toHaveProperty('total', 70);
|
|
394
|
+
expect(calculatedOrder).toHaveProperty('subTotal', 100);
|
|
395
|
+
});
|
|
396
|
+
});
|
package/lib/constants/index.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -2,6 +2,7 @@ const utils = require('@darkpos/utils');
|
|
|
2
2
|
const _ = require('lodash');
|
|
3
3
|
const moment = require('moment-timezone');
|
|
4
4
|
/** services */
|
|
5
|
+
const makeStoreActions = require('./store');
|
|
5
6
|
const makeItemActions = require('./item');
|
|
6
7
|
const makeOrderActions = require('./order');
|
|
7
8
|
const makeModifierActions = require('./modifier');
|
|
@@ -22,18 +23,26 @@ module.exports = session => {
|
|
|
22
23
|
};
|
|
23
24
|
|
|
24
25
|
//
|
|
25
|
-
|
|
26
|
-
const
|
|
26
|
+
// general actions
|
|
27
|
+
const storeActions = makeStoreActions(deps);
|
|
28
|
+
const modifierActions = makeModifierActions({ ...deps, storeActions });
|
|
29
|
+
const itemActions = makeItemActions({
|
|
30
|
+
...deps,
|
|
31
|
+
modifierActions,
|
|
32
|
+
storeActions,
|
|
33
|
+
});
|
|
27
34
|
const orderActions = makeOrderActions({
|
|
28
35
|
...deps,
|
|
29
36
|
itemActions,
|
|
30
37
|
modifierActions,
|
|
38
|
+
storeActions,
|
|
31
39
|
});
|
|
32
40
|
|
|
33
41
|
return {
|
|
34
42
|
item: itemActions,
|
|
35
43
|
order: orderActions,
|
|
36
44
|
modifier: modifierActions,
|
|
45
|
+
store: storeActions,
|
|
37
46
|
constants,
|
|
38
47
|
};
|
|
39
48
|
};
|
package/lib/item/calculate.js
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
|
+
const { getComputeModField } = require('../modifier/utils');
|
|
2
|
+
|
|
1
3
|
/* eslint-disable no-restricted-syntax */
|
|
2
4
|
module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
3
5
|
const { math } = utils;
|
|
4
6
|
//
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
const calculateOne = inputItem => {
|
|
7
|
+
const calculateOne = (inputItem, opts = {}) => {
|
|
8
8
|
const item = _.cloneDeep(inputItem);
|
|
9
|
+
const amountToPay =
|
|
10
|
+
typeof opts.amountToPay === 'number' ? opts.amountToPay : 0;
|
|
11
|
+
const paymentMethod = opts.paymentMethod || null;
|
|
12
|
+
const paymentType = opts.paymentType || null;
|
|
13
|
+
const startRequestDate = opts.startRequestDate || null;
|
|
14
|
+
const endRequestDate = opts.endRequestDate || null;
|
|
15
|
+
const lockPaymentModifiers = !!opts.lockPaymentModifiers;
|
|
16
|
+
const { paymentId } = opts;
|
|
17
|
+
|
|
9
18
|
if (!item) return item;
|
|
10
19
|
|
|
11
20
|
const subTotals = {
|
|
@@ -23,28 +32,68 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
23
32
|
|
|
24
33
|
const modifiers = [];
|
|
25
34
|
const { modifiers: itemModifiers = [] } = item;
|
|
26
|
-
|
|
35
|
+
|
|
36
|
+
const validatedModifiers = itemModifiers.map(each =>
|
|
37
|
+
modifierActions.validate(each, {
|
|
38
|
+
item,
|
|
39
|
+
startRequestDate,
|
|
40
|
+
endRequestDate,
|
|
41
|
+
})
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const amountOverride = validatedModifiers.find(each =>
|
|
27
45
|
modifierActions.isAmountOverride(each)
|
|
28
46
|
);
|
|
29
47
|
|
|
30
|
-
const modifiersToCompute =
|
|
48
|
+
const modifiersToCompute = validatedModifiers.filter(
|
|
31
49
|
each =>
|
|
32
|
-
!modifierActions.isOverride(each) &&
|
|
50
|
+
!modifierActions.isOverride(each) &&
|
|
51
|
+
each.compute &&
|
|
52
|
+
each.compute.type &&
|
|
53
|
+
modifierActions.isValid(each)
|
|
33
54
|
);
|
|
55
|
+
|
|
56
|
+
const paymentModifiersToCompute = validatedModifiers
|
|
57
|
+
.filter(
|
|
58
|
+
modifier =>
|
|
59
|
+
modifierActions.isPaymentModifier(modifier) &&
|
|
60
|
+
modifierActions.hasPaymentMethodType({
|
|
61
|
+
paymentModifier: modifier,
|
|
62
|
+
paymentMethod,
|
|
63
|
+
paymentType,
|
|
64
|
+
})
|
|
65
|
+
)
|
|
66
|
+
.filter(paymentModifier => {
|
|
67
|
+
const childModifiers = modifierActions.getChildren({
|
|
68
|
+
parentModifier: paymentModifier,
|
|
69
|
+
modifiers: modifiersToCompute,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
!modifierActions.isFixed(paymentModifier) ||
|
|
74
|
+
childModifiers.length === 0
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
34
78
|
if (amountOverride) modifiersToCompute.push(amountOverride);
|
|
35
79
|
|
|
36
|
-
const modifiersToNotCompute =
|
|
80
|
+
const modifiersToNotCompute = validatedModifiers
|
|
37
81
|
.filter(
|
|
38
82
|
each =>
|
|
83
|
+
modifierActions.isPaymentModifier(each) ||
|
|
39
84
|
!modifiersToCompute.find(
|
|
40
85
|
ceach => ceach.modifierId === each.modifierId
|
|
41
86
|
)
|
|
42
87
|
)
|
|
43
88
|
.map(each => modifierActions.calculate(each, { skip: true }));
|
|
44
89
|
|
|
45
|
-
if (modifiersToCompute.length) {
|
|
90
|
+
if (modifiersToCompute.length || paymentModifiersToCompute.length) {
|
|
46
91
|
// sort modifiers based on sort
|
|
47
|
-
const sortedModifiers = modifierActions.sort(
|
|
92
|
+
const sortedModifiers = modifierActions.sort([
|
|
93
|
+
...modifiersToCompute,
|
|
94
|
+
...paymentModifiersToCompute,
|
|
95
|
+
]);
|
|
96
|
+
|
|
48
97
|
let computedPrice = price;
|
|
49
98
|
let prvPrice = 0;
|
|
50
99
|
let prvSort;
|
|
@@ -60,12 +109,37 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
60
109
|
prvSort = null;
|
|
61
110
|
}
|
|
62
111
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
112
|
+
let _modifier = modifier;
|
|
113
|
+
|
|
114
|
+
if (modifierActions.isPaymentModifier(modifier)) {
|
|
115
|
+
_modifier = modifierActions.calculatePaymentModifier({
|
|
116
|
+
paymentModifier: modifier,
|
|
117
|
+
amountToPay,
|
|
118
|
+
itemBalance:
|
|
119
|
+
typeof item.totalPaid === 'number' && item.totalPaid > 0
|
|
120
|
+
? actions.getItemsBalance({ items: [item] })
|
|
121
|
+
: math.sub(computedPrice, item.totalPaid),
|
|
122
|
+
paymentMethod,
|
|
123
|
+
paymentType,
|
|
124
|
+
paymentId,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
_modifier = modifierActions.calculate(
|
|
129
|
+
{ ..._modifier },
|
|
130
|
+
{
|
|
131
|
+
price: computedPrice,
|
|
132
|
+
quantity,
|
|
133
|
+
}
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
if (
|
|
137
|
+
!modifierActions.isPaymentModifier(modifier) ||
|
|
138
|
+
lockPaymentModifiers
|
|
139
|
+
) {
|
|
140
|
+
modifiers.push(_modifier);
|
|
141
|
+
}
|
|
67
142
|
|
|
68
|
-
modifiers.push(_modifier);
|
|
69
143
|
const { type, _computed } = _modifier;
|
|
70
144
|
|
|
71
145
|
const computedAmountCalc = math.mul(_computed.amount, quantity);
|
|
@@ -113,10 +187,11 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
113
187
|
};
|
|
114
188
|
|
|
115
189
|
//
|
|
116
|
-
const calculateMany = (items = []
|
|
190
|
+
const calculateMany = (items = [], opts = null) =>
|
|
191
|
+
items.map(item => calculateOne(item, opts));
|
|
117
192
|
|
|
118
|
-
return function calculate(item) {
|
|
119
|
-
if (Array.isArray(item)) return calculateMany(item);
|
|
120
|
-
return calculateOne(item);
|
|
193
|
+
return function calculate(item, opts) {
|
|
194
|
+
if (Array.isArray(item)) return calculateMany(item, opts);
|
|
195
|
+
return calculateOne(item, opts);
|
|
121
196
|
};
|
|
122
197
|
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module.exports = ({ utils }) =>
|
|
2
|
+
function getAmounts(item, amount) {
|
|
3
|
+
const { status = {}, total, totalPaid } = item;
|
|
4
|
+
const totalDue = utils.math.sub(total, totalPaid);
|
|
5
|
+
const remainingAmount = utils.math.sub(amount, totalDue);
|
|
6
|
+
|
|
7
|
+
if (status && status.paid && status.paid.value)
|
|
8
|
+
return { amountToAssign: 0, remainingAmount: amount, isPaid: true };
|
|
9
|
+
|
|
10
|
+
if (amount >= totalDue)
|
|
11
|
+
return { amountToAssign: totalDue, remainingAmount, isPaid: true };
|
|
12
|
+
|
|
13
|
+
return { amountToAssign: amount, remainingAmount: 0, isPaid: false };
|
|
14
|
+
};
|
package/lib/item/getBalance.js
CHANGED
|
@@ -7,6 +7,7 @@ module.exports = ({ modifierActions, _, settings }) =>
|
|
|
7
7
|
.filter(
|
|
8
8
|
each =>
|
|
9
9
|
!modifierActions.isHidden(each) &&
|
|
10
|
+
modifierActions.isValid(each) &&
|
|
10
11
|
!modifierActions.isAmountOverride(each) &&
|
|
11
12
|
!modifierActions.isDepartment(each) &&
|
|
12
13
|
modifierActions.isDirect(each)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/* eslint-disable no-restricted-syntax */
|
|
2
|
+
|
|
3
|
+
module.exports = ({ utils }) => {
|
|
4
|
+
const { math } = utils;
|
|
5
|
+
return function getItemsTotals(items) {
|
|
6
|
+
const subTotals = {};
|
|
7
|
+
let subTotal = 0;
|
|
8
|
+
let total = 0;
|
|
9
|
+
|
|
10
|
+
for (const item of items) {
|
|
11
|
+
subTotal = math.add(subTotal, item.subTotals._actual);
|
|
12
|
+
const keys = Object.keys(item.subTotals).filter(
|
|
13
|
+
key => !key.includes('_')
|
|
14
|
+
);
|
|
15
|
+
for (const key of keys) {
|
|
16
|
+
subTotals[key] = math.add(subTotals[key], item.subTotals[key]);
|
|
17
|
+
}
|
|
18
|
+
total = math.add(total, item.total);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
subTotals,
|
|
23
|
+
subTotal,
|
|
24
|
+
total,
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
};
|
package/lib/item/getTotal.js
CHANGED
|
@@ -2,6 +2,8 @@ module.exports = ({ utils }) => {
|
|
|
2
2
|
const { math } = utils;
|
|
3
3
|
return function getTotal(item) {
|
|
4
4
|
if (!item || !item.subTotals) return 0;
|
|
5
|
-
return math.
|
|
5
|
+
return math.toDecimalPlaces(
|
|
6
|
+
math.add(item.subTotals._actual, item.subTotals._xincluded)
|
|
7
|
+
);
|
|
6
8
|
};
|
|
7
9
|
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module.exports = ({ modifierActions }) =>
|
|
2
|
+
function hasPaymentMethodType({ item, paymentMethod, paymentType }) {
|
|
3
|
+
if (
|
|
4
|
+
!item ||
|
|
5
|
+
!Array.isArray(item.modifiers) ||
|
|
6
|
+
(!paymentMethod && !paymentType)
|
|
7
|
+
)
|
|
8
|
+
return false;
|
|
9
|
+
|
|
10
|
+
return item.modifiers.some(modifier =>
|
|
11
|
+
modifierActions.hasPaymentMethodType({
|
|
12
|
+
paymentModifier: modifier,
|
|
13
|
+
paymentMethod,
|
|
14
|
+
paymentType,
|
|
15
|
+
})
|
|
16
|
+
);
|
|
17
|
+
};
|