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.
package/build/index.js CHANGED
@@ -244,6 +244,11 @@ exports.RatecardConditionalFeeTypeEnum = void 0;
244
244
  RatecardConditionalFeeTypeEnum[RatecardConditionalFeeTypeEnum["STATE"] = 3] = "STATE";
245
245
  })(exports.RatecardConditionalFeeTypeEnum || (exports.RatecardConditionalFeeTypeEnum = {}));
246
246
 
247
+ exports.RatecardConfigKeyEnum = void 0;
248
+ (function (RatecardConfigKeyEnum) {
249
+ RatecardConfigKeyEnum["ICMS_APPORTIONMENT_ENABLED"] = "icms_apportionment_enabled";
250
+ })(exports.RatecardConfigKeyEnum || (exports.RatecardConfigKeyEnum = {}));
251
+
247
252
  exports.RatecardFeeConfigKeyEnum = void 0;
248
253
  (function (RatecardFeeConfigKeyEnum) {
249
254
  // Configurações de fração de peso
@@ -966,41 +971,6 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug, allFeesForCal
966
971
  }
967
972
  };
968
973
 
969
- /**
970
- * Calcula o valor do ICMS baseado no total das outras taxas
971
- *
972
- * @param totalFromAllFees - Array com os valores das outras taxas
973
- * @param icmsRate - Alíquota do ICMS (ex: 0.12 para 12%)
974
- * @returns Objeto com o valor calculado e detalhes do cálculo
975
- */
976
- var calculateIcms = function (totalFromAllFees, icmsRate) {
977
- // Filtra valores válidos e soma
978
- var totalValueFromAllFees = totalFromAllFees
979
- .filter(function (value) { return !isNaN(+value) && +value > 0; })
980
- .reduce(function (previous, current) { return +previous + +current; }, 0);
981
- // Verifica se há valor válido para cálculo
982
- if (!totalValueFromAllFees || isNaN(+totalValueFromAllFees)) {
983
- return {
984
- total: 0,
985
- totalValueFromAllFees: 0,
986
- valueToDivide: 0,
987
- firstTotal: 0,
988
- isValid: false
989
- };
990
- }
991
- // Cálculo do ICMS: (soma / (1 - alíquota)) * alíquota
992
- var valueToDivide = (1 - icmsRate).toFixed(2);
993
- var firstTotal = +totalValueFromAllFees / +valueToDivide;
994
- var total = +firstTotal * icmsRate;
995
- return {
996
- total: total,
997
- totalValueFromAllFees: totalValueFromAllFees,
998
- valueToDivide: valueToDivide,
999
- firstTotal: firstTotal,
1000
- isValid: true
1001
- };
1002
- };
1003
-
1004
974
  /**
1005
975
  * Calcula todas as fees do tipo TOTAL_PERCENTAGE de forma iterativa
1006
976
  * para resolver a interdependência entre elas (ex: TRT e GAC)
@@ -1095,6 +1065,105 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCa
1095
1065
  return results;
1096
1066
  };
1097
1067
 
1068
+ /**
1069
+ * Calcula o valor do ICMS baseado no total das outras taxas
1070
+ *
1071
+ * @param totalFromAllFees - Array com os valores das outras taxas
1072
+ * @param icmsRate - Alíquota do ICMS (ex: 0.12 para 12%)
1073
+ * @returns Objeto com o valor calculado e detalhes do cálculo
1074
+ */
1075
+ var calculateIcms = function (totalFromAllFees, icmsRate) {
1076
+ // Filtra valores válidos e soma
1077
+ var totalValueFromAllFees = totalFromAllFees
1078
+ .filter(function (value) { return !isNaN(+value) && +value > 0; })
1079
+ .reduce(function (previous, current) { return +previous + +current; }, 0);
1080
+ // Verifica se há valor válido para cálculo
1081
+ if (!totalValueFromAllFees || isNaN(+totalValueFromAllFees)) {
1082
+ return {
1083
+ total: 0,
1084
+ totalValueFromAllFees: 0,
1085
+ valueToDivide: 0,
1086
+ firstTotal: 0,
1087
+ isValid: false
1088
+ };
1089
+ }
1090
+ // Cálculo do ICMS: (soma / (1 - alíquota)) * alíquota
1091
+ var valueToDivide = (1 - icmsRate).toFixed(2);
1092
+ var firstTotal = +totalValueFromAllFees / +valueToDivide;
1093
+ var total = +firstTotal * icmsRate;
1094
+ return {
1095
+ total: total,
1096
+ totalValueFromAllFees: totalValueFromAllFees,
1097
+ valueToDivide: valueToDivide,
1098
+ firstTotal: firstTotal,
1099
+ isValid: true
1100
+ };
1101
+ };
1102
+
1103
+ /**
1104
+ * Calcula o total de fees de forma consistente, usando cálculo iterativo para TOTAL_PERCENTAGE
1105
+ * @param fees - Array de fees a calcular
1106
+ * @param cte - CTE para cálculo
1107
+ * @param allFeesForCalc - Todas as fees disponíveis para cálculo
1108
+ * @returns Total calculado
1109
+ */
1110
+ var calculateFeesTotal = function (fees, cte, allFeesForCalc) {
1111
+ if (!fees || fees.length === 0)
1112
+ return 0;
1113
+ var cache = {};
1114
+ // PASSADA 1: Calcular fees normais (não TOTAL_PERCENTAGE, não ICMS)
1115
+ fees.forEach(function (fee, idx) {
1116
+ var _a, _b;
1117
+ if (((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) !== exports.FeeCalculationTypeEnum.TOTAL_PERCENTAGE &&
1118
+ ((_b = fee.fee) === null || _b === void 0 ? void 0 : _b.id) !== exports.FeeEnum.ICMS) {
1119
+ cache[idx] = calculateFee(fee, cte, fees, true, allFeesForCalc);
1120
+ }
1121
+ });
1122
+ // PASSADA 2: Calcular TOTAL_PERCENTAGE de forma iterativa
1123
+ var percentageFeesCache = calculateTotalPercentageFees(fees, cte, allFeesForCalc);
1124
+ fees.forEach(function (fee, idx) {
1125
+ var _a, _b;
1126
+ if (((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) === exports.FeeCalculationTypeEnum.TOTAL_PERCENTAGE &&
1127
+ ((_b = fee.fee) === null || _b === void 0 ? void 0 : _b.id)) {
1128
+ var cachedResult = percentageFeesCache.get(fee.fee.id);
1129
+ if (cachedResult) {
1130
+ cache[idx] = {
1131
+ totalToCalc: Number((cachedResult.totalToCalc || 0).toFixed(2)),
1132
+ };
1133
+ }
1134
+ }
1135
+ });
1136
+ // PASSADA 3: Calcular ICMS
1137
+ fees.forEach(function (fee, idx) {
1138
+ var _a;
1139
+ if (((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.id) === exports.FeeEnum.ICMS) {
1140
+ var icmsRate = (cte.icmsIncidence !== undefined && cte.icmsIncidence !== null && !isNaN(+cte.icmsIncidence))
1141
+ ? +(cte.icmsIncidence) / 100
1142
+ : fee.value || 0;
1143
+ var valuesWithoutIcms = Object.entries(cache)
1144
+ .filter(function (_a) {
1145
+ var _b;
1146
+ var key = _a[0];
1147
+ var feeIdx = Number(key);
1148
+ var f = fees[feeIdx];
1149
+ return ((_b = f.fee) === null || _b === void 0 ? void 0 : _b.id) !== exports.FeeEnum.ICMS;
1150
+ })
1151
+ .map(function (_a) {
1152
+ var calcFee = _a[1];
1153
+ return (calcFee === null || calcFee === void 0 ? void 0 : calcFee.totalToCalc) || 0;
1154
+ })
1155
+ .filter(function (val) { return val > 0; });
1156
+ var icmsCalculation = calculateIcms(valuesWithoutIcms, icmsRate);
1157
+ if (icmsCalculation.isValid) {
1158
+ cache[idx] = {
1159
+ totalToCalc: Number(icmsCalculation.total.toFixed(2)),
1160
+ };
1161
+ }
1162
+ }
1163
+ });
1164
+ return Object.values(cache).reduce(function (sum, calcFee) { return sum + ((calcFee === null || calcFee === void 0 ? void 0 : calcFee.totalToCalc) || 0); }, 0);
1165
+ };
1166
+
1098
1167
  var verifyConditionalFee = function (conditionalFeeToVerify, cte) {
1099
1168
  var _a, _b;
1100
1169
  if (conditionalFeeToVerify === null || conditionalFeeToVerify === void 0 ? void 0 : conditionalFeeToVerify.id) {
@@ -8142,6 +8211,7 @@ exports.addTariffMapping = addTariffMapping;
8142
8211
  exports.applyRedeliveryMultiplier = applyRedeliveryMultiplier;
8143
8212
  exports.buildDynamicMapping = buildDynamicMapping;
8144
8213
  exports.calculateFee = calculateFee;
8214
+ exports.calculateFeesTotal = calculateFeesTotal;
8145
8215
  exports.calculateIcms = calculateIcms;
8146
8216
  exports.calculateTotalPercentageFees = calculateTotalPercentageFees;
8147
8217
  exports.convertNumberToCurrency = convertNumberToCurrency;
package/build/index.js.gz CHANGED
Binary file