if exists (select name from sysobjects
         where name = 'InterPackConsulta' and type = 'P')
    drop procedure dbo.InterPackConsulta
go
create procedure dbo.InterPackConsulta
    (
     @parFechaDesde as datetime,@parFechaHasta as datetime,@Resultado varchar(2) output
    )
as
BEGIN
	declare @viCantidad smallint

	if exists (select name from sysobjects where name = 'TmpInterPack' and type = 'U')
	BEGIN
		DROP TABLE TmpInterPack
	END

	CREATE TABLE TmpInterPack (Legajo int NOT null, Apellido_Nombre varchar(40) null, Fecha_Ingreso datetime null, Hs_Presencia varchar(10) null, Sector varchar(40) null, Tipo varchar(1) null, Trabajador varchar(1) null, PRIMARY KEY(Legajo))

	INSERT INTO TmpInterPack 
	Select Ft.Legajo as Legajo
	 ,Max(Ft.Apellido_Nombre) as Apellido_Nombre
	 ,Max(Ft.Fecha_Ingreso) as Fecha_Ingreso
	 ,dbo.fn_MinutosSTR(SUM(Ft.Hs_Presencia)) as Hs_Presencia
	 ,Ft.Sector as Sector
	 ,Max(Ft.Tipo) as Tipo
	 , 'J' as Trabajador
	 From
	 (Select P.LegNume as Legajo
	 ,P.LegApNo as Apellido_Nombre
	 ,P.LegFeIn as Fecha_Ingreso
	 ,dbo.fn_STRMinutos(F1.FicHsAu2) as Hs_Presencia
	 ,S.SecDesc as Sector
	 , case when P.LegIntExt=0 then 'E' else 'A' end as Tipo
	 From FICHAS F
	 ,FICHAS1 F1
	 ,PERSONAL P
	 ,SECTORES S
	 Where (F.FicFech BETWEEN @parFechaDesde AND @parFechaHasta)
	 and P.LegTipo = 1
	 and P.LegFeEg = '17530101'
	 and F.FicLega = P.LegNume
	 and F.FicLega = F1.FicLega
	 and F.FicFech = F1.FicFech
	 and F.FicSect = S.SecCodi
	 and F1.FicHora = 95
	 ) Ft
	 Group by Ft.Sector,Ft.Legajo
	 Order by Ft.Sector,Ft.Legajo

	select @viCantidad = (Select count(*) From TmpInterPack)
	if @viCantidad > 0
		Set @Resultado = 'OK'
	Else
		Set @Resultado = 'NO'
End
Return

