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

View file

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

View file

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

View file

@ -4,6 +4,7 @@ namespace App\Helpers;
use App\CanastaLog; use App\CanastaLog;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use League\Csv\Exception;
class TransporteHelper class TransporteHelper
{ {
@ -20,6 +21,9 @@ class TransporteHelper
return self::cantidadTransporte($monto) * self::COSTO_TRANSPORTE; return self::cantidadTransporte($monto) * self::COSTO_TRANSPORTE;
} }
/**
* @throws Exception
*/
public static function filaTransporte() public static function filaTransporte()
{ {
$ultimaCanasta = CanastaLog::where('descripcion', CanastaHelper::CANASTA_CARGADA) $ultimaCanasta = CanastaLog::where('descripcion', CanastaHelper::CANASTA_CARGADA)
@ -27,12 +31,14 @@ class TransporteHelper
->pluck('path') ->pluck('path')
->first(); ->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) 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); Log::error($error);
return null; throw new Exception($error);
} }
} }

View file

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

View file

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

View file

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

View file

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

View file

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