Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- create or replace function resolve_lalur( str varchar, pdtinicio date, pdtfim date, pfilcod integer[] )
- returns numeric(9,2) as
- $$
- declare
- arrform varchar [];
- valor varchar;
- vformula varchar;
- begin
- -- retira espaços da formula e separa valores no array
- arrform := ( select regexp_split_to_array( regexp_replace(str, ' ', '', 'g'), '[^0-9A-Za-z\.]+' ) );
- for i in 1 .. array_length(arrform,1) loop
- if arrform[i] ~ '^M' then
- -- Pegar valor de movimento da conta
- valor := coalesce((select dre_saldo(regexp_replace(arrForm[i], '^M', '')::integer, pdtinicio, pdtfim, pfilcod)), 0)::varchar;
- str := ( select regexp_replace(str, arrform[i] || '([^0-9]|$)', valor || '\1', 'g') );
- raise notice '%', str;
- elsif arrform[i] ~ '^T' then
- vformula := (select formula from con_estrutura_lalur where id_estrutura_lalur = regexp_replace(arrForm[i], '^T', '')::integer);
- if vformula is not null then
- valor := ( select resolve_lalur(vformula, $2, $3, $4 )::varchar );
- else
- valor := '0';
- end if;
- str := ( select regexp_replace(str, arrform[i] || '([^0-9]|$)', valor || '\1', 'g') );
- raise notice '%', str;
- end if;
- end loop;
- return eval_numeric( str );
- end
- $$
- language 'plpgsql';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement