b2m-utils 0.0.277 → 0.0.278

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.js CHANGED
@@ -1283,9 +1283,14 @@ var getLaneFromRatecard = function (cte) {
1283
1283
  var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1284
1284
  var _a, _b, _c;
1285
1285
  var results = [];
1286
- // Separar ICMS das outras fees
1286
+ // Separar fees por tipo: normais, TOTAL_PERCENTAGE e ICMS
1287
1287
  var icmsFee = feesToCalc.find(function (f) { var _a; return ((_a = f.fee) === null || _a === void 0 ? void 0 : _a.id) === exports.FeeEnum.ICMS; });
1288
- var nonIcmsFees = feesToCalc.filter(function (f) { var _a; return ((_a = f.fee) === null || _a === void 0 ? void 0 : _a.id) !== exports.FeeEnum.ICMS; });
1288
+ var totalPercentageFees = feesToCalc.filter(function (f) { var _a; return ((_a = f.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) === exports.FeeCalculationTypeEnum.TOTAL_PERCENTAGE; });
1289
+ var normalFees = feesToCalc.filter(function (f) {
1290
+ var _a, _b;
1291
+ return ((_a = f.fee) === null || _a === void 0 ? void 0 : _a.id) !== exports.FeeEnum.ICMS &&
1292
+ ((_b = f.fee) === null || _b === void 0 ? void 0 : _b.feeCalculationTypeId) !== exports.FeeCalculationTypeEnum.TOTAL_PERCENTAGE;
1293
+ });
1289
1294
  var _loop_1 = function (item) {
1290
1295
  var feeTotal = calculateFee(item, cte, feesToCalc, false, allFeesForCalc);
1291
1296
  if (feeTotal === null || feeTotal === void 0 ? void 0 : feeTotal.totalFee) {
@@ -1296,8 +1301,8 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1296
1301
  var siblingFees = feesToCalc === null || feesToCalc === void 0 ? void 0 : feesToCalc.filter(function (i) { var _a, _b; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.parentId) === fee_1.parentId && ((_b = i.fee) === null || _b === void 0 ? void 0 : _b.id) !== fee_1.id; });
1297
1302
  if (siblingFees && (siblingFees === null || siblingFees === void 0 ? void 0 : siblingFees.length) > 0) {
1298
1303
  var siblingFeesResults = [];
1299
- for (var _d = 0, siblingFees_1 = siblingFees; _d < siblingFees_1.length; _d++) {
1300
- var item_1 = siblingFees_1[_d];
1304
+ for (var _e = 0, siblingFees_1 = siblingFees; _e < siblingFees_1.length; _e++) {
1305
+ var item_1 = siblingFees_1[_e];
1301
1306
  var result = calculateFee(item_1, cte, feesToCalc, false, allFeesForCalc);
1302
1307
  siblingFeesResults.push(result);
1303
1308
  }
@@ -1324,12 +1329,30 @@ var getCtesFeesResult = function (feesToCalc, cte, allFeesForCalc) {
1324
1329
  }
1325
1330
  }
1326
1331
  };
1327
- // PRIMEIRA PASSADA: Calcular todas as fees exceto ICMS
1328
- for (var _i = 0, nonIcmsFees_1 = nonIcmsFees; _i < nonIcmsFees_1.length; _i++) {
1329
- var item = nonIcmsFees_1[_i];
1332
+ // PASSADA 1: Calcular fees normais (não TOTAL_PERCENTAGE, não ICMS)
1333
+ for (var _i = 0, normalFees_1 = normalFees; _i < normalFees_1.length; _i++) {
1334
+ var item = normalFees_1[_i];
1330
1335
  _loop_1(item);
1331
1336
  }
1332
- // SEGUNDA PASSADA: Calcular ICMS com base nos resultadosfiltrados
1337
+ // PASSADA 2: Calcular fees TOTAL_PERCENTAGE usando a soma das fees normais calculadas
1338
+ for (var _d = 0, totalPercentageFees_1 = totalPercentageFees; _d < totalPercentageFees_1.length; _d++) {
1339
+ var item = totalPercentageFees_1[_d];
1340
+ // Calcular soma das fees normais (excluindo TOTAL_PERCENTAGE e ICMS)
1341
+ var totalWithoutPercentageFees = results
1342
+ .filter(function (r) { return r.total > 0; })
1343
+ .reduce(function (sum, r) { return sum + r.total; }, 0);
1344
+ var percentage = item.value || 0;
1345
+ var calculatedValue = (totalWithoutPercentageFees * percentage) / 100;
1346
+ if (!isNaN(+calculatedValue) && calculatedValue > 0) {
1347
+ results.push({
1348
+ total: +(calculatedValue).toFixed(2),
1349
+ resultFee: "".concat(convertNumberToCurrency(totalWithoutPercentageFees, 'pt-br'), " x ").concat(percentage, "% = ").concat(convertNumberToCurrency(calculatedValue, 'pt-br')),
1350
+ feeValue: percentage,
1351
+ feeId: item.feeId,
1352
+ });
1353
+ }
1354
+ }
1355
+ // PASSADA 3: Calcular ICMS com base em todas as fees anteriores (normais + TOTAL_PERCENTAGE)
1333
1356
  if (icmsFee) {
1334
1357
  // Coletar apenas os valores das fees que realmente contam (total > 0)
1335
1358
  var valuesForIcms = results
package/build/index.js.gz CHANGED
Binary file