@darkpos/pricing 1.0.80 → 1.0.81
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__/modifier.test.js +334 -1
- package/lib/item/adjustCreditModifiersDifference.js +34 -0
- package/lib/item/adjustFixedModifiersDifference.js +37 -0
- package/lib/item/getItemsTotals.js +2 -2
- package/lib/item/getSubtotal.js +5 -0
- package/lib/item/index.js +14 -0
- package/lib/item/splitAndCalculate.js +18 -0
- package/lib/item/spreadModifiers.js +21 -0
- package/lib/item/validateCreditModifiersTotal.js +23 -0
- package/lib/item/validateFixedModifiersTotal.js +23 -0
- package/lib/modifier/getCreditModifiersTotalEntities.js +12 -0
- package/lib/modifier/getFixedModifiersTotalEntities.js +12 -0
- package/lib/modifier/getMaxItemQuantity.js +5 -0
- package/lib/modifier/getSplittedModifiers.js +15 -30
- package/lib/modifier/index.js +6 -0
- package/lib/order/index.js +2 -4
- package/lib/order/splitItems.js +50 -0
- package/lib/order/validateCreditModifiersTotal.js +1 -1
- package/lib/order/validateFixedModifiersTotal.js +1 -1
- package/package.json +2 -2
- package/lib/order/getCreditModifiersTotal.js +0 -15
- package/lib/order/getFixedModifiersTotal.js +0 -15
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
const usePricing = require('../index');
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const mockStores = require('./mocks/stores');
|
|
4
|
+
|
|
5
|
+
const session = {
|
|
6
|
+
store: mockStores[0],
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const pricingService = usePricing(session);
|
|
4
10
|
|
|
5
11
|
describe('Modifier actions', () => {
|
|
6
12
|
test('CU-86dv8hzxg modifier.isGroup fn', () => {
|
|
@@ -1334,4 +1340,331 @@ describe('Modifier actions', () => {
|
|
|
1334
1340
|
expect(error).toEqual('modifier.has.reached.the.maximum.amount.of.applies');
|
|
1335
1341
|
expect(currentOrder.items[3].modifiers.length).toBe(0);
|
|
1336
1342
|
});
|
|
1343
|
+
test('CU-86dve295v should split item based on modifier.maxItemQuantity', () => {
|
|
1344
|
+
const order = {
|
|
1345
|
+
id: 'ord-123',
|
|
1346
|
+
items: [],
|
|
1347
|
+
modifiers: [],
|
|
1348
|
+
};
|
|
1349
|
+
const modifier = {
|
|
1350
|
+
_id: 1,
|
|
1351
|
+
compute: {
|
|
1352
|
+
amount: 10,
|
|
1353
|
+
action: 'subtract',
|
|
1354
|
+
type: 'fixed',
|
|
1355
|
+
},
|
|
1356
|
+
properties: {
|
|
1357
|
+
limits: { maxItemQuantity: 1 },
|
|
1358
|
+
},
|
|
1359
|
+
modifierId: 'aaa',
|
|
1360
|
+
};
|
|
1361
|
+
|
|
1362
|
+
order.items.push({
|
|
1363
|
+
quantity: 10,
|
|
1364
|
+
itemId: '111',
|
|
1365
|
+
price: 100,
|
|
1366
|
+
modifiers: [],
|
|
1367
|
+
});
|
|
1368
|
+
|
|
1369
|
+
let currentOrder = { ...order };
|
|
1370
|
+
|
|
1371
|
+
currentOrder = pricingService.order.addItemModifier({
|
|
1372
|
+
order: currentOrder,
|
|
1373
|
+
modifier,
|
|
1374
|
+
itemIndex: 0,
|
|
1375
|
+
});
|
|
1376
|
+
|
|
1377
|
+
expect(currentOrder.items.length).toEqual(1);
|
|
1378
|
+
|
|
1379
|
+
currentOrder = pricingService.order.splitItems({
|
|
1380
|
+
order: currentOrder,
|
|
1381
|
+
});
|
|
1382
|
+
|
|
1383
|
+
expect(currentOrder.items.length).toEqual(10);
|
|
1384
|
+
});
|
|
1385
|
+
|
|
1386
|
+
test('CU-86dve295v should split item based on modifier.maxItemQuantity, and spread modifiers accordingly 1', () => {
|
|
1387
|
+
const order = {
|
|
1388
|
+
id: 'ord-123',
|
|
1389
|
+
items: [],
|
|
1390
|
+
modifiers: [],
|
|
1391
|
+
};
|
|
1392
|
+
const modifier = {
|
|
1393
|
+
_id: 1,
|
|
1394
|
+
compute: {
|
|
1395
|
+
amount: 10,
|
|
1396
|
+
action: 'subtract',
|
|
1397
|
+
type: 'fixed',
|
|
1398
|
+
},
|
|
1399
|
+
properties: {
|
|
1400
|
+
limits: { maxItemQuantity: 1 },
|
|
1401
|
+
},
|
|
1402
|
+
modifierId: 'aaa',
|
|
1403
|
+
};
|
|
1404
|
+
|
|
1405
|
+
let currentOrder = { ...order };
|
|
1406
|
+
|
|
1407
|
+
currentOrder = pricingService.order.addItem({
|
|
1408
|
+
order,
|
|
1409
|
+
item: {
|
|
1410
|
+
quantity: 10,
|
|
1411
|
+
itemId: '111',
|
|
1412
|
+
price: 100,
|
|
1413
|
+
modifiers: [],
|
|
1414
|
+
subTotals: {},
|
|
1415
|
+
},
|
|
1416
|
+
}).updatedOrder;
|
|
1417
|
+
|
|
1418
|
+
currentOrder = pricingService.order.addItemModifier({
|
|
1419
|
+
order: currentOrder,
|
|
1420
|
+
modifier,
|
|
1421
|
+
itemIndex: 0,
|
|
1422
|
+
});
|
|
1423
|
+
|
|
1424
|
+
expect(currentOrder.items.length).toEqual(1);
|
|
1425
|
+
|
|
1426
|
+
currentOrder = pricingService.order.calculate(currentOrder);
|
|
1427
|
+
|
|
1428
|
+
currentOrder = pricingService.order.splitItems({
|
|
1429
|
+
order: currentOrder,
|
|
1430
|
+
});
|
|
1431
|
+
|
|
1432
|
+
expect(currentOrder.items.length).toEqual(10);
|
|
1433
|
+
expect(currentOrder.total).toEqual(990);
|
|
1434
|
+
|
|
1435
|
+
expect(currentOrder.items[0].total).toEqual(99);
|
|
1436
|
+
expect(currentOrder.items[0].modifiers[0]._computed.amount).toEqual(-1);
|
|
1437
|
+
expect(currentOrder.items[1].total).toEqual(99);
|
|
1438
|
+
expect(currentOrder.items[1].modifiers[0]._computed.amount).toEqual(-1);
|
|
1439
|
+
expect(currentOrder.items[2].total).toEqual(99);
|
|
1440
|
+
expect(currentOrder.items[2].modifiers[0]._computed.amount).toEqual(-1);
|
|
1441
|
+
expect(currentOrder.items[3].total).toEqual(99);
|
|
1442
|
+
expect(currentOrder.items[3].modifiers[0]._computed.amount).toEqual(-1);
|
|
1443
|
+
expect(currentOrder.items[4].total).toEqual(99);
|
|
1444
|
+
expect(currentOrder.items[4].modifiers[0]._computed.amount).toEqual(-1);
|
|
1445
|
+
expect(currentOrder.items[5].total).toEqual(99);
|
|
1446
|
+
expect(currentOrder.items[5].modifiers[0]._computed.amount).toEqual(-1);
|
|
1447
|
+
expect(currentOrder.items[6].total).toEqual(99);
|
|
1448
|
+
expect(currentOrder.items[6].modifiers[0]._computed.amount).toEqual(-1);
|
|
1449
|
+
expect(currentOrder.items[7].total).toEqual(99);
|
|
1450
|
+
expect(currentOrder.items[7].modifiers[0]._computed.amount).toEqual(-1);
|
|
1451
|
+
expect(currentOrder.items[8].total).toEqual(99);
|
|
1452
|
+
expect(currentOrder.items[8].modifiers[0]._computed.amount).toEqual(-1);
|
|
1453
|
+
expect(currentOrder.items[9].total).toEqual(99);
|
|
1454
|
+
expect(currentOrder.items[9].modifiers[0]._computed.amount).toEqual(-1);
|
|
1455
|
+
});
|
|
1456
|
+
|
|
1457
|
+
test('CU-86dve295v should split item based on modifier.maxItemQuantity=2, and spread modifiers accordingly 2', () => {
|
|
1458
|
+
const order = {
|
|
1459
|
+
id: 'ord-456',
|
|
1460
|
+
items: [],
|
|
1461
|
+
modifiers: [],
|
|
1462
|
+
};
|
|
1463
|
+
const modifier = {
|
|
1464
|
+
_id: 2,
|
|
1465
|
+
compute: {
|
|
1466
|
+
amount: 5,
|
|
1467
|
+
action: 'subtract',
|
|
1468
|
+
type: 'fixed',
|
|
1469
|
+
},
|
|
1470
|
+
properties: {
|
|
1471
|
+
limits: { maxItemQuantity: 2 },
|
|
1472
|
+
},
|
|
1473
|
+
modifierId: 'bbb',
|
|
1474
|
+
};
|
|
1475
|
+
|
|
1476
|
+
let currentOrder = { ...order };
|
|
1477
|
+
|
|
1478
|
+
currentOrder = pricingService.order.addItem({
|
|
1479
|
+
order,
|
|
1480
|
+
item: {
|
|
1481
|
+
quantity: 8,
|
|
1482
|
+
itemId: '222',
|
|
1483
|
+
price: 50,
|
|
1484
|
+
modifiers: [],
|
|
1485
|
+
subTotals: {},
|
|
1486
|
+
},
|
|
1487
|
+
}).updatedOrder;
|
|
1488
|
+
|
|
1489
|
+
currentOrder = pricingService.order.addItemModifier({
|
|
1490
|
+
order: currentOrder,
|
|
1491
|
+
modifier,
|
|
1492
|
+
itemIndex: 0,
|
|
1493
|
+
});
|
|
1494
|
+
|
|
1495
|
+
currentOrder = pricingService.order.calculate(currentOrder);
|
|
1496
|
+
|
|
1497
|
+
expect(currentOrder.items.length).toEqual(1);
|
|
1498
|
+
|
|
1499
|
+
expect(currentOrder.total).toEqual(395);
|
|
1500
|
+
|
|
1501
|
+
currentOrder = pricingService.order.splitItems({
|
|
1502
|
+
order: currentOrder,
|
|
1503
|
+
});
|
|
1504
|
+
|
|
1505
|
+
expect(currentOrder.items.length).toEqual(4);
|
|
1506
|
+
expect(currentOrder.total).toEqual(395);
|
|
1507
|
+
|
|
1508
|
+
currentOrder.items.forEach(item => {
|
|
1509
|
+
expect(item.quantity).toEqual(2);
|
|
1510
|
+
expect(item.total).toEqual(98.75);
|
|
1511
|
+
expect(item.modifiers[0]._computed.amount).toEqual(-1.25);
|
|
1512
|
+
});
|
|
1513
|
+
});
|
|
1514
|
+
|
|
1515
|
+
test('CU-86dve295v should split 1 item and left other untouched.', () => {
|
|
1516
|
+
const order = {
|
|
1517
|
+
id: 'ord-456',
|
|
1518
|
+
items: [],
|
|
1519
|
+
modifiers: [],
|
|
1520
|
+
};
|
|
1521
|
+
const modifier = {
|
|
1522
|
+
_id: 2,
|
|
1523
|
+
compute: {
|
|
1524
|
+
amount: 5,
|
|
1525
|
+
action: 'subtract',
|
|
1526
|
+
type: 'fixed',
|
|
1527
|
+
},
|
|
1528
|
+
properties: {
|
|
1529
|
+
limits: { maxItemQuantity: 2 },
|
|
1530
|
+
},
|
|
1531
|
+
modifierId: 'bbb',
|
|
1532
|
+
};
|
|
1533
|
+
|
|
1534
|
+
const redModifier = {
|
|
1535
|
+
_id: 3,
|
|
1536
|
+
modifierId: 'red-ccc',
|
|
1537
|
+
name: 'red',
|
|
1538
|
+
};
|
|
1539
|
+
|
|
1540
|
+
let currentOrder = { ...order };
|
|
1541
|
+
|
|
1542
|
+
currentOrder = pricingService.order.addItem({
|
|
1543
|
+
order,
|
|
1544
|
+
item: {
|
|
1545
|
+
quantity: 8,
|
|
1546
|
+
itemId: '222',
|
|
1547
|
+
price: 50,
|
|
1548
|
+
modifiers: [],
|
|
1549
|
+
subTotals: {},
|
|
1550
|
+
},
|
|
1551
|
+
}).updatedOrder;
|
|
1552
|
+
|
|
1553
|
+
currentOrder = pricingService.order.addItemModifier({
|
|
1554
|
+
order: currentOrder,
|
|
1555
|
+
modifier,
|
|
1556
|
+
itemIndex: 0,
|
|
1557
|
+
});
|
|
1558
|
+
|
|
1559
|
+
currentOrder = pricingService.order.addItemModifier({
|
|
1560
|
+
order: currentOrder,
|
|
1561
|
+
modifier: redModifier,
|
|
1562
|
+
itemIndex: 0,
|
|
1563
|
+
});
|
|
1564
|
+
|
|
1565
|
+
currentOrder = pricingService.order.addItem({
|
|
1566
|
+
order: currentOrder,
|
|
1567
|
+
item: {
|
|
1568
|
+
quantity: 1,
|
|
1569
|
+
itemId: '333',
|
|
1570
|
+
price: 100,
|
|
1571
|
+
modifiers: [],
|
|
1572
|
+
subTotals: {},
|
|
1573
|
+
},
|
|
1574
|
+
}).updatedOrder;
|
|
1575
|
+
|
|
1576
|
+
currentOrder = pricingService.order.calculate(currentOrder);
|
|
1577
|
+
|
|
1578
|
+
expect(currentOrder.items.length).toEqual(2);
|
|
1579
|
+
|
|
1580
|
+
expect(currentOrder.total).toEqual(495);
|
|
1581
|
+
|
|
1582
|
+
currentOrder = pricingService.order.splitItems({
|
|
1583
|
+
order: currentOrder,
|
|
1584
|
+
});
|
|
1585
|
+
|
|
1586
|
+
expect(currentOrder.items.length).toEqual(5);
|
|
1587
|
+
expect(currentOrder.total).toEqual(495);
|
|
1588
|
+
|
|
1589
|
+
expect(currentOrder.items[0].total).toEqual(100);
|
|
1590
|
+
expect(currentOrder.items[0].modifiers.length).toBe(0);
|
|
1591
|
+
|
|
1592
|
+
expect(currentOrder.items[1].total).toEqual(98.75);
|
|
1593
|
+
expect(currentOrder.items[1].modifiers[0].name).toBe('red');
|
|
1594
|
+
expect(currentOrder.items[1].modifiers[1]._computed.amount).toBe(-1.25);
|
|
1595
|
+
|
|
1596
|
+
expect(currentOrder.items[2].total).toEqual(98.75);
|
|
1597
|
+
expect(currentOrder.items[2].modifiers[0].name).toBe('red');
|
|
1598
|
+
expect(currentOrder.items[2].modifiers[1]._computed.amount).toBe(-1.25);
|
|
1599
|
+
|
|
1600
|
+
expect(currentOrder.items[3].total).toEqual(98.75);
|
|
1601
|
+
expect(currentOrder.items[3].modifiers[0].name).toBe('red');
|
|
1602
|
+
expect(currentOrder.items[3].modifiers[1]._computed.amount).toBe(-1.25);
|
|
1603
|
+
|
|
1604
|
+
expect(currentOrder.items[4].total).toEqual(98.75);
|
|
1605
|
+
expect(currentOrder.items[4].modifiers[0].name).toBe('red');
|
|
1606
|
+
expect(currentOrder.items[4].modifiers[1]._computed.amount).toBe(-1.25);
|
|
1607
|
+
});
|
|
1608
|
+
|
|
1609
|
+
test('CU-86dve295v should split item based on modifier.maxItemQuantity=4, and spread modifiers accordingly', () => {
|
|
1610
|
+
const order = {
|
|
1611
|
+
id: 'ord-456',
|
|
1612
|
+
items: [],
|
|
1613
|
+
modifiers: [],
|
|
1614
|
+
};
|
|
1615
|
+
const modifier = {
|
|
1616
|
+
_id: 2,
|
|
1617
|
+
compute: {
|
|
1618
|
+
amount: 5,
|
|
1619
|
+
action: 'subtract',
|
|
1620
|
+
type: 'fixed',
|
|
1621
|
+
},
|
|
1622
|
+
properties: {
|
|
1623
|
+
limits: { maxItemQuantity: 4 },
|
|
1624
|
+
},
|
|
1625
|
+
modifierId: 'bbb',
|
|
1626
|
+
};
|
|
1627
|
+
|
|
1628
|
+
let currentOrder = { ...order };
|
|
1629
|
+
|
|
1630
|
+
currentOrder = pricingService.order.addItem({
|
|
1631
|
+
order,
|
|
1632
|
+
item: {
|
|
1633
|
+
quantity: 10,
|
|
1634
|
+
itemId: '222',
|
|
1635
|
+
price: 50,
|
|
1636
|
+
modifiers: [],
|
|
1637
|
+
subTotals: {},
|
|
1638
|
+
},
|
|
1639
|
+
}).updatedOrder;
|
|
1640
|
+
|
|
1641
|
+
currentOrder = pricingService.order.addItemModifier({
|
|
1642
|
+
order: currentOrder,
|
|
1643
|
+
modifier,
|
|
1644
|
+
itemIndex: 0,
|
|
1645
|
+
});
|
|
1646
|
+
|
|
1647
|
+
currentOrder = pricingService.order.calculate(currentOrder);
|
|
1648
|
+
|
|
1649
|
+
expect(currentOrder.items.length).toEqual(1);
|
|
1650
|
+
|
|
1651
|
+
expect(currentOrder.total).toEqual(495);
|
|
1652
|
+
|
|
1653
|
+
currentOrder = pricingService.order.splitItems({
|
|
1654
|
+
order: currentOrder,
|
|
1655
|
+
});
|
|
1656
|
+
|
|
1657
|
+
expect(currentOrder.items.length).toEqual(3);
|
|
1658
|
+
|
|
1659
|
+
expect(currentOrder.items[0].quantity).toEqual(4);
|
|
1660
|
+
expect(currentOrder.items[0].modifiers[0]._computed.amount).toEqual(-2);
|
|
1661
|
+
|
|
1662
|
+
expect(currentOrder.items[1].quantity).toEqual(4);
|
|
1663
|
+
expect(currentOrder.items[1].modifiers[0]._computed.amount).toEqual(-2);
|
|
1664
|
+
|
|
1665
|
+
expect(currentOrder.items[2].quantity).toEqual(2);
|
|
1666
|
+
expect(currentOrder.items[2].modifiers[0]._computed.amount).toEqual(-1);
|
|
1667
|
+
|
|
1668
|
+
expect(currentOrder.total).toEqual(495);
|
|
1669
|
+
});
|
|
1337
1670
|
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module.exports = ({ utils, actions, modifierActions }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
|
|
4
|
+
return function adjustCreditModifiersDifference({ subItems, difference }) {
|
|
5
|
+
const selectedSubItemIndex = subItems.findIndex(
|
|
6
|
+
subItem =>
|
|
7
|
+
modifierActions.hasCreditModifier(subItem.modifiers) &&
|
|
8
|
+
!actions.isFullyPaid(subItem) &&
|
|
9
|
+
subItem.total > difference
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
if (selectedSubItemIndex !== -1) {
|
|
13
|
+
const subItemToUpdate = {
|
|
14
|
+
...subItems[selectedSubItemIndex],
|
|
15
|
+
};
|
|
16
|
+
const modifierToUpdateIdx = subItemToUpdate.modifiers.findIndex(
|
|
17
|
+
mod => mod.type === 'credit'
|
|
18
|
+
);
|
|
19
|
+
const prevAmount =
|
|
20
|
+
subItemToUpdate.modifiers[modifierToUpdateIdx].properties.maxAmount;
|
|
21
|
+
|
|
22
|
+
subItemToUpdate.modifiers[modifierToUpdateIdx].properties.maxAmount =
|
|
23
|
+
math.add(prevAmount, difference);
|
|
24
|
+
|
|
25
|
+
subItems.splice(
|
|
26
|
+
selectedSubItemIndex,
|
|
27
|
+
1,
|
|
28
|
+
actions.calculate(subItemToUpdate)
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return subItems;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module.exports = ({ utils, actions, modifierActions, _ }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
const { getComputeModField } = require('../modifier/utils');
|
|
4
|
+
return function adjustFixedModifiersDifference({ subItems, difference }) {
|
|
5
|
+
const selectedSubItemIndex = subItems.findIndex(
|
|
6
|
+
subItem =>
|
|
7
|
+
modifierActions.hasFixedModifier(subItem.modifiers) &&
|
|
8
|
+
!actions.isFullyPaid(subItem) &&
|
|
9
|
+
subItem.total > difference
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
if (selectedSubItemIndex !== -1) {
|
|
13
|
+
const subItemToUpdate = {
|
|
14
|
+
...subItems[selectedSubItemIndex],
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const modifierToUpdateIdx = subItemToUpdate.modifiers.findIndex(
|
|
18
|
+
mod => mod.compute && mod.compute.type === 'fixed'
|
|
19
|
+
);
|
|
20
|
+
const prevCompute = getComputeModField(
|
|
21
|
+
subItemToUpdate.modifiers[modifierToUpdateIdx]
|
|
22
|
+
);
|
|
23
|
+
const prevAmount = _.get(prevCompute, 'compute.amount', 0);
|
|
24
|
+
if (modifierToUpdateIdx !== -1) {
|
|
25
|
+
subItemToUpdate.modifiers[modifierToUpdateIdx].compute.amount =
|
|
26
|
+
math.add(prevAmount, difference);
|
|
27
|
+
}
|
|
28
|
+
subItems.splice(
|
|
29
|
+
selectedSubItemIndex,
|
|
30
|
+
1,
|
|
31
|
+
actions.calculate(subItemToUpdate)
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return subItems;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-restricted-syntax */
|
|
2
2
|
|
|
3
|
-
module.exports = ({ utils }) => {
|
|
3
|
+
module.exports = ({ utils, actions }) => {
|
|
4
4
|
const { math } = utils;
|
|
5
5
|
return function getItemsTotals(items) {
|
|
6
6
|
const subTotals = {};
|
|
@@ -8,7 +8,7 @@ module.exports = ({ utils }) => {
|
|
|
8
8
|
let total = 0;
|
|
9
9
|
|
|
10
10
|
for (const item of items) {
|
|
11
|
-
subTotal = math.add(subTotal, item
|
|
11
|
+
subTotal = math.add(subTotal, actions.getSubtotal(item));
|
|
12
12
|
const keys = Object.keys(item.subTotals).filter(
|
|
13
13
|
key => !key.includes('_')
|
|
14
14
|
);
|
package/lib/item/index.js
CHANGED
|
@@ -52,6 +52,13 @@ const getAddModifiers = require('./getAddModifiers');
|
|
|
52
52
|
const getRelatedItems = require('./getRelatedItems');
|
|
53
53
|
const getRelatedModifiers = require('./getRelatedModifiers');
|
|
54
54
|
const unpickItem = require('./unpick');
|
|
55
|
+
const spreadModifiers = require('./spreadModifiers');
|
|
56
|
+
const splitAndCalculate = require('./splitAndCalculate');
|
|
57
|
+
const validateFixedModifiersTotal = require('./validateFixedModifiersTotal');
|
|
58
|
+
const adjustFixedModifiersDifference = require('./adjustFixedModifiersDifference');
|
|
59
|
+
const validateCreditModifiersTotal = require('./validateCreditModifiersTotal');
|
|
60
|
+
const adjustCreditModifiersDifference = require('./adjustCreditModifiersDifference');
|
|
61
|
+
const getSubtotal = require('./getSubtotal');
|
|
55
62
|
|
|
56
63
|
const itemActions = (deps = {}) => {
|
|
57
64
|
const actions = {};
|
|
@@ -117,6 +124,13 @@ const itemActions = (deps = {}) => {
|
|
|
117
124
|
getRelatedItems: getRelatedItems(innerDeps),
|
|
118
125
|
getRelatedModifiers: getRelatedModifiers(innerDeps),
|
|
119
126
|
unpickItem: unpickItem(innerDeps),
|
|
127
|
+
spreadModifiers: spreadModifiers(innerDeps),
|
|
128
|
+
splitAndCalculate: splitAndCalculate(innerDeps),
|
|
129
|
+
validateFixedModifiersTotal: validateFixedModifiersTotal(innerDeps),
|
|
130
|
+
adjustFixedModifiersDifference: adjustFixedModifiersDifference(innerDeps),
|
|
131
|
+
validateCreditModifiersTotal: validateCreditModifiersTotal(innerDeps),
|
|
132
|
+
adjustCreditModifiersDifference: adjustCreditModifiersDifference(innerDeps),
|
|
133
|
+
getSubtotal: getSubtotal(innerDeps),
|
|
120
134
|
});
|
|
121
135
|
|
|
122
136
|
Object.keys(freezedActions).forEach(actionName => {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module.exports = ({ actions, modifierActions }) =>
|
|
2
|
+
function splitAndCalculate({ parentItem, subItems }) {
|
|
3
|
+
if (!parentItem || !Array.isArray(subItems)) return [];
|
|
4
|
+
return subItems.map(each => {
|
|
5
|
+
const subTotal = actions.getSubtotal(actions.calculate(each));
|
|
6
|
+
const parentSubTotal = actions.getSubtotal(parentItem);
|
|
7
|
+
|
|
8
|
+
const newItem = {
|
|
9
|
+
...each,
|
|
10
|
+
modifiers: modifierActions.getSplittedModifiers(
|
|
11
|
+
parentItem.modifiers,
|
|
12
|
+
parentSubTotal,
|
|
13
|
+
subTotal
|
|
14
|
+
),
|
|
15
|
+
};
|
|
16
|
+
return actions.calculate(newItem);
|
|
17
|
+
});
|
|
18
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module.exports = ({ actions }) =>
|
|
2
|
+
function spreadModifiers({ parentItem, subItems }) {
|
|
3
|
+
let subItemsWithCalculatedModifiers = actions.splitAndCalculate({
|
|
4
|
+
parentItem,
|
|
5
|
+
subItems,
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
// Validation
|
|
9
|
+
|
|
10
|
+
subItemsWithCalculatedModifiers = actions.validateFixedModifiersTotal({
|
|
11
|
+
parentItem,
|
|
12
|
+
subItems: [...subItemsWithCalculatedModifiers],
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
subItemsWithCalculatedModifiers = actions.validateCreditModifiersTotal({
|
|
16
|
+
parentItem,
|
|
17
|
+
subItems: [...subItemsWithCalculatedModifiers],
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return subItemsWithCalculatedModifiers;
|
|
21
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module.exports = ({ utils, actions, modifierActions }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
|
|
4
|
+
return function validateCreditModifiersTotal({ parentItem, subItems }) {
|
|
5
|
+
const creditModifiersTotalAmount = modifierActions.getCreditModifiersTotal(
|
|
6
|
+
parentItem.modifiers
|
|
7
|
+
);
|
|
8
|
+
const subItemsCreditModifiersTotalAmount =
|
|
9
|
+
modifierActions.getCreditModifiersTotalEntities(subItems);
|
|
10
|
+
|
|
11
|
+
if (creditModifiersTotalAmount !== subItemsCreditModifiersTotalAmount) {
|
|
12
|
+
return actions.adjustCreditModifiersDifference({
|
|
13
|
+
subItems: [...subItems],
|
|
14
|
+
difference: math.sub(
|
|
15
|
+
creditModifiersTotalAmount,
|
|
16
|
+
subItemsCreditModifiersTotalAmount
|
|
17
|
+
),
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return subItems;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module.exports = ({ utils, actions, modifierActions }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
|
|
4
|
+
return function validateFixedModifiersTotal({ parentItem, subItems }) {
|
|
5
|
+
const fixedModifiersTotalAmount = modifierActions.getFixedModifiersTotal(
|
|
6
|
+
parentItem.modifiers
|
|
7
|
+
);
|
|
8
|
+
const subItemsFixedModifiersTotalAmount =
|
|
9
|
+
modifierActions.getFixedModifiersTotalEntities(subItems);
|
|
10
|
+
|
|
11
|
+
if (fixedModifiersTotalAmount !== subItemsFixedModifiersTotalAmount) {
|
|
12
|
+
return actions.adjustFixedModifiersDifference({
|
|
13
|
+
subItems: [...subItems],
|
|
14
|
+
difference: math.sub(
|
|
15
|
+
fixedModifiersTotalAmount,
|
|
16
|
+
subItemsFixedModifiersTotalAmount
|
|
17
|
+
),
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return subItems;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module.exports = ({ utils, actions }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
|
|
4
|
+
return function getCreditModifiersTotalEntities(entities) {
|
|
5
|
+
if (!Array.isArray(entities)) return 0;
|
|
6
|
+
return entities.reduce(
|
|
7
|
+
(acc, subEntity) =>
|
|
8
|
+
math.add(acc, actions.getCreditModifiersTotal(subEntity.modifiers)),
|
|
9
|
+
0
|
|
10
|
+
);
|
|
11
|
+
};
|
|
12
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module.exports = ({ utils, actions }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
|
|
4
|
+
return function getFixedModifiersTotalEntities(entities) {
|
|
5
|
+
if (!Array.isArray(entities)) return 0;
|
|
6
|
+
return entities.reduce(
|
|
7
|
+
(acc, subEntity) =>
|
|
8
|
+
math.add(acc, actions.getFixedModifiersTotal(subEntity.modifiers)),
|
|
9
|
+
0
|
|
10
|
+
);
|
|
11
|
+
};
|
|
12
|
+
};
|
|
@@ -3,28 +3,21 @@ module.exports = ({ utils }) => {
|
|
|
3
3
|
const { getComputeModField } = require('./utils');
|
|
4
4
|
|
|
5
5
|
const divideModifierAmount = ({
|
|
6
|
-
|
|
6
|
+
totalOrigin,
|
|
7
7
|
modifierAmount,
|
|
8
|
-
|
|
8
|
+
totalSplited,
|
|
9
9
|
}) =>
|
|
10
|
-
|
|
10
|
+
totalOrigin
|
|
11
11
|
? math.toDecimalPlaces(
|
|
12
|
-
math.div(
|
|
13
|
-
math.mul(modifierAmount, totalSplitedOrder || 0),
|
|
14
|
-
totalOriginOrder
|
|
15
|
-
)
|
|
12
|
+
math.div(math.mul(modifierAmount, totalSplited || 0), totalOrigin)
|
|
16
13
|
)
|
|
17
14
|
: 0;
|
|
18
15
|
|
|
19
|
-
const splitCreditModifier = ({
|
|
20
|
-
modifier,
|
|
21
|
-
totalOriginOrder,
|
|
22
|
-
totalSplitedOrder,
|
|
23
|
-
}) => {
|
|
16
|
+
const splitCreditModifier = ({ modifier, totalOrigin, totalSplited }) => {
|
|
24
17
|
const maxAmount = divideModifierAmount({
|
|
25
18
|
modifierAmount: modifier.properties.maxAmount,
|
|
26
|
-
|
|
27
|
-
|
|
19
|
+
totalOrigin,
|
|
20
|
+
totalSplited,
|
|
28
21
|
});
|
|
29
22
|
const compute = getComputeModField(modifier);
|
|
30
23
|
return {
|
|
@@ -39,15 +32,11 @@ module.exports = ({ utils }) => {
|
|
|
39
32
|
};
|
|
40
33
|
};
|
|
41
34
|
|
|
42
|
-
const splitFixedModifier = ({
|
|
43
|
-
modifier,
|
|
44
|
-
totalOriginOrder,
|
|
45
|
-
totalSplitedOrder,
|
|
46
|
-
}) => {
|
|
35
|
+
const splitFixedModifier = ({ modifier, totalOrigin, totalSplited }) => {
|
|
47
36
|
const amount = divideModifierAmount({
|
|
48
37
|
modifierAmount: modifier.compute.amount,
|
|
49
|
-
|
|
50
|
-
|
|
38
|
+
totalOrigin,
|
|
39
|
+
totalSplited,
|
|
51
40
|
});
|
|
52
41
|
return {
|
|
53
42
|
...modifier,
|
|
@@ -58,27 +47,23 @@ module.exports = ({ utils }) => {
|
|
|
58
47
|
};
|
|
59
48
|
};
|
|
60
49
|
|
|
61
|
-
return function getSplittedModifiers(
|
|
62
|
-
modifiers,
|
|
63
|
-
totalOriginOrder,
|
|
64
|
-
totalSplitedOrder
|
|
65
|
-
) {
|
|
50
|
+
return function getSplittedModifiers(modifiers, totalOrigin, totalSplited) {
|
|
66
51
|
return modifiers.map(each => {
|
|
67
52
|
const modifier = { ...each };
|
|
68
53
|
const compute = getComputeModField(modifier);
|
|
69
54
|
if (modifier.type === 'credit') {
|
|
70
55
|
return splitCreditModifier({
|
|
71
56
|
modifier,
|
|
72
|
-
|
|
73
|
-
|
|
57
|
+
totalOrigin,
|
|
58
|
+
totalSplited,
|
|
74
59
|
});
|
|
75
60
|
}
|
|
76
61
|
|
|
77
62
|
if (compute && compute.type === 'fixed' && compute.amount) {
|
|
78
63
|
return splitFixedModifier({
|
|
79
64
|
modifier,
|
|
80
|
-
|
|
81
|
-
|
|
65
|
+
totalOrigin,
|
|
66
|
+
totalSplited,
|
|
82
67
|
});
|
|
83
68
|
}
|
|
84
69
|
return modifier;
|
package/lib/modifier/index.js
CHANGED
|
@@ -54,6 +54,8 @@ const getSuggestion = require('./getSuggestion');
|
|
|
54
54
|
const getUnhiddenModifiers = require('./getUnhiddenModifiers');
|
|
55
55
|
const getUsingCount = require('./getUsingCount');
|
|
56
56
|
const getFixedModifiersTotal = require('./getFixedModifiersTotal');
|
|
57
|
+
const getFixedModifiersTotalEntities = require('./getFixedModifiersTotalEntities');
|
|
58
|
+
const getCreditModifiersTotalEntities = require('./getCreditModifiersTotalEntities');
|
|
57
59
|
const getCreditModifiersTotal = require('./getCreditModifiersTotal');
|
|
58
60
|
const hasAddModifiers = require('./hasAddModifiers');
|
|
59
61
|
const hasAllTag = require('./hasAllTag');
|
|
@@ -158,6 +160,7 @@ const validateItemNumber = require('./validateItemNumber');
|
|
|
158
160
|
const isLimits = require('./isLimits');
|
|
159
161
|
const isItemSet = require('./isItemSet');
|
|
160
162
|
const isOrderUseValid = require('./isOrderUseValid');
|
|
163
|
+
const getMaxItemQuantity = require('./getMaxItemQuantity');
|
|
161
164
|
|
|
162
165
|
const modifierActions = (deps = {}) => {
|
|
163
166
|
const actions = {};
|
|
@@ -329,6 +332,9 @@ const modifierActions = (deps = {}) => {
|
|
|
329
332
|
isLimits: isLimits(innerDeps),
|
|
330
333
|
isItemSet: isItemSet(innerDeps),
|
|
331
334
|
isOrderUseValid: isOrderUseValid(innerDeps),
|
|
335
|
+
getMaxItemQuantity: getMaxItemQuantity(innerDeps),
|
|
336
|
+
getFixedModifiersTotalEntities: getFixedModifiersTotalEntities(innerDeps),
|
|
337
|
+
getCreditModifiersTotalEntities: getCreditModifiersTotalEntities(innerDeps),
|
|
332
338
|
});
|
|
333
339
|
|
|
334
340
|
Object.keys(freezedActions).forEach(actionName => {
|
package/lib/order/index.js
CHANGED
|
@@ -73,8 +73,6 @@ const getAppliedCredit = require('./getAppliedCredit');
|
|
|
73
73
|
const addCreditModifier = require('./addCreditModifier');
|
|
74
74
|
const adjustCreditModifiersDifference = require('./adjustCreditModifiersDifference');
|
|
75
75
|
const adjustFixedModifiersDifference = require('./adjustFixedModifiersDifference');
|
|
76
|
-
const getCreditModifiersTotal = require('./getCreditModifiersTotal');
|
|
77
|
-
const getFixedModifiersTotal = require('./getFixedModifiersTotal');
|
|
78
76
|
const splitAndCalculate = require('./splitAndCalculate');
|
|
79
77
|
const spreadModifiers = require('./spreadModifiers');
|
|
80
78
|
const validateCreditModifiersTotal = require('./validateCreditModifiersTotal');
|
|
@@ -90,6 +88,7 @@ const getBalance = require('./getBalance');
|
|
|
90
88
|
const getLastLocation = require('./getLastLocation');
|
|
91
89
|
const getModifierRelations = require('./getModifierRelations');
|
|
92
90
|
const resetItem = require('./resetItem');
|
|
91
|
+
const splitItems = require('./splitItems');
|
|
93
92
|
|
|
94
93
|
const orderActions = (deps = {}) => {
|
|
95
94
|
const actions = {};
|
|
@@ -174,8 +173,6 @@ const orderActions = (deps = {}) => {
|
|
|
174
173
|
addCreditModifier: addCreditModifier(innerDeps),
|
|
175
174
|
adjustCreditModifiersDifference: adjustCreditModifiersDifference(innerDeps),
|
|
176
175
|
adjustFixedModifiersDifference: adjustFixedModifiersDifference(innerDeps),
|
|
177
|
-
getCreditModifiersTotal: getCreditModifiersTotal(innerDeps),
|
|
178
|
-
getFixedModifiersTotal: getFixedModifiersTotal(innerDeps),
|
|
179
176
|
splitAndCalculate: splitAndCalculate(innerDeps),
|
|
180
177
|
spreadModifiers: spreadModifiers(innerDeps),
|
|
181
178
|
validateCreditModifiersTotal: validateCreditModifiersTotal(innerDeps),
|
|
@@ -191,6 +188,7 @@ const orderActions = (deps = {}) => {
|
|
|
191
188
|
getLastLocation: getLastLocation(innerDeps),
|
|
192
189
|
getModifierRelations: getModifierRelations(innerDeps),
|
|
193
190
|
resetItem: resetItem(innerDeps),
|
|
191
|
+
splitItems: splitItems(innerDeps),
|
|
194
192
|
});
|
|
195
193
|
|
|
196
194
|
Object.keys(freezedActions).forEach(actionName => {
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module.exports = ({ actions, modifierActions, itemActions }) =>
|
|
2
|
+
function splitItems({ order }) {
|
|
3
|
+
if (!order || !order.items) return order;
|
|
4
|
+
|
|
5
|
+
const newItems = [];
|
|
6
|
+
let shouldRecalculate = false;
|
|
7
|
+
|
|
8
|
+
order.items.forEach(item => {
|
|
9
|
+
if (!item.modifiers) {
|
|
10
|
+
newItems.push(item);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const maxItemQuantityMod = item.modifiers.find(mod =>
|
|
15
|
+
modifierActions.getMaxItemQuantity(mod)
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
const maxQuantity =
|
|
19
|
+
modifierActions.getMaxItemQuantity(maxItemQuantityMod);
|
|
20
|
+
|
|
21
|
+
if (!maxQuantity || item.quantity <= maxQuantity) {
|
|
22
|
+
newItems.push(item);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let remaining = item.quantity;
|
|
27
|
+
let spreadedItems = [];
|
|
28
|
+
while (remaining > 0) {
|
|
29
|
+
if (!shouldRecalculate) shouldRecalculate = true;
|
|
30
|
+
const splitQty = Math.min(remaining, maxQuantity);
|
|
31
|
+
spreadedItems.push({
|
|
32
|
+
...item,
|
|
33
|
+
quantity: splitQty,
|
|
34
|
+
modifiers: [],
|
|
35
|
+
});
|
|
36
|
+
remaining -= splitQty;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
spreadedItems = itemActions.spreadModifiers({
|
|
40
|
+
parentItem: item,
|
|
41
|
+
subItems: spreadedItems,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
newItems.push(...spreadedItems);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return shouldRecalculate
|
|
48
|
+
? actions.calculate({ ...order, items: newItems })
|
|
49
|
+
: order;
|
|
50
|
+
};
|
|
@@ -6,7 +6,7 @@ module.exports = ({ utils, actions, modifierActions }) => {
|
|
|
6
6
|
parentOrder.modifiers
|
|
7
7
|
);
|
|
8
8
|
const subOrdersCreditModifiersTotalAmount =
|
|
9
|
-
|
|
9
|
+
modifierActions.getCreditModifiersTotalEntities(subOrders);
|
|
10
10
|
|
|
11
11
|
if (creditModifiersTotalAmount !== subOrdersCreditModifiersTotalAmount) {
|
|
12
12
|
return actions.adjustCreditModifiersDifference({
|
|
@@ -6,7 +6,7 @@ module.exports = ({ utils, actions, modifierActions }) => {
|
|
|
6
6
|
parentOrder.modifiers
|
|
7
7
|
);
|
|
8
8
|
const subOrdersFixedModifiersTotalAmount =
|
|
9
|
-
|
|
9
|
+
modifierActions.getFixedModifiersTotalEntities(subOrders);
|
|
10
10
|
|
|
11
11
|
if (fixedModifiersTotalAmount !== subOrdersFixedModifiersTotalAmount) {
|
|
12
12
|
return actions.adjustFixedModifiersDifference({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/pricing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.81",
|
|
4
4
|
"description": "Pricing calculator",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"license": "ISC",
|
|
@@ -51,5 +51,5 @@
|
|
|
51
51
|
"supertest": "^6.2.3",
|
|
52
52
|
"supervisor": "^0.12.0"
|
|
53
53
|
},
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "41babe2d2f61ea0fac42d1f76c1c98a3afde8507"
|
|
55
55
|
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
module.exports = ({ utils, modifierActions }) => {
|
|
2
|
-
const { math } = utils;
|
|
3
|
-
|
|
4
|
-
return function getCreditModifiersTotal(orders) {
|
|
5
|
-
if (!Array.isArray(orders)) return 0;
|
|
6
|
-
return orders.reduce(
|
|
7
|
-
(acc, subOrder) =>
|
|
8
|
-
math.add(
|
|
9
|
-
acc,
|
|
10
|
-
modifierActions.getCreditModifiersTotal(subOrder.modifiers)
|
|
11
|
-
),
|
|
12
|
-
0
|
|
13
|
-
);
|
|
14
|
-
};
|
|
15
|
-
};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
module.exports = ({ utils, modifierActions }) => {
|
|
2
|
-
const { math } = utils;
|
|
3
|
-
|
|
4
|
-
return function getFixedModifiersTotal(orders) {
|
|
5
|
-
if (!Array.isArray(orders)) return 0;
|
|
6
|
-
return orders.reduce(
|
|
7
|
-
(acc, subOrder) =>
|
|
8
|
-
math.add(
|
|
9
|
-
acc,
|
|
10
|
-
modifierActions.getFixedModifiersTotal(subOrder.modifiers)
|
|
11
|
-
),
|
|
12
|
-
0
|
|
13
|
-
);
|
|
14
|
-
};
|
|
15
|
-
};
|