@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.
- package/.eslintrc +22 -0
- package/README.md +110 -4
- package/__TEST__/item.test.js +285 -0
- package/__TEST__/order.test.js +234 -0
- package/index.js +1 -0
- package/lib/constants/index.js +5 -0
- package/lib/constants/status.js +39 -0
- package/lib/index.js +36 -9
- package/lib/item/addIndirectModifier.js +55 -0
- package/lib/item/calculate.js +108 -0
- package/lib/item/create.js +35 -0
- package/lib/item/filterByDirectModifiers.js +13 -0
- package/lib/item/findOriginalItem.js +8 -0
- package/lib/item/getBalanceToPay.js +12 -0
- package/lib/item/getDepartmentModifiers.js +5 -0
- package/lib/item/getDiscountModifiers.js +7 -0
- package/lib/item/getItemModifiers.js +16 -0
- package/lib/item/getItemModifiersDescription.js +22 -0
- package/lib/item/getItemPrice.js +57 -0
- package/lib/item/getItemsTotalPaid.js +9 -0
- package/lib/item/getItemsTotalPieces.js +7 -0
- package/lib/item/getNotIncludedModifiers.js +11 -0
- package/lib/item/getParentItem.js +5 -0
- package/lib/item/getPendingIndex.js +5 -0
- package/lib/item/getTotal.js +8 -0
- package/lib/item/getTotalPieces.js +7 -0
- package/lib/item/getTotals.js +25 -0
- package/lib/item/hasCreateSubscription.js +7 -0
- package/lib/item/hasModifier.js +15 -0
- package/lib/item/hasModifierWithValue.js +11 -0
- package/lib/item/hasModifiers.js +9 -0
- package/lib/item/hasSubscription.js +5 -0
- package/lib/item/index.js +80 -4
- package/lib/item/isParentIncluded.js +4 -0
- package/lib/item/isRelatedItem.js +4 -0
- package/lib/item/isSelected.js +13 -0
- package/lib/item/openRecommendationDialog.js +4 -0
- package/lib/item/removeModifier.js +84 -0
- package/lib/item/removeModifierById.js +8 -0
- package/lib/item/removeModifiers.js +14 -0
- package/lib/order/addCreditModifier.js +27 -0
- package/lib/order/addDiscountModifier.js +9 -0
- package/lib/order/addItem.js +292 -0
- package/lib/order/addItemModifier.js +214 -0
- package/lib/order/addModifier.js +9 -0
- package/lib/order/addModifierToAllItems.js +16 -0
- package/lib/order/addNote.js +25 -0
- package/lib/order/addTable.js +11 -0
- package/lib/order/calculate.js +105 -0
- package/lib/order/calculateDue.js +15 -0
- package/lib/order/calculateOrdersDue.js +26 -0
- package/lib/order/canSplit.js +4 -0
- package/lib/order/changeEndDate.js +15 -0
- package/lib/order/changeLocation.js +20 -0
- package/lib/order/changeStartDate.js +14 -0
- package/lib/order/combineSubOrders.js +14 -0
- package/lib/order/containsSerial.js +5 -0
- package/lib/order/create.js +67 -0
- package/lib/order/createParent.js +9 -0
- package/lib/order/duplicateItem.js +16 -0
- package/lib/order/findItemIndex.js +6 -0
- package/lib/order/getAppliedCredit.js +5 -0
- package/lib/order/getBalance.js +9 -0
- package/lib/order/getCustomer.js +4 -0
- package/lib/order/getCustomerStoreCredit.js +14 -0
- package/lib/order/getCustomerSubscriptions.js +8 -0
- package/lib/order/getCustomerSubscriptionsByItem.js +14 -0
- package/lib/order/getDepartmentModifiers.js +9 -0
- package/lib/order/getEndDate.js +6 -0
- package/lib/order/getItemByItemId.js +6 -0
- package/lib/order/getItemIndex.js +9 -0
- package/lib/order/getItemsToPay.js +17 -0
- package/lib/order/getMenuItemsByCustomer.js +19 -0
- package/lib/order/getNumberOfItems.js +12 -0
- package/lib/order/getOrdersPieces.js +10 -0
- package/lib/order/getPendingItemIndex.js +5 -0
- package/lib/order/getRelatedItems.js +11 -0
- package/lib/order/getSameItems.js +14 -0
- package/lib/order/getScheduleByCustomer.js +20 -0
- package/lib/order/getScreenCustomerNotes.js +12 -0
- package/lib/order/getSelectedItem.js +6 -0
- package/lib/order/getStartDate.js +4 -0
- package/lib/order/getTip.js +10 -0
- package/lib/order/getTotalPieces.js +17 -0
- package/lib/order/getTotals.js +28 -0
- package/lib/order/hasAggregate.js +20 -0
- package/lib/order/hasItem.js +7 -0
- package/lib/order/hasRemainingSubscription.js +67 -0
- package/lib/order/hold.js +12 -0
- package/lib/order/index.js +167 -4
- package/lib/order/isDetailed.js +4 -0
- package/lib/order/isHold.js +4 -0
- package/lib/order/isNew.js +4 -0
- package/lib/order/isOpen.js +4 -0
- package/lib/order/isParent.js +7 -0
- package/lib/order/merge.js +96 -0
- package/lib/order/open.js +11 -0
- package/lib/order/pickEndDate.js +70 -0
- package/lib/order/removeApplyModifier.js +24 -0
- package/lib/order/removeDiscountModifier.js +16 -0
- package/lib/order/removeItem.js +56 -0
- package/lib/order/removeItemByIndex.js +5 -0
- package/lib/order/removeItemModifier.js +69 -0
- package/lib/order/removeModifier.js +21 -0
- package/lib/order/removeModifierByAttribute.js +23 -0
- package/lib/order/removeModifiersWithPaymentMethods.js +29 -0
- package/lib/order/removeModifiersWithPaymentTypes.js +27 -0
- package/lib/order/setCustomer.js +50 -0
- package/lib/order/splitByDepartments.js +151 -0
- package/lib/order/toggleModifier.js +9 -0
- package/lib/order/updateItem.js +8 -0
- package/lib/order/updateItemQuantity.js +35 -0
- package/lib/order/void.js +11 -0
- package/package.json +20 -23
- package/lib/item/actions.js +0 -135
- package/lib/modifier/actions.js +0 -163
- package/lib/modifier/constants.js +0 -46
- package/lib/modifier/index.js +0 -8
- package/lib/order/actions.js +0 -47
- package/lib/utils/defaultPrices.js +0 -10
- package/lib/utils/helpers.js +0 -26
- package/lib/utils/index.js +0 -9
- package/lib/utils/math.js +0 -71
package/.eslintrc
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": ["airbnb-base", "prettier", "plugin:jest/recommended"],
|
|
3
|
+
"env": {
|
|
4
|
+
"node": true,
|
|
5
|
+
"jest/globals": true
|
|
6
|
+
},
|
|
7
|
+
"plugins": ["prettier", "jest"],
|
|
8
|
+
"rules": {
|
|
9
|
+
"no-debugger": 0,
|
|
10
|
+
"no-underscore-dangle": 0,
|
|
11
|
+
"no-console": 0,
|
|
12
|
+
"global-require": 0,
|
|
13
|
+
"prettier/prettier": ["error", {
|
|
14
|
+
"singleQuote": true,
|
|
15
|
+
"trailingComma": "es5",
|
|
16
|
+
"bracketSpacing": true,
|
|
17
|
+
"bracketSameLine": false,
|
|
18
|
+
"arrowParens": "avoid",
|
|
19
|
+
"parser": "flow"
|
|
20
|
+
}]
|
|
21
|
+
}
|
|
22
|
+
}
|
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# @dark/pricing
|
|
2
|
+
|
|
1
3
|
## Dark Pricing
|
|
2
4
|
|
|
3
5
|
> TODO: calculation of defined schemas
|
|
@@ -5,9 +7,113 @@
|
|
|
5
7
|
### Usage
|
|
6
8
|
|
|
7
9
|
```
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
+
const usePricing = require('@darkpos/pricing');
|
|
11
|
+
|
|
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
|
+
}
|
|
10
99
|
|
|
11
|
-
const order = {
|
|
12
|
-
|
|
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
|
+
}
|
|
13
119
|
```
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
const usePricing = require('../index');
|
|
2
|
+
|
|
3
|
+
const pricingService = usePricing();
|
|
4
|
+
|
|
5
|
+
describe('Item actions', () => {
|
|
6
|
+
test('Get calculated Item: included: false && direct: false', () => {
|
|
7
|
+
const modifiers = [
|
|
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
|
+
},
|
|
41
|
+
];
|
|
42
|
+
const orderItem = { price: 30, quantity: 2, modifiers };
|
|
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
|
+
});
|
|
54
|
+
});
|
|
55
|
+
test('Get calculated Item: included: false && direct: true', () => {
|
|
56
|
+
const modifiers = [
|
|
57
|
+
{
|
|
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,
|
|
85
|
+
},
|
|
86
|
+
];
|
|
87
|
+
const orderItem = { price: 30, quantity: 2, modifiers };
|
|
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
|
+
});
|
|
100
|
+
});
|
|
101
|
+
test('Get calculated Item: included: true && direct: true', () => {
|
|
102
|
+
const modifiers = [
|
|
103
|
+
{
|
|
104
|
+
compute: {
|
|
105
|
+
amount: 2,
|
|
106
|
+
type: 'fixed',
|
|
107
|
+
action: 'subtract',
|
|
108
|
+
},
|
|
109
|
+
name: 'modifier1',
|
|
110
|
+
type: 'discount',
|
|
111
|
+
direct: true,
|
|
112
|
+
included: true,
|
|
113
|
+
},
|
|
114
|
+
{
|
|
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,
|
|
135
|
+
},
|
|
136
|
+
];
|
|
137
|
+
const orderItem = { price: 30, quantity: 2, modifiers };
|
|
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
|
+
});
|
|
284
|
+
});
|
|
285
|
+
});
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
const usePricing = require('../index');
|
|
2
|
+
|
|
3
|
+
const pricingService = usePricing();
|
|
4
|
+
|
|
5
|
+
describe('Order actions', () => {
|
|
6
|
+
test('Get calculated Order, one item', () => {
|
|
7
|
+
const modifiers = [
|
|
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
|
+
},
|
|
31
|
+
];
|
|
32
|
+
const orderItem = { price: 30, quantity: 2, modifiers };
|
|
33
|
+
const order = { items: [orderItem] };
|
|
34
|
+
const newOrder = pricingService.order.calculate(order);
|
|
35
|
+
|
|
36
|
+
expect(newOrder).toHaveProperty('total', 56);
|
|
37
|
+
expect(newOrder).toHaveProperty('subTotal', 60);
|
|
38
|
+
expect(newOrder).toHaveProperty('subTotals', {
|
|
39
|
+
discount: -4,
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
test('Get calculated Order, multiple items', () => {
|
|
43
|
+
const item1 = {
|
|
44
|
+
_id: 1,
|
|
45
|
+
price: 30,
|
|
46
|
+
quantity: 2,
|
|
47
|
+
modifiers: [
|
|
48
|
+
{
|
|
49
|
+
compute: {
|
|
50
|
+
amount: 2,
|
|
51
|
+
type: 'fixed',
|
|
52
|
+
action: 'subtract',
|
|
53
|
+
},
|
|
54
|
+
name: 'modifier1',
|
|
55
|
+
type: 'discount',
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
};
|
|
59
|
+
const item2 = {
|
|
60
|
+
_id: 2,
|
|
61
|
+
price: 10,
|
|
62
|
+
quantity: 1,
|
|
63
|
+
modifiers: [
|
|
64
|
+
{
|
|
65
|
+
compute: {
|
|
66
|
+
amount: 2,
|
|
67
|
+
type: 'fixed',
|
|
68
|
+
action: 'subtract',
|
|
69
|
+
},
|
|
70
|
+
name: 'modifier1',
|
|
71
|
+
type: 'discount',
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
};
|
|
75
|
+
const order = { items: [item1, item2] };
|
|
76
|
+
const newOrder = pricingService.order.calculate(order);
|
|
77
|
+
expect(newOrder).toHaveProperty('total', 64);
|
|
78
|
+
expect(newOrder).toHaveProperty('subTotal', 70);
|
|
79
|
+
expect(newOrder).toHaveProperty('subTotals', {
|
|
80
|
+
discount: -6,
|
|
81
|
+
});
|
|
82
|
+
// console.log(JSON.stringify(newOrder, 0, 2));
|
|
83
|
+
});
|
|
84
|
+
test('Get calculated Order, multiple items and indirect modifiers', () => {
|
|
85
|
+
const item1 = {
|
|
86
|
+
_id: 1,
|
|
87
|
+
price: 30,
|
|
88
|
+
quantity: 2,
|
|
89
|
+
};
|
|
90
|
+
const item2 = {
|
|
91
|
+
_id: 2,
|
|
92
|
+
price: 10,
|
|
93
|
+
quantity: 1,
|
|
94
|
+
};
|
|
95
|
+
const modifier1 = {
|
|
96
|
+
compute: {
|
|
97
|
+
amount: 2,
|
|
98
|
+
type: 'fixed',
|
|
99
|
+
action: 'subtract',
|
|
100
|
+
},
|
|
101
|
+
name: 'modifier1',
|
|
102
|
+
type: 'discount',
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const order = { items: [item1, item2], modifiers: [modifier1] };
|
|
106
|
+
|
|
107
|
+
const newOrder = pricingService.order.calculate(order);
|
|
108
|
+
expect(newOrder).toHaveProperty('total', 68);
|
|
109
|
+
expect(newOrder).toHaveProperty('subTotal', 70);
|
|
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
|
+
});
|
|
133
|
+
});
|
|
134
|
+
test('Get calculated Order, multiple items and indirect modifiers #2', () => {
|
|
135
|
+
const item1 = {
|
|
136
|
+
_id: 1,
|
|
137
|
+
price: 15.99,
|
|
138
|
+
quantity: 1,
|
|
139
|
+
};
|
|
140
|
+
const item2 = {
|
|
141
|
+
_id: 2,
|
|
142
|
+
price: 4.65,
|
|
143
|
+
quantity: 1,
|
|
144
|
+
};
|
|
145
|
+
const modifier1 = {
|
|
146
|
+
compute: {
|
|
147
|
+
amount: 10,
|
|
148
|
+
action: 'subtract',
|
|
149
|
+
type: 'percentage',
|
|
150
|
+
},
|
|
151
|
+
name: 'modifier1',
|
|
152
|
+
type: 'discount',
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const order = { items: [item1, item2], modifiers: [modifier1] };
|
|
156
|
+
const newOrder = pricingService.order.calculate(order);
|
|
157
|
+
expect(newOrder).toHaveProperty('total', 18.58);
|
|
158
|
+
expect(newOrder).toHaveProperty('subTotal', 20.64);
|
|
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
|
+
});
|
|
182
|
+
});
|
|
183
|
+
test('Get calculated Order, multiple items and indirect modifiers #3', () => {
|
|
184
|
+
const item1 = {
|
|
185
|
+
_id: 1,
|
|
186
|
+
price: 3,
|
|
187
|
+
quantity: 1,
|
|
188
|
+
};
|
|
189
|
+
const item2 = {
|
|
190
|
+
_id: 2,
|
|
191
|
+
price: 3,
|
|
192
|
+
quantity: 1,
|
|
193
|
+
};
|
|
194
|
+
const item3 = {
|
|
195
|
+
_id: 3,
|
|
196
|
+
price: 4,
|
|
197
|
+
quantity: 1,
|
|
198
|
+
};
|
|
199
|
+
const modifier1 = {
|
|
200
|
+
_id: 1,
|
|
201
|
+
compute: {
|
|
202
|
+
amount: 10,
|
|
203
|
+
type: 'percentage',
|
|
204
|
+
action: 'subtract',
|
|
205
|
+
},
|
|
206
|
+
name: `10% Discount`,
|
|
207
|
+
type: 'discount',
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const modifier2 = {
|
|
211
|
+
_id: 2,
|
|
212
|
+
name: `20% tax`,
|
|
213
|
+
type: 'tax',
|
|
214
|
+
compute: {
|
|
215
|
+
amount: 20,
|
|
216
|
+
type: 'percentage',
|
|
217
|
+
action: 'add',
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
|
|
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);
|
|
227
|
+
expect(newOrder).toHaveProperty('subTotal', 10);
|
|
228
|
+
expect(newOrder).toHaveProperty('subTotals', {
|
|
229
|
+
discount: -1,
|
|
230
|
+
tax: 2,
|
|
231
|
+
});
|
|
232
|
+
console.log('order =>', JSON.stringify(newOrder, 0, 2));
|
|
233
|
+
});
|
|
234
|
+
});
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./lib');
|
|
@@ -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
|
+
});
|