@darkpos/pricing 1.0.3 → 1.0.6

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 (123) hide show
  1. package/.eslintrc +22 -0
  2. package/README.md +110 -4
  3. package/__TEST__/item.test.js +285 -0
  4. package/__TEST__/order.test.js +234 -0
  5. package/index.js +1 -0
  6. package/lib/constants/index.js +5 -0
  7. package/lib/constants/status.js +39 -0
  8. package/lib/index.js +36 -9
  9. package/lib/item/addIndirectModifier.js +55 -0
  10. package/lib/item/calculate.js +108 -0
  11. package/lib/item/create.js +35 -0
  12. package/lib/item/filterByDirectModifiers.js +13 -0
  13. package/lib/item/findOriginalItem.js +8 -0
  14. package/lib/item/getBalanceToPay.js +12 -0
  15. package/lib/item/getDepartmentModifiers.js +5 -0
  16. package/lib/item/getDiscountModifiers.js +7 -0
  17. package/lib/item/getItemModifiers.js +16 -0
  18. package/lib/item/getItemModifiersDescription.js +22 -0
  19. package/lib/item/getItemPrice.js +57 -0
  20. package/lib/item/getItemsTotalPaid.js +9 -0
  21. package/lib/item/getItemsTotalPieces.js +7 -0
  22. package/lib/item/getNotIncludedModifiers.js +11 -0
  23. package/lib/item/getParentItem.js +5 -0
  24. package/lib/item/getPendingIndex.js +5 -0
  25. package/lib/item/getTotal.js +8 -0
  26. package/lib/item/getTotalPieces.js +7 -0
  27. package/lib/item/getTotals.js +25 -0
  28. package/lib/item/hasCreateSubscription.js +7 -0
  29. package/lib/item/hasModifier.js +15 -0
  30. package/lib/item/hasModifierWithValue.js +11 -0
  31. package/lib/item/hasModifiers.js +9 -0
  32. package/lib/item/hasSubscription.js +5 -0
  33. package/lib/item/index.js +80 -4
  34. package/lib/item/isParentIncluded.js +4 -0
  35. package/lib/item/isRelatedItem.js +4 -0
  36. package/lib/item/isSelected.js +13 -0
  37. package/lib/item/openRecommendationDialog.js +4 -0
  38. package/lib/item/removeModifier.js +84 -0
  39. package/lib/item/removeModifierById.js +8 -0
  40. package/lib/item/removeModifiers.js +14 -0
  41. package/lib/order/addCreditModifier.js +27 -0
  42. package/lib/order/addDiscountModifier.js +9 -0
  43. package/lib/order/addItem.js +292 -0
  44. package/lib/order/addItemModifier.js +214 -0
  45. package/lib/order/addModifier.js +9 -0
  46. package/lib/order/addModifierToAllItems.js +16 -0
  47. package/lib/order/addNote.js +25 -0
  48. package/lib/order/addTable.js +11 -0
  49. package/lib/order/calculate.js +105 -0
  50. package/lib/order/calculateDue.js +15 -0
  51. package/lib/order/calculateOrdersDue.js +26 -0
  52. package/lib/order/canSplit.js +4 -0
  53. package/lib/order/changeEndDate.js +15 -0
  54. package/lib/order/changeLocation.js +20 -0
  55. package/lib/order/changeStartDate.js +14 -0
  56. package/lib/order/combineSubOrders.js +14 -0
  57. package/lib/order/containsSerial.js +5 -0
  58. package/lib/order/create.js +67 -0
  59. package/lib/order/createParent.js +9 -0
  60. package/lib/order/duplicateItem.js +16 -0
  61. package/lib/order/findItemIndex.js +6 -0
  62. package/lib/order/getAppliedCredit.js +5 -0
  63. package/lib/order/getBalance.js +9 -0
  64. package/lib/order/getCustomer.js +4 -0
  65. package/lib/order/getCustomerStoreCredit.js +14 -0
  66. package/lib/order/getCustomerSubscriptions.js +8 -0
  67. package/lib/order/getCustomerSubscriptionsByItem.js +14 -0
  68. package/lib/order/getDepartmentModifiers.js +9 -0
  69. package/lib/order/getEndDate.js +6 -0
  70. package/lib/order/getItemByItemId.js +6 -0
  71. package/lib/order/getItemIndex.js +9 -0
  72. package/lib/order/getItemsToPay.js +17 -0
  73. package/lib/order/getMenuItemsByCustomer.js +19 -0
  74. package/lib/order/getNumberOfItems.js +12 -0
  75. package/lib/order/getOrdersPieces.js +10 -0
  76. package/lib/order/getPendingItemIndex.js +5 -0
  77. package/lib/order/getRelatedItems.js +11 -0
  78. package/lib/order/getSameItems.js +14 -0
  79. package/lib/order/getScheduleByCustomer.js +20 -0
  80. package/lib/order/getScreenCustomerNotes.js +12 -0
  81. package/lib/order/getSelectedItem.js +6 -0
  82. package/lib/order/getStartDate.js +4 -0
  83. package/lib/order/getTip.js +10 -0
  84. package/lib/order/getTotalPieces.js +17 -0
  85. package/lib/order/getTotals.js +28 -0
  86. package/lib/order/hasAggregate.js +20 -0
  87. package/lib/order/hasItem.js +7 -0
  88. package/lib/order/hasRemainingSubscription.js +67 -0
  89. package/lib/order/hold.js +12 -0
  90. package/lib/order/index.js +167 -4
  91. package/lib/order/isDetailed.js +4 -0
  92. package/lib/order/isHold.js +4 -0
  93. package/lib/order/isNew.js +4 -0
  94. package/lib/order/isOpen.js +4 -0
  95. package/lib/order/isParent.js +7 -0
  96. package/lib/order/merge.js +96 -0
  97. package/lib/order/open.js +11 -0
  98. package/lib/order/pickEndDate.js +70 -0
  99. package/lib/order/removeApplyModifier.js +24 -0
  100. package/lib/order/removeDiscountModifier.js +16 -0
  101. package/lib/order/removeItem.js +56 -0
  102. package/lib/order/removeItemByIndex.js +5 -0
  103. package/lib/order/removeItemModifier.js +69 -0
  104. package/lib/order/removeModifier.js +21 -0
  105. package/lib/order/removeModifierByAttribute.js +23 -0
  106. package/lib/order/removeModifiersWithPaymentMethods.js +29 -0
  107. package/lib/order/removeModifiersWithPaymentTypes.js +27 -0
  108. package/lib/order/setCustomer.js +50 -0
  109. package/lib/order/splitByDepartments.js +151 -0
  110. package/lib/order/toggleModifier.js +9 -0
  111. package/lib/order/updateItem.js +8 -0
  112. package/lib/order/updateItemQuantity.js +35 -0
  113. package/lib/order/void.js +11 -0
  114. package/package.json +20 -23
  115. package/lib/item/actions.js +0 -135
  116. package/lib/modifier/actions.js +0 -163
  117. package/lib/modifier/constants.js +0 -46
  118. package/lib/modifier/index.js +0 -8
  119. package/lib/order/actions.js +0 -47
  120. package/lib/utils/defaultPrices.js +0 -10
  121. package/lib/utils/helpers.js +0 -26
  122. package/lib/utils/index.js +0 -9
  123. package/lib/utils/math.js +0 -71
@@ -0,0 +1,11 @@
1
+ module.exports = () =>
2
+ function addTable({ order, table }) {
3
+ if (!order || !table) return order;
4
+ const nextOrder = { ...order };
5
+ const { location = {} } = nextOrder;
6
+ nextOrder.location = {
7
+ ...location,
8
+ table: { _id: table._id, name: table.name },
9
+ };
10
+ return nextOrder;
11
+ };
@@ -0,0 +1,105 @@
1
+ /* eslint-disable no-restricted-syntax */
2
+ /**
3
+ * return calculated Order
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) {
59
+ if (!inputOrder) return inputOrder;
60
+ const order = _.cloneDeep(inputOrder);
61
+ const { items = [] } = order;
62
+ if (!items.length)
63
+ return { ...order, subTotals: {}, subTotal: 0, total: 0 };
64
+
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);
68
+ itemsWNIM = itemActions.calculate(itemsWNIM);
69
+ const newOrder = { ...order, ...pricing(itemsWNIM), items: itemsWNIM };
70
+
71
+ if (!modifiers.length) return newOrder;
72
+
73
+ const newItems = itemsWNIM || items;
74
+ let itemsWIM = _.cloneDeep(newItems);
75
+
76
+ let computedTotal = newOrder.total;
77
+ const sortedModifiers = modifierActions.sort(modifiers);
78
+ for (const modifier of sortedModifiers) {
79
+ const isCredit = modifierActions.isCredit(modifier);
80
+ if (isCredit) {
81
+ const calculatedItemWIM = itemActions.calculate(itemsWIM);
82
+ ({ total: computedTotal } = pricing(calculatedItemWIM));
83
+ }
84
+ const itemsModifiers = itemActions.addIndirectModifier(
85
+ computedTotal,
86
+ newItems,
87
+ modifier
88
+ );
89
+ // adding the modifiers to items
90
+ itemsWIM = itemsWIM.map(item => ({
91
+ ...item,
92
+ modifiers: [...(item.modifiers || []), itemsModifiers[item._id]].filter(
93
+ Boolean
94
+ ),
95
+ }));
96
+ }
97
+
98
+ const calculatedItemWIM = itemActions.calculate(itemsWIM);
99
+ return {
100
+ ...order,
101
+ ...pricing(calculatedItemWIM),
102
+ items: calculatedItemWIM,
103
+ };
104
+ };
105
+ };
@@ -0,0 +1,15 @@
1
+ module.exports = ({ utils }) => {
2
+ const { math } = utils;
3
+ return function calculateDue(order) {
4
+ if (!order || !Array.isArray(order.items)) return 0;
5
+ let total = 0;
6
+ let totalPaid = 0;
7
+
8
+ order.items.forEach(item => {
9
+ total = math.add(total, item.total);
10
+ totalPaid = math.add(totalPaid, item.totalPaid);
11
+ });
12
+
13
+ return math.sub(total, totalPaid);
14
+ };
15
+ };
@@ -0,0 +1,26 @@
1
+ module.exports = ({ utils }) => {
2
+ const { math } = utils;
3
+ return function calculateOrdersDue(orders, orderItemsToPay) {
4
+ let total = 0;
5
+ let totalPaid = 0;
6
+ const isPartial = !!(
7
+ orderItemsToPay && Object.keys(orderItemsToPay).length > 0
8
+ );
9
+
10
+ orders.forEach(order => {
11
+ const orderItemIds =
12
+ (orderItemsToPay && orderItemsToPay[order._id]) || [];
13
+ order.items.forEach(it => {
14
+ if (!isPartial) {
15
+ total += it.total;
16
+ totalPaid += it.totalPaid;
17
+ } else if (orderItemIds.includes(it._id)) {
18
+ total += it.total;
19
+ totalPaid += it.totalPaid;
20
+ }
21
+ });
22
+ });
23
+
24
+ return math.sub(total, totalPaid);
25
+ };
26
+ };
@@ -0,0 +1,4 @@
1
+ module.exports = () =>
2
+ function canSplit(order) {
3
+ return !!(order && !(order.parentId || order.isParent));
4
+ };
@@ -0,0 +1,15 @@
1
+ module.exports = ({ utils }) =>
2
+ function changeEndDate({ date, order }) {
3
+ if (!date || !order) return order;
4
+ const nextOrder = { ...order };
5
+ if (!nextOrder.end) nextOrder.end = { __typename: 'OrderEnd' };
6
+
7
+ const isoString = utils.date.toISOString(date);
8
+ nextOrder.end = {
9
+ ...nextOrder.end,
10
+ actualDate: utils.date.isToday(date) ? isoString : '',
11
+ requestDate: isoString,
12
+ __typename: 'OrderEnd',
13
+ };
14
+ return nextOrder;
15
+ };
@@ -0,0 +1,20 @@
1
+ module.exports = () =>
2
+ function changeLocation({ order, location, isStart }) {
3
+ if (!order || !location) return order;
4
+ const nextOrder = { ...order };
5
+ if (isStart) {
6
+ if (!nextOrder.start) nextOrder.start = { __typename: 'OrderStart' };
7
+ nextOrder.start = {
8
+ ...nextOrder.start,
9
+ location,
10
+ };
11
+ return nextOrder;
12
+ }
13
+
14
+ if (!nextOrder.end) nextOrder.end = { __typename: 'OrderEnd' };
15
+ nextOrder.end = {
16
+ ...nextOrder.end,
17
+ location,
18
+ };
19
+ return nextOrder;
20
+ };
@@ -0,0 +1,14 @@
1
+ module.exports = ({ utils }) =>
2
+ function changeStartDate({ date, order }) {
3
+ if (!date || !order) return order;
4
+ const nextOrder = { ...order };
5
+ if (!nextOrder.start) nextOrder.start = { __typename: 'OrderStart' };
6
+
7
+ const isoString = utils.date.toISOString(date);
8
+ nextOrder.start = {
9
+ ...nextOrder.start,
10
+ actualDate: utils.date.isToday(date) ? isoString : '',
11
+ requestDate: isoString,
12
+ };
13
+ return nextOrder;
14
+ };
@@ -0,0 +1,14 @@
1
+ module.exports = () =>
2
+ function combineSubOrders({ order, subOrders = [] }) {
3
+ if (!order || !subOrders || !subOrders.length) return order;
4
+
5
+ const items = subOrders.reduce(
6
+ (arr, each) => arr.concat(each.items || []),
7
+ []
8
+ );
9
+ return {
10
+ ...order,
11
+ orders: subOrders,
12
+ items,
13
+ };
14
+ };
@@ -0,0 +1,5 @@
1
+ module.exports = () =>
2
+ function containsSerial({ order, serial }) {
3
+ if (!order || !order.items || !serial) return false;
4
+ return !!order.items.find(each => each.serial === serial);
5
+ };
@@ -0,0 +1,67 @@
1
+ module.exports = ({ utils, constants = {} }) => {
2
+ const { helpers } = utils;
3
+
4
+ const defaultOrderStatus = {
5
+ order: constants.status.Order.OPEN,
6
+ kitchen: constants.status.Kitchen.OPEN,
7
+ delivery: constants.status.Delivery.NONE,
8
+ detailed: true,
9
+ };
10
+ const orderEnd = {
11
+ actualDate: '',
12
+ requestDate: '',
13
+ location: null,
14
+ __typename: 'OrderEnd',
15
+ };
16
+ const orderStart = {
17
+ actualDate: utils.date.toISOString(),
18
+ requestDate: '',
19
+ location: null,
20
+ __typename: 'OrderStart',
21
+ };
22
+ const defaultOrder = {
23
+ get _id() {
24
+ return helpers.getObjectID();
25
+ },
26
+ parentId: null,
27
+ isParent: false,
28
+ subTotal: 0,
29
+ tax: 0,
30
+ modifiers: [],
31
+ discount: 0,
32
+ total: 0,
33
+ description: '',
34
+ displayId: '',
35
+ type: '',
36
+ location: {},
37
+ status: defaultOrderStatus,
38
+ attributes: [],
39
+ properties: {},
40
+ fee: 0,
41
+ user: null,
42
+ items: [],
43
+ notes: [],
44
+ customer: null,
45
+ start: orderStart,
46
+ end: orderEnd,
47
+ _storeId: '',
48
+ __typename: 'Order',
49
+ };
50
+
51
+ return function create(params = {}) {
52
+ return {
53
+ ...defaultOrder,
54
+ ...params,
55
+ };
56
+ };
57
+ };
58
+
59
+ /**
60
+ TODO: {
61
+ table: {
62
+ _id: locationState?.table._id,
63
+ name: locationState?.table.name,
64
+ },
65
+ type: location || LocationTypes.walkIn,
66
+ }
67
+ * */
@@ -0,0 +1,9 @@
1
+ module.exports = () =>
2
+ function createParent(order) {
3
+ if (!order) return order;
4
+ return {
5
+ ...order,
6
+ items: [],
7
+ isParent: true,
8
+ };
9
+ };
@@ -0,0 +1,16 @@
1
+ module.exports = ({ utils }) => {
2
+ const { helpers } = utils;
3
+ return function duplicateItem({ order, item }) {
4
+ if (!order || !order.items) return order;
5
+
6
+ const nextOrder = { ...order };
7
+ const newItem = {
8
+ ...item,
9
+ serial: '',
10
+ _id: helpers.getObjectID(),
11
+ };
12
+
13
+ nextOrder.items.push(newItem);
14
+ return nextOrder;
15
+ };
16
+ };
@@ -0,0 +1,6 @@
1
+ module.exports = () =>
2
+ function findItemIndex({ order, item }) {
3
+ if (!order || !item) return -1;
4
+ const { items = [] } = order;
5
+ return items.findIndex(each => each._id === item._id);
6
+ };
@@ -0,0 +1,5 @@
1
+ module.exports = ({ _ }) =>
2
+ function getAppliedCredit(order) {
3
+ if (!order) return 0;
4
+ return Math.abs(_.get(order, 'subTotals.credit', 0));
5
+ };
@@ -0,0 +1,9 @@
1
+ module.exports = ({ utils }) => {
2
+ const { math } = utils;
3
+ return function getBalance({ order }) {
4
+ if (!order) return 0;
5
+ if (Array.isArray(order)) return 0;
6
+
7
+ return math.sub(order.total, order.totalPaid);
8
+ };
9
+ };
@@ -0,0 +1,4 @@
1
+ module.exports = () =>
2
+ function getCustomer(order) {
3
+ return order && order.customer;
4
+ };
@@ -0,0 +1,14 @@
1
+ module.exports = ({ utils }) => {
2
+ const { math } = utils;
3
+ return function getCustomerStoreCredit(order) {
4
+ if (!order || !order.customer || !Array.isArray(order.customer.wallet))
5
+ return 0;
6
+ return order.customer.wallet
7
+ .filter(
8
+ each =>
9
+ each && each.provider === 'darkpos' && each.cards && each.cards.length
10
+ )
11
+ .reduce((acc, each) => [...acc, ...each.cards], [])
12
+ .reduce((acc, each) => math.add(acc, each.amount || 0), 0);
13
+ };
14
+ };
@@ -0,0 +1,8 @@
1
+ module.exports = ({ modifierActions }) =>
2
+ function getCustomerSubscriptions(customer) {
3
+ if (!customer || !Array.isArray(customer.modifiers)) return [];
4
+ const subscriptions = modifierActions.getSubscriptionModifiers(
5
+ customer.modifiers
6
+ );
7
+ return subscriptions;
8
+ };
@@ -0,0 +1,14 @@
1
+ module.exports = ({ actions, modifierActions }) =>
2
+ function getCustomerSubscriptionsByItem({ customer, item }) {
3
+ if (!customer || !item) return [];
4
+ const subscriptions = actions.getCustomerSubscriptions(customer);
5
+ return subscriptions.filter(each => {
6
+ const subscribedItem = modifierActions.getSubscriptionItem(each);
7
+
8
+ return (
9
+ subscribedItem &&
10
+ subscribedItem.itemId === item.itemId &&
11
+ (!subscribedItem.serial || subscribedItem.serial === item.serial)
12
+ );
13
+ });
14
+ };
@@ -0,0 +1,9 @@
1
+ module.exports = ({ itemActions }) =>
2
+ function getDepartmentModifiers(order) {
3
+ if (!order) return [];
4
+
5
+ return order.items.reduce(
6
+ (acc, item) => [...acc, ...itemActions.getDepartmentModifiers(item)],
7
+ []
8
+ );
9
+ };
@@ -0,0 +1,6 @@
1
+ module.exports = ({ utils }) => {
2
+ const { _ } = utils;
3
+ return function getEndDate({ order }) {
4
+ return _.get(order, 'end.requestDate', '');
5
+ };
6
+ };
@@ -0,0 +1,6 @@
1
+ module.exports = () =>
2
+ function getItemByItemId({ order = {}, itemId }) {
3
+ if (!order || !order.items) return null;
4
+ const item = order.items.find(each => each.itemId === itemId);
5
+ return item;
6
+ };
@@ -0,0 +1,9 @@
1
+ module.exports = () =>
2
+ function getItemIndex({ order, item }) {
3
+ if (!order || !order.items || !item) return -1;
4
+ return order.items.findIndex(each => {
5
+ if (!item.itemId && each.isPending) return item._id === each._id;
6
+
7
+ return each._id === item._id && each.itemId === item.itemId;
8
+ });
9
+ };
@@ -0,0 +1,17 @@
1
+ module.exports = () =>
2
+ function getItemsToPay({ orders, orderItemsToPay }) {
3
+ return (
4
+ (Array.isArray(orders) &&
5
+ orderItemsToPay &&
6
+ Object.keys(orderItemsToPay).reduce((acc, orderId) => {
7
+ const orderItemsIds = orderItemsToPay[orderId];
8
+ const order = orders.find(each => each._id === orderId);
9
+ if (!order || !orderItemsIds) return acc;
10
+ const items = order.items.filter(each =>
11
+ orderItemsIds.includes(each._id)
12
+ );
13
+ return acc.concat(items);
14
+ }, [])) ||
15
+ []
16
+ );
17
+ };
@@ -0,0 +1,19 @@
1
+ module.exports = ({ modifierActions }) =>
2
+ function getMenuItemByCustomer({ order, menuItems }) {
3
+ const { customer } = order || {};
4
+ if (!customer) return [];
5
+
6
+ return menuItems.filter(menuItem => {
7
+ const { item = {} } = menuItem;
8
+ const { priceLevels = [] } = item;
9
+ if (
10
+ !priceLevels.length &&
11
+ (modifierActions.hasAllTag(customer) ||
12
+ modifierActions.hasNoTags(customer))
13
+ )
14
+ return true;
15
+ return priceLevels.some(priceLevel =>
16
+ modifierActions.hasMatchTags(customer, priceLevel)
17
+ );
18
+ });
19
+ };
@@ -0,0 +1,12 @@
1
+ module.exports = ({ utils }) => {
2
+ const { _ } = utils;
3
+ return function getNumberOfItems(orders) {
4
+ if (!orders || !Array.isArray(orders)) return 0;
5
+ const sum = orders.reduce((acc, order) => {
6
+ const items = _.get(order, 'items', []);
7
+ return acc + items.lenght;
8
+ }, 0);
9
+
10
+ return sum;
11
+ };
12
+ };
@@ -0,0 +1,10 @@
1
+ module.exports = ({ actions, utils }) => {
2
+ const { math } = utils;
3
+ return function getOrdersPieces(orders) {
4
+ if (!Array.isArray(orders)) return 0;
5
+ return orders.reduce(
6
+ (acc, order) => math.add(acc, actions.getTotalPieces({ order })),
7
+ 0
8
+ );
9
+ };
10
+ };
@@ -0,0 +1,5 @@
1
+ module.exports = () =>
2
+ function getPendingItemIndex(order) {
3
+ if (!order || !order.items || !order.items.length) return -1;
4
+ return order.items.findIndex(each => each.isPending);
5
+ };
@@ -0,0 +1,11 @@
1
+ module.exports = () =>
2
+ function getRelatedItems({ order, item, compareId = true }) {
3
+ if (!order || !order.items || !item) return [];
4
+ return order.items
5
+ .filter(each => each.properties && each.properties.relatedItem)
6
+ .filter(
7
+ each =>
8
+ each.properties.parentItemId === item.itemId &&
9
+ (compareId ? each.properties.parentId === item._id : true)
10
+ );
11
+ };
@@ -0,0 +1,14 @@
1
+ module.exports = () =>
2
+ function getSameItems({ order, item }) {
3
+ const { items = [] } = order;
4
+ return items.filter(each => {
5
+ let check = each.itemId === item._id && !each.serial && !item.serial;
6
+ if (check && each.menuId && item.menuId) {
7
+ check =
8
+ each.menuId === item.menuId &&
9
+ each.path.toString() === item.path.toString();
10
+ }
11
+
12
+ return check;
13
+ });
14
+ };
@@ -0,0 +1,20 @@
1
+ module.exports = ({ _, settings }) => {
2
+ const orderSettings = _.get(settings, 'order');
3
+
4
+ return function getScheduleByCustomer(customer = {}) {
5
+ const { tags } = customer;
6
+ const { default: defaultSchedule, ...schedules } =
7
+ orderSettings.endDate || {};
8
+ if (!tags || !tags.length) return defaultSchedule;
9
+
10
+ const scheduleTags = Object.keys(schedules);
11
+ if (!scheduleTags || !scheduleTags.length) return defaultSchedule;
12
+
13
+ const tag = tags
14
+ .filter(each => each !== 'default' && each !== 'all')
15
+ .find(each => scheduleTags.includes(each));
16
+ if (!tag) return defaultSchedule;
17
+ console.log('schedule for the tag', tag, schedules[tag]);
18
+ return schedules[tag];
19
+ };
20
+ };
@@ -0,0 +1,12 @@
1
+ module.exports = ({ actions }) =>
2
+ function getScreenCustomerNotes(customer) {
3
+ let theCustomer = customer;
4
+ if (!theCustomer) theCustomer = actions.getCustomer();
5
+ if (!theCustomer) return [];
6
+ const { notes = [] } = theCustomer;
7
+ const screenNotes = notes.filter(
8
+ each => each.message && each.attributes.includes('screen')
9
+ );
10
+ if (!screenNotes.length) return [];
11
+ return screenNotes.map(each => each.message);
12
+ };
@@ -0,0 +1,6 @@
1
+ module.exports = () =>
2
+ function getSelectedItem({ order, itemIndex }) {
3
+ if (!order || !order.items || itemIndex < 0) return null;
4
+ const item = order.items[itemIndex];
5
+ return item;
6
+ };
@@ -0,0 +1,4 @@
1
+ module.exports = ({ _ }) =>
2
+ function getStartDate({ order }) {
3
+ return _.get(order, 'start.actualDate', '');
4
+ };
@@ -0,0 +1,10 @@
1
+ module.exports = ({ modifierActions, utils }) => {
2
+ const { math } = utils;
3
+ return function getTip(order) {
4
+ if (!order || !Array.isArray(order.modifiers)) return 0;
5
+
6
+ return order.modifiers
7
+ .filter(each => modifierActions.isGratuity(each))
8
+ .reduce((total, modifier) => math.sum(total, modifier.number || 0), 0);
9
+ };
10
+ };
@@ -0,0 +1,17 @@
1
+ module.exports = ({ itemActions, utils }) => {
2
+ const { math } = utils;
3
+ return function getTotalPieces({ order, subOrders }) {
4
+ if (!order) return 0;
5
+ if (order.items && order.items.length)
6
+ return itemActions.getItemsTotalPieces(order.items);
7
+ if (order.isParent && subOrders && subOrders.length)
8
+ return subOrders.reduce(
9
+ (acc, each) =>
10
+ math.add(acc, itemActions.getItemsTotalPieces(each.items)),
11
+ 0
12
+ );
13
+ if (order.properties && order.properties.pieceCount)
14
+ return order.properties.pieceCount;
15
+ return 0;
16
+ };
17
+ };
@@ -0,0 +1,28 @@
1
+ module.exports = ({ utils }) => {
2
+ const { math } = utils;
3
+ return function getTotals(orders) {
4
+ if (!Array.isArray(orders)) return { total: 0, subTotals: {} };
5
+ return orders
6
+ .filter(each => !!each.parentId || !each.isParent)
7
+ .reduce(
8
+ (acc, order) => {
9
+ const subTotals = {};
10
+ const { subTotals: orderSubTotals } = order;
11
+ if (orderSubTotals) {
12
+ const keys = Object.keys(orderSubTotals);
13
+ // eslint-disable-next-line no-restricted-syntax
14
+ for (const key of keys)
15
+ subTotals[key] = math.add(
16
+ acc.subTotals[key] || 0,
17
+ orderSubTotals[key]
18
+ );
19
+ }
20
+ return {
21
+ total: math.add(acc.total, order.total),
22
+ subTotals,
23
+ };
24
+ },
25
+ { total: 0, subTotals: {} }
26
+ );
27
+ };
28
+ };