76 lines
2 KiB
PHP
76 lines
2 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use App\CanastaLog;
|
|
use Illuminate\Support\Facades\Log;
|
|
use InvalidArgumentException;
|
|
use League\Csv\Exception;
|
|
|
|
class TransporteHelper
|
|
{
|
|
private const COSTO_TRANSPORTE = "bono-transporte";
|
|
private const MONTO_TRANSPORTE = "monto-transporte";
|
|
private static ?array $parametros = null;
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public static function cantidadTransporte($monto)
|
|
{
|
|
return ceil($monto / self::getParametro(self::MONTO_TRANSPORTE));
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public static function totalTransporte($monto)
|
|
{
|
|
return self::cantidadTransporte($monto) * self::getParametro(self::COSTO_TRANSPORTE);
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public static function filaTransporte()
|
|
{
|
|
$ultimaCanasta = CanastaLog::where('descripcion', CanastaHelper::CANASTA_CARGADA)
|
|
->orderBy('created_at', 'desc')
|
|
->pluck('path')
|
|
->first();
|
|
|
|
$registros = CsvHelper::getRecords($ultimaCanasta, "No se encontró la ultima canasta.");
|
|
$error = 'No hay fila de tipo T en la planilla: ' . $ultimaCanasta;
|
|
|
|
foreach ($registros as $key => $registro)
|
|
if ($registro[CanastaHelper::TIPO] == 'T')
|
|
return $key;
|
|
|
|
Log::error($error);
|
|
throw new Exception($error);
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public static function getParametro(string $id): int
|
|
{
|
|
if (self::$parametros === null) {
|
|
$records = CsvHelper::getRecords(resource_path('csv/parametros.csv'), "No se pudo leer el archivo.");
|
|
self::$parametros = [];
|
|
foreach ($records as $row) {
|
|
self::$parametros[$row['id']] = $row;
|
|
}
|
|
}
|
|
|
|
if (!isset(self::$parametros[$id])) {
|
|
throw new InvalidArgumentException("Parámetro '$id' no encontrado.");
|
|
}
|
|
|
|
return (int) self::$parametros[$id]['valor'];
|
|
}
|
|
|
|
public static function resetParametros(): void {
|
|
self::$parametros = null;
|
|
}
|
|
}
|