b2m-utils 0.0.293 → 0.0.295

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
@@ -1004,8 +1004,9 @@ var calculateIcms = function (totalFromAllFees, icmsRate) {
1004
1004
  /**
1005
1005
  * Calcula todas as fees do tipo TOTAL_PERCENTAGE de forma iterativa
1006
1006
  * para resolver a interdependência entre elas (ex: TRT e GAC)
1007
+ * @param preCalculatedValues - Opcional: Map de feeId -> valor já calculado (para valores alterados manualmente)
1007
1008
  */
1008
- var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc) {
1009
+ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCalculatedValues) {
1009
1010
  var results = new Map();
1010
1011
  // Separar fees em dois grupos
1011
1012
  // IMPORTANTE: ICMS não deve ser calculado aqui, ele tem tratamento especial no calculateFee
@@ -1023,16 +1024,22 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc) {
1023
1024
  if (percentageFees.length === 0) {
1024
1025
  return results;
1025
1026
  }
1026
- console.log("\n\uD83D\uDD04 Calculando ".concat(percentageFees.length, " fees TOTAL_PERCENTAGE de forma iterativa"));
1027
- console.log("\uD83D\uDCCB Fees: ".concat(percentageFees.map(function (f) { var _a; return "".concat((_a = f.fee) === null || _a === void 0 ? void 0 : _a.name, " (").concat(f.value, "%)"); }).join(', ')));
1028
- // Calcular fees não-percentuais (base fixa)
1029
- console.log("\uD83D\uDCCB Fees n\u00E3o-percentuais: ".concat(nonPercentageFees.map(function (f) { var _a; return "".concat((_a = f.fee) === null || _a === void 0 ? void 0 : _a.name); }).join(', ')));
1030
- var nonPercentageResults = filterSiblingFees(nonPercentageFees.map(function (it) {
1031
- return calculateFee(it, cte, allFees, false, allFeesForCalc);
1032
- }));
1033
- var baseNonPercentage = nonPercentageResults.reduce(function (sum, r) { return sum + (r.totalToCalc || 0); }, 0);
1034
- console.log("\uD83D\uDCCA Base n\u00E3o-percentual: R$ ".concat(baseNonPercentage.toFixed(2)));
1035
- console.log("\uD83D\uDCCA Detalhamento: ".concat(nonPercentageResults.map(function (r) { var _a, _b; return "".concat((_a = r.fee) === null || _a === void 0 ? void 0 : _a.name, "=").concat((_b = r.totalToCalc) === null || _b === void 0 ? void 0 : _b.toFixed(2)); }).join(', ')));
1027
+ var baseNonPercentage = 0;
1028
+ // Se temos valores pré-calculados, usar eles ao invés de recalcular
1029
+ if (preCalculatedValues) {
1030
+ baseNonPercentage = nonPercentageFees.reduce(function (sum, fee) {
1031
+ var _a;
1032
+ var value = ((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.id) ? preCalculatedValues.get(fee.fee.id) : undefined;
1033
+ return sum + (value !== undefined ? value : 0);
1034
+ }, 0);
1035
+ }
1036
+ else {
1037
+ // Calcular normalmente
1038
+ var nonPercentageResults = filterSiblingFees(nonPercentageFees.map(function (it) {
1039
+ return calculateFee(it, cte, allFees, false, allFeesForCalc);
1040
+ }));
1041
+ baseNonPercentage = nonPercentageResults.reduce(function (sum, r) { return sum + (r.totalToCalc || 0); }, 0);
1042
+ }
1036
1043
  // Calcular fees percentuais iterativamente até convergir
1037
1044
  var maxIterations = 10;
1038
1045
  var tolerance = 0.01; // Tolerância de 1 centavo
@@ -1040,10 +1047,8 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc) {
1040
1047
  var previousValues = new Map();
1041
1048
  percentageFees.forEach(function (pf) { return previousValues.set(pf.fee.id, 0); });
1042
1049
  var _loop_1 = function (iteration) {
1043
- console.log("\n--- Itera\u00E7\u00E3o ".concat(iteration + 1, " ---"));
1044
1050
  // Calcular cada fee percentual com base nos valores atuais das outras
1045
1051
  percentageFees.forEach(function (pf) {
1046
- var _a;
1047
1052
  // Calcular base: fees não-percentuais + outras fees percentuais (exceto ela mesma)
1048
1053
  var basePercentage = Array.from(previousValues.entries())
1049
1054
  .filter(function (_a) {
@@ -1057,7 +1062,6 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc) {
1057
1062
  var baseValue = baseNonPercentage + basePercentage;
1058
1063
  var percentValue = (+pf.value / 100);
1059
1064
  var calculatedValue = baseValue * percentValue;
1060
- console.log(" ".concat((_a = pf.fee) === null || _a === void 0 ? void 0 : _a.name, ": base=").concat(baseValue.toFixed(2), " (").concat(baseNonPercentage.toFixed(2), " + ").concat(basePercentage.toFixed(2), ") \u00D7 ").concat(pf.value, "% = ").concat(calculatedValue.toFixed(2)));
1061
1065
  previousValues.set(pf.fee.id, calculatedValue);
1062
1066
  // Armazenar resultado
1063
1067
  results.set(pf.fee.id, __assign(__assign({}, pf), { totalFee: calculatedValue, totalToCalc: calculatedValue, resultFee: "".concat(convertNumberToCurrency(baseValue, 'pt-br'), " x ").concat(+pf.value, "% = ").concat(convertNumberToCurrency(calculatedValue, 'pt-br')) }));
@@ -1080,7 +1084,6 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc) {
1080
1084
  hasConverged = false;
1081
1085
  }
1082
1086
  if (hasConverged) {
1083
- console.log("\u2705 Convergiu na itera\u00E7\u00E3o ".concat(iteration + 1, "!"));
1084
1087
  return "break";
1085
1088
  }
1086
1089
  };
@@ -1089,11 +1092,6 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc) {
1089
1092
  if (state_1 === "break")
1090
1093
  break;
1091
1094
  }
1092
- console.log("\n\uD83D\uDCC8 Resultado final das fees percentuais:");
1093
- results.forEach(function (result, feeId) {
1094
- var _a;
1095
- console.log(" ".concat((_a = result.fee) === null || _a === void 0 ? void 0 : _a.name, " (").concat(feeId, "): R$ ").concat((result.totalFee || 0).toFixed(2)));
1096
- });
1097
1095
  return results;
1098
1096
  };
1099
1097
 
@@ -1380,7 +1378,7 @@ var getLaneFromRatecard = function (cte) {
1380
1378
  };
1381
1379
 
1382
1380
  var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1383
- var _a, _b, _c;
1381
+ var _a, _b, _c, _d;
1384
1382
  var results = [];
1385
1383
  // Separar fees por tipo: normais, TOTAL_PERCENTAGE e ICMS
1386
1384
  var icmsFee = feesToCalc.find(function (f) { var _a; return ((_a = f.fee) === null || _a === void 0 ? void 0 : _a.id) === exports.FeeEnum.ICMS || f.feeId === exports.FeeEnum.ICMS; });
@@ -1407,8 +1405,8 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1407
1405
  });
1408
1406
  if (siblingFees && (siblingFees === null || siblingFees === void 0 ? void 0 : siblingFees.length) > 0) {
1409
1407
  var siblingFeesResults = [];
1410
- for (var _e = 0, siblingFees_1 = siblingFees; _e < siblingFees_1.length; _e++) {
1411
- var item_1 = siblingFees_1[_e];
1408
+ for (var _f = 0, siblingFees_1 = siblingFees; _f < siblingFees_1.length; _f++) {
1409
+ var item_1 = siblingFees_1[_f];
1412
1410
  var result = calculateFee(item_1, cte, feesToCalc, false, allFeesForCalc);
1413
1411
  siblingFeesResults.push(result);
1414
1412
  }
@@ -1440,22 +1438,26 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1440
1438
  var item = normalFees_1[_i];
1441
1439
  _loop_1(item);
1442
1440
  }
1443
- // PASSADA 2: Calcular fees TOTAL_PERCENTAGE usando a soma das fees normais já calculadas
1444
- for (var _d = 0, totalPercentageFees_1 = totalPercentageFees; _d < totalPercentageFees_1.length; _d++) {
1445
- var item = totalPercentageFees_1[_d];
1446
- // Calcular soma das fees normais (excluindo TOTAL_PERCENTAGE e ICMS)
1447
- var totalWithoutPercentageFees = results
1448
- .filter(function (r) { return r.total > 0; })
1449
- .reduce(function (sum, r) { return sum + r.total; }, 0);
1450
- var percentage = item.value || 0;
1451
- var calculatedValue = (totalWithoutPercentageFees * percentage) / 100;
1452
- if (!isNaN(+calculatedValue) && calculatedValue > 0) {
1453
- results.push({
1454
- total: +(calculatedValue).toFixed(2),
1455
- resultFee: "".concat(convertNumberToCurrency(totalWithoutPercentageFees, 'pt-br'), " x ").concat(percentage, "% = ").concat(convertNumberToCurrency(calculatedValue, 'pt-br')),
1456
- feeValue: percentage,
1457
- feeId: item.feeId,
1458
- });
1441
+ // PASSADA 2: Calcular fees TOTAL_PERCENTAGE de forma iterativa
1442
+ if (totalPercentageFees.length > 0) {
1443
+ console.log("\n\uD83D\uDD04 [getCtesFeesResult] Calculando ".concat(totalPercentageFees.length, " fees TOTAL_PERCENTAGE"));
1444
+ console.log("\uD83D\uDCCB Fees: ".concat(totalPercentageFees.map(function (f) { var _a; return "".concat((_a = f.fee) === null || _a === void 0 ? void 0 : _a.name, " (").concat(f.value, "%)"); }).join(', ')));
1445
+ var percentageFeesCache = calculateTotalPercentageFees(feesToCalc, cte, allFeesForCalc);
1446
+ console.log("\u2705 Cache gerado com ".concat(percentageFeesCache.size, " fees"));
1447
+ for (var _e = 0, totalPercentageFees_1 = totalPercentageFees; _e < totalPercentageFees_1.length; _e++) {
1448
+ var item = totalPercentageFees_1[_e];
1449
+ if ((_d = item.fee) === null || _d === void 0 ? void 0 : _d.id) {
1450
+ var cachedResult = percentageFeesCache.get(item.fee.id);
1451
+ if (cachedResult && cachedResult.totalFee && cachedResult.totalFee > 0) {
1452
+ console.log(" ".concat(item.fee.name, ": R$ ").concat(cachedResult.totalFee.toFixed(2)));
1453
+ results.push({
1454
+ total: Number(cachedResult.totalFee.toFixed(2)),
1455
+ resultFee: cachedResult.resultFee || '',
1456
+ feeValue: item.value || 0,
1457
+ feeId: item.feeId,
1458
+ });
1459
+ }
1460
+ }
1459
1461
  }
1460
1462
  }
1461
1463
  // PASSADA 3: Calcular ICMS com base em todas as fees anteriores (normais + TOTAL_PERCENTAGE)
package/build/index.js.gz CHANGED
Binary file