@darkpos/pricing 1.0.22 → 1.0.23

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.
@@ -94,7 +94,6 @@ describe('Order actions', () => {
94
94
  expect(newOrder).toHaveProperty('subTotals', {
95
95
  discount: -6,
96
96
  });
97
- // console.log(JSON.stringify(newOrder, 0, 2));
98
97
  });
99
98
  test('Get calculated Order, multiple items and indirect modifiers', () => {
100
99
  const item1 = {
@@ -125,23 +124,23 @@ describe('Order actions', () => {
125
124
  expect(newOrder).toHaveProperty('subTotals', {
126
125
  discount: -2,
127
126
  });
128
- expect(newOrder.items[0]).toHaveProperty('total', 58.28);
127
+ expect(newOrder.items[0]).toHaveProperty('total', 58.29);
129
128
  expect(newOrder.items[0]).toHaveProperty('subTotals', {
130
- discount: -1.72,
129
+ discount: -1.71,
131
130
  _included: 0,
132
- _xincluded: -1.72,
131
+ _xincluded: -1.71,
133
132
  _direct: 0,
134
- _xdirect: -1.72,
133
+ _xdirect: -1.71,
135
134
  _simple: 60,
136
135
  _actual: 60,
137
136
  });
138
- expect(newOrder.items[1]).toHaveProperty('total', 9.72);
137
+ expect(newOrder.items[1]).toHaveProperty('total', 9.71);
139
138
  expect(newOrder.items[1]).toHaveProperty('subTotals', {
140
- discount: -0.28,
139
+ discount: -0.29,
141
140
  _included: 0,
142
- _xincluded: -0.28,
141
+ _xincluded: -0.29,
143
142
  _direct: 0,
144
- _xdirect: -0.28,
143
+ _xdirect: -0.29,
145
144
  _simple: 10,
146
145
  _actual: 10,
147
146
  });
@@ -169,10 +168,10 @@ describe('Order actions', () => {
169
168
 
170
169
  const order = { items: [item1, item2], modifiers: [modifier1] };
171
170
  const newOrder = pricingService.order.calculate(order);
172
- expect(newOrder).toHaveProperty('total', 18.58);
171
+ expect(newOrder).toHaveProperty('total', 18.57);
173
172
  expect(newOrder).toHaveProperty('subTotal', 20.64);
174
173
  expect(newOrder).toHaveProperty('subTotals', {
175
- discount: -2.06,
174
+ discount: -2.07,
176
175
  });
177
176
  expect(newOrder.items[0]).toHaveProperty('total', 14.39);
178
177
  expect(newOrder.items[0]).toHaveProperty('subTotals', {
@@ -184,13 +183,13 @@ describe('Order actions', () => {
184
183
  _simple: 15.99,
185
184
  _actual: 15.99,
186
185
  });
187
- expect(newOrder.items[1]).toHaveProperty('total', 4.19);
186
+ expect(newOrder.items[1]).toHaveProperty('total', 4.18);
188
187
  expect(newOrder.items[1]).toHaveProperty('subTotals', {
189
- discount: -0.46,
188
+ discount: -0.47,
190
189
  _included: 0,
191
- _xincluded: -0.46,
190
+ _xincluded: -0.47,
192
191
  _direct: 0,
193
- _xdirect: -0.46,
192
+ _xdirect: -0.47,
194
193
  _simple: 4.65,
195
194
  _actual: 4.65,
196
195
  });
@@ -627,19 +626,649 @@ describe('Order actions', () => {
627
626
  });
628
627
  });
629
628
 
629
+ test('CU-861n7pum9 - Get calculated Order, multiple items, 2 modifiers: 1 order discount (sort 1), 1 order tax (sort 99)', () => {
630
+ const item1 = {
631
+ _id: 1,
632
+ price: 44,
633
+ quantity: 2,
634
+ };
635
+ const item2 = {
636
+ _id: 2,
637
+ price: 14.95,
638
+ quantity: 1,
639
+ };
640
+ const item3 = {
641
+ _id: 3,
642
+ price: 2.15,
643
+ quantity: 3,
644
+ };
645
+ const discountMod = {
646
+ _id: 1,
647
+ compute: {
648
+ amount: 15,
649
+ type: 'percentage',
650
+ action: 'subtract',
651
+ },
652
+ name: `15% Discount`,
653
+ type: 'discount',
654
+ properties: {
655
+ sort: 1,
656
+ },
657
+ };
658
+ const taxMod = {
659
+ _id: 2,
660
+ name: `7.75% tax`,
661
+ type: 'tax',
662
+ compute: {
663
+ amount: 7.75,
664
+ type: 'percentage',
665
+ action: 'add',
666
+ },
667
+ properties: {
668
+ sort: 99,
669
+ },
670
+ };
671
+
672
+ const order = {
673
+ items: [item1, item2, item3],
674
+ modifiers: [discountMod, taxMod],
675
+ };
676
+
677
+ const newOrder = pricingService.order.calculate(order);
678
+
679
+ expect(newOrder).toHaveProperty('total', 100.2);
680
+ expect(newOrder).toHaveProperty('subTotal', 109.4);
681
+ expect(newOrder).toHaveProperty('subTotals', {
682
+ discount: -16.41,
683
+ tax: 7.21,
684
+ });
685
+ expect(newOrder.items[0]).toHaveProperty('total', 80.6);
686
+ expect(newOrder.items[1]).toHaveProperty('total', 13.7);
687
+ expect(newOrder.items[2]).toHaveProperty('total', 5.9);
688
+ expect(newOrder.items[0].modifiers[0].compute).toHaveProperty(
689
+ 'amount',
690
+ 13.2
691
+ );
692
+ expect(newOrder.items[0].modifiers[1].compute).toHaveProperty(
693
+ 'amount',
694
+ 5.8
695
+ );
696
+
697
+ expect(newOrder.items[1].modifiers[0].compute).toHaveProperty(
698
+ 'amount',
699
+ 2.24
700
+ );
701
+ expect(newOrder.items[1].modifiers[1].compute).toHaveProperty(
702
+ 'amount',
703
+ 0.99
704
+ );
705
+
706
+ expect(newOrder.items[2].modifiers[0].compute).toHaveProperty(
707
+ 'amount',
708
+ 0.97
709
+ );
710
+ expect(newOrder.items[2].modifiers[1].compute).toHaveProperty(
711
+ 'amount',
712
+ 0.42
713
+ );
714
+ });
715
+
716
+ test(`CU-861n7pum9 - Get calculated Order, multiple items, 2 modifiers: 1 order discount (sort 1), 1 item tax (sort 99)`, () => {
717
+ const taxMod = {
718
+ _id: 2,
719
+ name: `7.75% tax`,
720
+ type: 'tax',
721
+ compute: {
722
+ amount: 7.75,
723
+ type: 'percentage',
724
+ action: 'add',
725
+ },
726
+ properties: {
727
+ sort: 99,
728
+ },
729
+ };
730
+
731
+ const discountMod = {
732
+ _id: 1,
733
+ compute: {
734
+ amount: 15,
735
+ type: 'percentage',
736
+ action: 'subtract',
737
+ },
738
+ name: `15% Discount`,
739
+ type: 'discount',
740
+ properties: {
741
+ sort: 1,
742
+ },
743
+ };
744
+
745
+ const item1 = {
746
+ _id: 1,
747
+ price: 44,
748
+ quantity: 2,
749
+ modifiers: [taxMod],
750
+ };
751
+ const item2 = {
752
+ _id: 2,
753
+ price: 14.95,
754
+ quantity: 1,
755
+ modifiers: [taxMod],
756
+ };
757
+ const item3 = {
758
+ _id: 3,
759
+ price: 2.15,
760
+ quantity: 3,
761
+ modifiers: [taxMod],
762
+ };
763
+
764
+ const order = {
765
+ items: [item1, item2, item3],
766
+ modifiers: [discountMod],
767
+ };
768
+
769
+ const newOrder = pricingService.order.calculate(order);
770
+
771
+ expect(newOrder).toHaveProperty('total', 100.2);
772
+ expect(newOrder).toHaveProperty('subTotal', 109.4);
773
+ expect(newOrder).toHaveProperty('subTotals', {
774
+ discount: -16.41,
775
+ tax: 7.21,
776
+ });
777
+ expect(newOrder.items[0]).toHaveProperty('total', 80.6);
778
+ expect(newOrder.items[1]).toHaveProperty('total', 13.7);
779
+ expect(newOrder.items[2]).toHaveProperty('total', 5.9);
780
+ expect(newOrder.items[0].modifiers[0].compute).toHaveProperty(
781
+ 'amount',
782
+ 13.2
783
+ );
784
+ expect(newOrder.items[0].modifiers[1]._computed).toHaveProperty(
785
+ 'amount',
786
+ 5.797
787
+ );
788
+
789
+ expect(newOrder.items[1].modifiers[0].compute).toHaveProperty(
790
+ 'amount',
791
+ 2.24
792
+ );
793
+ expect(newOrder.items[1].modifiers[1]._computed).toHaveProperty(
794
+ 'amount',
795
+ 0.985025
796
+ );
797
+
798
+ expect(newOrder.items[2].modifiers[0].compute).toHaveProperty(
799
+ 'amount',
800
+ 0.97
801
+ );
802
+ expect(newOrder.items[2].modifiers[1]._computed).toHaveProperty(
803
+ 'amount',
804
+ 0.4247
805
+ );
806
+ });
807
+
808
+ test('CU-861n7pum9 - Get calculated Order, multiple items, 2 modifiers: 1 item discount (sort 1), 1 item tax (sort 99)', () => {
809
+ const taxMod = {
810
+ _id: 2,
811
+ name: `7.75% tax`,
812
+ type: 'tax',
813
+ compute: {
814
+ amount: 7.75,
815
+ type: 'percentage',
816
+ action: 'add',
817
+ },
818
+ properties: {
819
+ sort: 99,
820
+ },
821
+ };
822
+
823
+ const discountMod = {
824
+ _id: 1,
825
+ compute: {
826
+ amount: 15,
827
+ type: 'percentage',
828
+ action: 'subtract',
829
+ },
830
+ name: `15% Discount`,
831
+ type: 'discount',
832
+ properties: {
833
+ sort: 1,
834
+ },
835
+ };
836
+
837
+ const item1 = {
838
+ _id: 1,
839
+ price: 44,
840
+ quantity: 2,
841
+ modifiers: [taxMod, discountMod],
842
+ };
843
+ const item2 = {
844
+ _id: 2,
845
+ price: 14.95,
846
+ quantity: 1,
847
+ modifiers: [taxMod, discountMod],
848
+ };
849
+ const item3 = {
850
+ _id: 3,
851
+ price: 2.15,
852
+ quantity: 3,
853
+ modifiers: [taxMod, discountMod],
854
+ };
855
+
856
+ const order = {
857
+ items: [item1, item2, item3],
858
+ modifiers: [],
859
+ };
860
+
861
+ const newOrder = pricingService.order.calculate(order);
862
+
863
+ expect(newOrder).toHaveProperty('total', 100.2);
864
+ expect(newOrder).toHaveProperty('subTotal', 109.4);
865
+ expect(newOrder).toHaveProperty('subTotals', {
866
+ discount: -16.41,
867
+ tax: 7.21,
868
+ });
869
+ expect(newOrder.items[0]).toHaveProperty('total', 80.6);
870
+ expect(newOrder.items[1]).toHaveProperty('total', 13.69);
871
+ expect(newOrder.items[2]).toHaveProperty('total', 5.91);
872
+ expect(newOrder.items[0].modifiers[0]._computed).toHaveProperty(
873
+ 'amount',
874
+ -13.2
875
+ );
876
+ expect(newOrder.items[0].modifiers[1]._computed).toHaveProperty(
877
+ 'amount',
878
+ 5.797
879
+ );
880
+
881
+ expect(newOrder.items[1].modifiers[0]._computed).toHaveProperty(
882
+ 'amount',
883
+ -2.2425
884
+ );
885
+ expect(newOrder.items[1].modifiers[1]._computed).toHaveProperty(
886
+ 'amount',
887
+ 0.98483125
888
+ );
889
+
890
+ expect(newOrder.items[2].modifiers[0]._computed).toHaveProperty(
891
+ 'amount',
892
+ -0.9675
893
+ );
894
+ expect(newOrder.items[2].modifiers[1]._computed).toHaveProperty(
895
+ 'amount',
896
+ 0.42489375
897
+ );
898
+ });
899
+
900
+ test('CU-861n7pum9 - Get calculated Order, multiple items, 2 modifiers: 1 item discount (sort 1), 1 order tax (sort 99)', () => {
901
+ const taxMod = {
902
+ _id: 2,
903
+ name: `7.75% tax`,
904
+ type: 'tax',
905
+ compute: {
906
+ amount: 7.75,
907
+ type: 'percentage',
908
+ action: 'add',
909
+ },
910
+ properties: {
911
+ sort: 99,
912
+ },
913
+ };
914
+
915
+ const discountMod = {
916
+ _id: 1,
917
+ compute: {
918
+ amount: 15,
919
+ type: 'percentage',
920
+ action: 'subtract',
921
+ },
922
+ name: `15% Discount`,
923
+ type: 'discount',
924
+ properties: {
925
+ sort: 1,
926
+ },
927
+ };
928
+
929
+ const item1 = {
930
+ _id: 1,
931
+ price: 44,
932
+ quantity: 2,
933
+ modifiers: [discountMod],
934
+ };
935
+ const item2 = {
936
+ _id: 2,
937
+ price: 14.95,
938
+ quantity: 1,
939
+ modifiers: [discountMod],
940
+ };
941
+ const item3 = {
942
+ _id: 3,
943
+ price: 2.15,
944
+ quantity: 3,
945
+ modifiers: [discountMod],
946
+ };
947
+
948
+ const order = {
949
+ items: [item1, item2, item3],
950
+ modifiers: [taxMod],
951
+ };
952
+
953
+ const newOrder = pricingService.order.calculate(order);
954
+
955
+ expect(newOrder).toHaveProperty('total', 100.2);
956
+ expect(newOrder).toHaveProperty('subTotal', 109.4);
957
+ expect(newOrder).toHaveProperty('subTotals', {
958
+ discount: -16.41,
959
+ tax: 7.21,
960
+ });
961
+ expect(newOrder.items[0]).toHaveProperty('total', 80.6);
962
+ expect(newOrder.items[1]).toHaveProperty('total', 13.7);
963
+ expect(newOrder.items[2]).toHaveProperty('total', 5.9);
964
+ expect(newOrder.items[0].modifiers[0]._computed).toHaveProperty(
965
+ 'amount',
966
+ -13.2
967
+ );
968
+ expect(newOrder.items[0].modifiers[1]._computed).toHaveProperty(
969
+ 'amount',
970
+ 5.8
971
+ );
972
+
973
+ expect(newOrder.items[1].modifiers[0]._computed).toHaveProperty(
974
+ 'amount',
975
+ -2.2425
976
+ );
977
+ expect(newOrder.items[1].modifiers[1]._computed).toHaveProperty(
978
+ 'amount',
979
+ 0.99
980
+ );
981
+
982
+ expect(newOrder.items[2].modifiers[0]._computed).toHaveProperty(
983
+ 'amount',
984
+ -0.9675
985
+ );
986
+ expect(newOrder.items[2].modifiers[1]._computed).toHaveProperty(
987
+ 'amount',
988
+ 0.42
989
+ );
990
+ });
991
+
992
+ test('CU-861n7pum9 - Get calculated Order, multiple items, 2 modifiers: 1 item discount (ignoreQty), 1 order tax', () => {
993
+ const taxMod = {
994
+ _id: 2,
995
+ name: `7.75% tax`,
996
+ type: 'tax',
997
+ compute: {
998
+ amount: 7.75,
999
+ type: 'percentage',
1000
+ action: 'add',
1001
+ },
1002
+ };
1003
+
1004
+ const discountMod = {
1005
+ _id: 1,
1006
+ compute: {
1007
+ amount: 1,
1008
+ type: 'fixed',
1009
+ action: 'subtract',
1010
+ },
1011
+ name: `1 USD`,
1012
+ type: 'fixed',
1013
+ properties: {
1014
+ ignoreQuantity: true,
1015
+ },
1016
+ };
1017
+
1018
+ const item1 = {
1019
+ _id: 1,
1020
+ price: 1,
1021
+ quantity: 1,
1022
+ modifiers: [discountMod],
1023
+ };
1024
+ const item2 = {
1025
+ _id: 2,
1026
+ price: 1,
1027
+ quantity: 1,
1028
+ modifiers: [discountMod],
1029
+ };
1030
+ const item3 = {
1031
+ _id: 3,
1032
+ price: 0.5,
1033
+ quantity: 2,
1034
+ modifiers: [discountMod],
1035
+ };
1036
+
1037
+ const order = {
1038
+ items: [item1, item2, item3],
1039
+ modifiers: [taxMod],
1040
+ };
1041
+
1042
+ const newOrder = pricingService.order.calculate(order);
1043
+
1044
+ expect(newOrder).toHaveProperty('total', 0);
1045
+ expect(newOrder).toHaveProperty('subTotal', 3);
1046
+ expect(newOrder).toHaveProperty('subTotals', {
1047
+ fixed: -3,
1048
+ tax: 0,
1049
+ });
1050
+ expect(newOrder.items[0]).toHaveProperty('total', 0);
1051
+ expect(newOrder.items[1]).toHaveProperty('total', 0);
1052
+ expect(newOrder.items[2]).toHaveProperty('total', 0);
1053
+ expect(newOrder.items[0].modifiers[0]._computed).toHaveProperty(
1054
+ 'amount',
1055
+ -1
1056
+ );
1057
+ expect(newOrder.items[0].modifiers[1]._computed).toHaveProperty(
1058
+ 'amount',
1059
+ 0
1060
+ );
1061
+
1062
+ expect(newOrder.items[1].modifiers[0]._computed).toHaveProperty(
1063
+ 'amount',
1064
+ -1
1065
+ );
1066
+ expect(newOrder.items[1].modifiers[1]._computed).toHaveProperty(
1067
+ 'amount',
1068
+ 0
1069
+ );
1070
+
1071
+ expect(newOrder.items[2].modifiers[0]._computed).toHaveProperty(
1072
+ 'amount',
1073
+ -1
1074
+ );
1075
+ expect(newOrder.items[2].modifiers[1]._computed).toHaveProperty(
1076
+ 'amount',
1077
+ 0
1078
+ );
1079
+ });
1080
+
1081
+ test('CU-861n7pum9 - Get calculated Order, multiple items, 2 modifiers: 1 order discount (sort 1), 1 order tax (sort 2)', () => {
1082
+ const taxMod = {
1083
+ _id: 2,
1084
+ name: `7.75% tax`,
1085
+ type: 'tax',
1086
+ compute: {
1087
+ amount: 7.75,
1088
+ type: 'percentage',
1089
+ action: 'add',
1090
+ },
1091
+ properties: {
1092
+ sort: 2,
1093
+ },
1094
+ };
1095
+
1096
+ const discountMod = {
1097
+ _id: 1,
1098
+ compute: {
1099
+ amount: 1,
1100
+ type: 'fixed',
1101
+ action: 'subtract',
1102
+ },
1103
+ name: `1 USD`,
1104
+ type: 'fixed',
1105
+ properties: {
1106
+ sort: 1,
1107
+ },
1108
+ };
1109
+
1110
+ const item1 = {
1111
+ _id: 1,
1112
+ price: 1,
1113
+ quantity: 1,
1114
+ modifiers: [],
1115
+ };
1116
+ const item2 = {
1117
+ _id: 2,
1118
+ price: 1,
1119
+ quantity: 1,
1120
+ modifiers: [],
1121
+ };
1122
+ const item3 = {
1123
+ _id: 3,
1124
+ price: 0.5,
1125
+ quantity: 2,
1126
+ modifiers: [],
1127
+ };
1128
+
1129
+ const order = {
1130
+ items: [item1, item2, item3],
1131
+ modifiers: [taxMod, discountMod],
1132
+ };
1133
+
1134
+ const newOrder = pricingService.order.calculate(order);
1135
+
1136
+ expect(newOrder).toHaveProperty('total', 2.15);
1137
+ expect(newOrder).toHaveProperty('subTotal', 3);
1138
+ expect(newOrder).toHaveProperty('subTotals', {
1139
+ fixed: -1,
1140
+ tax: 0.15,
1141
+ });
1142
+ expect(newOrder.items[0]).toHaveProperty('total', 0.72);
1143
+ expect(newOrder.items[1]).toHaveProperty('total', 0.72);
1144
+ expect(newOrder.items[2]).toHaveProperty('total', 0.71);
1145
+ expect(newOrder.items[0].modifiers[0]._computed).toHaveProperty(
1146
+ 'amount',
1147
+ -0.33
1148
+ );
1149
+ expect(newOrder.items[0].modifiers[1]._computed).toHaveProperty(
1150
+ 'amount',
1151
+ 0.05
1152
+ );
1153
+
1154
+ expect(newOrder.items[1].modifiers[0]._computed).toHaveProperty(
1155
+ 'amount',
1156
+ -0.33
1157
+ );
1158
+ expect(newOrder.items[1].modifiers[1]._computed).toHaveProperty(
1159
+ 'amount',
1160
+ 0.05
1161
+ );
1162
+
1163
+ expect(newOrder.items[2].modifiers[0]._computed).toHaveProperty(
1164
+ 'amount',
1165
+ -0.17
1166
+ );
1167
+ expect(newOrder.items[2].modifiers[1]._computed).toHaveProperty(
1168
+ 'amount',
1169
+ 0.05
1170
+ );
1171
+ });
1172
+
1173
+ test('CU-861n7pum9 - Get calculated Order, multiple items, 2 modifiers: 1 item discount, 1 order tax', () => {
1174
+ const taxMod = {
1175
+ _id: 2,
1176
+ name: `7.75% tax`,
1177
+ type: 'tax',
1178
+ compute: {
1179
+ amount: 7.75,
1180
+ type: 'percentage',
1181
+ action: 'add',
1182
+ },
1183
+ };
1184
+
1185
+ const discountMod = {
1186
+ _id: 1,
1187
+ compute: {
1188
+ amount: 1,
1189
+ type: 'fixed',
1190
+ action: 'subtract',
1191
+ },
1192
+ name: `1 USD`,
1193
+ type: 'fixed',
1194
+ };
1195
+
1196
+ const item1 = {
1197
+ _id: 1,
1198
+ price: 1,
1199
+ quantity: 1,
1200
+ modifiers: [discountMod],
1201
+ };
1202
+ const item2 = {
1203
+ _id: 2,
1204
+ price: 1,
1205
+ quantity: 1,
1206
+ modifiers: [discountMod],
1207
+ };
1208
+ const item3 = {
1209
+ _id: 3,
1210
+ price: 0.5,
1211
+ quantity: 2,
1212
+ modifiers: [discountMod],
1213
+ };
1214
+
1215
+ const order = {
1216
+ items: [item1, item2, item3],
1217
+ modifiers: [taxMod],
1218
+ };
1219
+
1220
+ const newOrder = pricingService.order.calculate(order);
1221
+
1222
+ expect(newOrder).toHaveProperty('total', 0);
1223
+ expect(newOrder).toHaveProperty('subTotal', 3);
1224
+ expect(newOrder).toHaveProperty('subTotals', {
1225
+ fixed: -3,
1226
+ tax: 0,
1227
+ });
1228
+ expect(newOrder.items[0]).toHaveProperty('total', 0);
1229
+ expect(newOrder.items[1]).toHaveProperty('total', 0);
1230
+ expect(newOrder.items[2]).toHaveProperty('total', 0);
1231
+ expect(newOrder.items[0].modifiers[0]._computed).toHaveProperty(
1232
+ 'amount',
1233
+ -1
1234
+ );
1235
+ expect(newOrder.items[0].modifiers[1]._computed).toHaveProperty(
1236
+ 'amount',
1237
+ 0
1238
+ );
1239
+
1240
+ expect(newOrder.items[1].modifiers[0]._computed).toHaveProperty(
1241
+ 'amount',
1242
+ -1
1243
+ );
1244
+ expect(newOrder.items[1].modifiers[1]._computed).toHaveProperty(
1245
+ 'amount',
1246
+ 0
1247
+ );
1248
+
1249
+ expect(newOrder.items[2].modifiers[0]._computed).toHaveProperty(
1250
+ 'amount',
1251
+ -1
1252
+ );
1253
+ expect(newOrder.items[2].modifiers[1]._computed).toHaveProperty(
1254
+ 'amount',
1255
+ 0
1256
+ );
1257
+ });
1258
+
630
1259
  test('CU-3und87h : Calculate order with 20 USD credit', () => {
631
1260
  const newOrder = pricingService.order.calculate(orderCredit);
632
1261
 
633
1262
  const [item1, item2, item3] = newOrder.items;
634
1263
  expect(newOrder).toHaveProperty('total', 12.5);
635
- expect(item1).toHaveProperty('total', 3.64);
636
- expect(item1.modifiers[1].compute).toHaveProperty('amount', 5.86);
1264
+ expect(item1).toHaveProperty('total', 3.66);
1265
+ expect(item1.modifiers[1].compute).toHaveProperty('amount', 5.84);
637
1266
 
638
- expect(item2).toHaveProperty('total', 5.39);
639
- expect(item2.modifiers[1].compute).toHaveProperty('amount', 8.61);
1267
+ expect(item2).toHaveProperty('total', 5.38);
1268
+ expect(item2.modifiers[1].compute).toHaveProperty('amount', 8.62);
640
1269
 
641
- expect(item3).toHaveProperty('total', 3.47);
642
- expect(item3.modifiers[1].compute).toHaveProperty('amount', 5.53);
1270
+ expect(item3).toHaveProperty('total', 3.46);
1271
+ expect(item3.modifiers[1].compute).toHaveProperty('amount', 5.54);
643
1272
  });
644
1273
 
645
1274
  test('Auto split a Parent Order', () => {
@@ -992,6 +1621,7 @@ describe('Order actions', () => {
992
1621
  modifiers: paymentModifiers,
993
1622
  order,
994
1623
  });
1624
+
995
1625
  const newOrder = pricingService.order.calculate(order);
996
1626
 
997
1627
  expect(newOrder).toHaveProperty('total', 36.45);
@@ -1000,9 +1630,9 @@ describe('Order actions', () => {
1000
1630
  discount: -10,
1001
1631
  fee: 4,
1002
1632
  });
1003
- expect(newOrder.items[0]).toHaveProperty('total', 8.06);
1004
- expect(newOrder.items[1]).toHaveProperty('total', 7.71);
1005
- expect(newOrder.items[2]).toHaveProperty('total', 11.37);
1006
- expect(newOrder.items[3]).toHaveProperty('total', 9.31);
1633
+ expect(newOrder.items[0]).toHaveProperty('total', 8.09);
1634
+ expect(newOrder.items[1]).toHaveProperty('total', 7.7);
1635
+ expect(newOrder.items[2]).toHaveProperty('total', 11.36);
1636
+ expect(newOrder.items[3]).toHaveProperty('total', 9.3);
1007
1637
  });
1008
1638
  });
@@ -25,7 +25,9 @@ module.exports = ({ utils, modifierActions }) => {
25
25
  );
26
26
  }, 0);
27
27
 
28
- // check validation
28
+ if (modifier.compute.type === 'percentage' && modifier.type !== 'credit')
29
+ return itemsModifiers;
30
+
29
31
  if (math.abs(totalDistributed) !== math.abs(orderModifierTotal)) {
30
32
  const difference = math.sub(orderModifierTotal, totalDistributed);
31
33
  const selectedItem = items.find(
@@ -66,14 +66,22 @@ module.exports = ({ _, utils, modifierActions }) => {
66
66
 
67
67
  modifiers.push(_modifier);
68
68
  const { type, _computed } = _modifier;
69
- let computedAmount = modifierActions.isIgnoreQuantity(_modifier)
70
- ? _computed.amount
71
- : math.mul(_computed.amount, quantity);
69
+
70
+ const computedAmountCalc = math.mul(_computed.amount, quantity);
71
+ const computedPriceCalc = math.mul(computedPrice * quantity);
72
+
73
+ let computedAmount =
74
+ _modifier.compute.type === 'percentage' ||
75
+ modifierActions.isIgnoreQuantity(_modifier) ||
76
+ math.gt(math.abs(computedAmountCalc), computedPriceCalc)
77
+ ? _computed.amount
78
+ : computedAmountCalc;
79
+
72
80
  // subscription modifiers have fixed amount and can't be multiplied by quantity
73
81
  if (modifierActions.isSubscription(_modifier))
74
82
  computedAmount = _computed.amount;
75
83
 
76
- prvPrice = math.add(prvPrice, computedAmount);
84
+ prvPrice = math.add(prvPrice, math.div(_computed.amount, quantity));
77
85
  prvSort = sort;
78
86
 
79
87
  if (included) {
@@ -90,7 +98,9 @@ module.exports = ({ _, utils, modifierActions }) => {
90
98
 
91
99
  subTotals._actual = math.add(subTotals._simple, subTotals._included);
92
100
 
93
- const total = math.add(subTotals._actual, subTotals._xincluded);
101
+ const total = math.toDecimalPlaces(
102
+ math.add(subTotals._actual, subTotals._xincluded)
103
+ );
94
104
 
95
105
  return {
96
106
  ...item,
@@ -16,15 +16,17 @@ module.exports = ({ utils }) => {
16
16
  }
17
17
  }
18
18
 
19
- const total = Object.keys(subTotals).reduce(
20
- (acc, key) => math.add(acc, subTotals[key]),
21
- subTotal
19
+ const total = math.toDecimalPlaces(
20
+ Object.keys(subTotals).reduce(
21
+ (acc, key) => math.add(acc, subTotals[key]),
22
+ subTotal
23
+ )
22
24
  );
23
25
 
24
26
  subTotals = Object.keys(subTotals).reduce(
25
27
  (acc, key) => ({
26
28
  ...acc,
27
- [key]: subTotals[key],
29
+ [key]: math.toDecimalPlaces(subTotals[key]),
28
30
  }),
29
31
  {}
30
32
  );
@@ -1,13 +1,13 @@
1
1
  /**
2
2
  * Get calculated modifier
3
3
  */
4
- module.exports = ({ _, constants, utils, localization, actions }) => {
4
+ module.exports = ({ _, constants, utils, localization, actions }) => {
5
5
  const { math } = utils;
6
6
  const { Modifier } = constants;
7
7
 
8
8
  return function calculate(
9
9
  modifier = {},
10
- options = { price: 0, skip: false }
10
+ options = { price: 0, quantity: 0, skip: false }
11
11
  ) {
12
12
  const { name, compute } = modifier;
13
13
  const { type, action, amount = 0 } = compute || {};
@@ -15,8 +15,16 @@
15
15
  amount: 0,
16
16
  description: '',
17
17
  };
18
- const maxAmount = actions.getProperty(modifier, 'maxAmount');
18
+
19
+ const maxAmountProp = actions.getProperty(modifier, 'maxAmount');
20
+
19
21
  if (!options.skip) {
22
+ const maxAmountCalc = math.mul(options.price, options.quantity);
23
+
24
+ const maxAmount = maxAmountProp
25
+ ? math.min(maxAmountProp, maxAmountCalc)
26
+ : maxAmountCalc;
27
+
20
28
  const multiplier = action === Modifier.Compute.Actions.SUBTRACT ? -1 : 1;
21
29
 
22
30
  if (type === Modifier.Compute.Types.FIXED)
@@ -24,15 +32,16 @@
24
32
 
25
33
  if (type === Modifier.Compute.Types.PERCENTAGE)
26
34
  _computed.amount = math.div(
27
- math.mul(multiplier, options.price, amount),
35
+ math.mul(multiplier, options.price, options.quantity, amount),
28
36
  100
29
37
  );
30
38
 
31
39
  if (!type || action === Modifier.Compute.Actions.OVERRIDE)
32
40
  _computed.amount = 0;
33
41
 
34
- if (maxAmount && math.gt(math.abs(_computed.amount), maxAmount))
35
- _computed.amount = maxAmount;
42
+ if (math.gt(math.abs(_computed.amount), maxAmount)) {
43
+ _computed.amount = maxAmount * multiplier;
44
+ }
36
45
  }
37
46
 
38
47
  const localAmount = localization.formatAmount(_computed.amount);
@@ -16,9 +16,14 @@ module.exports = ({ _, utils, constants, actions }) => {
16
16
  let modifierAmount = amount;
17
17
 
18
18
  if (type === Modifier.Compute.Types.PERCENTAGE) {
19
- const modifierAmountByPercentage = math.div(
20
- math.mul(options.orderTotal, amount),
21
- 100
19
+ const modifierAmountByPercentage = math.toDecimalPlaces(
20
+ math.div(
21
+ math.mul(
22
+ modifier.type === 'credit' ? options.orderTotal : options.itemTotal,
23
+ amount
24
+ ),
25
+ 100
26
+ )
22
27
  );
23
28
  modifierAmount =
24
29
  maxAmount !== null && modifierAmountByPercentage > maxAmount
@@ -26,14 +31,20 @@ module.exports = ({ _, utils, constants, actions }) => {
26
31
  : modifierAmountByPercentage;
27
32
  }
28
33
 
29
- let amountToApply = 0;
34
+ let amountToApply = modifierAmount;
30
35
 
31
- if (!math.isZero(options.orderTotal))
32
- amountToApply = math.div(
33
- math.mul(modifierAmount, options.itemTotal),
34
- options.orderTotal,
35
- options.itemQuantity
36
+ if (
37
+ !math.isZero(options.orderTotal) &&
38
+ (type !== Modifier.Compute.Types.PERCENTAGE || modifier.type === 'credit')
39
+ ) {
40
+ amountToApply = math.toDecimalPlaces(
41
+ math.div(
42
+ math.mul(modifierAmount, options.itemTotal),
43
+ options.orderTotal,
44
+ options.itemQuantity
45
+ )
36
46
  );
47
+ }
37
48
 
38
49
  const modifierToAdd = actions.create({
39
50
  ..._.cloneDeep(modifier),
@@ -43,6 +54,12 @@ module.exports = ({ _, utils, constants, actions }) => {
43
54
  amount: amountToApply,
44
55
  action: compute.action || Modifier.Compute.Actions.SUBTRACT,
45
56
  },
57
+ properties: {
58
+ ignoreQuantity:
59
+ (modifier.properties && modifier.properties.ignoreQuantity) ||
60
+ type === Modifier.Compute.Types.PERCENTAGE,
61
+ ...(modifier.properties || {}),
62
+ },
46
63
  });
47
64
  if (maxAmount !== null) delete modifierToAdd.properties.maxAmount;
48
65
 
@@ -7,9 +7,11 @@ module.exports = ({ utils }) => {
7
7
  totalSplitedOrder,
8
8
  }) =>
9
9
  totalOriginOrder
10
- ? math.div(
11
- math.mul(modifierAmount, totalSplitedOrder || 0),
12
- totalOriginOrder
10
+ ? math.toDecimalPlaces(
11
+ math.div(
12
+ math.mul(modifierAmount, totalSplitedOrder || 0),
13
+ totalOriginOrder
14
+ )
13
15
  )
14
16
  : 0;
15
17
 
@@ -17,6 +17,7 @@ module.exports = ({ utils }) => {
17
17
  orderSubTotals[key]
18
18
  );
19
19
  }
20
+
20
21
  return {
21
22
  total: math.add(acc.total, order.total),
22
23
  subTotals,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.22",
3
+ "version": "1.0.23",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -19,7 +19,7 @@
19
19
  "access": "public"
20
20
  },
21
21
  "dependencies": {
22
- "@darkpos/utils": "1.0.8",
22
+ "@darkpos/utils": "1.0.9",
23
23
  "lodash": "^4.17.21",
24
24
  "moment-timezone": "^0.5.34"
25
25
  },
@@ -36,5 +36,5 @@
36
36
  "supertest": "^6.2.3",
37
37
  "supervisor": "^0.12.0"
38
38
  },
39
- "gitHead": "82cfaf282b53da5c4343a36daad7e7d16fe4aa2c"
39
+ "gitHead": "6648705882ec8351463ba823c213cf7c198129ce"
40
40
  }