DROP PROCEDURE IN_BuscarMovimientoProc
GO
CREATE PROCEDURE IN_BuscarMovimientoProc
(@SITEID	UD_IDSITE,
 @FECHAINICIAL	SMALLDATETIME,
 @FECHAFINAL 	SMALLDATETIME,
 @INOUT		BIT,
 @ESTADODOC	INT
)
AS
	BEGIN
		IF (@SITEID<>'')
		  BEGIN
			IF (@ESTADODOC=0)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE SITEID=@SITEID AND INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT
				ORDER BY SITEID,BATNBR
			END
	
			IF (@ESTADODOC=1)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE SITEID=@SITEID AND INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT AND RLSED=1
				ORDER BY SITEID,BATNBR
			END
	
			IF (@ESTADODOC=2)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE SITEID=@SITEID AND INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT AND RLSED=0 AND DOCSTATUSID<>'AN'
				ORDER BY SITEID,BATNBR
			END
			
			IF (@ESTADODOC=3)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE SITEID=@SITEID AND INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT AND DOCSTATUSID='AN'
				ORDER BY SITEID,BATNBR
			END
		  END
		ELSE 
		  BEGIN
			IF (@ESTADODOC=0)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT
				ORDER BY SITEID,BATNBR
			END
	
			IF (@ESTADODOC=1)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT AND RLSED=1
				ORDER BY SITEID,BATNBR
			END
	
			IF (@ESTADODOC=2)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT AND RLSED=0 AND DOCSTATUSID<>'AN'
				ORDER BY SITEID,BATNBR
			END

			IF (@ESTADODOC=3)			
			BEGIN
				SELECT SITEID AS Almacen,BATNBR AS Nro_Movimiento,IN_INTRAHEAD.TYPEDOCID as Tipo_Movimiento,REFNBR as Nro_Documento,INTRDATE AS Fec_Documento 
				FROM IN_INTRAHEAD JOIN IN_TYPEDOC
				ON IN_INTRAHEAD.TYPEDOCID=IN_TYPEDOC.TYPEDOCID
				WHERE INTRDATEPROCE BETWEEN @FECHAINICIAL AND @FECHAFINAL AND INOUT=@INOUT  AND DOCSTATUSID='AN'
				ORDER BY SITEID,BATNBR
			END

		  END
	END





