b2m-utils 0.0.289 → 0.0.290
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 +87 -64
- package/build/index.esm.js.gz +0 -0
- package/build/index.esm.js.map +1 -1
- package/build/index.js +87 -63
- 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
|
@@ -579,71 +579,9 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug, allFeesForCal
|
|
|
579
579
|
case FeeCalculationTypeEnum.TOTAL_PERCENTAGE:
|
|
580
580
|
if (allFees && Array.isArray(allFees) && (allFees === null || allFees === void 0 ? void 0 : allFees.length) > 0) {
|
|
581
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
|
-
|
|
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) {
|
|
582
|
+
var filteredFeesResults = filterSiblingFees(filteredFees.map(function (it) {
|
|
587
583
|
return calculateFee(it, cte, allFees, debug, allFeesForCalc);
|
|
588
584
|
}));
|
|
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
585
|
var totalFromAllFees = filteredFeesResults.map(function (result) {
|
|
648
586
|
if (result === null || result === void 0 ? void 0 : result.totalToCalc) {
|
|
649
587
|
return result.totalToCalc;
|
|
@@ -1051,6 +989,91 @@ var calculateIcms = function (totalFromAllFees, icmsRate) {
|
|
|
1051
989
|
};
|
|
1052
990
|
};
|
|
1053
991
|
|
|
992
|
+
/**
|
|
993
|
+
* Calcula todas as fees do tipo TOTAL_PERCENTAGE de forma iterativa
|
|
994
|
+
* para resolver a interdependência entre elas (ex: TRT e GAC)
|
|
995
|
+
*/
|
|
996
|
+
var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc) {
|
|
997
|
+
var results = new Map();
|
|
998
|
+
// Separar fees em dois grupos
|
|
999
|
+
var nonPercentageFees = allFees.filter(function (i) { var _a; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) !== FeeCalculationTypeEnum.TOTAL_PERCENTAGE; });
|
|
1000
|
+
var percentageFees = allFees.filter(function (i) { var _a; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) === FeeCalculationTypeEnum.TOTAL_PERCENTAGE; });
|
|
1001
|
+
// Se não houver fees percentuais, retornar vazio
|
|
1002
|
+
if (percentageFees.length === 0) {
|
|
1003
|
+
return results;
|
|
1004
|
+
}
|
|
1005
|
+
console.log("\n\uD83D\uDD04 Calculando ".concat(percentageFees.length, " fees TOTAL_PERCENTAGE de forma iterativa"));
|
|
1006
|
+
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(', ')));
|
|
1007
|
+
// Calcular fees não-percentuais (base fixa)
|
|
1008
|
+
var nonPercentageResults = filterSiblingFees(nonPercentageFees.map(function (it) {
|
|
1009
|
+
return calculateFee(it, cte, allFees, false, allFeesForCalc);
|
|
1010
|
+
}));
|
|
1011
|
+
var baseNonPercentage = nonPercentageResults.reduce(function (sum, r) { return sum + (r.totalToCalc || 0); }, 0);
|
|
1012
|
+
console.log("\uD83D\uDCCA Base n\u00E3o-percentual: R$ ".concat(baseNonPercentage.toFixed(2)));
|
|
1013
|
+
// Calcular fees percentuais iterativamente até convergir
|
|
1014
|
+
var maxIterations = 10;
|
|
1015
|
+
var tolerance = 0.01; // Tolerância de 1 centavo
|
|
1016
|
+
// Inicializar com valores zerados
|
|
1017
|
+
var previousValues = new Map();
|
|
1018
|
+
percentageFees.forEach(function (pf) { return previousValues.set(pf.fee.id, 0); });
|
|
1019
|
+
var _loop_1 = function (iteration) {
|
|
1020
|
+
console.log("\n--- Itera\u00E7\u00E3o ".concat(iteration + 1, " ---"));
|
|
1021
|
+
// Calcular cada fee percentual com base nos valores atuais das outras
|
|
1022
|
+
percentageFees.forEach(function (pf) {
|
|
1023
|
+
var _a;
|
|
1024
|
+
// Calcular base: fees não-percentuais + outras fees percentuais (exceto ela mesma)
|
|
1025
|
+
var basePercentage = Array.from(previousValues.entries())
|
|
1026
|
+
.filter(function (_a) {
|
|
1027
|
+
var feeId = _a[0];
|
|
1028
|
+
return feeId !== pf.fee.id;
|
|
1029
|
+
})
|
|
1030
|
+
.reduce(function (sum, _a) {
|
|
1031
|
+
var value = _a[1];
|
|
1032
|
+
return sum + value;
|
|
1033
|
+
}, 0);
|
|
1034
|
+
var baseValue = baseNonPercentage + basePercentage;
|
|
1035
|
+
var percentValue = (+pf.value / 100);
|
|
1036
|
+
var calculatedValue = baseValue * percentValue;
|
|
1037
|
+
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)));
|
|
1038
|
+
previousValues.set(pf.fee.id, calculatedValue);
|
|
1039
|
+
// Armazenar resultado
|
|
1040
|
+
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')) }));
|
|
1041
|
+
});
|
|
1042
|
+
// Verificar convergência
|
|
1043
|
+
var hasConverged = true;
|
|
1044
|
+
var currentIteration = iteration;
|
|
1045
|
+
if (currentIteration > 0) {
|
|
1046
|
+
percentageFees.forEach(function (pf) {
|
|
1047
|
+
var currentValue = previousValues.get(pf.fee.id) || 0;
|
|
1048
|
+
var result = results.get(pf.fee.id);
|
|
1049
|
+
var prevValue = (result === null || result === void 0 ? void 0 : result.totalFee) || 0;
|
|
1050
|
+
var diff = Math.abs(currentValue - prevValue);
|
|
1051
|
+
if (diff > tolerance) {
|
|
1052
|
+
hasConverged = false;
|
|
1053
|
+
}
|
|
1054
|
+
});
|
|
1055
|
+
}
|
|
1056
|
+
else {
|
|
1057
|
+
hasConverged = false;
|
|
1058
|
+
}
|
|
1059
|
+
if (hasConverged) {
|
|
1060
|
+
console.log("\u2705 Convergiu na itera\u00E7\u00E3o ".concat(iteration + 1, "!"));
|
|
1061
|
+
return "break";
|
|
1062
|
+
}
|
|
1063
|
+
};
|
|
1064
|
+
for (var iteration = 0; iteration < maxIterations; iteration++) {
|
|
1065
|
+
var state_1 = _loop_1(iteration);
|
|
1066
|
+
if (state_1 === "break")
|
|
1067
|
+
break;
|
|
1068
|
+
}
|
|
1069
|
+
console.log("\n\uD83D\uDCC8 Resultado final das fees percentuais:");
|
|
1070
|
+
results.forEach(function (result, feeId) {
|
|
1071
|
+
var _a;
|
|
1072
|
+
console.log(" ".concat((_a = result.fee) === null || _a === void 0 ? void 0 : _a.name, " (").concat(feeId, "): R$ ").concat((result.totalFee || 0).toFixed(2)));
|
|
1073
|
+
});
|
|
1074
|
+
return results;
|
|
1075
|
+
};
|
|
1076
|
+
|
|
1054
1077
|
var verifyConditionalFee = function (conditionalFeeToVerify, cte) {
|
|
1055
1078
|
var _a, _b;
|
|
1056
1079
|
if (conditionalFeeToVerify === null || conditionalFeeToVerify === void 0 ? void 0 : conditionalFeeToVerify.id) {
|
|
@@ -8065,5 +8088,5 @@ var isTariffMatch = function (chargedName, auditedTariffName) {
|
|
|
8065
8088
|
});
|
|
8066
8089
|
};
|
|
8067
8090
|
|
|
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 };
|
|
8091
|
+
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
8092
|
//# sourceMappingURL=index.esm.js.map
|
package/build/index.esm.js.gz
CHANGED
|
Binary file
|