b2m-utils 0.0.311 → 0.0.313
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/buildFeeExclusionsMap/index.d.ts +3 -3
- package/build/functions/calculateFeesCacheWithDetails/index.d.ts +3 -1
- package/build/functions/calculateFeesTotal/index.d.ts +3 -2
- package/build/index.esm.js +108 -19
- package/build/index.esm.js.gz +0 -0
- package/build/index.esm.js.map +1 -1
- package/build/index.js +108 -19
- package/build/index.js.gz +0 -0
- package/build/index.js.map +1 -1
- package/package.json +1 -1
package/build/index.js
CHANGED
|
@@ -422,28 +422,57 @@ var applyRedeliveryMultiplier = function (total, cte) {
|
|
|
422
422
|
|
|
423
423
|
/**
|
|
424
424
|
* Constrói um Map de exclusões de fees baseado nas configurações do tarifário
|
|
425
|
-
* @param
|
|
425
|
+
* @param ratecard - Tarifário contendo as configurações de fees
|
|
426
426
|
* @returns Map onde a chave é o feeId e o valor é um Set de feeIds excluídos da base de cálculo
|
|
427
427
|
*/
|
|
428
|
-
var buildFeeExclusionsMap = function (
|
|
428
|
+
var buildFeeExclusionsMap = function (ratecard) {
|
|
429
|
+
var _a;
|
|
429
430
|
var exclusionsMap = new Map();
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
431
|
+
console.log('[buildFeeExclusionsMap] Iniciando construção do mapa de exclusões', {
|
|
432
|
+
ratecardId: ratecard === null || ratecard === void 0 ? void 0 : ratecard.id,
|
|
433
|
+
hasConfigs: !!(ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardFeeConfig),
|
|
434
|
+
configsCount: ((_a = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardFeeConfig) === null || _a === void 0 ? void 0 : _a.length) || 0
|
|
435
|
+
});
|
|
436
|
+
if (!(ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardFeeConfig)) {
|
|
437
|
+
console.log('[buildFeeExclusionsMap] Nenhuma configuração encontrada, retornando mapa vazio');
|
|
438
|
+
return exclusionsMap;
|
|
439
|
+
}
|
|
440
|
+
// Iterar sobre todas as configurações de fees do ratecard
|
|
441
|
+
ratecard.RatecardFeeConfig.forEach(function (config) {
|
|
442
|
+
var _a, _b;
|
|
443
|
+
// Verificar se é uma configuração de exclusão
|
|
444
|
+
if (((_a = config.feeConfig) === null || _a === void 0 ? void 0 : _a.configKey) === exports.RatecardFeeConfigKeyEnum.EXCLUDE_FROM_TOTAL_PERCENTAGE &&
|
|
445
|
+
config.configValue &&
|
|
446
|
+
config.feeId) {
|
|
447
|
+
console.log('[buildFeeExclusionsMap] Configuração de exclusão encontrada', {
|
|
448
|
+
feeId: config.feeId,
|
|
449
|
+
feeName: (_b = config.fee) === null || _b === void 0 ? void 0 : _b.name,
|
|
450
|
+
configValue: config.configValue
|
|
451
|
+
});
|
|
437
452
|
// Parse dos IDs separados por vírgula
|
|
438
|
-
var excludedIds =
|
|
453
|
+
var excludedIds = config.configValue
|
|
439
454
|
.split(',')
|
|
440
455
|
.map(function (id) { return parseInt(id.trim(), 10); })
|
|
441
456
|
.filter(function (id) { return !isNaN(id); });
|
|
442
457
|
if (excludedIds.length > 0) {
|
|
443
|
-
exclusionsMap.set(
|
|
458
|
+
exclusionsMap.set(config.feeId, new Set(excludedIds));
|
|
459
|
+
console.log('[buildFeeExclusionsMap] Exclusões adicionadas ao mapa', {
|
|
460
|
+
feeId: config.feeId,
|
|
461
|
+
excludedIds: Array.from(excludedIds)
|
|
462
|
+
});
|
|
444
463
|
}
|
|
445
464
|
}
|
|
446
465
|
});
|
|
466
|
+
console.log('[buildFeeExclusionsMap] Mapa de exclusões construído', {
|
|
467
|
+
totalExclusions: exclusionsMap.size,
|
|
468
|
+
exclusionsMap: Array.from(exclusionsMap.entries()).map(function (_a) {
|
|
469
|
+
var feeId = _a[0], excludedIds = _a[1];
|
|
470
|
+
return ({
|
|
471
|
+
feeId: feeId,
|
|
472
|
+
excludedIds: Array.from(excludedIds)
|
|
473
|
+
});
|
|
474
|
+
})
|
|
475
|
+
});
|
|
447
476
|
return exclusionsMap;
|
|
448
477
|
};
|
|
449
478
|
|
|
@@ -1046,17 +1075,31 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, curre
|
|
|
1046
1075
|
// Calcular base das fees não-percentuais
|
|
1047
1076
|
// Se temos currentValues, usar eles; senão calcular normalmente
|
|
1048
1077
|
var baseNonPercentage = 0;
|
|
1078
|
+
var nonPercentageDetails = [];
|
|
1049
1079
|
if (currentValues) {
|
|
1050
1080
|
// Usar valores atuais das fees não-percentuais
|
|
1051
1081
|
baseNonPercentage = nonPercentageFees.reduce(function (sum, fee) {
|
|
1052
1082
|
var _a;
|
|
1053
1083
|
var value = ((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.id) ? currentValues.get(fee.fee.id) : undefined;
|
|
1054
1084
|
if (value !== undefined) {
|
|
1085
|
+
nonPercentageDetails.push({
|
|
1086
|
+
feeId: fee.fee.id,
|
|
1087
|
+
feeName: fee.fee.name || 'Unknown',
|
|
1088
|
+
value: value
|
|
1089
|
+
});
|
|
1055
1090
|
return sum + value;
|
|
1056
1091
|
}
|
|
1057
1092
|
// Se não tem valor atual, calcular
|
|
1058
1093
|
var calculated = calculateFee(fee, cte, allFees, false, allFeesForCalc);
|
|
1059
|
-
|
|
1094
|
+
var calcValue = (calculated === null || calculated === void 0 ? void 0 : calculated.totalToCalc) || 0;
|
|
1095
|
+
if (calcValue > 0) {
|
|
1096
|
+
nonPercentageDetails.push({
|
|
1097
|
+
feeId: fee.fee.id,
|
|
1098
|
+
feeName: fee.fee.name || 'Unknown',
|
|
1099
|
+
value: calcValue
|
|
1100
|
+
});
|
|
1101
|
+
}
|
|
1102
|
+
return sum + calcValue;
|
|
1060
1103
|
}, 0);
|
|
1061
1104
|
}
|
|
1062
1105
|
else {
|
|
@@ -1064,8 +1107,24 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, curre
|
|
|
1064
1107
|
var nonPercentageResults = filterSiblingFees(nonPercentageFees.map(function (it) {
|
|
1065
1108
|
return calculateFee(it, cte, allFees, false, allFeesForCalc);
|
|
1066
1109
|
}));
|
|
1067
|
-
baseNonPercentage = nonPercentageResults.reduce(function (sum, r) {
|
|
1110
|
+
baseNonPercentage = nonPercentageResults.reduce(function (sum, r) {
|
|
1111
|
+
var _a;
|
|
1112
|
+
var value = r.totalToCalc || 0;
|
|
1113
|
+
if (value > 0 && ((_a = r.fee) === null || _a === void 0 ? void 0 : _a.id)) {
|
|
1114
|
+
nonPercentageDetails.push({
|
|
1115
|
+
feeId: r.fee.id,
|
|
1116
|
+
feeName: r.fee.name || 'Unknown',
|
|
1117
|
+
value: value
|
|
1118
|
+
});
|
|
1119
|
+
}
|
|
1120
|
+
return sum + value;
|
|
1121
|
+
}, 0);
|
|
1068
1122
|
}
|
|
1123
|
+
console.log('[calculateTotalPercentageFees] Base não-percentual calculada', {
|
|
1124
|
+
baseNonPercentage: baseNonPercentage,
|
|
1125
|
+
nonPercentageFeesCount: nonPercentageFees.length,
|
|
1126
|
+
nonPercentageDetails: nonPercentageDetails
|
|
1127
|
+
});
|
|
1069
1128
|
// Calcular fees percentuais iterativamente até convergir
|
|
1070
1129
|
var maxIterations = 10;
|
|
1071
1130
|
var tolerance = 0.01; // Tolerância de 1 centavo
|
|
@@ -1088,6 +1147,14 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, curre
|
|
|
1088
1147
|
}
|
|
1089
1148
|
// Calcular base: fees não-percentuais + outras fees percentuais (exceto ela mesma e excluídas)
|
|
1090
1149
|
var excludedFeeIds = (feeExclusions === null || feeExclusions === void 0 ? void 0 : feeExclusions.get(pf.fee.id)) || new Set();
|
|
1150
|
+
console.log('[calculateTotalPercentageFees] Calculando fee TOTAL_PERCENTAGE', {
|
|
1151
|
+
feeId: pf.fee.id,
|
|
1152
|
+
feeName: pf.fee.name,
|
|
1153
|
+
percentValue: pf.value,
|
|
1154
|
+
hasExclusions: excludedFeeIds.size > 0,
|
|
1155
|
+
excludedFeeIds: Array.from(excludedFeeIds),
|
|
1156
|
+
iteration: iteration
|
|
1157
|
+
});
|
|
1091
1158
|
var basePercentage = Array.from(previousValues.entries())
|
|
1092
1159
|
.filter(function (_a) {
|
|
1093
1160
|
var feeId = _a[0];
|
|
@@ -1100,6 +1167,23 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, curre
|
|
|
1100
1167
|
var baseValue = baseNonPercentage + basePercentage;
|
|
1101
1168
|
var percentValue = (+pf.value / 100);
|
|
1102
1169
|
var calculatedValue = baseValue * percentValue;
|
|
1170
|
+
console.log('[calculateTotalPercentageFees] Base calculada', {
|
|
1171
|
+
feeId: pf.fee.id,
|
|
1172
|
+
feeName: pf.fee.name,
|
|
1173
|
+
baseNonPercentage: baseNonPercentage,
|
|
1174
|
+
basePercentage: basePercentage,
|
|
1175
|
+
baseValue: baseValue,
|
|
1176
|
+
percentValue: "".concat(pf.value, "%"),
|
|
1177
|
+
calculatedValue: calculatedValue,
|
|
1178
|
+
allPreviousValues: Array.from(previousValues.entries()).map(function (_a) {
|
|
1179
|
+
var id = _a[0], val = _a[1];
|
|
1180
|
+
return ({
|
|
1181
|
+
feeId: id,
|
|
1182
|
+
value: val,
|
|
1183
|
+
excluded: excludedFeeIds.has(id)
|
|
1184
|
+
});
|
|
1185
|
+
})
|
|
1186
|
+
});
|
|
1103
1187
|
previousValues.set(pf.fee.id, calculatedValue);
|
|
1104
1188
|
// Armazenar resultado
|
|
1105
1189
|
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')) }));
|
|
@@ -1175,12 +1259,14 @@ var calculateIcms = function (totalFromAllFees, icmsRate) {
|
|
|
1175
1259
|
* Esta função centraliza a lógica de cálculo que estava duplicada em vários lugares do ModalCte.
|
|
1176
1260
|
*/
|
|
1177
1261
|
var calculateFeesCacheWithDetails = function (options) {
|
|
1178
|
-
var fees = options.fees, cte = options.cte, allFeesForCalc = options.allFeesForCalc, _a = options.excludedIndexes, excludedIndexes = _a === void 0 ? new Set() : _a, preCalculatedValues = options.preCalculatedValues, manuallyEditedFeeIds = options.manuallyEditedFeeIds;
|
|
1262
|
+
var fees = options.fees, cte = options.cte, allFeesForCalc = options.allFeesForCalc, _a = options.excludedIndexes, excludedIndexes = _a === void 0 ? new Set() : _a, preCalculatedValues = options.preCalculatedValues, manuallyEditedFeeIds = options.manuallyEditedFeeIds, ratecard = options.ratecard;
|
|
1179
1263
|
var cache = {};
|
|
1180
1264
|
// Filtrar fees não excluídas
|
|
1181
1265
|
var nonExcludedFees = fees.filter(function (_, idx) { return !excludedIndexes.has(idx); });
|
|
1182
|
-
//
|
|
1183
|
-
var
|
|
1266
|
+
// Obter ratecard se não foi fornecido
|
|
1267
|
+
var ratecardToUse = ratecard || getRatecardFromCte(cte) || undefined;
|
|
1268
|
+
// Construir mapa de exclusões a partir das configurações do ratecard
|
|
1269
|
+
var feeExclusions = buildFeeExclusionsMap(ratecardToUse);
|
|
1184
1270
|
// PASSADA 1: Calcular fees normais (não TOTAL_PERCENTAGE, não ICMS)
|
|
1185
1271
|
fees.forEach(function (fee, idx) {
|
|
1186
1272
|
var _a, _b;
|
|
@@ -1265,14 +1351,17 @@ var calculateFeesTotalFromCache = function (options) {
|
|
|
1265
1351
|
* @param fees - Array de fees a calcular
|
|
1266
1352
|
* @param cte - CTE para cálculo
|
|
1267
1353
|
* @param allFeesForCalc - Todas as fees disponíveis para cálculo
|
|
1354
|
+
* @param ratecard - Tarifário (opcional, será extraído do CTE se não fornecido)
|
|
1268
1355
|
* @returns Total calculado
|
|
1269
1356
|
*/
|
|
1270
|
-
var calculateFeesTotal = function (fees, cte, allFeesForCalc) {
|
|
1357
|
+
var calculateFeesTotal = function (fees, cte, allFeesForCalc, ratecard) {
|
|
1271
1358
|
if (!fees || fees.length === 0)
|
|
1272
1359
|
return 0;
|
|
1273
1360
|
var cache = {};
|
|
1274
|
-
//
|
|
1275
|
-
var
|
|
1361
|
+
// Obter ratecard se não foi fornecido
|
|
1362
|
+
var ratecardToUse = ratecard || getRatecardFromCte(cte) || undefined;
|
|
1363
|
+
// Construir mapa de exclusões a partir das configurações do ratecard
|
|
1364
|
+
var feeExclusions = buildFeeExclusionsMap(ratecardToUse);
|
|
1276
1365
|
// PASSADA 1: Calcular fees normais (não TOTAL_PERCENTAGE, não ICMS)
|
|
1277
1366
|
fees.forEach(function (fee, idx) {
|
|
1278
1367
|
var _a, _b;
|
package/build/index.js.gz
CHANGED
|
Binary file
|