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.
package/build/index.js CHANGED
@@ -1024,10 +1024,6 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCa
1024
1024
  if (percentageFees.length === 0) {
1025
1025
  return results;
1026
1026
  }
1027
- console.log("\n\uD83D\uDD04 Calculando ".concat(percentageFees.length, " fees TOTAL_PERCENTAGE de forma iterativa"));
1028
- 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(', ')));
1029
- // Calcular fees não-percentuais (base fixa)
1030
- 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(', ')));
1031
1027
  var baseNonPercentage = 0;
1032
1028
  // Se temos valores pré-calculados, usar eles ao invés de recalcular
1033
1029
  if (preCalculatedValues) {
@@ -1044,10 +1040,6 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCa
1044
1040
  }));
1045
1041
  baseNonPercentage = nonPercentageResults.reduce(function (sum, r) { return sum + (r.totalToCalc || 0); }, 0);
1046
1042
  }
1047
- console.log("\uD83D\uDCCA Base n\u00E3o-percentual: R$ ".concat(baseNonPercentage.toFixed(2)));
1048
- if (!preCalculatedValues) {
1049
- console.log("\uD83D\uDCCA Detalhamento calculado automaticamente");
1050
- }
1051
1043
  // Calcular fees percentuais iterativamente até convergir
1052
1044
  var maxIterations = 10;
1053
1045
  var tolerance = 0.01; // Tolerância de 1 centavo
@@ -1055,10 +1047,8 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCa
1055
1047
  var previousValues = new Map();
1056
1048
  percentageFees.forEach(function (pf) { return previousValues.set(pf.fee.id, 0); });
1057
1049
  var _loop_1 = function (iteration) {
1058
- console.log("\n--- Itera\u00E7\u00E3o ".concat(iteration + 1, " ---"));
1059
1050
  // Calcular cada fee percentual com base nos valores atuais das outras
1060
1051
  percentageFees.forEach(function (pf) {
1061
- var _a;
1062
1052
  // Calcular base: fees não-percentuais + outras fees percentuais (exceto ela mesma)
1063
1053
  var basePercentage = Array.from(previousValues.entries())
1064
1054
  .filter(function (_a) {
@@ -1072,7 +1062,6 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCa
1072
1062
  var baseValue = baseNonPercentage + basePercentage;
1073
1063
  var percentValue = (+pf.value / 100);
1074
1064
  var calculatedValue = baseValue * percentValue;
1075
- 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)));
1076
1065
  previousValues.set(pf.fee.id, calculatedValue);
1077
1066
  // Armazenar resultado
1078
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')) }));
@@ -1095,7 +1084,6 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCa
1095
1084
  hasConverged = false;
1096
1085
  }
1097
1086
  if (hasConverged) {
1098
- console.log("\u2705 Convergiu na itera\u00E7\u00E3o ".concat(iteration + 1, "!"));
1099
1087
  return "break";
1100
1088
  }
1101
1089
  };
@@ -1104,11 +1092,6 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, preCa
1104
1092
  if (state_1 === "break")
1105
1093
  break;
1106
1094
  }
1107
- console.log("\n\uD83D\uDCC8 Resultado final das fees percentuais:");
1108
- results.forEach(function (result, feeId) {
1109
- var _a;
1110
- console.log(" ".concat((_a = result.fee) === null || _a === void 0 ? void 0 : _a.name, " (").concat(feeId, "): R$ ").concat((result.totalFee || 0).toFixed(2)));
1111
- });
1112
1095
  return results;
1113
1096
  };
1114
1097
 
@@ -1395,7 +1378,7 @@ var getLaneFromRatecard = function (cte) {
1395
1378
  };
1396
1379
 
1397
1380
  var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1398
- var _a, _b, _c;
1381
+ var _a, _b, _c, _d, _e, _f;
1399
1382
  var results = [];
1400
1383
  // Separar fees por tipo: normais, TOTAL_PERCENTAGE e ICMS
1401
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; });
@@ -1406,6 +1389,8 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1406
1389
  f.feeId !== exports.FeeEnum.ICMS &&
1407
1390
  ((_b = f.fee) === null || _b === void 0 ? void 0 : _b.feeCalculationTypeId) !== exports.FeeCalculationTypeEnum.TOTAL_PERCENTAGE;
1408
1391
  });
1392
+ // PASSADA 1: Calcular fees normais (não TOTAL_PERCENTAGE, não ICMS)
1393
+ console.log("\n\uD83D\uDCE6 [getCtesFeesResult] Calculando ".concat(normalFees.length, " fees normais"));
1409
1394
  var _loop_1 = function (item) {
1410
1395
  var feeTotal = calculateFee(item, cte, feesToCalc, false, allFeesForCalc);
1411
1396
  if (feeTotal === null || feeTotal === void 0 ? void 0 : feeTotal.totalFee) {
@@ -1422,8 +1407,8 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1422
1407
  });
1423
1408
  if (siblingFees && (siblingFees === null || siblingFees === void 0 ? void 0 : siblingFees.length) > 0) {
1424
1409
  var siblingFeesResults = [];
1425
- for (var _e = 0, siblingFees_1 = siblingFees; _e < siblingFees_1.length; _e++) {
1426
- var item_1 = siblingFees_1[_e];
1410
+ for (var _h = 0, siblingFees_1 = siblingFees; _h < siblingFees_1.length; _h++) {
1411
+ var item_1 = siblingFees_1[_h];
1427
1412
  var result = calculateFee(item_1, cte, feesToCalc, false, allFeesForCalc);
1428
1413
  siblingFeesResults.push(result);
1429
1414
  }
@@ -1435,12 +1420,14 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1435
1420
  });
1436
1421
  if ((siblingFeesResults === null || siblingFeesResults === void 0 ? void 0 : siblingFeesResults.length) > 0) {
1437
1422
  if (((_b = siblingFeesResults[0]) === null || _b === void 0 ? void 0 : _b.totalFee) && ((_c = siblingFeesResults[0]) === null || _c === void 0 ? void 0 : _c.totalFee) > totalFee) {
1423
+ 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)"));
1438
1424
  totalToPush = 0;
1439
1425
  }
1440
1426
  }
1441
1427
  }
1442
1428
  }
1443
- if (!isNaN(+totalToPush)) {
1429
+ if (!isNaN(+totalToPush) && totalToPush > 0) {
1430
+ console.log(" \u2705 ".concat((_e = item.fee) === null || _e === void 0 ? void 0 : _e.name, ": R$ ").concat(totalToPush.toFixed(2)));
1444
1431
  results.push({
1445
1432
  total: +(totalToPush).toFixed(2),
1446
1433
  resultFee: resultFee,
@@ -1450,40 +1437,49 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1450
1437
  }
1451
1438
  }
1452
1439
  };
1453
- // PASSADA 1: Calcular fees normais (não TOTAL_PERCENTAGE, não ICMS)
1454
1440
  for (var _i = 0, normalFees_1 = normalFees; _i < normalFees_1.length; _i++) {
1455
1441
  var item = normalFees_1[_i];
1456
1442
  _loop_1(item);
1457
1443
  }
1458
- // PASSADA 2: Calcular fees TOTAL_PERCENTAGE usando a soma das fees normais calculadas
1459
- for (var _d = 0, totalPercentageFees_1 = totalPercentageFees; _d < totalPercentageFees_1.length; _d++) {
1460
- var item = totalPercentageFees_1[_d];
1461
- // Calcular soma das fees normais (excluindo TOTAL_PERCENTAGE e ICMS)
1462
- var totalWithoutPercentageFees = results
1463
- .filter(function (r) { return r.total > 0; })
1464
- .reduce(function (sum, r) { return sum + r.total; }, 0);
1465
- var percentage = item.value || 0;
1466
- var calculatedValue = (totalWithoutPercentageFees * percentage) / 100;
1467
- if (!isNaN(+calculatedValue) && calculatedValue > 0) {
1468
- results.push({
1469
- total: +(calculatedValue).toFixed(2),
1470
- resultFee: "".concat(convertNumberToCurrency(totalWithoutPercentageFees, 'pt-br'), " x ").concat(percentage, "% = ").concat(convertNumberToCurrency(calculatedValue, 'pt-br')),
1471
- feeValue: percentage,
1472
- feeId: item.feeId,
1473
- });
1444
+ console.log("\uD83D\uDCCA Total parcial (fees normais): R$ ".concat(results.reduce(function (sum, r) { return sum + r.total; }, 0).toFixed(2)));
1445
+ // PASSADA 2: Calcular fees TOTAL_PERCENTAGE de forma iterativa
1446
+ if (totalPercentageFees.length > 0) {
1447
+ console.log("\n\uD83D\uDD04 [getCtesFeesResult] Calculando ".concat(totalPercentageFees.length, " fees TOTAL_PERCENTAGE"));
1448
+ 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(', ')));
1449
+ var percentageFeesCache = calculateTotalPercentageFees(feesToCalc, cte, allFeesForCalc);
1450
+ console.log("\u2705 Cache gerado com ".concat(percentageFeesCache.size, " fees"));
1451
+ for (var _g = 0, totalPercentageFees_1 = totalPercentageFees; _g < totalPercentageFees_1.length; _g++) {
1452
+ var item = totalPercentageFees_1[_g];
1453
+ if ((_f = item.fee) === null || _f === void 0 ? void 0 : _f.id) {
1454
+ var cachedResult = percentageFeesCache.get(item.fee.id);
1455
+ if (cachedResult && cachedResult.totalFee && cachedResult.totalFee > 0) {
1456
+ console.log(" ".concat(item.fee.name, ": R$ ").concat(cachedResult.totalFee.toFixed(2)));
1457
+ results.push({
1458
+ total: Number(cachedResult.totalFee.toFixed(2)),
1459
+ resultFee: cachedResult.resultFee || '',
1460
+ feeValue: item.value || 0,
1461
+ feeId: item.feeId,
1462
+ });
1463
+ }
1464
+ }
1474
1465
  }
1475
1466
  }
1476
1467
  // PASSADA 3: Calcular ICMS com base em todas as fees anteriores (normais + TOTAL_PERCENTAGE)
1477
1468
  if (icmsFee) {
1469
+ console.log("\n\uD83D\uDCB0 [getCtesFeesResult] Calculando ICMS");
1478
1470
  // Coletar apenas os valores das fees que realmente contam (total > 0)
1479
1471
  var valuesForIcms = results
1480
1472
  .filter(function (r) { return r.total > 0; })
1481
1473
  .map(function (r) { return r.total; });
1474
+ 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)));
1482
1475
  var icmsRate = (cte.icmsIncidence !== undefined && cte.icmsIncidence !== null && !isNaN(+cte.icmsIncidence))
1483
1476
  ? +(cte.icmsIncidence) / 100
1484
1477
  : icmsFee.value || 0;
1478
+ console.log("\uD83D\uDCC8 Al\u00EDquota ICMS: ".concat((icmsRate * 100).toFixed(2), "%"));
1485
1479
  // Usar calculateIcms diretamente
1486
1480
  var icmsCalculation = calculateIcms(valuesForIcms, icmsRate);
1481
+ console.log("\u2705 ICMS calculado: R$ ".concat(icmsCalculation.total.toFixed(2)));
1482
+ console.log("\uD83D\uDCCB Total de fees no results antes do ICMS: ".concat(results.length));
1487
1483
  if (icmsCalculation.isValid && !isNaN(+icmsCalculation.total)) {
1488
1484
  results.push({
1489
1485
  total: +(icmsCalculation.total).toFixed(2),
@@ -1491,8 +1487,11 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1491
1487
  feeValue: icmsRate,
1492
1488
  feeId: icmsFee.feeId,
1493
1489
  });
1490
+ console.log("\uD83D\uDCCB Total de fees no results depois do ICMS: ".concat(results.length));
1494
1491
  }
1495
1492
  }
1493
+ console.log("\n\uD83C\uDFAF [getCtesFeesResult] Total final: R$ ".concat(results.reduce(function (sum, r) { return sum + r.total; }, 0).toFixed(2)));
1494
+ console.log("\uD83D\uDCCB Breakdown: ".concat(results.map(function (r) { return "R$ ".concat(r.total.toFixed(2)); }).join(' + '), "\n"));
1496
1495
  return results;
1497
1496
  };
1498
1497
 
package/build/index.js.gz CHANGED
Binary file