b2m-utils 0.0.294 → 0.0.296

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, _e, _f;
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; });
@@ -1402,6 +1385,8 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1402
1385
  f.feeId !== FeeEnum.ICMS &&
1403
1386
  ((_b = f.fee) === null || _b === void 0 ? void 0 : _b.feeCalculationTypeId) !== FeeCalculationTypeEnum.TOTAL_PERCENTAGE;
1404
1387
  });
1388
+ // PASSADA 1: Calcular fees normais (não TOTAL_PERCENTAGE, não ICMS)
1389
+ console.log("\n\uD83D\uDCE6 [getCtesFeesResult] Calculando ".concat(normalFees.length, " fees normais"));
1405
1390
  var _loop_1 = function (item) {
1406
1391
  var feeTotal = calculateFee(item, cte, feesToCalc, false, allFeesForCalc);
1407
1392
  if (feeTotal === null || feeTotal === void 0 ? void 0 : feeTotal.totalFee) {
@@ -1418,8 +1403,8 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1418
1403
  });
1419
1404
  if (siblingFees && (siblingFees === null || siblingFees === void 0 ? void 0 : siblingFees.length) > 0) {
1420
1405
  var siblingFeesResults = [];
1421
- for (var _e = 0, siblingFees_1 = siblingFees; _e < siblingFees_1.length; _e++) {
1422
- var item_1 = siblingFees_1[_e];
1406
+ for (var _h = 0, siblingFees_1 = siblingFees; _h < siblingFees_1.length; _h++) {
1407
+ var item_1 = siblingFees_1[_h];
1423
1408
  var result = calculateFee(item_1, cte, feesToCalc, false, allFeesForCalc);
1424
1409
  siblingFeesResults.push(result);
1425
1410
  }
@@ -1431,12 +1416,14 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1431
1416
  });
1432
1417
  if ((siblingFeesResults === null || siblingFeesResults === void 0 ? void 0 : siblingFeesResults.length) > 0) {
1433
1418
  if (((_b = siblingFeesResults[0]) === null || _b === void 0 ? void 0 : _b.totalFee) && ((_c = siblingFeesResults[0]) === null || _c === void 0 ? void 0 : _c.totalFee) > totalFee) {
1419
+ console.log(" \u26A0\uFE0F ".concat((_d = item.fee) === null || _d === void 0 ? void 0 : _d.name, ": R$ ").concat(totalFee.toFixed(2), " \u2192 R$ 0.00 (sibling maior)"));
1434
1420
  totalToPush = 0;
1435
1421
  }
1436
1422
  }
1437
1423
  }
1438
1424
  }
1439
- if (!isNaN(+totalToPush)) {
1425
+ if (!isNaN(+totalToPush) && totalToPush > 0) {
1426
+ console.log(" \u2705 ".concat((_e = item.fee) === null || _e === void 0 ? void 0 : _e.name, ": R$ ").concat(totalToPush.toFixed(2)));
1440
1427
  results.push({
1441
1428
  total: +(totalToPush).toFixed(2),
1442
1429
  resultFee: resultFee,
@@ -1446,40 +1433,49 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1446
1433
  }
1447
1434
  }
1448
1435
  };
1449
- // PASSADA 1: Calcular fees normais (não TOTAL_PERCENTAGE, não ICMS)
1450
1436
  for (var _i = 0, normalFees_1 = normalFees; _i < normalFees_1.length; _i++) {
1451
1437
  var item = normalFees_1[_i];
1452
1438
  _loop_1(item);
1453
1439
  }
1454
- // PASSADA 2: Calcular fees TOTAL_PERCENTAGE usando a soma das fees normais 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
- });
1440
+ console.log("\uD83D\uDCCA Total parcial (fees normais): R$ ".concat(results.reduce(function (sum, r) { return sum + r.total; }, 0).toFixed(2)));
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 _g = 0, totalPercentageFees_1 = totalPercentageFees; _g < totalPercentageFees_1.length; _g++) {
1448
+ var item = totalPercentageFees_1[_g];
1449
+ if ((_f = item.fee) === null || _f === void 0 ? void 0 : _f.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
+ }
1470
1461
  }
1471
1462
  }
1472
1463
  // PASSADA 3: Calcular ICMS com base em todas as fees anteriores (normais + TOTAL_PERCENTAGE)
1473
1464
  if (icmsFee) {
1465
+ console.log("\n\uD83D\uDCB0 [getCtesFeesResult] Calculando ICMS");
1474
1466
  // Coletar apenas os valores das fees que realmente contam (total > 0)
1475
1467
  var valuesForIcms = results
1476
1468
  .filter(function (r) { return r.total > 0; })
1477
1469
  .map(function (r) { return r.total; });
1470
+ console.log("\uD83D\uDCCA Valores para ICMS: ".concat(valuesForIcms.map(function (v) { return "R$ ".concat(v.toFixed(2)); }).join(' + '), " = R$ ").concat(valuesForIcms.reduce(function (a, b) { return a + b; }, 0).toFixed(2)));
1478
1471
  var icmsRate = (cte.icmsIncidence !== undefined && cte.icmsIncidence !== null && !isNaN(+cte.icmsIncidence))
1479
1472
  ? +(cte.icmsIncidence) / 100
1480
1473
  : icmsFee.value || 0;
1474
+ console.log("\uD83D\uDCC8 Al\u00EDquota ICMS: ".concat((icmsRate * 100).toFixed(2), "%"));
1481
1475
  // Usar calculateIcms diretamente
1482
1476
  var icmsCalculation = calculateIcms(valuesForIcms, icmsRate);
1477
+ console.log("\u2705 ICMS calculado: R$ ".concat(icmsCalculation.total.toFixed(2)));
1478
+ console.log("\uD83D\uDCCB Total de fees no results antes do ICMS: ".concat(results.length));
1483
1479
  if (icmsCalculation.isValid && !isNaN(+icmsCalculation.total)) {
1484
1480
  results.push({
1485
1481
  total: +(icmsCalculation.total).toFixed(2),
@@ -1487,8 +1483,11 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1487
1483
  feeValue: icmsRate,
1488
1484
  feeId: icmsFee.feeId,
1489
1485
  });
1486
+ console.log("\uD83D\uDCCB Total de fees no results depois do ICMS: ".concat(results.length));
1490
1487
  }
1491
1488
  }
1489
+ console.log("\n\uD83C\uDFAF [getCtesFeesResult] Total final: R$ ".concat(results.reduce(function (sum, r) { return sum + r.total; }, 0).toFixed(2)));
1490
+ console.log("\uD83D\uDCCB Breakdown: ".concat(results.map(function (r) { return "R$ ".concat(r.total.toFixed(2)); }).join(' + '), "\n"));
1492
1491
  return results;
1493
1492
  };
1494
1493
 
Binary file