@darkpos/pricing 1.0.4 → 1.0.7

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 (251) hide show
  1. package/.eslintrc +8 -10
  2. package/README.md +108 -4
  3. package/__TEST__/item.test.js +265 -34
  4. package/__TEST__/modifier/calculate.test.js +73 -0
  5. package/__TEST__/modifier/getAvailablePromotionsOrSubscriptions.test.js +81 -0
  6. package/__TEST__/modifier/getMatchTagsModifiers.test.js +109 -0
  7. package/__TEST__/modifier/getModifierIndex.test.js +40 -0
  8. package/__TEST__/modifier/getRecommendedModifiers.test.js +17 -0
  9. package/__TEST__/modifier/hasAllTags.test.js +25 -0
  10. package/__TEST__/modifier/hasAttribute.test.js +47 -0
  11. package/__TEST__/modifier/hasMatchTags.test.js +56 -0
  12. package/__TEST__/modifier/hasNoTags.test.js +21 -0
  13. package/__TEST__/modifier/mocks/toggleModifier_no1.json +13440 -0
  14. package/__TEST__/modifier/mocks/toggleModifier_no2.json +13438 -0
  15. package/__TEST__/modifier/mocks/toggleModifier_no3.json +13598 -0
  16. package/__TEST__/modifier/sort.test.js +41 -0
  17. package/__TEST__/order.test.js +147 -74
  18. package/index.js +1 -9
  19. package/lib/constants/Modifier/Attributes.js +9 -0
  20. package/lib/constants/Modifier/Compute.js +15 -0
  21. package/lib/constants/Modifier/Types.js +7 -0
  22. package/lib/constants/Modifier/index.js +9 -0
  23. package/lib/constants/Status.js +39 -0
  24. package/lib/constants/index.js +7 -0
  25. package/lib/index.js +38 -0
  26. package/lib/item/addIndirectModifier.js +55 -0
  27. package/lib/item/calculate.js +108 -0
  28. package/lib/item/create.js +35 -0
  29. package/lib/item/filterByDirectModifiers.js +13 -0
  30. package/lib/item/findOriginalItem.js +8 -0
  31. package/lib/item/getBalanceToPay.js +12 -0
  32. package/lib/item/getDepartmentModifiers.js +5 -0
  33. package/lib/item/getDiscountModifiers.js +7 -0
  34. package/lib/item/getItemModifiers.js +16 -0
  35. package/lib/item/getItemModifiersDescription.js +22 -0
  36. package/lib/item/getItemPrice.js +57 -0
  37. package/lib/item/getItemsTotalPaid.js +9 -0
  38. package/lib/item/getItemsTotalPieces.js +7 -0
  39. package/lib/item/getNotIncludedModifiers.js +11 -0
  40. package/lib/item/getParentItem.js +5 -0
  41. package/lib/item/getPendingIndex.js +5 -0
  42. package/lib/item/getTotal.js +8 -0
  43. package/lib/item/getTotalPieces.js +7 -0
  44. package/lib/item/getTotals.js +25 -0
  45. package/lib/item/hasCreateSubscription.js +7 -0
  46. package/lib/item/hasModifier.js +15 -0
  47. package/lib/item/hasModifierWithValue.js +11 -0
  48. package/lib/item/hasModifiers.js +9 -0
  49. package/lib/item/hasSubscription.js +5 -0
  50. package/lib/item/index.js +80 -4
  51. package/lib/item/isParentIncluded.js +4 -0
  52. package/lib/item/isRelatedItem.js +4 -0
  53. package/lib/item/isSelected.js +13 -0
  54. package/lib/item/openRecommendationDialog.js +4 -0
  55. package/lib/item/removeModifier.js +84 -0
  56. package/lib/item/removeModifierById.js +8 -0
  57. package/lib/item/removeModifiers.js +14 -0
  58. package/lib/modifier/addFile.js +19 -0
  59. package/lib/modifier/addImage.js +18 -0
  60. package/lib/modifier/calculate.js +43 -0
  61. package/lib/modifier/contains.js +5 -0
  62. package/lib/modifier/convertMenuModifiers.js +5 -0
  63. package/lib/modifier/create.js +44 -0
  64. package/lib/modifier/createAmountOverrideModifier.js +20 -0
  65. package/lib/modifier/createCreditModifier.js +32 -0
  66. package/lib/modifier/createDiscountModifier.js +25 -0
  67. package/lib/modifier/createGroupValuesModifier.js +29 -0
  68. package/lib/modifier/createIndirectModifier.js +42 -0
  69. package/lib/modifier/createSubscriptionModifier.js +19 -0
  70. package/lib/modifier/displayAsCard.js +9 -0
  71. package/lib/modifier/displayAsChips.js +9 -0
  72. package/lib/modifier/displayAsList.js +9 -0
  73. package/lib/modifier/duplicate.js +12 -0
  74. package/lib/modifier/enableAutoPopup.js +9 -0
  75. package/lib/modifier/filterByRequiredModifiers.js +4 -0
  76. package/lib/modifier/findById.js +7 -0
  77. package/lib/modifier/findByPaymentMethod.js +10 -0
  78. package/lib/modifier/findByPaymentType.js +10 -0
  79. package/lib/modifier/getAvailablePromotions.js +5 -0
  80. package/lib/modifier/getAvailablePromotionsOrSubscriptions.js +15 -0
  81. package/lib/modifier/getAvailableSubscriptions.js +5 -0
  82. package/lib/modifier/getByAttribute.js +6 -0
  83. package/lib/modifier/getCreditModifier.js +6 -0
  84. package/lib/modifier/getDefaultModifiers.js +4 -0
  85. package/lib/modifier/getDepartmentMaxItems.js +10 -0
  86. package/lib/modifier/getDepartmentModifiers.js +11 -0
  87. package/lib/modifier/getDescription.js +6 -0
  88. package/lib/modifier/getDescriptions.js +9 -0
  89. package/lib/modifier/getDirectModifiers.js +5 -0
  90. package/lib/modifier/getDiscountModifiers.js +7 -0
  91. package/lib/modifier/getEntityModifiers.js +11 -0
  92. package/lib/modifier/getEntityPromotions.js +11 -0
  93. package/lib/modifier/getExtendedTags.js +14 -0
  94. package/lib/modifier/getFiles.js +5 -0
  95. package/lib/modifier/getGroupModifiers.js +9 -0
  96. package/lib/modifier/getGroupOfItemsModifiers.js +4 -0
  97. package/lib/modifier/getGroupRelatedModifiers.js +26 -0
  98. package/lib/modifier/getImages.js +5 -0
  99. package/lib/modifier/getInheritedModifiers.js +30 -0
  100. package/lib/modifier/getItemModifiers.js +39 -0
  101. package/lib/modifier/getLaundryModifiers.js +9 -0
  102. package/lib/modifier/getMatchTagsModifiers.js +9 -0
  103. package/lib/modifier/getModifierIndex.js +14 -0
  104. package/lib/modifier/getPreferences.js +24 -0
  105. package/lib/modifier/getPromotionModifiers.js +5 -0
  106. package/lib/modifier/getPromptMessage.js +6 -0
  107. package/lib/modifier/getProperty.js +11 -0
  108. package/lib/modifier/getRecommendedModifiers.js +7 -0
  109. package/lib/modifier/getRelatedModifiers.js +10 -0
  110. package/lib/modifier/getRequiredModifiers.js +4 -0
  111. package/lib/modifier/getSelectedValues.js +7 -0
  112. package/lib/modifier/getService.js +5 -0
  113. package/lib/modifier/getSplittedModifiers.js +30 -0
  114. package/lib/modifier/getStarchModifier.js +9 -0
  115. package/lib/modifier/getSubscriptionItem.js +12 -0
  116. package/lib/modifier/getSubscriptionModifiers.js +5 -0
  117. package/lib/modifier/getSuggestion.js +9 -0
  118. package/lib/modifier/getUnhiddenModifiers.js +9 -0
  119. package/lib/modifier/getUsingCount.js +11 -0
  120. package/lib/modifier/hasAddModifiers.js +5 -0
  121. package/lib/modifier/hasAllTag.js +12 -0
  122. package/lib/modifier/hasAttribute.js +8 -0
  123. package/lib/modifier/hasAttributes.js +19 -0
  124. package/lib/modifier/hasCreateSubscription.js +10 -0
  125. package/lib/modifier/hasDisplayPrompt.js +9 -0
  126. package/lib/modifier/hasMatchTags.js +27 -0
  127. package/lib/modifier/hasModifier.js +12 -0
  128. package/lib/modifier/hasNoTags.js +9 -0
  129. package/lib/modifier/hasRelatedItems.js +8 -0
  130. package/lib/modifier/includePiecesInQuantity.js +10 -0
  131. package/lib/modifier/includesInGroup.js +9 -0
  132. package/lib/modifier/index.js +240 -6
  133. package/lib/modifier/isAmountOverride.js +7 -0
  134. package/lib/modifier/isAvailablePromotion.js +23 -0
  135. package/lib/modifier/isAvailableSubscription.js +28 -0
  136. package/lib/modifier/isCompact.js +12 -0
  137. package/lib/modifier/isCredit.js +6 -0
  138. package/lib/modifier/isCustomerTagsExtend.js +9 -0
  139. package/lib/modifier/isDefault.js +4 -0
  140. package/lib/modifier/isDepartment.js +6 -0
  141. package/lib/modifier/isDirect.js +6 -0
  142. package/lib/modifier/isDiscount.js +6 -0
  143. package/lib/modifier/isEditable.js +10 -0
  144. package/lib/modifier/isGratuity.js +6 -0
  145. package/lib/modifier/isGroupOfItems.js +12 -0
  146. package/lib/modifier/isGroupOfModifiers.js +12 -0
  147. package/lib/modifier/isGroupOfValues.js +13 -0
  148. package/lib/modifier/isHidden.js +4 -0
  149. package/lib/modifier/isIncluded.js +4 -0
  150. package/lib/modifier/isManual.js +9 -0
  151. package/lib/modifier/isMultiplier.js +11 -0
  152. package/lib/modifier/isOverride.js +6 -0
  153. package/lib/modifier/isPaymentMethods.js +8 -0
  154. package/lib/modifier/isPaymentTypes.js +8 -0
  155. package/lib/modifier/isPreferences.js +8 -0
  156. package/lib/modifier/isPriceOverride.js +7 -0
  157. package/lib/modifier/isPromotion.js +6 -0
  158. package/lib/modifier/isQuantityOverride.js +7 -0
  159. package/lib/modifier/isRemoveModifier.js +4 -0
  160. package/lib/modifier/isRequired.js +4 -0
  161. package/lib/modifier/isRequiredAndOverride.js +4 -0
  162. package/lib/modifier/isService.js +9 -0
  163. package/lib/modifier/isSingleValue.js +9 -0
  164. package/lib/modifier/isSubscription.js +6 -0
  165. package/lib/modifier/isTrackUsageSubscription.js +5 -0
  166. package/lib/modifier/isValueSelected.js +7 -0
  167. package/lib/modifier/purifyModifiers.js +15 -0
  168. package/lib/modifier/sort.js +35 -0
  169. package/lib/order/addCreditModifier.js +27 -0
  170. package/lib/order/addDiscountModifier.js +9 -0
  171. package/lib/order/addItem.js +292 -0
  172. package/lib/order/addItemModifier.js +214 -0
  173. package/lib/order/addModifier.js +9 -0
  174. package/lib/order/addModifierToAllItems.js +16 -0
  175. package/lib/order/addNote.js +25 -0
  176. package/lib/order/addTable.js +11 -0
  177. package/lib/order/calculate.js +105 -0
  178. package/lib/order/calculateDue.js +15 -0
  179. package/lib/order/calculateOrdersDue.js +26 -0
  180. package/lib/order/canSplit.js +4 -0
  181. package/lib/order/changeEndDate.js +15 -0
  182. package/lib/order/changeLocation.js +20 -0
  183. package/lib/order/changeStartDate.js +14 -0
  184. package/lib/order/combineSubOrders.js +14 -0
  185. package/lib/order/containsSerial.js +5 -0
  186. package/lib/order/create.js +67 -0
  187. package/lib/order/createParent.js +9 -0
  188. package/lib/order/duplicateItem.js +16 -0
  189. package/lib/order/findItemIndex.js +6 -0
  190. package/lib/order/getAppliedCredit.js +5 -0
  191. package/lib/order/getBalance.js +9 -0
  192. package/lib/order/getCustomer.js +4 -0
  193. package/lib/order/getCustomerStoreCredit.js +14 -0
  194. package/lib/order/getCustomerSubscriptions.js +8 -0
  195. package/lib/order/getCustomerSubscriptionsByItem.js +14 -0
  196. package/lib/order/getDepartmentModifiers.js +9 -0
  197. package/lib/order/getEndDate.js +4 -0
  198. package/lib/order/getItemByItemId.js +6 -0
  199. package/lib/order/getItemIndex.js +9 -0
  200. package/lib/order/getItemsToPay.js +17 -0
  201. package/lib/order/getMenuItemsByCustomer.js +19 -0
  202. package/lib/order/getNumberOfItems.js +10 -0
  203. package/lib/order/getOrdersPieces.js +10 -0
  204. package/lib/order/getPendingItemIndex.js +5 -0
  205. package/lib/order/getRelatedItems.js +11 -0
  206. package/lib/order/getSameItems.js +19 -0
  207. package/lib/order/getScheduleByCustomer.js +20 -0
  208. package/lib/order/getScreenCustomerNotes.js +12 -0
  209. package/lib/order/getSelectedItem.js +6 -0
  210. package/lib/order/getStartDate.js +4 -0
  211. package/lib/order/getTip.js +10 -0
  212. package/lib/order/getTotalPieces.js +17 -0
  213. package/lib/order/getTotals.js +28 -0
  214. package/lib/order/hasAggregate.js +20 -0
  215. package/lib/order/hasItem.js +7 -0
  216. package/lib/order/hasRemainingSubscription.js +67 -0
  217. package/lib/order/hold.js +12 -0
  218. package/lib/order/index.js +164 -4
  219. package/lib/order/isDetailed.js +4 -0
  220. package/lib/order/isHold.js +4 -0
  221. package/lib/order/isNew.js +4 -0
  222. package/lib/order/isOpen.js +4 -0
  223. package/lib/order/isParent.js +7 -0
  224. package/lib/order/merge.js +96 -0
  225. package/lib/order/open.js +11 -0
  226. package/lib/order/pickEndDate.js +70 -0
  227. package/lib/order/removeApplyModifier.js +24 -0
  228. package/lib/order/removeDiscountModifier.js +16 -0
  229. package/lib/order/removeItem.js +55 -0
  230. package/lib/order/removeItemByIndex.js +5 -0
  231. package/lib/order/removeItemModifier.js +69 -0
  232. package/lib/order/removeModifier.js +21 -0
  233. package/lib/order/removeModifierByAttribute.js +23 -0
  234. package/lib/order/removeModifiersWithPaymentMethods.js +29 -0
  235. package/lib/order/removeModifiersWithPaymentTypes.js +27 -0
  236. package/lib/order/setCustomer.js +50 -0
  237. package/lib/order/splitByDepartments.js +151 -0
  238. package/lib/order/toggleModifier.js +9 -0
  239. package/lib/order/updateItem.js +8 -0
  240. package/lib/order/updateItemQuantity.js +35 -0
  241. package/lib/order/void.js +11 -0
  242. package/package.json +17 -15
  243. package/__TEST__/modifier.test.js +0 -126
  244. package/lib/item/actions.js +0 -140
  245. package/lib/modifier/actions.js +0 -172
  246. package/lib/modifier/constants.js +0 -46
  247. package/lib/order/actions.js +0 -47
  248. package/lib/utils/defaultPrices.js +0 -10
  249. package/lib/utils/helpers.js +0 -26
  250. package/lib/utils/index.js +0 -9
  251. package/lib/utils/math.js +0 -71
@@ -0,0 +1,41 @@
1
+ const usePricing = require('../../index');
2
+
3
+ const pricingService = usePricing();
4
+
5
+ describe('Sort Function', () => {
6
+ test('usecase #1', () => {
7
+ const modifiers = [
8
+ {
9
+ _id: 1,
10
+ type: 'fixed',
11
+ action: 'add',
12
+ amount: 10,
13
+ name: 'someModifier',
14
+ properties: {},
15
+ },
16
+ {
17
+ _id: 2,
18
+ type: 'fixed',
19
+ action: 'add',
20
+ amount: 10,
21
+ name: 'someModifier',
22
+ properties: {
23
+ sort: 2,
24
+ },
25
+ },
26
+ {
27
+ _id: 3,
28
+ type: 'fixed',
29
+ action: 'add',
30
+ amount: 10,
31
+ name: 'someModifier',
32
+ properties: {
33
+ sort: 1,
34
+ },
35
+ },
36
+ ];
37
+ const sortedModifiers = pricingService.modifier.sort(modifiers);
38
+ const ids = sortedModifiers.map((mob) => mob._id);
39
+ expect(ids).toMatchObject([1, 3, 2]);
40
+ });
41
+ });
@@ -1,90 +1,137 @@
1
- const orderActions = require('../lib/order/actions');
1
+ const usePricing = require('../index');
2
+
3
+ const pricingService = usePricing();
2
4
 
3
5
  describe('Order actions', () => {
4
6
  test('Get calculated Order, one item', () => {
5
7
  const modifiers = [
6
- { amount: 20, type: 'percentage', action: 'add' },
7
- { amount: 20, type: 'percentage', action: 'subtract' },
8
- { amount: 2, type: 'fixed', action: 'subtract' },
8
+ {
9
+ compute: {
10
+ amount: 20,
11
+ type: 'percentage',
12
+ action: 'add',
13
+ },
14
+ name: 'modifier1',
15
+ type: 'discount',
16
+ },
17
+ {
18
+ compute: {
19
+ amount: 20,
20
+ type: 'percentage',
21
+ action: 'subtract',
22
+ },
23
+ name: 'modifier2',
24
+ type: 'discount',
25
+ },
26
+ {
27
+ compute: { amount: 2, type: 'fixed', action: 'subtract' },
28
+ name: 'modifier3',
29
+ type: 'discount',
30
+ },
9
31
  ];
10
32
  const orderItem = { price: 30, quantity: 2, modifiers };
11
33
  const order = { items: [orderItem] };
12
- const newOrder = orderActions.calculateOrder(order);
34
+ const newOrder = pricingService.order.calculate(order);
35
+
13
36
  expect(newOrder).toHaveProperty('total', 56);
14
37
  expect(newOrder).toHaveProperty('subTotal', 60);
15
- expect(newOrder).toHaveProperty('tax', 0);
16
- expect(newOrder).toHaveProperty('discount', 0);
17
- expect(newOrder).toHaveProperty('fee', -4);
18
- expect(newOrder).toHaveProperty('distributed', 0);
38
+ expect(newOrder).toHaveProperty('subTotals', {
39
+ discount: -4,
40
+ });
19
41
  });
20
42
  test('Get calculated Order, multiple items', () => {
21
43
  const item1 = {
22
44
  _id: 1,
23
45
  price: 30,
24
46
  quantity: 2,
25
- modifiers: [{ amount: 2, type: 'fixed', action: 'subtract' }],
47
+ modifiers: [
48
+ {
49
+ compute: {
50
+ amount: 2,
51
+ type: 'fixed',
52
+ action: 'subtract',
53
+ },
54
+ name: 'modifier1',
55
+ type: 'discount',
56
+ },
57
+ ],
26
58
  };
27
59
  const item2 = {
28
60
  _id: 2,
29
61
  price: 10,
30
62
  quantity: 1,
31
- modifiers: [{ amount: 2, type: 'fixed', action: 'subtract' }],
63
+ modifiers: [
64
+ {
65
+ compute: {
66
+ amount: 2,
67
+ type: 'fixed',
68
+ action: 'subtract',
69
+ },
70
+ name: 'modifier1',
71
+ type: 'discount',
72
+ },
73
+ ],
32
74
  };
33
75
  const order = { items: [item1, item2] };
34
- const newOrder = orderActions.calculateOrder(order);
76
+ const newOrder = pricingService.order.calculate(order);
35
77
  expect(newOrder).toHaveProperty('total', 64);
36
78
  expect(newOrder).toHaveProperty('subTotal', 70);
37
- expect(newOrder).toHaveProperty('tax', 0);
38
- expect(newOrder).toHaveProperty('discount', 0);
39
- expect(newOrder).toHaveProperty('fee', -6);
40
- expect(newOrder).toHaveProperty('distributed', 0);
79
+ expect(newOrder).toHaveProperty('subTotals', {
80
+ discount: -6,
81
+ });
82
+ // console.log(JSON.stringify(newOrder, 0, 2));
41
83
  });
42
- test('Get calculated Order, multiple items and discount modifiers', () => {
84
+ test('Get calculated Order, multiple items and indirect modifiers', () => {
43
85
  const item1 = {
44
86
  _id: 1,
45
87
  price: 30,
46
88
  quantity: 2,
47
- modifiers: [{ amount: 2, type: 'fixed', action: 'subtract', attributes: ['discount'] }],
48
89
  };
49
90
  const item2 = {
50
91
  _id: 2,
51
92
  price: 10,
52
93
  quantity: 1,
53
- modifiers: [{ amount: 2, type: 'fixed', action: 'subtract', attributes: ['discount'] }],
54
94
  };
55
- const order = { items: [item1, item2] };
56
- const newOrder = orderActions.calculateOrder(order);
57
- expect(newOrder).toHaveProperty('total', 64);
58
- expect(newOrder).toHaveProperty('subTotal', 70);
59
- expect(newOrder).toHaveProperty('tax', 0);
60
- expect(newOrder).toHaveProperty('discount', -6);
61
- expect(newOrder).toHaveProperty('fee', 0);
62
- expect(newOrder).toHaveProperty('distributed', 0);
63
- });
64
- test('Get calculated Order, multiple items and distributed modifiers', () => {
65
- const item1 = {
66
- _id: 1,
67
- price: 30,
68
- quantity: 2,
69
- };
70
- const item2 = {
71
- _id: 2,
72
- price: 10,
73
- quantity: 1,
95
+ const modifier1 = {
96
+ compute: {
97
+ amount: 2,
98
+ type: 'fixed',
99
+ action: 'subtract',
100
+ },
101
+ name: 'modifier1',
102
+ type: 'discount',
74
103
  };
75
- const modifier1 = { amount: 2, type: 'fixed', action: 'subtract', attributes: ['discount'] };
76
104
 
77
105
  const order = { items: [item1, item2], modifiers: [modifier1] };
78
- const newOrder = orderActions.calculateOrder(order);
79
106
 
107
+ const newOrder = pricingService.order.calculate(order);
80
108
  expect(newOrder).toHaveProperty('total', 68);
81
109
  expect(newOrder).toHaveProperty('subTotal', 70);
82
- expect(newOrder).toHaveProperty('tax', 0);
83
- expect(newOrder).toHaveProperty('discount', 0);
84
- expect(newOrder).toHaveProperty('fee', 0);
85
- expect(newOrder).toHaveProperty('distributed', -2);
110
+ expect(newOrder).toHaveProperty('subTotals', {
111
+ discount: -2,
112
+ });
113
+ expect(newOrder.items[0]).toHaveProperty('total', 58.28);
114
+ expect(newOrder.items[0]).toHaveProperty('subTotals', {
115
+ discount: -1.72,
116
+ _included: 0,
117
+ _xincluded: -1.72,
118
+ _direct: 0,
119
+ _xdirect: -1.72,
120
+ _simple: 60,
121
+ _actual: 60,
122
+ });
123
+ expect(newOrder.items[1]).toHaveProperty('total', 9.72);
124
+ expect(newOrder.items[1]).toHaveProperty('subTotals', {
125
+ discount: -0.28,
126
+ _included: 0,
127
+ _xincluded: -0.28,
128
+ _direct: 0,
129
+ _xdirect: -0.28,
130
+ _simple: 10,
131
+ _actual: 10,
132
+ });
86
133
  });
87
- test('Get calculated Order, multiple items and distributed modifiers #2', () => {
134
+ test('Get calculated Order, multiple items and indirect modifiers #2', () => {
88
135
  const item1 = {
89
136
  _id: 1,
90
137
  price: 15.99,
@@ -96,23 +143,44 @@ describe('Order actions', () => {
96
143
  quantity: 1,
97
144
  };
98
145
  const modifier1 = {
99
- amount: 10,
100
- type: 'percentage',
101
- action: 'subtract',
102
- attributes: ['discount'],
146
+ compute: {
147
+ amount: 10,
148
+ action: 'subtract',
149
+ type: 'percentage',
150
+ },
151
+ name: 'modifier1',
152
+ type: 'discount',
103
153
  };
104
154
 
105
155
  const order = { items: [item1, item2], modifiers: [modifier1] };
106
- const newOrder = orderActions.calculateOrder(order);
107
-
156
+ const newOrder = pricingService.order.calculate(order);
108
157
  expect(newOrder).toHaveProperty('total', 18.58);
109
158
  expect(newOrder).toHaveProperty('subTotal', 20.64);
110
- expect(newOrder).toHaveProperty('tax', 0);
111
- expect(newOrder).toHaveProperty('discount', 0);
112
- expect(newOrder).toHaveProperty('fee', 0);
113
- expect(newOrder).toHaveProperty('distributed', -2.06);
159
+ expect(newOrder).toHaveProperty('subTotals', {
160
+ discount: -2.06,
161
+ });
162
+ expect(newOrder.items[0]).toHaveProperty('total', 14.39);
163
+ expect(newOrder.items[0]).toHaveProperty('subTotals', {
164
+ discount: -1.6,
165
+ _included: 0,
166
+ _xincluded: -1.6,
167
+ _direct: 0,
168
+ _xdirect: -1.6,
169
+ _simple: 15.99,
170
+ _actual: 15.99,
171
+ });
172
+ expect(newOrder.items[1]).toHaveProperty('total', 4.19);
173
+ expect(newOrder.items[1]).toHaveProperty('subTotals', {
174
+ discount: -0.46,
175
+ _included: 0,
176
+ _xincluded: -0.46,
177
+ _direct: 0,
178
+ _xdirect: -0.46,
179
+ _simple: 4.65,
180
+ _actual: 4.65,
181
+ });
114
182
  });
115
- test('Get calculated Order, multiple items and distributed modifiers #3', () => {
183
+ test('Get calculated Order, multiple items and indirect modifiers #3', () => {
116
184
  const item1 = {
117
185
  _id: 1,
118
186
  price: 3,
@@ -130,31 +198,36 @@ describe('Order actions', () => {
130
198
  };
131
199
  const modifier1 = {
132
200
  _id: 1,
201
+ compute: {
202
+ amount: 10,
203
+ type: 'percentage',
204
+ action: 'subtract',
205
+ },
133
206
  name: `10% Discount`,
134
- amount: 10,
135
- type: 'percentage',
136
- action: 'subtract',
137
- attributes: ['discount'],
207
+ type: 'discount',
138
208
  };
139
209
 
140
210
  const modifier2 = {
141
211
  _id: 2,
142
- name: `20% Discount`,
143
- amount: 20,
144
- type: 'percentage',
145
- action: 'subtract',
146
- attributes: ['discount'],
212
+ name: `20% tax`,
213
+ type: 'tax',
214
+ compute: {
215
+ amount: 20,
216
+ type: 'percentage',
217
+ action: 'add',
218
+ },
147
219
  };
148
220
 
149
- const order = { items: [item1, item2, item3], modifiers: [modifier1, modifier2] };
150
- const newOrder = orderActions.calculateOrder(order);
151
-
152
- //console.log('order =>', JSON.stringify(newOrder, 0, 2));
153
- expect(newOrder).toHaveProperty('total', 7);
221
+ const order = {
222
+ items: [item1, item2, item3],
223
+ modifiers: [modifier1, modifier2],
224
+ };
225
+ const newOrder = pricingService.order.calculate(order);
226
+ expect(newOrder).toHaveProperty('total', 11);
154
227
  expect(newOrder).toHaveProperty('subTotal', 10);
155
- expect(newOrder).toHaveProperty('tax', 0);
156
- expect(newOrder).toHaveProperty('discount', 0);
157
- expect(newOrder).toHaveProperty('fee', 0);
158
- expect(newOrder).toHaveProperty('distributed', -3);
228
+ expect(newOrder).toHaveProperty('subTotals', {
229
+ discount: -1,
230
+ tax: 2,
231
+ });
159
232
  });
160
233
  });
package/index.js CHANGED
@@ -1,9 +1 @@
1
- const modifier = require('./lib/modifier');
2
- const item = require('./lib/item');
3
- const order = require('./lib/order');
4
-
5
- module.exports = {
6
- modifier,
7
- item,
8
- order,
9
- };
1
+ module.exports = require('./lib');
@@ -0,0 +1,9 @@
1
+ //
2
+ module.exports = {
3
+ SUBSCRIPTION: 'subscription',
4
+ PROMOTION: 'promotion',
5
+ GROUP: 'group',
6
+ LIST: 'list',
7
+ DEPARTMENT: 'department',
8
+ OVERRIDE: 'override',
9
+ };
@@ -0,0 +1,15 @@
1
+ const Types = {
2
+ FIXED: 'fixed',
3
+ PERCENTAGE: 'percentage',
4
+ };
5
+
6
+ const Actions = {
7
+ ADD: 'add',
8
+ SUBTRACT: 'subtract',
9
+ OVERRIDE: 'override',
10
+ };
11
+
12
+ module.exports = {
13
+ Types,
14
+ Actions,
15
+ };
@@ -0,0 +1,7 @@
1
+ //
2
+ module.exports = {
3
+ DISCOUNT: 'discount',
4
+ TAX: 'tax',
5
+ GRATUITY: 'gratuity',
6
+ CREDIT: 'credit',
7
+ };
@@ -0,0 +1,9 @@
1
+ const Compute = require('./Compute');
2
+ const Types = require('./Types');
3
+ const Attributes = require('./Attributes');
4
+
5
+ module.exports = {
6
+ Attributes,
7
+ Types,
8
+ Compute,
9
+ };
@@ -0,0 +1,39 @@
1
+ const Order = Object.freeze({
2
+ OPEN: 'open',
3
+ HOLD: 'hold',
4
+ VOID: 'void',
5
+ CLOSED: 'close',
6
+ });
7
+
8
+ const Invoice = Object.freeze({
9
+ PARTIAL: 'partial',
10
+ PENDING: 'pending',
11
+ CLOSED: 'close',
12
+ });
13
+
14
+ const Kitchen = Object.freeze({
15
+ OPEN: 'open',
16
+ CLOSED: 'closed',
17
+ });
18
+
19
+ const Delivery = Object.freeze({
20
+ NONE: 'none',
21
+ BOOKED: 'booked',
22
+ PICKED: 'picked',
23
+ DELIVER: 'deliver',
24
+ DELIVERED: 'delivered',
25
+ DELIVERED_PARTIAL: 'delivered.partial',
26
+ RETURNED: 'returned',
27
+ RETURNED_PARTIAL: 'returned.partial',
28
+ HELD_NOT_READY: 'held.not.ready',
29
+ HELD_FOR_RELEASE_APPROVAL: 'held.for.release.approval',
30
+ CANCELLED: 'cancelled',
31
+ LOST: 'lost',
32
+ });
33
+
34
+ module.exports = Object.freeze({
35
+ Order,
36
+ Invoice,
37
+ Kitchen,
38
+ Delivery,
39
+ });
@@ -0,0 +1,7 @@
1
+ const Status = require('./Status');
2
+ const Modifier = require('./Modifier');
3
+
4
+ module.exports = {
5
+ Status,
6
+ Modifier,
7
+ };
package/lib/index.js ADDED
@@ -0,0 +1,38 @@
1
+ const utils = require('@darkpos/utils');
2
+ const _ = require('lodash');
3
+ const moment = require('moment-timezone');
4
+ /** services */
5
+ const makeItemActions = require('./item');
6
+ const makeOrderActions = require('./order');
7
+ const makeModifierActions = require('./modifier');
8
+ const constants = require('./constants');
9
+
10
+ module.exports = session => {
11
+ const { useLocalization } = utils;
12
+ const settings = _.get(session, 'store._settings', {});
13
+ const localization = useLocalization(settings.localization);
14
+
15
+ const deps = {
16
+ settings,
17
+ utils,
18
+ localization,
19
+ _,
20
+ moment,
21
+ constants,
22
+ };
23
+
24
+ //
25
+ const modifierActions = makeModifierActions(deps);
26
+ const itemActions = makeItemActions({ ...deps, modifierActions });
27
+ const orderActions = makeOrderActions({
28
+ ...deps,
29
+ itemActions,
30
+ modifierActions,
31
+ });
32
+
33
+ return {
34
+ item: itemActions,
35
+ order: orderActions,
36
+ modifier: modifierActions,
37
+ };
38
+ };
@@ -0,0 +1,55 @@
1
+ module.exports = ({ utils, modifierActions }) => {
2
+ const { math } = utils;
3
+
4
+ return function addIndirectModifier(orderTotal, items, modifier) {
5
+ const itemsModifiers = {};
6
+ // get distributed price in order level
7
+ const { _computed } = modifierActions.calculate(modifier, {
8
+ price: orderTotal,
9
+ quantity: 1,
10
+ });
11
+
12
+ const orderModifierTotal = math.abs(_computed.amount);
13
+ const totalDistributed = items.reduce((acc, item) => {
14
+ const inheritedModifier = modifierActions.createIndirectModifier(
15
+ modifier,
16
+ {
17
+ orderTotal,
18
+ itemTotal: item.total,
19
+ itemQuantity: item.quantity,
20
+ }
21
+ );
22
+ itemsModifiers[item._id] = inheritedModifier;
23
+ return math.add(
24
+ acc,
25
+ math.mul(inheritedModifier.compute.amount, item.quantity)
26
+ );
27
+ }, 0);
28
+
29
+ // check validation
30
+ if (math.abs(totalDistributed) !== math.abs(orderModifierTotal)) {
31
+ const difference = math.sub(orderModifierTotal, totalDistributed);
32
+ // console.log('difference => ', difference);
33
+ const selectedItem = items.find(
34
+ item => math.sub(item.total, difference) > 0
35
+ );
36
+
37
+ if (selectedItem) {
38
+ const inheritedModifier = itemsModifiers[selectedItem._id];
39
+ itemsModifiers[selectedItem._id] = {
40
+ ...inheritedModifier,
41
+ compute: {
42
+ ...inheritedModifier.compute,
43
+ amount: math.add(
44
+ inheritedModifier.compute.amount,
45
+ math.div(difference, selectedItem.quantity)
46
+ ),
47
+ },
48
+ };
49
+ }
50
+ return itemsModifiers;
51
+ }
52
+
53
+ return itemsModifiers;
54
+ };
55
+ };
@@ -0,0 +1,108 @@
1
+ /* eslint-disable no-restricted-syntax */
2
+ module.exports = ({ _, utils, modifierActions }) => {
3
+ const { math } = utils;
4
+ //
5
+ const calculateOne = inputItem => {
6
+ const item = _.cloneDeep(inputItem);
7
+ if (!item) return item;
8
+
9
+ const subTotals = {
10
+ _included: 0,
11
+ _xincluded: 0,
12
+ _direct: 0,
13
+ _xdirect: 0,
14
+ _simple: 0,
15
+ _actual: 0,
16
+ };
17
+
18
+ const price = parseFloat(item.price);
19
+ const quantity = parseInt(item.quantity, 10);
20
+ subTotals._simple = math.mul(price, quantity);
21
+
22
+ const modifiers = [];
23
+ const { modifiers: itemModifiers = [] } = item;
24
+ const amountOverride = itemModifiers.find(each =>
25
+ modifierActions.isAmountOverride(each)
26
+ );
27
+
28
+ const modifiersToCompute = itemModifiers.filter(
29
+ each =>
30
+ !modifierActions.isOverride(each) && each.compute && each.compute.type
31
+ );
32
+ if (amountOverride) modifiersToCompute.push(amountOverride);
33
+
34
+ const modifiersToNotCompute = itemModifiers
35
+ .filter(
36
+ each =>
37
+ !modifiersToCompute.find(
38
+ ceach => ceach.modifierId === each.modifierId
39
+ )
40
+ )
41
+ .map(each => modifierActions.calculate(each, { skip: true }));
42
+
43
+ if (modifiersToCompute.length) {
44
+ // sort modifiers based on sort
45
+ const sortedModifiers = modifierActions.sort(modifiersToCompute);
46
+ let computedPrice = price;
47
+ let prvPrice = 0;
48
+ let prvSort;
49
+
50
+ for (const modifier of sortedModifiers) {
51
+ const included = modifierActions.isIncluded(modifier);
52
+ const direct = modifierActions.isDirect(modifier);
53
+ const sort = modifierActions.getProperty(modifier, 'sort');
54
+ // check if add amount to computedPrice;
55
+ if (sort !== prvSort) {
56
+ computedPrice = math.add(computedPrice, prvPrice);
57
+ prvPrice = 0;
58
+ prvSort = null;
59
+ }
60
+
61
+ const _modifier = modifierActions.calculate(modifier, {
62
+ price: computedPrice,
63
+ quantity,
64
+ skip: !!amountOverride,
65
+ });
66
+
67
+ modifiers.push(_modifier);
68
+ const { type, _computed } = _modifier;
69
+ // console.log('computedPrice => ', _computed);
70
+ let computedAmount = math.mul(_computed.amount, quantity);
71
+ // subscription modifiers have fixed amount and can't be multiplied by quantity
72
+ if (modifierActions.isSubscription(_modifier))
73
+ computedAmount = _computed.amount;
74
+
75
+ if (included) {
76
+ subTotals._included = math.add(subTotals._included, computedAmount);
77
+ prvPrice = math.add(prvPrice, computedAmount);
78
+ prvSort = sort;
79
+ } else {
80
+ subTotals._xincluded = math.add(subTotals._xincluded, computedAmount);
81
+ subTotals[type] = math.add(subTotals[type] || 0, computedAmount);
82
+ }
83
+ if (direct)
84
+ subTotals._direct = math.add(subTotals._direct, computedAmount);
85
+ else subTotals._xdirect = math.add(subTotals._xdirect, computedAmount);
86
+ }
87
+ }
88
+
89
+ subTotals._actual = math.add(subTotals._simple, subTotals._included);
90
+ // console.log('subTotals =>', subTotals);
91
+ const total = math.add(subTotals._actual, subTotals._xincluded);
92
+
93
+ return {
94
+ ...item,
95
+ modifiers: [...modifiersToNotCompute, ...modifiers],
96
+ subTotals,
97
+ total,
98
+ };
99
+ };
100
+
101
+ //
102
+ const calculateMany = (items = []) => items.map(item => calculateOne(item));
103
+
104
+ return function calculate(item) {
105
+ if (Array.isArray(item)) return calculateMany(item);
106
+ return calculateOne(item);
107
+ };
108
+ };
@@ -0,0 +1,35 @@
1
+ module.exports = ({ utils }) => {
2
+ const { helpers } = utils;
3
+
4
+ const defaultItem = {
5
+ location: {},
6
+ status: {
7
+ picked: { value: false, date: '' },
8
+ paid: { value: false, date: '' },
9
+ tracker: [],
10
+ },
11
+ name: '',
12
+ description: '',
13
+ pieces: 0,
14
+ modifiersTotalAmount: 0,
15
+ total: 0,
16
+ totalPaid: 0,
17
+ price: 0,
18
+ quantity: 0,
19
+ path: [],
20
+ notes: [],
21
+ menuId: null,
22
+ serial: null,
23
+ sku: null,
24
+ modifiers: [],
25
+ };
26
+
27
+ return function create(params) {
28
+ return {
29
+ ...defaultItem,
30
+ ...params,
31
+ __typename: 'OrderItem',
32
+ _id: helpers.getObjectID(),
33
+ };
34
+ };
35
+ };