b2m-utils 0.0.289 → 0.0.291
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/functions/calculateTotalPercentageFees/index.d.ts +7 -0
- package/build/functions/index.d.ts +1 -0
- package/build/index.esm.js +96 -65
- package/build/index.esm.js.gz +0 -0
- package/build/index.esm.js.map +1 -1
- package/build/index.js +96 -64
- package/build/index.js.gz +0 -0
- package/build/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Cte, RatecardLaneFee } from "../../types";
|
|
2
|
+
import { RatecardLaneFeeWithFeeResult } from "../filterSiblingFees";
|
|
3
|
+
/**
|
|
4
|
+
* Calcula todas as fees do tipo TOTAL_PERCENTAGE de forma iterativa
|
|
5
|
+
* para resolver a interdependência entre elas (ex: TRT e GAC)
|
|
6
|
+
*/
|
|
7
|
+
export declare const calculateTotalPercentageFees: (allFees: RatecardLaneFee[], cte: Cte, allFeesForCalc?: RatecardLaneFee[]) => Map<number, RatecardLaneFeeWithFeeResult>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './applyRedeliveryMultiplier';
|
|
2
2
|
export * from './calculateFee';
|
|
3
3
|
export * from './calculateIcms';
|
|
4
|
+
export * from './calculateTotalPercentageFees';
|
|
4
5
|
export * from './convertNumberToCurrency';
|
|
5
6
|
export * from './filterSiblingFees';
|
|
6
7
|
export * from './getAllFeesForCalculation';
|
package/build/index.esm.js
CHANGED
|
@@ -578,72 +578,18 @@ 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
|
-
|
|
582
|
-
//
|
|
583
|
-
var
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
581
|
+
// Filtrar fees excluindo a própria fee, ICMS e fees irmãs
|
|
582
|
+
// IMPORTANTE: Também excluir outras fees TOTAL_PERCENTAGE para evitar recursão infinita
|
|
583
|
+
var filteredFees = allFees.filter(function (i) {
|
|
584
|
+
var _a, _b, _c, _d;
|
|
585
|
+
return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.id) !== fee_1.id &&
|
|
586
|
+
((_b = i.fee) === null || _b === void 0 ? void 0 : _b.id) !== FeeEnum.ICMS &&
|
|
587
|
+
((_c = i.fee) === null || _c === void 0 ? void 0 : _c.parentId) !== fee_1.parentId &&
|
|
588
|
+
((_d = i.fee) === null || _d === void 0 ? void 0 : _d.feeCalculationTypeId) !== FeeCalculationTypeEnum.TOTAL_PERCENTAGE;
|
|
589
|
+
});
|
|
590
|
+
var filteredFeesResults = filterSiblingFees(filteredFees.map(function (it) {
|
|
587
591
|
return calculateFee(it, cte, allFees, debug, allFeesForCalc);
|
|
588
592
|
}));
|
|
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);
|
|
647
593
|
var totalFromAllFees = filteredFeesResults.map(function (result) {
|
|
648
594
|
if (result === null || result === void 0 ? void 0 : result.totalToCalc) {
|
|
649
595
|
return result.totalToCalc;
|
|
@@ -1051,6 +997,91 @@ var calculateIcms = function (totalFromAllFees, icmsRate) {
|
|
|
1051
997
|
};
|
|
1052
998
|
};
|
|
1053
999
|
|
|
1000
|
+
/**
|
|
1001
|
+
* Calcula todas as fees do tipo TOTAL_PERCENTAGE de forma iterativa
|
|
1002
|
+
* para resolver a interdependência entre elas (ex: TRT e GAC)
|
|
1003
|
+
*/
|
|
1004
|
+
var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc) {
|
|
1005
|
+
var results = new Map();
|
|
1006
|
+
// Separar fees em dois grupos
|
|
1007
|
+
var nonPercentageFees = allFees.filter(function (i) { var _a; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) !== FeeCalculationTypeEnum.TOTAL_PERCENTAGE; });
|
|
1008
|
+
var percentageFees = allFees.filter(function (i) { var _a; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) === FeeCalculationTypeEnum.TOTAL_PERCENTAGE; });
|
|
1009
|
+
// Se não houver fees percentuais, retornar vazio
|
|
1010
|
+
if (percentageFees.length === 0) {
|
|
1011
|
+
return results;
|
|
1012
|
+
}
|
|
1013
|
+
console.log("\n\uD83D\uDD04 Calculando ".concat(percentageFees.length, " fees TOTAL_PERCENTAGE de forma iterativa"));
|
|
1014
|
+
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(', ')));
|
|
1015
|
+
// Calcular fees não-percentuais (base fixa)
|
|
1016
|
+
var nonPercentageResults = filterSiblingFees(nonPercentageFees.map(function (it) {
|
|
1017
|
+
return calculateFee(it, cte, allFees, false, allFeesForCalc);
|
|
1018
|
+
}));
|
|
1019
|
+
var baseNonPercentage = nonPercentageResults.reduce(function (sum, r) { return sum + (r.totalToCalc || 0); }, 0);
|
|
1020
|
+
console.log("\uD83D\uDCCA Base n\u00E3o-percentual: R$ ".concat(baseNonPercentage.toFixed(2)));
|
|
1021
|
+
// Calcular fees percentuais iterativamente até convergir
|
|
1022
|
+
var maxIterations = 10;
|
|
1023
|
+
var tolerance = 0.01; // Tolerância de 1 centavo
|
|
1024
|
+
// Inicializar com valores zerados
|
|
1025
|
+
var previousValues = new Map();
|
|
1026
|
+
percentageFees.forEach(function (pf) { return previousValues.set(pf.fee.id, 0); });
|
|
1027
|
+
var _loop_1 = function (iteration) {
|
|
1028
|
+
console.log("\n--- Itera\u00E7\u00E3o ".concat(iteration + 1, " ---"));
|
|
1029
|
+
// Calcular cada fee percentual com base nos valores atuais das outras
|
|
1030
|
+
percentageFees.forEach(function (pf) {
|
|
1031
|
+
var _a;
|
|
1032
|
+
// Calcular base: fees não-percentuais + outras fees percentuais (exceto ela mesma)
|
|
1033
|
+
var basePercentage = Array.from(previousValues.entries())
|
|
1034
|
+
.filter(function (_a) {
|
|
1035
|
+
var feeId = _a[0];
|
|
1036
|
+
return feeId !== pf.fee.id;
|
|
1037
|
+
})
|
|
1038
|
+
.reduce(function (sum, _a) {
|
|
1039
|
+
var value = _a[1];
|
|
1040
|
+
return sum + value;
|
|
1041
|
+
}, 0);
|
|
1042
|
+
var baseValue = baseNonPercentage + basePercentage;
|
|
1043
|
+
var percentValue = (+pf.value / 100);
|
|
1044
|
+
var calculatedValue = baseValue * percentValue;
|
|
1045
|
+
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)));
|
|
1046
|
+
previousValues.set(pf.fee.id, calculatedValue);
|
|
1047
|
+
// Armazenar resultado
|
|
1048
|
+
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')) }));
|
|
1049
|
+
});
|
|
1050
|
+
// Verificar convergência
|
|
1051
|
+
var hasConverged = true;
|
|
1052
|
+
var currentIteration = iteration;
|
|
1053
|
+
if (currentIteration > 0) {
|
|
1054
|
+
percentageFees.forEach(function (pf) {
|
|
1055
|
+
var currentValue = previousValues.get(pf.fee.id) || 0;
|
|
1056
|
+
var result = results.get(pf.fee.id);
|
|
1057
|
+
var prevValue = (result === null || result === void 0 ? void 0 : result.totalFee) || 0;
|
|
1058
|
+
var diff = Math.abs(currentValue - prevValue);
|
|
1059
|
+
if (diff > tolerance) {
|
|
1060
|
+
hasConverged = false;
|
|
1061
|
+
}
|
|
1062
|
+
});
|
|
1063
|
+
}
|
|
1064
|
+
else {
|
|
1065
|
+
hasConverged = false;
|
|
1066
|
+
}
|
|
1067
|
+
if (hasConverged) {
|
|
1068
|
+
console.log("\u2705 Convergiu na itera\u00E7\u00E3o ".concat(iteration + 1, "!"));
|
|
1069
|
+
return "break";
|
|
1070
|
+
}
|
|
1071
|
+
};
|
|
1072
|
+
for (var iteration = 0; iteration < maxIterations; iteration++) {
|
|
1073
|
+
var state_1 = _loop_1(iteration);
|
|
1074
|
+
if (state_1 === "break")
|
|
1075
|
+
break;
|
|
1076
|
+
}
|
|
1077
|
+
console.log("\n\uD83D\uDCC8 Resultado final das fees percentuais:");
|
|
1078
|
+
results.forEach(function (result, feeId) {
|
|
1079
|
+
var _a;
|
|
1080
|
+
console.log(" ".concat((_a = result.fee) === null || _a === void 0 ? void 0 : _a.name, " (").concat(feeId, "): R$ ").concat((result.totalFee || 0).toFixed(2)));
|
|
1081
|
+
});
|
|
1082
|
+
return results;
|
|
1083
|
+
};
|
|
1084
|
+
|
|
1054
1085
|
var verifyConditionalFee = function (conditionalFeeToVerify, cte) {
|
|
1055
1086
|
var _a, _b;
|
|
1056
1087
|
if (conditionalFeeToVerify === null || conditionalFeeToVerify === void 0 ? void 0 : conditionalFeeToVerify.id) {
|
|
@@ -8065,5 +8096,5 @@ var isTariffMatch = function (chargedName, auditedTariffName) {
|
|
|
8065
8096
|
});
|
|
8066
8097
|
};
|
|
8067
8098
|
|
|
8068
|
-
export { ApplicationColumnNameEnum, ApplicationEnum, CarrierDocumentConfigTypeKeyEnum, CountryEnum, CteStatusEnum, CteVehicleTypeEnum, CurrencyEnum, DocumentTypeEnum, DomainConfigurationEnum, DomainTypeEnum, EXPORT_CTE_COLUMN_LABELS, EXPORT_CTE_TYPE_LABELS, ExportCteColumn, ExportCteTypeEnum, FeeCalculationTypeEnum, FeeCategoryEnum, FeeEnum, FreightRegionEnum, ImapHostsEnum, InputTypeEnum, ModalEnum, NotificationTypeEnum, PermissionEnum, RatecardConditionalFeeTypeEnum, RatecardFeeConfigKeyEnum, RatecardFeeConfigTypeEnum, RatecardModalEnum, SlaRegionEnum, SpotStatusEnum, TARIFF_MAPPING, TrackProcessProviderTypeEnum, addTariffMapping, applyRedeliveryMultiplier, buildDynamicMapping, calculateFee, calculateIcms, convertNumberToCurrency, filterSiblingFees, findAuditedMatch, formatDateString, getAllFeesForCalculation, getAuditTotalFromCte, getConfigurationFromDomain, getContractFromFreight, getContractRouteFromFreight, getCookies, getCteDateRange, getCteLane, getCteLaneFeesTotal, getCtesFeesResult, getDataFromToken, getFilteredFeesToAudit, getFormattedFreightPlaceName, getLaneFeesToCalc, getLaneFromRatecard, getNormalizedCityName, getRatecardFromCte, getRouteDeliveryTimeFromFreight, getRouteOnTimeFromFreight, getTariffVariations, isTariffMatch, matchAllTariffs, normalizeString, parseAliases, setFormattedDatesInObjects, verifyConditionalFee, verifyDefaultFees };
|
|
8099
|
+
export { ApplicationColumnNameEnum, ApplicationEnum, CarrierDocumentConfigTypeKeyEnum, CountryEnum, CteStatusEnum, CteVehicleTypeEnum, CurrencyEnum, DocumentTypeEnum, DomainConfigurationEnum, DomainTypeEnum, EXPORT_CTE_COLUMN_LABELS, EXPORT_CTE_TYPE_LABELS, ExportCteColumn, ExportCteTypeEnum, FeeCalculationTypeEnum, FeeCategoryEnum, FeeEnum, FreightRegionEnum, ImapHostsEnum, InputTypeEnum, ModalEnum, NotificationTypeEnum, PermissionEnum, RatecardConditionalFeeTypeEnum, RatecardFeeConfigKeyEnum, RatecardFeeConfigTypeEnum, RatecardModalEnum, SlaRegionEnum, SpotStatusEnum, TARIFF_MAPPING, TrackProcessProviderTypeEnum, addTariffMapping, applyRedeliveryMultiplier, buildDynamicMapping, calculateFee, calculateIcms, calculateTotalPercentageFees, convertNumberToCurrency, filterSiblingFees, findAuditedMatch, formatDateString, getAllFeesForCalculation, getAuditTotalFromCte, getConfigurationFromDomain, getContractFromFreight, getContractRouteFromFreight, getCookies, getCteDateRange, getCteLane, getCteLaneFeesTotal, getCtesFeesResult, getDataFromToken, getFilteredFeesToAudit, getFormattedFreightPlaceName, getLaneFeesToCalc, getLaneFromRatecard, getNormalizedCityName, getRatecardFromCte, getRouteDeliveryTimeFromFreight, getRouteOnTimeFromFreight, getTariffVariations, isTariffMatch, matchAllTariffs, normalizeString, parseAliases, setFormattedDatesInObjects, verifyConditionalFee, verifyDefaultFees };
|
|
8069
8100
|
//# sourceMappingURL=index.esm.js.map
|
package/build/index.esm.js.gz
CHANGED
|
Binary file
|