@darkpos/pricing 1.0.34 → 1.0.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__TEST__/order/order.test.js +339 -0
- package/lib/item/removeModifier.js +8 -6
- package/lib/order/calculate.js +8 -4
- package/lib/order/merge.js +10 -9
- package/package.json +2 -2
|
@@ -2287,4 +2287,343 @@ describe('Order actions', () => {
|
|
|
2287
2287
|
_actual: 10,
|
|
2288
2288
|
});
|
|
2289
2289
|
});
|
|
2290
|
+
|
|
2291
|
+
test('CU-86duna31h -> Get calculated Order, multiple items and indirect required discount modifiers', () => {
|
|
2292
|
+
const discountModifier = {
|
|
2293
|
+
_id: 1,
|
|
2294
|
+
compute: {
|
|
2295
|
+
amount: 10,
|
|
2296
|
+
type: 'percentage',
|
|
2297
|
+
action: 'subtract',
|
|
2298
|
+
},
|
|
2299
|
+
name: `10% Discount`,
|
|
2300
|
+
type: 'discount',
|
|
2301
|
+
required: true,
|
|
2302
|
+
};
|
|
2303
|
+
|
|
2304
|
+
const item1 = {
|
|
2305
|
+
_id: 1,
|
|
2306
|
+
price: 10,
|
|
2307
|
+
quantity: 1,
|
|
2308
|
+
modifiers: [
|
|
2309
|
+
{
|
|
2310
|
+
...discountModifier,
|
|
2311
|
+
_parentId: discountModifier._id,
|
|
2312
|
+
modifierId: discountModifier._id,
|
|
2313
|
+
},
|
|
2314
|
+
],
|
|
2315
|
+
};
|
|
2316
|
+
const item2 = {
|
|
2317
|
+
_id: 2,
|
|
2318
|
+
price: 20,
|
|
2319
|
+
quantity: 1,
|
|
2320
|
+
modifiers: [
|
|
2321
|
+
{
|
|
2322
|
+
...discountModifier,
|
|
2323
|
+
_parentId: discountModifier._id,
|
|
2324
|
+
modifierId: discountModifier._id,
|
|
2325
|
+
},
|
|
2326
|
+
],
|
|
2327
|
+
};
|
|
2328
|
+
|
|
2329
|
+
const order = { items: [item1, item2], modifiers: [discountModifier] };
|
|
2330
|
+
|
|
2331
|
+
const newOrder = pricingService.order.calculate(order);
|
|
2332
|
+
expect(newOrder).toHaveProperty('total', 27);
|
|
2333
|
+
expect(newOrder).toHaveProperty('subTotal', 30);
|
|
2334
|
+
expect(newOrder).toHaveProperty('subTotals', {
|
|
2335
|
+
discount: -3,
|
|
2336
|
+
});
|
|
2337
|
+
expect(newOrder.items[0]).toHaveProperty('total', 9);
|
|
2338
|
+
expect(newOrder.items[0]).toHaveProperty('subTotals', {
|
|
2339
|
+
discount: -1,
|
|
2340
|
+
_included: 0,
|
|
2341
|
+
_xincluded: -1,
|
|
2342
|
+
_direct: 0,
|
|
2343
|
+
_xdirect: -1,
|
|
2344
|
+
_simple: 10,
|
|
2345
|
+
_actual: 10,
|
|
2346
|
+
});
|
|
2347
|
+
expect(newOrder.items[1]).toHaveProperty('total', 18);
|
|
2348
|
+
expect(newOrder.items[1]).toHaveProperty('subTotals', {
|
|
2349
|
+
discount: -2,
|
|
2350
|
+
_included: 0,
|
|
2351
|
+
_xincluded: -2,
|
|
2352
|
+
_direct: 0,
|
|
2353
|
+
_xdirect: -2,
|
|
2354
|
+
_simple: 20,
|
|
2355
|
+
_actual: 20,
|
|
2356
|
+
});
|
|
2357
|
+
});
|
|
2358
|
+
|
|
2359
|
+
test('CU-86duna31h -> Get calculated Order, multiple items and indirect discount modifiers', () => {
|
|
2360
|
+
const discountModifier = {
|
|
2361
|
+
_id: 1,
|
|
2362
|
+
compute: {
|
|
2363
|
+
amount: 10,
|
|
2364
|
+
type: 'percentage',
|
|
2365
|
+
action: 'subtract',
|
|
2366
|
+
},
|
|
2367
|
+
name: `10% Discount`,
|
|
2368
|
+
type: 'discount',
|
|
2369
|
+
required: true,
|
|
2370
|
+
};
|
|
2371
|
+
|
|
2372
|
+
const item1 = {
|
|
2373
|
+
_id: 1,
|
|
2374
|
+
price: 10,
|
|
2375
|
+
quantity: 1,
|
|
2376
|
+
modifiers: [],
|
|
2377
|
+
};
|
|
2378
|
+
const item2 = {
|
|
2379
|
+
_id: 2,
|
|
2380
|
+
price: 20,
|
|
2381
|
+
quantity: 1,
|
|
2382
|
+
modifiers: [],
|
|
2383
|
+
};
|
|
2384
|
+
|
|
2385
|
+
const order = { items: [item1, item2], modifiers: [discountModifier] };
|
|
2386
|
+
|
|
2387
|
+
const newOrder = pricingService.order.calculate(order);
|
|
2388
|
+
expect(newOrder).toHaveProperty('total', 27);
|
|
2389
|
+
expect(newOrder).toHaveProperty('subTotal', 30);
|
|
2390
|
+
expect(newOrder).toHaveProperty('subTotals', {
|
|
2391
|
+
discount: -3,
|
|
2392
|
+
});
|
|
2393
|
+
expect(newOrder.items[0]).toHaveProperty('total', 9);
|
|
2394
|
+
expect(newOrder.items[0]).toHaveProperty('subTotals', {
|
|
2395
|
+
discount: -1,
|
|
2396
|
+
_included: 0,
|
|
2397
|
+
_xincluded: -1,
|
|
2398
|
+
_direct: 0,
|
|
2399
|
+
_xdirect: -1,
|
|
2400
|
+
_simple: 10,
|
|
2401
|
+
_actual: 10,
|
|
2402
|
+
});
|
|
2403
|
+
expect(newOrder.items[1]).toHaveProperty('total', 18);
|
|
2404
|
+
expect(newOrder.items[1]).toHaveProperty('subTotals', {
|
|
2405
|
+
discount: -2,
|
|
2406
|
+
_included: 0,
|
|
2407
|
+
_xincluded: -2,
|
|
2408
|
+
_direct: 0,
|
|
2409
|
+
_xdirect: -2,
|
|
2410
|
+
_simple: 20,
|
|
2411
|
+
_actual: 20,
|
|
2412
|
+
});
|
|
2413
|
+
});
|
|
2414
|
+
|
|
2415
|
+
test('CU-86duna31h -> Get calculated Order, multiple items and indirect required discount modifiers', () => {
|
|
2416
|
+
const discountModifier = {
|
|
2417
|
+
_id: 1,
|
|
2418
|
+
compute: {
|
|
2419
|
+
amount: 10,
|
|
2420
|
+
type: 'percentage',
|
|
2421
|
+
action: 'subtract',
|
|
2422
|
+
},
|
|
2423
|
+
name: `10% Discount`,
|
|
2424
|
+
type: 'discount',
|
|
2425
|
+
};
|
|
2426
|
+
|
|
2427
|
+
const item1 = {
|
|
2428
|
+
_id: 1,
|
|
2429
|
+
price: 10,
|
|
2430
|
+
quantity: 1,
|
|
2431
|
+
modifiers: [
|
|
2432
|
+
{
|
|
2433
|
+
...discountModifier,
|
|
2434
|
+
_parentId: discountModifier._id,
|
|
2435
|
+
modifierId: discountModifier._id,
|
|
2436
|
+
},
|
|
2437
|
+
],
|
|
2438
|
+
};
|
|
2439
|
+
const item2 = {
|
|
2440
|
+
_id: 2,
|
|
2441
|
+
price: 20,
|
|
2442
|
+
quantity: 1,
|
|
2443
|
+
modifiers: [
|
|
2444
|
+
{
|
|
2445
|
+
...discountModifier,
|
|
2446
|
+
_parentId: discountModifier._id,
|
|
2447
|
+
modifierId: discountModifier._id,
|
|
2448
|
+
},
|
|
2449
|
+
],
|
|
2450
|
+
};
|
|
2451
|
+
const order = { items: [item1, item2], modifiers: [discountModifier] };
|
|
2452
|
+
|
|
2453
|
+
const newOrder = pricingService.order.calculate(order);
|
|
2454
|
+
expect(newOrder).toHaveProperty('total', 27);
|
|
2455
|
+
expect(newOrder).toHaveProperty('subTotal', 30);
|
|
2456
|
+
expect(newOrder).toHaveProperty('subTotals', {
|
|
2457
|
+
discount: -3,
|
|
2458
|
+
});
|
|
2459
|
+
expect(newOrder.items[0]).toHaveProperty('total', 9);
|
|
2460
|
+
expect(newOrder.items[0]).toHaveProperty('subTotals', {
|
|
2461
|
+
discount: -1,
|
|
2462
|
+
_included: 0,
|
|
2463
|
+
_xincluded: -1,
|
|
2464
|
+
_direct: 0,
|
|
2465
|
+
_xdirect: -1,
|
|
2466
|
+
_simple: 10,
|
|
2467
|
+
_actual: 10,
|
|
2468
|
+
});
|
|
2469
|
+
expect(newOrder.items[1]).toHaveProperty('total', 18);
|
|
2470
|
+
expect(newOrder.items[1]).toHaveProperty('subTotals', {
|
|
2471
|
+
discount: -2,
|
|
2472
|
+
_included: 0,
|
|
2473
|
+
_xincluded: -2,
|
|
2474
|
+
_direct: 0,
|
|
2475
|
+
_xdirect: -2,
|
|
2476
|
+
_simple: 20,
|
|
2477
|
+
_actual: 20,
|
|
2478
|
+
});
|
|
2479
|
+
});
|
|
2480
|
+
|
|
2481
|
+
test('CU-86dueyuqj -> Merge Parent orders', () => {
|
|
2482
|
+
const item1 = {
|
|
2483
|
+
_id: 1,
|
|
2484
|
+
price: 10,
|
|
2485
|
+
quantity: 1,
|
|
2486
|
+
modifiers: [],
|
|
2487
|
+
};
|
|
2488
|
+
const item2 = {
|
|
2489
|
+
_id: 2,
|
|
2490
|
+
price: 20,
|
|
2491
|
+
quantity: 1,
|
|
2492
|
+
modifiers: [],
|
|
2493
|
+
};
|
|
2494
|
+
|
|
2495
|
+
const user = {
|
|
2496
|
+
_id: 'userId123',
|
|
2497
|
+
firstName: 'Esteban',
|
|
2498
|
+
lastName: 'Mero',
|
|
2499
|
+
};
|
|
2500
|
+
|
|
2501
|
+
const childOrder1 = pricingService.order.calculate({
|
|
2502
|
+
items: [item1],
|
|
2503
|
+
_id: 'abc',
|
|
2504
|
+
_parentId: 1,
|
|
2505
|
+
});
|
|
2506
|
+
const childOrder2 = pricingService.order.calculate({
|
|
2507
|
+
items: [item2],
|
|
2508
|
+
_id: 'bcd',
|
|
2509
|
+
_parentId: 2,
|
|
2510
|
+
});
|
|
2511
|
+
|
|
2512
|
+
const parentOrder1 = {
|
|
2513
|
+
items: [],
|
|
2514
|
+
orders: [childOrder1],
|
|
2515
|
+
_isParent: true,
|
|
2516
|
+
_id: 1,
|
|
2517
|
+
};
|
|
2518
|
+
const parentOrder2 = {
|
|
2519
|
+
items: [],
|
|
2520
|
+
orders: [childOrder2],
|
|
2521
|
+
_isParent: true,
|
|
2522
|
+
_id: 2,
|
|
2523
|
+
};
|
|
2524
|
+
|
|
2525
|
+
const [newParentOrder, voidedOrders, ordersToDelete, subOrders] =
|
|
2526
|
+
pricingService.order.merge({
|
|
2527
|
+
orders: [parentOrder1, parentOrder2],
|
|
2528
|
+
subOrdersToMerge: [childOrder1, childOrder2],
|
|
2529
|
+
aggregateItems: true,
|
|
2530
|
+
user,
|
|
2531
|
+
});
|
|
2532
|
+
|
|
2533
|
+
expect(newParentOrder).toHaveProperty('total', 30);
|
|
2534
|
+
expect(voidedOrders.length).toBe(1);
|
|
2535
|
+
expect(ordersToDelete.length).toBe(0);
|
|
2536
|
+
expect(subOrders.length).toBe(2);
|
|
2537
|
+
expect(newParentOrder.orders.length).toBe(2);
|
|
2538
|
+
});
|
|
2539
|
+
|
|
2540
|
+
test('CU-86dueyuqj -> Merge Orders that are not parents and not children', () => {
|
|
2541
|
+
const item1 = {
|
|
2542
|
+
_id: 1,
|
|
2543
|
+
price: 10,
|
|
2544
|
+
quantity: 1,
|
|
2545
|
+
modifiers: [],
|
|
2546
|
+
};
|
|
2547
|
+
const item2 = {
|
|
2548
|
+
_id: 2,
|
|
2549
|
+
price: 20,
|
|
2550
|
+
quantity: 1,
|
|
2551
|
+
modifiers: [],
|
|
2552
|
+
};
|
|
2553
|
+
|
|
2554
|
+
const user = {
|
|
2555
|
+
_id: 'userId123',
|
|
2556
|
+
firstName: 'Esteban',
|
|
2557
|
+
lastName: 'Mero',
|
|
2558
|
+
};
|
|
2559
|
+
|
|
2560
|
+
const order1 = pricingService.order.calculate({
|
|
2561
|
+
items: [item1],
|
|
2562
|
+
_id: 'abc',
|
|
2563
|
+
});
|
|
2564
|
+
const order2 = pricingService.order.calculate({
|
|
2565
|
+
items: [item2],
|
|
2566
|
+
_id: 'bcd',
|
|
2567
|
+
});
|
|
2568
|
+
|
|
2569
|
+
const [newOrder, voidedOrders, ordersToDelete, subOrders] =
|
|
2570
|
+
pricingService.order.merge({
|
|
2571
|
+
orders: [order1, order2],
|
|
2572
|
+
subOrdersToMerge: [],
|
|
2573
|
+
aggregateItems: true,
|
|
2574
|
+
user,
|
|
2575
|
+
});
|
|
2576
|
+
|
|
2577
|
+
expect(newOrder).toHaveProperty('total', 30);
|
|
2578
|
+
expect(voidedOrders.length).toBe(1);
|
|
2579
|
+
expect(ordersToDelete.length).toBe(0);
|
|
2580
|
+
expect(subOrders.length).toBe(0);
|
|
2581
|
+
expect(newOrder.orders).toBe(undefined);
|
|
2582
|
+
});
|
|
2583
|
+
|
|
2584
|
+
test('CU-86dueyuqj -> Merge Child orders', () => {
|
|
2585
|
+
const item1 = {
|
|
2586
|
+
_id: 1,
|
|
2587
|
+
price: 10,
|
|
2588
|
+
quantity: 1,
|
|
2589
|
+
modifiers: [],
|
|
2590
|
+
};
|
|
2591
|
+
const item2 = {
|
|
2592
|
+
_id: 2,
|
|
2593
|
+
price: 20,
|
|
2594
|
+
quantity: 1,
|
|
2595
|
+
modifiers: [],
|
|
2596
|
+
};
|
|
2597
|
+
|
|
2598
|
+
const user = {
|
|
2599
|
+
_id: 'userId123',
|
|
2600
|
+
firstName: 'Esteban',
|
|
2601
|
+
lastName: 'Mero',
|
|
2602
|
+
};
|
|
2603
|
+
|
|
2604
|
+
const childOrder1 = pricingService.order.calculate({
|
|
2605
|
+
items: [item1],
|
|
2606
|
+
_id: 'abc',
|
|
2607
|
+
_parentId: 1,
|
|
2608
|
+
});
|
|
2609
|
+
const childOrder2 = pricingService.order.calculate({
|
|
2610
|
+
items: [item2],
|
|
2611
|
+
_id: 'bcd',
|
|
2612
|
+
_parentId: 1,
|
|
2613
|
+
});
|
|
2614
|
+
|
|
2615
|
+
const [newParentOrder, voidedOrders, ordersToDelete, subOrders] =
|
|
2616
|
+
pricingService.order.merge({
|
|
2617
|
+
orders: [childOrder1, childOrder2],
|
|
2618
|
+
subOrdersToMerge: [],
|
|
2619
|
+
aggregateItems: true,
|
|
2620
|
+
user,
|
|
2621
|
+
});
|
|
2622
|
+
|
|
2623
|
+
expect(newParentOrder).toHaveProperty('total', 30);
|
|
2624
|
+
expect(voidedOrders.length).toBe(1);
|
|
2625
|
+
expect(ordersToDelete.length).toBe(0);
|
|
2626
|
+
expect(subOrders.length).toBe(0);
|
|
2627
|
+
expect(newParentOrder.orders).toBe(undefined);
|
|
2628
|
+
});
|
|
2290
2629
|
});
|
|
@@ -5,12 +5,14 @@ module.exports = ({ modifierActions, _, actions }) => {
|
|
|
5
5
|
const isValid = (itemModifiers, modifier) => {
|
|
6
6
|
if (!modifierActions.findById(itemModifiers, modifier._id)) return false;
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
)
|
|
13
|
-
|
|
8
|
+
// 18-SEP-24 - Commenting out due to bug: https://app.clickup.com/t/86duna31h.
|
|
9
|
+
// This logic was preventing required Order Discounts to be removed from the items.
|
|
10
|
+
// if (
|
|
11
|
+
// modifierActions.isRequired(modifier) &&
|
|
12
|
+
// !modifierActions.isGroupOfValues(modifier) &&
|
|
13
|
+
// !modifierActions.isRequiredAndOverride(modifier)
|
|
14
|
+
// )
|
|
15
|
+
// return false;
|
|
14
16
|
|
|
15
17
|
if (
|
|
16
18
|
!modifierActions.isGroupOfItems(modifier) &&
|
package/lib/order/calculate.js
CHANGED
|
@@ -2,14 +2,18 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* return calculated Order
|
|
4
4
|
*/
|
|
5
|
-
module.exports = ({ _, itemActions, modifierActions }) =>
|
|
5
|
+
module.exports = ({ _, actions, itemActions, modifierActions }) =>
|
|
6
6
|
function calculateorder(inputOrder) {
|
|
7
7
|
if (!inputOrder) return inputOrder;
|
|
8
8
|
const order = _.cloneDeep(inputOrder);
|
|
9
|
-
const { items = [] } = order;
|
|
9
|
+
const { items = [], orders = [] } = order;
|
|
10
10
|
|
|
11
|
-
if (!items.length)
|
|
12
|
-
|
|
11
|
+
if (!items.length) {
|
|
12
|
+
if (!orders.length)
|
|
13
|
+
return { ...order, subTotals: {}, subTotal: 0, total: 0 };
|
|
14
|
+
|
|
15
|
+
return { ...order, ...actions.getTotals(order.orders) };
|
|
16
|
+
}
|
|
13
17
|
|
|
14
18
|
const sortedOrderModifiers = modifierActions.sort(
|
|
15
19
|
modifierActions.removeLocked(order.modifiers)
|
package/lib/order/merge.js
CHANGED
|
@@ -54,15 +54,6 @@ module.exports = ({ _, actions, utils }) => {
|
|
|
54
54
|
};
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
newOrder = actions.calculate(newOrder);
|
|
58
|
-
newOrder = actions.addNote({
|
|
59
|
-
order: newOrder,
|
|
60
|
-
message: `This order was merged with ${
|
|
61
|
-
mOrders.length === 2 ? ` order ` : ` orders `
|
|
62
|
-
} # ${mOrders.slice(1).map(order => ` ${order.displayId}`)}`,
|
|
63
|
-
user,
|
|
64
|
-
});
|
|
65
|
-
|
|
66
57
|
const ordersToDelete = [];
|
|
67
58
|
const voidedOrders = [];
|
|
68
59
|
for (let i = 1; i < mOrders.length; i += 1) {
|
|
@@ -89,8 +80,18 @@ module.exports = ({ _, actions, utils }) => {
|
|
|
89
80
|
parentId: newOrder._id,
|
|
90
81
|
customer: newOrder.customer,
|
|
91
82
|
}));
|
|
83
|
+
newOrder.orders = subOrders;
|
|
92
84
|
}
|
|
93
85
|
|
|
86
|
+
newOrder = actions.calculate(newOrder);
|
|
87
|
+
newOrder = actions.addNote({
|
|
88
|
+
order: newOrder,
|
|
89
|
+
message: `This order was merged with ${
|
|
90
|
+
mOrders.length === 2 ? ` order ` : ` orders `
|
|
91
|
+
} # ${mOrders.slice(1).map(order => ` ${order.displayId}`)}`,
|
|
92
|
+
user,
|
|
93
|
+
});
|
|
94
|
+
|
|
94
95
|
return [newOrder, voidedOrders, ordersToDelete, subOrders];
|
|
95
96
|
};
|
|
96
97
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/pricing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.37",
|
|
4
4
|
"description": "Pricing calculator",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"license": "ISC",
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
"supertest": "^6.2.3",
|
|
37
37
|
"supervisor": "^0.12.0"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "880122bbb47242484095cd2db3f68036c95c569a"
|
|
40
40
|
}
|