b2m-utils 0.0.184 → 0.0.187

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.
@@ -1,3 +1,33 @@
1
+ var getRatecardFromCte = function (cte) {
2
+ var _a, _b, _c;
3
+ var ratecard = null;
4
+ 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) {
5
+ var ratecardFromThisYear = cte.carrier.Ratecard.find(function (ratecard) {
6
+ var today = new Date();
7
+ return (ratecard === null || ratecard === void 0 ? void 0 : ratecard.startsAt) && (ratecard === null || ratecard === void 0 ? void 0 : ratecard.endsAt) && today >= new Date(ratecard.startsAt) && today <= new Date(ratecard.endsAt);
8
+ });
9
+ if (ratecardFromThisYear) {
10
+ ratecard = ratecardFromThisYear;
11
+ }
12
+ }
13
+ return ratecard;
14
+ };
15
+
16
+ var applyRedeliveryMultiplier = function (total, cte) {
17
+ var redeliveryInfo = '';
18
+ if (cte === null || cte === void 0 ? void 0 : cte.isRedelivery) {
19
+ var ratecard = getRatecardFromCte(cte);
20
+ if (ratecard === null || ratecard === void 0 ? void 0 : ratecard.redeliveryPercentageValue) {
21
+ total = total * (ratecard.redeliveryPercentageValue / 100);
22
+ redeliveryInfo = " x ".concat(ratecard.redeliveryPercentageValue, "%");
23
+ }
24
+ }
25
+ return {
26
+ adjustedTotal: total,
27
+ redeliveryInfo: redeliveryInfo
28
+ };
29
+ };
30
+
1
31
  var ApplicationColumnNameEnum;
2
32
  (function (ApplicationColumnNameEnum) {
3
33
  ApplicationColumnNameEnum["DEPARTURE_AT"] = "departureAt";
@@ -200,14 +230,118 @@ var TrackProcessProviderTypeEnum;
200
230
  TrackProcessProviderTypeEnum[TrackProcessProviderTypeEnum["OTHERS"] = 5] = "OTHERS";
201
231
  })(TrackProcessProviderTypeEnum || (TrackProcessProviderTypeEnum = {}));
202
232
 
203
- var calculateFee = function (ratecardLaneFee, cte, allFees, debug) {
233
+ /******************************************************************************
234
+ Copyright (c) Microsoft Corporation.
235
+
236
+ Permission to use, copy, modify, and/or distribute this software for any
237
+ purpose with or without fee is hereby granted.
238
+
239
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
240
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
241
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
242
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
243
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
244
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
245
+ PERFORMANCE OF THIS SOFTWARE.
246
+ ***************************************************************************** */
247
+
248
+ var __assign = function() {
249
+ __assign = Object.assign || function __assign(t) {
250
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
251
+ s = arguments[i];
252
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
253
+ }
254
+ return t;
255
+ };
256
+ return __assign.apply(this, arguments);
257
+ };
258
+
259
+ function __awaiter(thisArg, _arguments, P, generator) {
260
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
261
+ return new (P || (P = Promise))(function (resolve, reject) {
262
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
263
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
264
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
265
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
266
+ });
267
+ }
268
+
269
+ function __generator(thisArg, body) {
270
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
271
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
272
+ function verb(n) { return function (v) { return step([n, v]); }; }
273
+ function step(op) {
274
+ if (f) throw new TypeError("Generator is already executing.");
275
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
276
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
277
+ if (y = 0, t) op = [op[0] & 2, t.value];
278
+ switch (op[0]) {
279
+ case 0: case 1: t = op; break;
280
+ case 4: _.label++; return { value: op[1], done: false };
281
+ case 5: _.label++; y = op[1]; op = [0]; continue;
282
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
283
+ default:
284
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
285
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
286
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
287
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
288
+ if (t[2]) _.ops.pop();
289
+ _.trys.pop(); continue;
290
+ }
291
+ op = body.call(thisArg, _);
292
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
293
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
294
+ }
295
+ }
296
+
297
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
298
+ var e = new Error(message);
299
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
300
+ };
301
+
302
+ var convertNumberToCurrency = function (value, locale, options) {
303
+ if (locale === void 0) { locale = 'en-US'; }
304
+ var configObject = {
305
+ locale: 'en-US',
306
+ currency: 'USD',
307
+ };
308
+ switch (locale) {
309
+ case 'pt-br':
310
+ configObject.locale = 'pt-br';
311
+ configObject.currency = 'BRL';
312
+ break;
313
+ }
314
+ return (+value).toLocaleString(configObject.locale, __assign({ style: 'currency', currency: configObject.currency }, options));
315
+ };
316
+
317
+ var filterSiblingFees = function (fees) {
318
+ return fees.filter(function (fee) {
319
+ var _a, _b, _c;
320
+ if ((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.parentId) {
321
+ var siblingFees = fees === null || fees === void 0 ? void 0 : fees.filter(function (i) { var _a, _b, _c, _d; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.parentId) === ((_b = fee.fee) === null || _b === void 0 ? void 0 : _b.parentId) && ((_c = i.fee) === null || _c === void 0 ? void 0 : _c.id) !== ((_d = fee.fee) === null || _d === void 0 ? void 0 : _d.id); });
322
+ if (siblingFees && (siblingFees === null || siblingFees === void 0 ? void 0 : siblingFees.length) > 0) {
323
+ siblingFees.sort(function (a, b) {
324
+ if ((b === null || b === void 0 ? void 0 : b.totalFee) && (a === null || a === void 0 ? void 0 : a.totalFee)) {
325
+ return +(b === null || b === void 0 ? void 0 : b.totalFee) - +(a === null || a === void 0 ? void 0 : a.totalFee);
326
+ }
327
+ return 0;
328
+ });
329
+ if ((siblingFees === null || siblingFees === void 0 ? void 0 : siblingFees.length) > 0) {
330
+ if (((_b = siblingFees[0]) === null || _b === void 0 ? void 0 : _b.totalFee) && fee.totalFee && ((_c = siblingFees[0]) === null || _c === void 0 ? void 0 : _c.totalFee) > fee.totalFee) {
331
+ return false;
332
+ }
333
+ }
334
+ }
335
+ }
336
+ return true;
337
+ });
338
+ };
339
+
340
+ var calculateFee = function (ratecardLaneFee, cte, allFees, debug, allFeesForCalc) {
204
341
  var _a, _b, _c, _d;
342
+ if (debug === void 0) { debug = false; }
205
343
  if (ratecardLaneFee.fee) {
206
344
  var value = ratecardLaneFee.value, fee_1 = ratecardLaneFee.fee;
207
- var valueCurrency = (+ratecardLaneFee.value).toLocaleString('pt-br', {
208
- style: 'currency',
209
- currency: 'BRL',
210
- });
211
345
  var totalFee = 0;
212
346
  var resultFee = '';
213
347
  var totalToCalc = 0;
@@ -218,25 +352,20 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug) {
218
352
  switch (fee_1.id) {
219
353
  case FeeEnum.ICMS:
220
354
  if (allFees && Array.isArray(allFees) && (allFees === null || allFees === void 0 ? void 0 : allFees.length) > 0) {
221
- var totalFromAllFees = allFees.filter(function (i) { var _a; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) !== FeeCalculationTypeEnum.TOTAL_PERCENTAGE; }).map(function (it) {
222
- var result = calculateFee(it, cte, allFees);
355
+ var totalFromAllFees = filterSiblingFees(allFees).filter(function (i) { var _a; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) !== FeeCalculationTypeEnum.TOTAL_PERCENTAGE; }).map(function (it) {
356
+ var result = calculateFee(it, cte, allFees, debug, allFeesForCalc);
223
357
  if (result === null || result === void 0 ? void 0 : result.totalToCalc) {
224
358
  return result.totalToCalc;
225
359
  }
226
360
  return 0;
227
- });
361
+ }, 0);
228
362
  if (totalFromAllFees && (totalFromAllFees === null || totalFromAllFees === void 0 ? void 0 : totalFromAllFees.length) > 0) {
229
363
  var totalValueFromAllFees = totalFromAllFees.filter(function (i) { return !isNaN(+i) && +i > 0; }).reduce(function (previous, current) {
230
- if (previous && current && !isNaN(+previous) && !isNaN(+current)) {
231
- return +previous + +current;
232
- }
233
- else {
234
- return 0;
235
- }
236
- });
364
+ return +previous + +current;
365
+ }, 0);
237
366
  if (totalValueFromAllFees && !isNaN(+totalValueFromAllFees)) {
238
- var valueToDivide = (1 - value);
239
- var firstTotal = +totalValueFromAllFees / valueToDivide;
367
+ var valueToDivide = (1 - value).toFixed(2);
368
+ var firstTotal = +totalValueFromAllFees / +valueToDivide;
240
369
  var total = +firstTotal * +value;
241
370
  var totalValueFromAllFeesCurrency = convertNumberToCurrency(+totalValueFromAllFees, 'pt-br');
242
371
  var resultCurrency = (+total).toLocaleString('pt-BR', {
@@ -245,6 +374,7 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug) {
245
374
  });
246
375
  var result = "(".concat(totalValueFromAllFeesCurrency, " / ").concat(valueToDivide, ") x ").concat(value, " = ").concat(resultCurrency);
247
376
  totalFee = total;
377
+ totalToCalc = Number(total.toFixed(2));
248
378
  resultFee = result;
249
379
  }
250
380
  }
@@ -256,8 +386,11 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug) {
256
386
  switch (fee_1.feeCalculationTypeId) {
257
387
  case FeeCalculationTypeEnum.TOTAL_PERCENTAGE:
258
388
  if (allFees && Array.isArray(allFees) && (allFees === null || allFees === void 0 ? void 0 : allFees.length) > 0) {
259
- var totalFromAllFees = allFees.filter(function (i) { var _a; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) !== FeeCalculationTypeEnum.TOTAL_PERCENTAGE; }).map(function (it) {
260
- var result = calculateFee(it, cte, allFees);
389
+ var totalFromAllFees = allFees.filter(function (i) { var _a; return ((_a = i.fee) === null || _a === void 0 ? void 0 : _a.feeCalculationTypeId) !== FeeCalculationTypeEnum.TOTAL_PERCENTAGE; }).map(function () {
390
+ // const result = calculateFee(it, cte, allFees, debug, allFeesForCalc);
391
+ var result = {
392
+ totalToCalc: 0,
393
+ };
261
394
  if (result === null || result === void 0 ? void 0 : result.totalToCalc) {
262
395
  return result.totalToCalc;
263
396
  }
@@ -274,110 +407,242 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug) {
274
407
  });
275
408
  if (totalValueFromAllFees && !isNaN(+totalValueFromAllFees)) {
276
409
  var valueToMultiply = (1 - value);
277
- var total = +totalValueFromAllFees * (valueToMultiply / 100);
410
+ var total_1 = +totalValueFromAllFees * (valueToMultiply / 100);
411
+ var _e = applyRedeliveryMultiplier(total_1, cte), adjustedTotal_1 = _e.adjustedTotal, redeliveryInfo_1 = _e.redeliveryInfo;
412
+ total_1 = adjustedTotal_1;
278
413
  var totalValueFromAllFeesCurrency = convertNumberToCurrency(+totalValueFromAllFees, 'pt-br');
279
- var resultCurrency = (+total).toLocaleString('pt-BR', {
414
+ var resultCurrency_1 = (+total_1).toLocaleString('pt-BR', {
280
415
  style: 'currency',
281
416
  currency: 'BRL',
282
417
  });
283
- var result = "".concat(totalValueFromAllFeesCurrency, " x ").concat(valueToMultiply, "% = ").concat(resultCurrency);
284
- totalFee = total;
285
- resultFee = result;
418
+ var result_1 = "".concat(totalValueFromAllFeesCurrency, " x ").concat(valueToMultiply, "%").concat(redeliveryInfo_1, " = ").concat(resultCurrency_1);
419
+ totalFee = total_1;
420
+ resultFee = result_1;
286
421
  }
287
422
  }
288
423
  }
289
424
  break;
290
425
  case FeeCalculationTypeEnum.FRACTIONED:
291
- if (cte.taxedWeight && !isNaN(+cte.taxedWeight)) {
426
+ if ((cte === null || cte === void 0 ? void 0 : cte.taxedWeight) && !isNaN(+(cte === null || cte === void 0 ? void 0 : cte.taxedWeight))) {
292
427
  var fractions = Math.ceil(+cte.taxedWeight / 100);
293
- var total = fractions * value;
294
- var resultCurrency = (+total).toLocaleString('pt-BR', {
428
+ var total_2 = fractions * value;
429
+ var _f = applyRedeliveryMultiplier(total_2, cte), adjustedTotal_2 = _f.adjustedTotal, redeliveryInfo_2 = _f.redeliveryInfo;
430
+ total_2 = adjustedTotal_2;
431
+ var valueCurrency_1 = (+value).toLocaleString('pt-br', {
295
432
  style: 'currency',
296
433
  currency: 'BRL',
297
434
  });
298
- var result = "".concat(fractions, " fra\u00E7\u00F5es x ").concat(valueCurrency, " = ").concat(resultCurrency);
299
- totalFee = total;
300
- resultFee = result;
435
+ var resultCurrency_2 = (+total_2).toLocaleString('pt-BR', {
436
+ style: 'currency',
437
+ currency: 'BRL',
438
+ });
439
+ var result_2 = "".concat(fractions, " fra\u00E7\u00F5es x ").concat(valueCurrency_1).concat(redeliveryInfo_2, " = ").concat(resultCurrency_2);
440
+ totalFee = total_2;
441
+ resultFee = result_2;
301
442
  }
302
443
  break;
303
444
  case FeeCalculationTypeEnum.FREIGHT:
304
- if (value && cte.freightValue) {
445
+ if (value && (cte === null || cte === void 0 ? void 0 : cte.freightValue)) {
305
446
  if (!isNaN(+value) && !isNaN(+cte.freightValue)) {
306
- var total = +cte.freightValue * (+value / 100);
447
+ var total_3 = +cte.freightValue * (+value / 100);
448
+ var _g = applyRedeliveryMultiplier(total_3, cte), adjustedTotal_3 = _g.adjustedTotal, redeliveryInfo_3 = _g.redeliveryInfo;
449
+ total_3 = adjustedTotal_3;
307
450
  var freightValueCurrency = (+cte.freightValue).toLocaleString('pt-br', {
308
451
  style: 'currency',
309
452
  currency: 'BRL',
310
453
  });
311
- var resultCurrency = (+total).toLocaleString('pt-br', {
454
+ var resultCurrency_3 = (+total_3).toLocaleString('pt-br', {
312
455
  style: 'currency',
313
456
  currency: 'BRL',
314
457
  });
315
- var result = "".concat(freightValueCurrency, " x ").concat(value, "% = ").concat(resultCurrency);
316
- totalFee = total;
317
- resultFee = result;
458
+ var result_3 = "".concat(freightValueCurrency, " x ").concat(value, "%").concat(redeliveryInfo_3, " = ").concat(resultCurrency_3);
459
+ totalFee = total_3;
460
+ resultFee = result_3;
318
461
  }
462
+ /*const commodityValue = cte.commodityValue ?? 0;
463
+
464
+ let total = +commodityValue * (+value / 100);
465
+
466
+ const { adjustedTotal, redeliveryInfo } = applyRedeliveryMultiplier(total, cte);
467
+ total = adjustedTotal;
468
+
469
+ const resultCurrency = (+total).toLocaleString('pt-br', {
470
+ style: 'currency',
471
+ currency: 'BRL',
472
+ });
473
+
474
+ const result = `${convertNumberToCurrency(+commodityValue, 'pt-br')} x ${value}%${redeliveryInfo} = ${resultCurrency}`;
475
+
476
+ totalFee = total;
477
+ resultFee = result;*/
319
478
  }
320
479
  break;
321
480
  case FeeCalculationTypeEnum.MIN_VALUE:
322
481
  case FeeCalculationTypeEnum.FIXED:
323
- totalFee = +value;
324
- resultFee = valueCurrency;
482
+ var total = +value;
483
+ var _h = applyRedeliveryMultiplier(total, cte), adjustedTotal = _h.adjustedTotal, redeliveryInfo = _h.redeliveryInfo;
484
+ total = adjustedTotal;
485
+ var valueCurrency = (+value).toLocaleString('pt-br', {
486
+ style: 'currency',
487
+ currency: 'BRL',
488
+ });
489
+ var resultCurrency = (+total).toLocaleString('pt-br', {
490
+ style: 'currency',
491
+ currency: 'BRL',
492
+ });
493
+ var result = "".concat(redeliveryInfo ? "".concat(valueCurrency).concat(redeliveryInfo, " = ").concat(resultCurrency) : valueCurrency);
494
+ totalFee = total;
495
+ resultFee = result;
496
+ if ((cte === null || cte === void 0 ? void 0 : cte.taxedWeight) && !ratecardLaneFee.maxWeight && ratecardLaneFee.minWeight && Math.ceil(+cte.taxedWeight) >= +ratecardLaneFee.minWeight) {
497
+ // Não tem maxWeight - é o último intervalo
498
+ // Encontrar todas as taxas do mesmo tipo que têm maxWeight
499
+ var feesWithMaxWeight = allFeesForCalc === null || allFeesForCalc === void 0 ? void 0 : allFeesForCalc.filter(function (fee) {
500
+ var _a, _b;
501
+ return ((_a = fee.fee) === null || _a === void 0 ? void 0 : _a.id) === ((_b = ratecardLaneFee.fee) === null || _b === void 0 ? void 0 : _b.id) &&
502
+ fee.maxWeight &&
503
+ (cte === null || cte === void 0 ? void 0 : cte.taxedWeight) &&
504
+ fee.maxWeight < cte.taxedWeight;
505
+ });
506
+ // Ordenar pelo maxWeight do maior para o menor
507
+ feesWithMaxWeight === null || feesWithMaxWeight === void 0 ? void 0 : feesWithMaxWeight.sort(function (a, b) { return b.maxWeight - a.maxWeight; });
508
+ // Pegar o primeiro (maior maxWeight)
509
+ var highestIntervalFee = feesWithMaxWeight === null || feesWithMaxWeight === void 0 ? void 0 : feesWithMaxWeight[0];
510
+ if (highestIntervalFee) {
511
+ // Calcula baseado na taxa do intervalo mais alto que tem maxWeight
512
+ var total_4 = +highestIntervalFee.value;
513
+ // Calcular diferença de peso
514
+ var weightDifference = Math.ceil(((cte === null || cte === void 0 ? void 0 : cte.taxedWeight) || 0) - (highestIntervalFee.maxWeight || 0));
515
+ // Calcular valor adicional pela diferença de peso
516
+ var additionalValue = weightDifference * +ratecardLaneFee.value;
517
+ // Somar ao total
518
+ total_4 += additionalValue;
519
+ // Aplicar multiplicador de redelivery ao valor final
520
+ var _j = applyRedeliveryMultiplier(total_4, cte), adjustedTotal_4 = _j.adjustedTotal, redeliveryInfo_4 = _j.redeliveryInfo;
521
+ total_4 = adjustedTotal_4;
522
+ var valueCurrency_2 = (+highestIntervalFee.value).toLocaleString('pt-br', {
523
+ style: 'currency',
524
+ currency: 'BRL',
525
+ });
526
+ var resultCurrency_4 = (+total_4).toLocaleString('pt-br', {
527
+ style: 'currency',
528
+ currency: 'BRL',
529
+ });
530
+ var result_4 = "".concat(valueCurrency_2, " + ").concat(weightDifference, " kg x ").concat(convertNumberToCurrency(+ratecardLaneFee.value, 'pt-br')).concat(redeliveryInfo_4, " = ").concat(resultCurrency_4);
531
+ totalFee = total_4;
532
+ resultFee = result_4;
533
+ }
534
+ }
325
535
  break;
326
536
  case FeeCalculationTypeEnum.COMMODITY_VALUE:
327
537
  if (value) {
328
- var commodityValue = (_a = cte.commodityValue) !== null && _a !== void 0 ? _a : 0;
329
- var total = +commodityValue * (+value / 100);
330
- var resultCurrency = (+total).toLocaleString('pt-br', {
538
+ var commodityValue = (_a = cte === null || cte === void 0 ? void 0 : cte.commodityValue) !== null && _a !== void 0 ? _a : 0;
539
+ var total_5 = +commodityValue * (+value / 100);
540
+ var _k = applyRedeliveryMultiplier(total_5, cte), adjustedTotal_5 = _k.adjustedTotal, redeliveryInfo_5 = _k.redeliveryInfo;
541
+ total_5 = adjustedTotal_5;
542
+ var resultCurrency_5 = (+total_5).toLocaleString('pt-br', {
331
543
  style: 'currency',
332
544
  currency: 'BRL',
333
545
  });
334
- var result = "".concat(convertNumberToCurrency(+commodityValue, 'pt-br'), " x ").concat(value, "% = ").concat(resultCurrency);
335
- totalFee = total;
336
- resultFee = result;
546
+ var result_5 = "".concat(convertNumberToCurrency(+commodityValue, 'pt-br'), " x ").concat(value, "%").concat(redeliveryInfo_5, " = ").concat(resultCurrency_5);
547
+ totalFee = total_5;
548
+ resultFee = result_5;
337
549
  }
338
550
  break;
339
551
  case FeeCalculationTypeEnum.TAXED_WEIGHT:
340
552
  if (value) {
341
- var taxedWeight = (_b = cte.taxedWeight) !== null && _b !== void 0 ? _b : 0;
342
- var total = +taxedWeight * +value;
343
- var resultCurrency = (+total).toLocaleString('pt-br', {
553
+ var taxedWeight = (_b = cte === null || cte === void 0 ? void 0 : cte.taxedWeight) !== null && _b !== void 0 ? _b : 0;
554
+ var total_6 = +taxedWeight * +value;
555
+ var _l = applyRedeliveryMultiplier(total_6, cte), adjustedTotal_6 = _l.adjustedTotal, redeliveryInfo_6 = _l.redeliveryInfo;
556
+ total_6 = adjustedTotal_6;
557
+ var resultCurrency_6 = (+total_6).toLocaleString('pt-br', {
344
558
  style: 'currency',
345
559
  currency: 'BRL',
346
560
  });
347
- var result = "".concat(taxedWeight, " kg x ").concat(convertNumberToCurrency(+value, 'pt-br', {
561
+ var result_6 = "".concat(taxedWeight, " kg x ").concat(convertNumberToCurrency(+value, 'pt-br', {
348
562
  minimumFractionDigits: 4,
349
- }), " = ").concat(resultCurrency);
350
- totalFee = total;
351
- resultFee = result;
563
+ })).concat(redeliveryInfo_6, " = ").concat(resultCurrency_6);
564
+ totalFee = total_6;
565
+ resultFee = result_6;
352
566
  }
353
567
  break;
354
- default:
355
- if (ratecardLaneFee.minWeight && cte.taxedWeight) {
356
- if (+ratecardLaneFee.minWeight <= +cte.taxedWeight) {
357
- // if (+ratecardLaneFee.maxWeight > +cte.taxedWeight) {
358
- var total = +value;
359
- var valueCurrency_1 = (+value).toLocaleString('pt-br', {
568
+ /*if (ratecardLaneFee.minWeight && cte?.taxedWeight) {
569
+
570
+ if (+ratecardLaneFee.minWeight <= +cte.taxedWeight) {
571
+
572
+ if (ratecardLaneFee.maxWeight && +ratecardLaneFee.maxWeight > +cte.taxedWeight) {
573
+
574
+ let total = +value;
575
+
576
+ const { adjustedTotal, redeliveryInfo } = applyRedeliveryMultiplier(total, cte);
577
+ total = adjustedTotal;
578
+
579
+ const valueCurrency = (+value).toLocaleString('pt-br', {
360
580
  style: 'currency',
361
581
  currency: 'BRL',
362
582
  });
363
- var resultCurrency = (+total).toLocaleString('pt-br', {
583
+
584
+ const resultCurrency = (+total).toLocaleString('pt-br', {
364
585
  style: 'currency',
365
586
  currency: 'BRL',
366
587
  });
367
- var result = "".concat(valueCurrency_1, " = ").concat(resultCurrency);
368
- /*return {
369
- total,
370
- result,
371
- }*/
588
+
589
+ const result = `${valueCurrency}${redeliveryInfo} = ${resultCurrency}`;
590
+
372
591
  totalFee = total;
373
592
  resultFee = result;
374
- // }
593
+
594
+ } else {
595
+ // Não tem maxWeight - é o último intervalo
596
+ // Encontrar todas as taxas do mesmo tipo que têm maxWeight
597
+ const feesWithMaxWeight = allFees?.filter(fee =>
598
+ fee.fee?.id === ratecardLaneFee.fee?.id &&
599
+ fee.maxWeight &&
600
+ cte?.taxedWeight &&
601
+ fee.maxWeight < cte.taxedWeight
602
+ );
603
+
604
+ // Ordenar pelo maxWeight do maior para o menor
605
+ feesWithMaxWeight?.sort((a, b) => (b.maxWeight! as number) - (a.maxWeight! as number));
606
+
607
+ // Pegar o primeiro (maior maxWeight)
608
+ const highestIntervalFee = feesWithMaxWeight?.[0];
609
+
610
+ if (highestIntervalFee) {
611
+ // Calcula baseado na taxa do intervalo mais alto que tem maxWeight
612
+ let total = +highestIntervalFee.value;
613
+
614
+ // Calcular diferença de peso
615
+ const weightDifference = (cte?.taxedWeight || 0) - (highestIntervalFee.maxWeight || 0);
616
+
617
+ // Calcular valor adicional pela diferença de peso
618
+ const additionalValue = weightDifference * +ratecardLaneFee.value;
619
+
620
+ // Somar ao total
621
+ total += additionalValue;
622
+
623
+ // Aplicar multiplicador de redelivery ao valor final
624
+ const { adjustedTotal, redeliveryInfo } = applyRedeliveryMultiplier(total, cte);
625
+ total = adjustedTotal;
626
+
627
+ const valueCurrency = (+highestIntervalFee.value).toLocaleString('pt-br', {
628
+ style: 'currency',
629
+ currency: 'BRL',
630
+ });
631
+
632
+ const resultCurrency = (+total).toLocaleString('pt-br', {
633
+ style: 'currency',
634
+ currency: 'BRL',
635
+ });
636
+
637
+ const result = `${valueCurrency} + ${weightDifference}kg x ${convertNumberToCurrency(+ratecardLaneFee.value, 'pt-br')}${redeliveryInfo} = ${resultCurrency}`;
638
+
639
+ totalFee = total;
640
+ resultFee = result;
641
+ }
375
642
  }
376
- /*return {
377
- total: 0,
378
- result: 0,
379
- };*/
643
+
380
644
  }
645
+ }*/
381
646
  }
382
647
  }
383
648
  totalToCalc = totalFee;
@@ -386,8 +651,12 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug) {
386
651
  if (siblingFees && (siblingFees === null || siblingFees === void 0 ? void 0 : siblingFees.length) > 0) {
387
652
  var siblingFeesResults = [];
388
653
  for (var _i = 0, siblingFees_1 = siblingFees; _i < siblingFees_1.length; _i++) {
389
- var item = siblingFees_1[_i];
390
- var result = calculateFee(item, cte);
654
+ siblingFees_1[_i];
655
+ // const result = calculateFee(item, cte, allFees, debug, allFeesForCalc);
656
+ var result = {
657
+ totalToCalc: 0,
658
+ totalFee: 0,
659
+ };
391
660
  siblingFeesResults.push(result);
392
661
  }
393
662
  siblingFeesResults.sort(function (a, b) {
@@ -396,6 +665,9 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug) {
396
665
  }
397
666
  return 0;
398
667
  });
668
+ /*console.log({
669
+ siblingFeesResults,
670
+ });*/
399
671
  if ((siblingFeesResults === null || siblingFeesResults === void 0 ? void 0 : siblingFeesResults.length) > 0) {
400
672
  if (((_c = siblingFeesResults[0]) === null || _c === void 0 ? void 0 : _c.totalFee) && ((_d = siblingFeesResults[0]) === null || _d === void 0 ? void 0 : _d.totalFee) > totalFee) {
401
673
  totalToCalc = 0;
@@ -405,114 +677,14 @@ var calculateFee = function (ratecardLaneFee, cte, allFees, debug) {
405
677
  }
406
678
  return {
407
679
  totalFee: +totalFee.toFixed(2),
408
- // totalFee: Math.ceil(+totalFee * 100) / 100,
409
- // totalFee,
410
680
  resultFee: resultFee,
411
681
  totalToCalc: totalToCalc,
412
682
  feeValue: ratecardLaneFee.value,
683
+ fee: fee_1,
413
684
  };
414
685
  }
415
686
  };
416
687
 
417
- /******************************************************************************
418
- Copyright (c) Microsoft Corporation.
419
-
420
- Permission to use, copy, modify, and/or distribute this software for any
421
- purpose with or without fee is hereby granted.
422
-
423
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
424
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
425
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
426
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
427
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
428
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
429
- PERFORMANCE OF THIS SOFTWARE.
430
- ***************************************************************************** */
431
-
432
- var __assign = function() {
433
- __assign = Object.assign || function __assign(t) {
434
- for (var s, i = 1, n = arguments.length; i < n; i++) {
435
- s = arguments[i];
436
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
437
- }
438
- return t;
439
- };
440
- return __assign.apply(this, arguments);
441
- };
442
-
443
- function __awaiter(thisArg, _arguments, P, generator) {
444
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
445
- return new (P || (P = Promise))(function (resolve, reject) {
446
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
447
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
448
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
449
- step((generator = generator.apply(thisArg, _arguments || [])).next());
450
- });
451
- }
452
-
453
- function __generator(thisArg, body) {
454
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
455
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
456
- function verb(n) { return function (v) { return step([n, v]); }; }
457
- function step(op) {
458
- if (f) throw new TypeError("Generator is already executing.");
459
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
460
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
461
- if (y = 0, t) op = [op[0] & 2, t.value];
462
- switch (op[0]) {
463
- case 0: case 1: t = op; break;
464
- case 4: _.label++; return { value: op[1], done: false };
465
- case 5: _.label++; y = op[1]; op = [0]; continue;
466
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
467
- default:
468
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
469
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
470
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
471
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
472
- if (t[2]) _.ops.pop();
473
- _.trys.pop(); continue;
474
- }
475
- op = body.call(thisArg, _);
476
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
477
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
478
- }
479
- }
480
-
481
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
482
- var e = new Error(message);
483
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
484
- };
485
-
486
- var convertNumberToCurrency = function (value, locale, options) {
487
- if (locale === void 0) { locale = 'en-US'; }
488
- var configObject = {
489
- locale: 'en-US',
490
- currency: 'USD',
491
- };
492
- switch (locale) {
493
- case 'pt-br':
494
- configObject.locale = 'pt-br';
495
- configObject.currency = 'BRL';
496
- break;
497
- }
498
- return (+value).toLocaleString(configObject.locale, __assign({ style: 'currency', currency: configObject.currency }, options));
499
- };
500
-
501
- var getRatecardFromCte = function (cte) {
502
- var _a, _b, _c;
503
- var ratecard = null;
504
- 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) {
505
- var ratecardFromThisYear = cte.carrier.Ratecard.find(function (ratecard) {
506
- var today = new Date();
507
- return (ratecard === null || ratecard === void 0 ? void 0 : ratecard.startsAt) && (ratecard === null || ratecard === void 0 ? void 0 : ratecard.endsAt) && today >= new Date(ratecard.startsAt) && today <= new Date(ratecard.endsAt);
508
- });
509
- if (ratecardFromThisYear) {
510
- ratecard = ratecardFromThisYear;
511
- }
512
- }
513
- return ratecard;
514
- };
515
-
516
688
  var getCteLane = function (cte) {
517
689
  var _a, _b, _c, _d, _e;
518
690
  if (cte === null || cte === void 0 ? void 0 : cte.carrier) {
@@ -6955,24 +7127,34 @@ var getFilteredFeesToAudit = function (_a) {
6955
7127
  };
6956
7128
 
6957
7129
  var getLaneFromRatecard = function (cte) {
6958
- var _a, _b, _c, _d;
7130
+ var _a, _b, _c, _d, _e, _f, _g, _h;
6959
7131
  var ratecard = getRatecardFromCte(cte);
6960
7132
  var cityOriginId = cte.cityOriginId, cityDestination = cte.cityDestination;
6961
7133
  var destinationUf = (_a = cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.state) === null || _a === void 0 ? void 0 : _a.uf;
6962
- if ((ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardLane) && ((_b = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardLane) === null || _b === void 0 ? void 0 : _b.length) > 0 && cityOriginId && destinationUf) {
7134
+ // Encontrar a ratecardRegion da cidade de destino
7135
+ var destinationRatecardRegionId = (_c = (_b = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardRegionCity) === null || _b === void 0 ? void 0 : _b.find(function (regionCity) { return regionCity.cityId === (cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.id); })) === null || _c === void 0 ? void 0 : _c.ratecardRegionId;
7136
+ if ((ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardLane) && ((_d = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardLane) === null || _d === void 0 ? void 0 : _d.length) > 0 && cityOriginId && destinationUf) {
6963
7137
  // Primeiro, buscar rota direta (Origem -> Destino)
6964
7138
  var directLane = ratecard.RatecardLane.find(function (i) {
6965
- return i.cityOriginId === cityOriginId && i.ufDestination === destinationUf;
7139
+ return i.cityOriginId === cityOriginId && i.ufDestination === destinationUf && i.ratecardRegionId === destinationRatecardRegionId;
7140
+ });
7141
+ console.log({
7142
+ directLane: directLane,
6966
7143
  });
6967
7144
  if (directLane) {
6968
7145
  return directLane;
6969
7146
  }
6970
7147
  // Se não encontrar rota direta, buscar rota inversa (Destino -> Origem)
6971
- // Para isso, precisamos encontrar a UF da origem
6972
- var originUf_1 = (_d = (_c = cte.cityOrigin) === null || _c === void 0 ? void 0 : _c.state) === null || _d === void 0 ? void 0 : _d.uf;
7148
+ // Para isso, precisamos encontrar a UF da origem e sua ratecardRegion
7149
+ var originUf_1 = (_f = (_e = cte.cityOrigin) === null || _e === void 0 ? void 0 : _e.state) === null || _f === void 0 ? void 0 : _f.uf;
7150
+ // Encontrar a ratecardRegion da cidade de origem
7151
+ var originRatecardRegionId_1 = (_h = (_g = ratecard === null || ratecard === void 0 ? void 0 : ratecard.RatecardRegionCity) === null || _g === void 0 ? void 0 : _g.find(function (regionCity) { var _a; return regionCity.cityId === ((_a = cte.cityOrigin) === null || _a === void 0 ? void 0 : _a.id); })) === null || _h === void 0 ? void 0 : _h.ratecardRegionId;
6973
7152
  if (originUf_1) {
6974
7153
  var reverseLane = ratecard.RatecardLane.find(function (i) {
6975
- return i.cityOriginId === (cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.id) && i.ufDestination === originUf_1;
7154
+ return i.cityOriginId === (cityDestination === null || cityDestination === void 0 ? void 0 : cityDestination.id) && i.ufDestination === originUf_1 && i.ratecardRegionId === originRatecardRegionId_1;
7155
+ });
7156
+ console.log({
7157
+ reverseLane: reverseLane,
6976
7158
  });
6977
7159
  if (reverseLane) {
6978
7160
  return reverseLane;
@@ -7177,5 +7359,5 @@ var setFormattedDatesInObjects = function (objectFormat) { return __awaiter(void
7177
7359
  });
7178
7360
  }); };
7179
7361
 
7180
- export { ApplicationColumnNameEnum, ApplicationEnum, CountryEnum, CteStatusEnum, CurrencyEnum, DocumentTypeEnum, DomainConfigurationEnum, DomainTypeEnum, FeeCalculationTypeEnum, FeeCategoryEnum, FeeEnum, FreightRegionEnum, ImapHostsEnum, ModalEnum, NotificationTypeEnum, PermissionEnum, RatecardConditionalFeeTypeEnum, RatecardModalEnum, SlaRegionEnum, SpotStatusEnum, TrackProcessProviderTypeEnum, calculateFee, convertNumberToCurrency, formatDateString, getAuditTotalFromCte, getConfigurationFromDomain, getContractFromFreight, getContractRouteFromFreight, getCookies, getCteDateRange, getCteLane, getCteLaneFeesTotal, getCtesFeesResult, getDataFromToken, getFilteredFeesToAudit, getFormattedFreightPlaceName, getLaneFeesToCalc, getLaneFromRatecard, getNormalizedCityName, getRatecardFromCte, getRouteDeliveryTimeFromFreight, getRouteOnTimeFromFreight, normalizeString, setFormattedDatesInObjects, verifyConditionalFee, verifyDefaultFees };
7362
+ export { ApplicationColumnNameEnum, ApplicationEnum, CountryEnum, CteStatusEnum, CurrencyEnum, DocumentTypeEnum, DomainConfigurationEnum, DomainTypeEnum, FeeCalculationTypeEnum, FeeCategoryEnum, FeeEnum, FreightRegionEnum, ImapHostsEnum, ModalEnum, NotificationTypeEnum, PermissionEnum, RatecardConditionalFeeTypeEnum, RatecardModalEnum, SlaRegionEnum, SpotStatusEnum, TrackProcessProviderTypeEnum, applyRedeliveryMultiplier, calculateFee, convertNumberToCurrency, filterSiblingFees, formatDateString, getAuditTotalFromCte, getConfigurationFromDomain, getContractFromFreight, getContractRouteFromFreight, getCookies, getCteDateRange, getCteLane, getCteLaneFeesTotal, getCtesFeesResult, getDataFromToken, getFilteredFeesToAudit, getFormattedFreightPlaceName, getLaneFeesToCalc, getLaneFromRatecard, getNormalizedCityName, getRatecardFromCte, getRouteDeliveryTimeFromFreight, getRouteOnTimeFromFreight, normalizeString, setFormattedDatesInObjects, verifyConditionalFee, verifyDefaultFees };
7181
7363
  //# sourceMappingURL=index.esm.js.map