Advertisement
marcopolorez

resolve_lalur

Mar 30th, 2016
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. create or replace function resolve_lalur( str varchar, pdtinicio date, pdtfim date, pfilcod  integer[] )
  2. returns numeric(9,2) as
  3. $$
  4. declare
  5.  arrform varchar [];
  6.  valor varchar;
  7.  vformula varchar;
  8. begin
  9.  
  10.  -- retira espaços da formula e separa valores no array
  11.  arrform := ( select regexp_split_to_array( regexp_replace(str, ' ', '', 'g'), '[^0-9A-Za-z\.]+' ) );
  12.  
  13.  for i in 1 .. array_length(arrform,1) loop
  14.  
  15.   if arrform[i] ~ '^M' then
  16.  
  17.    -- Pegar valor de movimento da conta
  18.    valor := coalesce((select dre_saldo(regexp_replace(arrForm[i], '^M', '')::integer, pdtinicio, pdtfim, pfilcod)), 0)::varchar;
  19.    str   :=  ( select regexp_replace(str, arrform[i] || '([^0-9]|$)', valor || '\1', 'g') );
  20.    raise notice '%', str;
  21.  
  22.   elsif arrform[i] ~ '^T' then
  23.  
  24.    vformula := (select formula from con_estrutura_lalur where id_estrutura_lalur = regexp_replace(arrForm[i], '^T', '')::integer);
  25.  
  26.    if vformula is not null then
  27.     valor := ( select resolve_lalur(vformula, $2, $3, $4 )::varchar );
  28.    else
  29.     valor := '0';
  30.    end if;
  31.  
  32.    str   :=  ( select regexp_replace(str, arrform[i] || '([^0-9]|$)', valor || '\1', 'g') );
  33.    raise notice '%', str;
  34.    
  35.   end if;
  36.  
  37.  end loop;
  38.  
  39.  return eval_numeric( str );
  40.  
  41. end
  42. $$
  43. language 'plpgsql';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement