b2m-utils 0.0.297 → 0.0.298

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
@@ -966,41 +966,6 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug, allFeesForCal
966
966
  }
967
967
  };
968
968
 
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
969
  /**
1005
970
  * Calcula todas as fees do tipo TOTAL_PERCENTAGE de forma iterativa
1006
971
  * para resolver a interdependência entre elas (ex: TRT e GAC)
@@ -1095,6 +1060,105 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCa
1095
1060
  return results;
1096
1061
  };
1097
1062
 
1063
+ /**
1064
+ * Calcula o valor do ICMS baseado no total das outras taxas
1065
+ *
1066
+ * @param totalFromAllFees - Array com os valores das outras taxas
1067
+ * @param icmsRate - Alíquota do ICMS (ex: 0.12 para 12%)
1068
+ * @returns Objeto com o valor calculado e detalhes do cálculo
1069
+ */
1070
+ var calculateIcms = function (totalFromAllFees, icmsRate) {
1071
+ // Filtra valores válidos e soma
1072
+ var totalValueFromAllFees = totalFromAllFees
1073
+ .filter(function (value) { return !isNaN(+value) && +value > 0; })
1074
+ .reduce(function (previous, current) { return +previous + +current; }, 0);
1075
+ // Verifica se há valor válido para cálculo
1076
+ if (!totalValueFromAllFees || isNaN(+totalValueFromAllFees)) {
1077
+ return {
1078
+ total: 0,
1079
+ totalValueFromAllFees: 0,
1080
+ valueToDivide: 0,
1081
+ firstTotal: 0,
1082
+ isValid: false
1083
+ };
1084
+ }
1085
+ // Cálculo do ICMS: (soma / (1 - alíquota)) * alíquota
1086
+ var valueToDivide = (1 - icmsRate).toFixed(2);
1087
+ var firstTotal = +totalValueFromAllFees / +valueToDivide;
1088
+ var total = +firstTotal * icmsRate;
1089
+ return {
1090
+ total: total,
1091
+ totalValueFromAllFees: totalValueFromAllFees,
1092
+ valueToDivide: valueToDivide,
1093
+ firstTotal: firstTotal,
1094
+ isValid: true
1095
+ };
1096
+ };
1097
+
1098
+ /**
1099
+ * Calcula o total de fees de forma consistente, usando cálculo iterativo para TOTAL_PERCENTAGE
1100
+ * @param fees - Array de fees a calcular
1101
+ * @param cte - CTE para cálculo
1102
+ * @param allFeesForCalc - Todas as fees disponíveis para cálculo
1103
+ * @returns Total calculado
1104
+ */
1105
+ var calculateFeesTotal = function (fees, cte, allFeesForCalc) {
1106
+ if (!fees || fees.length === 0)
1107
+ return 0;
1108
+ var cache = {};
1109
+ // PASSADA 1: Calcular fees normais (não TOTAL_PERCENTAGE, não ICMS)
1110
+ fees.forEach(function (fee, idx) {
1111
+ var _a, _b;
1112
+ if (((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) !== exports.FeeCalculationTypeEnum.TOTAL_PERCENTAGE &&
1113
+ ((_b = fee.fee) === null || _b === void 0 ? void 0 : _b.id) !== exports.FeeEnum.ICMS) {
1114
+ cache[idx] = calculateFee(fee, cte, fees, true, allFeesForCalc);
1115
+ }
1116
+ });
1117
+ // PASSADA 2: Calcular TOTAL_PERCENTAGE de forma iterativa
1118
+ var percentageFeesCache = calculateTotalPercentageFees(fees, cte, allFeesForCalc);
1119
+ fees.forEach(function (fee, idx) {
1120
+ var _a, _b;
1121
+ if (((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) === exports.FeeCalculationTypeEnum.TOTAL_PERCENTAGE &&
1122
+ ((_b = fee.fee) === null || _b === void 0 ? void 0 : _b.id)) {
1123
+ var cachedResult = percentageFeesCache.get(fee.fee.id);
1124
+ if (cachedResult) {
1125
+ cache[idx] = {
1126
+ totalToCalc: Number((cachedResult.totalToCalc || 0).toFixed(2)),
1127
+ };
1128
+ }
1129
+ }
1130
+ });
1131
+ // PASSADA 3: Calcular ICMS
1132
+ fees.forEach(function (fee, idx) {
1133
+ var _a;
1134
+ if (((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.id) === exports.FeeEnum.ICMS) {
1135
+ var icmsRate = (cte.icmsIncidence !== undefined && cte.icmsIncidence !== null && !isNaN(+cte.icmsIncidence))
1136
+ ? +(cte.icmsIncidence) / 100
1137
+ : fee.value || 0;
1138
+ var valuesWithoutIcms = Object.entries(cache)
1139
+ .filter(function (_a) {
1140
+ var _b;
1141
+ var key = _a[0];
1142
+ var feeIdx = Number(key);
1143
+ var f = fees[feeIdx];
1144
+ return ((_b = f.fee) === null || _b === void 0 ? void 0 : _b.id) !== exports.FeeEnum.ICMS;
1145
+ })
1146
+ .map(function (_a) {
1147
+ var calcFee = _a[1];
1148
+ return (calcFee === null || calcFee === void 0 ? void 0 : calcFee.totalToCalc) || 0;
1149
+ })
1150
+ .filter(function (val) { return val > 0; });
1151
+ var icmsCalculation = calculateIcms(valuesWithoutIcms, icmsRate);
1152
+ if (icmsCalculation.isValid) {
1153
+ cache[idx] = {
1154
+ totalToCalc: Number(icmsCalculation.total.toFixed(2)),
1155
+ };
1156
+ }
1157
+ }
1158
+ });
1159
+ return Object.values(cache).reduce(function (sum, calcFee) { return sum + ((calcFee === null || calcFee === void 0 ? void 0 : calcFee.totalToCalc) || 0); }, 0);
1160
+ };
1161
+
1098
1162
  var verifyConditionalFee = function (conditionalFeeToVerify, cte) {
1099
1163
  var _a, _b;
1100
1164
  if (conditionalFeeToVerify === null || conditionalFeeToVerify === void 0 ? void 0 : conditionalFeeToVerify.id) {
@@ -8142,6 +8206,7 @@ exports.addTariffMapping = addTariffMapping;
8142
8206
  exports.applyRedeliveryMultiplier = applyRedeliveryMultiplier;
8143
8207
  exports.buildDynamicMapping = buildDynamicMapping;
8144
8208
  exports.calculateFee = calculateFee;
8209
+ exports.calculateFeesTotal = calculateFeesTotal;
8145
8210
  exports.calculateIcms = calculateIcms;
8146
8211
  exports.calculateTotalPercentageFees = calculateTotalPercentageFees;
8147
8212
  exports.convertNumberToCurrency = convertNumberToCurrency;
package/build/index.js.gz CHANGED
Binary file