martes, 18 de noviembre de 2014
Fast posting in ADempiere
-- Aqui esta la función que se agregara como un nuevo proceso, ver aqui
-- Function: sp_contabilizar_comprobante(numeric, numeric)
-- DROP FUNCTION sp_contabilizar_comprobante(numeric, numeric);
CREATE OR REPLACE FUNCTION sp_contabilizar_comprobante(id_compania numeric, id_comprobante numeric)
RETURNS boolean AS
$BODY$
declare
id_periodo numeric(10,0);
esta_descuadrado boolean;
secuencia char;
seq_fact numeric;
begin
id_periodo =
(
select
gb.c_period_id
from gl_journalbatch gb
where gb.ad_client_id = id_compania
and gb.gl_journalbatch_id = id_comprobante
);
esta_descuadrado =
coalesce((
select true from rep_notas_descuadradas(id_periodo, id_comprobante)
), false)::boolean;
if(esta_descuadrado = false)then
RAISE NOTICE '1) Completando Lote ...';
-- Completar Lote
update gl_journalbatch
set processed = 'Y',
docstatus = 'CO',
docaction = 'CL',
isapproved = 'Y'
where gl_journalbatch_id = id_comprobante;
RAISE NOTICE '2) Completando & Contabilizando Nota ...';
-- Completar & Contabilizar Nota
update gl_journal
set docstatus = 'CO',
docaction = 'CL',
processed = 'Y',
posted = 'Y',
processedon = extract(epoch from datedoc::date) * 1000
where gl_journalbatch_id = id_comprobante;
RAISE NOTICE '3) Completando Linea ...';
-- Completar Linea
update gl_journalline
set processed = 'Y'
from gl_journalline gl
join gl_journal g
on gl.gl_journal_id = g.gl_journal_id
where gl_journalline.gl_journalline_id = gl.gl_journalline_id
and g.gl_journalbatch_id = id_comprobante;
RAISE NOTICE '4) Contabilizando Linea ...';
insert into fact_acct
(
fact_acct_id,
ad_client_id,
ad_org_id,
isactive,
created,
createdby,
updated,
updatedby,
c_acctschema_id,
account_id,
datetrx,
dateacct,
c_period_id,
ad_table_id,
record_id,
line_id,
gl_category_id,
postingtype,
c_currency_id,
amtsourcedr,
amtsourcecr,
amtacctdr,
amtacctcr,
c_uom_id,
qty,
description
)
select
coalesce((select max(f.fact_acct_id) from fact_acct f), 999999) +
row_number() over(order by g.gl_journal_id, gl.gl_journalline_id) as fact_acct_id,
gl.ad_client_id,
gl.ad_org_id,
gl.isactive,
gl.created,
gl.createdby,
gl.updated,
gl.updatedby,
1000001::numeric(10,0) as c_acctschema_id,
vc.account_id,
g.datedoc,
g.dateacct,
g.c_period_id,
224::numeric(10,0) as ad_table_id,
g.gl_journal_id as record_id,
gl.gl_journalline_id as line_id,
1000000::numeric(10,0) as gl_category_id,
'A'::char as postingtype,
209::numeric(10,0) as c_currency_id,
gl.amtacctdr as amtsourcedr,
gl.amtacctcr as amtsourcecr,
gl.amtacctdr,
gl.amtacctcr,
100::numeric(10,0) as c_uom_id,
0::numeric as qty,
(g.documentno || ' #' || gl.line || ' (' || g.description || ')')::varchar(255) as description
from gl_journalline gl
join gl_journal g
on gl.gl_journal_id = g.gl_journal_id
join c_validcombination vc
on gl.c_validcombination_id = vc.c_validcombination_id
where g.gl_journalbatch_id = id_comprobante
and not exists
(
select 1 from fact_acct f
where f.record_id = g.gl_journal_id
and f.line_id = gl.gl_journalline_id
);
RAISE NOTICE '5) Actualizando Secuencia ...';
/*secuencia =
(
select update_sequences()
);*/
/************** ACTUALIZAR SECUENCIA **********************/
seq_fact =
coalesce((
select max(f.fact_acct_id) from fact_acct f
), 1000000) + 1;
update AD_Sequence
set CurrentNext = seq_fact
where name = 'Fact_Acct'
and IsTableID = 'Y';
end if;
return esta_descuadrado;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario