DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`embalajes_stocks`(IN idPEOPLE INT, IN bolCLIENT BOOLEAN)
BEGIN

if bolCLIENT then

	select m.Id_Article,  Article, - cast(sum(m.Cantidad) as decimal) as Saldo
	from Movimientos m
    join Articles a on m.Id_Article = a.Id_Article
    join Tipos tp on tp.tipo_id = a.tipo_id 
	join Tickets t using(Id_Ticket)
	join Consignatarios cs using(Id_Consigna)
	where cs.Id_Cliente = idPEOPLE
    and Tipo = 'Contenedores'
	and t.Fecha > '2010-01-01'
    group by m.Id_Article;

else

select Id_Article, Article, sum(Cantidad) as Saldo
from
(select Id_Article, Cantidad
from Compres c
join Articles a using(Id_Article)
join Tipos tp using(tipo_id)
join Entradas e using(Id_Entrada)
join travel tr on tr.id = travel_id
where Id_Proveedor = idPEOPLE
and landing >= '2010-01-01'
and reino_id = 6

union all

select Id_Article,  - Cantidad
from Movimientos m
join Articles a using(Id_Article)
join Tipos tp using(tipo_id)
join Tickets t using(Id_Ticket)
join Consignatarios cs using(Id_Consigna)
join proveedores_clientes pc on pc.Id_Cliente = cs.Id_Cliente
where Id_Proveedor = idPEOPLE
and reino_id = 6
and t.Fecha > '2010-01-01') mov

join Articles a using(Id_Article)
group by Id_Article;

end if;

END$$
DELIMITER ;
