b2m-utils 0.0.310 → 0.0.311
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/enums/FeeEnum.d.ts +2 -1
- package/build/enums/RatecardFeeConfigKeyEnum.d.ts +2 -1
- package/build/functions/buildFeeExclusionsMap/index.d.ts +7 -0
- package/build/functions/calculateFeesCacheWithDetails/index.d.ts +33 -0
- package/build/functions/calculateTotalPercentageFees/index.d.ts +2 -1
- package/build/functions/index.d.ts +2 -0
- package/build/index.esm.js +133 -6
- package/build/index.esm.js.gz +0 -0
- package/build/index.esm.js.map +1 -1
- package/build/index.js +135 -5
- package/build/index.js.gz +0 -0
- package/build/index.js.map +1 -1
- package/package.json +1 -1
package/build/index.js
CHANGED
|
@@ -191,6 +191,7 @@ exports.FeeEnum = void 0;
|
|
|
191
191
|
FeeEnum[FeeEnum["TRT_PERCENTAGE_TOTAL"] = 169] = "TRT_PERCENTAGE_TOTAL";
|
|
192
192
|
FeeEnum[FeeEnum["TDA_PERCENTAGE_TOTAL"] = 170] = "TDA_PERCENTAGE_TOTAL";
|
|
193
193
|
FeeEnum[FeeEnum["TDE_PERCENTAGE_TOTAL"] = 171] = "TDE_PERCENTAGE_TOTAL";
|
|
194
|
+
FeeEnum[FeeEnum["APPOINTMENT_BY_INVOICE_QUANTITY"] = 172] = "APPOINTMENT_BY_INVOICE_QUANTITY";
|
|
194
195
|
})(exports.FeeEnum || (exports.FeeEnum = {}));
|
|
195
196
|
|
|
196
197
|
exports.FreightRegionEnum = void 0;
|
|
@@ -255,6 +256,8 @@ exports.RatecardFeeConfigKeyEnum = void 0;
|
|
|
255
256
|
(function (RatecardFeeConfigKeyEnum) {
|
|
256
257
|
// Configurações de fração de peso
|
|
257
258
|
RatecardFeeConfigKeyEnum["MIN_WEIGHT_FOR_FRACTION"] = "min_weight_for_fraction";
|
|
259
|
+
// Configurações de exclusão de fees
|
|
260
|
+
RatecardFeeConfigKeyEnum["EXCLUDE_FROM_TOTAL_PERCENTAGE"] = "exclude_from_total_percentage";
|
|
258
261
|
})(exports.RatecardFeeConfigKeyEnum || (exports.RatecardFeeConfigKeyEnum = {}));
|
|
259
262
|
|
|
260
263
|
exports.RatecardFeeConfigTypeEnum = void 0;
|
|
@@ -417,6 +420,33 @@ var applyRedeliveryMultiplier = function (total, cte) {
|
|
|
417
420
|
};
|
|
418
421
|
};
|
|
419
422
|
|
|
423
|
+
/**
|
|
424
|
+
* Constrói um Map de exclusões de fees baseado nas configurações do tarifário
|
|
425
|
+
* @param fees - Array de fees do tarifário
|
|
426
|
+
* @returns Map onde a chave é o feeId e o valor é um Set de feeIds excluídos da base de cálculo
|
|
427
|
+
*/
|
|
428
|
+
var buildFeeExclusionsMap = function (fees) {
|
|
429
|
+
var exclusionsMap = new Map();
|
|
430
|
+
fees.forEach(function (fee) {
|
|
431
|
+
var _a;
|
|
432
|
+
if (!((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.id) || !fee.fee.RatecardFeeConfig)
|
|
433
|
+
return;
|
|
434
|
+
// Procurar pela configuração de exclusão
|
|
435
|
+
var excludeConfig = fee.fee.RatecardFeeConfig.find(function (config) { var _a; return ((_a = config.feeConfig) === null || _a === void 0 ? void 0 : _a.configKey) === exports.RatecardFeeConfigKeyEnum.EXCLUDE_FROM_TOTAL_PERCENTAGE; });
|
|
436
|
+
if (excludeConfig === null || excludeConfig === void 0 ? void 0 : excludeConfig.configValue) {
|
|
437
|
+
// Parse dos IDs separados por vírgula
|
|
438
|
+
var excludedIds = excludeConfig.configValue
|
|
439
|
+
.split(',')
|
|
440
|
+
.map(function (id) { return parseInt(id.trim(), 10); })
|
|
441
|
+
.filter(function (id) { return !isNaN(id); });
|
|
442
|
+
if (excludedIds.length > 0) {
|
|
443
|
+
exclusionsMap.set(fee.fee.id, new Set(excludedIds));
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
return exclusionsMap;
|
|
448
|
+
};
|
|
449
|
+
|
|
420
450
|
/******************************************************************************
|
|
421
451
|
Copyright (c) Microsoft Corporation.
|
|
422
452
|
|
|
@@ -993,8 +1023,9 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug, allFeesForCal
|
|
|
993
1023
|
* para resolver a interdependência entre elas (ex: TRT e GAC)
|
|
994
1024
|
* @param currentValues - Opcional: Map de feeId -> valor atual (para usar valores já calculados/editados)
|
|
995
1025
|
* @param manuallyEditedFeeIds - Opcional: Set de feeIds que foram editadas manualmente e não devem ser recalculadas
|
|
1026
|
+
* @param feeExclusions - Opcional: Map de feeId -> Set de feeIds excluídos da base de cálculo
|
|
996
1027
|
*/
|
|
997
|
-
var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, currentValues, manuallyEditedFeeIds) {
|
|
1028
|
+
var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, currentValues, manuallyEditedFeeIds, feeExclusions) {
|
|
998
1029
|
var results = new Map();
|
|
999
1030
|
// Separar fees em dois grupos
|
|
1000
1031
|
// IMPORTANTE: ICMS não deve ser calculado aqui, ele tem tratamento especial no calculateFee
|
|
@@ -1055,11 +1086,12 @@ var calculateTotalPercentageFees = function (allFees, cte, allFeesForCalc, curre
|
|
|
1055
1086
|
results.set(pf.fee.id, __assign(__assign({}, pf), { totalFee: manualValue, totalToCalc: manualValue, resultFee: "".concat(convertNumberToCurrency(manualValue, 'pt-br'), " (editado manualmente)") }));
|
|
1056
1087
|
return;
|
|
1057
1088
|
}
|
|
1058
|
-
// Calcular base: fees não-percentuais + outras fees percentuais (exceto ela mesma)
|
|
1089
|
+
// Calcular base: fees não-percentuais + outras fees percentuais (exceto ela mesma e excluídas)
|
|
1090
|
+
var excludedFeeIds = (feeExclusions === null || feeExclusions === void 0 ? void 0 : feeExclusions.get(pf.fee.id)) || new Set();
|
|
1059
1091
|
var basePercentage = Array.from(previousValues.entries())
|
|
1060
1092
|
.filter(function (_a) {
|
|
1061
1093
|
var feeId = _a[0];
|
|
1062
|
-
return feeId !== pf.fee.id;
|
|
1094
|
+
return feeId !== pf.fee.id && !excludedFeeIds.has(feeId);
|
|
1063
1095
|
})
|
|
1064
1096
|
.reduce(function (sum, _a) {
|
|
1065
1097
|
var value = _a[1];
|
|
@@ -1136,6 +1168,98 @@ var calculateIcms = function (totalFromAllFees, icmsRate) {
|
|
|
1136
1168
|
};
|
|
1137
1169
|
};
|
|
1138
1170
|
|
|
1171
|
+
/**
|
|
1172
|
+
* Calcula todas as fees em 3 passadas (normais, TOTAL_PERCENTAGE, ICMS) e retorna um cache detalhado
|
|
1173
|
+
* com totalFee, totalToCalc e resultFee para cada fee.
|
|
1174
|
+
*
|
|
1175
|
+
* Esta função centraliza a lógica de cálculo que estava duplicada em vários lugares do ModalCte.
|
|
1176
|
+
*/
|
|
1177
|
+
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;
|
|
1179
|
+
var cache = {};
|
|
1180
|
+
// Filtrar fees não excluídas
|
|
1181
|
+
var nonExcludedFees = fees.filter(function (_, idx) { return !excludedIndexes.has(idx); });
|
|
1182
|
+
// Construir mapa de exclusões a partir das configurações
|
|
1183
|
+
var feeExclusions = buildFeeExclusionsMap(fees);
|
|
1184
|
+
// PASSADA 1: Calcular fees normais (não TOTAL_PERCENTAGE, não ICMS)
|
|
1185
|
+
fees.forEach(function (fee, idx) {
|
|
1186
|
+
var _a, _b;
|
|
1187
|
+
if (excludedIndexes.has(idx))
|
|
1188
|
+
return;
|
|
1189
|
+
if (((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) !== exports.FeeCalculationTypeEnum.TOTAL_PERCENTAGE &&
|
|
1190
|
+
((_b = fee.fee) === null || _b === void 0 ? void 0 : _b.id) !== exports.FeeEnum.ICMS) {
|
|
1191
|
+
var calculated = calculateFee(fee, cte, nonExcludedFees, true, allFeesForCalc);
|
|
1192
|
+
if (calculated) {
|
|
1193
|
+
cache[idx] = {
|
|
1194
|
+
totalFee: Number((calculated.totalFee || 0).toFixed(2)),
|
|
1195
|
+
totalToCalc: Number((calculated.totalToCalc || 0).toFixed(2)),
|
|
1196
|
+
resultFee: calculated.resultFee || '',
|
|
1197
|
+
};
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
});
|
|
1201
|
+
// PASSADA 2: Calcular TOTAL_PERCENTAGE de forma iterativa com exclusões
|
|
1202
|
+
var percentageFeesCache = calculateTotalPercentageFees(nonExcludedFees, cte, allFeesForCalc, preCalculatedValues, manuallyEditedFeeIds, feeExclusions);
|
|
1203
|
+
fees.forEach(function (fee, idx) {
|
|
1204
|
+
var _a, _b;
|
|
1205
|
+
if (excludedIndexes.has(idx))
|
|
1206
|
+
return;
|
|
1207
|
+
if (((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) === exports.FeeCalculationTypeEnum.TOTAL_PERCENTAGE && ((_b = fee.fee) === null || _b === void 0 ? void 0 : _b.id)) {
|
|
1208
|
+
var cachedResult = percentageFeesCache.get(fee.fee.id);
|
|
1209
|
+
if (cachedResult) {
|
|
1210
|
+
cache[idx] = {
|
|
1211
|
+
totalFee: Number((cachedResult.totalFee || 0).toFixed(2)),
|
|
1212
|
+
totalToCalc: Number((cachedResult.totalToCalc || 0).toFixed(2)),
|
|
1213
|
+
resultFee: cachedResult.resultFee || '',
|
|
1214
|
+
};
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
});
|
|
1218
|
+
// PASSADA 3: Calcular ICMS
|
|
1219
|
+
fees.forEach(function (fee, idx) {
|
|
1220
|
+
var _a;
|
|
1221
|
+
if (excludedIndexes.has(idx))
|
|
1222
|
+
return;
|
|
1223
|
+
if (((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.id) === exports.FeeEnum.ICMS) {
|
|
1224
|
+
var icmsRate = ((cte === null || cte === void 0 ? void 0 : cte.icmsIncidence) !== undefined && (cte === null || cte === void 0 ? void 0 : cte.icmsIncidence) !== null && !isNaN(+cte.icmsIncidence))
|
|
1225
|
+
? +(cte.icmsIncidence) / 100
|
|
1226
|
+
: fee.value || 0;
|
|
1227
|
+
var valuesWithoutIcms = Object.entries(cache)
|
|
1228
|
+
.filter(function (_a) {
|
|
1229
|
+
var _b;
|
|
1230
|
+
var key = _a[0];
|
|
1231
|
+
var feeIdx = Number(key);
|
|
1232
|
+
var f = fees[feeIdx];
|
|
1233
|
+
return ((_b = f.fee) === null || _b === void 0 ? void 0 : _b.id) !== exports.FeeEnum.ICMS;
|
|
1234
|
+
})
|
|
1235
|
+
.map(function (_a) {
|
|
1236
|
+
var calcFee = _a[1];
|
|
1237
|
+
return (calcFee === null || calcFee === void 0 ? void 0 : calcFee.totalToCalc) || 0;
|
|
1238
|
+
})
|
|
1239
|
+
.filter(function (val) { return val > 0; });
|
|
1240
|
+
var icmsCalculation = calculateIcms(valuesWithoutIcms, icmsRate);
|
|
1241
|
+
if (icmsCalculation.isValid) {
|
|
1242
|
+
var resultFee = icmsCalculation.totalValueFromAllFees && icmsCalculation.valueToDivide
|
|
1243
|
+
? "(".concat(icmsCalculation.totalValueFromAllFees.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' }), " / ").concat(icmsCalculation.valueToDivide, ") x ").concat(icmsRate, " = ").concat(icmsCalculation.total.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' }))
|
|
1244
|
+
: '';
|
|
1245
|
+
cache[idx] = {
|
|
1246
|
+
totalFee: Number(icmsCalculation.total.toFixed(2)),
|
|
1247
|
+
totalToCalc: Number(icmsCalculation.total.toFixed(2)),
|
|
1248
|
+
resultFee: resultFee,
|
|
1249
|
+
};
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
});
|
|
1253
|
+
return cache;
|
|
1254
|
+
};
|
|
1255
|
+
/**
|
|
1256
|
+
* Calcula o total de fees usando calculateFeesCacheWithDetails
|
|
1257
|
+
*/
|
|
1258
|
+
var calculateFeesTotalFromCache = function (options) {
|
|
1259
|
+
var cache = calculateFeesCacheWithDetails(options);
|
|
1260
|
+
return Object.values(cache).reduce(function (sum, calcFee) { return sum + ((calcFee === null || calcFee === void 0 ? void 0 : calcFee.totalToCalc) || 0); }, 0);
|
|
1261
|
+
};
|
|
1262
|
+
|
|
1139
1263
|
/**
|
|
1140
1264
|
* Calcula o total de fees de forma consistente, usando cálculo iterativo para TOTAL_PERCENTAGE
|
|
1141
1265
|
* @param fees - Array de fees a calcular
|
|
@@ -1147,6 +1271,8 @@ var calculateFeesTotal = function (fees, cte, allFeesForCalc) {
|
|
|
1147
1271
|
if (!fees || fees.length === 0)
|
|
1148
1272
|
return 0;
|
|
1149
1273
|
var cache = {};
|
|
1274
|
+
// Construir mapa de exclusões a partir das configurações
|
|
1275
|
+
var feeExclusions = buildFeeExclusionsMap(fees);
|
|
1150
1276
|
// PASSADA 1: Calcular fees normais (não TOTAL_PERCENTAGE, não ICMS)
|
|
1151
1277
|
fees.forEach(function (fee, idx) {
|
|
1152
1278
|
var _a, _b;
|
|
@@ -1155,8 +1281,8 @@ var calculateFeesTotal = function (fees, cte, allFeesForCalc) {
|
|
|
1155
1281
|
cache[idx] = calculateFee(fee, cte, fees, true, allFeesForCalc);
|
|
1156
1282
|
}
|
|
1157
1283
|
});
|
|
1158
|
-
// PASSADA 2: Calcular TOTAL_PERCENTAGE de forma iterativa
|
|
1159
|
-
var percentageFeesCache = calculateTotalPercentageFees(fees, cte, allFeesForCalc);
|
|
1284
|
+
// PASSADA 2: Calcular TOTAL_PERCENTAGE de forma iterativa com exclusões
|
|
1285
|
+
var percentageFeesCache = calculateTotalPercentageFees(fees, cte, allFeesForCalc, undefined, undefined, feeExclusions);
|
|
1160
1286
|
fees.forEach(function (fee, idx) {
|
|
1161
1287
|
var _a, _b;
|
|
1162
1288
|
if (((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) === exports.FeeCalculationTypeEnum.TOTAL_PERCENTAGE &&
|
|
@@ -1273,6 +1399,7 @@ var getFilteredFeesToAudit = function (_a) {
|
|
|
1273
1399
|
exports.FeeEnum.COLLECT_PERCENTAGE_TOTAL,
|
|
1274
1400
|
exports.FeeEnum.COLLECT_REVERSE_PERCENTAGE_TOTAL,
|
|
1275
1401
|
exports.FeeEnum.APPOINTMENT_MIN_VALUE,
|
|
1402
|
+
exports.FeeEnum.APPOINTMENT_BY_INVOICE_QUANTITY,
|
|
1276
1403
|
];
|
|
1277
1404
|
if (collectAndAppointmentFees.includes((_a = i.fee) === null || _a === void 0 ? void 0 : _a.id)) {
|
|
1278
1405
|
switch ((_b = i.fee) === null || _b === void 0 ? void 0 : _b.id) {
|
|
@@ -8287,8 +8414,11 @@ exports.TARIFF_MAPPING = TARIFF_MAPPING;
|
|
|
8287
8414
|
exports.addTariffMapping = addTariffMapping;
|
|
8288
8415
|
exports.applyRedeliveryMultiplier = applyRedeliveryMultiplier;
|
|
8289
8416
|
exports.buildDynamicMapping = buildDynamicMapping;
|
|
8417
|
+
exports.buildFeeExclusionsMap = buildFeeExclusionsMap;
|
|
8290
8418
|
exports.calculateFee = calculateFee;
|
|
8419
|
+
exports.calculateFeesCacheWithDetails = calculateFeesCacheWithDetails;
|
|
8291
8420
|
exports.calculateFeesTotal = calculateFeesTotal;
|
|
8421
|
+
exports.calculateFeesTotalFromCache = calculateFeesTotalFromCache;
|
|
8292
8422
|
exports.calculateIcms = calculateIcms;
|
|
8293
8423
|
exports.calculateTotalPercentageFees = calculateTotalPercentageFees;
|
|
8294
8424
|
exports.convertNumberToCurrency = convertNumberToCurrency;
|
package/build/index.js.gz
CHANGED
|
Binary file
|