@darkpos/pricing 1.0.46 → 1.0.48
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.test.js +254 -0
- package/__TEST__/order/addItem.test.js +12 -4
- package/__TEST__/order/order.test.js +54 -8
- package/lib/item/calculate.js +1 -1
- package/lib/item/getItemsBalance.js +0 -1
- package/lib/item/getPriceWithoutModifiers.js +28 -0
- package/lib/item/getTotalNeareastDifference.js +16 -0
- package/lib/item/getTotalsDifference.js +14 -0
- package/lib/item/index.js +6 -0
- package/lib/item/isFullyPaid.js +1 -1
- package/lib/modifier/getPriceWithoutModifier.js +38 -0
- package/lib/modifier/index.js +4 -0
- package/lib/modifier/isExtractCalculatedValue.js +5 -0
- package/lib/order/addItem.js +23 -11
- package/lib/order/addItemModifier.js +1 -1
- package/lib/order/calculate.js +26 -15
- package/lib/order/updateItemQuantity.js +2 -1
- package/lib/store/index.js +2 -0
- package/lib/store/isNeareastMultiple.js +6 -0
- package/package.json +4 -3
package/__TEST__/item.test.js
CHANGED
|
@@ -282,4 +282,258 @@ describe('Item actions', () => {
|
|
|
282
282
|
_xincluded: 0,
|
|
283
283
|
});
|
|
284
284
|
});
|
|
285
|
+
|
|
286
|
+
test('CU-86duwyvrr: Calculate item with modifier.extractCalculatedvalue=true percentageDiscount16', () => {
|
|
287
|
+
const percentageDiscount16 = {
|
|
288
|
+
compute: {
|
|
289
|
+
amount: 16,
|
|
290
|
+
type: 'percentage',
|
|
291
|
+
action: 'subtract',
|
|
292
|
+
},
|
|
293
|
+
properties: {
|
|
294
|
+
extractCalculatedValue: true,
|
|
295
|
+
},
|
|
296
|
+
name: 'percentageDiscount16',
|
|
297
|
+
type: 'discount',
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const orderItem = {
|
|
301
|
+
price: 116,
|
|
302
|
+
quantity: 1,
|
|
303
|
+
modifiers: [percentageDiscount16],
|
|
304
|
+
};
|
|
305
|
+
const newItem = pricingService.item.calculate(orderItem);
|
|
306
|
+
|
|
307
|
+
expect(newItem).toHaveProperty('total', 116);
|
|
308
|
+
expect(newItem).toHaveProperty('price', 116);
|
|
309
|
+
expect(newItem.modifiers[0]).toHaveProperty('_computed', {
|
|
310
|
+
amount: -22.095238095238095,
|
|
311
|
+
description: 'percentageDiscount16 (-$22.10)',
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
expect(newItem).toHaveProperty('subTotals', {
|
|
315
|
+
discount: -22.095238095238095,
|
|
316
|
+
_included: 0,
|
|
317
|
+
_xincluded: -22.095238095238095,
|
|
318
|
+
_direct: 0,
|
|
319
|
+
_xdirect: -22.095238095238095,
|
|
320
|
+
_simple: 138.0952380952381,
|
|
321
|
+
_actual: 138.0952380952381,
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
test('CU-86duwyvrr: Calculate item with modifier.extractCalculatedvalue=true percentageFee16', () => {
|
|
326
|
+
const percentageFee16 = {
|
|
327
|
+
compute: {
|
|
328
|
+
amount: 16,
|
|
329
|
+
type: 'percentage',
|
|
330
|
+
action: 'add',
|
|
331
|
+
},
|
|
332
|
+
properties: {
|
|
333
|
+
extractCalculatedValue: true,
|
|
334
|
+
},
|
|
335
|
+
name: 'percentageFee16',
|
|
336
|
+
type: 'tax',
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
const orderItem = { price: 116, quantity: 1, modifiers: [percentageFee16] };
|
|
340
|
+
const newItem = pricingService.item.calculate(orderItem);
|
|
341
|
+
|
|
342
|
+
expect(newItem).toHaveProperty('total', 116);
|
|
343
|
+
expect(newItem).toHaveProperty('price', 116);
|
|
344
|
+
expect(newItem.modifiers[0]).toHaveProperty('_computed', {
|
|
345
|
+
amount: 16,
|
|
346
|
+
description: 'percentageFee16 ($16.00)',
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
expect(newItem).toHaveProperty('subTotals', {
|
|
350
|
+
tax: 16,
|
|
351
|
+
_included: 0,
|
|
352
|
+
_xincluded: 16,
|
|
353
|
+
_direct: 0,
|
|
354
|
+
_xdirect: 16,
|
|
355
|
+
_simple: 100,
|
|
356
|
+
_actual: 100,
|
|
357
|
+
});
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
test('CU-86duwyvrr: Calculate item with modifier.extractCalculatedvalue=true fixedDiscount16', () => {
|
|
361
|
+
const fixedDiscount16 = {
|
|
362
|
+
compute: {
|
|
363
|
+
amount: 16,
|
|
364
|
+
type: 'fixed',
|
|
365
|
+
action: 'subtract',
|
|
366
|
+
},
|
|
367
|
+
properties: {
|
|
368
|
+
extractCalculatedValue: true,
|
|
369
|
+
},
|
|
370
|
+
name: 'fixedDiscount16',
|
|
371
|
+
type: 'discount',
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
const orderItem = {
|
|
375
|
+
price: 116,
|
|
376
|
+
quantity: 1,
|
|
377
|
+
modifiers: [fixedDiscount16],
|
|
378
|
+
};
|
|
379
|
+
const newItem = pricingService.item.calculate(orderItem);
|
|
380
|
+
|
|
381
|
+
expect(newItem).toHaveProperty('total', 116);
|
|
382
|
+
expect(newItem).toHaveProperty('price', 116);
|
|
383
|
+
expect(newItem.modifiers[0]).toHaveProperty('_computed', {
|
|
384
|
+
amount: -16,
|
|
385
|
+
description: 'fixedDiscount16 (-$16.00)',
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
expect(newItem).toHaveProperty('subTotals', {
|
|
389
|
+
discount: -16,
|
|
390
|
+
_included: 0,
|
|
391
|
+
_xincluded: -16,
|
|
392
|
+
_direct: 0,
|
|
393
|
+
_xdirect: -16,
|
|
394
|
+
_simple: 132,
|
|
395
|
+
_actual: 132,
|
|
396
|
+
});
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
test('CU-86duwyvrr: Calculate item with modifier.extractCalculatedvalue=true fixedFee16', () => {
|
|
400
|
+
const fixedFee16 = {
|
|
401
|
+
compute: {
|
|
402
|
+
amount: 16,
|
|
403
|
+
type: 'fixed',
|
|
404
|
+
action: 'add',
|
|
405
|
+
},
|
|
406
|
+
properties: {
|
|
407
|
+
extractCalculatedValue: true,
|
|
408
|
+
},
|
|
409
|
+
name: 'fixedFee16',
|
|
410
|
+
type: 'tax',
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
const orderItem = {
|
|
414
|
+
price: 116,
|
|
415
|
+
quantity: 1,
|
|
416
|
+
modifiers: [fixedFee16],
|
|
417
|
+
};
|
|
418
|
+
const newItem = pricingService.item.calculate(orderItem);
|
|
419
|
+
|
|
420
|
+
expect(newItem).toHaveProperty('total', 116);
|
|
421
|
+
expect(newItem).toHaveProperty('price', 116);
|
|
422
|
+
expect(newItem.modifiers[0]).toHaveProperty('_computed', {
|
|
423
|
+
amount: 16,
|
|
424
|
+
description: 'fixedFee16 ($16.00)',
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
expect(newItem).toHaveProperty('subTotals', {
|
|
428
|
+
tax: 16,
|
|
429
|
+
_included: 0,
|
|
430
|
+
_xincluded: 16,
|
|
431
|
+
_direct: 0,
|
|
432
|
+
_xdirect: 16,
|
|
433
|
+
_simple: 100,
|
|
434
|
+
_actual: 100,
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
test('CU-86duwyvrr: Calculate item with modifier.extractCalculatedvalue=true fixedFee16 and other fixed fee of 10%', () => {
|
|
439
|
+
const fixedFee16 = {
|
|
440
|
+
compute: {
|
|
441
|
+
amount: 16,
|
|
442
|
+
type: 'fixed',
|
|
443
|
+
action: 'add',
|
|
444
|
+
},
|
|
445
|
+
properties: {
|
|
446
|
+
extractCalculatedValue: true,
|
|
447
|
+
},
|
|
448
|
+
name: 'fixedFee16',
|
|
449
|
+
type: 'tax',
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
const fixedFee10 = {
|
|
453
|
+
compute: {
|
|
454
|
+
amount: 10,
|
|
455
|
+
type: 'fixed',
|
|
456
|
+
action: 'add',
|
|
457
|
+
},
|
|
458
|
+
name: 'fixedFee10',
|
|
459
|
+
type: 'tax',
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
const orderItem = {
|
|
463
|
+
price: 116,
|
|
464
|
+
quantity: 1,
|
|
465
|
+
modifiers: [fixedFee16, fixedFee10],
|
|
466
|
+
};
|
|
467
|
+
const newItem = pricingService.item.calculate(orderItem);
|
|
468
|
+
|
|
469
|
+
expect(newItem).toHaveProperty('total', 126);
|
|
470
|
+
expect(newItem).toHaveProperty('price', 116);
|
|
471
|
+
expect(newItem.modifiers[0]).toHaveProperty('_computed', {
|
|
472
|
+
amount: 16,
|
|
473
|
+
description: 'fixedFee16 ($16.00)',
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
expect(newItem).toHaveProperty('subTotals', {
|
|
477
|
+
tax: 26,
|
|
478
|
+
_included: 0,
|
|
479
|
+
_xincluded: 26,
|
|
480
|
+
_direct: 0,
|
|
481
|
+
_xdirect: 26,
|
|
482
|
+
_simple: 100,
|
|
483
|
+
_actual: 100,
|
|
484
|
+
});
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
test('CU-86duwyvrr: Calculate item with modifier.extractCalculatedvalue=true fixedFee16 and other percentage fee of 10%', () => {
|
|
488
|
+
const fixedFee16 = {
|
|
489
|
+
compute: {
|
|
490
|
+
amount: 16,
|
|
491
|
+
type: 'fixed',
|
|
492
|
+
action: 'add',
|
|
493
|
+
},
|
|
494
|
+
properties: {
|
|
495
|
+
extractCalculatedValue: true,
|
|
496
|
+
},
|
|
497
|
+
name: 'fixedFee16',
|
|
498
|
+
type: 'tax',
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
const percentageFee10 = {
|
|
502
|
+
compute: {
|
|
503
|
+
amount: 10,
|
|
504
|
+
type: 'percentage',
|
|
505
|
+
action: 'add',
|
|
506
|
+
},
|
|
507
|
+
name: 'percentageFee10',
|
|
508
|
+
type: 'tax',
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
const orderItem = {
|
|
512
|
+
price: 116,
|
|
513
|
+
quantity: 1,
|
|
514
|
+
modifiers: [fixedFee16, percentageFee10],
|
|
515
|
+
};
|
|
516
|
+
const newItem = pricingService.item.calculate(orderItem);
|
|
517
|
+
|
|
518
|
+
expect(newItem).toHaveProperty('total', 126);
|
|
519
|
+
expect(newItem).toHaveProperty('price', 116);
|
|
520
|
+
expect(newItem.modifiers[0]).toHaveProperty('_computed', {
|
|
521
|
+
amount: 16,
|
|
522
|
+
description: 'fixedFee16 ($16.00)',
|
|
523
|
+
});
|
|
524
|
+
expect(newItem.modifiers[1]).toHaveProperty('_computed', {
|
|
525
|
+
amount: 10,
|
|
526
|
+
description: 'percentageFee10 ($10.00)',
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
expect(newItem).toHaveProperty('subTotals', {
|
|
530
|
+
tax: 26,
|
|
531
|
+
_included: 0,
|
|
532
|
+
_xincluded: 26,
|
|
533
|
+
_direct: 0,
|
|
534
|
+
_xdirect: 26,
|
|
535
|
+
_simple: 100,
|
|
536
|
+
_actual: 100,
|
|
537
|
+
});
|
|
538
|
+
});
|
|
285
539
|
});
|
|
@@ -26,7 +26,11 @@ describe('addItem function', () => {
|
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
// Call the function
|
|
29
|
-
const
|
|
29
|
+
const {
|
|
30
|
+
updatedOrder,
|
|
31
|
+
itemIndex,
|
|
32
|
+
item: addedItem,
|
|
33
|
+
} = addItem({
|
|
30
34
|
order,
|
|
31
35
|
item,
|
|
32
36
|
itemIndex: -1, // No existing item in order
|
|
@@ -67,7 +71,11 @@ describe('addItem function', () => {
|
|
|
67
71
|
};
|
|
68
72
|
|
|
69
73
|
// Call the function
|
|
70
|
-
const
|
|
74
|
+
const {
|
|
75
|
+
updatedOrder,
|
|
76
|
+
itemIndex,
|
|
77
|
+
item: addedItem,
|
|
78
|
+
} = addItem({
|
|
71
79
|
order,
|
|
72
80
|
item,
|
|
73
81
|
itemIndex: -1, // Assuming no explicit index
|
|
@@ -99,7 +107,7 @@ describe('addItem function', () => {
|
|
|
99
107
|
};
|
|
100
108
|
|
|
101
109
|
// Call the function with an overridden quantity
|
|
102
|
-
const
|
|
110
|
+
const { updatedOrder, item: addedItem } = addItem({
|
|
103
111
|
order,
|
|
104
112
|
item,
|
|
105
113
|
itemIndex: -1, // Assuming no explicit index
|
|
@@ -179,7 +187,7 @@ describe('addItem function', () => {
|
|
|
179
187
|
};
|
|
180
188
|
|
|
181
189
|
// Call the function
|
|
182
|
-
const
|
|
190
|
+
const { updatedOrder } = addItem({
|
|
183
191
|
order,
|
|
184
192
|
item,
|
|
185
193
|
itemIndex: -1, // Assuming no explicit index
|
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
const usePricing = require('../../index');
|
|
2
2
|
const mockStores = require('../mocks/stores');
|
|
3
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
4
|
const orderCredit = require('../mocks/order-credit.json');
|
|
13
5
|
const { Types } = require('../../lib/constants/Modifier/Compute');
|
|
14
6
|
|
|
@@ -2574,4 +2566,58 @@ describe('Order actions', () => {
|
|
|
2574
2566
|
expect(subOrders.length).toBe(0);
|
|
2575
2567
|
expect(newParentOrder.orders).toBe(undefined);
|
|
2576
2568
|
});
|
|
2569
|
+
|
|
2570
|
+
test('Get calculated Order, multiple items and indirect modifiers #2 toNeareastMultiple', () => {
|
|
2571
|
+
const pricingService2 = usePricing({
|
|
2572
|
+
store: { _settings: { localization: { nearestMultiple: '0.5' } } },
|
|
2573
|
+
});
|
|
2574
|
+
|
|
2575
|
+
const item1 = {
|
|
2576
|
+
_id: 1,
|
|
2577
|
+
price: 15.99,
|
|
2578
|
+
quantity: 1,
|
|
2579
|
+
};
|
|
2580
|
+
const item2 = {
|
|
2581
|
+
_id: 2,
|
|
2582
|
+
price: 4.65,
|
|
2583
|
+
quantity: 1,
|
|
2584
|
+
};
|
|
2585
|
+
const modifier1 = {
|
|
2586
|
+
compute: {
|
|
2587
|
+
amount: 10,
|
|
2588
|
+
action: 'subtract',
|
|
2589
|
+
type: 'percentage',
|
|
2590
|
+
},
|
|
2591
|
+
name: 'modifier1',
|
|
2592
|
+
type: 'discount',
|
|
2593
|
+
};
|
|
2594
|
+
|
|
2595
|
+
const order = { items: [item1, item2], modifiers: [modifier1] };
|
|
2596
|
+
const newOrder = pricingService2.order.calculate(order);
|
|
2597
|
+
expect(newOrder).toHaveProperty('total', 19);
|
|
2598
|
+
expect(newOrder).toHaveProperty('subTotal', 20.64);
|
|
2599
|
+
expect(newOrder).toHaveProperty('subTotals', {
|
|
2600
|
+
discount: -2.064,
|
|
2601
|
+
});
|
|
2602
|
+
expect(newOrder.items[0]).toHaveProperty('total', 14.81);
|
|
2603
|
+
expect(newOrder.items[0]).toHaveProperty('subTotals', {
|
|
2604
|
+
discount: -1.599,
|
|
2605
|
+
_included: 0,
|
|
2606
|
+
_xincluded: -1.599,
|
|
2607
|
+
_direct: 0,
|
|
2608
|
+
_xdirect: -1.599,
|
|
2609
|
+
_simple: 15.99,
|
|
2610
|
+
_actual: 15.99,
|
|
2611
|
+
});
|
|
2612
|
+
expect(newOrder.items[1]).toHaveProperty('total', 4.19);
|
|
2613
|
+
expect(newOrder.items[1]).toHaveProperty('subTotals', {
|
|
2614
|
+
discount: -0.465,
|
|
2615
|
+
_included: 0,
|
|
2616
|
+
_xincluded: -0.465,
|
|
2617
|
+
_direct: 0,
|
|
2618
|
+
_xdirect: -0.465,
|
|
2619
|
+
_simple: 4.65,
|
|
2620
|
+
_actual: 4.65,
|
|
2621
|
+
});
|
|
2622
|
+
});
|
|
2577
2623
|
});
|
package/lib/item/calculate.js
CHANGED
|
@@ -26,7 +26,7 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
26
26
|
_actual: 0,
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
const price = parseFloat(item
|
|
29
|
+
const price = parseFloat(actions.getPriceWithoutModifiers({ item }));
|
|
30
30
|
const quantity = parseInt(item.quantity, 10);
|
|
31
31
|
subTotals._simple = math.mul(price, quantity);
|
|
32
32
|
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module.exports = ({ modifierActions }) =>
|
|
2
|
+
/* eslint-disable no-restricted-syntax */
|
|
3
|
+
function getPriceWithoutModifiers({ item }) {
|
|
4
|
+
if (
|
|
5
|
+
!item ||
|
|
6
|
+
!Array.isArray(item.modifiers) ||
|
|
7
|
+
typeof item.price !== 'number'
|
|
8
|
+
)
|
|
9
|
+
return item.price;
|
|
10
|
+
|
|
11
|
+
const calculatedModifiers = item.modifiers.filter(modifier =>
|
|
12
|
+
modifierActions.isExtractCalculatedValue({ modifier })
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
if (calculatedModifiers.length === 0) return item.price;
|
|
16
|
+
|
|
17
|
+
const sortedModifiers = modifierActions.sort([...calculatedModifiers]);
|
|
18
|
+
let computedPrice = item.price;
|
|
19
|
+
|
|
20
|
+
for (const modifier of sortedModifiers) {
|
|
21
|
+
computedPrice = modifierActions.getPriceWithoutModifier({
|
|
22
|
+
itemPrice: computedPrice,
|
|
23
|
+
modifier,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return computedPrice;
|
|
28
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module.exports = ({ utils, settings }) =>
|
|
2
|
+
function getTotalNeareastDifference({ items }) {
|
|
3
|
+
const { total } = items.reduce(
|
|
4
|
+
(acc, item) => ({
|
|
5
|
+
total: utils.math.add(acc.total, item.total),
|
|
6
|
+
}),
|
|
7
|
+
{ total: 0 }
|
|
8
|
+
);
|
|
9
|
+
|
|
10
|
+
const totalToNeareast = utils.math.toNearestMultiple(
|
|
11
|
+
total,
|
|
12
|
+
settings.localization.nearestMultiple
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
return utils.math.sub(totalToNeareast, total);
|
|
16
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module.exports = ({ utils }) =>
|
|
2
|
+
function getTotalsDifference({ items }) {
|
|
3
|
+
const { total, subTotalsTotal } = items.reduce(
|
|
4
|
+
(acc, item) => ({
|
|
5
|
+
total: utils.math.add(acc.total, item.total),
|
|
6
|
+
subTotalsTotal: utils.math.add(
|
|
7
|
+
acc.subTotalsTotal,
|
|
8
|
+
utils.math.add(item.subTotals._actual, item.subTotals._xincluded)
|
|
9
|
+
),
|
|
10
|
+
}),
|
|
11
|
+
{ total: 0, subTotalsTotal: 0 }
|
|
12
|
+
);
|
|
13
|
+
return utils.math.sub(subTotalsTotal, total);
|
|
14
|
+
};
|
package/lib/item/index.js
CHANGED
|
@@ -41,6 +41,9 @@ const hasPaymentModifierWithPaymentId = require('./hasPaymentModifierWithPayment
|
|
|
41
41
|
const getBalance = require('./getBalance');
|
|
42
42
|
const getPipeModifiers = require('./getPipeModifiers');
|
|
43
43
|
const getInvalidRequiredModifiers = require('./getInvalidRequiredModifiers');
|
|
44
|
+
const getPriceWithoutModifiers = require('./getPriceWithoutModifiers');
|
|
45
|
+
const getTotalsDifference = require('./getTotalsDifference');
|
|
46
|
+
const getTotalNeareastDifference = require('./getTotalNeareastDifference');
|
|
44
47
|
|
|
45
48
|
const itemActions = (deps = {}) => {
|
|
46
49
|
const actions = {};
|
|
@@ -95,6 +98,9 @@ const itemActions = (deps = {}) => {
|
|
|
95
98
|
getBalance: getBalance(innerDeps),
|
|
96
99
|
getPipeModifiers: getPipeModifiers(innerDeps),
|
|
97
100
|
getInvalidRequiredModifiers: getInvalidRequiredModifiers(innerDeps),
|
|
101
|
+
getPriceWithoutModifiers: getPriceWithoutModifiers(innerDeps),
|
|
102
|
+
getTotalsDifference: getTotalsDifference(innerDeps),
|
|
103
|
+
getTotalNeareastDifference: getTotalNeareastDifference(innerDeps),
|
|
98
104
|
});
|
|
99
105
|
|
|
100
106
|
Object.keys(freezedActions).forEach(actionName => {
|
package/lib/item/isFullyPaid.js
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module.exports = ({ constants, utils }) => {
|
|
2
|
+
const { Modifier } = constants;
|
|
3
|
+
const { math } = utils;
|
|
4
|
+
|
|
5
|
+
return function getPriceWithoutModifier({ itemPrice, modifier }) {
|
|
6
|
+
if (
|
|
7
|
+
!modifier ||
|
|
8
|
+
!modifier.compute ||
|
|
9
|
+
!modifier.compute.type ||
|
|
10
|
+
typeof itemPrice !== 'number'
|
|
11
|
+
)
|
|
12
|
+
return itemPrice;
|
|
13
|
+
|
|
14
|
+
if (modifier.compute.action === Modifier.Compute.Actions.ADD) {
|
|
15
|
+
if (modifier.compute.type === Modifier.Compute.Types.PERCENTAGE)
|
|
16
|
+
return math.div(
|
|
17
|
+
itemPrice,
|
|
18
|
+
math.add(1, math.div(modifier.compute.amount, 100))
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
if (modifier.compute.type === Modifier.Compute.Types.FIXED)
|
|
22
|
+
return math.sub(itemPrice, modifier.compute.amount);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (modifier.compute.action === Modifier.Compute.Actions.SUBTRACT) {
|
|
26
|
+
if (modifier.compute.type === Modifier.Compute.Types.PERCENTAGE)
|
|
27
|
+
return math.div(
|
|
28
|
+
itemPrice,
|
|
29
|
+
math.sub(1, math.div(modifier.compute.amount, 100))
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
if (modifier.compute.type === Modifier.Compute.Types.FIXED)
|
|
33
|
+
return math.add(itemPrice, modifier.compute.amount);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return itemPrice;
|
|
37
|
+
};
|
|
38
|
+
};
|
package/lib/modifier/index.js
CHANGED
|
@@ -146,6 +146,8 @@ const calculatePaymentDiscountModifier = require('./calculatePaymentDiscountModi
|
|
|
146
146
|
const calculatePaymentFeeModifier = require('./calculatePaymentFeeModifier');
|
|
147
147
|
const isCalculatedPaymentModifier = require('./isCalculatedPaymentModifier');
|
|
148
148
|
const isChild = require('./isChild');
|
|
149
|
+
const isExtractCalculatedValue = require('./isExtractCalculatedValue');
|
|
150
|
+
const getPriceWithoutModifier = require('./getPriceWithoutModifier');
|
|
149
151
|
|
|
150
152
|
const modifierActions = (deps = {}) => {
|
|
151
153
|
const actions = {};
|
|
@@ -305,6 +307,8 @@ const modifierActions = (deps = {}) => {
|
|
|
305
307
|
calculatePaymentFeeModifier: calculatePaymentFeeModifier(innerDeps),
|
|
306
308
|
isCalculatedPaymentModifier: isCalculatedPaymentModifier(innerDeps),
|
|
307
309
|
isChild: isChild(innerDeps),
|
|
310
|
+
isExtractCalculatedValue: isExtractCalculatedValue(innerDeps),
|
|
311
|
+
getPriceWithoutModifier: getPriceWithoutModifier(innerDeps),
|
|
308
312
|
});
|
|
309
313
|
|
|
310
314
|
Object.keys(freezedActions).forEach(actionName => {
|
package/lib/order/addItem.js
CHANGED
|
@@ -187,6 +187,7 @@ module.exports = ({ actions, itemActions, modifierActions, settings, _ }) => {
|
|
|
187
187
|
!_.isEmpty(orderItem.modifiers)
|
|
188
188
|
)
|
|
189
189
|
requiredModifiers = getUnselectedModifiers(orderItem, requiredModifiers);
|
|
190
|
+
|
|
190
191
|
if (!_.isEmpty(requiredModifiers)) {
|
|
191
192
|
modifiersToAdd.push(
|
|
192
193
|
...requiredModifiers.filter(
|
|
@@ -213,17 +214,23 @@ module.exports = ({ actions, itemActions, modifierActions, settings, _ }) => {
|
|
|
213
214
|
let [nextOrder, nextItemIndex] = params;
|
|
214
215
|
const [, , nextItem] = params;
|
|
215
216
|
|
|
217
|
+
const overrideModifiers = modifiersToAdd.filter(modToAdd =>
|
|
218
|
+
modifierActions.isOverride(modToAdd)
|
|
219
|
+
);
|
|
220
|
+
|
|
216
221
|
if (modifiersToAdd.length) {
|
|
217
|
-
nextOrder = modifiersToAdd
|
|
218
|
-
(
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
222
|
+
nextOrder = modifiersToAdd
|
|
223
|
+
.filter(modToAdd => !modifierActions.isOverride(modToAdd))
|
|
224
|
+
.reduce(
|
|
225
|
+
(acc, modifier) =>
|
|
226
|
+
actions.addItemModifier({
|
|
227
|
+
itemIndex: nextItemIndex,
|
|
228
|
+
order: acc,
|
|
229
|
+
modifier,
|
|
230
|
+
cache,
|
|
231
|
+
}),
|
|
232
|
+
nextOrder
|
|
233
|
+
);
|
|
227
234
|
}
|
|
228
235
|
|
|
229
236
|
const { reArrangedOrder, newIndex } = reArrangeNewItem({
|
|
@@ -243,6 +250,11 @@ module.exports = ({ actions, itemActions, modifierActions, settings, _ }) => {
|
|
|
243
250
|
nextOrder.items.splice(idxToRemove, 1);
|
|
244
251
|
}
|
|
245
252
|
|
|
246
|
-
return
|
|
253
|
+
return {
|
|
254
|
+
updatedOrder: nextOrder,
|
|
255
|
+
itemIndex: nextItemIndex,
|
|
256
|
+
item: nextItem,
|
|
257
|
+
overrideModifiers,
|
|
258
|
+
};
|
|
247
259
|
};
|
|
248
260
|
};
|
|
@@ -244,7 +244,7 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
|
|
|
244
244
|
|
|
245
245
|
if (modifierActions.hasRelatedItems(modifier)) {
|
|
246
246
|
order = modifier.properties.group.items.reduce((acc, each) => {
|
|
247
|
-
const
|
|
247
|
+
const { updatedOrder: nextOrder } = actions.addItem({
|
|
248
248
|
item: {
|
|
249
249
|
...each,
|
|
250
250
|
itemId: each._id,
|
package/lib/order/calculate.js
CHANGED
|
@@ -2,7 +2,14 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* return calculated Order
|
|
4
4
|
*/
|
|
5
|
-
module.exports = ({
|
|
5
|
+
module.exports = ({
|
|
6
|
+
_,
|
|
7
|
+
actions,
|
|
8
|
+
itemActions,
|
|
9
|
+
modifierActions,
|
|
10
|
+
utils,
|
|
11
|
+
storeActions,
|
|
12
|
+
}) =>
|
|
6
13
|
function calculateorder(inputOrder, opts = {}) {
|
|
7
14
|
if (!inputOrder) return inputOrder;
|
|
8
15
|
const order = _.cloneDeep(inputOrder);
|
|
@@ -97,24 +104,28 @@ module.exports = ({ _, actions, itemActions, modifierActions, utils }) =>
|
|
|
97
104
|
|
|
98
105
|
const calculatedItemWIM = itemActions.calculate(itemsWIM, options);
|
|
99
106
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
(acc, item) => ({
|
|
103
|
-
total: utils.math.add(acc.total, item.total),
|
|
104
|
-
subTotalsTotal: utils.math.add(
|
|
105
|
-
acc.subTotalsTotal,
|
|
106
|
-
utils.math.add(item.subTotals._actual, item.subTotals._xincluded)
|
|
107
|
-
),
|
|
108
|
-
}),
|
|
109
|
-
{ total: 0, subTotalsTotal: 0 }
|
|
110
|
-
);
|
|
111
|
-
const difference = utils.math.sub(subTotalsTotal, total);
|
|
112
|
-
|
|
113
|
-
if (difference > 0) {
|
|
107
|
+
const addToItemTotal = difference => {
|
|
108
|
+
if (difference > 0 && calculatedItemWIM.length) {
|
|
114
109
|
calculatedItemWIM[0].total = utils.math.toDecimalPlaces(
|
|
115
110
|
utils.math.add(calculatedItemWIM[0].total, difference)
|
|
116
111
|
);
|
|
117
112
|
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
if (calculatedItemWIM.length > 1) {
|
|
116
|
+
const difference = itemActions.getTotalsDifference({
|
|
117
|
+
items: calculatedItemWIM,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
addToItemTotal(difference);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (storeActions.isNeareastMultiple() && calculatedItemWIM.length > 0) {
|
|
124
|
+
const difference = itemActions.getTotalNeareastDifference({
|
|
125
|
+
items: calculatedItemWIM,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
if (difference > 0) addToItemTotal(difference);
|
|
118
129
|
}
|
|
119
130
|
|
|
120
131
|
return {
|
|
@@ -14,7 +14,7 @@ module.exports = ({ actions, settings, _ }) => {
|
|
|
14
14
|
const updatedQuantity = reset
|
|
15
15
|
? quantity
|
|
16
16
|
: parseInt(`${item.quantity}${quantity}`, 10);
|
|
17
|
-
|
|
17
|
+
const { updatedOrder } = actions.updateItem({
|
|
18
18
|
order,
|
|
19
19
|
itemIndex,
|
|
20
20
|
item: {
|
|
@@ -22,6 +22,7 @@ module.exports = ({ actions, settings, _ }) => {
|
|
|
22
22
|
quantity: updatedQuantity,
|
|
23
23
|
},
|
|
24
24
|
});
|
|
25
|
+
return updatedOrder;
|
|
25
26
|
}
|
|
26
27
|
if (isBefore) {
|
|
27
28
|
return actions.addItem({
|
package/lib/store/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
//
|
|
2
2
|
const pickEndDate = require('./pickEndDate');
|
|
3
3
|
const getRecommendedEndDate = require('./getRecommendedEndDate');
|
|
4
|
+
const isNeareastMultiple = require('./isNeareastMultiple');
|
|
4
5
|
|
|
5
6
|
const storeActions = (deps = {}) => {
|
|
6
7
|
const actions = {};
|
|
@@ -13,6 +14,7 @@ const storeActions = (deps = {}) => {
|
|
|
13
14
|
const freezedActions = Object.freeze({
|
|
14
15
|
pickEndDate: pickEndDate(innerDeps),
|
|
15
16
|
getRecommendedEndDate: getRecommendedEndDate(innerDeps),
|
|
17
|
+
isNeareastMultiple: isNeareastMultiple(innerDeps),
|
|
16
18
|
});
|
|
17
19
|
|
|
18
20
|
Object.keys(freezedActions).forEach(actionName => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/pricing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.48",
|
|
4
4
|
"description": "Pricing calculator",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"license": "ISC",
|
|
@@ -16,13 +16,14 @@
|
|
|
16
16
|
"test:order": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/order.test.js",
|
|
17
17
|
"test:validateConditions": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/validateConditionsCalculate.test.js",
|
|
18
18
|
"test:hasModifier": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/modifier/hasModifier.test.js",
|
|
19
|
+
"test:item": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/item.test.js",
|
|
19
20
|
"lint": "eslint --quiet lib/"
|
|
20
21
|
},
|
|
21
22
|
"publishConfig": {
|
|
22
23
|
"access": "public"
|
|
23
24
|
},
|
|
24
25
|
"dependencies": {
|
|
25
|
-
"@darkpos/utils": "1.0.
|
|
26
|
+
"@darkpos/utils": "^1.0.11",
|
|
26
27
|
"lodash": "^4.17.21",
|
|
27
28
|
"moment-timezone": "^0.5.34"
|
|
28
29
|
},
|
|
@@ -39,5 +40,5 @@
|
|
|
39
40
|
"supertest": "^6.2.3",
|
|
40
41
|
"supervisor": "^0.12.0"
|
|
41
42
|
},
|
|
42
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "4706909fded1e1083441ae48c8dc154cd569a48b"
|
|
43
44
|
}
|