Descargar diseño aqui.
-- Function: adempiere.rep_balance_general(numeric, numeric, character, character)
-- DROP FUNCTION adempiere.rep_balance_general(numeric, numeric, character, character);
CREATE OR REPLACE FUNCTION adempiere.rep_balance_general(
IN id_compania numeric,
IN id_periodo numeric,
IN es_cierre_contable character,
IN es_periodo_ajuste character)
RETURNS TABLE(nombre_compania character varying, id_tipo_cuenta character, tipo_cuenta character varying, id_naturaleza character, id_cuenta_mayor numeric, cod_cuenta_mayor numeric, cuenta_mayor character varying, id_cuenta_padre numeric, cod_cuenta_padre numeric, cuenta_padre character varying, id_cuenta_contable numeric, cod_cuenta_contable character varying, cuenta_contable character varying, id_etiqueta numeric, etiqueta character varying, orden integer, id_titulo numeric, titulo character varying, id_subtitulo numeric, subtitulo character varying, pre_titulo character varying, pre_subtitulo character varying, saldo numeric, total_balance numeric, fecha_ini date, fecha_fin date) AS
$BODY$
declare
nombre_compania_x varchar(255);
p_movimientos_totales boolean;
p_tipo_periodo char;
p_num_periodo numeric(10,0);
finicial date;
ffinal date;
v_periodo numeric(10,0);
begin
nombre_compania_x =
(
select c.name from ad_client c
where c.ad_client_id = id_compania
);
-- Obtener el tipo de periodo (estandar o ajuste) y el consecutivo de periodo
select
p.periodtype,
p.periodno,
p.startdate,
p.enddate
into
p_tipo_periodo,
p_num_periodo,
finicial,
ffinal
from c_period p
where p.c_period_id = id_periodo;
v_periodo =
(
select
p.c_period_id
from c_period p
where p.periodno =
case
when es_cierre_contable = 'Y' then
p_num_periodo - 1
else
p_num_periodo
end
);
drop table if exists tmp_cuentas_balance;
create temporary table tmp_cuentas_balance as
select
u.accounttype as id_tipo_cuenta_x,
case u.accounttype
when 'A' then 'ACTIVO'
when 'L' then 'PASIVO Y PATRIMONIO'
when 'O' then 'PASIVO Y PATRIMONIO'
when 'E' then 'PASIVO Y PATRIMONIO'
else ''
end::varchar as tipo_cuenta_x,
u.nat as id_naturaleza_x,
u.ctamayor_id as id_cuenta_mayor_x,
u.ctmayor_value as cod_cuenta_mayor_x,
u.ctmayor_name as cuenta_mayor_x,
u.parent_id as id_cuenta_padre_x,
u.parent_value as cod_cuenta_padre_x,
u.parent_name as cuenta_padre_x,
u.c_elementvalue_id as id_cuenta_contable_x,
u.value as cod_cuenta_contable_x,
u.name as cuenta_contable_x,
bg.nic_balance_grupo_id as id_etiqueta_x,
bg.name as etiqueta_x,
bc.orden as orden_x,
bt.nic_balance_titulo_id as id_titulo_x,
bt.name as titulo_x,
bs.nic_balance_subtitulo_id as id_subtitulo_x,
bs.name as subtitulo_x,
bt.prefijo as pre_titulo_x,
bs.prefijo as pre_subtitulo_x
from usr_v_vcc u
join nic_balance_cuentas bc
on u.parent_id = bc.account_id
join nic_balance_titulo bt
on bc.nic_balance_titulo_id = bt.nic_balance_titulo_id
join nic_balance_subtitulo bs
on bc.nic_balance_subtitulo_id = bs.nic_balance_subtitulo_id
join nic_balance_grupo bg
on bc.nic_balance_grupo_id = bg.nic_balance_grupo_id
where u.c_elementvalue_id in
(
-- select bc.account_id from nic_balance_cuentas bc
select distinct sf.id_cuenta from nic_saldos_finales sf
) and u.accounttype in ('A', 'L', 'O', 'E')
and u.nat in ('D', 'C');
drop table if exists tmp_saldos_balance;
create temporary table tmp_saldos_balance as
select
sf.tipo_cuenta as tipo_cuenta_x,
sf.id_cuenta as id_cuenta_contable_x,
sf.id_periodo as id_periodo_x,
sf.tipo_periodo as tipo_periodo_x,
sf.num_periodo as num_periodo_x,
sf.es_cierre_contable as es_cierre_contable_x,
sf.es_periodo_ajuste as es_periodo_ajuste_x,
coalesce(sf.debito_final, 0.00) - coalesce(sf.credito_final, 0.00) as dc_x,
coalesce(sf.credito_final, 0.00) - coalesce(sf.debito_final, 0.00) as cd_x
from nic_saldos_finales sf
where sf.ad_client_id = id_compania
and sf.tipo_periodo in ('S', 'A')
and sf.es_cierre_contable = 'N'
and sf.id_cuenta in
(
select cc.id_cuenta_contable_x from tmp_cuentas_balance cc
)
and sf.num_periodo <= p_num_periodo
union all
select
sf.tipo_cuenta as tipo_cuenta_x,
sf.id_cuenta as id_cuenta_contable_x,
sf.id_periodo as id_periodo_x,
sf.tipo_periodo as tipo_periodo_x,
sf.num_periodo as num_periodo_x,
sf.es_cierre_contable as es_cierre_contable_x,
sf.es_periodo_ajuste as es_periodo_ajuste_x,
coalesce(sf.debito_final, 0.00) - coalesce(sf.credito_final, 0.00) as dc_x,
coalesce(sf.credito_final, 0.00) - coalesce(sf.debito_final, 0.00) as cd_x
from nic_saldos_finales sf
where sf.ad_client_id = id_compania
and sf.tipo_periodo in ('S', 'A')
and sf.es_cierre_contable = 'Y'
and sf.id_cuenta in
(
select cc.id_cuenta_contable_x from tmp_cuentas_balance cc
)
and sf.num_periodo <= p_num_periodo;
drop table if exists tmp_balance_general;
create temporary table tmp_balance_general as
select
cc.id_tipo_cuenta_x,
cc.tipo_cuenta_x,
cc.id_naturaleza_x,
cc.id_cuenta_mayor_x,
cc.cod_cuenta_mayor_x,
cc.cuenta_mayor_x,
cc.id_cuenta_padre_x,
cc.cod_cuenta_padre_x,
cc.cuenta_padre_x,
cc.id_cuenta_contable_x,
cc.cod_cuenta_contable_x,
cc.cuenta_contable_x,
cc.id_etiqueta_x,
cc.etiqueta_x,
cc.orden_x,
cc.id_titulo_x,
cc.titulo_x,
cc.id_subtitulo_x,
cc.subtitulo_x,
cc.pre_titulo_x,
cc.pre_subtitulo_x,
coalesce(case cc.id_tipo_cuenta_x
when 'A' then
case cc.id_naturaleza_x
when 'D' then
(select sum(sb.dc_x) from tmp_saldos_balance sb
where sb.id_cuenta_contable_x = cc.id_cuenta_contable_x)
when 'C' then
(select sum(sb.dc_x) from tmp_saldos_balance sb
where sb.id_cuenta_contable_x = cc.id_cuenta_contable_x)
else
0.00
end
else
case cc.id_naturaleza_x
when 'D' then
(select sum(sb.cd_x) from tmp_saldos_balance sb
where sb.id_cuenta_contable_x = cc.id_cuenta_contable_x)
when 'C' then
(select sum(sb.cd_x) from tmp_saldos_balance sb
where sb.id_cuenta_contable_x = cc.id_cuenta_contable_x)
else
0.00
end
end, 0.00) as saldo_x
from tmp_cuentas_balance cc;
drop table if exists tmp_balance_ultimate;
create temporary table tmp_balance_ultimate as
select
t.id_tipo_cuenta_x,
t.tipo_cuenta_x,
t.id_naturaleza_x,
t.id_cuenta_mayor_x,
t.cod_cuenta_mayor_x,
t.cuenta_mayor_x,
t.id_cuenta_padre_x,
t.cod_cuenta_padre_x,
t.cuenta_padre_x,
t.id_cuenta_contable_x,
t.cod_cuenta_contable_x,
t.cuenta_contable_x,
t.id_etiqueta_x,
t.etiqueta_x,
t.orden_x,
t.id_titulo_x,
t.titulo_x,
t.id_subtitulo_x,
t.subtitulo_x,
t.pre_titulo_x,
t.pre_subtitulo_x,
sum(t.saldo_x) as saldo_x,
(select sum(t2.saldo_x) from tmp_balance_general t2) as total_balance_x
from tmp_balance_general t
group by
t.id_tipo_cuenta_x,
t.tipo_cuenta_x,
t.id_naturaleza_x,
t.id_cuenta_mayor_x,
t.cod_cuenta_mayor_x,
t.cuenta_mayor_x,
t.id_cuenta_padre_x,
t.cod_cuenta_padre_x,
t.cuenta_padre_x,
t.id_cuenta_contable_x,
t.cod_cuenta_contable_x,
t.cuenta_contable_x,
t.id_etiqueta_x,
t.etiqueta_x,
t.orden_x,
t.id_titulo_x,
t.titulo_x,
t.id_subtitulo_x,
t.subtitulo_x,
t.pre_titulo_x,
t.pre_subtitulo_x
union all
select
null as id_tipo_cuenta_x,
'PASIVO Y PATRIMONIO' as tipo_cuenta_x,
'C' as id_naturaleza_x,
null as id_cuenta_mayor_x,
'30100000' as cod_cuenta_mayor_x,
'CAPITAL SOCIAL' as cuenta_mayor_x,
null as id_cuenta_padre_x,
null as cod_cuenta_padre_x,
'Utilidad o Perdida Acumulada' as cuenta_padre_x,
null as id_cuenta_contable_x,
null as cod_cuenta_contable_x,
null as cuenta_contable_x,
null as id_etiqueta_x,
'Súper Avit o Déficit del Período' as etiqueta_x,
100 as orden_x,
1000002 as id_titulo_x,
'PATRIMONIO' as titulo_x,
1000004 as id_subtitulo_x,
'UTILIDAD O PERDIDA' as subtitulo_x,
'TOTAL' as pre_titulo_x,
'TOTAL' as pre_subtitulo_x,
coalesce((select sum(er.saldo_acum) from rep_estado_resultado(id_compania, id_periodo, es_cierre_contable, es_periodo_ajuste) er), 0.00) as saldo_x,
0.00 as total_balance_x;
return query
(
select
nombre_compania_x,
t.id_tipo_cuenta_x,
t.tipo_cuenta_x,
t.id_naturaleza_x,
t.id_cuenta_mayor_x,
t.cod_cuenta_mayor_x,
t.cuenta_mayor_x,
t.id_cuenta_padre_x,
t.cod_cuenta_padre_x,
t.cuenta_padre_x,
t.id_cuenta_contable_x,
t.cod_cuenta_contable_x,
t.cuenta_contable_x,
t.id_etiqueta_x,
t.etiqueta_x,
t.orden_x,
t.id_titulo_x,
t.titulo_x,
t.id_subtitulo_x,
t.subtitulo_x,
t.pre_titulo_x,
t.pre_subtitulo_x,
t.saldo_x,
t.total_balance_x,
finicial as fecha_ini_x,
case
when es_cierre_contable = 'Y' then
(extract(year from ffinal)::varchar || '1231')::date
when es_periodo_ajuste = 'Y' then
(extract(year from ffinal)::varchar || '1231')::date
else ffinal
end as fecha_fin_x
from tmp_balance_ultimate t
where abs(t.saldo_x) > 0
order by
t.orden_x,
t.cuenta_padre_x,
t.cuenta_mayor_x,
t.etiqueta_x,
t.cod_cuenta_contable_x asc
);
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
Mostrando entradas con la etiqueta Development Adempiere. Mostrar todas las entradas
Mostrando entradas con la etiqueta Development Adempiere. Mostrar todas las entradas
martes, 23 de enero de 2018
Estado de Resultado en ADempiere
Descargar diseño aqui.
-- Function: adempiere.rep_estado_resultado(numeric, numeric, character, character)
-- DROP FUNCTION adempiere.rep_estado_resultado(numeric, numeric, character, character);
CREATE OR REPLACE FUNCTION adempiere.rep_estado_resultado(
IN id_compania numeric,
IN id_periodo numeric,
IN es_cierre_contable character,
IN es_periodo_ajuste character)
RETURNS TABLE(nombre_compania character varying, id_tipo_cuenta character, tipo_cuenta character varying, id_naturaleza character, id_cuenta_mayor numeric, cod_cuenta_mayor numeric, cuenta_mayor character varying, id_cuenta_padre numeric, cod_cuenta_padre numeric, cuenta_padre character varying, id_cuenta_contable numeric, cod_cuenta_contable character varying, cuenta_contable character varying, orden integer, id_titulo numeric, titulo character varying, id_subtitulo numeric, subtitulo character varying, pre_titulo character varying, pre_subtitulo character varying, saldo numeric, saldo_acum numeric, porc numeric, porc_acum numeric, fecha_ini date, fecha_fin date) AS
$BODY$
declare
nombre_compania_x varchar(255);
p_movimientos_totales boolean;
p_tipo_periodo char;
p_num_periodo numeric(10,0);
finicial date;
ffinal date;
begin
nombre_compania_x =
(
select c.name from ad_client c
where c.ad_client_id = id_compania
);
-- Obtener el tipo de periodo (estandar o ajuste) y el consecutivo de periodo
select
p.periodtype,
p.periodno,
p.startdate,
p.enddate
into
p_tipo_periodo,
p_num_periodo,
finicial,
ffinal
from c_period p
where p.c_period_id = id_periodo;
drop table if exists tmp_cuentas_estado;
create temporary table tmp_cuentas_estado as
select
u.accounttype as id_tipo_cuenta_x,
case u.accounttype
when 'R' then '01 - INGRESOS'
when 'E' then '02 - EGRESOS'
else ''
end::varchar as tipo_cuenta_x,
u.nat as id_naturaleza_x,
u.ctamayor_id as id_cuenta_mayor_x,
u.ctmayor_value as cod_cuenta_mayor_x,
u.ctmayor_name as cuenta_mayor_x,
u.parent_id as id_cuenta_padre_x,
u.parent_value as cod_cuenta_padre_x,
u.parent_name as cuenta_padre_x,
u.c_elementvalue_id as id_cuenta_contable_x,
u.value as cod_cuenta_contable_x,
u.name as cuenta_contable_x,
ce.orden as orden_x,
te.nic_titulo_estado_id as id_titulo_x,
te.name as titulo_x,
se.nic_subtitulo_estado_id::numeric(10,0) as id_subtitulo_x,
se.name as subtitulo_x,
coalesce(te.prefijo, '') as pre_titulo_x,
coalesce(se.prefijo, '') as pre_subtitulo_x
from usr_v_vcc u
join nic_cuentas_estado ce
on ce.account_id = u.parent_id
join nic_titulo_estado te
on ce.nic_titulo_estado_id = te.nic_titulo_estado_id
join nic_subtitulo_estado se
on ce.nic_subtitulo_estado_id = se.nic_subtitulo_estado_id
where u.c_elementvalue_id in
(
select distinct sf.id_cuenta from nic_saldos_finales sf
);
drop table if exists tmp_movimientos_estado;
create temporary table tmp_movimientos_estado
(
tipo_cuenta_x char,
id_cuenta_contable_x numeric(10,0),
id_periodo_x numeric(10,0),
tipo_periodo_x char,
num_periodo_x numeric(10,0),
es_cierre_contable_x char,
es_periodo_ajuste_x char,
saldo_x numeric(10,2),
saldo_acum_x numeric(10,2)
);
if(es_cierre_contable = 'N')then
insert into tmp_movimientos_estado
with cte_movimientos_estado as
(
select
sf.tipo_cuenta as tipo_cuenta_x,
sf.id_cuenta as id_cuenta_contable_x,
sf.id_periodo as id_periodo_x,
sf.tipo_periodo as tipo_periodo_x,
sf.num_periodo as num_periodo_x,
sf.es_cierre_contable as es_cierre_contable_x,
sf.es_periodo_ajuste as es_periodo_ajuste_x,
sum(coalesce(sf.credito_final, 0.00) - coalesce(sf.debito_final, 0.00))::numeric(10,2) as saldo_x,
0.00::numeric(10,2) as saldo_acum_x
from nic_saldos_finales sf
where sf.tipo_cuenta in ('R', 'E') -- Ingresos y Gastos (Estado de Resultado)
and sf.tipo_periodo = p_tipo_periodo
and sf.id_periodo = id_periodo
group by
sf.tipo_cuenta,
sf.id_cuenta,
sf.id_periodo,
sf.tipo_periodo,
sf.num_periodo,
sf.es_cierre_contable,
sf.es_periodo_ajuste
union all
select
sf.tipo_cuenta as tipo_cuenta_x,
sf.id_cuenta as id_cuenta_contable_x,
sf.id_periodo as id_periodo_x,
sf.tipo_periodo as tipo_periodo_x,
sf.num_periodo as num_periodo_x,
sf.es_cierre_contable as es_cierre_contable_x,
sf.es_periodo_ajuste as es_periodo_ajuste_x,
sum(coalesce(sf.credito_final, 0.00) - coalesce(sf.debito_final, 0.00))::numeric(10,2) as saldo_x,
0.00::numeric(10,2) as saldo_acum_x
from nic_saldos_finales sf
where sf.tipo_cuenta in ('R', 'E') -- Ingresos y Gastos (Estado de Resultado)
and sf.num_periodo =
case
when es_periodo_ajuste = 'Y' then
p_num_periodo - 1
else
null
end
group by
sf.tipo_cuenta,
sf.id_cuenta,
sf.id_periodo,
sf.tipo_periodo,
sf.num_periodo,
sf.es_cierre_contable,
sf.es_periodo_ajuste
union all
select
sf.tipo_cuenta as tipo_cuenta_x,
sf.id_cuenta as id_cuenta_contable_x,
sf.id_periodo as id_periodo_x,
sf.tipo_periodo as tipo_periodo_x,
sf.num_periodo as num_periodo_x,
sf.es_cierre_contable as es_cierre_contable_x,
sf.es_periodo_ajuste as es_periodo_ajuste_x,
0.00::numeric(10,2) as saldo_x,
sum(coalesce(sf.credito_final, 0.00) - coalesce(sf.debito_final, 0.00))::numeric(10,2) as saldo_acum_x
from nic_saldos_finales sf
where sf.tipo_cuenta in ('R', 'E') -- Ingresos y Gastos (Estado de Resultado)
and sf.num_periodo <= p_num_periodo
group by
sf.tipo_cuenta,
sf.id_cuenta,
sf.id_periodo,
sf.tipo_periodo,
sf.num_periodo,
sf.es_cierre_contable,
sf.es_periodo_ajuste
)
select
me.tipo_cuenta_x,
me.id_cuenta_contable_x,
me.id_periodo_x,
me.tipo_periodo_x,
me.num_periodo_x,
me.es_cierre_contable_x,
me.es_periodo_ajuste_x,
sum(me.saldo_x) as saldo_x,
sum(me.saldo_acum_x) as saldo_acum_x
from cte_movimientos_estado me
group by
me.tipo_cuenta_x,
me.id_cuenta_contable_x,
me.id_periodo_x,
me.tipo_periodo_x,
me.num_periodo_x,
me.es_cierre_contable_x,
me.es_periodo_ajuste_x,
me.saldo_x,
me.saldo_acum_x;
else
insert into tmp_movimientos_estado
with cte_movimientos_estado as
(
-- Movimientos contables del período seleccionado
select
sf.tipo_cuenta as tipo_cuenta_x,
sf.id_cuenta as id_cuenta_contable_x,
sf.id_periodo as id_periodo_x,
sf.tipo_periodo as tipo_periodo_x,
sf.num_periodo as num_periodo_x,
sf.es_cierre_contable as es_cierre_contable_x,
sf.es_periodo_ajuste as es_periodo_ajuste_x,
sum(coalesce(sf.credito_final, 0.00) - coalesce(sf.debito_final, 0.00))::numeric(10,2) as saldo_x,
0.00::numeric(10,2) as saldo_acum_x
from nic_saldos_finales sf
where sf.tipo_cuenta in ('R', 'E') -- Ingresos y Gastos (Estado de Resultado)
and sf.tipo_periodo = p_tipo_periodo
and sf.id_periodo = id_periodo
group by
sf.tipo_cuenta,
sf.id_cuenta,
sf.id_periodo,
sf.tipo_periodo,
sf.num_periodo,
sf.es_cierre_contable,
sf.es_periodo_ajuste
union all
-- Movimientos contables acumulados sin cierre y sin ajuste
select
sf.tipo_cuenta as tipo_cuenta_x,
sf.id_cuenta as id_cuenta_contable_x,
sf.id_periodo as id_periodo_x,
sf.tipo_periodo as tipo_periodo_x,
sf.num_periodo as num_periodo_x,
sf.es_cierre_contable as es_cierre_contable_x,
sf.es_periodo_ajuste as es_periodo_ajuste_x,
0.00::numeric(10,2) as saldo_x,
0.00::numeric(10,2) as saldo_acum_x
from nic_saldos_finales sf
where sf.tipo_cuenta in ('R', 'E') -- Ingresos y Gastos (Estado de Resultado)
and sf.num_periodo <= p_num_periodo
group by
sf.tipo_cuenta,
sf.id_cuenta,
sf.id_periodo,
sf.tipo_periodo,
sf.num_periodo,
sf.es_cierre_contable,
sf.es_periodo_ajuste
)
select
me.tipo_cuenta_x,
me.id_cuenta_contable_x,
me.id_periodo_x,
me.tipo_periodo_x,
me.num_periodo_x,
me.es_cierre_contable_x,
me.es_periodo_ajuste_x,
sum(me.saldo_x) as saldo_x,
sum(me.saldo_acum_x) as saldo_acum_x
from cte_movimientos_estado me
group by
me.tipo_cuenta_x,
me.id_cuenta_contable_x,
me.id_periodo_x,
me.tipo_periodo_x,
me.num_periodo_x,
me.es_cierre_contable_x,
me.es_periodo_ajuste_x,
me.saldo_x,
me.saldo_acum_x;
end if;
return query
(
with cte_estado_preliminar as
(
select
ce.id_tipo_cuenta_x,
ce.tipo_cuenta_x,
ce.id_naturaleza_x,
ce.id_cuenta_mayor_x,
ce.cod_cuenta_mayor_x,
ce.cuenta_mayor_x,
ce.id_cuenta_padre_x,
ce.cod_cuenta_padre_x,
ce.cuenta_padre_x,
ce.id_cuenta_contable_x,
ce.cod_cuenta_contable_x,
ce.cuenta_contable_x,
ce.orden_x,
ce.id_titulo_x,
ce.titulo_x,
ce.id_subtitulo_x,
ce.subtitulo_x,
ce.pre_titulo_x,
ce.pre_subtitulo_x,
me.saldo_x,
me.saldo_acum_x
from tmp_movimientos_estado me
join tmp_cuentas_estado ce
on me.id_cuenta_contable_x = ce.id_cuenta_contable_x
),
cte_estado_resultado as
(
select
er.id_tipo_cuenta_x,
er.tipo_cuenta_x,
er.id_naturaleza_x,
er.id_cuenta_mayor_x,
er.cod_cuenta_mayor_x,
er.cuenta_mayor_x,
er.id_cuenta_padre_x,
er.cod_cuenta_padre_x,
er.cuenta_padre_x,
er.id_cuenta_contable_x,
er.cod_cuenta_contable_x,
er.cuenta_contable_x,
er.orden_x,
er.id_titulo_x,
er.titulo_x,
er.id_subtitulo_x,
er.subtitulo_x,
er.pre_titulo_x,
er.pre_subtitulo_x,
er.saldo_x,
er.saldo_acum_x,
(select sum(er2.saldo_x) from cte_estado_preliminar er2
where er2.subtitulo_x = er.subtitulo_x) as porc_x,
(select sum(er2.saldo_acum_x) from cte_estado_preliminar er2
where er2.subtitulo_x = er.subtitulo_x) as porc_acum_x
from cte_estado_preliminar er
),
cte_estado_penultimo as
(
select
er.id_tipo_cuenta_x,
er.tipo_cuenta_x,
er.id_naturaleza_x,
er.id_cuenta_mayor_x,
er.cod_cuenta_mayor_x,
er.cuenta_mayor_x,
er.id_cuenta_padre_x,
er.cod_cuenta_padre_x,
er.cuenta_padre_x,
er.id_cuenta_contable_x,
er.cod_cuenta_contable_x,
er.cuenta_contable_x,
er.orden_x,
er.id_titulo_x,
er.titulo_x,
er.id_subtitulo_x,
er.subtitulo_x,
er.pre_titulo_x,
er.pre_subtitulo_x,
er.saldo_x,
er.saldo_acum_x,
case
when er.porc_x = 0 then
0.00
else
er.saldo_x / er.porc_x * 100
end::numeric(18,9) as porc_x,
case
when er.porc_acum_x = 0 then
0.00
else
er.saldo_acum_x / er.porc_acum_x * 100
end::numeric(18,9) as porc_acum_x
from cte_estado_resultado er
),
cte_estado_ultimate as
(
select
er.id_tipo_cuenta_x,
er.tipo_cuenta_x,
er.id_naturaleza_x,
er.id_cuenta_mayor_x,
er.cod_cuenta_mayor_x,
er.cuenta_mayor_x,
er.id_cuenta_padre_x,
er.cod_cuenta_padre_x,
er.cuenta_padre_x,
er.id_cuenta_contable_x,
er.cod_cuenta_contable_x,
er.cuenta_contable_x,
er.orden_x,
er.id_titulo_x,
er.titulo_x,
er.id_subtitulo_x,
er.subtitulo_x,
er.pre_titulo_x,
er.pre_subtitulo_x,
sum(er.saldo_x) as saldo_x,
sum(er.saldo_acum_x) as saldo_acum_x,
sum(er.porc_x) as porc_x,
sum(er.porc_acum_x) as porc_acum_x
from cte_estado_penultimo er
group by
er.id_tipo_cuenta_x,
er.tipo_cuenta_x,
er.id_naturaleza_x,
er.id_cuenta_mayor_x,
er.cod_cuenta_mayor_x,
er.cuenta_mayor_x,
er.id_cuenta_padre_x,
er.cod_cuenta_padre_x,
er.cuenta_padre_x,
er.id_cuenta_contable_x,
er.cod_cuenta_contable_x,
er.cuenta_contable_x,
er.orden_x,
er.id_titulo_x,
er.titulo_x,
er.id_subtitulo_x,
er.subtitulo_x,
er.pre_titulo_x,
er.pre_subtitulo_x
)
select
nombre_compania_x,
er.id_tipo_cuenta_x,
er.tipo_cuenta_x,
er.id_naturaleza_x,
er.id_cuenta_mayor_x,
er.cod_cuenta_mayor_x,
er.cuenta_mayor_x,
er.id_cuenta_padre_x,
er.cod_cuenta_padre_x,
er.cuenta_padre_x,
er.id_cuenta_contable_x,
er.cod_cuenta_contable_x,
er.cuenta_contable_x,
er.orden_x,
er.id_titulo_x,
er.titulo_x,
er.id_subtitulo_x,
er.subtitulo_x,
er.pre_titulo_x,
er.pre_subtitulo_x,
er.saldo_x,
er.saldo_acum_x,
er.porc_x,
er.porc_acum_x,
finicial as fecha_ini_x,
case
when es_cierre_contable = 'Y' then
(extract(year from ffinal)::varchar || '1231')::date
when es_periodo_ajuste = 'Y' then
(extract(year from ffinal)::varchar || '1231')::date
else ffinal
end as fecha_fin_x
from cte_estado_ultimate er
where (abs(er.saldo_x) > 0 or abs(er.saldo_acum_x) > 0)
order by
er.orden_x,
er.cuenta_padre_x,
er.cuenta_mayor_x,
er.cuenta_contable_x,
er.cod_cuenta_mayor_x asc
);
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
-- Function: adempiere.rep_estado_resultado(numeric, numeric, character, character)
-- DROP FUNCTION adempiere.rep_estado_resultado(numeric, numeric, character, character);
CREATE OR REPLACE FUNCTION adempiere.rep_estado_resultado(
IN id_compania numeric,
IN id_periodo numeric,
IN es_cierre_contable character,
IN es_periodo_ajuste character)
RETURNS TABLE(nombre_compania character varying, id_tipo_cuenta character, tipo_cuenta character varying, id_naturaleza character, id_cuenta_mayor numeric, cod_cuenta_mayor numeric, cuenta_mayor character varying, id_cuenta_padre numeric, cod_cuenta_padre numeric, cuenta_padre character varying, id_cuenta_contable numeric, cod_cuenta_contable character varying, cuenta_contable character varying, orden integer, id_titulo numeric, titulo character varying, id_subtitulo numeric, subtitulo character varying, pre_titulo character varying, pre_subtitulo character varying, saldo numeric, saldo_acum numeric, porc numeric, porc_acum numeric, fecha_ini date, fecha_fin date) AS
$BODY$
declare
nombre_compania_x varchar(255);
p_movimientos_totales boolean;
p_tipo_periodo char;
p_num_periodo numeric(10,0);
finicial date;
ffinal date;
begin
nombre_compania_x =
(
select c.name from ad_client c
where c.ad_client_id = id_compania
);
-- Obtener el tipo de periodo (estandar o ajuste) y el consecutivo de periodo
select
p.periodtype,
p.periodno,
p.startdate,
p.enddate
into
p_tipo_periodo,
p_num_periodo,
finicial,
ffinal
from c_period p
where p.c_period_id = id_periodo;
drop table if exists tmp_cuentas_estado;
create temporary table tmp_cuentas_estado as
select
u.accounttype as id_tipo_cuenta_x,
case u.accounttype
when 'R' then '01 - INGRESOS'
when 'E' then '02 - EGRESOS'
else ''
end::varchar as tipo_cuenta_x,
u.nat as id_naturaleza_x,
u.ctamayor_id as id_cuenta_mayor_x,
u.ctmayor_value as cod_cuenta_mayor_x,
u.ctmayor_name as cuenta_mayor_x,
u.parent_id as id_cuenta_padre_x,
u.parent_value as cod_cuenta_padre_x,
u.parent_name as cuenta_padre_x,
u.c_elementvalue_id as id_cuenta_contable_x,
u.value as cod_cuenta_contable_x,
u.name as cuenta_contable_x,
ce.orden as orden_x,
te.nic_titulo_estado_id as id_titulo_x,
te.name as titulo_x,
se.nic_subtitulo_estado_id::numeric(10,0) as id_subtitulo_x,
se.name as subtitulo_x,
coalesce(te.prefijo, '') as pre_titulo_x,
coalesce(se.prefijo, '') as pre_subtitulo_x
from usr_v_vcc u
join nic_cuentas_estado ce
on ce.account_id = u.parent_id
join nic_titulo_estado te
on ce.nic_titulo_estado_id = te.nic_titulo_estado_id
join nic_subtitulo_estado se
on ce.nic_subtitulo_estado_id = se.nic_subtitulo_estado_id
where u.c_elementvalue_id in
(
select distinct sf.id_cuenta from nic_saldos_finales sf
);
drop table if exists tmp_movimientos_estado;
create temporary table tmp_movimientos_estado
(
tipo_cuenta_x char,
id_cuenta_contable_x numeric(10,0),
id_periodo_x numeric(10,0),
tipo_periodo_x char,
num_periodo_x numeric(10,0),
es_cierre_contable_x char,
es_periodo_ajuste_x char,
saldo_x numeric(10,2),
saldo_acum_x numeric(10,2)
);
if(es_cierre_contable = 'N')then
insert into tmp_movimientos_estado
with cte_movimientos_estado as
(
select
sf.tipo_cuenta as tipo_cuenta_x,
sf.id_cuenta as id_cuenta_contable_x,
sf.id_periodo as id_periodo_x,
sf.tipo_periodo as tipo_periodo_x,
sf.num_periodo as num_periodo_x,
sf.es_cierre_contable as es_cierre_contable_x,
sf.es_periodo_ajuste as es_periodo_ajuste_x,
sum(coalesce(sf.credito_final, 0.00) - coalesce(sf.debito_final, 0.00))::numeric(10,2) as saldo_x,
0.00::numeric(10,2) as saldo_acum_x
from nic_saldos_finales sf
where sf.tipo_cuenta in ('R', 'E') -- Ingresos y Gastos (Estado de Resultado)
and sf.tipo_periodo = p_tipo_periodo
and sf.id_periodo = id_periodo
group by
sf.tipo_cuenta,
sf.id_cuenta,
sf.id_periodo,
sf.tipo_periodo,
sf.num_periodo,
sf.es_cierre_contable,
sf.es_periodo_ajuste
union all
select
sf.tipo_cuenta as tipo_cuenta_x,
sf.id_cuenta as id_cuenta_contable_x,
sf.id_periodo as id_periodo_x,
sf.tipo_periodo as tipo_periodo_x,
sf.num_periodo as num_periodo_x,
sf.es_cierre_contable as es_cierre_contable_x,
sf.es_periodo_ajuste as es_periodo_ajuste_x,
sum(coalesce(sf.credito_final, 0.00) - coalesce(sf.debito_final, 0.00))::numeric(10,2) as saldo_x,
0.00::numeric(10,2) as saldo_acum_x
from nic_saldos_finales sf
where sf.tipo_cuenta in ('R', 'E') -- Ingresos y Gastos (Estado de Resultado)
and sf.num_periodo =
case
when es_periodo_ajuste = 'Y' then
p_num_periodo - 1
else
null
end
group by
sf.tipo_cuenta,
sf.id_cuenta,
sf.id_periodo,
sf.tipo_periodo,
sf.num_periodo,
sf.es_cierre_contable,
sf.es_periodo_ajuste
union all
select
sf.tipo_cuenta as tipo_cuenta_x,
sf.id_cuenta as id_cuenta_contable_x,
sf.id_periodo as id_periodo_x,
sf.tipo_periodo as tipo_periodo_x,
sf.num_periodo as num_periodo_x,
sf.es_cierre_contable as es_cierre_contable_x,
sf.es_periodo_ajuste as es_periodo_ajuste_x,
0.00::numeric(10,2) as saldo_x,
sum(coalesce(sf.credito_final, 0.00) - coalesce(sf.debito_final, 0.00))::numeric(10,2) as saldo_acum_x
from nic_saldos_finales sf
where sf.tipo_cuenta in ('R', 'E') -- Ingresos y Gastos (Estado de Resultado)
and sf.num_periodo <= p_num_periodo
group by
sf.tipo_cuenta,
sf.id_cuenta,
sf.id_periodo,
sf.tipo_periodo,
sf.num_periodo,
sf.es_cierre_contable,
sf.es_periodo_ajuste
)
select
me.tipo_cuenta_x,
me.id_cuenta_contable_x,
me.id_periodo_x,
me.tipo_periodo_x,
me.num_periodo_x,
me.es_cierre_contable_x,
me.es_periodo_ajuste_x,
sum(me.saldo_x) as saldo_x,
sum(me.saldo_acum_x) as saldo_acum_x
from cte_movimientos_estado me
group by
me.tipo_cuenta_x,
me.id_cuenta_contable_x,
me.id_periodo_x,
me.tipo_periodo_x,
me.num_periodo_x,
me.es_cierre_contable_x,
me.es_periodo_ajuste_x,
me.saldo_x,
me.saldo_acum_x;
else
insert into tmp_movimientos_estado
with cte_movimientos_estado as
(
-- Movimientos contables del período seleccionado
select
sf.tipo_cuenta as tipo_cuenta_x,
sf.id_cuenta as id_cuenta_contable_x,
sf.id_periodo as id_periodo_x,
sf.tipo_periodo as tipo_periodo_x,
sf.num_periodo as num_periodo_x,
sf.es_cierre_contable as es_cierre_contable_x,
sf.es_periodo_ajuste as es_periodo_ajuste_x,
sum(coalesce(sf.credito_final, 0.00) - coalesce(sf.debito_final, 0.00))::numeric(10,2) as saldo_x,
0.00::numeric(10,2) as saldo_acum_x
from nic_saldos_finales sf
where sf.tipo_cuenta in ('R', 'E') -- Ingresos y Gastos (Estado de Resultado)
and sf.tipo_periodo = p_tipo_periodo
and sf.id_periodo = id_periodo
group by
sf.tipo_cuenta,
sf.id_cuenta,
sf.id_periodo,
sf.tipo_periodo,
sf.num_periodo,
sf.es_cierre_contable,
sf.es_periodo_ajuste
union all
-- Movimientos contables acumulados sin cierre y sin ajuste
select
sf.tipo_cuenta as tipo_cuenta_x,
sf.id_cuenta as id_cuenta_contable_x,
sf.id_periodo as id_periodo_x,
sf.tipo_periodo as tipo_periodo_x,
sf.num_periodo as num_periodo_x,
sf.es_cierre_contable as es_cierre_contable_x,
sf.es_periodo_ajuste as es_periodo_ajuste_x,
0.00::numeric(10,2) as saldo_x,
0.00::numeric(10,2) as saldo_acum_x
from nic_saldos_finales sf
where sf.tipo_cuenta in ('R', 'E') -- Ingresos y Gastos (Estado de Resultado)
and sf.num_periodo <= p_num_periodo
group by
sf.tipo_cuenta,
sf.id_cuenta,
sf.id_periodo,
sf.tipo_periodo,
sf.num_periodo,
sf.es_cierre_contable,
sf.es_periodo_ajuste
)
select
me.tipo_cuenta_x,
me.id_cuenta_contable_x,
me.id_periodo_x,
me.tipo_periodo_x,
me.num_periodo_x,
me.es_cierre_contable_x,
me.es_periodo_ajuste_x,
sum(me.saldo_x) as saldo_x,
sum(me.saldo_acum_x) as saldo_acum_x
from cte_movimientos_estado me
group by
me.tipo_cuenta_x,
me.id_cuenta_contable_x,
me.id_periodo_x,
me.tipo_periodo_x,
me.num_periodo_x,
me.es_cierre_contable_x,
me.es_periodo_ajuste_x,
me.saldo_x,
me.saldo_acum_x;
end if;
return query
(
with cte_estado_preliminar as
(
select
ce.id_tipo_cuenta_x,
ce.tipo_cuenta_x,
ce.id_naturaleza_x,
ce.id_cuenta_mayor_x,
ce.cod_cuenta_mayor_x,
ce.cuenta_mayor_x,
ce.id_cuenta_padre_x,
ce.cod_cuenta_padre_x,
ce.cuenta_padre_x,
ce.id_cuenta_contable_x,
ce.cod_cuenta_contable_x,
ce.cuenta_contable_x,
ce.orden_x,
ce.id_titulo_x,
ce.titulo_x,
ce.id_subtitulo_x,
ce.subtitulo_x,
ce.pre_titulo_x,
ce.pre_subtitulo_x,
me.saldo_x,
me.saldo_acum_x
from tmp_movimientos_estado me
join tmp_cuentas_estado ce
on me.id_cuenta_contable_x = ce.id_cuenta_contable_x
),
cte_estado_resultado as
(
select
er.id_tipo_cuenta_x,
er.tipo_cuenta_x,
er.id_naturaleza_x,
er.id_cuenta_mayor_x,
er.cod_cuenta_mayor_x,
er.cuenta_mayor_x,
er.id_cuenta_padre_x,
er.cod_cuenta_padre_x,
er.cuenta_padre_x,
er.id_cuenta_contable_x,
er.cod_cuenta_contable_x,
er.cuenta_contable_x,
er.orden_x,
er.id_titulo_x,
er.titulo_x,
er.id_subtitulo_x,
er.subtitulo_x,
er.pre_titulo_x,
er.pre_subtitulo_x,
er.saldo_x,
er.saldo_acum_x,
(select sum(er2.saldo_x) from cte_estado_preliminar er2
where er2.subtitulo_x = er.subtitulo_x) as porc_x,
(select sum(er2.saldo_acum_x) from cte_estado_preliminar er2
where er2.subtitulo_x = er.subtitulo_x) as porc_acum_x
from cte_estado_preliminar er
),
cte_estado_penultimo as
(
select
er.id_tipo_cuenta_x,
er.tipo_cuenta_x,
er.id_naturaleza_x,
er.id_cuenta_mayor_x,
er.cod_cuenta_mayor_x,
er.cuenta_mayor_x,
er.id_cuenta_padre_x,
er.cod_cuenta_padre_x,
er.cuenta_padre_x,
er.id_cuenta_contable_x,
er.cod_cuenta_contable_x,
er.cuenta_contable_x,
er.orden_x,
er.id_titulo_x,
er.titulo_x,
er.id_subtitulo_x,
er.subtitulo_x,
er.pre_titulo_x,
er.pre_subtitulo_x,
er.saldo_x,
er.saldo_acum_x,
case
when er.porc_x = 0 then
0.00
else
er.saldo_x / er.porc_x * 100
end::numeric(18,9) as porc_x,
case
when er.porc_acum_x = 0 then
0.00
else
er.saldo_acum_x / er.porc_acum_x * 100
end::numeric(18,9) as porc_acum_x
from cte_estado_resultado er
),
cte_estado_ultimate as
(
select
er.id_tipo_cuenta_x,
er.tipo_cuenta_x,
er.id_naturaleza_x,
er.id_cuenta_mayor_x,
er.cod_cuenta_mayor_x,
er.cuenta_mayor_x,
er.id_cuenta_padre_x,
er.cod_cuenta_padre_x,
er.cuenta_padre_x,
er.id_cuenta_contable_x,
er.cod_cuenta_contable_x,
er.cuenta_contable_x,
er.orden_x,
er.id_titulo_x,
er.titulo_x,
er.id_subtitulo_x,
er.subtitulo_x,
er.pre_titulo_x,
er.pre_subtitulo_x,
sum(er.saldo_x) as saldo_x,
sum(er.saldo_acum_x) as saldo_acum_x,
sum(er.porc_x) as porc_x,
sum(er.porc_acum_x) as porc_acum_x
from cte_estado_penultimo er
group by
er.id_tipo_cuenta_x,
er.tipo_cuenta_x,
er.id_naturaleza_x,
er.id_cuenta_mayor_x,
er.cod_cuenta_mayor_x,
er.cuenta_mayor_x,
er.id_cuenta_padre_x,
er.cod_cuenta_padre_x,
er.cuenta_padre_x,
er.id_cuenta_contable_x,
er.cod_cuenta_contable_x,
er.cuenta_contable_x,
er.orden_x,
er.id_titulo_x,
er.titulo_x,
er.id_subtitulo_x,
er.subtitulo_x,
er.pre_titulo_x,
er.pre_subtitulo_x
)
select
nombre_compania_x,
er.id_tipo_cuenta_x,
er.tipo_cuenta_x,
er.id_naturaleza_x,
er.id_cuenta_mayor_x,
er.cod_cuenta_mayor_x,
er.cuenta_mayor_x,
er.id_cuenta_padre_x,
er.cod_cuenta_padre_x,
er.cuenta_padre_x,
er.id_cuenta_contable_x,
er.cod_cuenta_contable_x,
er.cuenta_contable_x,
er.orden_x,
er.id_titulo_x,
er.titulo_x,
er.id_subtitulo_x,
er.subtitulo_x,
er.pre_titulo_x,
er.pre_subtitulo_x,
er.saldo_x,
er.saldo_acum_x,
er.porc_x,
er.porc_acum_x,
finicial as fecha_ini_x,
case
when es_cierre_contable = 'Y' then
(extract(year from ffinal)::varchar || '1231')::date
when es_periodo_ajuste = 'Y' then
(extract(year from ffinal)::varchar || '1231')::date
else ffinal
end as fecha_fin_x
from cte_estado_ultimate er
where (abs(er.saldo_x) > 0 or abs(er.saldo_acum_x) > 0)
order by
er.orden_x,
er.cuenta_padre_x,
er.cuenta_mayor_x,
er.cuenta_contable_x,
er.cod_cuenta_mayor_x asc
);
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
Balanza de Comprobación en ADempiere
Descargar diseño aqui
-- Function: adempiere.rep_balanza_comprobacion(numeric, date, date, character, character)
-- DROP FUNCTION adempiere.rep_balanza_comprobacion(numeric, date, date, character, character);
CREATE OR REPLACE FUNCTION adempiere.rep_balanza_comprobacion(
IN id_compania numeric,
IN finicial date,
IN ffinal date,
IN es_cierre_contable character,
IN es_periodo_ajuste character)
RETURNS TABLE(nombre_compania character varying, c_elementvalue_id numeric, codigocuenta character varying, nat character varying, tipo_cuenta character varying, nombrecuenta character varying, ctamayor_id numeric, ctmayor_value numeric, ctmayor_name character varying, parent_id numeric, parent_value numeric, parent_name character varying, inicialdr numeric, inicialcr numeric, debitos numeric, creditos numeric, finaldr numeric, finalcr numeric, fecha_ini date, fecha_fin date) AS
$BODY$
declare
id_periodo numeric(10,0);
num_periodo numeric(10,0);
-- Fecha inicial del ultimo mes
finicial_ultimo_mes date;
p_nombre_compania varchar;
begin
p_nombre_compania =
(
select
c.name
from ad_client c
where c.ad_client_id = id_compania
);
id_periodo =
(
select p.c_period_id from c_period p
where p.ad_client_id = id_compania
and p.startdate = finicial
and p.enddate = ffinal
and p.periodtype = case when es_cierre_contable = 'Y' or es_periodo_ajuste = 'Y' then 'A' else 'S' end
);
num_periodo =
(
select p.periodno from c_period p
where p.c_period_id = id_periodo
);
-- Otener rangos de fechas para el ultimo mes contabilizado
-- y rango de fechas para el cierre contable
finicial_ultimo_mes =
(
select
p.startdate
from fact_acct f
join c_period p
on f.c_period_id = p.c_period_id
where f.dateacct = ffinal
limit 1
);
drop table if exists tmp_cuentas_contables;
create temporary table tmp_cuentas_contables as
select
u.c_elementvalue_id as c_elementvalue_id_x,
u.value as codigocuenta_x,
cast(u.nat as varchar) as nat_x,
u.accounttype::varchar as tipo_cuenta_x,
cast(replace(u.name, ',', '') as varchar) as nombrecuenta_x,
cast(u.ctamayor_id as numeric(10,0)) as ctamayor_id_x,
cast(u.ctmayor_value as numeric(10,0)) as ctmayor_value_x,
u.ctmayor_name as ctmayor_name_x,
cast(u.parent_id as numeric(10,0)) as parent_id_x,
cast(u.parent_value as numeric(10,0)) as parent_value_x,
u.parent_name as parent_name_x
from usr_v_vcc u
where u.c_elementvalue_id in
(
select distinct sf.id_cuenta from nic_saldos_finales sf
);
drop table if exists tmp_movimientos_contables;
create temporary table tmp_movimientos_contables as
select
sf.id_cuenta,
sf.id_periodo,
sf.tipo_periodo,
sf.num_periodo,
sf.es_cierre_contable,
sf.es_periodo_ajuste,
sum(coalesce(sf.debito_final, 0.00)) as debito_final,
sum(coalesce(sf.credito_final, 0.00)) as credito_final,
sum(coalesce(sf.debito_final, 0.00) - coalesce(sf.credito_final, 0.00)) as dc,
sum(coalesce(sf.credito_final, 0.00) - coalesce(sf.debito_final, 0.00)) as cd
from nic_saldos_finales sf
group by
sf.id_cuenta,
sf.id_periodo,
sf.tipo_periodo,
sf.num_periodo,
sf.es_cierre_contable,
sf.es_periodo_ajuste;
drop table if exists tmp_balanza_comprobacion;
create temporary table tmp_balanza_comprobacion as
select
cc.c_elementvalue_id_x,
cc.codigocuenta_x,
cc.nat_x,
cc.tipo_cuenta_x,
cc.nombrecuenta_x,
cc.ctamayor_id_x,
cc.ctmayor_value_x,
cc.ctmayor_name_x,
cc.parent_id_x,
cc.parent_value_x,
cc.parent_name_x,
case cc.nat_x
when 'D' then
coalesce((select sum(m.dc) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo <
case
when es_periodo_ajuste = 'Y' then
num_periodo - 1
else
num_periodo
end), 0.00)
when 'C' then
0.00
end as inicialdr_x,
case cc.nat_x
when 'D' then
0.00
when 'C' then
coalesce((select sum(m.cd) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo <
case
when es_periodo_ajuste = 'Y' then
num_periodo - 1
else
num_periodo
end), 0.00)
end as inicialcr_x,
case
when es_periodo_ajuste = 'Y' then
coalesce((select sum(m.debito_final) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo =
case
when es_periodo_ajuste = 'Y' then
num_periodo - 1
else
num_periodo
end
and m.es_cierre_contable = es_cierre_contable), 0.00) +
coalesce((select sum(m.debito_final) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo = num_periodo
and m.es_cierre_contable = es_cierre_contable), 0.00)
else
coalesce((select sum(m.debito_final) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo = num_periodo
and m.es_cierre_contable = es_cierre_contable), 0.00)
end as debitos_x,
case
when es_periodo_ajuste = 'Y' then
coalesce((select sum(m.credito_final) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo =
case
when es_periodo_ajuste = 'Y' then
num_periodo - 1
else
num_periodo
end
and m.es_cierre_contable = es_cierre_contable), 0.00) +
coalesce((select sum(m.credito_final) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo = num_periodo
and m.es_cierre_contable = es_cierre_contable), 0.00)
else
coalesce((select sum(m.credito_final) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo = num_periodo
and m.es_cierre_contable = es_cierre_contable), 0.00)
end as creditos_x,
case cc.nat_x
when 'D' then
coalesce((select sum(m.dc) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo <= num_periodo), 0.00)
when 'C' then
0.00
end as finaldr_x,
case cc.nat_x
when 'D' then
0.00
when 'C' then
coalesce((select sum(m.cd) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo <= num_periodo), 0.00)
end as finalcr_x
from tmp_cuentas_contables cc;
return query
(
select
p_nombre_compania as nombre_compania_x,
bc.c_elementvalue_id_x,
bc.codigocuenta_x,
bc.nat_x,
bc.tipo_cuenta_x,
bc.nombrecuenta_x,
bc.ctamayor_id_x,
bc.ctmayor_value_x,
bc.ctmayor_name_x,
bc.parent_id_x,
bc.parent_value_x,
bc.parent_name_x,
bc.inicialdr_x,
bc.inicialcr_x,
bc.debitos_x,
bc.creditos_x,
bc.finaldr_x,
bc.finalcr_x,
finicial as fecha_ini_x,
case
when es_cierre_contable = 'Y' then
(extract(year from ffinal)::varchar || '1231')::date
when es_periodo_ajuste = 'Y' then
(extract(year from ffinal)::varchar || '1231')::date
else ffinal
end as fecha_fin_x
from tmp_balanza_comprobacion bc
where (bc.inicialdr_x <> 0.00
or bc.inicialcr_x <> 0.00
or bc.debitos_x <> 0.00
or bc.creditos_x <> 0.00
or bc.finaldr_x <> 0.00
or bc.finalcr_x <> 0.00)
order by
case bc.tipo_cuenta_x
when 'A' then -- Activo
1
when 'L' then -- Pasivo
2
when 'O' then -- Patrimonio
3
when 'R' then -- Ingreso
4
when 'E' then -- Gasto
5
when 'M' then -- Memo
6
else
7
end,
bc.ctmayor_value_x,
bc.codigocuenta_x
);
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
-- Function: adempiere.rep_balanza_comprobacion(numeric, date, date, character, character)
-- DROP FUNCTION adempiere.rep_balanza_comprobacion(numeric, date, date, character, character);
CREATE OR REPLACE FUNCTION adempiere.rep_balanza_comprobacion(
IN id_compania numeric,
IN finicial date,
IN ffinal date,
IN es_cierre_contable character,
IN es_periodo_ajuste character)
RETURNS TABLE(nombre_compania character varying, c_elementvalue_id numeric, codigocuenta character varying, nat character varying, tipo_cuenta character varying, nombrecuenta character varying, ctamayor_id numeric, ctmayor_value numeric, ctmayor_name character varying, parent_id numeric, parent_value numeric, parent_name character varying, inicialdr numeric, inicialcr numeric, debitos numeric, creditos numeric, finaldr numeric, finalcr numeric, fecha_ini date, fecha_fin date) AS
$BODY$
declare
id_periodo numeric(10,0);
num_periodo numeric(10,0);
-- Fecha inicial del ultimo mes
finicial_ultimo_mes date;
p_nombre_compania varchar;
begin
p_nombre_compania =
(
select
c.name
from ad_client c
where c.ad_client_id = id_compania
);
id_periodo =
(
select p.c_period_id from c_period p
where p.ad_client_id = id_compania
and p.startdate = finicial
and p.enddate = ffinal
and p.periodtype = case when es_cierre_contable = 'Y' or es_periodo_ajuste = 'Y' then 'A' else 'S' end
);
num_periodo =
(
select p.periodno from c_period p
where p.c_period_id = id_periodo
);
-- Otener rangos de fechas para el ultimo mes contabilizado
-- y rango de fechas para el cierre contable
finicial_ultimo_mes =
(
select
p.startdate
from fact_acct f
join c_period p
on f.c_period_id = p.c_period_id
where f.dateacct = ffinal
limit 1
);
drop table if exists tmp_cuentas_contables;
create temporary table tmp_cuentas_contables as
select
u.c_elementvalue_id as c_elementvalue_id_x,
u.value as codigocuenta_x,
cast(u.nat as varchar) as nat_x,
u.accounttype::varchar as tipo_cuenta_x,
cast(replace(u.name, ',', '') as varchar) as nombrecuenta_x,
cast(u.ctamayor_id as numeric(10,0)) as ctamayor_id_x,
cast(u.ctmayor_value as numeric(10,0)) as ctmayor_value_x,
u.ctmayor_name as ctmayor_name_x,
cast(u.parent_id as numeric(10,0)) as parent_id_x,
cast(u.parent_value as numeric(10,0)) as parent_value_x,
u.parent_name as parent_name_x
from usr_v_vcc u
where u.c_elementvalue_id in
(
select distinct sf.id_cuenta from nic_saldos_finales sf
);
drop table if exists tmp_movimientos_contables;
create temporary table tmp_movimientos_contables as
select
sf.id_cuenta,
sf.id_periodo,
sf.tipo_periodo,
sf.num_periodo,
sf.es_cierre_contable,
sf.es_periodo_ajuste,
sum(coalesce(sf.debito_final, 0.00)) as debito_final,
sum(coalesce(sf.credito_final, 0.00)) as credito_final,
sum(coalesce(sf.debito_final, 0.00) - coalesce(sf.credito_final, 0.00)) as dc,
sum(coalesce(sf.credito_final, 0.00) - coalesce(sf.debito_final, 0.00)) as cd
from nic_saldos_finales sf
group by
sf.id_cuenta,
sf.id_periodo,
sf.tipo_periodo,
sf.num_periodo,
sf.es_cierre_contable,
sf.es_periodo_ajuste;
drop table if exists tmp_balanza_comprobacion;
create temporary table tmp_balanza_comprobacion as
select
cc.c_elementvalue_id_x,
cc.codigocuenta_x,
cc.nat_x,
cc.tipo_cuenta_x,
cc.nombrecuenta_x,
cc.ctamayor_id_x,
cc.ctmayor_value_x,
cc.ctmayor_name_x,
cc.parent_id_x,
cc.parent_value_x,
cc.parent_name_x,
case cc.nat_x
when 'D' then
coalesce((select sum(m.dc) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo <
case
when es_periodo_ajuste = 'Y' then
num_periodo - 1
else
num_periodo
end), 0.00)
when 'C' then
0.00
end as inicialdr_x,
case cc.nat_x
when 'D' then
0.00
when 'C' then
coalesce((select sum(m.cd) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo <
case
when es_periodo_ajuste = 'Y' then
num_periodo - 1
else
num_periodo
end), 0.00)
end as inicialcr_x,
case
when es_periodo_ajuste = 'Y' then
coalesce((select sum(m.debito_final) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo =
case
when es_periodo_ajuste = 'Y' then
num_periodo - 1
else
num_periodo
end
and m.es_cierre_contable = es_cierre_contable), 0.00) +
coalesce((select sum(m.debito_final) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo = num_periodo
and m.es_cierre_contable = es_cierre_contable), 0.00)
else
coalesce((select sum(m.debito_final) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo = num_periodo
and m.es_cierre_contable = es_cierre_contable), 0.00)
end as debitos_x,
case
when es_periodo_ajuste = 'Y' then
coalesce((select sum(m.credito_final) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo =
case
when es_periodo_ajuste = 'Y' then
num_periodo - 1
else
num_periodo
end
and m.es_cierre_contable = es_cierre_contable), 0.00) +
coalesce((select sum(m.credito_final) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo = num_periodo
and m.es_cierre_contable = es_cierre_contable), 0.00)
else
coalesce((select sum(m.credito_final) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo = num_periodo
and m.es_cierre_contable = es_cierre_contable), 0.00)
end as creditos_x,
case cc.nat_x
when 'D' then
coalesce((select sum(m.dc) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo <= num_periodo), 0.00)
when 'C' then
0.00
end as finaldr_x,
case cc.nat_x
when 'D' then
0.00
when 'C' then
coalesce((select sum(m.cd) from tmp_movimientos_contables m
where m.id_cuenta = cc.c_elementvalue_id_x
and m.num_periodo <= num_periodo), 0.00)
end as finalcr_x
from tmp_cuentas_contables cc;
return query
(
select
p_nombre_compania as nombre_compania_x,
bc.c_elementvalue_id_x,
bc.codigocuenta_x,
bc.nat_x,
bc.tipo_cuenta_x,
bc.nombrecuenta_x,
bc.ctamayor_id_x,
bc.ctmayor_value_x,
bc.ctmayor_name_x,
bc.parent_id_x,
bc.parent_value_x,
bc.parent_name_x,
bc.inicialdr_x,
bc.inicialcr_x,
bc.debitos_x,
bc.creditos_x,
bc.finaldr_x,
bc.finalcr_x,
finicial as fecha_ini_x,
case
when es_cierre_contable = 'Y' then
(extract(year from ffinal)::varchar || '1231')::date
when es_periodo_ajuste = 'Y' then
(extract(year from ffinal)::varchar || '1231')::date
else ffinal
end as fecha_fin_x
from tmp_balanza_comprobacion bc
where (bc.inicialdr_x <> 0.00
or bc.inicialcr_x <> 0.00
or bc.debitos_x <> 0.00
or bc.creditos_x <> 0.00
or bc.finaldr_x <> 0.00
or bc.finalcr_x <> 0.00)
order by
case bc.tipo_cuenta_x
when 'A' then -- Activo
1
when 'L' then -- Pasivo
2
when 'O' then -- Patrimonio
3
when 'R' then -- Ingreso
4
when 'E' then -- Gasto
5
when 'M' then -- Memo
6
else
7
end,
bc.ctmayor_value_x,
bc.codigocuenta_x
);
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
Generar Cierre Contable en ADempiere
-- Function: adempiere.sp_generar_cierre_contable(numeric, numeric)
-- DROP FUNCTION adempiere.sp_generar_cierre_contable(numeric, numeric);
CREATE OR REPLACE FUNCTION adempiere.sp_generar_cierre_contable(
id_compania numeric,
id_periodo numeric)
RETURNS boolean AS
$BODY$
declare
num_periodo numeric(10,0);
periodo varchar(60);
fecha_inicio date;
fecha_fin date;
ingresos_debito numeric(10,2);
ingresos_credito numeric(10,2);
egresos_debito numeric(10,2);
egresos_credito numeric(10,2);
id_lote numeric(10,0);
id_nota numeric(10,0);
secuencia char;
saldo_total numeric(10,2);
begin
select
periodno,
p.name,
p.startdate,
p.enddate
into num_periodo, periodo, fecha_inicio, fecha_fin
from c_period p
where p.ad_client_id = id_compania
and p.c_period_id = id_periodo;
drop table if exists tmp_saldos_previos;
create table tmp_saldos_previos as
select
sf.id_cuenta,
ev.value as cod_cuenta,
ev.name as cuenta_contable,
sf.tipo_cuenta,
case sf.naturaleza
when 'D' then
sum(coalesce(sf.debito_final, 0.00)) - sum(coalesce(sf.credito_final, 0.00))
when 'C' then
0.00
end as debito,
case sf.naturaleza
when 'D' then
0.00
when 'C' then
sum(coalesce(sf.credito_final, 0.00)) - sum(coalesce(sf.debito_final, 0.00))
end as credito
from nic_saldos_finales sf
join c_elementvalue ev
on sf.id_cuenta = ev.c_elementvalue_id
where sf.tipo_cuenta in ('R', 'E') -- Ingresos y Gastos (Estado de Resultado)
and sf.id_periodo = id_periodo
group by
sf.id_cuenta,
ev.value,
ev.name,
sf.tipo_cuenta,
sf.naturaleza
union all
select
sf.id_cuenta,
ev.value as cod_cuenta,
ev.name as cuenta_contable,
sf.tipo_cuenta,
case sf.naturaleza
when 'D' then
sum(coalesce(sf.debito_final, 0.00)) - sum(coalesce(sf.credito_final, 0.00))
when 'C' then
0.00
end as debito,
case sf.naturaleza
when 'D' then
0.00
when 'C' then
sum(coalesce(sf.credito_final, 0.00)) - sum(coalesce(sf.debito_final, 0.00))
end as credito
from nic_saldos_finales sf
join c_elementvalue ev
on sf.id_cuenta = ev.c_elementvalue_id
where sf.tipo_cuenta in ('R', 'E') -- Ingresos y Gastos (Estado de Resultado)
and sf.num_periodo <= num_periodo
group by
sf.id_cuenta,
ev.value,
ev.name,
sf.tipo_cuenta,
sf.naturaleza;
saldo_total = (select sum(sp.credito - debito) from tmp_saldos_previos sp);
drop table if exists tmp_saldos_cuenta;
create temporary table tmp_saldos_cuenta as
select
sp.id_cuenta,
sp.cod_cuenta,
sp.cuenta_contable,
sp.tipo_cuenta,
coalesce(sp.debito, 0.00) as debito,
coalesce(sp.credito, 0.00) as credito
from tmp_saldos_previos sp
where (sp.debito <> 0 or sp.credito <> 0)
union all
select
ev.c_elementvalue_id as id_cuenta,
ev.value as cod_cuenta,
ev.name as cuenta_contable,
ev.accounttype as tipo_cuenta,
coalesce(case
when saldo_total > 0 then -- Ganancia
saldo_total
else -- Perdida
0.00
end, 0.00) as debito,
coalesce(case
when saldo_total > 0 then -- Ganancia
0.00
else -- Perdida
saldo_total
end, 0.00) as credito
from c_elementvalue ev
where ev.c_elementvalue_id = 1000881 -- 90000002 Pérdida y Ganancia (Credito)
and saldo_total <> 0;
insert into gl_journalbatch
(
gl_journalbatch_id,
ad_client_id,
ad_org_id,
isactive,
created,
createdby,
updated,
updatedby,
documentno,
description,
postingtype,
gl_category_id,
datedoc,
dateacct,
c_period_id,
c_currency_id,
totaldr,
totalcr,
controlamt,
processing,
processed,
copyfrom,
c_doctype_id,
docstatus,
docaction,
isapproved
)
select
((select max(gb.gl_journalbatch_id) from gl_journalbatch gb) + 1) as gl_journalbatch_id,
id_compania as ad_client_id,
1000001 as ad_org_id,
cast('Y' as char) as isactive,
now() as created,
100 as createdby,
now() as updated,
100 as updatedby,
periodo::varchar(30) as documentno,
cast('Cierre contable al ' || fecha_fin as varchar(255)) as description,
cast('A' as char) as postingtype,
cast(1000000 as numeric(10,0)) as gl_category_id,
fecha_fin as datedoc,
fecha_fin as dateacct,
id_periodo as c_period_id,
cast(209 as numeric(10,0)) as c_currency_id,
coalesce(sum(sc.credito), 0.00)::numeric(10,2) as totaldr,
coalesce(sum(sc.debito), 0.00)::numeric(10,2) as totalcr,
0::numeric as controlamt,
'N'::char as processing,
'N'::char as processed,
'N'::char as copyfrom,
1000000::numeric(10,0) as c_doctype_id,
'DR'::char(2) as docstatus,
'CO'::char(2) as docaction,
'N'::char(1) as isapproved
from tmp_saldos_cuenta sc;
id_lote =
(
select
max(gb.gl_journalbatch_id)
from gl_journalbatch gb
);
insert into gl_journal
(
gl_journal_id,
ad_client_id,
ad_org_id,
isactive,
created,
createdby,
updated,
updatedby,
c_acctschema_id,
c_doctype_id,
documentno,
docstatus,
docaction,
isapproved,
isprinted,
description,
postingtype,
gl_category_id,
datedoc,
dateacct,
c_period_id,
c_currency_id,
currencyrate,
gl_journalbatch_id,
totaldr,
totalcr,
controlamt,
processing,
processed,
posted,
c_conversiontype_id
)
select
((select max(g.gl_journal_id) from gl_journal g) + 1) as gl_journal_id,
id_compania as ad_client_id,
1000001 as ad_org_id,
cast('Y' as char) as isactive,
now() as created,
100 as createdby,
now() as updated,
100 as updatedby,
1000001::numeric(10,0) as c_acctschema_id,
1000000::numeric(10,0) as c_doctype_id,
periodo::varchar(30) as documentno,
'DR'::char(2) as docstatus,
'CO'::char(2) as docaction,
'Y'::char as isapproved,
'N'::char as isprinted,
cast('Cierre contable al ' || fecha_fin as varchar(255)) as description,
'A'::char as postingtype,
1000000::numeric(10,0) as gl_category_id,
fecha_fin as datedoc,
fecha_fin as dateacct,
id_periodo as c_period_id,
cast(209 as numeric(10,0)) as c_currency_id,
1::numeric as currencyrate,
id_lote as gl_journalbatch_id,
coalesce(sum(sc.credito), 0.00)::numeric(10,2) as totaldr,
coalesce(sum(sc.debito), 0.00)::numeric(10,2) as totalcr,
0::numeric as controlamt,
'N'::char as processing,
'N'::char as processed,
'N'::char as posted,
114::numeric(10,0) as c_conversiontype_id
from tmp_saldos_cuenta sc;
id_nota =
(
select
max(g.gl_journal_id)
from gl_journal g
where g.gl_journalbatch_id = id_lote
);
insert into c_validcombination
(
c_validcombination_id,
ad_client_id,
ad_org_id,
isactive,
created,
createdby,
updated,
updatedby,
combination,
description,
isfullyqualified,
c_acctschema_id,
account_id
)
with cte_combinaciones_contables as
(
select
id_compania as ad_client_id,
1000001 as ad_org_id,
cast('Y' as char) as isactive,
now() as created,
100 as createdby,
now() as updated,
100 as updatedby,
'Enterprise-' || sc.cod_cuenta || '-_-_'::varchar(60) as combination,
'Enterprise-' || sc.cuenta_contable || '-_-_'::varchar(255) as description,
'Y'::char as isfullyqualified,
1000001::numeric(10,0) as c_acctschema_id,
sc.id_cuenta as account_id,
sc.tipo_cuenta,
sc.cod_cuenta
from tmp_saldos_cuenta sc
where not exists
(
select 1 from c_validcombination vc
where vc.ad_client_id = id_compania
and vc.combination = 'Enterprise-' || sc.cod_cuenta || '-_-_'
and vc.description = 'Enterprise-' || sc.cuenta_contable || '-_-_'
)
)
select
(select max(vc.c_validcombination_id) from c_validcombination vc) +
row_number() over(order by cc.tipo_cuenta, cc.cod_cuenta) as c_validcombination_id,
cc.ad_client_id,
cc.ad_org_id,
cc.isactive,
cc.created,
cc.createdby,
cc.updated,
cc.updatedby,
cc.combination,
cc.description,
cc.isfullyqualified,
cc.c_acctschema_id,
cc.account_id
from cte_combinaciones_contables cc;
insert into gl_journalline
(
gl_journalline_id,
ad_client_id,
ad_org_id,
isactive,
created,
createdby,
updated,
updatedby,
gl_journal_id,
line,
isgenerated,
description,
amtsourcedr,
amtsourcecr,
c_currency_id,
currencyrate,
dateacct,
amtacctdr,
amtacctcr,
c_uom_id,
qty,
c_validcombination_id,
c_conversiontype_id,
processed,
a_createasset,
a_processed
)
select
((select max(g.gl_journalline_id) from gl_journalline g) +
row_number() over(order by sc.tipo_cuenta, sc.cod_cuenta)) as gl_journalline_id,
id_compania as ad_client_id,
1000001 as ad_org_id,
cast('Y' as char) as isactive,
now() as created,
100 as createdby,
now() as updated,
100 as updatedby,
id_nota as gl_journal_id,
(row_number() over(order by sc.tipo_cuenta, sc.cod_cuenta) * 10)::numeric(10,0) as line,
'N'::char as isgenerated,
sc.cuenta_contable as description,
sc.credito::numeric(10,2) as amtsourcedr,
sc.debito::numeric(10,2) as amtsourcecr,
209::numeric(10,0) as c_currency_id,
1::numeric as currencyrate,
fecha_fin as dateacct,
sc.credito::numeric(10,2) as amtacctdr,
sc.debito::numeric(10,2) as amtacctcr,
1000001::numeric(10,0) as c_uom_id,
0::numeric as qty,
(select max(vc.c_validcombination_id) from c_validcombination vc
where vc.ad_client_id = 1000001
and vc.combination = 'Enterprise-' || sc.cod_cuenta || '-_-_'
and vc.description = 'Enterprise-' || sc.cuenta_contable || '-_-_') as c_validcombination_id,
114::numeric(10,0) as c_conversiontype_id,
'N'::char as processed,
'N'::char as a_createasset,
'N'::char as a_processed
from tmp_saldos_cuenta sc;
secuencia =
(
select update_sequences()
);
return true;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
-- DROP FUNCTION adempiere.sp_generar_cierre_contable(numeric, numeric);
CREATE OR REPLACE FUNCTION adempiere.sp_generar_cierre_contable(
id_compania numeric,
id_periodo numeric)
RETURNS boolean AS
$BODY$
declare
num_periodo numeric(10,0);
periodo varchar(60);
fecha_inicio date;
fecha_fin date;
ingresos_debito numeric(10,2);
ingresos_credito numeric(10,2);
egresos_debito numeric(10,2);
egresos_credito numeric(10,2);
id_lote numeric(10,0);
id_nota numeric(10,0);
secuencia char;
saldo_total numeric(10,2);
begin
select
periodno,
p.name,
p.startdate,
p.enddate
into num_periodo, periodo, fecha_inicio, fecha_fin
from c_period p
where p.ad_client_id = id_compania
and p.c_period_id = id_periodo;
drop table if exists tmp_saldos_previos;
create table tmp_saldos_previos as
select
sf.id_cuenta,
ev.value as cod_cuenta,
ev.name as cuenta_contable,
sf.tipo_cuenta,
case sf.naturaleza
when 'D' then
sum(coalesce(sf.debito_final, 0.00)) - sum(coalesce(sf.credito_final, 0.00))
when 'C' then
0.00
end as debito,
case sf.naturaleza
when 'D' then
0.00
when 'C' then
sum(coalesce(sf.credito_final, 0.00)) - sum(coalesce(sf.debito_final, 0.00))
end as credito
from nic_saldos_finales sf
join c_elementvalue ev
on sf.id_cuenta = ev.c_elementvalue_id
where sf.tipo_cuenta in ('R', 'E') -- Ingresos y Gastos (Estado de Resultado)
and sf.id_periodo = id_periodo
group by
sf.id_cuenta,
ev.value,
ev.name,
sf.tipo_cuenta,
sf.naturaleza
union all
select
sf.id_cuenta,
ev.value as cod_cuenta,
ev.name as cuenta_contable,
sf.tipo_cuenta,
case sf.naturaleza
when 'D' then
sum(coalesce(sf.debito_final, 0.00)) - sum(coalesce(sf.credito_final, 0.00))
when 'C' then
0.00
end as debito,
case sf.naturaleza
when 'D' then
0.00
when 'C' then
sum(coalesce(sf.credito_final, 0.00)) - sum(coalesce(sf.debito_final, 0.00))
end as credito
from nic_saldos_finales sf
join c_elementvalue ev
on sf.id_cuenta = ev.c_elementvalue_id
where sf.tipo_cuenta in ('R', 'E') -- Ingresos y Gastos (Estado de Resultado)
and sf.num_periodo <= num_periodo
group by
sf.id_cuenta,
ev.value,
ev.name,
sf.tipo_cuenta,
sf.naturaleza;
saldo_total = (select sum(sp.credito - debito) from tmp_saldos_previos sp);
drop table if exists tmp_saldos_cuenta;
create temporary table tmp_saldos_cuenta as
select
sp.id_cuenta,
sp.cod_cuenta,
sp.cuenta_contable,
sp.tipo_cuenta,
coalesce(sp.debito, 0.00) as debito,
coalesce(sp.credito, 0.00) as credito
from tmp_saldos_previos sp
where (sp.debito <> 0 or sp.credito <> 0)
union all
select
ev.c_elementvalue_id as id_cuenta,
ev.value as cod_cuenta,
ev.name as cuenta_contable,
ev.accounttype as tipo_cuenta,
coalesce(case
when saldo_total > 0 then -- Ganancia
saldo_total
else -- Perdida
0.00
end, 0.00) as debito,
coalesce(case
when saldo_total > 0 then -- Ganancia
0.00
else -- Perdida
saldo_total
end, 0.00) as credito
from c_elementvalue ev
where ev.c_elementvalue_id = 1000881 -- 90000002 Pérdida y Ganancia (Credito)
and saldo_total <> 0;
insert into gl_journalbatch
(
gl_journalbatch_id,
ad_client_id,
ad_org_id,
isactive,
created,
createdby,
updated,
updatedby,
documentno,
description,
postingtype,
gl_category_id,
datedoc,
dateacct,
c_period_id,
c_currency_id,
totaldr,
totalcr,
controlamt,
processing,
processed,
copyfrom,
c_doctype_id,
docstatus,
docaction,
isapproved
)
select
((select max(gb.gl_journalbatch_id) from gl_journalbatch gb) + 1) as gl_journalbatch_id,
id_compania as ad_client_id,
1000001 as ad_org_id,
cast('Y' as char) as isactive,
now() as created,
100 as createdby,
now() as updated,
100 as updatedby,
periodo::varchar(30) as documentno,
cast('Cierre contable al ' || fecha_fin as varchar(255)) as description,
cast('A' as char) as postingtype,
cast(1000000 as numeric(10,0)) as gl_category_id,
fecha_fin as datedoc,
fecha_fin as dateacct,
id_periodo as c_period_id,
cast(209 as numeric(10,0)) as c_currency_id,
coalesce(sum(sc.credito), 0.00)::numeric(10,2) as totaldr,
coalesce(sum(sc.debito), 0.00)::numeric(10,2) as totalcr,
0::numeric as controlamt,
'N'::char as processing,
'N'::char as processed,
'N'::char as copyfrom,
1000000::numeric(10,0) as c_doctype_id,
'DR'::char(2) as docstatus,
'CO'::char(2) as docaction,
'N'::char(1) as isapproved
from tmp_saldos_cuenta sc;
id_lote =
(
select
max(gb.gl_journalbatch_id)
from gl_journalbatch gb
);
insert into gl_journal
(
gl_journal_id,
ad_client_id,
ad_org_id,
isactive,
created,
createdby,
updated,
updatedby,
c_acctschema_id,
c_doctype_id,
documentno,
docstatus,
docaction,
isapproved,
isprinted,
description,
postingtype,
gl_category_id,
datedoc,
dateacct,
c_period_id,
c_currency_id,
currencyrate,
gl_journalbatch_id,
totaldr,
totalcr,
controlamt,
processing,
processed,
posted,
c_conversiontype_id
)
select
((select max(g.gl_journal_id) from gl_journal g) + 1) as gl_journal_id,
id_compania as ad_client_id,
1000001 as ad_org_id,
cast('Y' as char) as isactive,
now() as created,
100 as createdby,
now() as updated,
100 as updatedby,
1000001::numeric(10,0) as c_acctschema_id,
1000000::numeric(10,0) as c_doctype_id,
periodo::varchar(30) as documentno,
'DR'::char(2) as docstatus,
'CO'::char(2) as docaction,
'Y'::char as isapproved,
'N'::char as isprinted,
cast('Cierre contable al ' || fecha_fin as varchar(255)) as description,
'A'::char as postingtype,
1000000::numeric(10,0) as gl_category_id,
fecha_fin as datedoc,
fecha_fin as dateacct,
id_periodo as c_period_id,
cast(209 as numeric(10,0)) as c_currency_id,
1::numeric as currencyrate,
id_lote as gl_journalbatch_id,
coalesce(sum(sc.credito), 0.00)::numeric(10,2) as totaldr,
coalesce(sum(sc.debito), 0.00)::numeric(10,2) as totalcr,
0::numeric as controlamt,
'N'::char as processing,
'N'::char as processed,
'N'::char as posted,
114::numeric(10,0) as c_conversiontype_id
from tmp_saldos_cuenta sc;
id_nota =
(
select
max(g.gl_journal_id)
from gl_journal g
where g.gl_journalbatch_id = id_lote
);
insert into c_validcombination
(
c_validcombination_id,
ad_client_id,
ad_org_id,
isactive,
created,
createdby,
updated,
updatedby,
combination,
description,
isfullyqualified,
c_acctschema_id,
account_id
)
with cte_combinaciones_contables as
(
select
id_compania as ad_client_id,
1000001 as ad_org_id,
cast('Y' as char) as isactive,
now() as created,
100 as createdby,
now() as updated,
100 as updatedby,
'Enterprise-' || sc.cod_cuenta || '-_-_'::varchar(60) as combination,
'Enterprise-' || sc.cuenta_contable || '-_-_'::varchar(255) as description,
'Y'::char as isfullyqualified,
1000001::numeric(10,0) as c_acctschema_id,
sc.id_cuenta as account_id,
sc.tipo_cuenta,
sc.cod_cuenta
from tmp_saldos_cuenta sc
where not exists
(
select 1 from c_validcombination vc
where vc.ad_client_id = id_compania
and vc.combination = 'Enterprise-' || sc.cod_cuenta || '-_-_'
and vc.description = 'Enterprise-' || sc.cuenta_contable || '-_-_'
)
)
select
(select max(vc.c_validcombination_id) from c_validcombination vc) +
row_number() over(order by cc.tipo_cuenta, cc.cod_cuenta) as c_validcombination_id,
cc.ad_client_id,
cc.ad_org_id,
cc.isactive,
cc.created,
cc.createdby,
cc.updated,
cc.updatedby,
cc.combination,
cc.description,
cc.isfullyqualified,
cc.c_acctschema_id,
cc.account_id
from cte_combinaciones_contables cc;
insert into gl_journalline
(
gl_journalline_id,
ad_client_id,
ad_org_id,
isactive,
created,
createdby,
updated,
updatedby,
gl_journal_id,
line,
isgenerated,
description,
amtsourcedr,
amtsourcecr,
c_currency_id,
currencyrate,
dateacct,
amtacctdr,
amtacctcr,
c_uom_id,
qty,
c_validcombination_id,
c_conversiontype_id,
processed,
a_createasset,
a_processed
)
select
((select max(g.gl_journalline_id) from gl_journalline g) +
row_number() over(order by sc.tipo_cuenta, sc.cod_cuenta)) as gl_journalline_id,
id_compania as ad_client_id,
1000001 as ad_org_id,
cast('Y' as char) as isactive,
now() as created,
100 as createdby,
now() as updated,
100 as updatedby,
id_nota as gl_journal_id,
(row_number() over(order by sc.tipo_cuenta, sc.cod_cuenta) * 10)::numeric(10,0) as line,
'N'::char as isgenerated,
sc.cuenta_contable as description,
sc.credito::numeric(10,2) as amtsourcedr,
sc.debito::numeric(10,2) as amtsourcecr,
209::numeric(10,0) as c_currency_id,
1::numeric as currencyrate,
fecha_fin as dateacct,
sc.credito::numeric(10,2) as amtacctdr,
sc.debito::numeric(10,2) as amtacctcr,
1000001::numeric(10,0) as c_uom_id,
0::numeric as qty,
(select max(vc.c_validcombination_id) from c_validcombination vc
where vc.ad_client_id = 1000001
and vc.combination = 'Enterprise-' || sc.cod_cuenta || '-_-_'
and vc.description = 'Enterprise-' || sc.cuenta_contable || '-_-_') as c_validcombination_id,
114::numeric(10,0) as c_conversiontype_id,
'N'::char as processed,
'N'::char as a_createasset,
'N'::char as a_processed
from tmp_saldos_cuenta sc;
secuencia =
(
select update_sequences()
);
return true;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Suscribirse a:
Entradas (Atom)