b2m-utils 0.0.227 → 0.0.229

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
@@ -219,7 +219,7 @@ exports.TrackProcessProviderTypeEnum = void 0;
219
219
  })(exports.TrackProcessProviderTypeEnum || (exports.TrackProcessProviderTypeEnum = {}));
220
220
 
221
221
  var getRatecardFromCte = function (cte) {
222
- var _a, _b, _c;
222
+ var _a, _b, _c, _d, _e, _f, _g;
223
223
  var ratecard = null;
224
224
  if (((_a = cte.carrier) === null || _a === void 0 ? void 0 : _a.Ratecard) && ((_c = (_b = cte.carrier) === null || _b === void 0 ? void 0 : _b.Ratecard) === null || _c === void 0 ? void 0 : _c.length) > 0) {
225
225
  var today_1 = new Date();
@@ -250,12 +250,38 @@ var getRatecardFromCte = function (cte) {
250
250
  }
251
251
  break;
252
252
  default:
253
+ // LTL: Busca principal validando rota através de RatecardLane
254
+ var destinationUf_1 = (_e = (_d = cte.cityDestination) === null || _d === void 0 ? void 0 : _d.state) === null || _e === void 0 ? void 0 : _e.uf;
255
+ var originUf_1 = (_g = (_f = cte.cityOrigin) === null || _f === void 0 ? void 0 : _f.state) === null || _g === void 0 ? void 0 : _g.uf;
253
256
  var ratecardFromThisYear = cte.carrier.Ratecard.find(function (ratecard) {
254
- return (ratecard === null || ratecard === void 0 ? void 0 : ratecard.startsAt) && (ratecard === null || ratecard === void 0 ? void 0 : ratecard.endsAt) && today_1 >= new Date(ratecard.startsAt) && today_1 <= new Date(ratecard.endsAt) && (ratecard === null || ratecard === void 0 ? void 0 : ratecard.modalId) === cte.ratecardModalId;
257
+ var _a;
258
+ var isValidDate = (ratecard === null || ratecard === void 0 ? void 0 : ratecard.startsAt) && (ratecard === null || ratecard === void 0 ? void 0 : ratecard.endsAt) && today_1 >= new Date(ratecard.startsAt) && today_1 <= new Date(ratecard.endsAt);
259
+ var isValidModal = (ratecard === null || ratecard === void 0 ? void 0 : ratecard.modalId) === cte.ratecardModalId;
260
+ // Verificar se existe lane com a rota (cityOriginId -> ufDestination)
261
+ var hasDirectLane = (_a = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardLane) === null || _a === void 0 ? void 0 : _a.some(function (lane) {
262
+ return lane.cityOriginId === cte.cityOriginId && lane.ufDestination === destinationUf_1;
263
+ });
264
+ return isValidDate && isValidModal && hasDirectLane;
255
265
  });
256
266
  if (ratecardFromThisYear) {
257
267
  ratecard = ratecardFromThisYear;
258
268
  }
269
+ else {
270
+ // Rota reversa: Verificar se existe lane com rota inversa (cityDestinationId -> originUf)
271
+ var reverseRatecardFromThisYear = cte.carrier.Ratecard.find(function (ratecard) {
272
+ var _a;
273
+ var isValidDate = (ratecard === null || ratecard === void 0 ? void 0 : ratecard.startsAt) && (ratecard === null || ratecard === void 0 ? void 0 : ratecard.endsAt) && today_1 >= new Date(ratecard.startsAt) && today_1 <= new Date(ratecard.endsAt);
274
+ var isValidModal = (ratecard === null || ratecard === void 0 ? void 0 : ratecard.modalId) === cte.ratecardModalId;
275
+ // Verificar se existe lane com a rota inversa (cityDestinationId -> originUf)
276
+ var hasReverseLane = (_a = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardLane) === null || _a === void 0 ? void 0 : _a.some(function (lane) {
277
+ return lane.cityOriginId === cte.cityDestinationId && lane.ufDestination === originUf_1;
278
+ });
279
+ return isValidDate && isValidModal && hasReverseLane;
280
+ });
281
+ if (reverseRatecardFromThisYear) {
282
+ ratecard = reverseRatecardFromThisYear;
283
+ }
284
+ }
259
285
  break;
260
286
  }
261
287
  }
@@ -540,8 +566,23 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug, allFeesForCal
540
566
  currency: 'BRL',
541
567
  });
542
568
  var result = "".concat(redeliveryInfo ? "".concat(valueCurrency).concat(redeliveryInfo, " = ").concat(resultCurrency) : valueCurrency);
569
+ // Se for FTL com distância e tiver intervalo de KM, adicionar informação na descrição
570
+ if ((cte === null || cte === void 0 ? void 0 : cte.ratecardModalId) === exports.RatecardModalEnum.ROAD_DOMESTIC_FTL && (cte === null || cte === void 0 ? void 0 : cte.distance) && (ratecardLaneFee.minKm || ratecardLaneFee.maxKm)) {
571
+ var kmRange = ratecardLaneFee.maxKm
572
+ ? "".concat(ratecardLaneFee.minKm || 0, "-").concat(ratecardLaneFee.maxKm, " km")
573
+ : "".concat(ratecardLaneFee.minKm, "+ km");
574
+ result = "".concat(cte.distance, " km (").concat(kmRange, ") = ").concat(redeliveryInfo ? "".concat(valueCurrency).concat(redeliveryInfo, " = ").concat(resultCurrency) : valueCurrency);
575
+ }
576
+ // Se houver taxedWeight e tiver intervalo de peso, adicionar informação na descrição
577
+ else if ((cte === null || cte === void 0 ? void 0 : cte.taxedWeight) && (ratecardLaneFee.minWeight || ratecardLaneFee.maxWeight)) {
578
+ var weightRange = ratecardLaneFee.maxWeight
579
+ ? "".concat(ratecardLaneFee.minWeight || 0, "-").concat(ratecardLaneFee.maxWeight, " kg")
580
+ : "".concat(ratecardLaneFee.minWeight, "+ kg");
581
+ result = "".concat(cte.taxedWeight, " kg (").concat(weightRange, ") = ").concat(redeliveryInfo ? "".concat(valueCurrency).concat(redeliveryInfo, " = ").concat(resultCurrency) : valueCurrency);
582
+ }
543
583
  totalFee = total;
544
584
  resultFee = result;
585
+ // Lógica de intervalo de peso
545
586
  if ((cte === null || cte === void 0 ? void 0 : cte.taxedWeight) && !ratecardLaneFee.maxWeight && ratecardLaneFee.minWeight && Math.ceil(+cte.taxedWeight) >= +ratecardLaneFee.minWeight) {
546
587
  // Não tem maxWeight - é o último intervalo
547
588
  // Encontrar todas as taxas do mesmo tipo que têm maxWeight
@@ -581,37 +622,77 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug, allFeesForCal
581
622
  resultFee = result_4;
582
623
  }
583
624
  }
625
+ // Lógica de intervalo de KM para FTL
626
+ else if ((cte === null || cte === void 0 ? void 0 : cte.ratecardModalId) === exports.RatecardModalEnum.ROAD_DOMESTIC_FTL && (cte === null || cte === void 0 ? void 0 : cte.distance) && !ratecardLaneFee.maxKm && ratecardLaneFee.minKm && +cte.distance >= +ratecardLaneFee.minKm) {
627
+ // Não tem maxKm - é o último intervalo
628
+ // Encontrar todas as taxas do mesmo tipo que têm maxKm
629
+ var feesWithMaxKm = allFeesForCalc === null || allFeesForCalc === void 0 ? void 0 : allFeesForCalc.filter(function (fee) {
630
+ var _a, _b;
631
+ return ((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.id) === ((_b = ratecardLaneFee.fee) === null || _b === void 0 ? void 0 : _b.id) &&
632
+ fee.maxKm &&
633
+ (cte === null || cte === void 0 ? void 0 : cte.distance) &&
634
+ +fee.maxKm < +cte.distance;
635
+ });
636
+ // Ordenar pelo maxKm do maior para o menor
637
+ feesWithMaxKm === null || feesWithMaxKm === void 0 ? void 0 : feesWithMaxKm.sort(function (a, b) { return b.maxKm - a.maxKm; });
638
+ // Pegar o primeiro (maior maxKm)
639
+ var highestIntervalFee = feesWithMaxKm === null || feesWithMaxKm === void 0 ? void 0 : feesWithMaxKm[0];
640
+ if (highestIntervalFee) {
641
+ // Calcula baseado na taxa do intervalo mais alto que tem maxKm
642
+ var total_5 = +highestIntervalFee.value;
643
+ // Calcular diferença de distância
644
+ var kmDifference = Number((((cte === null || cte === void 0 ? void 0 : cte.distance) || 0) - (highestIntervalFee.maxKm || 0)).toFixed(2));
645
+ // Calcular valor adicional pela diferença de km
646
+ var additionalValue = kmDifference * +ratecardLaneFee.value;
647
+ // Somar ao total
648
+ total_5 += additionalValue;
649
+ // Aplicar multiplicador de redelivery ao valor final
650
+ var _k = applyRedeliveryMultiplier(total_5, cte), adjustedTotal_5 = _k.adjustedTotal, redeliveryInfo_5 = _k.redeliveryInfo;
651
+ total_5 = adjustedTotal_5;
652
+ var valueCurrency_3 = (+highestIntervalFee.value).toLocaleString('pt-br', {
653
+ style: 'currency',
654
+ currency: 'BRL',
655
+ });
656
+ var resultCurrency_5 = (+total_5).toLocaleString('pt-br', {
657
+ style: 'currency',
658
+ currency: 'BRL',
659
+ });
660
+ var result_5 = "".concat(cte.distance, " km = ").concat(valueCurrency_3, " + ").concat(kmDifference, " km x ").concat(convertNumberToCurrency(+ratecardLaneFee.value, 'pt-br')).concat(redeliveryInfo_5, " = ").concat(resultCurrency_5);
661
+ totalFee = total_5;
662
+ resultFee = result_5;
663
+ }
664
+ }
584
665
  break;
585
666
  case exports.FeeCalculationTypeEnum.COMMODITY_VALUE:
586
667
  if (value) {
587
668
  var commodityValue = (_a = cte === null || cte === void 0 ? void 0 : cte.commodityValue) !== null && _a !== void 0 ? _a : 0;
588
- var total_5 = +commodityValue * (+value / 100);
589
- var _k = applyRedeliveryMultiplier(total_5, cte), adjustedTotal_5 = _k.adjustedTotal, redeliveryInfo_5 = _k.redeliveryInfo;
590
- total_5 = adjustedTotal_5;
591
- var resultCurrency_5 = (+total_5).toLocaleString('pt-br', {
669
+ var total_6 = +commodityValue * (+value / 100);
670
+ var _l = applyRedeliveryMultiplier(total_6, cte), adjustedTotal_6 = _l.adjustedTotal, redeliveryInfo_6 = _l.redeliveryInfo;
671
+ total_6 = adjustedTotal_6;
672
+ var resultCurrency_6 = (+total_6).toLocaleString('pt-br', {
592
673
  style: 'currency',
593
674
  currency: 'BRL',
594
675
  });
595
- var result_5 = "".concat(convertNumberToCurrency(+commodityValue, 'pt-br'), " x ").concat(value, "%").concat(redeliveryInfo_5, " = ").concat(resultCurrency_5);
596
- totalFee = total_5;
597
- resultFee = result_5;
676
+ var result_6 = "".concat(convertNumberToCurrency(+commodityValue, 'pt-br'), " x ").concat(value, "%").concat(redeliveryInfo_6, " = ").concat(resultCurrency_6);
677
+ totalFee = total_6;
678
+ resultFee = result_6;
598
679
  }
599
680
  break;
600
681
  case exports.FeeCalculationTypeEnum.TAXED_WEIGHT:
601
682
  if (value) {
602
683
  var taxedWeight = (_b = cte === null || cte === void 0 ? void 0 : cte.taxedWeight) !== null && _b !== void 0 ? _b : 0;
603
- var total_6 = +taxedWeight * +value;
604
- var _l = applyRedeliveryMultiplier(total_6, cte), adjustedTotal_6 = _l.adjustedTotal, redeliveryInfo_6 = _l.redeliveryInfo;
605
- total_6 = adjustedTotal_6;
606
- var resultCurrency_6 = (+total_6).toLocaleString('pt-br', {
684
+ var total_7 = +taxedWeight * +value;
685
+ var _m = applyRedeliveryMultiplier(total_7, cte), adjustedTotal_7 = _m.adjustedTotal, redeliveryInfo_7 = _m.redeliveryInfo;
686
+ total_7 = adjustedTotal_7;
687
+ var resultCurrency_7 = (+total_7).toLocaleString('pt-br', {
607
688
  style: 'currency',
608
689
  currency: 'BRL',
609
690
  });
610
- var result_6 = "".concat(taxedWeight, " kg x ").concat(convertNumberToCurrency(+value, 'pt-br', {
691
+ var result_7 = "".concat(taxedWeight, " kg x ").concat(convertNumberToCurrency(+value, 'pt-br', {
611
692
  minimumFractionDigits: 4,
612
- })).concat(redeliveryInfo_6, " = ").concat(resultCurrency_6);
613
- totalFee = total_6;
614
- resultFee = result_6;
693
+ })).concat(redeliveryInfo_7, " = ").concat(resultCurrency_7);
694
+ totalFee = total_7;
695
+ resultFee = result_7;
615
696
  }
616
697
  break;
617
698
  /*if (ratecardLaneFee.minWeight && cte?.taxedWeight) {
package/build/index.js.gz CHANGED
Binary file