martes, 23 de julio de 2019
Account Catalog Query
-- select * from rep_plan_cuentas() pc;
-- drop function rep_plan_cuentas();
create or replace function rep_plan_cuentas()
returns table
(
nolinea_x bigint,
node_id_x numeric(10,0),
nivel_x int,
tipo_cuenta_x char,
id_cuenta_padre_x numeric(10,0),
cod_cuenta_padre_x varchar,
cuenta_padre_x varchar,
codigo_contable_x varchar,
cuenta_contable_x varchar,
nat_x char,
issummary char
)
as $$
begin
return query
(
WITH RECURSIVE cte (nivel, node_id, seqno, parent_id, sort) AS
(
SELECT 0 AS nivel,
C .node_id,
C .seqno,
C .parent_id,
(
lpad (C .seqno :: CHARACTER VARYING, 2, '0') || C .node_id :: CHARACTER VARYING
) AS sort
FROM adempiere.ad_treenode AS C
WHERE C .ad_client_id = 1000000
AND C .ad_tree_id = 1000000
and C.parent_id = 0
UNION ALL
SELECT (cte.nivel + 1) AS nivel,
C .node_id,
C .seqno,
C .parent_id,
(
cte.sort || '/' || lpad (C .seqno :: CHARACTER VARYING, 2, '0') || C .node_id :: CHARACTER VARYING
) AS sort
FROM adempiere.ad_treenode AS C
JOIN CTE
ON C .parent_id = cte.node_id
WHERE C .ad_client_id = 1000000
AND C .ad_tree_id = 1000000
) SELECT
ROW_NUMBER() OVER (ORDER BY sort) AS nolinea ,
C.node_id,
C.nivel,
ce.accounttype as tipo_cuenta,
ev.c_elementvalue_id as id_cuenta_padre,
ev.value as cod_cuenta_padre,
ev.name as cuenta_padre,
ce.value as codigo_cuenta,
ce.name as cuenta_contable,
ce.accountsign as nat,
ce.issummary
from cte AS C
JOIN adempiere.c_elementvalue AS ce ON ce.c_elementvalue_id = C.node_id
join adempiere.c_elementvalue as ev on ev.c_elementvalue_id = C.parent_id
where ce.ad_client_id = 1000000
AND ce.isactive = 'Y'
ORDER BY
sort asc
);
end;
$$ language plpgsql;
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario