b2m-utils 0.0.286 → 0.0.288

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
@@ -582,10 +582,56 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug, allFeesForCal
582
582
  switch (fee_1.feeCalculationTypeId) {
583
583
  case exports.FeeCalculationTypeEnum.TOTAL_PERCENTAGE:
584
584
  if (allFees && Array.isArray(allFees) && (allFees === null || allFees === void 0 ? void 0 : allFees.length) > 0) {
585
- var filteredFees_1 = allFees.filter(function (i) { var _a, _b; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) !== exports.FeeCalculationTypeEnum.TOTAL_PERCENTAGE && ((_b = i.fee) === null || _b === void 0 ? void 0 : _b.parentId) !== fee_1.parentId; });
586
- var filteredFeesResults = filterSiblingFees(filteredFees_1.map(function (it) {
587
- return calculateFee(it, cte, filteredFees_1, debug, allFeesForCalc);
585
+ var filteredFees = allFees.filter(function (i) { var _a, _b, _c; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.id) !== fee_1.id && ((_b = i.fee) === null || _b === void 0 ? void 0 : _b.id) !== exports.FeeEnum.ICMS && ((_c = i.fee) === null || _c === void 0 ? void 0 : _c.parentId) !== fee_1.parentId; });
586
+ // Separar fees em dois grupos
587
+ var nonPercentageFees = filteredFees.filter(function (i) { var _a; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) !== exports.FeeCalculationTypeEnum.TOTAL_PERCENTAGE; });
588
+ var percentageFees_1 = filteredFees.filter(function (i) { var _a; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) === exports.FeeCalculationTypeEnum.TOTAL_PERCENTAGE; });
589
+ // Calcular fees não-percentuais (base fixa)
590
+ var nonPercentageResults_1 = filterSiblingFees(nonPercentageFees.map(function (it) {
591
+ return calculateFee(it, cte, allFees, debug, allFeesForCalc);
588
592
  }));
593
+ // Calcular fees percentuais iterativamente até convergir
594
+ var percentageResults = [];
595
+ var maxIterations = 10;
596
+ var tolerance_1 = 0.01; // Tolerância de 1 centavo
597
+ // Inicializar com valores zerados
598
+ var previousValues_1 = new Map();
599
+ percentageFees_1.forEach(function (pf) { return previousValues_1.set(pf.fee.id, 0); });
600
+ var _loop_1 = function (iteration) {
601
+ // Calcular cada fee percentual com base nos valores atuais das outras
602
+ var currentResults = percentageFees_1.map(function (pf) {
603
+ // Criar uma lista temporária com os valores já calculados das outras fees percentuais
604
+ var tempFees = percentageFees_1
605
+ .filter(function (f) { return f.fee.id !== pf.fee.id; })
606
+ .map(function (f) { return (__assign(__assign({}, f), { totalFee: previousValues_1.get(f.fee.id) || 0, totalToCalc: previousValues_1.get(f.fee.id) || 0 })); });
607
+ // Calcular base: fees não-percentuais + outras fees percentuais
608
+ var baseValue = nonPercentageResults_1.reduce(function (sum, r) { return sum + (r.totalToCalc || 0); }, 0) +
609
+ tempFees.reduce(function (sum, f) { return sum + (f.totalFee || 0); }, 0);
610
+ var percentValue = (+pf.value / 100);
611
+ var calculatedValue = baseValue * percentValue;
612
+ return __assign(__assign({}, pf), { totalFee: calculatedValue, totalToCalc: calculatedValue, fee: pf.fee, feeValue: pf.value, resultFee: "".concat(convertNumberToCurrency(baseValue, 'pt-br'), " x ").concat(+pf.value, "% = ").concat(convertNumberToCurrency(calculatedValue, 'pt-br')) });
613
+ });
614
+ // Verificar convergência
615
+ var hasConverged = true;
616
+ currentResults.forEach(function (result) {
617
+ var prevValue = previousValues_1.get(result.feeId) || 0;
618
+ if (Math.abs(result.totalFee - prevValue) > tolerance_1) {
619
+ hasConverged = false;
620
+ }
621
+ previousValues_1.set(result.feeId, result.totalFee);
622
+ });
623
+ percentageResults = currentResults;
624
+ if (hasConverged) {
625
+ return "break";
626
+ }
627
+ };
628
+ for (var iteration = 0; iteration < maxIterations; iteration++) {
629
+ var state_1 = _loop_1(iteration);
630
+ if (state_1 === "break")
631
+ break;
632
+ }
633
+ // Combinar resultados
634
+ var filteredFeesResults = __spreadArray(__spreadArray([], nonPercentageResults_1, true), percentageResults, true);
589
635
  var totalFromAllFees = filteredFeesResults.map(function (result) {
590
636
  if (result === null || result === void 0 ? void 0 : result.totalToCalc) {
591
637
  return result.totalToCalc;
package/build/index.js.gz CHANGED
Binary file