@darkpos/pricing 1.0.95 → 1.0.97

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.
@@ -597,7 +597,6 @@ describe('Item actions', () => {
597
597
  },
598
598
  ],
599
599
  _id: '675354e939a47228afd1f199',
600
- properties: { basePrice: 13.5 },
601
600
 
602
601
  itemId: '62cdbfd01ee1b400193281ee',
603
602
  };
@@ -651,7 +650,6 @@ describe('Item actions', () => {
651
650
  },
652
651
  ],
653
652
  _id: '675354e939a47228afd1f199',
654
- properties: { basePrice: 13.5 },
655
653
 
656
654
  itemId: '62cdbfd01ee1b400193281ee',
657
655
  };
@@ -704,7 +702,6 @@ describe('Item actions', () => {
704
702
  quantity: 3,
705
703
  modifiers: [quantityOverrideMod],
706
704
  _id: '67535f34b4c5ea63d2d79640',
707
- properties: { basePrice: 13.5 },
708
705
  itemId: '62cdbfd01ee1b400193281ee',
709
706
  menuRuleId: '65660855dfffaf2da26fb870',
710
707
  __typename: 'OrderItem',
@@ -780,7 +777,6 @@ describe('Item actions', () => {
780
777
  },
781
778
  ],
782
779
  _id: '675354e939a47228afd1f199',
783
- properties: { basePrice: 13.5 },
784
780
 
785
781
  itemId: '62cdbfd01ee1b400193281ee',
786
782
  };
@@ -810,7 +806,7 @@ describe('Item actions', () => {
810
806
  name: "Men's 3pc Tuxedo",
811
807
  pieces: 3,
812
808
  total: 141.75,
813
- price: 135,
809
+ price: 13.5,
814
810
  quantity: 1,
815
811
  modifiers: [
816
812
  {
@@ -837,7 +833,6 @@ describe('Item actions', () => {
837
833
  },
838
834
  ],
839
835
  _id: '675360d5bc57ccecf751a5b5',
840
- properties: { basePrice: 13.5 },
841
836
  itemId: '62cdbfd01ee1b400193281ee',
842
837
  };
843
838
  const newItem = pricingService.item.calculate(item);
@@ -1161,9 +1156,6 @@ describe('Item actions', () => {
1161
1156
  modifiers: [groupMod],
1162
1157
  _id: '67e2cadd8dcf08ebcc5ef887',
1163
1158
  __typename: 'OrderItem',
1164
- properties: {
1165
- basePrice: 100,
1166
- },
1167
1159
  };
1168
1160
 
1169
1161
  const relatedItem1 = {
@@ -1259,9 +1251,6 @@ describe('Item actions', () => {
1259
1251
  modifiers: [groupMod, rawRelatedMod1],
1260
1252
  _id: '67e2cadd8dcf08ebcc5ef887',
1261
1253
  __typename: 'OrderItem',
1262
- properties: {
1263
- basePrice: 100,
1264
- },
1265
1254
  };
1266
1255
 
1267
1256
  const relatedItem1 = {
@@ -36,9 +36,7 @@ describe('Manual actions', () => {
36
36
  modifiers: [groupMod],
37
37
  _id: '67e2cadd8dcf08ebcc5ef887',
38
38
  __typename: 'OrderItem',
39
- properties: {
40
- basePrice: 100,
41
- },
39
+
42
40
  itemId: 'abcd123',
43
41
  };
44
42
 
@@ -265,7 +265,6 @@ test('getInvalidRequiredModifiers having related items', () => {
265
265
  parentItemId: '62cdbfd01ee1b4001932821a',
266
266
  parentId: '67dc63610c106ab7d481d7dd',
267
267
  includeParent: true,
268
- basePrice: 3,
269
268
  groupPath: ',67dc63610c106ab7d481d7de',
270
269
  },
271
270
  },
@@ -162,9 +162,7 @@ describe('Modifier actions', () => {
162
162
  itemId: '123',
163
163
  price: 100,
164
164
  modifiers: [],
165
- properties: {
166
- basePrice: 100,
167
- },
165
+ properties: {},
168
166
  });
169
167
 
170
168
  const conditionsBag = [];
@@ -467,7 +465,6 @@ describe('Modifier actions', () => {
467
465
  url: null,
468
466
  color: 'rgba(254,86,33,1)',
469
467
  iconName: 'ActivitySquare',
470
- basePrice: 213,
471
468
  },
472
469
  hasInventory: true,
473
470
  inventoryType: 'push',
@@ -1435,4 +1435,67 @@ describe('Order actions', () => {
1435
1435
  expect(resultedOrderPrepay.modifiers[1]._computed.amount).toBe(-1);
1436
1436
  expect(resultedOrderPrepay.modifiers[2]._computed.amount).toBe(-1);
1437
1437
  });
1438
+
1439
+ test('Should ignore modifier when conditions are not met and apply it when they are', () => {
1440
+ const cashDiscountModifier = {
1441
+ _id: '670e7cd236bf42091cb35624',
1442
+ attributes: [],
1443
+ modifierId: '670d7f7e746d1ae0f847aba6',
1444
+ _parentId: null,
1445
+ locked: false,
1446
+ name: 'CASH 2 USD DISCOUNT',
1447
+ sku: '',
1448
+ description: '',
1449
+ group: 'Offers',
1450
+ type: 'discount',
1451
+ tags: ['default', 'all'],
1452
+ conditions: {
1453
+ valid: false,
1454
+ rules: [
1455
+ {
1456
+ key: 'paymentTypes',
1457
+ value: ['cash'],
1458
+ operand: '$nin',
1459
+ },
1460
+ ],
1461
+ },
1462
+ compute: {
1463
+ type: 'fixed',
1464
+ action: 'subtract',
1465
+ amount: 2,
1466
+ },
1467
+ };
1468
+ const orderItem = {
1469
+ price: 10,
1470
+ quantity: 1,
1471
+ modifiers: [cashDiscountModifier],
1472
+ total: 10,
1473
+ totalPaid: 0,
1474
+ };
1475
+
1476
+ let cashFixedDiscountOrder = pricingService.order.calculate(
1477
+ { items: [orderItem] },
1478
+ { paymentType: 'cash' }
1479
+ );
1480
+
1481
+ expect(cashFixedDiscountOrder).toHaveProperty('total', 10);
1482
+ expect(cashFixedDiscountOrder).toHaveProperty('subTotal', 10);
1483
+ expect(cashFixedDiscountOrder).toHaveProperty('subTotals', {});
1484
+ expect(cashFixedDiscountOrder.items[0].modifiers).toHaveLength(1);
1485
+
1486
+ cashFixedDiscountOrder = pricingService.order.calculate(
1487
+ { items: [orderItem] },
1488
+ {
1489
+ paymentType: 'check',
1490
+ amountToPay: 1,
1491
+ lockPaymentModifiers: true,
1492
+ }
1493
+ );
1494
+ expect(cashFixedDiscountOrder).toHaveProperty('total', 8);
1495
+ expect(cashFixedDiscountOrder).toHaveProperty('subTotal', 10);
1496
+ expect(cashFixedDiscountOrder).toHaveProperty('subTotals', {
1497
+ discount: -2,
1498
+ });
1499
+ expect(cashFixedDiscountOrder.items[0].modifiers).toHaveLength(2);
1500
+ });
1438
1501
  });
@@ -3300,9 +3300,6 @@ describe('Order actions', () => {
3300
3300
  totalPaid: 0,
3301
3301
  notes: [],
3302
3302
  subTotals: {},
3303
- properties: {
3304
- basePrice: 0,
3305
- },
3306
3303
  };
3307
3304
 
3308
3305
  const item2 = {
@@ -3322,9 +3319,6 @@ describe('Order actions', () => {
3322
3319
  total: 12,
3323
3320
  totalPaid: 0,
3324
3321
  subTotals: {},
3325
- properties: {
3326
- basePrice: 12,
3327
- },
3328
3322
  };
3329
3323
 
3330
3324
  const order = {
@@ -3474,9 +3468,6 @@ describe('Order actions', () => {
3474
3468
  totalPaid: 0,
3475
3469
  notes: [],
3476
3470
  subTotals: {},
3477
- properties: {
3478
- basePrice: 0,
3479
- },
3480
3471
  };
3481
3472
 
3482
3473
  const item2 = {
@@ -3496,9 +3487,6 @@ describe('Order actions', () => {
3496
3487
  total: 12,
3497
3488
  totalPaid: 0,
3498
3489
  subTotals: {},
3499
- properties: {
3500
- basePrice: 12,
3501
- },
3502
3490
  };
3503
3491
 
3504
3492
  const order = {
@@ -3648,9 +3636,6 @@ describe('Order actions', () => {
3648
3636
  totalPaid: 0,
3649
3637
  notes: [],
3650
3638
  subTotals: {},
3651
- properties: {
3652
- basePrice: 0,
3653
- },
3654
3639
  _id: 1,
3655
3640
  };
3656
3641
 
@@ -58,6 +58,9 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
58
58
  startRequestDate,
59
59
  endRequestDate,
60
60
  allItems,
61
+ paymentMethod,
62
+ paymentType,
63
+ isPrepay,
61
64
  })
62
65
  );
63
66
 
@@ -66,19 +69,15 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
66
69
  ((each.compute && each.compute.type) ||
67
70
  (modifierActions.isOverride(each) &&
68
71
  !modifierActions.isNotesOverride(each))) &&
69
- modifierActions.isValid(each)
72
+ modifierActions.isValid(each) &&
73
+ !modifierActions.isPaymentModifier(each)
70
74
  );
71
75
 
72
76
  const paymentModifiersToCompute = validatedModifiers
73
77
  .filter(
74
78
  modifier =>
75
- modifierActions.isPaymentModifier(modifier) &&
76
- modifierActions.hasPaymentMethodType({
77
- paymentModifier: modifier,
78
- paymentMethod,
79
- paymentType,
80
- isPrepay,
81
- })
79
+ modifierActions.isValid(modifier) &&
80
+ modifierActions.isPaymentModifier(modifier)
82
81
  )
83
82
  .filter(paymentModifier => {
84
83
  const childModifiers = modifierActions.getChildren({
@@ -161,10 +160,7 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
161
160
  typeof item.totalPaid === 'number' && item.totalPaid > 0
162
161
  ? actions.getItemsBalance({ items: [item] })
163
162
  : math.sub(computedPrice, item.totalPaid),
164
- paymentMethod,
165
- paymentType,
166
163
  paymentId,
167
- isPrepay,
168
164
  });
169
165
  }
170
166
 
@@ -1,7 +1,5 @@
1
1
  module.exports = () =>
2
2
  function getBasePrice(item) {
3
3
  if (!item) return 0;
4
- if (!item.properties || typeof item.properties.basePrice !== 'number')
5
- return item.price;
6
- return item.properties.basePrice;
4
+ return item.price || 0;
7
5
  };
package/lib/item/index.js CHANGED
@@ -33,7 +33,6 @@ const isFullyPaid = require('./isFullyPaid');
33
33
  const getTotalPrice = require('./getTotalPrice');
34
34
  const getItems = require('./getItems');
35
35
  const getAmounts = require('./getAmounts');
36
- const hasPaymentMethodType = require('./hasPaymentMethodType');
37
36
  const removePaymentModifiersByPaymentId = require('./removePaymentModifiersByPaymentId');
38
37
  const hasPaymentModifierWithPaymentId = require('./hasPaymentModifierWithPaymentId');
39
38
  const getBalance = require('./getBalance');
@@ -111,7 +110,6 @@ const itemActions = (deps = {}) => {
111
110
  getTotalPrice: getTotalPrice(innerDeps),
112
111
  getItems: getItems(innerDeps),
113
112
  getAmounts: getAmounts(innerDeps),
114
- hasPaymentMethodType: hasPaymentMethodType(innerDeps),
115
113
  removePaymentModifiersByPaymentId:
116
114
  removePaymentModifiersByPaymentId(innerDeps),
117
115
  hasPaymentModifierWithPaymentId: hasPaymentModifierWithPaymentId(innerDeps),
@@ -1,7 +1,15 @@
1
1
  module.exports = ({ actions, utils }) => {
2
2
  const modifierConditionPass = (
3
3
  modifier,
4
- { item, startRequestDate, endRequestDate, allItems }
4
+ {
5
+ item,
6
+ startRequestDate,
7
+ endRequestDate,
8
+ allItems,
9
+ paymentMethod,
10
+ paymentType,
11
+ isPrepay,
12
+ }
5
13
  ) =>
6
14
  modifier.conditions && Array.isArray(modifier.conditions.rules)
7
15
  ? modifier.conditions.rules.every(condition => {
@@ -89,11 +97,17 @@ module.exports = ({ actions, utils }) => {
89
97
  condition.operand
90
98
  );
91
99
  case 'paymentMethods':
92
- return false;
100
+ return actions.validatePaymentCondition({
101
+ condition,
102
+ paymentValue: paymentMethod,
103
+ });
93
104
  case 'paymentTypes':
94
- return false;
105
+ return actions.validatePaymentCondition({
106
+ condition,
107
+ paymentValue: paymentType,
108
+ });
95
109
  case 'payment': {
96
- return false;
110
+ return !!isPrepay;
97
111
  }
98
112
  default:
99
113
  return false;
@@ -107,6 +121,9 @@ module.exports = ({ actions, utils }) => {
107
121
  endRequestDate = null,
108
122
  startRequestDate = null,
109
123
  allItems = null,
124
+ paymentMethod,
125
+ paymentType,
126
+ isPrepay,
110
127
  } = opts;
111
128
  return (
112
129
  modifier &&
@@ -115,6 +132,9 @@ module.exports = ({ actions, utils }) => {
115
132
  startRequestDate,
116
133
  endRequestDate,
117
134
  allItems,
135
+ paymentMethod,
136
+ paymentType,
137
+ isPrepay,
118
138
  })
119
139
  );
120
140
  }
@@ -5,20 +5,9 @@ module.exports = ({ actions, utils }) => {
5
5
  paymentModifier: paymentModifierParam,
6
6
  amountToPay: amountToPayParam,
7
7
  itemBalance: itemBalanceParam,
8
- paymentMethod,
9
- paymentType,
10
8
  paymentId,
11
- isPrepay,
12
9
  }) {
13
- if (
14
- !actions.hasPaymentMethodType({
15
- paymentModifier: paymentModifierParam,
16
- paymentMethod,
17
- paymentType,
18
- isPrepay,
19
- })
20
- )
21
- return undefined;
10
+ if (!actions.isValid(paymentModifierParam)) return false;
22
11
 
23
12
  const paymentModifier = {
24
13
  ...paymentModifierParam,
@@ -1,4 +1,4 @@
1
- module.exports = ({ actions, localization, _ }) =>
1
+ module.exports = ({ actions, localization, _, utils }) =>
2
2
  function createDescription({ modifier, price }) {
3
3
  const { _computed, compute, name } = modifier;
4
4
 
@@ -15,8 +15,11 @@ module.exports = ({ actions, localization, _ }) =>
15
15
  description = `${name}${localAmount ? ` (${localAmount})` : ''}`;
16
16
 
17
17
  if (actions.isPriceOverride(modifier)) {
18
+ const dividedPrice = localization.formatAmount(
19
+ price && compute.amount ? utils.math.div(price, compute.amount) : 0
20
+ );
18
21
  description = isMultiplier
19
- ? `${name} (${compute.amount} Unit @ ${localBasePrice}/Unit)`
22
+ ? `${name} (${compute.amount} Unit @ ${dividedPrice}/Unit)`
20
23
  : `${name} (${localization.formatAmount(compute.amount)}/Unit)`;
21
24
  }
22
25
 
@@ -126,7 +126,6 @@ const validateInArr = require('./validateInArr');
126
126
  const isPercentage = require('./isPercentage');
127
127
  const getChildren = require('./getChildren');
128
128
  const getComputedAmount = require('./getComputedAmount');
129
- const hasPaymentMethodType = require('./hasPaymentMethodType');
130
129
  const calculatePaymentModifier = require('./calculatePaymentModifier');
131
130
  const calculatePaymentDiscountModifier = require('./calculatePaymentDiscountModifier');
132
131
  const calculatePaymentFeeModifier = require('./calculatePaymentFeeModifier');
@@ -167,6 +166,7 @@ const getOverrideAmount = require('./getOverrideAmount');
167
166
  const getOverrideNote = require('./getOverrideNote');
168
167
  const isNotesOverride = require('./isNotesOverride');
169
168
  const isOverrideSubtotal = require('./isOverrideSubtotal');
169
+ const validatePaymentCondition = require('./validatePaymentCondition');
170
170
 
171
171
  const modifierActions = (deps = {}) => {
172
172
  const actions = {};
@@ -304,7 +304,6 @@ const modifierActions = (deps = {}) => {
304
304
  isPercentage: isPercentage(innerDeps),
305
305
  getChildren: getChildren(innerDeps),
306
306
  getComputedAmount: getComputedAmount(innerDeps),
307
- hasPaymentMethodType: hasPaymentMethodType(innerDeps),
308
307
  calculatePaymentModifier: calculatePaymentModifier(innerDeps),
309
308
  calculatePaymentDiscountModifier:
310
309
  calculatePaymentDiscountModifier(innerDeps),
@@ -347,6 +346,7 @@ const modifierActions = (deps = {}) => {
347
346
  getOverrideNote: getOverrideNote(innerDeps),
348
347
  isNotesOverride: isNotesOverride(innerDeps),
349
348
  isOverrideSubtotal: isOverrideSubtotal(innerDeps),
349
+ validatePaymentCondition: validatePaymentCondition(innerDeps),
350
350
  });
351
351
 
352
352
  Object.keys(freezedActions).forEach(actionName => {
@@ -0,0 +1,16 @@
1
+ module.exports = () =>
2
+ function validatePaymentCondition({ condition, paymentValue }) {
3
+ const { value, operand } = condition;
4
+ if (!value || !operand || !paymentValue) return false;
5
+
6
+ const includes = value.includes(paymentValue);
7
+
8
+ switch (operand) {
9
+ case '$in':
10
+ return includes;
11
+ case '$nin':
12
+ return !includes;
13
+ default:
14
+ return false;
15
+ }
16
+ };
@@ -80,8 +80,6 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
80
80
  order,
81
81
  modifier: _modifier,
82
82
  item: itemProp,
83
- customer,
84
- originalItem,
85
83
  onConditionsNotMet,
86
84
  onError,
87
85
  }) => {
@@ -115,13 +113,7 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
115
113
 
116
114
  item.properties = {
117
115
  ...(item.properties || {}),
118
- basePrice: item.price,
119
116
  };
120
- item.price = itemActions.getItemPrice({
121
- item,
122
- itemPriceLevels: originalItem ? originalItem.priceLevels : undefined,
123
- customer,
124
- });
125
117
 
126
118
  return item;
127
119
  };
@@ -1,4 +1,4 @@
1
- module.exports = ({ actions, modifierActions, itemActions, _ }) => {
1
+ module.exports = ({ actions, modifierActions, _ }) => {
2
2
  const removeModifier = ({ item, modifier }) => {
3
3
  const nextItem = { ...item, modifiers: [] };
4
4
 
@@ -29,7 +29,6 @@ module.exports = ({ actions, modifierActions, itemActions, _ }) => {
29
29
  return order;
30
30
 
31
31
  let item = actions.getSelectedItem({ order, itemIndex });
32
- const customer = actions.getCustomer(order);
33
32
 
34
33
  const contains = modifierActions.contains(item.modifiers, modifier);
35
34
 
@@ -42,11 +41,6 @@ module.exports = ({ actions, modifierActions, itemActions, _ }) => {
42
41
  originalItem,
43
42
  });
44
43
 
45
- item.price = itemActions.getItemPrice({
46
- item,
47
- itemPriceLevels: originalItem ? originalItem.priceLevels : undefined,
48
- customer,
49
- });
50
44
  order.items[itemIndex] = item;
51
45
 
52
46
  return order;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.95",
3
+ "version": "1.0.97",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -54,5 +54,5 @@
54
54
  "supertest": "^6.2.3",
55
55
  "supervisor": "^0.12.0"
56
56
  },
57
- "gitHead": "edb497d37801c9749d2c3722a67657abe77c9ab6"
57
+ "gitHead": "f0f3b0c934c2061d16a7237e6173d317dc15e811"
58
58
  }
@@ -1,23 +0,0 @@
1
- module.exports = ({ modifierActions }) =>
2
- function hasPaymentMethodType({
3
- item,
4
- paymentMethod,
5
- paymentType,
6
- isPrepay,
7
- }) {
8
- if (
9
- !item ||
10
- !Array.isArray(item.modifiers) ||
11
- (!paymentMethod && !paymentType)
12
- )
13
- return false;
14
-
15
- return item.modifiers.some(modifier =>
16
- modifierActions.hasPaymentMethodType({
17
- paymentModifier: modifier,
18
- paymentMethod,
19
- paymentType,
20
- isPrepay,
21
- })
22
- );
23
- };
@@ -1,29 +0,0 @@
1
- module.exports = ({ actions }) =>
2
- function hasPaymentMethodType({
3
- paymentModifier,
4
- paymentMethod,
5
- paymentType,
6
- isPrepay,
7
- }) {
8
- if (!actions.isPaymentModifier(paymentModifier)) return false;
9
-
10
- if (actions.isPrepayModifier(paymentModifier) && !isPrepay) return false;
11
-
12
- if (
13
- actions.isPaymentTypeModifier(paymentModifier) &&
14
- !paymentModifier.conditions.rules.some(rule =>
15
- rule.value.includes(paymentType)
16
- )
17
- )
18
- return false;
19
-
20
- if (
21
- actions.isPaymentMethodModifier(paymentModifier) &&
22
- !paymentModifier.conditions.rules.some(rule =>
23
- rule.value.includes(paymentMethod)
24
- )
25
- )
26
- return false;
27
-
28
- return true;
29
- };