@darkpos/pricing 1.0.15 → 1.0.17

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.
Files changed (66) hide show
  1. package/__TEST__/mocks/order-credit.json +474 -0
  2. package/__TEST__/mocks/partially-paid/input-items.json +1 -0
  3. package/__TEST__/mocks/partially-paid/order-modifiers.json +53 -0
  4. package/__TEST__/mocks/partially-paid/order-partially-paid.json +890 -0
  5. package/__TEST__/mocks/scripts/calculate-partially-paid/index.js +27 -0
  6. package/__TEST__/mocks/scripts/calculate-unpaid/index.js +28 -0
  7. package/__TEST__/mocks/scripts/order-to-string.js +18 -0
  8. package/__TEST__/mocks/stores.js +435 -0
  9. package/__TEST__/mocks/unpaid/input-items.json +6 -0
  10. package/__TEST__/mocks/unpaid/order-modifiers.json +53 -0
  11. package/__TEST__/mocks/unpaid/order-not-paid.json +684 -0
  12. package/__TEST__/order/order.test.js +627 -0
  13. package/lib/item/addIndirectModifier.js +2 -3
  14. package/lib/item/calculate.js +1 -2
  15. package/lib/item/getBalance.js +9 -0
  16. package/lib/item/getItemModifiers.js +3 -14
  17. package/lib/item/getItems.js +5 -0
  18. package/lib/item/getTotal.js +1 -1
  19. package/lib/item/getTotalPrice.js +10 -0
  20. package/lib/item/getTotals.js +31 -18
  21. package/lib/item/index.js +12 -0
  22. package/lib/item/isFullyPaid.js +6 -0
  23. package/lib/item/markModifiersAsLocked.js +11 -0
  24. package/lib/item/removeModifier.js +10 -9
  25. package/lib/item/removePaymentModifiers.js +15 -0
  26. package/lib/modifier/create.js +2 -2
  27. package/lib/modifier/createAmountOverrideModifier.js +1 -3
  28. package/lib/modifier/createCreditModifier.js +2 -4
  29. package/lib/modifier/createDiscountModifier.js +1 -3
  30. package/lib/modifier/createIndirectModifier.js +14 -5
  31. package/lib/modifier/createPaymentModifier.js +12 -0
  32. package/lib/modifier/createSubscriptionModifier.js +1 -3
  33. package/lib/modifier/duplicate.js +1 -1
  34. package/lib/modifier/findById.js +4 -1
  35. package/lib/modifier/findByPaymentMethod.js +1 -1
  36. package/lib/modifier/findByPaymentType.js +1 -1
  37. package/lib/modifier/getItemModifiers.js +1 -3
  38. package/lib/modifier/getLockedModifiers.js +5 -0
  39. package/lib/modifier/getSplittedModifiers.js +4 -6
  40. package/lib/modifier/hasItems.js +8 -0
  41. package/lib/modifier/index.js +20 -4
  42. package/lib/modifier/isFixed.js +10 -0
  43. package/lib/modifier/isFixedDiscount.js +4 -0
  44. package/lib/modifier/isPaymentMethodModifier.js +8 -0
  45. package/lib/modifier/isPaymentModifier.js +7 -0
  46. package/lib/modifier/isPaymentTypeModifier.js +8 -0
  47. package/lib/modifier/isSubtract.js +10 -0
  48. package/lib/modifier/removeLocked.js +8 -0
  49. package/lib/order/addItem.js +104 -106
  50. package/lib/order/addItemModifier.js +0 -3
  51. package/lib/order/addModifier.js +14 -2
  52. package/lib/order/addModifiers.js +15 -0
  53. package/lib/order/autoSplit.js +34 -0
  54. package/lib/order/calculate.js +46 -68
  55. package/lib/order/getScheduleByCustomer.js +0 -1
  56. package/lib/order/index.js +10 -0
  57. package/lib/order/markModifiersAsLocked.js +14 -0
  58. package/lib/order/removeItem.js +0 -2
  59. package/lib/order/removeModifier.js +5 -1
  60. package/lib/order/removeModifiers.js +18 -0
  61. package/lib/order/splitByDepartments.js +10 -13
  62. package/lib/order/syncSubOrderItemsFromParent.js +24 -0
  63. package/package.json +3 -2
  64. package/__TEST__/order.test.js +0 -233
  65. package/lib/modifier/isPaymentMethods.js +0 -8
  66. package/lib/modifier/isPaymentTypes.js +0 -8
@@ -127,126 +127,124 @@ module.exports = ({ actions, itemActions, modifierActions, settings, _ }) => {
127
127
  // Default modifiers
128
128
  if (idx === undefined && !_.isEmpty(item.modifiers))
129
129
  modifiersToAdd.push(
130
- ...item.modifiers.map(each => modifierActions.create(each))
130
+ ...item.modifiers.map(each => ({...each}))
131
131
  );
132
132
 
133
- const pendingItem = actions.getSelectedItem({ order, itemIndex });
134
-
135
- if (pendingItem && pendingItem.isPending && !combined) {
136
- idx = itemIndex;
137
- combined = !!pendingItem.itemId;
138
- orderItem.quantity = pendingItem.quantity;
139
- orderItem.isPending = !orderItem.itemId;
140
- // add inventory
141
- if (pendingItem.inventory) {
142
- const inventory = getInventory(pendingItem);
143
- if (inventory) {
144
- orderItem.inventory = inventory;
145
- if (inventory.serial) orderItem.serial = inventory.serial;
146
- const modifiers = modifierActions.getEntityModifiers(inventory);
147
- if (!_.isEmpty(modifiers)) modifiersToAdd.push(...modifiers);
148
- }
133
+ const pendingItem = actions.getSelectedItem({ order, itemIndex });
134
+
135
+ if (pendingItem && pendingItem.isPending && !combined) {
136
+ idx = itemIndex;
137
+ combined = !!pendingItem.itemId;
138
+ orderItem.quantity = pendingItem.quantity;
139
+ orderItem.isPending = !orderItem.itemId;
140
+ // add inventory
141
+ if (pendingItem.inventory) {
142
+ const inventory = getInventory(pendingItem);
143
+ if (inventory) {
144
+ orderItem.inventory = inventory;
145
+ if (inventory.serial) orderItem.serial = inventory.serial;
146
+ const modifiers = modifierActions.getEntityModifiers(inventory);
147
+ if (!_.isEmpty(modifiers)) modifiersToAdd.push(...modifiers);
149
148
  }
150
149
  }
150
+ }
151
151
 
152
- if (overridenQuantity > -1) {
153
- orderItem.quantity = overridenQuantity;
154
- }
155
-
156
- if (!combined) {
157
- // add price
158
- orderItem.price = itemActions.getItemPrice({
159
- cache,
160
- customer,
161
- item: orderItem,
162
- });
163
- if (itemActions.isRelatedItem(orderItem)) {
164
- // console.log('CHECKING Parent ...', orderItem.properties);
165
- const parent = itemActions.getParentItem(order.items, orderItem);
166
- // get inherited modifiers
167
- const inheritedModifiers = modifierActions.getInheritedModifiers({
168
- parent,
169
- child: item,
170
- });
171
- if (!_.isEmpty(inheritedModifiers))
172
- modifiersToAdd.push(...inheritedModifiers);
173
- // Remove parent if it is only a repair Item
174
- if (!itemActions.isParentIncluded(orderItem)) {
175
- order = actions.removeItem({ order, item: parent, hard: false });
176
- }
177
- }
178
- }
152
+ if (overridenQuantity > -1) {
153
+ orderItem.quantity = overridenQuantity;
154
+ }
179
155
 
180
- // add modifiers
181
- let requiredModifiers = modifierActions.getItemModifiers({
182
- modifiers: itemModifiers,
183
- customer,
156
+ if (!combined) {
157
+ // add price
158
+ orderItem.price = itemActions.getItemPrice({
184
159
  cache,
160
+ customer,
161
+ item: orderItem,
185
162
  });
186
- // Do not add again if they are already there
187
- if (
188
- !_.isEmpty(requiredModifiers) &&
189
- combined &&
190
- !_.isEmpty(orderItem.modifiers)
191
- )
192
- requiredModifiers = getUnselectedModifiers(orderItem, requiredModifiers);
193
- if (!_.isEmpty(requiredModifiers)) {
194
- modifiersToAdd.push(
195
- ...requiredModifiers.filter(
196
- reqMod =>
197
- !modifiersToAdd.find(
198
- modToAdd => modToAdd.modifierId === reqMod.modifierId
199
- )
200
- )
201
- );
163
+ if (itemActions.isRelatedItem(orderItem)) {
164
+ const parent = itemActions.getParentItem(order.items, orderItem);
165
+ // get inherited modifiers
166
+ const inheritedModifiers = modifierActions.getInheritedModifiers({
167
+ parent,
168
+ child: item,
169
+ });
170
+ if (!_.isEmpty(inheritedModifiers))
171
+ modifiersToAdd.push(...inheritedModifiers);
172
+ // Remove parent if it is only a repair Item
173
+ if (!itemActions.isParentIncluded(orderItem)) {
174
+ order = actions.removeItem({ order, item: parent, hard: false });
175
+ }
202
176
  }
177
+ }
203
178
 
204
- orderItem = actions.getOrderItemSubscriptions({
205
- order,
206
- item: orderItem,
207
- combined,
208
- cache,
209
- });
179
+ // add modifiers
180
+ let requiredModifiers = modifierActions.getItemModifiers({
181
+ modifiers: itemModifiers,
182
+ customer,
183
+ cache,
184
+ });
185
+ // Do not add again if they are already there
186
+ if (
187
+ !_.isEmpty(requiredModifiers) &&
188
+ combined &&
189
+ !_.isEmpty(orderItem.modifiers)
190
+ )
191
+ requiredModifiers = getUnselectedModifiers(orderItem, requiredModifiers);
192
+ if (!_.isEmpty(requiredModifiers)) {
193
+ modifiersToAdd.push(
194
+ ...requiredModifiers.filter(
195
+ reqMod =>
196
+ !modifiersToAdd.find(
197
+ modToAdd => modToAdd.modifierId === reqMod.modifierId
198
+ )
199
+ )
200
+ );
201
+ }
210
202
 
211
- const params = addOrderItem({
212
- order,
213
- item: orderItem,
214
- itemIndex: idx,
215
- });
216
- let [nextOrder, nextItemIndex] = params;
217
- const [, , nextItem] = params;
218
-
219
- if (modifiersToAdd.length) {
220
- // console.log('Adding Modifier to the item index ...', nextItemIndex);
221
- nextOrder = modifiersToAdd.reduce(
222
- (acc, modifier) =>
223
- actions.addItemModifier({
224
- itemIndex: nextItemIndex,
225
- order: acc,
226
- modifier,
227
- cache,
228
- }),
229
- nextOrder
230
- );
231
- }
203
+ orderItem = actions.getOrderItemSubscriptions({
204
+ order,
205
+ item: orderItem,
206
+ combined,
207
+ cache,
208
+ });
209
+
210
+ const params = addOrderItem({
211
+ order,
212
+ item: orderItem,
213
+ itemIndex: idx,
214
+ });
215
+ let [nextOrder, nextItemIndex] = params;
216
+ const [, , nextItem] = params;
217
+
218
+ if (modifiersToAdd.length) {
219
+ nextOrder = modifiersToAdd.reduce(
220
+ (acc, modifier) =>
221
+ actions.addItemModifier({
222
+ itemIndex: nextItemIndex,
223
+ order: acc,
224
+ modifier,
225
+ cache,
226
+ }),
227
+ nextOrder
228
+ );
229
+ }
232
230
 
233
- const { reArrangedOrder, newIndex } = reArrangeNewItem({
234
- nextOrder,
235
- nextItemIndex,
236
- });
231
+ const { reArrangedOrder, newIndex } = reArrangeNewItem({
232
+ nextOrder,
233
+ nextItemIndex,
234
+ });
237
235
 
238
- if (reArrangedOrder && newIndex) {
239
- nextOrder = { ...reArrangedOrder };
240
- nextItemIndex = newIndex;
241
- }
236
+ if (reArrangedOrder && newIndex) {
237
+ nextOrder = { ...reArrangedOrder };
238
+ nextItemIndex = newIndex;
239
+ }
242
240
 
243
- // Need to delete any pending Items if the current was combined
241
+ // Need to delete any pending Items if the current was combined
244
242
 
245
- if (combined && pendingItemIndex > -1 && idx !== itemIndex) {
246
- const idxToRemove = actions.getPendingItemIndex(nextOrder);
247
- nextOrder.items.splice(idxToRemove, 1);
248
- }
243
+ if (combined && pendingItemIndex > -1 && idx !== itemIndex) {
244
+ const idxToRemove = actions.getPendingItemIndex(nextOrder);
245
+ nextOrder.items.splice(idxToRemove, 1);
246
+ }
249
247
 
250
- return [nextOrder, nextItemIndex, nextItem];
251
- };
248
+ return [nextOrder, nextItemIndex, nextItem];
249
+ };
252
250
  };
@@ -135,7 +135,6 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
135
135
  modifierActions.isTrackUsageSubscription(modifier) &&
136
136
  !itemActions.hasCreateSubscription(order.items)
137
137
  ) {
138
- // console.log('Removing UsageTracker Modifier ...');
139
138
  return order;
140
139
  }
141
140
  // Remove if it has it already only if it is not group of value or override modifier
@@ -147,7 +146,6 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
147
146
  modifierActions.isOverride(modifier)
148
147
  )
149
148
  ) {
150
- // console.log('Removing MODIFIER ...');
151
149
  order = actions.removeItemModifier({
152
150
  order,
153
151
  modifier,
@@ -188,7 +186,6 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
188
186
  order.items[itemIndex] = item;
189
187
 
190
188
  if (modifierActions.hasRelatedItems(modifier)) {
191
- // console.log('Adding related Items ...', modifier.properties.group.items);
192
189
  order = modifier.properties.group.items.reduce((acc, each) => {
193
190
  const [nextOrder] = actions.addItem({
194
191
  item: {
@@ -1,9 +1,21 @@
1
- module.exports = ({ modifierActions }) =>
1
+ module.exports = ({ actions, modifierActions }) =>
2
2
  function addModifier({ order, modifier }) {
3
3
  if (!modifier) return order;
4
4
  const { modifiers = [] } = order;
5
+
6
+ if (modifierActions.isFixedDiscount(modifier)) {
7
+ const orderDue = actions.calculateDue(order);
8
+ if (modifier.compute.amount > orderDue) {
9
+ const err = new Error();
10
+ err.valid = false;
11
+ err.orderDue = orderDue;
12
+ err.message = 'discount.can.not.be.applied.order.due.is.this.amount';
13
+ throw err;
14
+ }
15
+ }
16
+
5
17
  return {
6
18
  ...order,
7
- modifiers: [...modifiers, modifierActions.create(modifier)],
19
+ modifiers: [...modifiers, { ...modifier }],
8
20
  };
9
21
  };
@@ -0,0 +1,15 @@
1
+ module.exports = ({ actions }) =>
2
+ function addModifiers({ order, modifiers }) {
3
+ if (!Array.isArray(modifiers) || modifiers.length === 0) return order;
4
+
5
+ return modifiers.reduce(
6
+ (nextOrder, modifier) =>
7
+ actions.addModifier({
8
+ modifier: {
9
+ ...modifier,
10
+ },
11
+ order: nextOrder,
12
+ }),
13
+ order
14
+ );
15
+ };
@@ -0,0 +1,34 @@
1
+ module.exports = ({ actions }) =>
2
+ function autoSplit({ parentOrder }) {
3
+ let subOrders = parentOrder.orders || [];
4
+ let newSubOrders = [];
5
+ const existingItems = subOrders
6
+ .map(subOrder => subOrder.items || [])
7
+ .flat();
8
+ const newItems = parentOrder.items.filter(item => {
9
+ const existingItem = existingItems.find(
10
+ eItem => String(eItem._id) === String(item._id)
11
+ );
12
+ return !existingItem;
13
+ });
14
+
15
+ if (newItems.length > 0) {
16
+ newSubOrders = actions.splitByDepartments({
17
+ ...parentOrder,
18
+ items: newItems,
19
+ });
20
+ }
21
+
22
+ // reassign displayIds
23
+ subOrders = [...subOrders, ...newSubOrders].map((subOrder, idx) => ({
24
+ ...subOrder,
25
+ displayId: `${parentOrder.displayId}-${idx + 1}`,
26
+ }));
27
+
28
+ subOrders = actions.spreadModifiers({
29
+ parentOrder,
30
+ subOrders,
31
+ });
32
+
33
+ return subOrders;
34
+ };
@@ -2,90 +2,68 @@
2
2
  /**
3
3
  * return calculated Order
4
4
  */
5
- module.exports = ({ _, utils, itemActions, modifierActions }) => {
6
- const { math } = utils;
7
-
8
- const pricing = items => {
9
- let subTotals = {};
10
- let subTotal = 0;
11
-
12
- for (const item of items) {
13
- subTotal = math.add(subTotal, item.subTotals._actual);
14
- const keys = Object.keys(item.subTotals).filter(
15
- key => !key.includes('_')
16
- );
17
- for (const key of keys) {
18
- subTotals[key] = math.add(subTotals[key] || 0, item.subTotals[key]);
19
- }
20
- }
21
-
22
- const total = Object.keys(subTotals).reduce(
23
- (acc, key) => math.add(acc, subTotals[key]),
24
- subTotal
25
- );
26
-
27
- subTotals = Object.keys(subTotals).reduce(
28
- (acc, key) => ({
29
- ...acc,
30
- [key]: subTotals[key],
31
- }),
32
- {}
33
- );
34
-
35
- return {
36
- subTotals,
37
- subTotal,
38
- total,
39
- };
40
- };
41
-
42
- const getItems = (items, orderModifiers) => {
43
- const existsInOrderModifier = modifier =>
44
- orderModifiers &&
45
- orderModifiers.find(each => each.modifierId === modifier.modifierId);
46
-
47
- return items.map(item => {
48
- const modifiers = (item.modifiers || []).filter(
49
- modifier => !existsInOrderModifier(modifier)
50
- );
51
- return {
52
- ...item,
53
- modifiers,
54
- };
55
- });
56
- };
57
-
58
- return function calculateorder(inputOrder) {
5
+ module.exports = ({ _, itemActions, modifierActions }) =>
6
+ function calculateorder(inputOrder) {
59
7
  if (!inputOrder) return inputOrder;
60
8
  const order = _.cloneDeep(inputOrder);
61
9
  const { items = [] } = order;
10
+
62
11
  if (!items.length)
63
12
  return { ...order, subTotals: {}, subTotal: 0, total: 0 };
64
13
 
65
- const modifiers = _.uniqBy(order.modifiers, '_id');
66
- // if there is no order level modifiers, we have to remove them from items
67
- let itemsWNIM = getItems(items, modifiers);
14
+ const orderModifiers = modifierActions.removeLocked(order.modifiers);
15
+
16
+ let itemsWNIM = items.map(item =>
17
+ itemActions.removeModifiers({ item, modifiers: orderModifiers })
18
+ );
19
+
68
20
  itemsWNIM = itemActions.calculate(itemsWNIM);
69
- const newOrder = { ...order, ...pricing(itemsWNIM), items: itemsWNIM };
70
21
 
71
- if (!modifiers.length) return newOrder;
22
+ const newOrder = {
23
+ ...order,
24
+ ...itemActions.getTotals(itemsWNIM),
25
+ items: itemsWNIM,
26
+ };
27
+
28
+ if (!orderModifiers.length) return newOrder;
72
29
 
73
30
  const newItems = itemsWNIM || items;
74
31
  let itemsWIM = _.cloneDeep(newItems);
75
32
 
76
33
  let computedTotal = newOrder.total;
77
- const sortedModifiers = modifierActions.sort(modifiers);
78
- for (const modifier of sortedModifiers) {
34
+
35
+ for (const modifier of orderModifiers) {
79
36
  const isCredit = modifierActions.isCredit(modifier);
37
+ let tempItems = newItems;
38
+
80
39
  if (isCredit) {
81
40
  const calculatedItemWIM = itemActions.calculate(itemsWIM);
82
- ({ total: computedTotal } = pricing(calculatedItemWIM));
41
+ computedTotal = itemActions.getTotals(calculatedItemWIM).total;
83
42
  }
84
- const itemsModifiers = itemActions.addIndirectModifier(
85
- computedTotal,
86
- newItems,
87
- modifier
43
+
44
+ const hasItems = modifierActions.hasItems({ modifier });
45
+
46
+ if (hasItems) {
47
+ tempItems = itemActions.getItems(tempItems, modifier.items);
48
+ }
49
+
50
+ tempItems = tempItems.map(tempItem =>
51
+ itemActions.removeModifiers({
52
+ item: tempItem,
53
+ modifiers: modifierActions.getLockedModifiers(tempItem.modifiers),
54
+ })
88
55
  );
56
+
57
+ tempItems = itemActions.calculate(tempItems);
58
+
59
+ computedTotal = itemActions.getTotals(tempItems).total;
60
+
61
+ const itemsModifiers = itemActions.addIndirectModifier({
62
+ orderTotal: computedTotal,
63
+ items: tempItems,
64
+ modifier,
65
+ });
66
+
89
67
  // adding the modifiers to items
90
68
  itemsWIM = itemsWIM.map(item => ({
91
69
  ...item,
@@ -96,10 +74,10 @@ module.exports = ({ _, utils, itemActions, modifierActions }) => {
96
74
  }
97
75
 
98
76
  const calculatedItemWIM = itemActions.calculate(itemsWIM);
77
+
99
78
  return {
100
79
  ...order,
101
- ...pricing(calculatedItemWIM),
80
+ ...itemActions.getTotals(calculatedItemWIM),
102
81
  items: calculatedItemWIM,
103
82
  };
104
83
  };
105
- };
@@ -14,7 +14,6 @@ module.exports = ({ _, settings }) => {
14
14
  .filter(each => each !== 'default' && each !== 'all')
15
15
  .find(each => scheduleTags.includes(each));
16
16
  if (!tag) return defaultSchedule;
17
- console.log('schedule for the tag', tag, schedules[tag]);
18
17
  return schedules[tag];
19
18
  };
20
19
  };
@@ -42,6 +42,8 @@ const holdOrder = require('./hold');
42
42
  const openOrder = require('./open');
43
43
  const isDetailed = require('./isDetailed');
44
44
  const splitByDepartments = require('./splitByDepartments');
45
+ const autoSplit = require('./autoSplit');
46
+ const syncSubOrderItemsFromParent = require('./syncSubOrderItemsFromParent');
45
47
  const createParent = require('./createParent');
46
48
  const getOrdersPieces = require('./getOrdersPieces');
47
49
  const getTotals = require('./getTotals');
@@ -80,6 +82,9 @@ const splitAndCalculate = require('./splitAndCalculate');
80
82
  const spreadModifiers = require('./spreadModifiers');
81
83
  const validateCreditModifiersTotal = require('./validateCreditModifiersTotal');
82
84
  const validateFixedModifiersTotal = require('./validateFixedModifiersTotal');
85
+ const markModifiersAsLocked = require('./markModifiersAsLocked');
86
+ const removeModifiers = require('./removeModifiers');
87
+ const addModifiers = require('./addModifiers');
83
88
 
84
89
  const orderActions = (deps = {}) => {
85
90
  const actions = {};
@@ -133,6 +138,8 @@ const orderActions = (deps = {}) => {
133
138
  openOrder: openOrder(innerDeps),
134
139
  isDetailed: isDetailed(innerDeps),
135
140
  splitByDepartments: splitByDepartments(innerDeps),
141
+ autoSplit: autoSplit(innerDeps),
142
+ syncSubOrderItemsFromParent: syncSubOrderItemsFromParent(innerDeps),
136
143
  createParent: createParent(innerDeps),
137
144
  getOrdersPieces: getOrdersPieces(innerDeps),
138
145
  getTotals: getTotals(innerDeps),
@@ -172,6 +179,9 @@ const orderActions = (deps = {}) => {
172
179
  spreadModifiers: spreadModifiers(innerDeps),
173
180
  validateCreditModifiersTotal: validateCreditModifiersTotal(innerDeps),
174
181
  validateFixedModifiersTotal: validateFixedModifiersTotal(innerDeps),
182
+ markModifiersAsLocked: markModifiersAsLocked(innerDeps),
183
+ removeModifiers: removeModifiers(innerDeps),
184
+ addModifiers: addModifiers(innerDeps),
175
185
  });
176
186
 
177
187
  Object.keys(freezedActions).forEach(actionName => {
@@ -0,0 +1,14 @@
1
+ module.exports = ({ itemActions }) =>
2
+ function markModifiersAsLocked({ order, modifierIds }) {
3
+ if (!order || !Array.isArray(order.modifiers)) return {};
4
+
5
+ return {
6
+ ...order,
7
+ modifiers: order.modifiers.map(mod =>
8
+ modifierIds.includes(mod._id) ? { ...mod, locked: true } : mod
9
+ ),
10
+ items: (order.items || []).map(item =>
11
+ itemActions.markModifiersAsLocked({ item, modifierIds })
12
+ ),
13
+ };
14
+ };
@@ -22,7 +22,6 @@ module.exports = ({ actions, _, itemActions }) => {
22
22
  items[orderItemIdx] = { ...orderItem, price: 0, modifiers: [] };
23
23
  if (hasRelatedItem) {
24
24
  items = updateRelatedItems(items, item._id);
25
- // console.log('Update related items ...', items);
26
25
  }
27
26
  }
28
27
  // Decrease the quantity
@@ -43,7 +42,6 @@ module.exports = ({ actions, _, itemActions }) => {
43
42
  const parent = itemActions.getParentItem(items, orderItem);
44
43
  const parentIndex = actions.getItemIndex({ order, item: parent });
45
44
  const siblings = actions.getRelatedItems({ order, item: parent });
46
- // console.log('Removing parent item ...');
47
45
  if (siblings <= 1) items.splice(parentIndex, 1);
48
46
  }
49
47
 
@@ -6,13 +6,17 @@ module.exports = ({ modifierActions, itemActions }) =>
6
6
  entity: order,
7
7
  modifier,
8
8
  });
9
+
9
10
  const nextOrder = { ...order };
10
11
 
11
12
  if (index > -1) {
12
13
  const { items, modifiers } = nextOrder;
13
14
  if (items)
14
15
  nextOrder.items = items.map(each =>
15
- itemActions.removeModifier({ item: each, modifier: modifiers[index] })
16
+ itemActions.removeModifier({
17
+ item: each,
18
+ modifier: modifiers[index],
19
+ })
16
20
  );
17
21
 
18
22
  nextOrder.modifiers.splice(index, 1);
@@ -0,0 +1,18 @@
1
+ module.exports = ({ actions }) =>
2
+ function removeModifiers({ order, modifiers }) {
3
+ let _order = {};
4
+
5
+ if (modifiers.length > 0)
6
+ _order = modifiers.reduce(
7
+ (nextOrder, modifier) =>
8
+ actions.removeModifier({
9
+ order: nextOrder,
10
+ modifier: {
11
+ ...modifier,
12
+ },
13
+ }),
14
+ order
15
+ );
16
+
17
+ return _order;
18
+ };
@@ -1,11 +1,4 @@
1
- module.exports = ({
2
- utils,
3
- _,
4
- actions,
5
- itemActions,
6
- modifierActions,
7
- settings,
8
- }) => {
1
+ module.exports = ({ utils, _, itemActions, modifierActions, settings }) => {
9
2
  const orderSettings = _.get(settings, 'order');
10
3
  const { date, helpers } = utils;
11
4
  const noteDefaults = {
@@ -59,6 +52,15 @@ module.exports = ({
59
52
  displayId,
60
53
  discount: 0,
61
54
  tax: 0,
55
+ totalPaid: 0,
56
+ total: 0,
57
+ status: {
58
+ order: 'open',
59
+ delivery: order.status.delivery,
60
+ detailed: order.status.detailed,
61
+ fullyPaid: false,
62
+ fullyPicked: false,
63
+ },
62
64
  parentId: orderSettings.allowSuborder ? order._id : null,
63
65
  items,
64
66
  modifiers: [],
@@ -120,11 +122,6 @@ module.exports = ({
120
122
 
121
123
  splitOrders = joinItemQuantity(splitOrders);
122
124
 
123
- splitOrders = actions.spreadModifiers({
124
- parentOrder: order,
125
- subOrders: splitOrders,
126
- });
127
-
128
125
  return splitOrders;
129
126
  };
130
127
  };
@@ -0,0 +1,24 @@
1
+ module.exports = () =>
2
+ function syncSubOrderItemsFromParent({ parentOrder }) {
3
+ const parentItems = parentOrder.items || [];
4
+ const subOrders = [...(parentOrder.orders || [])].map(subOrder => {
5
+ const items = subOrder.items
6
+ .map(cItem => {
7
+ const foundItem = parentItems.find(
8
+ pItem => String(pItem._id) === String(cItem._id)
9
+ );
10
+ return foundItem;
11
+ })
12
+ .filter(cItem => !!cItem);
13
+
14
+ return {
15
+ ...subOrder,
16
+ items,
17
+ };
18
+ });
19
+
20
+ return {
21
+ ...parentOrder,
22
+ orders: subOrders,
23
+ };
24
+ };