salix/db/routines/vn2008/procedures/suppliersDebt.sql

36 lines
904 B
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`suppliersDebt`()
BEGIN
SELECT abbreviation as Empresa, Proveedor, Euros, divisa, empresa_id, proveedor_id
FROM
(
SELECT empresa_id,
proveedor_id,
cast(sum(Euros) AS DECIMAL(10,2)) as Euros,
cast(sum(divisa) AS DECIMAL(10,2)) as divisa
FROM
(
SELECT empresa_id,
proveedor_id,
-1 * cantidad as Euros,
-1 * divisa as divisa
FROM recibida_vencimiento rv
INNER JOIN recibida r ON r.id = rv.recibida_id
WHERE r.fecha > '2014-12-31'
UNION ALL
SELECT empresa_id,
id_proveedor,
importe,
divisa
FROM pago
WHERE fecha > '2014-12-31'
) sub
GROUP BY empresa_id, proveedor_id
) sub2
JOIN Proveedores p ON p.Id_Proveedor = sub2.proveedor_id
JOIN empresa e ON e.id = sub2.empresa_id;
END$$
DELIMITER ;