@darkpos/pricing 1.0.9 → 1.0.12

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.
@@ -41,10 +41,14 @@ module.exports = ({ utils, modifierActions }) => {
41
41
  compute: {
42
42
  ...inheritedModifier.compute,
43
43
  amount: math.add(
44
- inheritedModifier.compute.amount,
45
- math.div(difference, selectedItem.quantity)
44
+ math.mul(inheritedModifier.compute.amount, selectedItem.quantity),
45
+ difference
46
46
  ),
47
47
  },
48
+ properties: {
49
+ ...inheritedModifier.properties,
50
+ ignoreQuantity: true,
51
+ },
48
52
  };
49
53
  }
50
54
  return itemsModifiers;
@@ -67,7 +67,9 @@ module.exports = ({ _, utils, modifierActions }) => {
67
67
  modifiers.push(_modifier);
68
68
  const { type, _computed } = _modifier;
69
69
  // console.log('computedPrice => ', _computed);
70
- let computedAmount = math.mul(_computed.amount, quantity);
70
+ let computedAmount = modifierActions.isIgnoreQuantity(_modifier)
71
+ ? _computed.amount
72
+ : math.mul(_computed.amount, quantity);
71
73
  // subscription modifiers have fixed amount and can't be multiplied by quantity
72
74
  if (modifierActions.isSubscription(_modifier))
73
75
  computedAmount = _computed.amount;
@@ -59,7 +59,7 @@ module.exports = ({ modifierActions, _, actions }) => {
59
59
  );
60
60
 
61
61
  const hasOverride = modifiersToApply.find(each =>
62
- modifierActions.isOverride(each)
62
+ modifierActions.isOverride(each) || modifierActions.isComputedOverride(each)
63
63
  );
64
64
 
65
65
  if (hasOverride) {
@@ -68,7 +68,7 @@ module.exports = ({ modifierActions, _, actions }) => {
68
68
 
69
69
  if (modifierActions.isQuantityOverride(modifier)) nextItem.quantity = 1;
70
70
 
71
- if (modifierActions.isPriceOverride(modifier))
71
+ if (modifierActions.isPriceOverride(modifier) || modifierActions.isComputedOverride(modifier))
72
72
  nextItem.price = actions.getItemPrice({
73
73
  item: originalItem,
74
74
  cache,
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Get calculated modifier
3
3
  */
4
- module.exports = ({ _, constants, utils, localization, actions }) => {
4
+ module.exports = ({ _, constants, utils, localization, actions }) => {
5
5
  const { math } = utils;
6
6
  const { Modifier } = constants;
7
7
 
@@ -29,7 +29,7 @@ module.exports = ({ _, constants, utils, localization, actions }) => {
29
29
  );
30
30
 
31
31
  if (!type || action === Modifier.Compute.Actions.OVERRIDE)
32
- _computed.amount = amount;
32
+ _computed.amount = 0;
33
33
 
34
34
  if (maxAmount && math.gt(math.abs(_computed.amount), maxAmount))
35
35
  _computed.amount = maxAmount;
@@ -85,6 +85,7 @@ const isService = require('./isService');
85
85
  const isSingleValue = require('./isSingleValue');
86
86
  const isGroupOfItems = require('./isGroupOfItems');
87
87
  const isOverride = require('./isOverride');
88
+ const isComputedOverride = require('./isComputedOverride');
88
89
  const isDepartment = require('./isDepartment');
89
90
  const isRequired = require('./isRequired');
90
91
  const isMultiplier = require('./isMultiplier');
@@ -98,6 +99,7 @@ const isCustomerTagsExtend = require('./isCustomerTagsExtend');
98
99
  const isAmountOverride = require('./isAmountOverride');
99
100
  const isDiscount = require('./isDiscount');
100
101
  const isHidden = require('./isHidden');
102
+ const isIgnoreQuantity = require('./isIgnoreQuantity');
101
103
  const isGratuity = require('./isGratuity');
102
104
  const isPaymentMethods = require('./isPaymentMethods');
103
105
  const isPaymentTypes = require('./isPaymentTypes');
@@ -207,6 +209,7 @@ const modifierActions = (deps = {}) => {
207
209
  isSingleValue: isSingleValue(innerDeps),
208
210
  isGroupOfItems: isGroupOfItems(innerDeps),
209
211
  isOverride: isOverride(innerDeps),
212
+ isComputedOverride: isComputedOverride(innerDeps),
210
213
  isDepartment: isDepartment(innerDeps),
211
214
  isRequired: isRequired(innerDeps),
212
215
  isMultiplier: isMultiplier(innerDeps),
@@ -219,6 +222,7 @@ const modifierActions = (deps = {}) => {
219
222
  isAmountOverride: isAmountOverride(innerDeps),
220
223
  isDiscount: isDiscount(innerDeps),
221
224
  isHidden: isHidden(innerDeps),
225
+ isIgnoreQuantity: isIgnoreQuantity(innerDeps),
222
226
  isGratuity: isGratuity(innerDeps),
223
227
  isPaymentMethods: isPaymentMethods(innerDeps),
224
228
  isPaymentTypes: isPaymentTypes(innerDeps),
@@ -0,0 +1,7 @@
1
+ module.exports = ({ actions, constants }) => {
2
+ const { Modifier } = constants;
3
+ return function isComputedOverride(modifier) {
4
+ const computeAction = modifier && modifier.compute && modifier.compute.action;
5
+ return computeAction === Modifier.Attributes.OVERRIDE;
6
+ };
7
+ };
@@ -0,0 +1,5 @@
1
+ module.exports = () =>
2
+ function isIgnoreQuantity(modifier) {
3
+ if (!modifier || !modifier.properties) return false;
4
+ return modifier.properties.ignoreQuantity;
5
+ };
@@ -76,7 +76,7 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
76
76
  // group of values use case
77
77
  if (
78
78
  modifierActions.isGroupOfValues(modifier) ||
79
- modifierActions.isRequiredAndOverride(modifier)
79
+ modifierActions.isOverride(modifier)
80
80
  ) {
81
81
  if (modifierIndex > -1) item.modifiers[modifierIndex] = modifierToAdd;
82
82
  else item.modifiers.push(modifierToAdd);
@@ -99,7 +99,7 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
99
99
  item.quantity = modifier.compute.amount || 1;
100
100
  }
101
101
 
102
- if (modifierActions.isPriceOverride(modifier)) {
102
+ if (modifierActions.isPriceOverride(modifier) || modifierActions.isComputedOverride(modifier)) {
103
103
  item.price = getPrice({ modifier, item });
104
104
  } else {
105
105
  item.price = itemActions.getItemPrice({
@@ -144,7 +144,7 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
144
144
  usingCount >= item.quantity &&
145
145
  !(
146
146
  modifierActions.isGroupOfValues(modifier) ||
147
- modifierActions.isRequiredAndOverride(modifier)
147
+ modifierActions.isOverride(modifier)
148
148
  )
149
149
  ) {
150
150
  // console.log('Removing MODIFIER ...');
@@ -1,27 +1,19 @@
1
1
  module.exports = ({ settings, _, moment }) => {
2
2
  const timezone = _.get(settings, 'localization.timezone', 'America/New_York');
3
- const isInSchedule = (each, date) => {
4
- const isoWeekday = moment(date).tz(timezone).isoWeekday();
5
- let check = false;
6
- if (each.day !== undefined && each.day !== null)
7
- check = String(each.day) === String(isoWeekday === 7 ? 0 : isoWeekday);
8
3
 
9
- if (each.date)
10
- check =
11
- moment(date).startOf('day').tz(timezone).format('YYYYMMDD') ===
12
- moment(each.date).startOf('day').tz(timezone).format('YYYYMMDD');
13
- return check;
14
- };
15
-
16
- const checkStoreSchedule = date => {
4
+ const getClosedDays = () => {
17
5
  const schedule = _.get(settings, 'schedule', {});
18
6
  const { close = [] } = schedule;
19
7
 
20
- if (close.length) return !close.find(each => isInSchedule(each, date));
8
+ if (!Array.isArray(close)) return [];
9
+ return close.map(closedDate => ({
10
+ isoWeekDay: moment(closedDate.date).tz(timezone).isoWeekday(),
11
+ date: moment(closedDate.date).tz(timezone)
12
+ }));
13
+ }
14
+
15
+ return function pickEndDate(schedule = {}) {
21
16
 
22
- return true;
23
- };
24
- return function pickEndDate(schedule = {}, skip = 0, date = undefined) {
25
17
  const {
26
18
  addDays = 1,
27
19
  hour,
@@ -30,41 +22,37 @@ module.exports = ({ settings, _, moment }) => {
30
22
  cutHour,
31
23
  cutDay = 1,
32
24
  } = schedule;
33
- let days = skip || Number(addDays);
34
25
 
35
- const todayTimezone = moment().tz(timezone);
36
- const dateTimezone = date || todayTimezone;
26
+ const todayTZ = moment().tz(timezone);
27
+ let endDateTZ = todayTZ.clone();
28
+
29
+ const closedDays = getClosedDays();
37
30
 
38
31
  if (
39
- !skip &&
40
- !date &&
41
32
  cutHour &&
42
- todayTimezone.isAfter(moment(schedule.cutHour, 'hh:mm:ss').tz(timezone))
33
+ todayTZ.isAfter(moment(cutHour, 'hh:mm:ss').tz(timezone))
43
34
  ) {
44
- console.log('cut day applied');
45
- days += cutDay;
35
+ endDateTZ = endDateTZ.add(cutDay, 'days')
46
36
  }
47
37
 
48
- const endDate = dateTimezone.add(Number(days), 'days');
49
- console.log('check date => ', endDate.format());
38
+ let addDaysCounter = 0;
50
39
 
51
- const isoWeekday = endDate.isoWeekday();
52
- console.log('isoWeekday => ', isoWeekday);
40
+ while (addDaysCounter < addDays) {
41
+ endDateTZ = endDateTZ.add(1, 'days');
42
+ const endDateISOWeekday = endDateTZ.isoWeekday() === 7 ? 0 : endDateTZ.isoWeekday();
43
+
44
+ if (!skipDays.includes(endDateISOWeekday) && !closedDays.some(closedDay => {
45
+ return closedDay.date.isSame(endDateTZ, 'day');
46
+ })) {
47
+ addDaysCounter++;
48
+ }
53
49
 
54
- if (skipDays && skipDays.includes(isoWeekday === 7 ? 0 : isoWeekday)) {
55
- console.log('check next day: in Skip days');
56
- return pickEndDate(schedule, 1, endDate);
57
- }
58
- if (!checkStoreSchedule(endDate)) {
59
- console.log('check next day: in Close days');
60
- return pickEndDate(schedule, 1, endDate);
61
50
  }
62
51
 
63
- if (hour !== undefined) endDate.set('hour', hour);
64
- if (minute !== undefined) endDate.set('minute', minute);
65
- endDate.set('second', 0);
52
+ if (hour !== undefined) endDateTZ.set('hour', hour);
53
+ if (minute !== undefined) endDateTZ.set('minute', minute);
54
+ endDateTZ.set('second', 0);
66
55
 
67
- console.log('final date in local', endDate.format());
68
- return moment.utc(endDate).format();
56
+ return moment.utc(endDateTZ).format();
69
57
  };
70
- };
58
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.9",
3
+ "version": "1.0.12",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -35,5 +35,5 @@
35
35
  "supertest": "^6.2.3",
36
36
  "supervisor": "^0.12.0"
37
37
  },
38
- "gitHead": "49aabd35419a731d175953158d83ff27206a9db7"
38
+ "gitHead": "c4950f95d5e9b9a8d9af97699a9f7f2acd1d35ca"
39
39
  }