@darkpos/pricing 1.0.15 → 1.0.17

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.
Files changed (66) hide show
  1. package/__TEST__/mocks/order-credit.json +474 -0
  2. package/__TEST__/mocks/partially-paid/input-items.json +1 -0
  3. package/__TEST__/mocks/partially-paid/order-modifiers.json +53 -0
  4. package/__TEST__/mocks/partially-paid/order-partially-paid.json +890 -0
  5. package/__TEST__/mocks/scripts/calculate-partially-paid/index.js +27 -0
  6. package/__TEST__/mocks/scripts/calculate-unpaid/index.js +28 -0
  7. package/__TEST__/mocks/scripts/order-to-string.js +18 -0
  8. package/__TEST__/mocks/stores.js +435 -0
  9. package/__TEST__/mocks/unpaid/input-items.json +6 -0
  10. package/__TEST__/mocks/unpaid/order-modifiers.json +53 -0
  11. package/__TEST__/mocks/unpaid/order-not-paid.json +684 -0
  12. package/__TEST__/order/order.test.js +627 -0
  13. package/lib/item/addIndirectModifier.js +2 -3
  14. package/lib/item/calculate.js +1 -2
  15. package/lib/item/getBalance.js +9 -0
  16. package/lib/item/getItemModifiers.js +3 -14
  17. package/lib/item/getItems.js +5 -0
  18. package/lib/item/getTotal.js +1 -1
  19. package/lib/item/getTotalPrice.js +10 -0
  20. package/lib/item/getTotals.js +31 -18
  21. package/lib/item/index.js +12 -0
  22. package/lib/item/isFullyPaid.js +6 -0
  23. package/lib/item/markModifiersAsLocked.js +11 -0
  24. package/lib/item/removeModifier.js +10 -9
  25. package/lib/item/removePaymentModifiers.js +15 -0
  26. package/lib/modifier/create.js +2 -2
  27. package/lib/modifier/createAmountOverrideModifier.js +1 -3
  28. package/lib/modifier/createCreditModifier.js +2 -4
  29. package/lib/modifier/createDiscountModifier.js +1 -3
  30. package/lib/modifier/createIndirectModifier.js +14 -5
  31. package/lib/modifier/createPaymentModifier.js +12 -0
  32. package/lib/modifier/createSubscriptionModifier.js +1 -3
  33. package/lib/modifier/duplicate.js +1 -1
  34. package/lib/modifier/findById.js +4 -1
  35. package/lib/modifier/findByPaymentMethod.js +1 -1
  36. package/lib/modifier/findByPaymentType.js +1 -1
  37. package/lib/modifier/getItemModifiers.js +1 -3
  38. package/lib/modifier/getLockedModifiers.js +5 -0
  39. package/lib/modifier/getSplittedModifiers.js +4 -6
  40. package/lib/modifier/hasItems.js +8 -0
  41. package/lib/modifier/index.js +20 -4
  42. package/lib/modifier/isFixed.js +10 -0
  43. package/lib/modifier/isFixedDiscount.js +4 -0
  44. package/lib/modifier/isPaymentMethodModifier.js +8 -0
  45. package/lib/modifier/isPaymentModifier.js +7 -0
  46. package/lib/modifier/isPaymentTypeModifier.js +8 -0
  47. package/lib/modifier/isSubtract.js +10 -0
  48. package/lib/modifier/removeLocked.js +8 -0
  49. package/lib/order/addItem.js +104 -106
  50. package/lib/order/addItemModifier.js +0 -3
  51. package/lib/order/addModifier.js +14 -2
  52. package/lib/order/addModifiers.js +15 -0
  53. package/lib/order/autoSplit.js +34 -0
  54. package/lib/order/calculate.js +46 -68
  55. package/lib/order/getScheduleByCustomer.js +0 -1
  56. package/lib/order/index.js +10 -0
  57. package/lib/order/markModifiersAsLocked.js +14 -0
  58. package/lib/order/removeItem.js +0 -2
  59. package/lib/order/removeModifier.js +5 -1
  60. package/lib/order/removeModifiers.js +18 -0
  61. package/lib/order/splitByDepartments.js +10 -13
  62. package/lib/order/syncSubOrderItemsFromParent.js +24 -0
  63. package/package.json +3 -2
  64. package/__TEST__/order.test.js +0 -233
  65. package/lib/modifier/isPaymentMethods.js +0 -8
  66. package/lib/modifier/isPaymentTypes.js +0 -8
@@ -0,0 +1,627 @@
1
+ const usePricing = require('../../index');
2
+ const mockStores = require('../mocks/stores');
3
+
4
+ const orderNotPaid = require('../mocks/unpaid/order-not-paid.json');
5
+ const orderModifiersNotPaid = require('../mocks/unpaid/order-modifiers.json');
6
+ const inputItemsNotPaidOrder = require('../mocks/unpaid/input-items.json');
7
+
8
+ const orderPartiallyPaid = require('../mocks/partially-paid/order-partially-paid.json');
9
+ const orderModifiersPartiallyPaid = require('../mocks/partially-paid/order-modifiers.json');
10
+ const inputItemsPartiallyPaidOrder = require('../mocks/partially-paid/input-items.json');
11
+
12
+ const orderCredit = require('../mocks/order-credit.json');
13
+
14
+ const session = {
15
+ store: mockStores[0],
16
+ };
17
+
18
+ const pricingService = usePricing(session);
19
+
20
+ describe('Order actions', () => {
21
+ test('Get calculated Order, one item', () => {
22
+ const modifiers = [
23
+ {
24
+ compute: {
25
+ amount: 20,
26
+ type: 'percentage',
27
+ action: 'add',
28
+ },
29
+ name: 'modifier1',
30
+ type: 'discount',
31
+ },
32
+ {
33
+ compute: {
34
+ amount: 20,
35
+ type: 'percentage',
36
+ action: 'subtract',
37
+ },
38
+ name: 'modifier2',
39
+ type: 'discount',
40
+ },
41
+ {
42
+ compute: { amount: 2, type: 'fixed', action: 'subtract' },
43
+ name: 'modifier3',
44
+ type: 'discount',
45
+ },
46
+ ];
47
+ const orderItem = { price: 30, quantity: 2, modifiers };
48
+ const order = { items: [orderItem] };
49
+ const newOrder = pricingService.order.calculate(order);
50
+
51
+ expect(newOrder).toHaveProperty('total', 56);
52
+ expect(newOrder).toHaveProperty('subTotal', 60);
53
+ expect(newOrder).toHaveProperty('subTotals', {
54
+ discount: -4,
55
+ });
56
+ });
57
+ test('Get calculated Order, multiple items', () => {
58
+ const item1 = {
59
+ _id: 1,
60
+ price: 30,
61
+ quantity: 2,
62
+ modifiers: [
63
+ {
64
+ compute: {
65
+ amount: 2,
66
+ type: 'fixed',
67
+ action: 'subtract',
68
+ },
69
+ name: 'modifier1',
70
+ type: 'discount',
71
+ },
72
+ ],
73
+ };
74
+ const item2 = {
75
+ _id: 2,
76
+ price: 10,
77
+ quantity: 1,
78
+ modifiers: [
79
+ {
80
+ compute: {
81
+ amount: 2,
82
+ type: 'fixed',
83
+ action: 'subtract',
84
+ },
85
+ name: 'modifier1',
86
+ type: 'discount',
87
+ },
88
+ ],
89
+ };
90
+ const order = { items: [item1, item2] };
91
+ const newOrder = pricingService.order.calculate(order);
92
+ expect(newOrder).toHaveProperty('total', 64);
93
+ expect(newOrder).toHaveProperty('subTotal', 70);
94
+ expect(newOrder).toHaveProperty('subTotals', {
95
+ discount: -6,
96
+ });
97
+ // console.log(JSON.stringify(newOrder, 0, 2));
98
+ });
99
+ test('Get calculated Order, multiple items and indirect modifiers', () => {
100
+ const item1 = {
101
+ _id: 1,
102
+ price: 30,
103
+ quantity: 2,
104
+ };
105
+ const item2 = {
106
+ _id: 2,
107
+ price: 10,
108
+ quantity: 1,
109
+ };
110
+ const modifier1 = {
111
+ compute: {
112
+ amount: 2,
113
+ type: 'fixed',
114
+ action: 'subtract',
115
+ },
116
+ name: 'modifier1',
117
+ type: 'discount',
118
+ };
119
+
120
+ const order = { items: [item1, item2], modifiers: [modifier1] };
121
+
122
+ const newOrder = pricingService.order.calculate(order);
123
+ expect(newOrder).toHaveProperty('total', 68);
124
+ expect(newOrder).toHaveProperty('subTotal', 70);
125
+ expect(newOrder).toHaveProperty('subTotals', {
126
+ discount: -2,
127
+ });
128
+ expect(newOrder.items[0]).toHaveProperty('total', 58.28);
129
+ expect(newOrder.items[0]).toHaveProperty('subTotals', {
130
+ discount: -1.72,
131
+ _included: 0,
132
+ _xincluded: -1.72,
133
+ _direct: 0,
134
+ _xdirect: -1.72,
135
+ _simple: 60,
136
+ _actual: 60,
137
+ });
138
+ expect(newOrder.items[1]).toHaveProperty('total', 9.72);
139
+ expect(newOrder.items[1]).toHaveProperty('subTotals', {
140
+ discount: -0.28,
141
+ _included: 0,
142
+ _xincluded: -0.28,
143
+ _direct: 0,
144
+ _xdirect: -0.28,
145
+ _simple: 10,
146
+ _actual: 10,
147
+ });
148
+ });
149
+ test('Get calculated Order, multiple items and indirect modifiers #2', () => {
150
+ const item1 = {
151
+ _id: 1,
152
+ price: 15.99,
153
+ quantity: 1,
154
+ };
155
+ const item2 = {
156
+ _id: 2,
157
+ price: 4.65,
158
+ quantity: 1,
159
+ };
160
+ const modifier1 = {
161
+ compute: {
162
+ amount: 10,
163
+ action: 'subtract',
164
+ type: 'percentage',
165
+ },
166
+ name: 'modifier1',
167
+ type: 'discount',
168
+ };
169
+
170
+ const order = { items: [item1, item2], modifiers: [modifier1] };
171
+ const newOrder = pricingService.order.calculate(order);
172
+ expect(newOrder).toHaveProperty('total', 18.58);
173
+ expect(newOrder).toHaveProperty('subTotal', 20.64);
174
+ expect(newOrder).toHaveProperty('subTotals', {
175
+ discount: -2.06,
176
+ });
177
+ expect(newOrder.items[0]).toHaveProperty('total', 14.39);
178
+ expect(newOrder.items[0]).toHaveProperty('subTotals', {
179
+ discount: -1.6,
180
+ _included: 0,
181
+ _xincluded: -1.6,
182
+ _direct: 0,
183
+ _xdirect: -1.6,
184
+ _simple: 15.99,
185
+ _actual: 15.99,
186
+ });
187
+ expect(newOrder.items[1]).toHaveProperty('total', 4.19);
188
+ expect(newOrder.items[1]).toHaveProperty('subTotals', {
189
+ discount: -0.46,
190
+ _included: 0,
191
+ _xincluded: -0.46,
192
+ _direct: 0,
193
+ _xdirect: -0.46,
194
+ _simple: 4.65,
195
+ _actual: 4.65,
196
+ });
197
+ });
198
+ test('Get calculated Order, multiple items and indirect modifiers #3', () => {
199
+ const item1 = {
200
+ _id: 1,
201
+ price: 3,
202
+ quantity: 1,
203
+ };
204
+ const item2 = {
205
+ _id: 2,
206
+ price: 3,
207
+ quantity: 1,
208
+ };
209
+ const item3 = {
210
+ _id: 3,
211
+ price: 4,
212
+ quantity: 1,
213
+ };
214
+ const modifier1 = {
215
+ _id: 1,
216
+ compute: {
217
+ amount: 10,
218
+ type: 'percentage',
219
+ action: 'subtract',
220
+ },
221
+ name: `10% Discount`,
222
+ type: 'discount',
223
+ };
224
+
225
+ const modifier2 = {
226
+ _id: 2,
227
+ name: `20% tax`,
228
+ type: 'tax',
229
+ compute: {
230
+ amount: 20,
231
+ type: 'percentage',
232
+ action: 'add',
233
+ },
234
+ };
235
+
236
+ const order = {
237
+ items: [item1, item2, item3],
238
+ modifiers: [modifier1, modifier2],
239
+ };
240
+ const newOrder = pricingService.order.calculate(order);
241
+ expect(newOrder).toHaveProperty('total', 11);
242
+ expect(newOrder).toHaveProperty('subTotal', 10);
243
+ expect(newOrder).toHaveProperty('subTotals', {
244
+ discount: -1,
245
+ tax: 2,
246
+ });
247
+ });
248
+
249
+ test('CU-3und87h : Calculate order with 20 USD credit', () => {
250
+ const newOrder = pricingService.order.calculate(orderCredit);
251
+
252
+ const [item1, item2, item3] = newOrder.items;
253
+ expect(newOrder).toHaveProperty('total', 12.5);
254
+ expect(item1).toHaveProperty('total', 3.64);
255
+ expect(item1.modifiers[1].compute).toHaveProperty('amount', 5.86);
256
+
257
+ expect(item2).toHaveProperty('total', 5.39);
258
+ expect(item2.modifiers[1].compute).toHaveProperty('amount', 8.61);
259
+
260
+ expect(item3).toHaveProperty('total', 3.47);
261
+ expect(item3.modifiers[1].compute).toHaveProperty('amount', 5.53);
262
+ });
263
+
264
+ test('Auto split a Parent Order', () => {
265
+ const parentOrder = {
266
+ _id: '6392069cb6c81965a442289d',
267
+ displayId: '1',
268
+ parentId: null,
269
+ isParent: false,
270
+ subTotal: 15,
271
+ tax: 0,
272
+ discount: 0,
273
+ total: 15,
274
+ description: '',
275
+ type: '',
276
+ fee: 0,
277
+ location: {},
278
+ status: {
279
+ order: 'open',
280
+ kitchen: 'open',
281
+ delivery: 'none',
282
+ detailed: true,
283
+ },
284
+ attributes: [],
285
+ properties: {
286
+ pieces: [],
287
+ notify: {
288
+ sms: false,
289
+ },
290
+ print: {},
291
+ split: 'auto',
292
+ },
293
+ subTotals: {},
294
+ user: {
295
+ _id: '5a09fd650cd56e741f78aba6',
296
+ },
297
+ items: [
298
+ {
299
+ _id: '639206f1b6c81965a44228ba',
300
+ itemId: '62cdbfd01ee1b40019328252',
301
+ name: 'Box Shirt - Fold',
302
+ description: '',
303
+ modifiersTotalAmount: 0,
304
+ location: {},
305
+ price: 2,
306
+ pieces: 1,
307
+ quantity: 1,
308
+ path: ['menuCategories', 8],
309
+ menuId: '62cdbfd0aaf93c00193d2efe',
310
+ serial: null,
311
+ sku: null,
312
+ total: 2,
313
+ status: {
314
+ picked: {
315
+ value: false,
316
+ date: '',
317
+ },
318
+ paid: {
319
+ value: false,
320
+ date: '',
321
+ },
322
+ tracker: [],
323
+ },
324
+ subTotals: {
325
+ _included: 0,
326
+ _xincluded: 0,
327
+ _direct: 0,
328
+ _xdirect: 0,
329
+ _simple: 2,
330
+ _actual: 2,
331
+ },
332
+ properties: false,
333
+ modifiers: [
334
+ {
335
+ _id: '639206f1b6c81965a44228bc',
336
+ modifierId: '62cdbfd01ee1b4001932816c',
337
+ name: 'Laundry',
338
+ sku: '',
339
+ description: '',
340
+ group: '',
341
+ type: null,
342
+ attributes: ['department'],
343
+ color: '',
344
+ backgroundColor: null,
345
+ icon: null,
346
+ url: '',
347
+ tags: [],
348
+ order: 2,
349
+ included: false,
350
+ direct: true,
351
+ hidden: true,
352
+ print: false,
353
+ required: true,
354
+ recommended: false,
355
+ default: false,
356
+ code: 'L',
357
+ properties: {
358
+ department: {
359
+ tagSize: 'Small',
360
+ customerTagsExtend: null,
361
+ maxItems: 4,
362
+ },
363
+ },
364
+ _computed: {
365
+ amount: 0,
366
+ description: 'Laundry ($0.00)',
367
+ },
368
+ addModifiers: [],
369
+ delModifiers: [],
370
+ conditions: null,
371
+ compute: null,
372
+ },
373
+ ],
374
+ notes: [],
375
+ },
376
+ {
377
+ _id: '639206efb6c81965a44228b7',
378
+ itemId: '62cdbfd01ee1b40019328284',
379
+ name: 'Pants',
380
+ description: '',
381
+ modifiersTotalAmount: 0,
382
+ location: {},
383
+ price: 4,
384
+ pieces: 1,
385
+ quantity: 1,
386
+ path: ['menuCategories', 11],
387
+ menuId: '62cdbfd0aaf93c00193d2efe',
388
+ serial: null,
389
+ sku: null,
390
+ total: 4,
391
+ status: {
392
+ picked: {
393
+ value: false,
394
+ date: '',
395
+ },
396
+ paid: {
397
+ value: false,
398
+ date: '',
399
+ },
400
+ tracker: [],
401
+ },
402
+ subTotals: {
403
+ _included: 0,
404
+ _xincluded: 0,
405
+ _direct: 0,
406
+ _xdirect: 0,
407
+ _simple: 4,
408
+ _actual: 4,
409
+ },
410
+ properties: false,
411
+ modifiers: [
412
+ {
413
+ _id: '639206efb6c81965a44228b9',
414
+ modifierId: '62cdbfd01ee1b4001932816d',
415
+ name: 'Dry Cleaning',
416
+ sku: '',
417
+ description: '',
418
+ group: '',
419
+ type: null,
420
+ attributes: ['department'],
421
+ color: '',
422
+ backgroundColor: null,
423
+ icon: null,
424
+ url: '',
425
+ tags: null,
426
+ order: 1,
427
+ included: false,
428
+ direct: true,
429
+ hidden: true,
430
+ print: false,
431
+ required: true,
432
+ recommended: false,
433
+ default: false,
434
+ code: 'D',
435
+ properties: {
436
+ department: {
437
+ tagSize: 'Small',
438
+ customerTagsExtend: null,
439
+ maxItems: 4,
440
+ },
441
+ },
442
+ _computed: {
443
+ amount: 0,
444
+ description: 'Dry Cleaning ($0.00)',
445
+ },
446
+ addModifiers: [],
447
+ delModifiers: [],
448
+ conditions: null,
449
+ compute: null,
450
+ },
451
+ ],
452
+ notes: [],
453
+ },
454
+ {
455
+ _id: '639206edb6c81965a44228b4',
456
+ itemId: '62cdbfd01ee1b400193281f1',
457
+ name: 'M-L 2pc Suit',
458
+ description: null,
459
+ modifiersTotalAmount: 0,
460
+ location: {},
461
+ price: 9,
462
+ pieces: 2,
463
+ quantity: 1,
464
+ path: ['menuCategories', 0],
465
+ menuId: '62cdbfd0aaf93c00193d2efe',
466
+ serial: null,
467
+ sku: null,
468
+ total: 9,
469
+ status: {
470
+ picked: {
471
+ value: false,
472
+ date: '',
473
+ },
474
+ paid: {
475
+ value: false,
476
+ date: '',
477
+ },
478
+ tracker: [],
479
+ },
480
+ subTotals: {
481
+ _included: 0,
482
+ _xincluded: 0,
483
+ _direct: 0,
484
+ _xdirect: 0,
485
+ _simple: 9,
486
+ _actual: 9,
487
+ },
488
+ properties: false,
489
+ modifiers: [
490
+ {
491
+ _id: '639206edb6c81965a44228b6',
492
+ modifierId: '62cdbfd01ee1b4001932816d',
493
+ name: 'Dry Cleaning',
494
+ sku: '',
495
+ description: '',
496
+ group: '',
497
+ type: null,
498
+ attributes: ['department'],
499
+ color: '',
500
+ backgroundColor: null,
501
+ icon: null,
502
+ url: '',
503
+ tags: null,
504
+ order: 1,
505
+ included: false,
506
+ direct: true,
507
+ hidden: true,
508
+ print: false,
509
+ required: true,
510
+ recommended: false,
511
+ default: false,
512
+ code: 'D',
513
+ properties: {
514
+ department: {
515
+ tagSize: 'Small',
516
+ customerTagsExtend: null,
517
+ maxItems: 4,
518
+ },
519
+ },
520
+ _computed: {
521
+ amount: 0,
522
+ description: 'Dry Cleaning ($0.00)',
523
+ },
524
+ addModifiers: [],
525
+ delModifiers: [],
526
+ conditions: null,
527
+ compute: null,
528
+ },
529
+ ],
530
+ notes: [],
531
+ },
532
+ ],
533
+ notes: [],
534
+ start: {
535
+ actualDate: '2022-12-08T15:45:27.838Z',
536
+ requestDate: '2022-12-08T15:45:27.838Z',
537
+ location: null,
538
+ },
539
+ end: {
540
+ actualDate: '',
541
+ requestDate: '2022-12-12T22:00:00.000Z',
542
+ location: {
543
+ name: 'Location Name',
544
+ locationType: 'store',
545
+ storeId: '59c042222a985ae70278619f',
546
+ },
547
+ },
548
+ customer: {
549
+ _id: '5c73d50b213453368dd30422',
550
+ },
551
+ modifiers: [],
552
+ };
553
+
554
+ const subOrders = pricingService.order
555
+ .autoSplit({ parentOrder })
556
+ .sort((a, b) => {
557
+ if (a.displayId < b.displayId) return -1;
558
+ if (a.displayId > b.displayId) return 1;
559
+ return 0;
560
+ });
561
+
562
+ expect(subOrders[0].displayId).toBe('1-1');
563
+ expect(subOrders[0].items.map(item => item.name)).toEqual([
564
+ 'Box Shirt - Fold',
565
+ ]);
566
+ expect(subOrders[1].displayId).toBe('1-2');
567
+ expect(subOrders[1].items.map(item => item.name)).toEqual([
568
+ 'Pants',
569
+ 'M-L 2pc Suit',
570
+ ]);
571
+ });
572
+ test('Get calculated UNPAID Order, multiple items and paymentModifiers', () => {
573
+ const paymentModifiers = orderModifiersNotPaid.map(orderMod =>
574
+ pricingService.modifier.createPaymentModifier({
575
+ modifier: orderMod,
576
+ items: inputItemsNotPaidOrder,
577
+ })
578
+ );
579
+
580
+ let order = { ...orderNotPaid };
581
+
582
+ order = pricingService.order.addModifiers({
583
+ modifiers: paymentModifiers,
584
+ order,
585
+ });
586
+
587
+ const newOrder = pricingService.order.calculate(order);
588
+
589
+ expect(newOrder).toHaveProperty('total', 34.45);
590
+ expect(newOrder).toHaveProperty('subTotal', 42.45);
591
+ expect(newOrder).toHaveProperty('subTotals', {
592
+ discount: -10,
593
+ fee: 2,
594
+ });
595
+ expect(newOrder.items[0]).toHaveProperty('total', 8.08);
596
+ expect(newOrder.items[1]).toHaveProperty('total', 7.71);
597
+ expect(newOrder.items[2]).toHaveProperty('total', 11.36);
598
+ expect(newOrder.items[3]).toHaveProperty('total', 7.3);
599
+ });
600
+ test('Get calculated PARTIALLY PAID Order, multiple items and paymentModifiers', () => {
601
+ const paymentModifiers = orderModifiersPartiallyPaid.map(orderMod =>
602
+ pricingService.modifier.createPaymentModifier({
603
+ modifier: orderMod,
604
+ items: inputItemsPartiallyPaidOrder,
605
+ })
606
+ );
607
+
608
+ let order = { ...orderPartiallyPaid };
609
+
610
+ order = pricingService.order.addModifiers({
611
+ modifiers: paymentModifiers,
612
+ order,
613
+ });
614
+ const newOrder = pricingService.order.calculate(order);
615
+
616
+ expect(newOrder).toHaveProperty('total', 36.45);
617
+ expect(newOrder).toHaveProperty('subTotal', 42.45);
618
+ expect(newOrder).toHaveProperty('subTotals', {
619
+ discount: -10,
620
+ fee: 4,
621
+ });
622
+ expect(newOrder.items[0]).toHaveProperty('total', 8.07);
623
+ expect(newOrder.items[1]).toHaveProperty('total', 7.71);
624
+ expect(newOrder.items[2]).toHaveProperty('total', 11.37);
625
+ expect(newOrder.items[3]).toHaveProperty('total', 9.3);
626
+ });
627
+ });
@@ -1,14 +1,13 @@
1
1
  module.exports = ({ utils, modifierActions }) => {
2
2
  const { math } = utils;
3
3
 
4
- return function addIndirectModifier(orderTotal, items, modifier) {
4
+ return function addIndirectModifier({ orderTotal, items, modifier }) {
5
5
  const itemsModifiers = {};
6
6
  // get distributed price in order level
7
7
  const { _computed } = modifierActions.calculate(modifier, {
8
8
  price: orderTotal,
9
9
  quantity: 1,
10
10
  });
11
-
12
11
  const orderModifierTotal = math.abs(_computed.amount);
13
12
  const totalDistributed = items.reduce((acc, item) => {
14
13
  const inheritedModifier = modifierActions.createIndirectModifier(
@@ -29,7 +28,6 @@ module.exports = ({ utils, modifierActions }) => {
29
28
  // check validation
30
29
  if (math.abs(totalDistributed) !== math.abs(orderModifierTotal)) {
31
30
  const difference = math.sub(orderModifierTotal, totalDistributed);
32
- // console.log('difference => ', difference);
33
31
  const selectedItem = items.find(
34
32
  item => math.sub(item.total, difference) > 0
35
33
  );
@@ -51,6 +49,7 @@ module.exports = ({ utils, modifierActions }) => {
51
49
  },
52
50
  };
53
51
  }
52
+
54
53
  return itemsModifiers;
55
54
  }
56
55
 
@@ -66,7 +66,6 @@ module.exports = ({ _, utils, modifierActions }) => {
66
66
 
67
67
  modifiers.push(_modifier);
68
68
  const { type, _computed } = _modifier;
69
- // console.log('computedPrice => ', _computed);
70
69
  let computedAmount = modifierActions.isIgnoreQuantity(_modifier)
71
70
  ? _computed.amount
72
71
  : math.mul(_computed.amount, quantity);
@@ -89,7 +88,7 @@ module.exports = ({ _, utils, modifierActions }) => {
89
88
  }
90
89
 
91
90
  subTotals._actual = math.add(subTotals._simple, subTotals._included);
92
- // console.log('subTotals =>', subTotals);
91
+
93
92
  const total = math.add(subTotals._actual, subTotals._xincluded);
94
93
 
95
94
  return {
@@ -0,0 +1,9 @@
1
+ module.exports = ({ utils }) => {
2
+ const { math } = utils;
3
+ return function getBalance({ item }) {
4
+ if (!item) return 0;
5
+ if (Array.isArray(item)) return 0;
6
+
7
+ return math.sub(item.total, item.totalPaid);
8
+ };
9
+ };
@@ -1,16 +1,5 @@
1
- module.exports = ({ modifierActions }) =>
2
- function getItemModifiers(item, orderModifiers) {
3
- const existsInOrderModifier = modifier =>
4
- orderModifiers &&
5
- orderModifiers.find(each => each.modifierId === modifier.modifierId);
1
+ module.exports = () =>
2
+ function getItemModifiers(item) {
6
3
 
7
- let modifiers = [];
8
- if (!item || !Array.isArray(item.modifiers)) return modifiers;
9
-
10
- modifiers.push(...item.modifiers);
11
-
12
- if (Array.isArray(orderModifiers) && orderModifiers.length)
13
- modifiers = modifiers.filter(each => !existsInOrderModifier(each));
14
-
15
- return modifiers.filter(modifierActions.isDirect);
4
+ return item.modifiers;
16
5
  };
@@ -0,0 +1,5 @@
1
+ module.exports = () =>
2
+ function getItems(items, itemsToPay) {
3
+ if (!Array.isArray(itemsToPay)) return items;
4
+ return items.filter(item => itemsToPay.includes(item._id));
5
+ };