diff --git a/app/GrupoDeCompra.php b/app/GrupoDeCompra.php index 8ab1f9c..33c8043 100644 --- a/app/GrupoDeCompra.php +++ b/app/GrupoDeCompra.php @@ -7,7 +7,7 @@ use Mpdf\Mpdf; use League\Csv\CannotInsertRecord; use League\Csv\Writer; use App\Producto; -use DB; +use Illuminate\Support\Facades\DB; use League\Csv\Reader; class GrupoDeCompra extends Model @@ -17,7 +17,34 @@ class GrupoDeCompra extends Model protected $table = 'grupos_de_compra'; protected $hidden = ['password']; - public function toggleDevoluciones() + /** + * @param $gdcs + * @param array $planilla + * @return array + */ + public static function getPlanilla($gdcs, array $planilla): array + { + $barrios = [""]; + foreach ($gdcs as $i => $gdc) { + $barrios[] = $gdc->nombre; + $productos_en_pedido = DB::table('pedidos_aprobados')->where('grupo_de_compra_id', $gdc->id)->get()->keyBy('producto_id'); + $pedidos = $gdc->pedidosAprobados(); + foreach ($productos_en_pedido as $id => $producto_pedido) { + $total = 0; + // Poner cantidad de cada producto para cada núcleo + foreach ($pedidos as $pedido) { + list($_, $_, $cantidad) = $gdc->agregarCantidad($pedido, $id, [], 0, 0); + $total = $total + $cantidad; + } + $fila = Producto::productosIdFila()[$id]; + $planilla[$fila][$i+1] = $total; + } + } + array_splice($planilla, 0, 0, array($barrios)); + return $planilla; + } + + public function toggleDevoluciones() { $this->devoluciones_habilitadas = !$this->devoluciones_habilitadas; $this->save(); @@ -47,7 +74,8 @@ class GrupoDeCompra extends Model $mpdf->Output($filename, "D"); } - function filaVacia(string $product, int $columns) { + static function filaVacia(string $product, int $columns): array + { $fila = [$product]; for ($i = 1; $i <= $columns; $i++) { $fila[$i] = "0"; @@ -56,23 +84,23 @@ class GrupoDeCompra extends Model } //Asume que los productos están gruadados en orden de fila - public function obtenerTemplateDeFilasVacias(int $columns){ + public static function obtenerTemplateDeFilasVacias(int $columns){ $productosFilaID = Producto::productosFilaID(); $productosIDNombre = Producto::productosIDNombre(); $num_fila = 1; $template = []; foreach ($productosFilaID as $fila => $id) { for ($i = $num_fila; $i < $fila; $i++) { - $template[$i] = $this->filaVacia("", $columns); + $template[$i] = GrupoDeCompra::filaVacia("", $columns); } - $template[$fila] = $this->filaVacia($productosIDNombre[$id], $columns); + $template[$fila] = GrupoDeCompra::filaVacia($productosIDNombre[$id], $columns); $num_fila = $fila+1; } - $template[$this->obtenerFilaDeBonoTransporte()] = $this->filaVacia("Bonos de transporte", $columns); + $template[GrupoDeCompra::obtenerFilaDeBonoTransporte()] = GrupoDeCompra::filaVacia("Bonos de transporte", $columns); return $template; } - private function obtenerFilaDeBonoTransporte() { + private static function obtenerFilaDeBonoTransporte() { $csv = Reader::createFromPath(resource_path('csv/productos.csv'), 'r'); $csv->setDelimiter("|"); $csv->setEnclosure("'"); @@ -84,7 +112,7 @@ class GrupoDeCompra extends Model throw new Exception('No hay bono de transporte'); } - private function totalPedidosSinBonos() { + private function totalPedidosSinBonos() { $total = 0; foreach ($this->pedidosAprobados() as $pedido) { $total += ceil($pedido->totalSinBonos()); @@ -116,19 +144,7 @@ class GrupoDeCompra extends Model } public function exportarPedidoEnCSV(){ - $productos_en_pedido = DB::table('pedidos_aprobados')->where('grupo_de_compra_id',$this->id)->get()->keyBy('producto_id'); - - //si no hay pedidos aprobados, salir - if ($productos_en_pedido->count() == 0) { \Log::debug("El grupo de compra ". $this->nombre . " no tiene pedidos aprobados."); return; } - $records = $this->obtenerTemplateDeFilasVacias(1); - $productos_id_fila = Producto::productosIdFila(); - foreach ($productos_en_pedido as $id => $producto_pedido){ - $fila = $productos_id_fila[$id]; - $records[$fila][1] = $producto_pedido->cantidad_pedida; - } - - $records[$this->obtenerFilaDeBonoTransporte()][1] = $this->calcularCantidadBDT(); - + $records = $this->generarColumnaCantidades(); try { $writer = Writer::createFromPath(resource_path('csv/exports/'.$this->nombre.'.csv'), 'w'); $writer->insertAll($records); @@ -137,11 +153,35 @@ class GrupoDeCompra extends Model } } + public function generarColumnaCantidades() { + $productos_en_pedido = DB::table('pedidos_aprobados')->where('grupo_de_compra_id',$this->id)->get()->keyBy('producto_id'); + + //si no hay pedidos aprobados, salir + if ($productos_en_pedido->count() == 0) { + \Log::debug("El grupo de compra ". $this->nombre . " no tiene pedidos aprobados."); + return []; + } + + $records = $this->obtenerTemplateDeFilasVacias(1); + $productos_id_fila = Producto::productosIdFila(); + foreach ($productos_en_pedido as $id => $producto_pedido){ + $fila = $productos_id_fila[$id]; + $records[$fila][1] = $producto_pedido->cantidad_pedida; + } + + $records[$this->obtenerFilaDeBonoTransporte()][1] = $this->calcularCantidadBDT(); + + return $records; + } + public function exportarPedidoConNucleosEnCSV() { $productos_en_pedido = DB::table('pedidos_aprobados')->where('grupo_de_compra_id',$this->id)->get()->keyBy('producto_id'); // si no hay pedidos aprobados, salir - if ($productos_en_pedido->count() == 0) { \Log::debug("El grupo de compra ". $this->nombre . " no tiene pedidos aprobados."); return; } + if ($productos_en_pedido->count() == 0) { + \Log::debug("El grupo de compra ". $this->nombre . " no tiene pedidos aprobados."); + return; + } $pedidos = $this->pedidosAprobados(); // Generar tabla vacía con una columna por núcleo @@ -153,9 +193,7 @@ class GrupoDeCompra extends Model $i = 1; // Poner cantidad de cada producto para cada núcleo foreach ($pedidos as $pedido) { - $producto = $pedido->productos()->find($id); - $records[$fila][$i] = $producto == NULL ? 0 : $producto["pivot"]["cantidad"]; - $i++; + list($records, $i, $_) = $this->agregarCantidad($pedido, $id, $records, $fila, $i); } } // Insertar lista de núcleos en la primera fila @@ -176,8 +214,26 @@ class GrupoDeCompra extends Model } } + public function agregarCantidad($pedido, $id, array $records, $fila, int $i): array + { + $producto = $pedido->productos()->find($id); + $cantidad = $producto == NULL ? 0 : $producto->pivot->cantidad; + $records[$fila][$i] = $cantidad; + $i++; + return array($records, $i, $cantidad); + } + public static function exportarTodosLosPedidosEnCSV(){ - $gdcs=GrupoDeCompra::all(); - foreach ($gdcs as $gdc){ $gdc->exportarPedidoEnCSV(); } + $gdcs = GrupoDeCompra::all(); + $planilla = GrupoDeCompra::obtenerTemplateDeFilasVacias($gdcs->count()); + $planilla = self::getPlanilla($gdcs, $planilla); + + // Guardar en un archivo .csv + try { + $writer = Writer::createFromPath(resource_path('csv/exports/total-pedidos.csv'), 'w'); + $writer->insertAll($planilla); + } catch (CannotInsertRecord $e) { + var_export($e->getRecords()); + } } } diff --git a/app/Http/Controllers/Api/PedidoController.php b/app/Http/Controllers/Api/PedidoController.php deleted file mode 100644 index 8f2ec61..0000000 --- a/app/Http/Controllers/Api/PedidoController.php +++ /dev/null @@ -1,9 +0,0 @@ -is_admin) { + if ($user->is_compras) { + return redirect('compras/pedidos'); + } else if ($user->is_admin) { session(['admin_gdc' => $user->grupo_de_compra_id]); return redirect('admin/pedidos'); } else { diff --git a/app/Http/Controllers/ComprasController.php b/app/Http/Controllers/ComprasController.php new file mode 100644 index 0000000..5c7e3d1 --- /dev/null +++ b/app/Http/Controllers/ComprasController.php @@ -0,0 +1,23 @@ +download($file); + } + + public function show() + { + return view('auth/compras_login'); + } +} diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 62c91ee..243f1f2 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -57,6 +57,7 @@ class Kernel extends HttpKernel protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'admin' => \App\Http\Middleware\Admin::class, + 'compras' => \App\Http\Middleware\Compras::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, diff --git a/app/Http/Middleware/Compras.php b/app/Http/Middleware/Compras.php new file mode 100644 index 0000000..a9d9f6b --- /dev/null +++ b/app/Http/Middleware/Compras.php @@ -0,0 +1,29 @@ +route('compras_login.show'); + + if (Auth::user()->is_compras) { + return $next($request); + } else { + return response('Necesitás ser de comisión compras para hacer esto', 403); + } + } +} diff --git a/app/Producto.php b/app/Producto.php index 624230e..1a70545 100644 --- a/app/Producto.php +++ b/app/Producto.php @@ -38,14 +38,14 @@ class Producto extends Model return $request->has('paginar') && intval($request->input('paginar')) ? intval($request->input('paginar')) : self::$paginarPorDefecto; } - public static function productosIDFila() { - return Producto::pluck('fila', 'id',)->all(); - } - public static function productosFilaID() { return Producto::pluck('id', 'fila',)->all(); } + public static function productosIDFila() { + return Producto::pluck('fila', 'id',)->all(); + } + public static function productosIDNombre() { return Producto::pluck('nombre', 'id',)->all(); } diff --git a/database/migrations/2024_08_28_000025_agregar_is_compras_a_user.php b/database/migrations/2024_08_28_000025_agregar_is_compras_a_user.php new file mode 100644 index 0000000..57d12ac --- /dev/null +++ b/database/migrations/2024_08_28_000025_agregar_is_compras_a_user.php @@ -0,0 +1,32 @@ +boolean('is_compras')->after('is_admin')->default(0); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('producto_subpedido', function (Blueprint $table) { + $table->dropColumn('is_compras'); + }); + } +} diff --git a/resources/js/app.js b/resources/js/app.js index 76282e5..9867ac8 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -4,7 +4,6 @@ * building robust, powerful web applications using Vue and Laravel. */ import axios from 'axios'; -import animate from 'animate.css'; import Vue from 'vue'; window.Vue = require('vue'); window.Event = new Vue(); @@ -18,8 +17,8 @@ window.bulmaToast = require('bulma-toast'); * * Eg. ./components/ExampleComponent.vue -> */ -const files = require.context('./', true, /\.vue$/i) -files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default)) +import './components'; + /** * Constants */ diff --git a/resources/js/components.js b/resources/js/components.js new file mode 100644 index 0000000..d2ca0bb --- /dev/null +++ b/resources/js/components.js @@ -0,0 +1,25 @@ +import Vue from 'vue'; + +const requireComponent = require.context('./components', true, /\.vue$/); + +// Registro automático de componentes: +// e.g. components/foo/bar/UnComponente.vue +// se registra como 'foo-bar-un-componente' +requireComponent.keys().forEach(fileName => { + // Get the component config + const componentConfig = requireComponent(fileName); + // Get the PascalCase name of the component + const componentName = fileName + .replace(/^\.\/(.*)\.\w+$/, '$1') // Remove "./" from the beginning and the file extension from the end + .replace(/\//g, '-') // Replace directories with hyphens + .replace(/([a-z])([A-Z])/g, '$1-$2') // Insert hyphen between camelCase words + .toLowerCase() // Convert to lowercase + // Globally register the component + Vue.component( + componentName, + // Look for the component options on `.default`, which will + // exist if the component was exported with `export default`, + // otherwise fall back to module's root. + componentConfig.default || componentConfig + ); +}); diff --git a/resources/js/components/PedidoBody.vue b/resources/js/components/PedidoBody.vue deleted file mode 100644 index d8e671d..0000000 --- a/resources/js/components/PedidoBody.vue +++ /dev/null @@ -1,31 +0,0 @@ - - - diff --git a/resources/js/components/PedidosAdminBody.vue b/resources/js/components/admin/Body.vue similarity index 73% rename from resources/js/components/PedidosAdminBody.vue rename to resources/js/components/admin/Body.vue index 90fd8e1..5010fa6 100644 --- a/resources/js/components/PedidosAdminBody.vue +++ b/resources/js/components/admin/Body.vue @@ -1,15 +1,15 @@ - - diff --git a/resources/js/components/PedidosAdminCaracteristicasOpcionales.vue b/resources/js/components/admin/CaracteristicasOpcionales.vue similarity index 71% rename from resources/js/components/PedidosAdminCaracteristicasOpcionales.vue rename to resources/js/components/admin/CaracteristicasOpcionales.vue index 50d72e7..242dfb2 100644 --- a/resources/js/components/PedidosAdminCaracteristicasOpcionales.vue +++ b/resources/js/components/admin/CaracteristicasOpcionales.vue @@ -1,10 +1,8 @@ diff --git a/resources/js/components/BarrioSelect.vue b/resources/js/components/comunes/BarrioSelect.vue similarity index 100% rename from resources/js/components/BarrioSelect.vue rename to resources/js/components/comunes/BarrioSelect.vue diff --git a/resources/js/components/NavBar.vue b/resources/js/components/comunes/NavBar.vue similarity index 88% rename from resources/js/components/NavBar.vue rename to resources/js/components/comunes/NavBar.vue index efe574a..bb5a6d4 100644 --- a/resources/js/components/NavBar.vue +++ b/resources/js/components/comunes/NavBar.vue @@ -11,7 +11,7 @@ - + - +
- {{ (passwordVisible ? 'Ocultar' : 'Mostrar') + ' contraseña'}} + {{ (passwordVisible ? 'Ocultar' : 'Mostrar') + ' contraseña'}}
@@ -24,7 +24,6 @@ + } + diff --git a/resources/js/components/SubpedidoSelect.vue b/resources/js/components/pedidos/SubpedidoSelect.vue similarity index 100% rename from resources/js/components/SubpedidoSelect.vue rename to resources/js/components/pedidos/SubpedidoSelect.vue diff --git a/resources/views/auth/admin_login.blade.php b/resources/views/auth/admin_login.blade.php index fe34390..f1d4a07 100644 --- a/resources/views/auth/admin_login.blade.php +++ b/resources/views/auth/admin_login.blade.php @@ -21,11 +21,11 @@ Contraseña incorrecta, intentalo nuevamente. @enderror - +
@csrf - - + +
diff --git a/resources/views/auth/admin_subpedidos.blade.php b/resources/views/auth/admin_subpedidos.blade.php index d714ce6..76fedf2 100644 --- a/resources/views/auth/admin_subpedidos.blade.php +++ b/resources/views/auth/admin_subpedidos.blade.php @@ -1,13 +1,5 @@ @extends('layouts.app') @section('content') - + @endsection - diff --git a/resources/views/auth/compras_login.blade.php b/resources/views/auth/compras_login.blade.php new file mode 100644 index 0000000..6217038 --- /dev/null +++ b/resources/views/auth/compras_login.blade.php @@ -0,0 +1,33 @@ + + + + + + {{ config('app.name', 'Pedidos del MPS') }} + + + + + +
+
+

+ Pedidos MPS +

+

+ Bienvenidx a la sección de compras de la aplicación del Mercado Popular de Subsistencia +

+ @error('name') +
+ Contraseña incorrecta, intentalo nuevamente. +
+ @enderror +
+ @csrf + +
+
+
+ + + diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php index d3a2e5f..822042a 100644 --- a/resources/views/auth/login.blade.php +++ b/resources/views/auth/login.blade.php @@ -11,7 +11,7 @@
- +

Pedidos MPS

@@ -23,20 +23,14 @@ Contraseña incorrecta, intentalo nuevamente.
@enderror - +
@csrf - - + +
- diff --git a/resources/views/compras_pedidos.blade.php b/resources/views/compras_pedidos.blade.php new file mode 100644 index 0000000..92cf9fe --- /dev/null +++ b/resources/views/compras_pedidos.blade.php @@ -0,0 +1,5 @@ +@extends('layouts.app') + +@section('content') + +@endsection diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 8ba4413..a06c090 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -19,7 +19,7 @@
- + - - + +
- + @yield('content')
diff --git a/resources/views/productos.blade.php b/resources/views/productos.blade.php index d3877a4..3b90bcf 100644 --- a/resources/views/productos.blade.php +++ b/resources/views/productos.blade.php @@ -1,6 +1,6 @@ @extends('layouts.app') @section('content') - - + + @endsection diff --git a/resources/views/subpedidos_create.blade.php b/resources/views/subpedidos_create.blade.php index fbeb87e..db0ccaf 100644 --- a/resources/views/subpedidos_create.blade.php +++ b/resources/views/subpedidos_create.blade.php @@ -9,7 +9,7 @@

Bienvenidx a la aplicación de pedidos del Mercado Popular de Subsistencia

- + @endsection diff --git a/routes/web.php b/routes/web.php index f0df5a8..c2b0ea0 100644 --- a/routes/web.php +++ b/routes/web.php @@ -75,3 +75,10 @@ Route::middleware('auth')->group( function() { })->name('obtenerSesion'); }); }); + +Route::get('/compras', 'ComprasController@show')->name('compras_login.show'); + +Route::middleware(['compras'])->group( function() { + Route::get('/compras/pedidos', 'ComprasController@indexPedidos')->name('compras.pedidos'); + Route::get('/compras/pedidos/descargar', 'ComprasController@descargarPedidos')->name('compras.pedidos.descargar'); +});