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

52 lines
1.9 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`Cajas_Saldo_Detalle`()
BEGIN
Select Proveedores.Id_Proveedor, Proveedor, Fecha, round(Importe) as Importe
from Proveedores
join
(
select Fecha, sub.Id_Proveedor, sum(Importe) as Importe
from
(
select round(sum(Importe),2) as Importe, TIMESTAMPADD(DAY,-1,util.VN_CURDATE()) as Fecha, Id_Proveedor
from pago
join Proveedores using(Id_Proveedor)
where Fecha >= '2011-01-01'
and Id_Proveedor NOT IN (select id from empresa)
group by Id_Proveedor
union all
select IFNULL(-1 * round(sum(rv.Cantidad / if(Id_Moneda = 2,rate,1)),2),0) AS Vto, TIMESTAMPADD(DAY,-1,util.VN_CURDATE()), proveedor_id
from recibida_vencimiento rv
join recibida r on r.id = rv.recibida_id
join reference_rate rr on rr.`date` = rv.fecha
join Proveedores P on P.Id_Proveedor = r.proveedor_id
join Paises PS on PS.Id = P.pais_id
where rv.fecha between '2011-01-01' and util.VN_CURDATE()-1
and Id_Proveedor NOT IN (select id from empresa)
group by proveedor_id
union all
select -1 * round(rv.Cantidad / if(Id_Moneda = 2,(SELECT value FROM vn.referenceRate ORDER BY dated DESC LIMIT 1),1),2) AS Vto, rv.fecha, proveedor_id
from recibida_vencimiento rv
join recibida r on r.id = rv.recibida_id
join Proveedores P on P.Id_Proveedor = r.proveedor_id
join Paises PS on PS.Id = P.pais_id
where rv.fecha >= util.VN_CURDATE()
and Id_Proveedor NOT IN (select id from empresa)
union all
select @remesas:= @pago:= @saldo:= 0.0000, util.VN_CURDATE(), @prov:= 0) sub
group by Fecha,Id_Proveedor
) sub2 using(Id_Proveedor)
having Importe < -100
;
END$$
DELIMITER ;