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\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
|
||||||
{
|
{
|
||||||
|
@ -166,10 +165,6 @@ 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();
|
||||||
|
@ -187,9 +182,6 @@ class GrupoDeCompra extends Model
|
||||||
return $template;
|
return $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function exportarPedidoEnCSV()
|
public function exportarPedidoEnCSV()
|
||||||
{
|
{
|
||||||
$records = $this->generarColumnaCantidades();
|
$records = $this->generarColumnaCantidades();
|
||||||
|
@ -198,9 +190,6 @@ 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();
|
||||||
|
@ -223,9 +212,6 @@ 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();
|
||||||
|
|
|
@ -9,7 +9,6 @@ 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
|
||||||
{
|
{
|
||||||
|
@ -46,14 +45,10 @@ 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, "No se pudo leer el archivo.");
|
$registros = CsvHelper::getRecords($archivo);
|
||||||
|
|
||||||
$toInsert = [];
|
$toInsert = [];
|
||||||
$categoria = '';
|
$categoria = '';
|
||||||
|
|
||||||
|
|
|
@ -13,10 +13,7 @@ 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("|");
|
||||||
|
@ -25,7 +22,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());
|
||||||
throw new Exception($message, $e);
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ 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
|
||||||
{
|
{
|
||||||
|
@ -21,9 +20,6 @@ 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)
|
||||||
|
@ -31,14 +27,12 @@ class TransporteHelper
|
||||||
->pluck('path')
|
->pluck('path')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
$registros = CsvHelper::getRecords($ultimaCanasta, "No se encontró la ultima canasta.");
|
$registros = CsvHelper::getRecords($ultimaCanasta);
|
||||||
$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')
|
if ($registro[CanastaHelper::TIPO] == 'T') return $key;
|
||||||
return $key;
|
|
||||||
|
|
||||||
Log::error($error);
|
Log::error('No hay fila de tipo T en la planilla: ' . $ultimaCanasta);
|
||||||
throw new Exception($error);
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\GrupoDeCompra;
|
use App\GrupoDeCompra;
|
||||||
use League\Csv\Exception;
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||||
|
|
||||||
class AdminController extends Controller
|
class AdminController extends Controller
|
||||||
{
|
{
|
||||||
|
@ -20,13 +20,9 @@ class AdminController extends Controller
|
||||||
$gdc->exportarPedidosAPdf();
|
$gdc->exportarPedidosAPdf();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function exportarPedidoACSV(GrupoDeCompra $gdc)
|
public function exportarPedidoACSV(GrupoDeCompra $gdc): BinaryFileResponse
|
||||||
{
|
{
|
||||||
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);
|
||||||
|
|
||||||
|
@ -37,13 +33,9 @@ class AdminController extends Controller
|
||||||
return response()->download($files[0]);
|
return response()->download($files[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function exportarPedidoConNucleosACSV(GrupoDeCompra $gdc)
|
public function exportarPedidoConNucleosACSV(GrupoDeCompra $gdc): BinaryFileResponse
|
||||||
{
|
{
|
||||||
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);
|
||||||
|
|
||||||
|
|
|
@ -4,13 +4,12 @@ namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\GrupoDeCompra;
|
use App\GrupoDeCompra;
|
||||||
use App\Helpers\CanastaHelper;
|
use App\Helpers\CanastaHelper;
|
||||||
use App\Helpers\CsvHelper;
|
|
||||||
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
|
||||||
{
|
{
|
||||||
|
@ -23,13 +22,9 @@ class ComisionesController
|
||||||
return view('auth/login');
|
return view('auth/login');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function descargarPedidos()
|
public function descargarPedidos(): BinaryFileResponse
|
||||||
{
|
{
|
||||||
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);
|
||||||
|
|
||||||
|
@ -64,11 +59,7 @@ 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',
|
||||||
|
@ -88,11 +79,17 @@ class ComisionesController
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$file = $request->file('data')->getPathname();
|
$file = $request->file('data')->getPathname();
|
||||||
|
$csv = Reader::createFromPath($file, 'r');
|
||||||
try {
|
try {
|
||||||
$records = CsvHelper::getRecords($file, "No se pudo leer el archivo.");
|
$csv->setDelimiter("|");
|
||||||
} catch (Exception $e) {
|
$csv->setEnclosure("'");
|
||||||
return response()->json(['message' => $e->getMessage()], 500);
|
$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) {
|
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\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
|
||||||
{
|
{
|
||||||
|
@ -76,9 +75,6 @@ class Producto extends Model
|
||||||
->get();
|
->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
static public function planillaTotales()
|
static public function planillaTotales()
|
||||||
{
|
{
|
||||||
$headers = ['Producto'];
|
$headers = ['Producto'];
|
||||||
|
|
|
@ -11,7 +11,6 @@ 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()
|
||||||
{
|
{
|
||||||
|
|
|
@ -13,11 +13,10 @@ 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', 'No se pudo leer la planilla de barrios.');
|
$registros = CsvHelper::getRecords('csv/barrios.csv');
|
||||||
$gdcToInsert = [];
|
$gdcToInsert = [];
|
||||||
$usersToInsert = [];
|
$usersToInsert = [];
|
||||||
$roles = UserRole::where('nombre', 'barrio')->orWhere('nombre', 'admin_barrio')->get();
|
$roles = UserRole::where('nombre', 'barrio')->orWhere('nombre', 'admin_barrio')->get();
|
||||||
|
|
Loading…
Add table
Reference in a new issue