b2m-utils 0.0.184 → 0.0.186

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