@darkpos/pricing 1.0.92 → 1.0.95
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/__TEST__/item.test.js +46 -48
- package/__TEST__/modifier/addItemModifier.test.js +181 -60
- package/__TEST__/modifier/overrideModifiers.test.js +196 -0
- package/__TEST__/order/conditionsNotMet.test.js +124 -0
- package/__TEST__/order/getParentTotals.test.js +79 -0
- package/__TEST__/order/order-payment-modifier.test.js +335 -28
- package/lib/item/calculate.js +76 -19
- package/lib/item/getItemPrice.js +1 -12
- package/lib/item/getPriceWithoutModifiers.js +7 -5
- package/lib/item/hasPaymentMethodType.js +7 -1
- package/lib/item/index.js +6 -0
- package/lib/item/isOverwrittenPrice.js +4 -0
- package/lib/item/isOverwrittenQuantity.js +4 -0
- package/lib/item/overrideNotes.js +24 -0
- package/lib/item/removeModifier.js +1 -22
- package/lib/item/removeModifiers.js +1 -2
- package/lib/modifier/areConditionsMet.js +4 -1
- package/lib/modifier/calculate.js +20 -2
- package/lib/modifier/calculatePaymentModifier.js +2 -0
- package/lib/modifier/createDescription.js +1 -1
- package/lib/modifier/duplicate.js +3 -0
- package/lib/modifier/getOverrideAmount.js +7 -0
- package/lib/modifier/getOverrideNote.js +5 -0
- package/lib/modifier/getServiceOverride.js +5 -0
- package/lib/modifier/hasPaymentMethodType.js +21 -6
- package/lib/modifier/index.js +20 -12
- package/lib/modifier/{isManual.js → isManualOverride.js} +1 -1
- package/lib/modifier/{isMultiplier.js → isMultiplierOverride.js} +1 -1
- package/lib/modifier/isNotesOverride.js +7 -0
- package/lib/modifier/isOverrideSubtotal.js +9 -0
- package/lib/modifier/isPaymentModifier.js +2 -1
- package/lib/modifier/isPercentage.js +1 -1
- package/lib/modifier/isPrepayModifier.js +11 -0
- package/lib/modifier/{isService.js → isServiceOverride.js} +1 -1
- package/lib/modifier/isTotalOverride.js +7 -0
- package/lib/modifier/sort.js +16 -27
- package/lib/order/addItem.js +10 -12
- package/lib/order/addItemModifier.js +15 -62
- package/lib/order/getTotals.js +21 -22
- package/lib/order/removeItemModifier.js +2 -27
- package/lib/order/splitItems.js +2 -1
- package/package.json +5 -2
- package/lib/modifier/createAmountOverrideModifier.js +0 -18
- package/lib/modifier/getService.js +0 -5
- package/lib/modifier/mutateModifier.js +0 -23
|
@@ -884,4 +884,128 @@ describe('Conditions not met for the item', () => {
|
|
|
884
884
|
expect(order.items[1].total).toBe(20);
|
|
885
885
|
expect(order.items[1].modifiers.length).toBe(1);
|
|
886
886
|
});
|
|
887
|
+
|
|
888
|
+
test('Payment type condition is met (payment modifier and using prepay at the same time)', () => {
|
|
889
|
+
const prePayDiscount1Usd = {
|
|
890
|
+
_id: '6819114c06c23d37c1f19412',
|
|
891
|
+
name: '1 usd pre pay disc',
|
|
892
|
+
type: 'discount',
|
|
893
|
+
tags: ['default'],
|
|
894
|
+
direct: true,
|
|
895
|
+
conditions: {
|
|
896
|
+
valid: null,
|
|
897
|
+
rules: [
|
|
898
|
+
{
|
|
899
|
+
key: 'paymentMethods',
|
|
900
|
+
value: ['cash'],
|
|
901
|
+
operand: '$in',
|
|
902
|
+
},
|
|
903
|
+
{
|
|
904
|
+
key: 'paymentTypes',
|
|
905
|
+
value: ['cash'],
|
|
906
|
+
operand: '$in',
|
|
907
|
+
},
|
|
908
|
+
{
|
|
909
|
+
key: 'payment',
|
|
910
|
+
value: 'prepay',
|
|
911
|
+
operand: '$in',
|
|
912
|
+
},
|
|
913
|
+
],
|
|
914
|
+
},
|
|
915
|
+
compute: {
|
|
916
|
+
type: 'fixed',
|
|
917
|
+
action: 'subtract',
|
|
918
|
+
amount: 1,
|
|
919
|
+
},
|
|
920
|
+
};
|
|
921
|
+
|
|
922
|
+
const item = {
|
|
923
|
+
_id: 'abc',
|
|
924
|
+
price: 10,
|
|
925
|
+
quantity: 1,
|
|
926
|
+
modifiers: [prePayDiscount1Usd],
|
|
927
|
+
};
|
|
928
|
+
|
|
929
|
+
const resultedOrder = pricingService.order.calculate(
|
|
930
|
+
{
|
|
931
|
+
id: 'ord-123',
|
|
932
|
+
items: [item],
|
|
933
|
+
modifiers: [],
|
|
934
|
+
},
|
|
935
|
+
{
|
|
936
|
+
paymentMethod: 'cash',
|
|
937
|
+
paymentType: 'cash',
|
|
938
|
+
}
|
|
939
|
+
);
|
|
940
|
+
|
|
941
|
+
expect(resultedOrder.total).toBe(10);
|
|
942
|
+
expect(resultedOrder.items[0].total).toBe(10);
|
|
943
|
+
expect(resultedOrder.items[0].modifiers[0]._computed.amount).toBe(0);
|
|
944
|
+
|
|
945
|
+
const resultedOrderPrepay = pricingService.order.calculate(resultedOrder, {
|
|
946
|
+
paymentMethod: 'cash',
|
|
947
|
+
paymentType: 'cash',
|
|
948
|
+
isPrepay: true,
|
|
949
|
+
});
|
|
950
|
+
|
|
951
|
+
expect(resultedOrderPrepay.total).toBe(9);
|
|
952
|
+
expect(resultedOrderPrepay.items[0].total).toBe(9);
|
|
953
|
+
expect(resultedOrderPrepay.items[0].modifiers[0]._computed.amount).toBe(0);
|
|
954
|
+
expect(resultedOrderPrepay.items[0].modifiers[1]._computed.amount).toBe(-1);
|
|
955
|
+
});
|
|
956
|
+
|
|
957
|
+
test('Payment type condition is met (using only prepay rule)', () => {
|
|
958
|
+
const prePayDiscount1Usd = {
|
|
959
|
+
_id: '6819114c06c23d37c1f19412',
|
|
960
|
+
name: '1 usd pre pay disc',
|
|
961
|
+
type: 'discount',
|
|
962
|
+
tags: ['default'],
|
|
963
|
+
direct: true,
|
|
964
|
+
conditions: {
|
|
965
|
+
valid: null,
|
|
966
|
+
rules: [
|
|
967
|
+
{
|
|
968
|
+
key: 'payment',
|
|
969
|
+
value: 'prepay',
|
|
970
|
+
operand: '$in',
|
|
971
|
+
},
|
|
972
|
+
],
|
|
973
|
+
},
|
|
974
|
+
compute: {
|
|
975
|
+
type: 'fixed',
|
|
976
|
+
action: 'subtract',
|
|
977
|
+
amount: 1,
|
|
978
|
+
},
|
|
979
|
+
};
|
|
980
|
+
|
|
981
|
+
const item = {
|
|
982
|
+
_id: 'abc',
|
|
983
|
+
price: 10,
|
|
984
|
+
quantity: 1,
|
|
985
|
+
modifiers: [prePayDiscount1Usd],
|
|
986
|
+
};
|
|
987
|
+
|
|
988
|
+
const resultedOrder = pricingService.order.calculate({
|
|
989
|
+
id: 'ord-123',
|
|
990
|
+
items: [item],
|
|
991
|
+
modifiers: [],
|
|
992
|
+
});
|
|
993
|
+
|
|
994
|
+
expect(resultedOrder.total).toBe(10);
|
|
995
|
+
expect(resultedOrder.items[0].total).toBe(10);
|
|
996
|
+
expect(resultedOrder.items[0].modifiers[0]._computed.amount).toBe(0);
|
|
997
|
+
|
|
998
|
+
const resultedOrderPrepay = pricingService.order.calculate(resultedOrder, {
|
|
999
|
+
isPrepay: true,
|
|
1000
|
+
});
|
|
1001
|
+
|
|
1002
|
+
expect(resultedOrderPrepay.total).toBe(9);
|
|
1003
|
+
expect(resultedOrderPrepay.items[0].total).toBe(9);
|
|
1004
|
+
expect(resultedOrderPrepay.items[0].modifiers[0]._computed.amount).toBe(0);
|
|
1005
|
+
expect(resultedOrderPrepay.items[0].modifiers[1]._computed.amount).toBe(-1);
|
|
1006
|
+
expect(resultedOrderPrepay.items[0].modifiers[1].properties).toMatchObject({
|
|
1007
|
+
isCalculatedPaymentModifier: true,
|
|
1008
|
+
paymentId: undefined,
|
|
1009
|
+
});
|
|
1010
|
+
});
|
|
887
1011
|
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const usePricing = require('../../index');
|
|
2
|
+
const mockStores = require('../mocks/stores');
|
|
3
|
+
|
|
4
|
+
const session = {
|
|
5
|
+
store: mockStores[0],
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const pricingService = usePricing(session);
|
|
9
|
+
describe('Get parent totals', () => {
|
|
10
|
+
it('should return zero totals if input is not an array', () => {
|
|
11
|
+
expect(pricingService.order.getTotals(null)).toEqual({
|
|
12
|
+
total: 0,
|
|
13
|
+
subTotal: 0,
|
|
14
|
+
subTotals: {},
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should calculate totals for simple sub-orders', () => {
|
|
19
|
+
const orders = [
|
|
20
|
+
{
|
|
21
|
+
parentId: '123',
|
|
22
|
+
total: 10,
|
|
23
|
+
subTotal: 8,
|
|
24
|
+
subTotals: {
|
|
25
|
+
base: 8,
|
|
26
|
+
tax: 2,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
parentId: '123',
|
|
31
|
+
total: 20,
|
|
32
|
+
subTotal: 18,
|
|
33
|
+
subTotals: {
|
|
34
|
+
base: 18,
|
|
35
|
+
tax: 2,
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
expect(pricingService.order.getTotals(orders)).toEqual({
|
|
41
|
+
total: 30,
|
|
42
|
+
subTotal: 26,
|
|
43
|
+
subTotals: {
|
|
44
|
+
base: 26,
|
|
45
|
+
tax: 4,
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should merge subtotals with missing keys', () => {
|
|
51
|
+
const orders = [
|
|
52
|
+
{
|
|
53
|
+
parentId: '1',
|
|
54
|
+
total: 5,
|
|
55
|
+
subTotal: 5,
|
|
56
|
+
subTotals: {
|
|
57
|
+
fee: 1,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
parentId: '1',
|
|
62
|
+
total: 10,
|
|
63
|
+
subTotal: 10,
|
|
64
|
+
subTotals: {
|
|
65
|
+
tax: 2,
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
expect(pricingService.order.getTotals(orders)).toEqual({
|
|
71
|
+
total: 15,
|
|
72
|
+
subTotal: 15,
|
|
73
|
+
subTotals: {
|
|
74
|
+
fee: 1,
|
|
75
|
+
tax: 2,
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -44,40 +44,28 @@ describe('Order actions', () => {
|
|
|
44
44
|
total: 10,
|
|
45
45
|
totalPaid: 0,
|
|
46
46
|
};
|
|
47
|
-
|
|
48
47
|
let cashPercentageDiscountOrder = pricingService.order.calculate(
|
|
49
48
|
{ items: [orderItem] },
|
|
50
49
|
{ paymentType: 'cash' }
|
|
51
50
|
);
|
|
52
|
-
|
|
53
51
|
expect(cashPercentageDiscountOrder).toHaveProperty('total', 9);
|
|
54
52
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotal', 10);
|
|
55
53
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotals', {
|
|
56
54
|
discount: -1,
|
|
57
55
|
});
|
|
58
56
|
expect(cashPercentageDiscountOrder.items[0].modifiers).toHaveLength(2);
|
|
59
|
-
|
|
60
57
|
expect(cashPercentageDiscountOrder.items[0].modifiers[0]._computed).toEqual(
|
|
61
58
|
expect.objectContaining({
|
|
62
59
|
amount: 0,
|
|
63
60
|
description: 'CASH 10% DISCOUNT',
|
|
64
61
|
})
|
|
65
62
|
);
|
|
66
|
-
|
|
67
63
|
expect(cashPercentageDiscountOrder.items[0].modifiers[1]._computed).toEqual(
|
|
68
64
|
expect.objectContaining({
|
|
69
65
|
amount: -1,
|
|
70
66
|
description: '$1.00 discount (-$1.00)',
|
|
71
67
|
})
|
|
72
68
|
);
|
|
73
|
-
|
|
74
|
-
expect(cashPercentageDiscountOrder.items[0].modifiers[0]._computed).toEqual(
|
|
75
|
-
expect.objectContaining({
|
|
76
|
-
amount: 0,
|
|
77
|
-
description: 'CASH 10% DISCOUNT',
|
|
78
|
-
})
|
|
79
|
-
);
|
|
80
|
-
|
|
81
69
|
cashPercentageDiscountOrder = pricingService.order.calculate(
|
|
82
70
|
{ items: [orderItem] },
|
|
83
71
|
{
|
|
@@ -88,14 +76,12 @@ describe('Order actions', () => {
|
|
|
88
76
|
}
|
|
89
77
|
);
|
|
90
78
|
cashPercentageDiscountOrder.items[0].totalPaid = 1;
|
|
91
|
-
|
|
92
79
|
expect(cashPercentageDiscountOrder).toHaveProperty('total', 9.89);
|
|
93
80
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotal', 10);
|
|
94
81
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotals', {
|
|
95
82
|
discount: -0.1111111111111112,
|
|
96
83
|
});
|
|
97
84
|
expect(cashPercentageDiscountOrder.items[0].modifiers).toHaveLength(2);
|
|
98
|
-
|
|
99
85
|
cashPercentageDiscountOrder = pricingService.order.calculate(
|
|
100
86
|
cashPercentageDiscountOrder,
|
|
101
87
|
{
|
|
@@ -106,7 +92,6 @@ describe('Order actions', () => {
|
|
|
106
92
|
}
|
|
107
93
|
);
|
|
108
94
|
cashPercentageDiscountOrder.items[0].totalPaid = 3;
|
|
109
|
-
|
|
110
95
|
expect(cashPercentageDiscountOrder).toHaveProperty('total', 9.67);
|
|
111
96
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotal', 10);
|
|
112
97
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotals', {
|
|
@@ -141,7 +126,6 @@ describe('Order actions', () => {
|
|
|
141
126
|
paymentId: '111',
|
|
142
127
|
})
|
|
143
128
|
);
|
|
144
|
-
|
|
145
129
|
cashPercentageDiscountOrder = pricingService.order.calculate(
|
|
146
130
|
cashPercentageDiscountOrder,
|
|
147
131
|
{
|
|
@@ -151,7 +135,6 @@ describe('Order actions', () => {
|
|
|
151
135
|
paymentId: '333',
|
|
152
136
|
}
|
|
153
137
|
);
|
|
154
|
-
|
|
155
138
|
expect(cashPercentageDiscountOrder).toHaveProperty('total', 9);
|
|
156
139
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotal', 10);
|
|
157
140
|
expect(cashPercentageDiscountOrder).toHaveProperty('subTotals', {
|
|
@@ -703,7 +686,7 @@ describe('Order actions', () => {
|
|
|
703
686
|
modifiers: [
|
|
704
687
|
{
|
|
705
688
|
_id: '671818d40e5dd150d9830904',
|
|
706
|
-
attributes: [
|
|
689
|
+
attributes: [],
|
|
707
690
|
modifierId: '62cdbfd01ee1b4001932818f',
|
|
708
691
|
_parentId: null,
|
|
709
692
|
locked: null,
|
|
@@ -722,15 +705,7 @@ describe('Order actions', () => {
|
|
|
722
705
|
recommended: false,
|
|
723
706
|
default: false,
|
|
724
707
|
code: '',
|
|
725
|
-
properties: {
|
|
726
|
-
override: {
|
|
727
|
-
field: 'price',
|
|
728
|
-
type: 'manual',
|
|
729
|
-
multiplier: true,
|
|
730
|
-
},
|
|
731
|
-
group: null,
|
|
732
|
-
sort: null,
|
|
733
|
-
},
|
|
708
|
+
properties: {},
|
|
734
709
|
_computed: {
|
|
735
710
|
amount: 0,
|
|
736
711
|
description: 'Alice Blue',
|
|
@@ -1110,7 +1085,7 @@ describe('Order actions', () => {
|
|
|
1110
1085
|
expect(resultedOrder.items[0].modifiers[0]._computed).toEqual(
|
|
1111
1086
|
expect.objectContaining({
|
|
1112
1087
|
amount: 0,
|
|
1113
|
-
description: 'Alice Blue
|
|
1088
|
+
description: 'Alice Blue',
|
|
1114
1089
|
})
|
|
1115
1090
|
);
|
|
1116
1091
|
|
|
@@ -1128,4 +1103,336 @@ describe('Order actions', () => {
|
|
|
1128
1103
|
})
|
|
1129
1104
|
);
|
|
1130
1105
|
});
|
|
1106
|
+
|
|
1107
|
+
test('Applies fixed and percentage discounts with fixed modifier added first (both payment modifiers)', () => {
|
|
1108
|
+
const prePayDiscount1Usd = {
|
|
1109
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1110
|
+
name: '1 usd pre pay disc',
|
|
1111
|
+
type: 'discount',
|
|
1112
|
+
tags: ['default'],
|
|
1113
|
+
direct: true,
|
|
1114
|
+
conditions: {
|
|
1115
|
+
valid: null,
|
|
1116
|
+
rules: [
|
|
1117
|
+
{
|
|
1118
|
+
key: 'paymentTypes',
|
|
1119
|
+
value: ['cash'],
|
|
1120
|
+
operand: '$in',
|
|
1121
|
+
},
|
|
1122
|
+
],
|
|
1123
|
+
},
|
|
1124
|
+
compute: {
|
|
1125
|
+
type: 'fixed',
|
|
1126
|
+
action: 'subtract',
|
|
1127
|
+
amount: 1,
|
|
1128
|
+
},
|
|
1129
|
+
};
|
|
1130
|
+
|
|
1131
|
+
const cashDiscount10Percent = {
|
|
1132
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1133
|
+
name: '10 % cash discount',
|
|
1134
|
+
type: 'discount',
|
|
1135
|
+
tags: ['default'],
|
|
1136
|
+
direct: true,
|
|
1137
|
+
conditions: {
|
|
1138
|
+
valid: null,
|
|
1139
|
+
rules: [
|
|
1140
|
+
{
|
|
1141
|
+
key: 'paymentTypes',
|
|
1142
|
+
value: ['cash'],
|
|
1143
|
+
operand: '$in',
|
|
1144
|
+
},
|
|
1145
|
+
],
|
|
1146
|
+
},
|
|
1147
|
+
compute: {
|
|
1148
|
+
type: 'percentage',
|
|
1149
|
+
action: 'subtract',
|
|
1150
|
+
amount: 10,
|
|
1151
|
+
},
|
|
1152
|
+
};
|
|
1153
|
+
|
|
1154
|
+
const item = {
|
|
1155
|
+
_id: 'abc',
|
|
1156
|
+
price: 10,
|
|
1157
|
+
quantity: 1,
|
|
1158
|
+
modifiers: [prePayDiscount1Usd, cashDiscount10Percent],
|
|
1159
|
+
};
|
|
1160
|
+
|
|
1161
|
+
const resultedOrderPrepay = pricingService.item.calculate(item, {
|
|
1162
|
+
paymentType: 'cash',
|
|
1163
|
+
paymentMethod: 'cash',
|
|
1164
|
+
isPrepay: true,
|
|
1165
|
+
lockPaymentModifiers: true,
|
|
1166
|
+
paymentId: 'abcd123',
|
|
1167
|
+
amountToPay: 8,
|
|
1168
|
+
});
|
|
1169
|
+
|
|
1170
|
+
expect(resultedOrderPrepay.total).toBe(8);
|
|
1171
|
+
|
|
1172
|
+
expect(resultedOrderPrepay.modifiers[0]._computed.amount).toBe(0);
|
|
1173
|
+
expect(resultedOrderPrepay.modifiers[1]._computed.amount).toBe(0);
|
|
1174
|
+
expect(resultedOrderPrepay.modifiers[2]._computed.amount).toBe(-1);
|
|
1175
|
+
expect(resultedOrderPrepay.modifiers[3]._computed.amount).toBe(-1);
|
|
1176
|
+
});
|
|
1177
|
+
|
|
1178
|
+
test('Applies percentage and fixed discounts with percentage modifier added first (both paymentModifiers)', () => {
|
|
1179
|
+
const prePayDiscount1Usd = {
|
|
1180
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1181
|
+
name: '1 usd pre pay disc',
|
|
1182
|
+
type: 'discount',
|
|
1183
|
+
tags: ['default'],
|
|
1184
|
+
direct: true,
|
|
1185
|
+
conditions: {
|
|
1186
|
+
valid: null,
|
|
1187
|
+
rules: [
|
|
1188
|
+
{
|
|
1189
|
+
key: 'paymentTypes',
|
|
1190
|
+
value: ['cash'],
|
|
1191
|
+
operand: '$in',
|
|
1192
|
+
},
|
|
1193
|
+
],
|
|
1194
|
+
},
|
|
1195
|
+
compute: {
|
|
1196
|
+
type: 'fixed',
|
|
1197
|
+
action: 'subtract',
|
|
1198
|
+
amount: 1,
|
|
1199
|
+
},
|
|
1200
|
+
};
|
|
1201
|
+
|
|
1202
|
+
const cashDiscount10Percent = {
|
|
1203
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1204
|
+
name: '10 % cash discount',
|
|
1205
|
+
type: 'discount',
|
|
1206
|
+
tags: ['default'],
|
|
1207
|
+
direct: true,
|
|
1208
|
+
conditions: {
|
|
1209
|
+
valid: null,
|
|
1210
|
+
rules: [
|
|
1211
|
+
{
|
|
1212
|
+
key: 'paymentTypes',
|
|
1213
|
+
value: ['cash'],
|
|
1214
|
+
operand: '$in',
|
|
1215
|
+
},
|
|
1216
|
+
],
|
|
1217
|
+
},
|
|
1218
|
+
compute: {
|
|
1219
|
+
type: 'percentage',
|
|
1220
|
+
action: 'subtract',
|
|
1221
|
+
amount: 10,
|
|
1222
|
+
},
|
|
1223
|
+
};
|
|
1224
|
+
|
|
1225
|
+
const item = {
|
|
1226
|
+
_id: 'abc',
|
|
1227
|
+
price: 10,
|
|
1228
|
+
quantity: 1,
|
|
1229
|
+
modifiers: [cashDiscount10Percent, prePayDiscount1Usd],
|
|
1230
|
+
};
|
|
1231
|
+
|
|
1232
|
+
const resultedOrderPrepay = pricingService.item.calculate(item, {
|
|
1233
|
+
paymentType: 'cash',
|
|
1234
|
+
paymentMethod: 'cash',
|
|
1235
|
+
isPrepay: true,
|
|
1236
|
+
lockPaymentModifiers: true,
|
|
1237
|
+
paymentId: 'abcd123',
|
|
1238
|
+
amountToPay: 8,
|
|
1239
|
+
});
|
|
1240
|
+
|
|
1241
|
+
expect(resultedOrderPrepay.total).toBe(8);
|
|
1242
|
+
|
|
1243
|
+
expect(resultedOrderPrepay.modifiers[0]._computed.amount).toBe(0);
|
|
1244
|
+
expect(resultedOrderPrepay.modifiers[1]._computed.amount).toBe(0);
|
|
1245
|
+
expect(resultedOrderPrepay.modifiers[2]._computed.amount).toBe(-1);
|
|
1246
|
+
expect(resultedOrderPrepay.modifiers[3]._computed.amount).toBe(-1);
|
|
1247
|
+
});
|
|
1248
|
+
|
|
1249
|
+
test('Applies one payment discount and one regular fixed discount (percentage first)', () => {
|
|
1250
|
+
const prePayDiscount1Usd = {
|
|
1251
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1252
|
+
name: '1 usd pre pay disc',
|
|
1253
|
+
type: 'discount',
|
|
1254
|
+
tags: ['default'],
|
|
1255
|
+
direct: true,
|
|
1256
|
+
conditions: {
|
|
1257
|
+
valid: null,
|
|
1258
|
+
rules: [],
|
|
1259
|
+
},
|
|
1260
|
+
compute: {
|
|
1261
|
+
type: 'fixed',
|
|
1262
|
+
action: 'subtract',
|
|
1263
|
+
amount: 1,
|
|
1264
|
+
},
|
|
1265
|
+
};
|
|
1266
|
+
|
|
1267
|
+
const cashDiscount10Percent = {
|
|
1268
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1269
|
+
name: '10 % cash discount',
|
|
1270
|
+
type: 'discount',
|
|
1271
|
+
tags: ['default'],
|
|
1272
|
+
direct: true,
|
|
1273
|
+
conditions: {
|
|
1274
|
+
valid: null,
|
|
1275
|
+
rules: [
|
|
1276
|
+
{
|
|
1277
|
+
key: 'paymentTypes',
|
|
1278
|
+
value: ['cash'],
|
|
1279
|
+
operand: '$in',
|
|
1280
|
+
},
|
|
1281
|
+
],
|
|
1282
|
+
},
|
|
1283
|
+
compute: {
|
|
1284
|
+
type: 'percentage',
|
|
1285
|
+
action: 'subtract',
|
|
1286
|
+
amount: 10,
|
|
1287
|
+
},
|
|
1288
|
+
};
|
|
1289
|
+
|
|
1290
|
+
const item = {
|
|
1291
|
+
_id: 'abc',
|
|
1292
|
+
price: 10,
|
|
1293
|
+
quantity: 1,
|
|
1294
|
+
modifiers: [cashDiscount10Percent, prePayDiscount1Usd],
|
|
1295
|
+
};
|
|
1296
|
+
|
|
1297
|
+
const resultedOrderPrepay = pricingService.item.calculate(item, {
|
|
1298
|
+
paymentType: 'cash',
|
|
1299
|
+
paymentMethod: 'cash',
|
|
1300
|
+
isPrepay: true,
|
|
1301
|
+
lockPaymentModifiers: true,
|
|
1302
|
+
paymentId: 'abcd123',
|
|
1303
|
+
amountToPay: 8,
|
|
1304
|
+
});
|
|
1305
|
+
|
|
1306
|
+
expect(resultedOrderPrepay.total).toBe(8);
|
|
1307
|
+
|
|
1308
|
+
expect(resultedOrderPrepay.modifiers[0]._computed.amount).toBe(0);
|
|
1309
|
+
expect(resultedOrderPrepay.modifiers[1]._computed.amount).toBe(-1);
|
|
1310
|
+
expect(resultedOrderPrepay.modifiers[2]._computed.amount).toBe(-1);
|
|
1311
|
+
});
|
|
1312
|
+
|
|
1313
|
+
test('Applies one payment discount and one regular percentage discount (fixed first)', () => {
|
|
1314
|
+
const prePayDiscount1Usd = {
|
|
1315
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1316
|
+
name: '1 usd pre pay disc',
|
|
1317
|
+
type: 'discount',
|
|
1318
|
+
tags: ['default'],
|
|
1319
|
+
direct: true,
|
|
1320
|
+
conditions: {
|
|
1321
|
+
valid: null,
|
|
1322
|
+
rules: [],
|
|
1323
|
+
},
|
|
1324
|
+
compute: {
|
|
1325
|
+
type: 'fixed',
|
|
1326
|
+
action: 'subtract',
|
|
1327
|
+
amount: 1,
|
|
1328
|
+
},
|
|
1329
|
+
};
|
|
1330
|
+
|
|
1331
|
+
const cashDiscount10Percent = {
|
|
1332
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1333
|
+
name: '10 % cash discount',
|
|
1334
|
+
type: 'discount',
|
|
1335
|
+
tags: ['default'],
|
|
1336
|
+
direct: true,
|
|
1337
|
+
conditions: {
|
|
1338
|
+
valid: null,
|
|
1339
|
+
rules: [
|
|
1340
|
+
{
|
|
1341
|
+
key: 'paymentTypes',
|
|
1342
|
+
value: ['cash'],
|
|
1343
|
+
operand: '$in',
|
|
1344
|
+
},
|
|
1345
|
+
],
|
|
1346
|
+
},
|
|
1347
|
+
compute: {
|
|
1348
|
+
type: 'percentage',
|
|
1349
|
+
action: 'subtract',
|
|
1350
|
+
amount: 10,
|
|
1351
|
+
},
|
|
1352
|
+
};
|
|
1353
|
+
|
|
1354
|
+
const item = {
|
|
1355
|
+
_id: 'abc',
|
|
1356
|
+
price: 10,
|
|
1357
|
+
quantity: 1,
|
|
1358
|
+
modifiers: [prePayDiscount1Usd, cashDiscount10Percent],
|
|
1359
|
+
};
|
|
1360
|
+
|
|
1361
|
+
const resultedOrderPrepay = pricingService.item.calculate(item, {
|
|
1362
|
+
paymentType: 'cash',
|
|
1363
|
+
paymentMethod: 'cash',
|
|
1364
|
+
isPrepay: true,
|
|
1365
|
+
lockPaymentModifiers: true,
|
|
1366
|
+
paymentId: 'abcd123',
|
|
1367
|
+
amountToPay: 8,
|
|
1368
|
+
});
|
|
1369
|
+
|
|
1370
|
+
expect(resultedOrderPrepay.total).toBe(8);
|
|
1371
|
+
|
|
1372
|
+
expect(resultedOrderPrepay.modifiers[0]._computed.amount).toBe(0);
|
|
1373
|
+
expect(resultedOrderPrepay.modifiers[1]._computed.amount).toBe(-1);
|
|
1374
|
+
expect(resultedOrderPrepay.modifiers[2]._computed.amount).toBe(-1);
|
|
1375
|
+
});
|
|
1376
|
+
|
|
1377
|
+
test('Applies one payment discount and one regular percentage discount (fixed first) withouth sending amountToPay', () => {
|
|
1378
|
+
const prePayDiscount1Usd = {
|
|
1379
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1380
|
+
name: '1 usd pre pay disc',
|
|
1381
|
+
type: 'discount',
|
|
1382
|
+
tags: ['default'],
|
|
1383
|
+
direct: true,
|
|
1384
|
+
conditions: {
|
|
1385
|
+
valid: null,
|
|
1386
|
+
rules: [],
|
|
1387
|
+
},
|
|
1388
|
+
compute: {
|
|
1389
|
+
type: 'fixed',
|
|
1390
|
+
action: 'subtract',
|
|
1391
|
+
amount: 1,
|
|
1392
|
+
},
|
|
1393
|
+
};
|
|
1394
|
+
|
|
1395
|
+
const cashDiscount10Percent = {
|
|
1396
|
+
_id: '6819114c06c23d37c1f19412',
|
|
1397
|
+
name: '10 % cash discount',
|
|
1398
|
+
type: 'discount',
|
|
1399
|
+
tags: ['default'],
|
|
1400
|
+
direct: true,
|
|
1401
|
+
conditions: {
|
|
1402
|
+
valid: null,
|
|
1403
|
+
rules: [
|
|
1404
|
+
{
|
|
1405
|
+
key: 'paymentTypes',
|
|
1406
|
+
value: ['cash'],
|
|
1407
|
+
operand: '$in',
|
|
1408
|
+
},
|
|
1409
|
+
],
|
|
1410
|
+
},
|
|
1411
|
+
compute: {
|
|
1412
|
+
type: 'percentage',
|
|
1413
|
+
action: 'subtract',
|
|
1414
|
+
amount: 10,
|
|
1415
|
+
},
|
|
1416
|
+
};
|
|
1417
|
+
|
|
1418
|
+
const item = {
|
|
1419
|
+
_id: 'abc',
|
|
1420
|
+
price: 10,
|
|
1421
|
+
quantity: 1,
|
|
1422
|
+
modifiers: [prePayDiscount1Usd, cashDiscount10Percent],
|
|
1423
|
+
};
|
|
1424
|
+
|
|
1425
|
+
const resultedOrderPrepay = pricingService.item.calculate(item, {
|
|
1426
|
+
paymentType: 'cash',
|
|
1427
|
+
paymentMethod: 'cash',
|
|
1428
|
+
isPrepay: true,
|
|
1429
|
+
paymentId: 'abcd123',
|
|
1430
|
+
});
|
|
1431
|
+
|
|
1432
|
+
expect(resultedOrderPrepay.total).toBe(8);
|
|
1433
|
+
|
|
1434
|
+
expect(resultedOrderPrepay.modifiers[0]._computed.amount).toBe(0);
|
|
1435
|
+
expect(resultedOrderPrepay.modifiers[1]._computed.amount).toBe(-1);
|
|
1436
|
+
expect(resultedOrderPrepay.modifiers[2]._computed.amount).toBe(-1);
|
|
1437
|
+
});
|
|
1131
1438
|
});
|