create or replace function fn_fco_nov_var(p_compania   in char,
                                               p_trabajador in char,
                                               p_agrupacion in char,
                                               p_anio       in number,
                                               p_mes        in number,
                                               p_indica     in varchar2)
  return number is
  Result number;
begin
  if p_indica = 'importe' then
  begin
    select decode(p_mes,
    1, a.acum_imp_mes_01, 2, a.acum_imp_mes_02, 3, a.acum_imp_mes_03,
    4, a.acum_imp_mes_04, 5, a.acum_imp_mes_05, 6, a.acum_imp_mes_06,
    7, a.acum_imp_mes_07, 8, a.acum_imp_mes_08, 9, a.acum_imp_mes_09,
    10, a.acum_imp_mes_10, 11, a.acum_imp_mes_11, 12, a.acum_imp_mes_12)
    into result
    FROM acumulados_mensuales a
    where a.compania = p_compania
    and a.trabajador = p_trabajador
    and a.agr_conceptos = p_agrupacion
    and a.anio = p_anio
    and decode(p_mes,
    1, a.acum_imp_mes_01, 2, a.acum_imp_mes_02, 3, a.acum_imp_mes_03,
    4, a.acum_imp_mes_04, 5, a.acum_imp_mes_05, 6, a.acum_imp_mes_06,
    7, a.acum_imp_mes_07, 8, a.acum_imp_mes_08, 9, a.acum_imp_mes_09,
    10, a.acum_imp_mes_10, 11, a.acum_imp_mes_11, 12, a.acum_imp_mes_12 ) <> 0;
   exception when others then result:= 0;
   end;
   end if;

  if p_indica = 'tiempo' then
  begin
    select decode(p_mes,
    1, a.acum_tie_mes_01, 2, a.acum_tie_mes_02, 3, a.acum_tie_mes_03,
    4, a.acum_tie_mes_04, 5, a.acum_tie_mes_05, 6, a.acum_tie_mes_06,
    7, a.acum_tie_mes_07, 8, a.acum_tie_mes_08, 9, a.acum_tie_mes_09,
    10, a.acum_tie_mes_10, 11, a.acum_tie_mes_11, 12, a.acum_tie_mes_12)
    into result
    FROM acumulados_mensuales a
    where a.compania = p_compania
    and a.trabajador = p_trabajador
    and a.agr_conceptos = p_agrupacion
    and a.anio = p_anio
    and decode(p_mes,
    1, a.acum_tie_mes_01, 2, a.acum_tie_mes_02, 3, a.acum_tie_mes_03,
    4, a.acum_tie_mes_04, 5, a.acum_tie_mes_05, 6, a.acum_tie_mes_06,
    7, a.acum_tie_mes_07, 8, a.acum_tie_mes_08, 9, a.acum_tie_mes_09,
    10, a.acum_tie_mes_10, 11, a.acum_tie_mes_11, 12, a.acum_tie_mes_12) <> 0;
   exception when others then result:= 0;
   end;
   end if;

  return(Result);
end fn_fco_nov_var;
/
