IF OBJECT_ID('dbo.fn_fco_nro_trab_eps') IS NOT NULL
    DROP FUNCTION dbo.fn_fco_nro_trab_eps
GO
CREATE FUNCTION dbo.fn_fco_nro_trab_eps
(  
    @pch_compania char(4),  
    @psi_anio smallint,  
    @pti_mes tinyint,  
    @pch_sucursal char(10)  
)  
RETURNS decimal(5,0)  
AS  
BEGIN  
DECLARE   
    @vdc_total decimal(5,0),  
    @vdt_fec_ini smalldatetime,  
    @vdt_fec_fin smalldatetime,  
    @vti_por_suc tinyint  
      
    IF IsNull(RTrim(LTrim(@pch_sucursal)), '') = ''   
        SET @vti_por_suc = 0  
    ELSE  
        SET @vti_por_suc = 1  
  
    SET @vdt_fec_ini = Convert(smalldatetime, RTrim(@psi_anio) + Right('00' + RTrim(@pti_mes), 2) + '01')
    SET @vdt_fec_fin = DateAdd(d, -1, DateAdd(m, 1, @vdt_fec_ini))

  
    SELECT  
        @vdc_total = count(*)   
    FROM  
        trabajadores_grales   
    WHERE  
        compania = @pch_compania And  
        clase_nomina <> 'NP' And  
        fecha_ingreso <= @vdt_fec_fin And  
        (fecha_baja is null or fecha_baja >= @vdt_fec_ini)   
/*And  
        ((@vti_por_suc = 0) OR  
         (@vti_por_suc = 1 AND trabajador in  
            (SELECT a.trabajador FROM rel_trab_agr a, parametros_mf b  
             WHERE b.compania = @pch_compania  
               AND b.parametro = 'autoliq'  
               AND b.secuencia = 23)))       
    SELECT  
        @vdc_total  = @vdc_total  + count(*)   
    FROM  
        trabajadores_grales  
    WHERE  
        compania = @pch_compania And  
        clase_nomina = 'NP' And  
        sit_trabajador = 1*/  
  
    RETURN @vdc_total     
END  
GO
GRANT EXECUTE ON dbo.fn_fco_nro_trab_eps TO PUBLIC
GO
