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