63 lines
1.5 KiB
PHP
63 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class CrearPedidosAprobadosViewCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'view:CrearPedidosAprobadosView';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Crea view "pedidos_aprobados"';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
DB::statement("
|
|
CREATE OR REPLACE VIEW pedidos_aprobados
|
|
AS
|
|
SELECT
|
|
g.id as grupo_de_compra_id,
|
|
g.nombre as grupo_de_compra_nombre,
|
|
g.region as grupo_de_compra_region,
|
|
pr.id AS producto_id,
|
|
pr.nombre as producto_nombre,
|
|
pr.precio as producto_precio,
|
|
SUM(ps.cantidad) as cantidad_pedida,
|
|
pr.precio*SUM(ps.cantidad) as total_por_producto
|
|
FROM grupos_de_compra g
|
|
JOIN subpedidos s ON (s.grupo_de_compra_id = g.id AND s.aprobado=1)
|
|
JOIN producto_subpedido ps ON (ps.subpedido_id = s.id)
|
|
JOIN productos pr ON (pr.id = ps.producto_id)
|
|
GROUP BY
|
|
g.id, g.nombre, pr.id, pr.nombre
|
|
;");
|
|
return 0;
|
|
}
|
|
}
|