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

	IF bolCLIENT THEN

		SELECT m.Id_Article
				, Article
				, IF(Cantidad < 0, - Cantidad, NULL) Entrada
				, IF(Cantidad < 0, NULL, Cantidad) Salida
				, 'T' Tabla
				, t.id Registro
				, t.shipped Fecha
				, w.name Almacen
				, cast(Preu as Decimal(5,2)) Precio
				, c.Cliente Proveedor
				, abbreviation Empresa
			FROM Movimientos m
				JOIN Articles a using(Id_Article)
				JOIN vn.ticket t ON t.id = m.Id_Ticket
				JOIN empresa e ON e.id = t.companyFk
				JOIN vn.warehouse w ON w.id = t.warehouseFk
				JOIN Consignatarios cs ON cs.Id_Consigna = t.addressFk
				JOIN Clientes c ON c.Id_Cliente = cs.Id_Cliente
			WHERE cs.Id_Cliente = idPEOPLE
				AND m.Id_Article = idARTICLE
				AND t.shipped > '2010-01-01';

	ELSE

		SELECT Id_Article,
				Tabla, 
				Registro,
				Fecha,
				Article,
				w.name Almacen,
				Entrada,
				Salida,
				Proveedor,
				cast(Precio as Decimal(5,2)) Precio
		FROM
			(SELECT Id_Article
				, IF(Cantidad > 0, Cantidad, NULL) Entrada
				, IF(Cantidad > 0, NULL,- Cantidad) Salida
				, 'E' Tabla
				, Id_Entrada Registro
				, landing Fecha
				, tr.warehouse_id
				, Costefijo Precio
			FROM Compres c
				JOIN Entradas e using(Id_Entrada)
				JOIN travel tr ON tr.id = travel_id
			WHERE Id_Proveedor = idPEOPLE
				AND Id_Article = idARTICLE
				AND landing >= '2010-01-01'

			union all

			SELECT Id_Article
				, IF(Cantidad < 0, - Cantidad, NULL) Entrada
				, IF(Cantidad < 0, NULL, Cantidad) Salida
				, 'T'
				, Id_Ticket
				, Fecha
				, t.warehouseFk warehouse_id
				, Preu
			FROM Movimientos m
				JOIN vn.ticket t ON t.id = m.Id_Ticket
				JOIN Consignatarios cs ON cs.Id_Consigna = t.addressFk
				JOIN proveedores_clientes pc ON pc.Id_Cliente = cs.Id_Cliente
			WHERE Id_Proveedor = idPEOPLE
				AND Id_Article = idARTICLE
				AND t.shipped > '2010-01-01') mov
			JOIN Articles a using(Id_Article)
			JOIN Proveedores p ON Id_Proveedor = idPEOPLE
			JOIN warehouse w ON w.id = mov.warehouse_id;
	END IF;
END$$
DELIMITER ;
