@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
package/.eslintrc CHANGED
@@ -7,18 +7,16 @@
7
7
  "plugins": ["prettier", "jest"],
8
8
  "rules": {
9
9
  "no-debugger": 0,
10
- "import/no-extraneous-dependencies": 0,
11
10
  "no-underscore-dangle": 0,
12
- "import/no-dynamic-require": 0,
13
11
  "no-console": 0,
14
12
  "global-require": 0,
15
- "prettier/prettier": [
16
- "error",
17
- {
18
- "trailingComma": "es5",
19
- "singleQuote": true,
20
- "printWidth": 100
21
- }
22
- ]
13
+ "prettier/prettier": ["error", {
14
+ "singleQuote": true,
15
+ "trailingComma": "es5",
16
+ "bracketSpacing": true,
17
+ "bracketSameLine": false,
18
+ "arrowParens": "avoid",
19
+ "parser": "flow"
20
+ }]
23
21
  }
24
22
  }
package/README.md CHANGED
@@ -7,9 +7,113 @@
7
7
  ### Usage
8
8
 
9
9
  ```
10
- const pricing = require('dark-pricing');
11
- const { modifier, item, order } = pricing;
10
+ const usePricing = require('@darkpos/pricing');
12
11
 
13
- const order = {};
14
- order.calculateOrder(order);
12
+ const options = {
13
+ ...localization,
14
+ };
15
+
16
+ const pricingService = usePricing(options);
17
+
18
+ //
19
+ const newOrder = pricingService.order.calculate(oldOrder);
20
+ ```
21
+
22
+ Sample of a calculated Item:
23
+
24
+ ```
25
+ const item = {
26
+ name: 'shirt',
27
+ price: 2,
28
+ modifiers: [
29
+ {
30
+ action: 'add',
31
+ type: 'fixed',
32
+ amount: 1
33
+ attributes: [hidden]
34
+ category: 'fee',
35
+ properties: {
36
+ sort: 1,
37
+ included: true,
38
+ },
39
+ computed: {
40
+ amount: 1
41
+ }
42
+ } ----> subTotal: 3
43
+ {
44
+ action: 'add',
45
+ type: 'percentage',
46
+ amount: 20
47
+ category: 'tax',
48
+ properties: {
49
+ direct: false,
50
+ sort: 2,
51
+ included: true
52
+ }
53
+ computed: {
54
+ amount: 0.60
55
+ }
56
+ }
57
+ {
58
+ action: 'add',
59
+ type: 'percentage',
60
+ amount: 30
61
+ category: 'tax',
62
+ properties: {
63
+ direct: false,
64
+ sort: 2,
65
+ included: true
66
+ }
67
+ computed: {
68
+ amount: 0.90
69
+ }
70
+ }
71
+ ----> subTotal: 4.50
72
+ {
73
+ action: 'subtract',
74
+ type: 'fixed',
75
+ amount: 1
76
+ category: 'discount'
77
+ properties: {
78
+ direct: false,
79
+ sort: 3,
80
+ included: false
81
+ }
82
+ computed: {
83
+ amount: -1
84
+ }
85
+ }],
86
+ subTotals: {
87
+ tax: (included: false) ---> 0.60
88
+ discount: (included: false) --->-1.00
89
+ fee: ---> 0.00
90
+ _included: (included: true) ---> 1
91
+ _xincluded ---> -0.40
92
+ _direct: sum(modifiers with direct: true) ---> 1.60 *
93
+ _xdirect: (direct: false) ---> -1
94
+ _simple: price * quantity ---> 2.00 *
95
+ _actual: _simple + _included ---> 3.00
96
+ }
97
+ total: _actual + _xincluded ---> 2.60
98
+ }
99
+
100
+ const order = {
101
+ items: [item],
102
+ modifiers: [{
103
+ action: 'subtract',
104
+ type: 'fixed',
105
+ amount: 1
106
+ attributes: [hidden]
107
+ group: 'discount'
108
+ }],
109
+ subTotal: sum of _actuals ---> 3.00
110
+ ---
111
+ subTotals: {
112
+ tax: sum of subTotals.tax ---> 0.60
113
+ fee: sum of subTotals.fee ---> 0.00
114
+ discount: sum of subTotals.discount --->-1.00
115
+ }
116
+ ---
117
+ total: sum of above ---> 2.60
118
+ }
15
119
  ```
@@ -1,54 +1,285 @@
1
- const itemActions = require("../lib/item/actions");
1
+ const usePricing = require('../index');
2
2
 
3
- describe("Item actions", () => {
4
- test("Get calculated Item", () => {
3
+ const pricingService = usePricing();
4
+
5
+ describe('Item actions', () => {
6
+ test('Get calculated Item: included: false && direct: false', () => {
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
+ direct: false,
17
+ included: false,
18
+ },
19
+ {
20
+ compute: {
21
+ amount: 20,
22
+ type: 'percentage',
23
+ action: 'subtract',
24
+ },
25
+ name: 'modifier2',
26
+ type: 'discount',
27
+ direct: false,
28
+ included: false,
29
+ },
30
+ {
31
+ compute: {
32
+ amount: 2,
33
+ type: 'fixed',
34
+ action: 'subtract',
35
+ },
36
+ name: 'modifier3',
37
+ type: 'discount',
38
+ direct: false,
39
+ included: false,
40
+ },
9
41
  ];
10
42
  const orderItem = { price: 30, quantity: 2, modifiers };
11
- const newItem = itemActions.calculateItem(orderItem);
12
- expect(newItem).toHaveProperty("total", 56);
13
- expect(newItem).toHaveProperty("subTotal", 60);
43
+ const newItem = pricingService.item.calculate(orderItem);
44
+ expect(newItem).toHaveProperty('total', 56);
45
+ expect(newItem).toHaveProperty('subTotals', {
46
+ discount: -4,
47
+ _included: 0,
48
+ _xincluded: -4,
49
+ _direct: 0,
50
+ _xdirect: -4,
51
+ _simple: 60,
52
+ _actual: 60,
53
+ });
14
54
  });
15
- test("Get calculated Item with distributed Modifiers", () => {
55
+ test('Get calculated Item: included: false && direct: true', () => {
16
56
  const modifiers = [
17
57
  {
18
- amount: 20,
19
- type: "percentage",
20
- action: "add",
21
- attributes: ["distributed"]
58
+ compute: { amount: 2, type: 'fixed', action: 'subtract' },
59
+ type: 'discount',
60
+ name: 'modifier1',
61
+ direct: true,
62
+ included: false,
63
+ },
64
+ {
65
+ compute: {
66
+ amount: 20,
67
+ type: 'percentage',
68
+ action: 'subtract',
69
+ },
70
+ name: 'modifier2',
71
+ type: 'discount',
72
+ direct: true,
73
+ included: false,
74
+ },
75
+ {
76
+ compute: {
77
+ amount: 2,
78
+ type: 'fixed',
79
+ action: 'add',
80
+ },
81
+ name: 'modifier3',
82
+ type: 'tax',
83
+ direct: true,
84
+ included: false,
22
85
  },
23
- { amount: 20, type: "percentage", action: "subtract" },
24
- { amount: 2, type: "fixed", action: "subtract" }
25
86
  ];
26
87
  const orderItem = { price: 30, quantity: 2, modifiers };
27
- const newItem = itemActions.calculateItem(orderItem);
28
- expect(newItem).toHaveProperty("total", 56);
29
- expect(newItem).toHaveProperty("subTotal", 60);
30
- expect(newItem).toHaveProperty("distributed", 12);
88
+ const newItem = pricingService.item.calculate(orderItem);
89
+ expect(newItem).toHaveProperty('total', 48);
90
+ expect(newItem).toHaveProperty('subTotals', {
91
+ discount: -16,
92
+ tax: 4,
93
+ _included: 0,
94
+ _xincluded: -12,
95
+ _direct: -12,
96
+ _xdirect: 0,
97
+ _simple: 60,
98
+ _actual: 60,
99
+ });
31
100
  });
32
- test("Get calculated Item with discount Modifiers", () => {
101
+ test('Get calculated Item: included: true && direct: true', () => {
33
102
  const modifiers = [
34
103
  {
35
- amount: 20,
36
- type: "percentage",
37
- action: "add",
38
- attributes: ["discount"]
104
+ compute: {
105
+ amount: 2,
106
+ type: 'fixed',
107
+ action: 'subtract',
108
+ },
109
+ name: 'modifier1',
110
+ type: 'discount',
111
+ direct: true,
112
+ included: true,
39
113
  },
40
114
  {
41
- amount: 20,
42
- type: "percentage",
43
- action: "subtract",
44
- attributes: ["discount"]
115
+ compute: {
116
+ amount: 20,
117
+ type: 'percentage',
118
+ action: 'subtract',
119
+ },
120
+ name: 'modifier2',
121
+ type: 'discount',
122
+ direct: true,
123
+ included: true,
124
+ },
125
+ {
126
+ compute: {
127
+ amount: 2,
128
+ type: 'fixed',
129
+ action: 'add',
130
+ },
131
+ name: 'modifier3',
132
+ type: 'upcharge',
133
+ direct: true,
134
+ included: true,
45
135
  },
46
- { amount: 2, type: "fixed", action: "subtract", attributes: ["discount"] }
47
136
  ];
48
137
  const orderItem = { price: 30, quantity: 2, modifiers };
49
- const newItem = itemActions.calculateItem(orderItem);
50
- expect(newItem).toHaveProperty("total", 56);
51
- expect(newItem).toHaveProperty("subTotal", 60);
52
- expect(newItem).toHaveProperty("discount", -4);
138
+ const newItem = pricingService.item.calculate(orderItem);
139
+ expect(newItem).toHaveProperty('total', 48);
140
+ expect(newItem).toHaveProperty('subTotals', {
141
+ _included: -12,
142
+ _xincluded: 0,
143
+ _direct: -12,
144
+ _xdirect: 0,
145
+ _simple: 60,
146
+ _actual: 48,
147
+ });
148
+ });
149
+ test('Get calculated Item: included: true, sample 1', () => {
150
+ const modifiers = [
151
+ {
152
+ compute: {
153
+ amount: 2,
154
+ type: 'fixed',
155
+ action: 'add',
156
+ },
157
+ name: 'modifier1',
158
+ type: 'upcharge',
159
+ included: true,
160
+ direct: true,
161
+ },
162
+ ];
163
+ const orderItem = { price: 2, quantity: 1, modifiers };
164
+ const newItem = pricingService.item.calculate(orderItem);
165
+ expect(newItem).toHaveProperty('total', 4);
166
+ expect(newItem).toHaveProperty('subTotals', {
167
+ _included: 2,
168
+ _xincluded: 0,
169
+ _direct: 2,
170
+ _xdirect: 0,
171
+ _simple: 2,
172
+ _actual: 4,
173
+ });
174
+ });
175
+ test('Get calculated Item: included: true, sample 2', () => {
176
+ const modifiers = [
177
+ {
178
+ compute: { amount: 2, type: 'fixed', action: 'add' },
179
+ name: 'modifier1',
180
+ type: 'upcharge',
181
+ included: true,
182
+ direct: true,
183
+ properties: {
184
+ sort: 1,
185
+ },
186
+ },
187
+ {
188
+ compute: {
189
+ amount: 10,
190
+ type: 'percentage',
191
+ action: 'add',
192
+ },
193
+ name: 'modifier2',
194
+ type: 'tax',
195
+ properties: {
196
+ sort: 2,
197
+ },
198
+ included: true,
199
+ direct: true,
200
+ },
201
+ ];
202
+ const orderItem = { price: 2, quantity: 1, modifiers };
203
+ const newItem = pricingService.item.calculate(orderItem);
204
+ expect(newItem).toHaveProperty('total', 4.4);
205
+ expect(newItem).toHaveProperty('subTotals', {
206
+ _included: 2.4,
207
+ _xincluded: 0,
208
+ _direct: 2.4,
209
+ _xdirect: 0,
210
+ _simple: 2,
211
+ _actual: 4.4,
212
+ });
213
+ });
214
+ test('Get calculated Item: included: true, sample 3', () => {
215
+ const modifiers = [
216
+ {
217
+ compute: {
218
+ amount: 2,
219
+ type: 'fixed',
220
+ action: 'subtract',
221
+ },
222
+ name: 'modifier1',
223
+ type: 'discount',
224
+ properties: {
225
+ sort: 1,
226
+ },
227
+ included: true,
228
+ direct: true,
229
+ },
230
+ {
231
+ compute: {
232
+ amount: 2,
233
+ type: 'fixed',
234
+ action: 'add',
235
+ },
236
+ name: 'modifier1',
237
+ type: 'upcharge',
238
+ included: true,
239
+ direct: true,
240
+ properties: {
241
+ sort: 1,
242
+ },
243
+ },
244
+ {
245
+ compute: {
246
+ amount: 10,
247
+ type: 'percentage',
248
+ action: 'add',
249
+ },
250
+ name: 'modifier2',
251
+ type: 'tax',
252
+ properties: {
253
+ sort: 2,
254
+ },
255
+ included: true,
256
+ direct: true,
257
+ },
258
+ {
259
+ compute: {
260
+ amount: 6,
261
+ type: 'percentage',
262
+ action: 'add',
263
+ },
264
+ name: 'modifier3',
265
+ type: 'tax',
266
+ properties: {
267
+ sort: 2,
268
+ },
269
+ included: true,
270
+ direct: true,
271
+ },
272
+ ];
273
+ const orderItem = { price: 2, quantity: 1, modifiers };
274
+ const newItem = pricingService.item.calculate(orderItem);
275
+ expect(newItem).toHaveProperty('total', 2.32);
276
+ expect(newItem).toHaveProperty('subTotals', {
277
+ _xdirect: 0,
278
+ _simple: 2,
279
+ _actual: 2.32,
280
+ _direct: 0.32,
281
+ _included: 0.32,
282
+ _xincluded: 0,
283
+ });
53
284
  });
54
285
  });
@@ -0,0 +1,73 @@
1
+ const usePricing = require('../../index');
2
+
3
+ const pricingService = usePricing();
4
+
5
+ describe('Calculate Function', () => {
6
+ test('calculate with fix amount and add action', () => {
7
+ const modifier = {
8
+ compute: {
9
+ type: 'fixed',
10
+ action: 'add',
11
+ amount: 10,
12
+ },
13
+ name: 'someModifier',
14
+ };
15
+ const orderItem = { price: 30, quantity: 1 };
16
+ const computedModifier = pricingService.modifier.calculate(modifier, orderItem);
17
+ expect(computedModifier._computed).toMatchObject({
18
+ amount: 10,
19
+ description: 'someModifier ($10.00)',
20
+ });
21
+ });
22
+
23
+ test('calculate with fix amount and subtract action', () => {
24
+ const modifier = {
25
+ compute: {
26
+ type: 'fixed',
27
+ action: 'subtract',
28
+ amount: 10,
29
+ },
30
+ name: 'someModifier',
31
+ };
32
+ const orderItem = { price: 30, quantity: 1 };
33
+ const computedModifier = pricingService.modifier.calculate(modifier, orderItem);
34
+ expect(computedModifier._computed).toMatchObject({
35
+ amount: -10,
36
+ description: 'someModifier (-$10.00)',
37
+ });
38
+ });
39
+
40
+ test('calculate with percentage amount and subtract action', () => {
41
+ const modifier = {
42
+ compute: {
43
+ amount: 20,
44
+ type: 'percentage',
45
+ action: 'subtract',
46
+ },
47
+ name: 'someModifier',
48
+ };
49
+ const orderItem = { price: 30, quantity: 1 };
50
+ const computedModifier = pricingService.modifier.calculate(modifier, orderItem);
51
+ expect(computedModifier._computed).toMatchObject({
52
+ amount: -6,
53
+ description: 'someModifier (-$6.00)',
54
+ });
55
+ });
56
+
57
+ test('calculate with percentage amount and add action', () => {
58
+ const modifier = {
59
+ compute: {
60
+ amount: 20,
61
+ type: 'percentage',
62
+ action: 'add',
63
+ },
64
+ name: 'someModifier',
65
+ };
66
+ const orderItem = { price: 30, quantity: 1 };
67
+ const computedModifier = pricingService.modifier.calculate(modifier, orderItem);
68
+ expect(computedModifier._computed).toMatchObject({
69
+ amount: 6,
70
+ description: 'someModifier ($6.00)',
71
+ });
72
+ });
73
+ });
@@ -0,0 +1,81 @@
1
+ const usePricing = require('../../index');
2
+
3
+ const pricingService = usePricing();
4
+
5
+ describe('GetAvailablePromotionsOrSubscriptions Function', () => {
6
+ test('usecases', () => {
7
+ const today = new Date();
8
+ expect(
9
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions()
10
+ ).toStrictEqual([]);
11
+
12
+ const modifier = { attributes: ['promotion'] };
13
+ expect(
14
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
15
+ ).toStrictEqual([modifier]);
16
+
17
+ modifier.properties = {
18
+ promotion: { numberOfUsesLimit: 4, numberOfUses: 3 },
19
+ };
20
+ expect(
21
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
22
+ ).toStrictEqual([modifier]);
23
+
24
+ modifier.properties.promotion.numberOfUses = 4;
25
+ expect(
26
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
27
+ ).toStrictEqual([]);
28
+
29
+ modifier.properties.promotion.numberOfUses = 5;
30
+ expect(
31
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
32
+ ).toStrictEqual([]);
33
+
34
+ modifier.properties = {
35
+ promotion: { dateLimit: { from: today.toISOString(), to: '' } },
36
+ };
37
+ expect(
38
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
39
+ ).toStrictEqual([modifier]);
40
+
41
+ const tomorrow = new Date(today);
42
+ tomorrow.setDate(tomorrow.getDate() + 1);
43
+ modifier.properties.promotion.dateLimit.from = tomorrow.toISOString();
44
+ expect(
45
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
46
+ ).toStrictEqual([]);
47
+
48
+ const yesterday = new Date(today);
49
+ yesterday.setDate(yesterday.getDate() - 1);
50
+ modifier.properties.promotion.dateLimit.from = yesterday.toISOString();
51
+ expect(
52
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
53
+ ).toStrictEqual([modifier]);
54
+
55
+ modifier.properties.promotion.dateLimit.to = yesterday.toISOString();
56
+ expect(
57
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
58
+ ).toStrictEqual([]);
59
+
60
+ modifier.properties.promotion.dateLimit.to = today.toISOString();
61
+ expect(
62
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
63
+ ).toStrictEqual([modifier]);
64
+
65
+ modifier.properties.promotion.dateLimit.to = tomorrow.toISOString();
66
+ expect(
67
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
68
+ ).toStrictEqual([modifier]);
69
+
70
+ modifier.attributes = ['subscription'];
71
+ modifier.properties.subscription = modifier.properties.promotion;
72
+ expect(
73
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
74
+ ).toStrictEqual([modifier]);
75
+
76
+ delete modifier.properties;
77
+ expect(
78
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
79
+ ).toStrictEqual([]);
80
+ });
81
+ });