b2m-utils 0.0.294 → 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.
@@ -1,7 +1,7 @@
1
1
  import { Cte, RatecardLaneFee } from "../../types";
2
2
  export declare const getCtesFeesResult: (feesToCalc: RatecardLaneFee[], cte: Cte, allFeesForCalc?: RatecardLaneFee[]) => {
3
3
  total: number;
4
- resultFee: string;
4
+ resultFee: any;
5
5
  feeValue: number;
6
6
  feeId: number;
7
7
  }[];
@@ -1020,10 +1020,6 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCa
1020
1020
  if (percentageFees.length === 0) {
1021
1021
  return results;
1022
1022
  }
1023
- console.log("\n\uD83D\uDD04 Calculando ".concat(percentageFees.length, " fees TOTAL_PERCENTAGE de forma iterativa"));
1024
- 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(', ')));
1025
- // Calcular fees não-percentuais (base fixa)
1026
- 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(', ')));
1027
1023
  var baseNonPercentage = 0;
1028
1024
  // Se temos valores pré-calculados, usar eles ao invés de recalcular
1029
1025
  if (preCalculatedValues) {
@@ -1040,10 +1036,6 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCa
1040
1036
  }));
1041
1037
  baseNonPercentage = nonPercentageResults.reduce(function (sum, r) { return sum + (r.totalToCalc || 0); }, 0);
1042
1038
  }
1043
- console.log("\uD83D\uDCCA Base n\u00E3o-percentual: R$ ".concat(baseNonPercentage.toFixed(2)));
1044
- if (!preCalculatedValues) {
1045
- console.log("\uD83D\uDCCA Detalhamento calculado automaticamente");
1046
- }
1047
1039
  // Calcular fees percentuais iterativamente até convergir
1048
1040
  var maxIterations = 10;
1049
1041
  var tolerance = 0.01; // Tolerância de 1 centavo
@@ -1051,10 +1043,8 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCa
1051
1043
  var previousValues = new Map();
1052
1044
  percentageFees.forEach(function (pf) { return previousValues.set(pf.fee.id, 0); });
1053
1045
  var _loop_1 = function (iteration) {
1054
- console.log("\n--- Itera\u00E7\u00E3o ".concat(iteration + 1, " ---"));
1055
1046
  // Calcular cada fee percentual com base nos valores atuais das outras
1056
1047
  percentageFees.forEach(function (pf) {
1057
- var _a;
1058
1048
  // Calcular base: fees não-percentuais + outras fees percentuais (exceto ela mesma)
1059
1049
  var basePercentage = Array.from(previousValues.entries())
1060
1050
  .filter(function (_a) {
@@ -1068,7 +1058,6 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCa
1068
1058
  var baseValue = baseNonPercentage + basePercentage;
1069
1059
  var percentValue = (+pf.value / 100);
1070
1060
  var calculatedValue = baseValue * percentValue;
1071
- 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)));
1072
1061
  previousValues.set(pf.fee.id, calculatedValue);
1073
1062
  // Armazenar resultado
1074
1063
  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')) }));
@@ -1091,7 +1080,6 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCa
1091
1080
  hasConverged = false;
1092
1081
  }
1093
1082
  if (hasConverged) {
1094
- console.log("\u2705 Convergiu na itera\u00E7\u00E3o ".concat(iteration + 1, "!"));
1095
1083
  return "break";
1096
1084
  }
1097
1085
  };
@@ -1100,11 +1088,6 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCa
1100
1088
  if (state_1 === "break")
1101
1089
  break;
1102
1090
  }
1103
- console.log("\n\uD83D\uDCC8 Resultado final das fees percentuais:");
1104
- results.forEach(function (result, feeId) {
1105
- var _a;
1106
- console.log(" ".concat((_a = result.fee) === null || _a === void 0 ? void 0 : _a.name, " (").concat(feeId, "): R$ ").concat((result.totalFee || 0).toFixed(2)));
1107
- });
1108
1091
  return results;
1109
1092
  };
1110
1093
 
@@ -1391,7 +1374,7 @@ var getLaneFromRatecard = function (cte) {
1391
1374
  };
1392
1375
 
1393
1376
  var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1394
- var _a, _b, _c;
1377
+ var _a, _b, _c, _d;
1395
1378
  var results = [];
1396
1379
  // Separar fees por tipo: normais, TOTAL_PERCENTAGE e ICMS
1397
1380
  var icmsFee = feesToCalc.find(function (f) { var _a; return ((_a = f.fee) === null || _a === void 0 ? void 0 : _a.id) === FeeEnum.ICMS || f.feeId === FeeEnum.ICMS; });
@@ -1418,8 +1401,8 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1418
1401
  });
1419
1402
  if (siblingFees && (siblingFees === null || siblingFees === void 0 ? void 0 : siblingFees.length) > 0) {
1420
1403
  var siblingFeesResults = [];
1421
- for (var _e = 0, siblingFees_1 = siblingFees; _e < siblingFees_1.length; _e++) {
1422
- var item_1 = siblingFees_1[_e];
1404
+ for (var _f = 0, siblingFees_1 = siblingFees; _f < siblingFees_1.length; _f++) {
1405
+ var item_1 = siblingFees_1[_f];
1423
1406
  var result = calculateFee(item_1, cte, feesToCalc, false, allFeesForCalc);
1424
1407
  siblingFeesResults.push(result);
1425
1408
  }
@@ -1451,22 +1434,26 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1451
1434
  var item = normalFees_1[_i];
1452
1435
  _loop_1(item);
1453
1436
  }
1454
- // PASSADA 2: Calcular fees TOTAL_PERCENTAGE usando a soma das fees normais já calculadas
1455
- for (var _d = 0, totalPercentageFees_1 = totalPercentageFees; _d < totalPercentageFees_1.length; _d++) {
1456
- var item = totalPercentageFees_1[_d];
1457
- // Calcular soma das fees normais (excluindo TOTAL_PERCENTAGE e ICMS)
1458
- var totalWithoutPercentageFees = results
1459
- .filter(function (r) { return r.total > 0; })
1460
- .reduce(function (sum, r) { return sum + r.total; }, 0);
1461
- var percentage = item.value || 0;
1462
- var calculatedValue = (totalWithoutPercentageFees * percentage) / 100;
1463
- if (!isNaN(+calculatedValue) && calculatedValue > 0) {
1464
- results.push({
1465
- total: +(calculatedValue).toFixed(2),
1466
- resultFee: "".concat(convertNumberToCurrency(totalWithoutPercentageFees, 'pt-br'), " x ").concat(percentage, "% = ").concat(convertNumberToCurrency(calculatedValue, 'pt-br')),
1467
- feeValue: percentage,
1468
- feeId: item.feeId,
1469
- });
1437
+ // PASSADA 2: Calcular fees TOTAL_PERCENTAGE de forma iterativa
1438
+ if (totalPercentageFees.length > 0) {
1439
+ console.log("\n\uD83D\uDD04 [getCtesFeesResult] Calculando ".concat(totalPercentageFees.length, " fees TOTAL_PERCENTAGE"));
1440
+ 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(', ')));
1441
+ var percentageFeesCache = calculateTotalPercentageFees(feesToCalc, cte, allFeesForCalc);
1442
+ console.log("\u2705 Cache gerado com ".concat(percentageFeesCache.size, " fees"));
1443
+ for (var _e = 0, totalPercentageFees_1 = totalPercentageFees; _e < totalPercentageFees_1.length; _e++) {
1444
+ var item = totalPercentageFees_1[_e];
1445
+ if ((_d = item.fee) === null || _d === void 0 ? void 0 : _d.id) {
1446
+ var cachedResult = percentageFeesCache.get(item.fee.id);
1447
+ if (cachedResult && cachedResult.totalFee && cachedResult.totalFee > 0) {
1448
+ console.log(" ".concat(item.fee.name, ": R$ ").concat(cachedResult.totalFee.toFixed(2)));
1449
+ results.push({
1450
+ total: Number(cachedResult.totalFee.toFixed(2)),
1451
+ resultFee: cachedResult.resultFee || '',
1452
+ feeValue: item.value || 0,
1453
+ feeId: item.feeId,
1454
+ });
1455
+ }
1456
+ }
1470
1457
  }
1471
1458
  }
1472
1459
  // PASSADA 3: Calcular ICMS com base em todas as fees anteriores (normais + TOTAL_PERCENTAGE)
Binary file