Agregado mensaje de error a getRegistros

This commit is contained in:
Alejandro Tasistro 2025-06-19 15:29:48 -03:00
parent 612abc4378
commit a3b6d686d3
9 changed files with 67 additions and 17 deletions

View file

@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use League\Csv\Exception;
class GrupoDeCompra extends Model
{
@ -165,6 +166,10 @@ class GrupoDeCompra extends Model
}
//Asume que los productos están gruadados en orden de fila
/**
* @throws Exception
*/
public static function obtenerTemplateDeFilasVacias(int $columns): array
{
$productosFilaID = Producto::productosFilaID();
@ -182,6 +187,9 @@ class GrupoDeCompra extends Model
return $template;
}
/**
* @throws Exception
*/
public function exportarPedidoEnCSV()
{
$records = $this->generarColumnaCantidades();
@ -190,6 +198,9 @@ class GrupoDeCompra extends Model
CsvHelper::generarCsv('csv/exports/' . $this->nombre . '-' . $fecha . '.csv', $records);
}
/**
* @throws Exception
*/
public function generarColumnaCantidades(): array
{
$productos_en_pedido = $this->productosPedidos();
@ -212,6 +223,9 @@ class GrupoDeCompra extends Model
return $records;
}
/**
* @throws Exception
*/
public function exportarPedidoConNucleosEnCSV()
{
$productos_en_pedido = $this->productosPedidos();

View file

@ -9,6 +9,7 @@ use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use League\Csv\Exception;
class CanastaHelper
{
@ -45,10 +46,14 @@ class CanastaHelper
return $nombre;
}
/**
* @throws Exception
*/
public static function cargarCanasta($archivo) {
self::limpiarTablas();
$registros = CsvHelper::getRecords($archivo);
$registros = CsvHelper::getRecords($archivo, "No se pudo leer el archivo.");
$toInsert = [];
$categoria = '';

View file

@ -13,7 +13,10 @@ use League\Csv\Writer;
class CsvHelper
{
public static function getRecords($filePath): Iterator {
/**
* @throws Exception
*/
public static function getRecords($filePath, $message): Iterator {
$csv = Reader::createFromPath(storage_path($filePath));
try {
$csv->setDelimiter("|");
@ -22,7 +25,7 @@ class CsvHelper
return $csv->getRecords();
} catch (InvalidArgument|Exception $e) {
Log::error($e->getMessage());
return null;
throw new Exception($message, $e);
}
}

View file

@ -4,6 +4,7 @@ namespace App\Helpers;
use App\CanastaLog;
use Illuminate\Support\Facades\Log;
use League\Csv\Exception;
class TransporteHelper
{
@ -20,6 +21,9 @@ class TransporteHelper
return self::cantidadTransporte($monto) * self::COSTO_TRANSPORTE;
}
/**
* @throws Exception
*/
public static function filaTransporte()
{
$ultimaCanasta = CanastaLog::where('descripcion', CanastaHelper::CANASTA_CARGADA)
@ -27,12 +31,14 @@ class TransporteHelper
->pluck('path')
->first();
$registros = CsvHelper::getRecords($ultimaCanasta);
$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;
if ($registro[CanastaHelper::TIPO] == 'T')
return $key;
Log::error('No hay fila de tipo T en la planilla: ' . $ultimaCanasta);
return null;
Log::error($error);
throw new Exception($error);
}
}

View file

@ -3,7 +3,7 @@
namespace App\Http\Controllers;
use App\GrupoDeCompra;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use League\Csv\Exception;
class AdminController extends Controller
{
@ -20,9 +20,13 @@ class AdminController extends Controller
$gdc->exportarPedidosAPdf();
}
public function exportarPedidoACSV(GrupoDeCompra $gdc): BinaryFileResponse
public function exportarPedidoACSV(GrupoDeCompra $gdc)
{
$gdc->exportarPedidoEnCSV();
try {
$gdc->exportarPedidoEnCSV();
} catch (Exception $e) {
return response()->json(['message' => $e->getMessage()]);
}
$pattern = storage_path('csv/exports/'. $gdc->nombre . '-*.csv');
$files = glob($pattern);
@ -33,9 +37,13 @@ class AdminController extends Controller
return response()->download($files[0]);
}
public function exportarPedidoConNucleosACSV(GrupoDeCompra $gdc): BinaryFileResponse
public function exportarPedidoConNucleosACSV(GrupoDeCompra $gdc)
{
$gdc->exportarPedidoConNucleosEnCSV();
try {
$gdc->exportarPedidoConNucleosEnCSV();
} catch (Exception $e) {
return response()->json(['message' => $e->getMessage()]);
}
$pattern = storage_path('csv/exports/'.$gdc->nombre.'-completo-*.csv');
$files = glob($pattern);

View file

@ -7,9 +7,9 @@ use App\Helpers\CanastaHelper;
use App\Producto;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use League\Csv\Exception;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use League\Csv\Reader;
use DatabaseSeeder;
class ComisionesController
{
@ -22,9 +22,13 @@ class ComisionesController
return view('auth/login');
}
public function descargarPedidos(): BinaryFileResponse
public function descargarPedidos()
{
Producto::planillaTotales();
try {
Producto::planillaTotales();
} catch (Exception $e) {
return response()->json(['message' => $e->getMessage()], 500);
}
$pattern = storage_path('csv/exports/pedidos-por-barrio-*.csv');
$files = glob($pattern);
@ -59,7 +63,11 @@ class ComisionesController
]);
$nombre = CanastaHelper::guardarCanasta($request->file('data'), self::CANASTAS_PATH);
CanastaHelper::cargarCanasta(self::CANASTAS_PATH . $nombre);
try {
CanastaHelper::cargarCanasta(self::CANASTAS_PATH . $nombre);
} catch (Exception $e) {
return response()->json(['message' => $e->getMessage()], 500);
}
return response()->json([
'message' => 'Canasta cargada exitosamente',

View file

@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use League\Csv\Exception;
class Producto extends Model
{
@ -75,6 +76,9 @@ class Producto extends Model
->get();
}
/**
* @throws Exception
*/
static public function planillaTotales()
{
$headers = ['Producto'];

View file

@ -11,6 +11,7 @@ class CanastaSeeder extends Seeder
* Run the database seeds.
*
* @return void
* @throws \League\Csv\Exception
*/
public function run()
{

View file

@ -13,10 +13,11 @@ class GrupoDeCompraSeeder extends Seeder
* Run the database seeds.
*
* @return void
* @throws \League\Csv\Exception
*/
public function run()
{
$registros = CsvHelper::getRecords('csv/barrios.csv');
$registros = CsvHelper::getRecords('csv/barrios.csv', 'No se pudo leer la planilla de barrios.');
$gdcToInsert = [];
$usersToInsert = [];
$roles = UserRole::where('nombre', 'barrio')->orWhere('nombre', 'admin_barrio')->get();