b2m-utils 0.0.229 → 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
@@ -367,6 +367,16 @@ function __generator(thisArg, body) {
367
367
  }
368
368
  }
369
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
+
370
380
  typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
371
381
  var e = new Error(message);
372
382
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
@@ -814,29 +824,6 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug, allFeesForCal
814
824
  }
815
825
  };
816
826
 
817
- var getCteLane = function (cte) {
818
- var _a, _b, _c, _d, _e;
819
- if (cte === null || cte === void 0 ? void 0 : cte.carrier) {
820
- var carrier = cte === null || cte === void 0 ? void 0 : cte.carrier;
821
- if ((_a = carrier === null || carrier === void 0 ? void 0 : carrier.Ratecard) === null || _a === void 0 ? void 0 : _a.length) {
822
- 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;
823
- if (carrier === null || carrier === void 0 ? void 0 : carrier.Ratecard[0]) {
824
- var ratecard = carrier === null || carrier === void 0 ? void 0 : carrier.Ratecard[0];
825
- if (ratecard.RatecardLane && ((_d = ratecard.RatecardLane) === null || _d === void 0 ? void 0 : _d.length) > 0 && cte.cityOriginId && destinationUf_1) {
826
- var region = (_e = ratecard.RatecardRegionCity) === null || _e === void 0 ? void 0 : _e.find(function (it) { return it.cityId === cte.cityDestinationId; });
827
- 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;
828
- return ratecard.RatecardLane.find(function (i) {
829
- if (i.cityOriginId === cte.cityOriginId && i.ufDestination === destinationUf_1 && i.ratecardRegionId === regionId_1) {
830
- return true;
831
- }
832
- return false;
833
- });
834
- }
835
- }
836
- }
837
- }
838
- };
839
-
840
827
  var verifyConditionalFee = function (conditionalFeeToVerify, cte) {
841
828
  var _a;
842
829
  if (conditionalFeeToVerify === null || conditionalFeeToVerify === void 0 ? void 0 : conditionalFeeToVerify.id) {
@@ -857,83 +844,217 @@ var verifyConditionalFee = function (conditionalFeeToVerify, cte) {
857
844
  return false;
858
845
  };
859
846
 
860
- var getLaneFeesToCalc = function (cte) {
861
- var _a, _b;
862
- var ratecard = getRatecardFromCte(cte);
863
- var lane = getCteLane(cte);
864
- if ((_a = lane === null || lane === void 0 ? void 0 : lane.RatecardLaneFee) === null || _a === void 0 ? void 0 : _a.length) {
865
- var newFilteredFees = (_b = lane === null || lane === void 0 ? void 0 : lane.RatecardLaneFee) === null || _b === void 0 ? void 0 : _b.filter(function (i) {
866
- var _a, _b, _c, _d, _e;
867
- var defaultConditionalFees = [
868
- exports.FeeEnum.TDA_FREIGHT,
869
- exports.FeeEnum.TDA_MIN,
870
- exports.FeeEnum.TDE_FREIGHT,
871
- exports.FeeEnum.TDE_MIN,
872
- ];
873
- var conditionalFeeToVerify;
874
- 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); });
875
- if (defaultConditionalFees.includes((_b = i.fee) === null || _b === void 0 ? void 0 : _b.id) && !(conditionalFees === null || conditionalFees === void 0 ? void 0 : conditionalFees.length)) {
876
- 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;
877
872
  }
878
- if (conditionalFees && (conditionalFees === null || conditionalFees === void 0 ? void 0 : conditionalFees.length) > 0) {
879
- var _loop_1 = function (index) {
880
- var conditionalFee = conditionalFees[index];
881
- if (conditionalFee) {
882
- conditionalFeeToVerify = conditionalFee;
883
- }
884
- else if ((_c = i.fee) === null || _c === void 0 ? void 0 : _c.parentId) {
885
- 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; });
886
- if (siblingFee_1) {
887
- 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); });
888
- if (conditionalFeeSibling) {
889
- conditionalFeeToVerify = conditionalFeeSibling;
890
- }
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;
891
891
  }
892
892
  }
893
- if (conditionalFeeToVerify === null || conditionalFeeToVerify === void 0 ? void 0 : conditionalFeeToVerify.id) {
894
- var result = verifyConditionalFee(conditionalFeeToVerify, cte);
895
- if (result) {
896
- return { value: true };
897
- }
898
- else if (index === conditionalFees.length - 1) {
899
- return { value: false };
900
- }
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 };
901
901
  }
902
- };
903
- for (var index = 0; index < conditionalFees.length; index++) {
904
- var state_1 = _loop_1(index);
905
- if (typeof state_1 === "object")
906
- return state_1.value;
907
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;
908
908
  }
909
- if (i.minWeight) {
910
- if (cte === null || cte === void 0 ? void 0 : cte.taxedWeight) {
911
- if (Math.ceil(+cte.taxedWeight) >= +i.minWeight) {
912
- if (Math.ceil(+cte.taxedWeight) <= +i.maxWeight) {
913
- return true;
914
- }
915
- else if (!i.maxWeight) {
916
- return true;
917
- }
918
- }
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;
919
916
  }
920
917
  }
921
- else {
922
- 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
+ }
923
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)) {
924
940
  return false;
925
- });
926
- if ((newFilteredFees === null || newFilteredFees === void 0 ? void 0 : newFilteredFees.length) > 0) {
927
- return newFilteredFees;
928
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;
929
1049
  }
1050
+ return undefined;
930
1051
  };
931
1052
 
932
- var getCtesFeesResult = function (feesToCalc, cte) {
1053
+ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
933
1054
  var _a, _b, _c, _d;
934
1055
  var results = [];
935
1056
  var _loop_1 = function (item) {
936
- var feeTotal = calculateFee(item, cte, feesToCalc);
1057
+ var feeTotal = calculateFee(item, cte, feesToCalc, true, allFeesForCalc);
937
1058
  if (feeTotal === null || feeTotal === void 0 ? void 0 : feeTotal.totalFee) {
938
1059
  var totalFee = feeTotal.totalFee, resultFee = feeTotal.resultFee, feeValue = feeTotal.feeValue;
939
1060
  var totalToPush = totalFee;
@@ -977,9 +1098,9 @@ var getCtesFeesResult = function (feesToCalc, cte) {
977
1098
  return results;
978
1099
  };
979
1100
 
980
- var getCteLaneFeesTotal = function (feesToCalc, cte) {
1101
+ var getCteLaneFeesTotal = function (feesToCalc, cte, allFeesForCalc) {
981
1102
  if ((feesToCalc === null || feesToCalc === void 0 ? void 0 : feesToCalc.length) > 0) {
982
- var results = getCtesFeesResult(feesToCalc, cte);
1103
+ var results = getCtesFeesResult(feesToCalc, cte, allFeesForCalc);
983
1104
  /*const results = [];
984
1105
 
985
1106
  for (const item of feesToCalc) {
@@ -1052,9 +1173,26 @@ var getCteLaneFeesTotal = function (feesToCalc, cte) {
1052
1173
  };
1053
1174
 
1054
1175
  var getAuditTotalFromCte = function (cte) {
1055
- var laneFees = getLaneFeesToCalc(cte);
1056
- var total = getCteLaneFeesTotal(laneFees !== null && laneFees !== void 0 ? laneFees : [], cte);
1057
- 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
+ }
1058
1196
  return total !== null && total !== void 0 ? total : 0;
1059
1197
  };
1060
1198
 
@@ -7189,176 +7327,99 @@ var getCteDateRange = function (ctes) {
7189
7327
  return "".concat(formatDate(oldest), " - ").concat(formatDate(newest));
7190
7328
  };
7191
7329
 
7192
- var getFilteredFeesToAudit = function (_a) {
7193
- var _b;
7194
- var lane = _a.lane, ratecard = _a.ratecard, cte = _a.cte;
7195
- var filteredFees = (_b = lane === null || lane === void 0 ? void 0 : lane.RatecardLaneFee) === null || _b === void 0 ? void 0 : _b.filter(function (i) {
7196
- var _a, _b, _c, _d, _e;
7197
- var defaultConditionalFees = [
7198
- exports.FeeEnum.SUFRAMA,
7199
- exports.FeeEnum.TDA_FREIGHT,
7200
- exports.FeeEnum.TDA_MIN,
7201
- exports.FeeEnum.TDE_FREIGHT,
7202
- exports.FeeEnum.TDE_MIN,
7203
- ];
7204
- var conditionalFeeToVerify;
7205
- 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); });
7206
- if (defaultConditionalFees.includes((_b = i.fee) === null || _b === void 0 ? void 0 : _b.id) && !(conditionalFees === null || conditionalFees === void 0 ? void 0 : conditionalFees.length)) {
7207
- return false;
7208
- }
7209
- if (conditionalFees && (conditionalFees === null || conditionalFees === void 0 ? void 0 : conditionalFees.length) > 0) {
7210
- var _loop_1 = function (index) {
7211
- var conditionalFee = conditionalFees[index];
7212
- if (conditionalFee) {
7213
- conditionalFeeToVerify = conditionalFee;
7214
- }
7215
- else if ((_c = i.fee) === null || _c === void 0 ? void 0 : _c.parentId) {
7216
- 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; });
7217
- if (siblingFee_1) {
7218
- 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); });
7219
- if (conditionalFeeSibling) {
7220
- 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;
7221
7344
  }
7222
- }
7223
- }
7224
- if (conditionalFeeToVerify === null || conditionalFeeToVerify === void 0 ? void 0 : conditionalFeeToVerify.id) {
7225
- var result = verifyConditionalFee(conditionalFeeToVerify, cte);
7226
- if (result) {
7227
- return { value: true };
7228
- }
7229
- else if (index === conditionalFees.length - 1) {
7230
- return { value: false };
7231
- }
7232
- }
7233
- };
7234
- for (var index = 0; index < conditionalFees.length; index++) {
7235
- var state_1 = _loop_1(index);
7236
- if (typeof state_1 === "object")
7237
- return state_1.value;
7238
- }
7239
- }
7240
- if (i.minKm !== null && i.minKm !== undefined) {
7241
- if (cte === null || cte === void 0 ? void 0 : cte.distance) {
7242
- var distanceInRange = +cte.distance >= +i.minKm && (!i.maxKm || +cte.distance <= +i.maxKm);
7243
- var vehicleTypeMatches = !i.vehicleTypeId || cte.vehicleTypeId === i.vehicleTypeId;
7244
- if (distanceInRange && vehicleTypeMatches) {
7245
- return true;
7246
- }
7247
- }
7248
- }
7249
- else if (i.minWeight) {
7250
- if (cte === null || cte === void 0 ? void 0 : cte.taxedWeight) {
7251
- if (Math.ceil(+cte.taxedWeight) >= +i.minWeight) {
7252
- if (Math.ceil(+cte.taxedWeight) <= +i.maxWeight) {
7253
- return true;
7254
- }
7255
- else if (!i.maxWeight) {
7256
- return true;
7257
- }
7345
+ return false;
7346
+ });
7258
7347
  }
7259
7348
  }
7260
7349
  }
7261
- else {
7262
- return true;
7263
- }
7264
- return false;
7265
- });
7266
- // Remover fees duplicadas, mantendo apenas uma ocorrência de cada feeId
7267
- var seenFeeIds = new Set();
7268
- var uniqueFees = filteredFees === null || filteredFees === void 0 ? void 0 : filteredFees.filter(function (fee) {
7269
- if (fee.feeId && seenFeeIds.has(fee.feeId)) {
7270
- return false;
7271
- }
7272
- if (fee.feeId) {
7273
- seenFeeIds.add(fee.feeId);
7274
- }
7275
- return true;
7276
- });
7277
- return uniqueFees;
7350
+ }
7278
7351
  };
7279
7352
 
7280
- var getLaneFromRatecard = function (cte) {
7281
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
7353
+ var getLaneFeesToCalc = function (cte) {
7354
+ var _a, _b;
7282
7355
  var ratecard = getRatecardFromCte(cte);
7283
- var cityOriginId = cte.cityOriginId, cityDestination = cte.cityDestination;
7284
- var destinationUf = (_a = cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.state) === null || _a === void 0 ? void 0 : _a.uf;
7285
- // Encontrar a ratecardRegion da cidade de destino
7286
- 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;
7287
- 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) {
7288
- return undefined;
7289
- }
7290
- // Lógica diferenciada por modal
7291
- switch (ratecard.modalId) {
7292
- case exports.RatecardModalEnum.ROAD_DOMESTIC_FTL:
7293
- // FTL: Verificar rota por UF de destino
7294
- if (destinationUf) {
7295
- var directLane = ratecard.RatecardLane.find(function (i) {
7296
- return i.ufDestination === destinationUf && i.ratecardRegionId === destinationRatecardRegionId;
7297
- });
7298
- if (directLane) {
7299
- return directLane;
7300
- }
7301
- // Fallback: Verificar se existe lane única com fees de KM e cidade origem no ratecard_origin_cities
7302
- var uniqueLane = ratecard.RatecardLane[0];
7303
- // Verificar se a lane tem fees com intervalo de KM
7304
- 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; });
7305
- // Verificar se a cidade de origem está no ratecard_origin_cities
7306
- var hasOriginCity = (_f = ratecard.ratecard_origin_cities) === null || _f === void 0 ? void 0 : _f.some(function (originCity) { return originCity.cityId === cityOriginId; });
7307
- if (hasKmFees && hasOriginCity) {
7308
- // Mockar informações para exibição no frontend
7309
- return __assign(__assign({}, uniqueLane), { city: cte.cityOrigin, ufDestination: destinationUf, ratecardRegion: uniqueLane.ratecardRegion || { name: destinationUf } });
7310
- }
7311
- // Rota inversa FTL: Verificar se cidade de destino está em ratecard_origin_cities
7312
- var originUf_1 = (_h = (_g = cte.cityOrigin) === null || _g === void 0 ? void 0 : _g.state) === null || _h === void 0 ? void 0 : _h.uf;
7313
- if (originUf_1) {
7314
- // Verificar se existe lane com ufDestination = originUf
7315
- var reverseLane = ratecard.RatecardLane.find(function (i) {
7316
- return i.ufDestination === originUf_1;
7317
- });
7318
- if (reverseLane) {
7319
- // Verificar se a cidade de destino está no ratecard_origin_cities
7320
- 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); });
7321
- if (hasDestinationCity) {
7322
- 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
+ }
7323
7384
  }
7324
7385
  }
7325
- // Fallback inverso: Lane única com cidade de destino em ratecard_origin_cities
7326
- 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); });
7327
- if (hasKmFees && hasDestinationCityInOrigins) {
7328
- // Mockar informações para exibição no frontend (rota inversa)
7329
- 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
+ }
7330
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;
7331
7400
  }
7332
7401
  }
7333
- break;
7334
- case exports.RatecardModalEnum.ROAD_DOMESTIC_LTL:
7335
- default:
7336
- // LTL: Lógica original (rota direta + inversa)
7337
- if (cityOriginId && destinationUf) {
7338
- // Primeiro, buscar rota direta (Origem -> Destino)
7339
- var directLane = ratecard.RatecardLane.find(function (i) {
7340
- return i.cityOriginId === cityOriginId && i.ufDestination === destinationUf && i.ratecardRegionId === destinationRatecardRegionId;
7341
- });
7342
- if (directLane) {
7343
- return directLane;
7344
- }
7345
- // Se não encontrar rota direta, buscar rota inversa (Destino -> Origem)
7346
- // Para isso, precisamos encontrar a UF da origem e sua ratecardRegion
7347
- var originUf_2 = (_m = (_l = cte.cityOrigin) === null || _l === void 0 ? void 0 : _l.state) === null || _m === void 0 ? void 0 : _m.uf;
7348
- // Encontrar a ratecardRegion da cidade de origem
7349
- 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;
7350
- if (originUf_2) {
7351
- var reverseLane = ratecard.RatecardLane.find(function (i) {
7352
- return i.cityOriginId === (cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.id) && i.ufDestination === originUf_2 && i.ratecardRegionId === originRatecardRegionId_1;
7353
- });
7354
- if (reverseLane) {
7355
- 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
+ }
7356
7411
  }
7357
7412
  }
7358
7413
  }
7359
- 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
+ }
7360
7422
  }
7361
- return undefined;
7362
7423
  };
7363
7424
 
7364
7425
  var getNormalizedCityName = function (name) {
@@ -7561,6 +7622,7 @@ exports.calculateFee = calculateFee;
7561
7622
  exports.convertNumberToCurrency = convertNumberToCurrency;
7562
7623
  exports.filterSiblingFees = filterSiblingFees;
7563
7624
  exports.formatDateString = formatDateString;
7625
+ exports.getAllFeesForCalculation = getAllFeesForCalculation;
7564
7626
  exports.getAuditTotalFromCte = getAuditTotalFromCte;
7565
7627
  exports.getConfigurationFromDomain = getConfigurationFromDomain;
7566
7628
  exports.getContractFromFreight = getContractFromFreight;
package/build/index.js.gz CHANGED
Binary file