b2m-utils 0.0.228 → 0.0.230

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.
@@ -215,7 +215,7 @@ var TrackProcessProviderTypeEnum;
215
215
  })(TrackProcessProviderTypeEnum || (TrackProcessProviderTypeEnum = {}));
216
216
 
217
217
  var getRatecardFromCte = function (cte) {
218
- var _a, _b, _c;
218
+ var _a, _b, _c, _d, _e, _f, _g;
219
219
  var ratecard = null;
220
220
  if (((_a = cte.carrier) === null || _a === void 0 ? void 0 : _a.Ratecard) && ((_c = (_b = cte.carrier) === null || _b === void 0 ? void 0 : _b.Ratecard) === null || _c === void 0 ? void 0 : _c.length) > 0) {
221
221
  var today_1 = new Date();
@@ -246,12 +246,38 @@ var getRatecardFromCte = function (cte) {
246
246
  }
247
247
  break;
248
248
  default:
249
+ // LTL: Busca principal validando rota através de RatecardLane
250
+ var destinationUf_1 = (_e = (_d = cte.cityDestination) === null || _d === void 0 ? void 0 : _d.state) === null || _e === void 0 ? void 0 : _e.uf;
251
+ var originUf_1 = (_g = (_f = cte.cityOrigin) === null || _f === void 0 ? void 0 : _f.state) === null || _g === void 0 ? void 0 : _g.uf;
249
252
  var ratecardFromThisYear = cte.carrier.Ratecard.find(function (ratecard) {
250
- return (ratecard === null || ratecard === void 0 ? void 0 : ratecard.startsAt) && (ratecard === null || ratecard === void 0 ? void 0 : ratecard.endsAt) && today_1 >= new Date(ratecard.startsAt) && today_1 <= new Date(ratecard.endsAt) && (ratecard === null || ratecard === void 0 ? void 0 : ratecard.modalId) === cte.ratecardModalId;
253
+ var _a;
254
+ var isValidDate = (ratecard === null || ratecard === void 0 ? void 0 : ratecard.startsAt) && (ratecard === null || ratecard === void 0 ? void 0 : ratecard.endsAt) && today_1 >= new Date(ratecard.startsAt) && today_1 <= new Date(ratecard.endsAt);
255
+ var isValidModal = (ratecard === null || ratecard === void 0 ? void 0 : ratecard.modalId) === cte.ratecardModalId;
256
+ // Verificar se existe lane com a rota (cityOriginId -> ufDestination)
257
+ var hasDirectLane = (_a = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardLane) === null || _a === void 0 ? void 0 : _a.some(function (lane) {
258
+ return lane.cityOriginId === cte.cityOriginId && lane.ufDestination === destinationUf_1;
259
+ });
260
+ return isValidDate && isValidModal && hasDirectLane;
251
261
  });
252
262
  if (ratecardFromThisYear) {
253
263
  ratecard = ratecardFromThisYear;
254
264
  }
265
+ else {
266
+ // Rota reversa: Verificar se existe lane com rota inversa (cityDestinationId -> originUf)
267
+ var reverseRatecardFromThisYear = cte.carrier.Ratecard.find(function (ratecard) {
268
+ var _a;
269
+ var isValidDate = (ratecard === null || ratecard === void 0 ? void 0 : ratecard.startsAt) && (ratecard === null || ratecard === void 0 ? void 0 : ratecard.endsAt) && today_1 >= new Date(ratecard.startsAt) && today_1 <= new Date(ratecard.endsAt);
270
+ var isValidModal = (ratecard === null || ratecard === void 0 ? void 0 : ratecard.modalId) === cte.ratecardModalId;
271
+ // Verificar se existe lane com a rota inversa (cityDestinationId -> originUf)
272
+ var hasReverseLane = (_a = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardLane) === null || _a === void 0 ? void 0 : _a.some(function (lane) {
273
+ return lane.cityOriginId === cte.cityDestinationId && lane.ufDestination === originUf_1;
274
+ });
275
+ return isValidDate && isValidModal && hasReverseLane;
276
+ });
277
+ if (reverseRatecardFromThisYear) {
278
+ ratecard = reverseRatecardFromThisYear;
279
+ }
280
+ }
255
281
  break;
256
282
  }
257
283
  }
@@ -337,6 +363,16 @@ function __generator(thisArg, body) {
337
363
  }
338
364
  }
339
365
 
366
+ function __spreadArray(to, from, pack) {
367
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
368
+ if (ar || !(i in from)) {
369
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
370
+ ar[i] = from[i];
371
+ }
372
+ }
373
+ return to.concat(ar || Array.prototype.slice.call(from));
374
+ }
375
+
340
376
  typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
341
377
  var e = new Error(message);
342
378
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
@@ -784,29 +820,6 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug, allFeesForCal
784
820
  }
785
821
  };
786
822
 
787
- var getCteLane = function (cte) {
788
- var _a, _b, _c, _d, _e;
789
- if (cte === null || cte === void 0 ? void 0 : cte.carrier) {
790
- var carrier = cte === null || cte === void 0 ? void 0 : cte.carrier;
791
- if ((_a = carrier === null || carrier === void 0 ? void 0 : carrier.Ratecard) === null || _a === void 0 ? void 0 : _a.length) {
792
- var destinationUf_1 = (_c = (_b = cte === null || cte === void 0 ? void 0 : cte.cityDestination) === null || _b === void 0 ? void 0 : _b.state) === null || _c === void 0 ? void 0 : _c.uf;
793
- if (carrier === null || carrier === void 0 ? void 0 : carrier.Ratecard[0]) {
794
- var ratecard = carrier === null || carrier === void 0 ? void 0 : carrier.Ratecard[0];
795
- if (ratecard.RatecardLane && ((_d = ratecard.RatecardLane) === null || _d === void 0 ? void 0 : _d.length) > 0 && cte.cityOriginId && destinationUf_1) {
796
- var region = (_e = ratecard.RatecardRegionCity) === null || _e === void 0 ? void 0 : _e.find(function (it) { return it.cityId === cte.cityDestinationId; });
797
- var regionId_1 = (region === null || region === void 0 ? void 0 : region.ratecardRegionId) ? region.ratecardRegionId : (region === null || region === void 0 ? void 0 : region.regionId) ? region.regionId : 0;
798
- return ratecard.RatecardLane.find(function (i) {
799
- if (i.cityOriginId === cte.cityOriginId && i.ufDestination === destinationUf_1 && i.ratecardRegionId === regionId_1) {
800
- return true;
801
- }
802
- return false;
803
- });
804
- }
805
- }
806
- }
807
- }
808
- };
809
-
810
823
  var verifyConditionalFee = function (conditionalFeeToVerify, cte) {
811
824
  var _a;
812
825
  if (conditionalFeeToVerify === null || conditionalFeeToVerify === void 0 ? void 0 : conditionalFeeToVerify.id) {
@@ -827,83 +840,217 @@ var verifyConditionalFee = function (conditionalFeeToVerify, cte) {
827
840
  return false;
828
841
  };
829
842
 
830
- var getLaneFeesToCalc = function (cte) {
831
- var _a, _b;
832
- var ratecard = getRatecardFromCte(cte);
833
- var lane = getCteLane(cte);
834
- if ((_a = lane === null || lane === void 0 ? void 0 : lane.RatecardLaneFee) === null || _a === void 0 ? void 0 : _a.length) {
835
- var newFilteredFees = (_b = lane === null || lane === void 0 ? void 0 : lane.RatecardLaneFee) === null || _b === void 0 ? void 0 : _b.filter(function (i) {
836
- var _a, _b, _c, _d, _e;
837
- var defaultConditionalFees = [
838
- FeeEnum.TDA_FREIGHT,
839
- FeeEnum.TDA_MIN,
840
- FeeEnum.TDE_FREIGHT,
841
- FeeEnum.TDE_MIN,
842
- ];
843
- var conditionalFeeToVerify;
844
- var conditionalFees = (_a = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardConditionalFee) === null || _a === void 0 ? void 0 : _a.filter(function (it) { var _a; return it.feeId === ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.id); });
845
- if (defaultConditionalFees.includes((_b = i.fee) === null || _b === void 0 ? void 0 : _b.id) && !(conditionalFees === null || conditionalFees === void 0 ? void 0 : conditionalFees.length)) {
846
- return false;
843
+ var getFilteredFeesToAudit = function (_a) {
844
+ var _b;
845
+ var lane = _a.lane, ratecard = _a.ratecard, cte = _a.cte;
846
+ var filteredFees = (_b = lane === null || lane === void 0 ? void 0 : lane.RatecardLaneFee) === null || _b === void 0 ? void 0 : _b.filter(function (i) {
847
+ var _a, _b, _c, _d, _e, _f, _g;
848
+ var defaultConditionalFees = [
849
+ FeeEnum.SUFRAMA,
850
+ FeeEnum.TDA_FREIGHT,
851
+ FeeEnum.TDA_MIN,
852
+ FeeEnum.TDE_FREIGHT,
853
+ FeeEnum.TDE_MIN,
854
+ FeeEnum.TRT_FREIGHT,
855
+ FeeEnum.TRT_MIN,
856
+ 94,
857
+ ];
858
+ var collectFees = [
859
+ FeeEnum.COLLECT_PERCENTAGE_TOTAL,
860
+ FeeEnum.COLLECT_REVERSE_PERCENTAGE_TOTAL,
861
+ ];
862
+ if (collectFees.includes((_a = i.fee) === null || _a === void 0 ? void 0 : _a.id)) {
863
+ switch ((_b = i.fee) === null || _b === void 0 ? void 0 : _b.id) {
864
+ case FeeEnum.COLLECT_PERCENTAGE_TOTAL:
865
+ return cte.isCollect;
866
+ case FeeEnum.COLLECT_REVERSE_PERCENTAGE_TOTAL:
867
+ return cte.isCollectReverse;
847
868
  }
848
- if (conditionalFees && (conditionalFees === null || conditionalFees === void 0 ? void 0 : conditionalFees.length) > 0) {
849
- var _loop_1 = function (index) {
850
- var conditionalFee = conditionalFees[index];
851
- if (conditionalFee) {
852
- conditionalFeeToVerify = conditionalFee;
853
- }
854
- else if ((_c = i.fee) === null || _c === void 0 ? void 0 : _c.parentId) {
855
- var siblingFee_1 = (_d = lane === null || lane === void 0 ? void 0 : lane.RatecardLaneFee) === null || _d === void 0 ? void 0 : _d.find(function (it) { var _a, _b; return ((_a = it.fee) === null || _a === void 0 ? void 0 : _a.parentId) === i.fee.parentId && ((_b = it.fee) === null || _b === void 0 ? void 0 : _b.id) !== i.fee.id; });
856
- if (siblingFee_1) {
857
- var conditionalFeeSibling = (_e = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardConditionalFee) === null || _e === void 0 ? void 0 : _e.find(function (i) { var _a; return i.feeId === ((_a = siblingFee_1.fee) === null || _a === void 0 ? void 0 : _a.id); });
858
- if (conditionalFeeSibling) {
859
- conditionalFeeToVerify = conditionalFeeSibling;
860
- }
869
+ }
870
+ var conditionalFeeToVerify;
871
+ var conditionalFees = (_c = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardConditionalFee) === null || _c === void 0 ? void 0 : _c.filter(function (it) { var _a; return it.feeId === ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.id); });
872
+ if (defaultConditionalFees.includes((_d = i.fee) === null || _d === void 0 ? void 0 : _d.id) && !(conditionalFees === null || conditionalFees === void 0 ? void 0 : conditionalFees.length)) {
873
+ return false;
874
+ }
875
+ if (conditionalFees && (conditionalFees === null || conditionalFees === void 0 ? void 0 : conditionalFees.length) > 0) {
876
+ var _loop_1 = function (index) {
877
+ var conditionalFee = conditionalFees[index];
878
+ if (conditionalFee) {
879
+ conditionalFeeToVerify = conditionalFee;
880
+ }
881
+ else if ((_e = i.fee) === null || _e === void 0 ? void 0 : _e.parentId) {
882
+ var siblingFee_1 = (_f = lane === null || lane === void 0 ? void 0 : lane.RatecardLaneFee) === null || _f === void 0 ? void 0 : _f.find(function (it) { var _a, _b; return ((_a = it.fee) === null || _a === void 0 ? void 0 : _a.parentId) === i.fee.parentId && ((_b = it.fee) === null || _b === void 0 ? void 0 : _b.id) !== i.fee.id; });
883
+ if (siblingFee_1) {
884
+ var conditionalFeeSibling = (_g = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardConditionalFee) === null || _g === void 0 ? void 0 : _g.find(function (i) { var _a; return i.feeId === ((_a = siblingFee_1.fee) === null || _a === void 0 ? void 0 : _a.id); });
885
+ if (conditionalFeeSibling) {
886
+ conditionalFeeToVerify = conditionalFeeSibling;
861
887
  }
862
888
  }
863
- if (conditionalFeeToVerify === null || conditionalFeeToVerify === void 0 ? void 0 : conditionalFeeToVerify.id) {
864
- var result = verifyConditionalFee(conditionalFeeToVerify, cte);
865
- if (result) {
866
- return { value: true };
867
- }
868
- else if (index === conditionalFees.length - 1) {
869
- return { value: false };
870
- }
889
+ }
890
+ if (conditionalFeeToVerify === null || conditionalFeeToVerify === void 0 ? void 0 : conditionalFeeToVerify.id) {
891
+ var result = verifyConditionalFee(conditionalFeeToVerify, cte);
892
+ if (result) {
893
+ return { value: true };
894
+ }
895
+ else if (index === conditionalFees.length - 1) {
896
+ return { value: false };
871
897
  }
872
- };
873
- for (var index = 0; index < conditionalFees.length; index++) {
874
- var state_1 = _loop_1(index);
875
- if (typeof state_1 === "object")
876
- return state_1.value;
877
898
  }
899
+ };
900
+ for (var index = 0; index < conditionalFees.length; index++) {
901
+ var state_1 = _loop_1(index);
902
+ if (typeof state_1 === "object")
903
+ return state_1.value;
878
904
  }
879
- if (i.minWeight) {
880
- if (cte === null || cte === void 0 ? void 0 : cte.taxedWeight) {
881
- if (Math.ceil(+cte.taxedWeight) >= +i.minWeight) {
882
- if (Math.ceil(+cte.taxedWeight) <= +i.maxWeight) {
883
- return true;
884
- }
885
- else if (!i.maxWeight) {
886
- return true;
887
- }
888
- }
905
+ }
906
+ if (i.minKm !== null && i.minKm !== undefined) {
907
+ if (cte === null || cte === void 0 ? void 0 : cte.distance) {
908
+ var distanceInRange = +cte.distance >= +i.minKm && (!i.maxKm || +cte.distance <= +i.maxKm);
909
+ var vehicleTypeMatches = !i.vehicleTypeId || cte.vehicleTypeId === i.vehicleTypeId;
910
+ if (distanceInRange && vehicleTypeMatches) {
911
+ return true;
889
912
  }
890
913
  }
891
- else {
892
- return true;
914
+ }
915
+ else if (i.minWeight) {
916
+ if (cte === null || cte === void 0 ? void 0 : cte.taxedWeight) {
917
+ if (Math.ceil(+cte.taxedWeight) >= +i.minWeight) {
918
+ if (Math.ceil(+cte.taxedWeight) <= +i.maxWeight) {
919
+ return true;
920
+ }
921
+ else if (!i.maxWeight) {
922
+ return true;
923
+ }
924
+ }
893
925
  }
926
+ }
927
+ else {
928
+ return true;
929
+ }
930
+ return false;
931
+ });
932
+ // Remover fees duplicadas, mantendo apenas uma ocorrência de cada feeId
933
+ var seenFeeIds = new Set();
934
+ var uniqueFees = filteredFees === null || filteredFees === void 0 ? void 0 : filteredFees.filter(function (fee) {
935
+ if (fee.feeId && seenFeeIds.has(fee.feeId)) {
894
936
  return false;
895
- });
896
- if ((newFilteredFees === null || newFilteredFees === void 0 ? void 0 : newFilteredFees.length) > 0) {
897
- return newFilteredFees;
898
937
  }
938
+ if (fee.feeId) {
939
+ seenFeeIds.add(fee.feeId);
940
+ }
941
+ return true;
942
+ });
943
+ return uniqueFees;
944
+ };
945
+
946
+ var getAllFeesForCalculation = function (_a) {
947
+ var _b;
948
+ var lane = _a.lane, ratecard = _a.ratecard, cte = _a.cte;
949
+ // Obter taxas aplicáveis (função atual)
950
+ var applicableFees = getFilteredFeesToAudit({ lane: lane, ratecard: ratecard, cte: cte });
951
+ // Extrair tipos de taxas únicos das taxas aplicáveis
952
+ var feeTypes = Array.from(new Set(applicableFees === null || applicableFees === void 0 ? void 0 : applicableFees.map(function (fee) { var _a; return (_a = fee.fee) === null || _a === void 0 ? void 0 : _a.id; }).filter(Boolean)));
953
+ // Encontrar taxas de intervalo do mesmo tipo
954
+ var intervalFees = (_b = lane === null || lane === void 0 ? void 0 : lane.RatecardLaneFee) === null || _b === void 0 ? void 0 : _b.filter(function (fee) {
955
+ var _a;
956
+ return feeTypes.includes((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.id) &&
957
+ (fee.minWeight || fee.maxWeight);
958
+ });
959
+ // Combinar taxas aplicáveis com taxas de intervalo, removendo duplicatas
960
+ var allFees = __spreadArray(__spreadArray([], (applicableFees || []), true), (intervalFees || []), true);
961
+ var uniqueFees = Array.from(new Map(allFees.map(function (fee) { return [fee.id, fee]; })).values());
962
+ return uniqueFees;
963
+ };
964
+
965
+ var getLaneFromRatecard = function (cte) {
966
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
967
+ var ratecard = getRatecardFromCte(cte);
968
+ var cityOriginId = cte.cityOriginId, cityDestination = cte.cityDestination;
969
+ var destinationUf = (_a = cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.state) === null || _a === void 0 ? void 0 : _a.uf;
970
+ // Encontrar a ratecardRegion da cidade de destino
971
+ var destinationRatecardRegionId = (_c = (_b = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardRegionCity) === null || _b === void 0 ? void 0 : _b.find(function (regionCity) { return regionCity.cityId === (cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.id); })) === null || _c === void 0 ? void 0 : _c.ratecardRegionId;
972
+ if (!(ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardLane) || ((_d = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardLane) === null || _d === void 0 ? void 0 : _d.length) === 0) {
973
+ return undefined;
974
+ }
975
+ // Lógica diferenciada por modal
976
+ switch (ratecard.modalId) {
977
+ case RatecardModalEnum.ROAD_DOMESTIC_FTL:
978
+ // FTL: Verificar rota por UF de destino
979
+ if (destinationUf) {
980
+ var directLane = ratecard.RatecardLane.find(function (i) {
981
+ return i.ufDestination === destinationUf && i.ratecardRegionId === destinationRatecardRegionId;
982
+ });
983
+ if (directLane) {
984
+ return directLane;
985
+ }
986
+ // Fallback: Verificar se existe lane única com fees de KM e cidade origem no ratecard_origin_cities
987
+ var uniqueLane = ratecard.RatecardLane[0];
988
+ // Verificar se a lane tem fees com intervalo de KM
989
+ var hasKmFees = (_e = uniqueLane === null || uniqueLane === void 0 ? void 0 : uniqueLane.RatecardLaneFee) === null || _e === void 0 ? void 0 : _e.some(function (fee) { return fee.minKm !== null; });
990
+ // Verificar se a cidade de origem está no ratecard_origin_cities
991
+ var hasOriginCity = (_f = ratecard.ratecard_origin_cities) === null || _f === void 0 ? void 0 : _f.some(function (originCity) { return originCity.cityId === cityOriginId; });
992
+ if (hasKmFees && hasOriginCity) {
993
+ // Mockar informações para exibição no frontend
994
+ return __assign(__assign({}, uniqueLane), { city: cte.cityOrigin, ufDestination: destinationUf, ratecardRegion: uniqueLane.ratecardRegion || { name: destinationUf } });
995
+ }
996
+ // Rota inversa FTL: Verificar se cidade de destino está em ratecard_origin_cities
997
+ var originUf_1 = (_h = (_g = cte.cityOrigin) === null || _g === void 0 ? void 0 : _g.state) === null || _h === void 0 ? void 0 : _h.uf;
998
+ if (originUf_1) {
999
+ // Verificar se existe lane com ufDestination = originUf
1000
+ var reverseLane = ratecard.RatecardLane.find(function (i) {
1001
+ return i.ufDestination === originUf_1;
1002
+ });
1003
+ if (reverseLane) {
1004
+ // Verificar se a cidade de destino está no ratecard_origin_cities
1005
+ var hasDestinationCity = (_j = ratecard.ratecard_origin_cities) === null || _j === void 0 ? void 0 : _j.some(function (originCity) { return originCity.cityId === (cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.id); });
1006
+ if (hasDestinationCity) {
1007
+ return reverseLane;
1008
+ }
1009
+ }
1010
+ // Fallback inverso: Lane única com cidade de destino em ratecard_origin_cities
1011
+ var hasDestinationCityInOrigins = (_k = ratecard.ratecard_origin_cities) === null || _k === void 0 ? void 0 : _k.some(function (originCity) { return originCity.cityId === (cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.id); });
1012
+ if (hasKmFees && hasDestinationCityInOrigins) {
1013
+ // Mockar informações para exibição no frontend (rota inversa)
1014
+ return __assign(__assign({}, uniqueLane), { city: cityDestination, ufDestination: originUf_1, ratecardRegion: uniqueLane.ratecardRegion || { name: originUf_1 } });
1015
+ }
1016
+ }
1017
+ }
1018
+ break;
1019
+ case RatecardModalEnum.ROAD_DOMESTIC_LTL:
1020
+ default:
1021
+ // LTL: Lógica original (rota direta + inversa)
1022
+ if (cityOriginId && destinationUf) {
1023
+ // Primeiro, buscar rota direta (Origem -> Destino)
1024
+ var directLane = ratecard.RatecardLane.find(function (i) {
1025
+ return i.cityOriginId === cityOriginId && i.ufDestination === destinationUf && i.ratecardRegionId === destinationRatecardRegionId;
1026
+ });
1027
+ if (directLane) {
1028
+ return directLane;
1029
+ }
1030
+ // Se não encontrar rota direta, buscar rota inversa (Destino -> Origem)
1031
+ // Para isso, precisamos encontrar a UF da origem e sua ratecardRegion
1032
+ var originUf_2 = (_m = (_l = cte.cityOrigin) === null || _l === void 0 ? void 0 : _l.state) === null || _m === void 0 ? void 0 : _m.uf;
1033
+ // Encontrar a ratecardRegion da cidade de origem
1034
+ var originRatecardRegionId_1 = (_p = (_o = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardRegionCity) === null || _o === void 0 ? void 0 : _o.find(function (regionCity) { return regionCity.cityId === cityOriginId; })) === null || _p === void 0 ? void 0 : _p.ratecardRegionId;
1035
+ if (originUf_2) {
1036
+ var reverseLane = ratecard.RatecardLane.find(function (i) {
1037
+ return i.cityOriginId === (cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.id) && i.ufDestination === originUf_2 && i.ratecardRegionId === originRatecardRegionId_1;
1038
+ });
1039
+ if (reverseLane) {
1040
+ return reverseLane;
1041
+ }
1042
+ }
1043
+ }
1044
+ break;
899
1045
  }
1046
+ return undefined;
900
1047
  };
901
1048
 
902
- var getCtesFeesResult = function (feesToCalc, cte) {
1049
+ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
903
1050
  var _a, _b, _c, _d;
904
1051
  var results = [];
905
1052
  var _loop_1 = function (item) {
906
- var feeTotal = calculateFee(item, cte, feesToCalc);
1053
+ var feeTotal = calculateFee(item, cte, feesToCalc, true, allFeesForCalc);
907
1054
  if (feeTotal === null || feeTotal === void 0 ? void 0 : feeTotal.totalFee) {
908
1055
  var totalFee = feeTotal.totalFee, resultFee = feeTotal.resultFee, feeValue = feeTotal.feeValue;
909
1056
  var totalToPush = totalFee;
@@ -947,9 +1094,9 @@ var getCtesFeesResult = function (feesToCalc, cte) {
947
1094
  return results;
948
1095
  };
949
1096
 
950
- var getCteLaneFeesTotal = function (feesToCalc, cte) {
1097
+ var getCteLaneFeesTotal = function (feesToCalc, cte, allFeesForCalc) {
951
1098
  if ((feesToCalc === null || feesToCalc === void 0 ? void 0 : feesToCalc.length) > 0) {
952
- var results = getCtesFeesResult(feesToCalc, cte);
1099
+ var results = getCtesFeesResult(feesToCalc, cte, allFeesForCalc);
953
1100
  /*const results = [];
954
1101
 
955
1102
  for (const item of feesToCalc) {
@@ -1022,9 +1169,26 @@ var getCteLaneFeesTotal = function (feesToCalc, cte) {
1022
1169
  };
1023
1170
 
1024
1171
  var getAuditTotalFromCte = function (cte) {
1025
- var laneFees = getLaneFeesToCalc(cte);
1026
- var total = getCteLaneFeesTotal(laneFees !== null && laneFees !== void 0 ? laneFees : [], cte);
1027
- if (cte.id === 38) ;
1172
+ var lane = getLaneFromRatecard(cte);
1173
+ var ratecard = getRatecardFromCte(cte);
1174
+ // Obter taxas filtradas com lógica de coleta
1175
+ var laneFees = lane && ratecard ?
1176
+ getFilteredFeesToAudit({ lane: lane, ratecard: ratecard, cte: cte }) : [];
1177
+ // Obter todas as taxas para cálculo
1178
+ var allFeesForCalc = lane && ratecard ?
1179
+ getAllFeesForCalculation({
1180
+ lane: lane,
1181
+ ratecard: ratecard,
1182
+ cte: cte
1183
+ }) : [];
1184
+ var total = getCteLaneFeesTotal(laneFees || [], cte, allFeesForCalc);
1185
+ // Se for reentrega, verificar o valor mínimo
1186
+ if (cte.isRedelivery && total) {
1187
+ var ratecard_1 = getRatecardFromCte(cte);
1188
+ if ((ratecard_1 === null || ratecard_1 === void 0 ? void 0 : ratecard_1.redeliveryMinValue) && total < ratecard_1.redeliveryMinValue) {
1189
+ return +ratecard_1.redeliveryMinValue;
1190
+ }
1191
+ }
1028
1192
  return total !== null && total !== void 0 ? total : 0;
1029
1193
  };
1030
1194
 
@@ -7159,176 +7323,99 @@ var getCteDateRange = function (ctes) {
7159
7323
  return "".concat(formatDate(oldest), " - ").concat(formatDate(newest));
7160
7324
  };
7161
7325
 
7162
- var getFilteredFeesToAudit = function (_a) {
7163
- var _b;
7164
- var lane = _a.lane, ratecard = _a.ratecard, cte = _a.cte;
7165
- var filteredFees = (_b = lane === null || lane === void 0 ? void 0 : lane.RatecardLaneFee) === null || _b === void 0 ? void 0 : _b.filter(function (i) {
7166
- var _a, _b, _c, _d, _e;
7167
- var defaultConditionalFees = [
7168
- FeeEnum.SUFRAMA,
7169
- FeeEnum.TDA_FREIGHT,
7170
- FeeEnum.TDA_MIN,
7171
- FeeEnum.TDE_FREIGHT,
7172
- FeeEnum.TDE_MIN,
7173
- ];
7174
- var conditionalFeeToVerify;
7175
- var conditionalFees = (_a = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardConditionalFee) === null || _a === void 0 ? void 0 : _a.filter(function (it) { var _a; return it.feeId === ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.id); });
7176
- if (defaultConditionalFees.includes((_b = i.fee) === null || _b === void 0 ? void 0 : _b.id) && !(conditionalFees === null || conditionalFees === void 0 ? void 0 : conditionalFees.length)) {
7177
- return false;
7178
- }
7179
- if (conditionalFees && (conditionalFees === null || conditionalFees === void 0 ? void 0 : conditionalFees.length) > 0) {
7180
- var _loop_1 = function (index) {
7181
- var conditionalFee = conditionalFees[index];
7182
- if (conditionalFee) {
7183
- conditionalFeeToVerify = conditionalFee;
7184
- }
7185
- else if ((_c = i.fee) === null || _c === void 0 ? void 0 : _c.parentId) {
7186
- var siblingFee_1 = (_d = lane === null || lane === void 0 ? void 0 : lane.RatecardLaneFee) === null || _d === void 0 ? void 0 : _d.find(function (it) { var _a, _b; return ((_a = it.fee) === null || _a === void 0 ? void 0 : _a.parentId) === i.fee.parentId && ((_b = it.fee) === null || _b === void 0 ? void 0 : _b.id) !== i.fee.id; });
7187
- if (siblingFee_1) {
7188
- var conditionalFeeSibling = (_e = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardConditionalFee) === null || _e === void 0 ? void 0 : _e.find(function (i) { var _a; return i.feeId === ((_a = siblingFee_1.fee) === null || _a === void 0 ? void 0 : _a.id); });
7189
- if (conditionalFeeSibling) {
7190
- conditionalFeeToVerify = conditionalFeeSibling;
7326
+ var getCteLane = function (cte) {
7327
+ var _a, _b, _c, _d, _e;
7328
+ if (cte === null || cte === void 0 ? void 0 : cte.carrier) {
7329
+ var carrier = cte === null || cte === void 0 ? void 0 : cte.carrier;
7330
+ if ((_a = carrier === null || carrier === void 0 ? void 0 : carrier.Ratecard) === null || _a === void 0 ? void 0 : _a.length) {
7331
+ var destinationUf_1 = (_c = (_b = cte === null || cte === void 0 ? void 0 : cte.cityDestination) === null || _b === void 0 ? void 0 : _b.state) === null || _c === void 0 ? void 0 : _c.uf;
7332
+ if (carrier === null || carrier === void 0 ? void 0 : carrier.Ratecard[0]) {
7333
+ var ratecard = carrier === null || carrier === void 0 ? void 0 : carrier.Ratecard[0];
7334
+ if (ratecard.RatecardLane && ((_d = ratecard.RatecardLane) === null || _d === void 0 ? void 0 : _d.length) > 0 && cte.cityOriginId && destinationUf_1) {
7335
+ var region = (_e = ratecard.RatecardRegionCity) === null || _e === void 0 ? void 0 : _e.find(function (it) { return it.cityId === cte.cityDestinationId; });
7336
+ var regionId_1 = (region === null || region === void 0 ? void 0 : region.ratecardRegionId) ? region.ratecardRegionId : (region === null || region === void 0 ? void 0 : region.regionId) ? region.regionId : 0;
7337
+ return ratecard.RatecardLane.find(function (i) {
7338
+ if (i.cityOriginId === cte.cityOriginId && i.ufDestination === destinationUf_1 && i.ratecardRegionId === regionId_1) {
7339
+ return true;
7191
7340
  }
7192
- }
7193
- }
7194
- if (conditionalFeeToVerify === null || conditionalFeeToVerify === void 0 ? void 0 : conditionalFeeToVerify.id) {
7195
- var result = verifyConditionalFee(conditionalFeeToVerify, cte);
7196
- if (result) {
7197
- return { value: true };
7198
- }
7199
- else if (index === conditionalFees.length - 1) {
7200
- return { value: false };
7201
- }
7202
- }
7203
- };
7204
- for (var index = 0; index < conditionalFees.length; index++) {
7205
- var state_1 = _loop_1(index);
7206
- if (typeof state_1 === "object")
7207
- return state_1.value;
7208
- }
7209
- }
7210
- if (i.minKm !== null && i.minKm !== undefined) {
7211
- if (cte === null || cte === void 0 ? void 0 : cte.distance) {
7212
- var distanceInRange = +cte.distance >= +i.minKm && (!i.maxKm || +cte.distance <= +i.maxKm);
7213
- var vehicleTypeMatches = !i.vehicleTypeId || cte.vehicleTypeId === i.vehicleTypeId;
7214
- if (distanceInRange && vehicleTypeMatches) {
7215
- return true;
7216
- }
7217
- }
7218
- }
7219
- else if (i.minWeight) {
7220
- if (cte === null || cte === void 0 ? void 0 : cte.taxedWeight) {
7221
- if (Math.ceil(+cte.taxedWeight) >= +i.minWeight) {
7222
- if (Math.ceil(+cte.taxedWeight) <= +i.maxWeight) {
7223
- return true;
7224
- }
7225
- else if (!i.maxWeight) {
7226
- return true;
7227
- }
7341
+ return false;
7342
+ });
7228
7343
  }
7229
7344
  }
7230
7345
  }
7231
- else {
7232
- return true;
7233
- }
7234
- return false;
7235
- });
7236
- // Remover fees duplicadas, mantendo apenas uma ocorrência de cada feeId
7237
- var seenFeeIds = new Set();
7238
- var uniqueFees = filteredFees === null || filteredFees === void 0 ? void 0 : filteredFees.filter(function (fee) {
7239
- if (fee.feeId && seenFeeIds.has(fee.feeId)) {
7240
- return false;
7241
- }
7242
- if (fee.feeId) {
7243
- seenFeeIds.add(fee.feeId);
7244
- }
7245
- return true;
7246
- });
7247
- return uniqueFees;
7346
+ }
7248
7347
  };
7249
7348
 
7250
- var getLaneFromRatecard = function (cte) {
7251
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
7349
+ var getLaneFeesToCalc = function (cte) {
7350
+ var _a, _b;
7252
7351
  var ratecard = getRatecardFromCte(cte);
7253
- var cityOriginId = cte.cityOriginId, cityDestination = cte.cityDestination;
7254
- var destinationUf = (_a = cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.state) === null || _a === void 0 ? void 0 : _a.uf;
7255
- // Encontrar a ratecardRegion da cidade de destino
7256
- var destinationRatecardRegionId = (_c = (_b = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardRegionCity) === null || _b === void 0 ? void 0 : _b.find(function (regionCity) { return regionCity.cityId === (cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.id); })) === null || _c === void 0 ? void 0 : _c.ratecardRegionId;
7257
- if (!(ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardLane) || ((_d = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardLane) === null || _d === void 0 ? void 0 : _d.length) === 0) {
7258
- return undefined;
7259
- }
7260
- // Lógica diferenciada por modal
7261
- switch (ratecard.modalId) {
7262
- case RatecardModalEnum.ROAD_DOMESTIC_FTL:
7263
- // FTL: Verificar rota por UF de destino
7264
- if (destinationUf) {
7265
- var directLane = ratecard.RatecardLane.find(function (i) {
7266
- return i.ufDestination === destinationUf && i.ratecardRegionId === destinationRatecardRegionId;
7267
- });
7268
- if (directLane) {
7269
- return directLane;
7270
- }
7271
- // Fallback: Verificar se existe lane única com fees de KM e cidade origem no ratecard_origin_cities
7272
- var uniqueLane = ratecard.RatecardLane[0];
7273
- // Verificar se a lane tem fees com intervalo de KM
7274
- var hasKmFees = (_e = uniqueLane === null || uniqueLane === void 0 ? void 0 : uniqueLane.RatecardLaneFee) === null || _e === void 0 ? void 0 : _e.some(function (fee) { return fee.minKm !== null; });
7275
- // Verificar se a cidade de origem está no ratecard_origin_cities
7276
- var hasOriginCity = (_f = ratecard.ratecard_origin_cities) === null || _f === void 0 ? void 0 : _f.some(function (originCity) { return originCity.cityId === cityOriginId; });
7277
- if (hasKmFees && hasOriginCity) {
7278
- // Mockar informações para exibição no frontend
7279
- return __assign(__assign({}, uniqueLane), { city: cte.cityOrigin, ufDestination: destinationUf, ratecardRegion: uniqueLane.ratecardRegion || { name: destinationUf } });
7280
- }
7281
- // Rota inversa FTL: Verificar se cidade de destino está em ratecard_origin_cities
7282
- var originUf_1 = (_h = (_g = cte.cityOrigin) === null || _g === void 0 ? void 0 : _g.state) === null || _h === void 0 ? void 0 : _h.uf;
7283
- if (originUf_1) {
7284
- // Verificar se existe lane com ufDestination = originUf
7285
- var reverseLane = ratecard.RatecardLane.find(function (i) {
7286
- return i.ufDestination === originUf_1;
7287
- });
7288
- if (reverseLane) {
7289
- // Verificar se a cidade de destino está no ratecard_origin_cities
7290
- var hasDestinationCity = (_j = ratecard.ratecard_origin_cities) === null || _j === void 0 ? void 0 : _j.some(function (originCity) { return originCity.cityId === (cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.id); });
7291
- if (hasDestinationCity) {
7292
- return reverseLane;
7352
+ var lane = getCteLane(cte);
7353
+ if ((_a = lane === null || lane === void 0 ? void 0 : lane.RatecardLaneFee) === null || _a === void 0 ? void 0 : _a.length) {
7354
+ var newFilteredFees = (_b = lane === null || lane === void 0 ? void 0 : lane.RatecardLaneFee) === null || _b === void 0 ? void 0 : _b.filter(function (i) {
7355
+ var _a, _b, _c, _d, _e;
7356
+ var defaultConditionalFees = [
7357
+ FeeEnum.TDA_FREIGHT,
7358
+ FeeEnum.TDA_MIN,
7359
+ FeeEnum.TDE_FREIGHT,
7360
+ FeeEnum.TDE_MIN,
7361
+ ];
7362
+ var conditionalFeeToVerify;
7363
+ var conditionalFees = (_a = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardConditionalFee) === null || _a === void 0 ? void 0 : _a.filter(function (it) { var _a; return it.feeId === ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.id); });
7364
+ if (defaultConditionalFees.includes((_b = i.fee) === null || _b === void 0 ? void 0 : _b.id) && !(conditionalFees === null || conditionalFees === void 0 ? void 0 : conditionalFees.length)) {
7365
+ return false;
7366
+ }
7367
+ if (conditionalFees && (conditionalFees === null || conditionalFees === void 0 ? void 0 : conditionalFees.length) > 0) {
7368
+ var _loop_1 = function (index) {
7369
+ var conditionalFee = conditionalFees[index];
7370
+ if (conditionalFee) {
7371
+ conditionalFeeToVerify = conditionalFee;
7372
+ }
7373
+ else if ((_c = i.fee) === null || _c === void 0 ? void 0 : _c.parentId) {
7374
+ var siblingFee_1 = (_d = lane === null || lane === void 0 ? void 0 : lane.RatecardLaneFee) === null || _d === void 0 ? void 0 : _d.find(function (it) { var _a, _b; return ((_a = it.fee) === null || _a === void 0 ? void 0 : _a.parentId) === i.fee.parentId && ((_b = it.fee) === null || _b === void 0 ? void 0 : _b.id) !== i.fee.id; });
7375
+ if (siblingFee_1) {
7376
+ var conditionalFeeSibling = (_e = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardConditionalFee) === null || _e === void 0 ? void 0 : _e.find(function (i) { var _a; return i.feeId === ((_a = siblingFee_1.fee) === null || _a === void 0 ? void 0 : _a.id); });
7377
+ if (conditionalFeeSibling) {
7378
+ conditionalFeeToVerify = conditionalFeeSibling;
7379
+ }
7293
7380
  }
7294
7381
  }
7295
- // Fallback inverso: Lane única com cidade de destino em ratecard_origin_cities
7296
- var hasDestinationCityInOrigins = (_k = ratecard.ratecard_origin_cities) === null || _k === void 0 ? void 0 : _k.some(function (originCity) { return originCity.cityId === (cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.id); });
7297
- if (hasKmFees && hasDestinationCityInOrigins) {
7298
- // Mockar informações para exibição no frontend (rota inversa)
7299
- return __assign(__assign({}, uniqueLane), { city: cityDestination, ufDestination: originUf_1, ratecardRegion: uniqueLane.ratecardRegion || { name: originUf_1 } });
7382
+ if (conditionalFeeToVerify === null || conditionalFeeToVerify === void 0 ? void 0 : conditionalFeeToVerify.id) {
7383
+ var result = verifyConditionalFee(conditionalFeeToVerify, cte);
7384
+ if (result) {
7385
+ return { value: true };
7386
+ }
7387
+ else if (index === conditionalFees.length - 1) {
7388
+ return { value: false };
7389
+ }
7300
7390
  }
7391
+ };
7392
+ for (var index = 0; index < conditionalFees.length; index++) {
7393
+ var state_1 = _loop_1(index);
7394
+ if (typeof state_1 === "object")
7395
+ return state_1.value;
7301
7396
  }
7302
7397
  }
7303
- break;
7304
- case RatecardModalEnum.ROAD_DOMESTIC_LTL:
7305
- default:
7306
- // LTL: Lógica original (rota direta + inversa)
7307
- if (cityOriginId && destinationUf) {
7308
- // Primeiro, buscar rota direta (Origem -> Destino)
7309
- var directLane = ratecard.RatecardLane.find(function (i) {
7310
- return i.cityOriginId === cityOriginId && i.ufDestination === destinationUf && i.ratecardRegionId === destinationRatecardRegionId;
7311
- });
7312
- if (directLane) {
7313
- return directLane;
7314
- }
7315
- // Se não encontrar rota direta, buscar rota inversa (Destino -> Origem)
7316
- // Para isso, precisamos encontrar a UF da origem e sua ratecardRegion
7317
- var originUf_2 = (_m = (_l = cte.cityOrigin) === null || _l === void 0 ? void 0 : _l.state) === null || _m === void 0 ? void 0 : _m.uf;
7318
- // Encontrar a ratecardRegion da cidade de origem
7319
- var originRatecardRegionId_1 = (_p = (_o = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardRegionCity) === null || _o === void 0 ? void 0 : _o.find(function (regionCity) { return regionCity.cityId === cityOriginId; })) === null || _p === void 0 ? void 0 : _p.ratecardRegionId;
7320
- if (originUf_2) {
7321
- var reverseLane = ratecard.RatecardLane.find(function (i) {
7322
- return i.cityOriginId === (cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.id) && i.ufDestination === originUf_2 && i.ratecardRegionId === originRatecardRegionId_1;
7323
- });
7324
- if (reverseLane) {
7325
- return reverseLane;
7398
+ if (i.minWeight) {
7399
+ if (cte === null || cte === void 0 ? void 0 : cte.taxedWeight) {
7400
+ if (Math.ceil(+cte.taxedWeight) >= +i.minWeight) {
7401
+ if (Math.ceil(+cte.taxedWeight) <= +i.maxWeight) {
7402
+ return true;
7403
+ }
7404
+ else if (!i.maxWeight) {
7405
+ return true;
7406
+ }
7326
7407
  }
7327
7408
  }
7328
7409
  }
7329
- break;
7410
+ else {
7411
+ return true;
7412
+ }
7413
+ return false;
7414
+ });
7415
+ if ((newFilteredFees === null || newFilteredFees === void 0 ? void 0 : newFilteredFees.length) > 0) {
7416
+ return newFilteredFees;
7417
+ }
7330
7418
  }
7331
- return undefined;
7332
7419
  };
7333
7420
 
7334
7421
  var getNormalizedCityName = function (name) {
@@ -7526,5 +7613,5 @@ var setFormattedDatesInObjects = function (objectFormat) { return __awaiter(void
7526
7613
  });
7527
7614
  }); };
7528
7615
 
7529
- export { ApplicationColumnNameEnum, ApplicationEnum, CountryEnum, CteStatusEnum, CteVehicleTypeEnum, CurrencyEnum, DocumentTypeEnum, DomainConfigurationEnum, DomainTypeEnum, FeeCalculationTypeEnum, FeeCategoryEnum, FeeEnum, FreightRegionEnum, ImapHostsEnum, ModalEnum, NotificationTypeEnum, PermissionEnum, RatecardConditionalFeeTypeEnum, RatecardModalEnum, SlaRegionEnum, SpotStatusEnum, TrackProcessProviderTypeEnum, applyRedeliveryMultiplier, calculateFee, convertNumberToCurrency, filterSiblingFees, formatDateString, getAuditTotalFromCte, getConfigurationFromDomain, getContractFromFreight, getContractRouteFromFreight, getCookies, getCteDateRange, getCteLane, getCteLaneFeesTotal, getCtesFeesResult, getDataFromToken, getFilteredFeesToAudit, getFormattedFreightPlaceName, getLaneFeesToCalc, getLaneFromRatecard, getNormalizedCityName, getRatecardFromCte, getRouteDeliveryTimeFromFreight, getRouteOnTimeFromFreight, normalizeString, setFormattedDatesInObjects, verifyConditionalFee, verifyDefaultFees };
7616
+ export { ApplicationColumnNameEnum, ApplicationEnum, CountryEnum, CteStatusEnum, CteVehicleTypeEnum, CurrencyEnum, DocumentTypeEnum, DomainConfigurationEnum, DomainTypeEnum, FeeCalculationTypeEnum, FeeCategoryEnum, FeeEnum, FreightRegionEnum, ImapHostsEnum, ModalEnum, NotificationTypeEnum, PermissionEnum, RatecardConditionalFeeTypeEnum, RatecardModalEnum, SlaRegionEnum, SpotStatusEnum, TrackProcessProviderTypeEnum, applyRedeliveryMultiplier, calculateFee, convertNumberToCurrency, filterSiblingFees, formatDateString, getAllFeesForCalculation, getAuditTotalFromCte, getConfigurationFromDomain, getContractFromFreight, getContractRouteFromFreight, getCookies, getCteDateRange, getCteLane, getCteLaneFeesTotal, getCtesFeesResult, getDataFromToken, getFilteredFeesToAudit, getFormattedFreightPlaceName, getLaneFeesToCalc, getLaneFromRatecard, getNormalizedCityName, getRatecardFromCte, getRouteDeliveryTimeFromFreight, getRouteOnTimeFromFreight, normalizeString, setFormattedDatesInObjects, verifyConditionalFee, verifyDefaultFees };
7530
7617
  //# sourceMappingURL=index.esm.js.map