diff --git a/app/Producto.php b/app/Producto.php index 5617617..554c24b 100644 --- a/app/Producto.php +++ b/app/Producto.php @@ -19,15 +19,10 @@ class Producto extends Model public function subpedidos(): BelongsToMany { - return $this->belongsToMany(Subpedido::class, 'productos_subpedidos')->withPivot(["cantidad", "notas"]); + return $this->belongsToMany(Subpedido::class, 'productos_subpedidos') + ->withPivot(["cantidad", "notas"]); } - public static function noBarriales() - { - return self::where('nombre', 'not like', '%barrial%'); - } - - // Este método permite que se apliquen los filtros al hacer una request (por ejemplo, de búsqueda) public function scopeFiltrar($query, FiltroDeProducto $filtros): Builder { return $filtros->aplicar($query); @@ -35,7 +30,9 @@ class Producto extends Model public static function getPaginar(Request $request): int { - return $request->has('paginar') && intval($request->input('paginar')) ? intval($request->input('paginar')) : self::all()->count(); + return $request->has('paginar') && intval($request->input('paginar')) ? + intval($request->input('paginar')) : + self::all()->count(); } public static function productosFilaID() @@ -53,19 +50,29 @@ class Producto extends Model return self::noBarriales()->pluck('nombre', 'id')->all(); } + public static function noBarriales() + { + return self::where('nombre', 'not like', '%barrial%'); + } + static public function cantidadesPorBarrio(): Collection { - $barrios = GrupoDeCompra::barriosMenosPrueba() - ->pluck('id', 'nombre'); + $barrios = GrupoDeCompra::barriosMenosPrueba()->pluck('id', 'nombre'); $columnasBarrios = $barrios->map(function ($id, $nombre) { - return DB::raw("SUM(CASE WHEN subpedidos.grupo_de_compra_id = $id AND subpedidos.aprobado = 1 THEN producto_subpedido.cantidad ELSE 0 END) as `$nombre`"); + return DB::raw(" + SUM(CASE WHEN subpedidos.grupo_de_compra_id = $id + AND subpedidos.aprobado = 1 + AND subpedidos.tipo_pedido_id = 1 + THEN producto_subpedido.cantidad + ELSE 0 END) + as `$nombre`"); })->toArray(); - return DB::table('productos') - ->where('productos.nombre', 'not like', '%barrial%') + return self::noBarriales() ->leftJoin('producto_subpedido', 'productos.id', '=', 'producto_subpedido.producto_id') ->leftJoin('subpedidos', 'subpedidos.id', '=', 'producto_subpedido.subpedido_id') + ->where('subpedidos.tipo_pedido_id', '=', 1) ->select(array_merge( ['productos.fila as fila'], ['productos.nombre as producto'], @@ -82,8 +89,7 @@ class Producto extends Model static public function planillaTotales() { $headers = ['Producto']; - $barrios = GrupoDeCompra::barriosMenosPrueba() - ->pluck('nombre')->toArray(); + $barrios = GrupoDeCompra::barriosMenosPrueba()->pluck('nombre')->toArray(); $headers = array_merge($headers, $barrios); $cantidadesPorBarrio = self::cantidadesPorBarrio(); @@ -109,21 +115,23 @@ class Producto extends Model $ultimaFila = $fila; } - foreach ($transportePorBarrio as $key => $cantidad) { + foreach ($transportePorBarrio as $cantidad) { $planilla[$filaTransporte][] = $cantidad; } $fecha = now()->format('Y-m-d'); - CsvHelper::generarCsv('csv/exports/pedidos-por-barrio- ' . $fecha . '.csv', $planilla, $headers); + $filePath = 'csv/exports/pedidos-por-barrio- ' . $fecha . '.csv'; + CsvHelper::generarCsv($filePath, $planilla, $headers); } public static function notasPorBarrio(): Collection { - return DB::table('productos') + return self::noBarriales() ->join('producto_subpedido', 'productos.id', '=', 'producto_subpedido.producto_id') ->join('subpedidos', 'producto_subpedido.subpedido_id', '=', 'subpedidos.id') ->join('grupos_de_compra', 'subpedidos.grupo_de_compra_id', '=', 'grupos_de_compra.id') ->where('productos.requiere_notas', 1) + ->where('subpedidos.tipo_pedido_id', '=', 1) ->select( 'productos.nombre as producto', 'grupos_de_compra.nombre as barrio', @@ -145,13 +153,16 @@ class Producto extends Model foreach ($notasPorBarrio as $producto => $notasGrupo) { $fila = [$producto]; foreach ($barrios as $barrio) { - $notas = $notasGrupo->where('barrio', $barrio)->pluck('notas')->implode('; '); + $notas = $notasGrupo->where('barrio', $barrio) + ->pluck('notas') + ->implode('; '); $fila[] = $notas ?: ''; } $planilla[] = $fila; } $fecha = now()->format('Y-m-d'); - CsvHelper::generarCsv('csv/exports/notas-por-barrio-' . $fecha . '.csv', $planilla, $headers); + $filePath = 'csv/exports/notas-por-barrio-' . $fecha . '.csv'; + CsvHelper::generarCsv($filePath, $planilla, $headers); } }