-- Function: adempiere.sp_obtener_movimientos_contables()
-- DROP FUNCTION adempiere.sp_obtener_movimientos_contables();
CREATE OR REPLACE FUNCTION adempiere.sp_obtener_movimientos_contables()
RETURNS boolean AS
$BODY$
declare
v_saldos_historicos varchar;
begin
truncate table nic_saldos_finales;
-- Insertar los movimientos totales
drop table if exists tmp_movimientos_totales;
create temporary table tmp_movimientos_totales as
select
f.ad_org_id,
f.c_period_id as id_periodo,
p.periodtype as tipo_periodo,
p.periodno as num_periodo,
p.es_cierre_contable,
p.es_periodo_ajuste,
f.account_id as id_cuenta,
f.dateacct as fecha_cuenta,
sum(f.amtacctdr) as debito_final,
sum(f.amtacctcr) as credito_final
from fact_acct f
join c_period p
on f.c_period_id = p.c_period_id
where f.ad_client_id = 1000001
and f.ad_table_id = 224
and f.line_id > 0
group by
f.ad_org_id,
f.c_period_id,
p.periodtype,
p.periodno,
p.es_cierre_contable,
p.es_periodo_ajuste,
f.account_id,
f.dateacct;
insert into nic_saldos_finales
select
coalesce((select max(s.nic_saldos_finales_id) from nic_saldos_finales s), 999999) +
row_number() over(order by mf.id_periodo, mf.id_cuenta),
ev.ad_client_id,
mf.ad_org_id,
ev.isactive,
ev.created,
ev.createdby,
ev.updated,
ev.updatedby,
mf.id_periodo,
mf.tipo_periodo,
mf.num_periodo,
mf.es_cierre_contable,
mf.es_periodo_ajuste,
mf.id_cuenta,
mf.fecha_cuenta,
ev.accounttype,
ev.accountsign,
mf.debito_final as debito_final,
mf.credito_final as credito_final
from c_elementvalue ev
join tmp_movimientos_totales mf
on ev.c_elementvalue_id = mf.id_cuenta
where ev.ad_client_id = 1000001
and ev.isactive = 'Y';
-- v_saldos_historicos = (select * from fnt_calcularsaldoshistoricos_abiertos());
return true;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
No hay comentarios:
Publicar un comentario