b2m-utils 0.0.287 → 0.0.289
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.esm.js +65 -3
- package/build/index.esm.js.gz +0 -0
- package/build/index.esm.js.map +1 -1
- package/build/index.js +65 -3
- package/build/index.js.gz +0 -0
- package/build/index.js.map +1 -1
- package/package.json +1 -1
package/build/index.esm.js
CHANGED
|
@@ -578,10 +578,72 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug, allFeesForCal
|
|
|
578
578
|
switch (fee_1.feeCalculationTypeId) {
|
|
579
579
|
case FeeCalculationTypeEnum.TOTAL_PERCENTAGE:
|
|
580
580
|
if (allFees && Array.isArray(allFees) && (allFees === null || allFees === void 0 ? void 0 : allFees.length) > 0) {
|
|
581
|
-
var
|
|
582
|
-
|
|
583
|
-
|
|
581
|
+
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) !== FeeEnum.ICMS && ((_c = i.fee) === null || _c === void 0 ? void 0 : _c.parentId) !== fee_1.parentId; });
|
|
582
|
+
// Separar fees em dois grupos
|
|
583
|
+
var nonPercentageFees = filteredFees.filter(function (i) { var _a; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) !== FeeCalculationTypeEnum.TOTAL_PERCENTAGE; });
|
|
584
|
+
var percentageFees_1 = filteredFees.filter(function (i) { var _a; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) === FeeCalculationTypeEnum.TOTAL_PERCENTAGE; });
|
|
585
|
+
// Calcular fees não-percentuais (base fixa)
|
|
586
|
+
var nonPercentageResults_1 = filterSiblingFees(nonPercentageFees.map(function (it) {
|
|
587
|
+
return calculateFee(it, cte, allFees, debug, allFeesForCalc);
|
|
584
588
|
}));
|
|
589
|
+
// Calcular fees percentuais iterativamente até convergir
|
|
590
|
+
var percentageResults = [];
|
|
591
|
+
var maxIterations = 10;
|
|
592
|
+
var tolerance_1 = 0.01; // Tolerância de 1 centavo
|
|
593
|
+
// Inicializar com valores zerados
|
|
594
|
+
var previousValues_1 = new Map();
|
|
595
|
+
percentageFees_1.forEach(function (pf) { return previousValues_1.set(pf.fee.id, 0); });
|
|
596
|
+
console.log("\n\uD83D\uDD04 Iniciando c\u00E1lculo iterativo para fee ".concat(fee_1.id, " (").concat(fee_1.name, ")"));
|
|
597
|
+
console.log("\uD83D\uDCCA Base n\u00E3o-percentual: R$ ".concat(nonPercentageResults_1.reduce(function (sum, r) { return sum + (r.totalToCalc || 0); }, 0).toFixed(2)));
|
|
598
|
+
console.log("\uD83D\uDCCB Fees percentuais a calcular: ".concat(percentageFees_1.map(function (f) { var _a; return "".concat((_a = f.fee) === null || _a === void 0 ? void 0 : _a.name, " (").concat(f.value, "%)"); }).join(', ')));
|
|
599
|
+
var _loop_1 = function (iteration) {
|
|
600
|
+
console.log("\n--- Itera\u00E7\u00E3o ".concat(iteration + 1, " ---"));
|
|
601
|
+
// Calcular cada fee percentual com base nos valores atuais das outras
|
|
602
|
+
var currentResults = percentageFees_1.map(function (pf) {
|
|
603
|
+
var _a;
|
|
604
|
+
// Criar uma lista temporária com os valores já calculados das outras fees percentuais
|
|
605
|
+
var tempFees = percentageFees_1
|
|
606
|
+
.filter(function (f) { return f.fee.id !== pf.fee.id; })
|
|
607
|
+
.map(function (f) { return (__assign(__assign({}, f), { totalFee: previousValues_1.get(f.fee.id) || 0, totalToCalc: previousValues_1.get(f.fee.id) || 0 })); });
|
|
608
|
+
// Calcular base: fees não-percentuais + outras fees percentuais
|
|
609
|
+
var baseNonPercentage = nonPercentageResults_1.reduce(function (sum, r) { return sum + (r.totalToCalc || 0); }, 0);
|
|
610
|
+
var basePercentage = tempFees.reduce(function (sum, f) { return sum + (f.totalFee || 0); }, 0);
|
|
611
|
+
var baseValue = baseNonPercentage + basePercentage;
|
|
612
|
+
var percentValue = (+pf.value / 100);
|
|
613
|
+
var calculatedValue = baseValue * percentValue;
|
|
614
|
+
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)));
|
|
615
|
+
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')) });
|
|
616
|
+
});
|
|
617
|
+
// Verificar convergência
|
|
618
|
+
var hasConverged = true;
|
|
619
|
+
currentResults.forEach(function (result) {
|
|
620
|
+
var _a;
|
|
621
|
+
var prevValue = previousValues_1.get(result.feeId) || 0;
|
|
622
|
+
var diff = Math.abs(result.totalFee - prevValue);
|
|
623
|
+
if (diff > tolerance_1) {
|
|
624
|
+
hasConverged = false;
|
|
625
|
+
}
|
|
626
|
+
console.log(" ".concat((_a = result.fee) === null || _a === void 0 ? void 0 : _a.name, ": anterior=").concat(prevValue.toFixed(2), ", atual=").concat(result.totalFee.toFixed(2), ", diff=").concat(diff.toFixed(4)));
|
|
627
|
+
previousValues_1.set(result.feeId, result.totalFee);
|
|
628
|
+
});
|
|
629
|
+
percentageResults = currentResults;
|
|
630
|
+
if (hasConverged) {
|
|
631
|
+
console.log("\u2705 Convergiu na itera\u00E7\u00E3o ".concat(iteration + 1, "!"));
|
|
632
|
+
return "break";
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
for (var iteration = 0; iteration < maxIterations; iteration++) {
|
|
636
|
+
var state_1 = _loop_1(iteration);
|
|
637
|
+
if (state_1 === "break")
|
|
638
|
+
break;
|
|
639
|
+
}
|
|
640
|
+
console.log("\n\uD83D\uDCC8 Resultado final das fees percentuais:");
|
|
641
|
+
percentageResults.forEach(function (r) {
|
|
642
|
+
var _a;
|
|
643
|
+
console.log(" ".concat((_a = r.fee) === null || _a === void 0 ? void 0 : _a.name, ": R$ ").concat((r.totalFee || 0).toFixed(2)));
|
|
644
|
+
});
|
|
645
|
+
// Combinar resultados
|
|
646
|
+
var filteredFeesResults = __spreadArray(__spreadArray([], nonPercentageResults_1, true), percentageResults, true);
|
|
585
647
|
var totalFromAllFees = filteredFeesResults.map(function (result) {
|
|
586
648
|
if (result === null || result === void 0 ? void 0 : result.totalToCalc) {
|
|
587
649
|
return result.totalToCalc;
|
package/build/index.esm.js.gz
CHANGED
|
Binary file
|