Compare commits
No commits in common. "91225d7796aeefde65092bc5a8d6d035d235678e" and "ac559770c0c2dfc48cbeb1c461855e4d9d4a1f3d" have entirely different histories.
91225d7796
...
ac559770c0
10 changed files with 125 additions and 72 deletions
|
@ -11,7 +11,6 @@ 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
|
||||
{
|
||||
|
@ -166,10 +165,6 @@ 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();
|
||||
|
@ -187,9 +182,6 @@ class GrupoDeCompra extends Model
|
|||
return $template;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function exportarPedidoEnCSV()
|
||||
{
|
||||
$records = $this->generarColumnaCantidades();
|
||||
|
@ -198,9 +190,6 @@ class GrupoDeCompra extends Model
|
|||
CsvHelper::generarCsv('csv/exports/' . $this->nombre . '-' . $fecha . '.csv', $records);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function generarColumnaCantidades(): array
|
||||
{
|
||||
$productos_en_pedido = $this->productosPedidos();
|
||||
|
@ -223,9 +212,6 @@ class GrupoDeCompra extends Model
|
|||
return $records;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function exportarPedidoConNucleosEnCSV()
|
||||
{
|
||||
$productos_en_pedido = $this->productosPedidos();
|
||||
|
|
|
@ -9,7 +9,6 @@ use Illuminate\Support\Arr;
|
|||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Str;
|
||||
use League\Csv\Exception;
|
||||
|
||||
class CanastaHelper
|
||||
{
|
||||
|
@ -46,14 +45,10 @@ class CanastaHelper
|
|||
return $nombre;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function cargarCanasta($archivo) {
|
||||
self::limpiarTablas();
|
||||
|
||||
$registros = CsvHelper::getRecords($archivo, "No se pudo leer el archivo.");
|
||||
|
||||
$registros = CsvHelper::getRecords($archivo);
|
||||
$toInsert = [];
|
||||
$categoria = '';
|
||||
|
||||
|
|
|
@ -13,10 +13,7 @@ use League\Csv\Writer;
|
|||
|
||||
class CsvHelper
|
||||
{
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function getRecords($filePath, $message): Iterator {
|
||||
public static function getRecords($filePath): Iterator {
|
||||
$csv = Reader::createFromPath(storage_path($filePath));
|
||||
try {
|
||||
$csv->setDelimiter("|");
|
||||
|
@ -25,7 +22,7 @@ class CsvHelper
|
|||
return $csv->getRecords();
|
||||
} catch (InvalidArgument|Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
throw new Exception($message, $e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@ namespace App\Helpers;
|
|||
|
||||
use App\CanastaLog;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use League\Csv\Exception;
|
||||
|
||||
class TransporteHelper
|
||||
{
|
||||
|
@ -21,9 +20,6 @@ class TransporteHelper
|
|||
return self::cantidadTransporte($monto) * self::COSTO_TRANSPORTE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function filaTransporte()
|
||||
{
|
||||
$ultimaCanasta = CanastaLog::where('descripcion', CanastaHelper::CANASTA_CARGADA)
|
||||
|
@ -31,14 +27,12 @@ class TransporteHelper
|
|||
->pluck('path')
|
||||
->first();
|
||||
|
||||
$registros = CsvHelper::getRecords($ultimaCanasta, "No se encontró la ultima canasta.");
|
||||
$error = 'No hay fila de tipo T en la planilla: ' . $ultimaCanasta;
|
||||
$registros = CsvHelper::getRecords($ultimaCanasta);
|
||||
|
||||
foreach ($registros as $key => $registro)
|
||||
if ($registro[CanastaHelper::TIPO] == 'T')
|
||||
return $key;
|
||||
if ($registro[CanastaHelper::TIPO] == 'T') return $key;
|
||||
|
||||
Log::error($error);
|
||||
throw new Exception($error);
|
||||
Log::error('No hay fila de tipo T en la planilla: ' . $ultimaCanasta);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace App\Http\Controllers;
|
||||
|
||||
use App\GrupoDeCompra;
|
||||
use League\Csv\Exception;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
class AdminController extends Controller
|
||||
{
|
||||
|
@ -20,13 +20,9 @@ class AdminController extends Controller
|
|||
$gdc->exportarPedidosAPdf();
|
||||
}
|
||||
|
||||
public function exportarPedidoACSV(GrupoDeCompra $gdc)
|
||||
public function exportarPedidoACSV(GrupoDeCompra $gdc): BinaryFileResponse
|
||||
{
|
||||
try {
|
||||
$gdc->exportarPedidoEnCSV();
|
||||
} catch (Exception $e) {
|
||||
return response()->json(['message' => $e->getMessage()]);
|
||||
}
|
||||
$gdc->exportarPedidoEnCSV();
|
||||
$pattern = storage_path('csv/exports/'. $gdc->nombre . '-*.csv');
|
||||
$files = glob($pattern);
|
||||
|
||||
|
@ -37,13 +33,9 @@ class AdminController extends Controller
|
|||
return response()->download($files[0]);
|
||||
}
|
||||
|
||||
public function exportarPedidoConNucleosACSV(GrupoDeCompra $gdc)
|
||||
public function exportarPedidoConNucleosACSV(GrupoDeCompra $gdc): BinaryFileResponse
|
||||
{
|
||||
try {
|
||||
$gdc->exportarPedidoConNucleosEnCSV();
|
||||
} catch (Exception $e) {
|
||||
return response()->json(['message' => $e->getMessage()]);
|
||||
}
|
||||
$gdc->exportarPedidoConNucleosEnCSV();
|
||||
$pattern = storage_path('csv/exports/'.$gdc->nombre.'-completo-*.csv');
|
||||
$files = glob($pattern);
|
||||
|
||||
|
|
|
@ -4,13 +4,12 @@ namespace App\Http\Controllers;
|
|||
|
||||
use App\GrupoDeCompra;
|
||||
use App\Helpers\CanastaHelper;
|
||||
use App\Helpers\CsvHelper;
|
||||
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
|
||||
{
|
||||
|
@ -23,13 +22,9 @@ class ComisionesController
|
|||
return view('auth/login');
|
||||
}
|
||||
|
||||
public function descargarPedidos()
|
||||
public function descargarPedidos(): BinaryFileResponse
|
||||
{
|
||||
try {
|
||||
Producto::planillaTotales();
|
||||
} catch (Exception $e) {
|
||||
return response()->json(['message' => $e->getMessage()], 500);
|
||||
}
|
||||
Producto::planillaTotales();
|
||||
$pattern = storage_path('csv/exports/pedidos-por-barrio-*.csv');
|
||||
$files = glob($pattern);
|
||||
|
||||
|
@ -64,11 +59,7 @@ class ComisionesController
|
|||
]);
|
||||
|
||||
$nombre = CanastaHelper::guardarCanasta($request->file('data'), self::CANASTAS_PATH);
|
||||
try {
|
||||
CanastaHelper::cargarCanasta(self::CANASTAS_PATH . $nombre);
|
||||
} catch (Exception $e) {
|
||||
return response()->json(['message' => $e->getMessage()], 500);
|
||||
}
|
||||
CanastaHelper::cargarCanasta(self::CANASTAS_PATH . $nombre);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Canasta cargada exitosamente',
|
||||
|
@ -88,11 +79,17 @@ class ComisionesController
|
|||
]);
|
||||
|
||||
$file = $request->file('data')->getPathname();
|
||||
|
||||
$csv = Reader::createFromPath($file, 'r');
|
||||
try {
|
||||
$records = CsvHelper::getRecords($file, "No se pudo leer el archivo.");
|
||||
} catch (Exception $e) {
|
||||
return response()->json(['message' => $e->getMessage()], 500);
|
||||
$csv->setDelimiter("|");
|
||||
$csv->setEnclosure("'");
|
||||
$csv->setHeaderOffset(0);
|
||||
$records = $csv->getRecords();
|
||||
} catch (InvalidArgument|Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
return response()->json([
|
||||
'message' => 'No se pudo leer el csv',
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($records as $record) {
|
||||
|
|
98
app/Http/Controllers/ComprasController.php
Normal file
98
app/Http/Controllers/ComprasController.php
Normal file
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\GrupoDeCompra;
|
||||
use App\Helpers\CanastaHelper;
|
||||
use App\Producto;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use League\Csv\Reader;
|
||||
use DatabaseSeeder;
|
||||
|
||||
class ComprasController
|
||||
{
|
||||
const CANASTAS_PATH = 'csv/canastas/';
|
||||
const BARRIO = "Barrio";
|
||||
const SALDO = "Saldo";
|
||||
|
||||
public function indexPedidos() {
|
||||
return view('compras_pedidos');
|
||||
}
|
||||
|
||||
public function descargarPedidos(): BinaryFileResponse
|
||||
{
|
||||
Producto::planillaTotales();
|
||||
$file = resource_path('csv/exports/pedidos-por-barrio.csv');
|
||||
return response()->download($file);
|
||||
}
|
||||
|
||||
public function descargarNotas(): BinaryFileResponse
|
||||
{
|
||||
Producto::planillaNotas();
|
||||
$file = resource_path('csv/exports/notas-por-barrio.csv');
|
||||
return response()->download($file);
|
||||
}
|
||||
|
||||
public function pdf() {
|
||||
GrupoDeCompra::exportarPedidosBarrialesAPdf();
|
||||
}
|
||||
|
||||
public function show()
|
||||
{
|
||||
return view('auth/login');
|
||||
}
|
||||
|
||||
public function cargarCanasta(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'data' => 'required|file|mimes:csv,txt|max:2048',
|
||||
]);
|
||||
|
||||
$nombre = CanastaHelper::guardarCanasta($request->file('data'), self::CANASTAS_PATH);
|
||||
CanastaHelper::cargarCanasta(self::CANASTAS_PATH . $nombre);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Canasta cargada exitosamente',
|
||||
]);
|
||||
}
|
||||
|
||||
public function descargarCanastaEjemplo(): BinaryFileResponse
|
||||
{
|
||||
$file = resource_path('csv/productos.csv');
|
||||
return response()->download($file);
|
||||
}
|
||||
|
||||
public function cargarSaldos(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'data' => 'required|file|mimes:csv,txt|max:2048',
|
||||
]);
|
||||
|
||||
$file = $request->file('data')->getPathname();
|
||||
$csv = Reader::createFromPath($file, 'r');
|
||||
try {
|
||||
$csv->setDelimiter("|");
|
||||
$csv->setEnclosure("'");
|
||||
$csv->setHeaderOffset(0);
|
||||
$records = $csv->getRecords();
|
||||
} catch (InvalidArgument|Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
return response()->json([
|
||||
'message' => 'No se pudo leer el csv',
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($records as $record) {
|
||||
$barrio = $record[self::BARRIO];
|
||||
$saldo = $record[self::SALDO];
|
||||
GrupoDeCompra::where('nombre', $barrio)
|
||||
->update(['saldo' => $saldo]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Saldos cargados exitosamente',
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -11,7 +11,6 @@ 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
|
||||
{
|
||||
|
@ -76,9 +75,6 @@ class Producto extends Model
|
|||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
static public function planillaTotales()
|
||||
{
|
||||
$headers = ['Producto'];
|
||||
|
|
|
@ -11,7 +11,6 @@ class CanastaSeeder extends Seeder
|
|||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
* @throws \League\Csv\Exception
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
|
|
|
@ -13,11 +13,10 @@ class GrupoDeCompraSeeder extends Seeder
|
|||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
* @throws \League\Csv\Exception
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$registros = CsvHelper::getRecords('csv/barrios.csv', 'No se pudo leer la planilla de barrios.');
|
||||
$registros = CsvHelper::getRecords('csv/barrios.csv');
|
||||
$gdcToInsert = [];
|
||||
$usersToInsert = [];
|
||||
$roles = UserRole::where('nombre', 'barrio')->orWhere('nombre', 'admin_barrio')->get();
|
||||
|
|
Loading…
Add table
Reference in a new issue