@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.
- package/.eslintrc +8 -10
- package/README.md +108 -4
- package/__TEST__/item.test.js +265 -34
- package/__TEST__/modifier/calculate.test.js +73 -0
- package/__TEST__/modifier/getAvailablePromotionsOrSubscriptions.test.js +81 -0
- package/__TEST__/modifier/getMatchTagsModifiers.test.js +109 -0
- package/__TEST__/modifier/getModifierIndex.test.js +40 -0
- package/__TEST__/modifier/getRecommendedModifiers.test.js +17 -0
- package/__TEST__/modifier/hasAllTags.test.js +25 -0
- package/__TEST__/modifier/hasAttribute.test.js +47 -0
- package/__TEST__/modifier/hasMatchTags.test.js +56 -0
- package/__TEST__/modifier/hasNoTags.test.js +21 -0
- package/__TEST__/modifier/mocks/toggleModifier_no1.json +13440 -0
- package/__TEST__/modifier/mocks/toggleModifier_no2.json +13438 -0
- package/__TEST__/modifier/mocks/toggleModifier_no3.json +13598 -0
- package/__TEST__/modifier/sort.test.js +41 -0
- package/__TEST__/order.test.js +147 -74
- package/index.js +1 -9
- package/lib/constants/Modifier/Attributes.js +9 -0
- package/lib/constants/Modifier/Compute.js +15 -0
- package/lib/constants/Modifier/Types.js +7 -0
- package/lib/constants/Modifier/index.js +9 -0
- package/lib/constants/Status.js +39 -0
- package/lib/constants/index.js +7 -0
- package/lib/index.js +38 -0
- 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/modifier/addFile.js +19 -0
- package/lib/modifier/addImage.js +18 -0
- package/lib/modifier/calculate.js +43 -0
- package/lib/modifier/contains.js +5 -0
- package/lib/modifier/convertMenuModifiers.js +5 -0
- package/lib/modifier/create.js +44 -0
- package/lib/modifier/createAmountOverrideModifier.js +20 -0
- package/lib/modifier/createCreditModifier.js +32 -0
- package/lib/modifier/createDiscountModifier.js +25 -0
- package/lib/modifier/createGroupValuesModifier.js +29 -0
- package/lib/modifier/createIndirectModifier.js +42 -0
- package/lib/modifier/createSubscriptionModifier.js +19 -0
- package/lib/modifier/displayAsCard.js +9 -0
- package/lib/modifier/displayAsChips.js +9 -0
- package/lib/modifier/displayAsList.js +9 -0
- package/lib/modifier/duplicate.js +12 -0
- package/lib/modifier/enableAutoPopup.js +9 -0
- package/lib/modifier/filterByRequiredModifiers.js +4 -0
- package/lib/modifier/findById.js +7 -0
- package/lib/modifier/findByPaymentMethod.js +10 -0
- package/lib/modifier/findByPaymentType.js +10 -0
- package/lib/modifier/getAvailablePromotions.js +5 -0
- package/lib/modifier/getAvailablePromotionsOrSubscriptions.js +15 -0
- package/lib/modifier/getAvailableSubscriptions.js +5 -0
- package/lib/modifier/getByAttribute.js +6 -0
- package/lib/modifier/getCreditModifier.js +6 -0
- package/lib/modifier/getDefaultModifiers.js +4 -0
- package/lib/modifier/getDepartmentMaxItems.js +10 -0
- package/lib/modifier/getDepartmentModifiers.js +11 -0
- package/lib/modifier/getDescription.js +6 -0
- package/lib/modifier/getDescriptions.js +9 -0
- package/lib/modifier/getDirectModifiers.js +5 -0
- package/lib/modifier/getDiscountModifiers.js +7 -0
- package/lib/modifier/getEntityModifiers.js +11 -0
- package/lib/modifier/getEntityPromotions.js +11 -0
- package/lib/modifier/getExtendedTags.js +14 -0
- package/lib/modifier/getFiles.js +5 -0
- package/lib/modifier/getGroupModifiers.js +9 -0
- package/lib/modifier/getGroupOfItemsModifiers.js +4 -0
- package/lib/modifier/getGroupRelatedModifiers.js +26 -0
- package/lib/modifier/getImages.js +5 -0
- package/lib/modifier/getInheritedModifiers.js +30 -0
- package/lib/modifier/getItemModifiers.js +39 -0
- package/lib/modifier/getLaundryModifiers.js +9 -0
- package/lib/modifier/getMatchTagsModifiers.js +9 -0
- package/lib/modifier/getModifierIndex.js +14 -0
- package/lib/modifier/getPreferences.js +24 -0
- package/lib/modifier/getPromotionModifiers.js +5 -0
- package/lib/modifier/getPromptMessage.js +6 -0
- package/lib/modifier/getProperty.js +11 -0
- package/lib/modifier/getRecommendedModifiers.js +7 -0
- package/lib/modifier/getRelatedModifiers.js +10 -0
- package/lib/modifier/getRequiredModifiers.js +4 -0
- package/lib/modifier/getSelectedValues.js +7 -0
- package/lib/modifier/getService.js +5 -0
- package/lib/modifier/getSplittedModifiers.js +30 -0
- package/lib/modifier/getStarchModifier.js +9 -0
- package/lib/modifier/getSubscriptionItem.js +12 -0
- package/lib/modifier/getSubscriptionModifiers.js +5 -0
- package/lib/modifier/getSuggestion.js +9 -0
- package/lib/modifier/getUnhiddenModifiers.js +9 -0
- package/lib/modifier/getUsingCount.js +11 -0
- package/lib/modifier/hasAddModifiers.js +5 -0
- package/lib/modifier/hasAllTag.js +12 -0
- package/lib/modifier/hasAttribute.js +8 -0
- package/lib/modifier/hasAttributes.js +19 -0
- package/lib/modifier/hasCreateSubscription.js +10 -0
- package/lib/modifier/hasDisplayPrompt.js +9 -0
- package/lib/modifier/hasMatchTags.js +27 -0
- package/lib/modifier/hasModifier.js +12 -0
- package/lib/modifier/hasNoTags.js +9 -0
- package/lib/modifier/hasRelatedItems.js +8 -0
- package/lib/modifier/includePiecesInQuantity.js +10 -0
- package/lib/modifier/includesInGroup.js +9 -0
- package/lib/modifier/index.js +240 -6
- package/lib/modifier/isAmountOverride.js +7 -0
- package/lib/modifier/isAvailablePromotion.js +23 -0
- package/lib/modifier/isAvailableSubscription.js +28 -0
- package/lib/modifier/isCompact.js +12 -0
- package/lib/modifier/isCredit.js +6 -0
- package/lib/modifier/isCustomerTagsExtend.js +9 -0
- package/lib/modifier/isDefault.js +4 -0
- package/lib/modifier/isDepartment.js +6 -0
- package/lib/modifier/isDirect.js +6 -0
- package/lib/modifier/isDiscount.js +6 -0
- package/lib/modifier/isEditable.js +10 -0
- package/lib/modifier/isGratuity.js +6 -0
- package/lib/modifier/isGroupOfItems.js +12 -0
- package/lib/modifier/isGroupOfModifiers.js +12 -0
- package/lib/modifier/isGroupOfValues.js +13 -0
- package/lib/modifier/isHidden.js +4 -0
- package/lib/modifier/isIncluded.js +4 -0
- package/lib/modifier/isManual.js +9 -0
- package/lib/modifier/isMultiplier.js +11 -0
- package/lib/modifier/isOverride.js +6 -0
- package/lib/modifier/isPaymentMethods.js +8 -0
- package/lib/modifier/isPaymentTypes.js +8 -0
- package/lib/modifier/isPreferences.js +8 -0
- package/lib/modifier/isPriceOverride.js +7 -0
- package/lib/modifier/isPromotion.js +6 -0
- package/lib/modifier/isQuantityOverride.js +7 -0
- package/lib/modifier/isRemoveModifier.js +4 -0
- package/lib/modifier/isRequired.js +4 -0
- package/lib/modifier/isRequiredAndOverride.js +4 -0
- package/lib/modifier/isService.js +9 -0
- package/lib/modifier/isSingleValue.js +9 -0
- package/lib/modifier/isSubscription.js +6 -0
- package/lib/modifier/isTrackUsageSubscription.js +5 -0
- package/lib/modifier/isValueSelected.js +7 -0
- package/lib/modifier/purifyModifiers.js +15 -0
- package/lib/modifier/sort.js +35 -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 +4 -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 +10 -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 +19 -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 +164 -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 +55 -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 +17 -15
- package/__TEST__/modifier.test.js +0 -126
- package/lib/item/actions.js +0 -140
- package/lib/modifier/actions.js +0 -172
- package/lib/modifier/constants.js +0 -46
- 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
|
@@ -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
|
+
});
|
package/__TEST__/order.test.js
CHANGED
|
@@ -1,90 +1,137 @@
|
|
|
1
|
-
const
|
|
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
|
-
{
|
|
7
|
-
|
|
8
|
-
|
|
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 =
|
|
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('
|
|
16
|
-
|
|
17
|
-
|
|
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: [
|
|
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: [
|
|
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 =
|
|
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('
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
|
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
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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('
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
expect(newOrder).toHaveProperty('
|
|
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
|
|
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
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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 =
|
|
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('
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
expect(newOrder).toHaveProperty('
|
|
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
|
|
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
|
-
|
|
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%
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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 = {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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('
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
228
|
+
expect(newOrder).toHaveProperty('subTotals', {
|
|
229
|
+
discount: -1,
|
|
230
|
+
tax: 2,
|
|
231
|
+
});
|
|
159
232
|
});
|
|
160
233
|
});
|
package/index.js
CHANGED
|
@@ -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
|
+
});
|
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
|
+
};
|