jueves, 15 de agosto de 2013
Scheduled Payments in Adempiere
-- select * from rep_pagos_programados('130815', 0);
drop function if exists rep_pagos_programados(fecha_corte date, id_tercero numeric(10,0));
create or replace function rep_pagos_programados(fecha_corte date, id_tercero numeric(10,0))
returns table
(
id_factura numeric(10,0),
num_factura varchar,
fecha_factura date,
fecha_vencimiento date,
id_proveedor numeric(10,0),
cod_proveedor varchar,
nombre_proveedor varchar,
monto_extranjero numeric(10,2),
monto_nacional numeric(10,2),
concepto_factura varchar
) as $$
begin
return query
(
with cte_pagos_programados as
(
select
f.c_invoice_id as id_factura_x,
f.documentno as num_factura_x,
cast(f.dateinvoiced as date) as fecha_factura_x,
(f.dateinvoiced + pt.netdays) as fecha_vencimiento_x,
bp.c_bpartner_id id_proveedor_x,
bp.value cod_proveedor_x,
cast(upper(bp.name) as varchar) nombre_proveedor_x,
cast(case f.c_currency_id
when 100 then -- monto extranjero
f.grandtotal
else
0.00
end as numeric(10,2)) as monto_extranjero_x,
cast(case f.c_currency_id
when 100 then -- monto extranjero (convertir a nacional)
f.grandtotal *
coalesce((select cr.multiplyrate from c_conversion_rate cr
where cr.validfrom = f.dateinvoiced
and cr.validto = f.dateinvoiced), 0.00)
else
f.grandtotal -- monto_nacional
end as numeric(10,2)) as monto_nacional_x,
f.description as concepto_factura_x
from c_invoice f
join c_paymentterm pt
on f.c_paymentterm_id = pt.c_paymentterm_id
join c_bpartner bp
on f.c_bpartner_id = bp.c_bpartner_id
where f.ad_client_id = 1000001
and f.issotrx = 'N' -- no son ventas
and f.c_doctypetarget_id in (1000005) -- AP Invoice
and f.docstatus = 'CO' -- completada
and not exists
(
select 1 from c_payment p
where p.ad_client_id = 1000001
and p.c_invoice_id = f.c_invoice_id
)
and not exists
(
select 1 from c_paymentallocate p
where p.ad_client_id = 1000001
and p.c_invoice_id = f.c_invoice_id
)
)
select
c.id_factura_x,
c.num_factura_x,
c.fecha_factura_x,
c.fecha_vencimiento_x,
c.id_proveedor_x,
c.cod_proveedor_x,
c.nombre_proveedor_x,
c.monto_extranjero_x,
c.monto_nacional_x,
c.concepto_factura_x
from cte_pagos_programados c
where c.fecha_factura_x <= fecha_corte
and c.id_proveedor_x = case when id_tercero = 0 then c.id_proveedor_x else id_tercero end
order by
c.id_proveedor_x,
c.fecha_vencimiento_x
);
end;
$$ language plpgsql;
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario