ALTER     PROCEDURE [dbo].[sp_proDisponible]
(
 @fecha datetime,
 @idop as bigint= 0,
 @codetapa int= 0,
 @codmaquina int= 0,
 @tipo int = 1
)
AS
declare @dia as int
declare @hora as time
declare @RESULTADO as int = 0
declare @count as int=0
declare @a as int
declare @b as int
declare @c as int

set @hora=CONVERT(TIME,@fecha) 
set @dia= datepart(DW,@fecha)

SET @RESULTADO =0
IF @dia = 1
  BEGIN
      
	SELECT @a = COUNT(codTurno)  FROM  [proTurnos]
	WHERE @hora between [LUNinicio] AND [LUNtermino] AND ([LUNinicio] < [LUNtermino]) 
	AND LUNActivo=1

	SELECT @b = COUNT(codTurno) FROM [proTurnos]
	WHERE @hora between [LUNinicio] AND '23:59:59.0000000' AND ([LUNinicio] > [LUNtermino])
	AND LUNActivo=1
  
	SELECT @c = COUNT(codTurno) FROM  [proTurnos]
	WHERE @hora between '00:00:00.0000000' AND [DOMtermino]
	AND DOMActivo=1
    SET @count = @a+@b+@c
	
	END
  ELSE IF @dia = 2
	  BEGIN
		SELECT @a = COUNT(codTurno)  FROM  [proTurnos]
		WHERE @hora between [Marinicio] AND [Martermino] AND ([Marinicio] < [Martermino]) 
		AND MArActivo=1

		SELECT @b = COUNT(codTurno) FROM [proTurnos]
		WHERE @hora between [Marinicio] AND '23:59:59.0000000' AND ([Marinicio] > [Martermino])
		AND MarActivo=1
	  
		SELECT @c = COUNT(codTurno) FROM  [proTurnos]
		WHERE @hora between '00:00:00.0000000' AND [Luntermino]
		AND LunActivo=1
		SET @count = @a+@b+@c
	  END
  ELSE IF @dia = 3
  BEGIN
		SELECT @a = COUNT(codTurno)  FROM  [proTurnos]
		WHERE @hora between [Mieinicio] AND [Mietermino] AND ([Mieinicio] < [Mietermino]) 
		AND MieActivo=1

		SELECT @b = COUNT(codTurno) FROM [proTurnos]
		WHERE @hora between [Mieinicio] AND '23:59:59.0000000' AND ([Mieinicio] > [Mietermino])
		AND MieActivo=1
	  
		SELECT @c = COUNT(codTurno) FROM  [proTurnos]
		WHERE @hora between '00:00:00.0000000' AND [Martermino]
		AND MarActivo=1
		SET @count = @a+@b+@c
     END
  ELSE IF @dia = 4
  BEGIN
	    SELECT @a = COUNT(codTurno)  FROM  [proTurnos]
		WHERE @hora between [Jueinicio] AND [Juetermino] AND ([Jueinicio] < [Juetermino]) 
		AND JueActivo=1

		SELECT @b = COUNT(codTurno) FROM [proTurnos]
		WHERE @hora between [Jueinicio] AND '23:59:59.0000000' AND ([Jueinicio] > [Juetermino])
		AND JueActivo=1
	  
		SELECT @c = COUNT(codTurno) FROM  [proTurnos]
		WHERE @hora between '00:00:00.0000000' AND [Mietermino] 
		AND MieActivo=1
		SET @count = @a+@b+@c
    END
  ELSE IF @dia = 5
  BEGIN
	    SELECT @a = COUNT(codTurno)  FROM  [proTurnos]
		WHERE @hora between [Vieinicio] AND [Vietermino] AND ([Vieinicio] < [Vietermino]) 
		AND VieActivo=1

		SELECT @b = COUNT(codTurno) FROM [proTurnos]
		WHERE @hora between [Vieinicio] AND '23:59:59.0000000' AND ([Vieinicio] > [Vietermino])
		AND VieActivo=1
	  
		SELECT @c = COUNT(codTurno) FROM  [proTurnos]
		WHERE @hora between '00:00:00.0000000' AND [Juetermino] 
		AND JueActivo=1
		SET @count = @a+@b+@c
	
  END
  ELSE IF @dia = 6
  BEGIN
	    SELECT @a = COUNT(codTurno)  FROM  [proTurnos]
		WHERE @hora between [Sabinicio] AND [Sabtermino] AND ([Sabinicio] < [Sabtermino]) 
		AND SabActivo=1

		SELECT @b = COUNT(codTurno) FROM [proTurnos]
		WHERE @hora between [Sabinicio] AND '23:59:59.0000000' AND ([Sabinicio] > [Sabtermino])
		AND SabActivo=1
	  
		SELECT @c = COUNT(codTurno) FROM  [proTurnos]
		WHERE @hora between '00:00:00.0000000' AND [Vietermino]
		AND VieActivo=1
		SET @count = @a+@b+@c
  END
  ELSE IF @dia = 7
  BEGIN
	    SELECT @a = COUNT(codTurno)  FROM  [proTurnos]
		WHERE @hora between [Sabinicio] AND [Sabtermino] AND ([Sabinicio] < [Sabtermino]) 
		AND SabActivo=1

		SELECT @b = COUNT(codTurno) FROM [proTurnos]
		WHERE @hora between [Sabinicio] AND '23:59:59.0000000' AND ([Sabinicio] > [Sabtermino])
		AND SabActivo=1
	  
		SELECT @c = COUNT(codTurno) FROM  [proTurnos]
		WHERE @hora between '00:00:00.0000000' AND [Sabtermino]
		AND SabActivo=1
		SET @count = @a+@b+@c
  END 

IF @count = 0
BEGIN
SELECT '1' as respuesta
RETURN
END
------VERIFICACION EN DESCANSOS-----
SELECT @RESULTADO = COUNT([codTurno]) FROM proDescansos
WHERE dia=@dia AND @hora between [inicio] AND [termino]

IF @RESULTADO > 0
BEGIN
SELECT '2' as respuesta
RETURN 
END

------VERIFICACION EN FERIADOS-----
SELECT @RESULTADO = COUNT([id]) FROM [proFeriados]
WHERE @fecha between [inicio] AND [termino]
AND tipo=1


IF @RESULTADO > 0
BEGIN
SELECT '3' as respuesta
RETURN 
END


------VERIFICACION EN FERIADOS ETAPA-----
SELECT @RESULTADO = COUNT([id]) FROM [proFeriados]
WHERE @fecha between [inicio] AND [termino]
AND tipo=2 and codigo = @codetapa


IF @RESULTADO > 0
BEGIN
SELECT '4' as respuesta
RETURN 
END


------VERIFICACION EN FERIADOS MAQUINA-----

SELECT @RESULTADO = COUNT([id]) 
FROM         proFeriados 
WHERE @fecha between [inicio] AND [termino]
AND tipo=3 and codigo = @codmaquina



IF @RESULTADO > 0
BEGIN
SELECT '5' as respuesta
RETURN 
END


------VERIFICACION EN ORDENES-----
------TIPO= "1" INCLUYE ORDEN "2" ESCLUYE ORDENES "3" INLUYE ORDENES SIMULACION
IF @TIPO = 1 OR @TIPO=4
BEGIN
		SELECT @RESULTADO = COUNT(idOp)
		FROM   proOPEtapas	INNER JOIN
                      proMaquinas ON  proOPEtapas.maquina = proMaquinas.codMaquina
					  AND proOPEtapas.periodo = proMaquinas.periodo
		WHERE maquina = @codmaquina
		AND idop <> @idOp
		AND proMaquinas.exclusiva=1
		AND @fecha between [inicio] AND [termino]
			
	IF @RESULTADO > 0
		BEGIN
		SELECT '6' as respuesta
		RETURN 
	END
END

IF @TIPO = 3 OR @TIPO=4
BEGIN
		SELECT @RESULTADO = COUNT(idOp)
		FROM   proOPEtapasSim	INNER JOIN
                      proMaquinas ON  proOPEtapasSim.maquina = proMaquinas.codMaquina
		WHERE maquina = @codmaquina
		AND exclusiva=1
		AND @fecha between [inicio] AND [termino]
	
	
	IF @RESULTADO > 0
		BEGIN
		SELECT '6' as respuesta
		RETURN 
	END
END


SELECT '0' as respuesta