b2m-utils 0.0.297 → 0.0.299

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.
@@ -0,0 +1,3 @@
1
+ export declare enum RatecardConfigKeyEnum {
2
+ ICMS_APPORTIONMENT_ENABLED = "icms_apportionment_enabled"
3
+ }
@@ -20,6 +20,7 @@ export * from './ModalEnum';
20
20
  export * from './NotificationTypeEnum';
21
21
  export * from './PermissionEnum';
22
22
  export * from './RatecardConditionalFeeTypeEnum';
23
+ export * from './RatecardConfigKeyEnum';
23
24
  export * from './RatecardFeeConfigKeyEnum';
24
25
  export * from './RatecardFeeConfigTypeEnum';
25
26
  export * from './RatecardModalEnum';
@@ -0,0 +1,9 @@
1
+ import { Cte, RatecardLaneFee } from "../../types";
2
+ /**
3
+ * Calcula o total de fees de forma consistente, usando cálculo iterativo para TOTAL_PERCENTAGE
4
+ * @param fees - Array de fees a calcular
5
+ * @param cte - CTE para cálculo
6
+ * @param allFeesForCalc - Todas as fees disponíveis para cálculo
7
+ * @returns Total calculado
8
+ */
9
+ export declare const calculateFeesTotal: (fees: RatecardLaneFee[], cte: Cte, allFeesForCalc?: RatecardLaneFee[]) => number;
@@ -1,5 +1,6 @@
1
1
  export * from './applyRedeliveryMultiplier';
2
2
  export * from './calculateFee';
3
+ export * from './calculateFeesTotal';
3
4
  export * from './calculateIcms';
4
5
  export * from './calculateTotalPercentageFees';
5
6
  export * from './convertNumberToCurrency';
@@ -240,6 +240,11 @@ var RatecardConditionalFeeTypeEnum;
240
240
  RatecardConditionalFeeTypeEnum[RatecardConditionalFeeTypeEnum["STATE"] = 3] = "STATE";
241
241
  })(RatecardConditionalFeeTypeEnum || (RatecardConditionalFeeTypeEnum = {}));
242
242
 
243
+ var RatecardConfigKeyEnum;
244
+ (function (RatecardConfigKeyEnum) {
245
+ RatecardConfigKeyEnum["ICMS_APPORTIONMENT_ENABLED"] = "icms_apportionment_enabled";
246
+ })(RatecardConfigKeyEnum || (RatecardConfigKeyEnum = {}));
247
+
243
248
  var RatecardFeeConfigKeyEnum;
244
249
  (function (RatecardFeeConfigKeyEnum) {
245
250
  // Configurações de fração de peso
@@ -962,41 +967,6 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug, allFeesForCal
962
967
  }
963
968
  };
964
969
 
965
- /**
966
- * Calcula o valor do ICMS baseado no total das outras taxas
967
- *
968
- * @param totalFromAllFees - Array com os valores das outras taxas
969
- * @param icmsRate - Alíquota do ICMS (ex: 0.12 para 12%)
970
- * @returns Objeto com o valor calculado e detalhes do cálculo
971
- */
972
- var calculateIcms = function (totalFromAllFees, icmsRate) {
973
- // Filtra valores válidos e soma
974
- var totalValueFromAllFees = totalFromAllFees
975
- .filter(function (value) { return !isNaN(+value) && +value > 0; })
976
- .reduce(function (previous, current) { return +previous + +current; }, 0);
977
- // Verifica se há valor válido para cálculo
978
- if (!totalValueFromAllFees || isNaN(+totalValueFromAllFees)) {
979
- return {
980
- total: 0,
981
- totalValueFromAllFees: 0,
982
- valueToDivide: 0,
983
- firstTotal: 0,
984
- isValid: false
985
- };
986
- }
987
- // Cálculo do ICMS: (soma / (1 - alíquota)) * alíquota
988
- var valueToDivide = (1 - icmsRate).toFixed(2);
989
- var firstTotal = +totalValueFromAllFees / +valueToDivide;
990
- var total = +firstTotal * icmsRate;
991
- return {
992
- total: total,
993
- totalValueFromAllFees: totalValueFromAllFees,
994
- valueToDivide: valueToDivide,
995
- firstTotal: firstTotal,
996
- isValid: true
997
- };
998
- };
999
-
1000
970
  /**
1001
971
  * Calcula todas as fees do tipo TOTAL_PERCENTAGE de forma iterativa
1002
972
  * para resolver a interdependência entre elas (ex: TRT e GAC)
@@ -1091,6 +1061,105 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCa
1091
1061
  return results;
1092
1062
  };
1093
1063
 
1064
+ /**
1065
+ * Calcula o valor do ICMS baseado no total das outras taxas
1066
+ *
1067
+ * @param totalFromAllFees - Array com os valores das outras taxas
1068
+ * @param icmsRate - Alíquota do ICMS (ex: 0.12 para 12%)
1069
+ * @returns Objeto com o valor calculado e detalhes do cálculo
1070
+ */
1071
+ var calculateIcms = function (totalFromAllFees, icmsRate) {
1072
+ // Filtra valores válidos e soma
1073
+ var totalValueFromAllFees = totalFromAllFees
1074
+ .filter(function (value) { return !isNaN(+value) && +value > 0; })
1075
+ .reduce(function (previous, current) { return +previous + +current; }, 0);
1076
+ // Verifica se há valor válido para cálculo
1077
+ if (!totalValueFromAllFees || isNaN(+totalValueFromAllFees)) {
1078
+ return {
1079
+ total: 0,
1080
+ totalValueFromAllFees: 0,
1081
+ valueToDivide: 0,
1082
+ firstTotal: 0,
1083
+ isValid: false
1084
+ };
1085
+ }
1086
+ // Cálculo do ICMS: (soma / (1 - alíquota)) * alíquota
1087
+ var valueToDivide = (1 - icmsRate).toFixed(2);
1088
+ var firstTotal = +totalValueFromAllFees / +valueToDivide;
1089
+ var total = +firstTotal * icmsRate;
1090
+ return {
1091
+ total: total,
1092
+ totalValueFromAllFees: totalValueFromAllFees,
1093
+ valueToDivide: valueToDivide,
1094
+ firstTotal: firstTotal,
1095
+ isValid: true
1096
+ };
1097
+ };
1098
+
1099
+ /**
1100
+ * Calcula o total de fees de forma consistente, usando cálculo iterativo para TOTAL_PERCENTAGE
1101
+ * @param fees - Array de fees a calcular
1102
+ * @param cte - CTE para cálculo
1103
+ * @param allFeesForCalc - Todas as fees disponíveis para cálculo
1104
+ * @returns Total calculado
1105
+ */
1106
+ var calculateFeesTotal = function (fees, cte, allFeesForCalc) {
1107
+ if (!fees || fees.length === 0)
1108
+ return 0;
1109
+ var cache = {};
1110
+ // PASSADA 1: Calcular fees normais (não TOTAL_PERCENTAGE, não ICMS)
1111
+ fees.forEach(function (fee, idx) {
1112
+ var _a, _b;
1113
+ if (((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) !== FeeCalculationTypeEnum.TOTAL_PERCENTAGE &&
1114
+ ((_b = fee.fee) === null || _b === void 0 ? void 0 : _b.id) !== FeeEnum.ICMS) {
1115
+ cache[idx] = calculateFee(fee, cte, fees, true, allFeesForCalc);
1116
+ }
1117
+ });
1118
+ // PASSADA 2: Calcular TOTAL_PERCENTAGE de forma iterativa
1119
+ var percentageFeesCache = calculateTotalPercentageFees(fees, cte, allFeesForCalc);
1120
+ fees.forEach(function (fee, idx) {
1121
+ var _a, _b;
1122
+ if (((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) === FeeCalculationTypeEnum.TOTAL_PERCENTAGE &&
1123
+ ((_b = fee.fee) === null || _b === void 0 ? void 0 : _b.id)) {
1124
+ var cachedResult = percentageFeesCache.get(fee.fee.id);
1125
+ if (cachedResult) {
1126
+ cache[idx] = {
1127
+ totalToCalc: Number((cachedResult.totalToCalc || 0).toFixed(2)),
1128
+ };
1129
+ }
1130
+ }
1131
+ });
1132
+ // PASSADA 3: Calcular ICMS
1133
+ fees.forEach(function (fee, idx) {
1134
+ var _a;
1135
+ if (((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.id) === FeeEnum.ICMS) {
1136
+ var icmsRate = (cte.icmsIncidence !== undefined && cte.icmsIncidence !== null && !isNaN(+cte.icmsIncidence))
1137
+ ? +(cte.icmsIncidence) / 100
1138
+ : fee.value || 0;
1139
+ var valuesWithoutIcms = Object.entries(cache)
1140
+ .filter(function (_a) {
1141
+ var _b;
1142
+ var key = _a[0];
1143
+ var feeIdx = Number(key);
1144
+ var f = fees[feeIdx];
1145
+ return ((_b = f.fee) === null || _b === void 0 ? void 0 : _b.id) !== FeeEnum.ICMS;
1146
+ })
1147
+ .map(function (_a) {
1148
+ var calcFee = _a[1];
1149
+ return (calcFee === null || calcFee === void 0 ? void 0 : calcFee.totalToCalc) || 0;
1150
+ })
1151
+ .filter(function (val) { return val > 0; });
1152
+ var icmsCalculation = calculateIcms(valuesWithoutIcms, icmsRate);
1153
+ if (icmsCalculation.isValid) {
1154
+ cache[idx] = {
1155
+ totalToCalc: Number(icmsCalculation.total.toFixed(2)),
1156
+ };
1157
+ }
1158
+ }
1159
+ });
1160
+ return Object.values(cache).reduce(function (sum, calcFee) { return sum + ((calcFee === null || calcFee === void 0 ? void 0 : calcFee.totalToCalc) || 0); }, 0);
1161
+ };
1162
+
1094
1163
  var verifyConditionalFee = function (conditionalFeeToVerify, cte) {
1095
1164
  var _a, _b;
1096
1165
  if (conditionalFeeToVerify === null || conditionalFeeToVerify === void 0 ? void 0 : conditionalFeeToVerify.id) {
@@ -8131,5 +8200,5 @@ var isTariffMatch = function (chargedName, auditedTariffName) {
8131
8200
  });
8132
8201
  };
8133
8202
 
8134
- export { ApplicationColumnNameEnum, ApplicationEnum, CarrierDocumentConfigTypeKeyEnum, CountryEnum, CteStatusEnum, CteVehicleTypeEnum, CurrencyEnum, DocumentTypeEnum, DomainConfigurationEnum, DomainTypeEnum, EXPORT_CTE_COLUMN_LABELS, EXPORT_CTE_TYPE_LABELS, ExportCteColumn, ExportCteTypeEnum, FeeCalculationTypeEnum, FeeCategoryEnum, FeeEnum, FreightRegionEnum, ImapHostsEnum, InputTypeEnum, ModalEnum, NotificationTypeEnum, PermissionEnum, RatecardConditionalFeeTypeEnum, RatecardFeeConfigKeyEnum, RatecardFeeConfigTypeEnum, RatecardModalEnum, SlaRegionEnum, SpotStatusEnum, TARIFF_MAPPING, TrackProcessProviderTypeEnum, addTariffMapping, applyRedeliveryMultiplier, buildDynamicMapping, calculateFee, calculateIcms, calculateTotalPercentageFees, convertNumberToCurrency, filterSiblingFees, findAuditedMatch, formatDateString, getAllFeesForCalculation, getAuditTotalFromCte, getConfigurationFromDomain, getContractFromFreight, getContractRouteFromFreight, getCookies, getCteDateRange, getCteLane, getCteLaneFeesTotal, getCtesFeesResult, getDataFromToken, getFilteredFeesToAudit, getFormattedFreightPlaceName, getLaneFeesToCalc, getLaneFromRatecard, getNormalizedCityName, getRatecardFromCte, getRouteDeliveryTimeFromFreight, getRouteOnTimeFromFreight, getTariffVariations, isTariffMatch, matchAllTariffs, normalizeString, parseAliases, setFormattedDatesInObjects, verifyConditionalFee, verifyDefaultFees };
8203
+ export { ApplicationColumnNameEnum, ApplicationEnum, CarrierDocumentConfigTypeKeyEnum, CountryEnum, CteStatusEnum, CteVehicleTypeEnum, CurrencyEnum, DocumentTypeEnum, DomainConfigurationEnum, DomainTypeEnum, EXPORT_CTE_COLUMN_LABELS, EXPORT_CTE_TYPE_LABELS, ExportCteColumn, ExportCteTypeEnum, FeeCalculationTypeEnum, FeeCategoryEnum, FeeEnum, FreightRegionEnum, ImapHostsEnum, InputTypeEnum, ModalEnum, NotificationTypeEnum, PermissionEnum, RatecardConditionalFeeTypeEnum, RatecardConfigKeyEnum, RatecardFeeConfigKeyEnum, RatecardFeeConfigTypeEnum, RatecardModalEnum, SlaRegionEnum, SpotStatusEnum, TARIFF_MAPPING, TrackProcessProviderTypeEnum, addTariffMapping, applyRedeliveryMultiplier, buildDynamicMapping, calculateFee, calculateFeesTotal, calculateIcms, calculateTotalPercentageFees, convertNumberToCurrency, filterSiblingFees, findAuditedMatch, formatDateString, getAllFeesForCalculation, getAuditTotalFromCte, getConfigurationFromDomain, getContractFromFreight, getContractRouteFromFreight, getCookies, getCteDateRange, getCteLane, getCteLaneFeesTotal, getCtesFeesResult, getDataFromToken, getFilteredFeesToAudit, getFormattedFreightPlaceName, getLaneFeesToCalc, getLaneFromRatecard, getNormalizedCityName, getRatecardFromCte, getRouteDeliveryTimeFromFreight, getRouteOnTimeFromFreight, getTariffVariations, isTariffMatch, matchAllTariffs, normalizeString, parseAliases, setFormattedDatesInObjects, verifyConditionalFee, verifyDefaultFees };
8135
8204
  //# sourceMappingURL=index.esm.js.map
Binary file