create or replace function fn_fco_tarifa_FSP(pch_compania   in char,
                                             pch_trabajador in char,
                                             psi_anio       in number,
                                             pti_mes        in number,
                                             pdc_valor      number,
                                             pbi_pensionado binary_integer)
  return number is
  vdc_tarifa   number(6, 5);
  vdc_sal_min  number(16, 6);
  vdc_salarios number(5, 2); -- Cantidad de Salarios Mínimos
begin
  SELECT real
    into vdc_sal_min
    FROM parametros_rh
   WHERE compania = pch_compania
     AND parametro_rh = 'Sal_Minimo'
     AND secuencia_parametro = 1
     AND to_char(fecha, 'YYYYMM') <=
         Rtrim(psi_anio) || rtrim(ltrim(to_char(pti_mes, '00')));

  IF vdc_sal_min IS NULL then
    SELECT salario_minimo
      into vdc_sal_min
      FROM hist_sal_min_zona h,
           (select max(fecha) fecha from hist_sal_min_zona where zona = 'A') a
     WHERE h.zona = 'A'
       and h.fecha = a.fecha
       AND to_char(h.fecha, 'YYYYMM') <=
           Rtrim(psi_anio) || to_char(RTrim(pti_mes), '00');
  end if;

  IF nvl(vdc_sal_min, 0) = 0 then
    vdc_salarios := 0;
  ELSE
    vdc_salarios := pdc_valor / vdc_sal_min;
  end if;

  begin
    SELECT porcentaje
      into vdc_tarifa
      FROM impuestos
     WHERE impuesto = 5
       AND vdc_salarios BETWEEN limite_inferior AND limite_superior;
  exception
    when no_data_found then
      vdc_tarifa := 0;
  end;

  RETURN nvl(vdc_tarifa, 0);

end fn_fco_tarifa_FSP;
/
