ALTER PROCEDURE [dbo].[sp_venEstacionamientoListar]

(
 @fecini AS DATETIME='01-01-1900', 
 @fecfin AS DATETIME='01-01-2050',
 @tipo   AS INT,
 @patente AS VARCHAR(6)=''
)

AS

IF @tipo=0 --todos
BEGIN
SELECT 
	codEstacionamiento,
	codBar,
	patente,
	entrada,
	usuarioEnt,
	salida,
	usuarioSal,
	estadia,
	numBoleta,
	monto,	
	
	CASE 
		WHEN ISNULL(salida_ok,0)=1 THEN 'SI'
		ELSE 'NO'
	END AS salida_ok,

	CASE
		WHEN ISNULL(lpr_in,0)= 1 THEN 'SI'
		ELSE 'NO'
	END AS lpr_in,
	
	CASE
	   WHEN ISNULL (lpr_out,0)= 1 THEN 'SI'
	   ELSE 'NO'
	END AS lpr_out,

	CASE
		WHEN ISNULL (blanco,0)=1 THEN 'BLANCO'
		WHEN ISNULL (negro,0)=1 THEN 'NEGRO'
		ELSE 'NORMAL'
	END AS tipo		
	FROM venEstacionamiento 
		WHERE (entrada between @fecini and @fecfin)
		AND (patente=@patente or @patente='')
		ORDER BY entrada DESC
END

IF @tipo=1 --pendientes
BEGIN
SELECT 
	codEstacionamiento,
	codBar,
	patente,
	entrada,
	usuarioEnt,
	salida,
	usuarioSal,
	estadia,
	numBoleta,
	monto, 

	CASE 
		WHEN ISNULL(salida_ok,0)=1 THEN 'SI'
		ELSE 'NO'
	END AS salida_ok,
	
	CASE
		WHEN ISNULL(lpr_in,0)= 1 THEN 'SI'
		ELSE 'NO'
	END AS lpr_in,
	
	CASE
	   WHEN ISNULL (lpr_out,0)= 1 THEN 'SI'
	   ELSE 'NO'
	END AS lpr_out,

	CASE
		WHEN ISNULL (blanco,0)=1 THEN 'BLANCO'
		WHEN ISNULL (negro,0)=1 THEN 'NEGRO'
		ELSE 'NORMAL'
	END AS tipo		

		FROM venEstacionamiento 
		WHERE (entrada between @fecini and @fecfin)
		AND salida IS NULL
		AND (patente=@patente or @patente='')
	ORDER BY entrada DESC
END

IF @tipo=2 --cerrados
BEGIN
SELECT 
	codEstacionamiento,
	codBar,
	patente,
	entrada,
	usuarioEnt,
	salida,
	usuarioSal,
	estadia,
	numBoleta,
    monto, 
	
	CASE 
		WHEN ISNULL(salida_ok,0)=1 THEN 'SI'
		ELSE 'NO'
	END AS salida_ok,
	
	CASE
		WHEN ISNULL(lpr_in,0)= 1 THEN 'SI'
		ELSE 'NO'
	END AS lpr_in,
	
	CASE
	   WHEN ISNULL (lpr_out,0)= 1 THEN 'SI'
	   ELSE 'NO'
	END AS lpr_out,

	CASE
		WHEN ISNULL (blanco,0)=1 THEN 'BLANCO'
		WHEN ISNULL (negro,0)=1 THEN 'NEGRO'
		ELSE 'NORMAL'
	END AS tipo		

		FROM venEstacionamiento 
		WHERE (entrada between @fecini and @fecfin)
		AND salida IS NOT NULL
	    AND (patente=@patente or @patente='')
	ORDER BY entrada DESC
END
IF @tipo = 3
	BEGIN
		SELECT 
			D.codEstacionamiento,
			D.codBar,
			D.patente,
			E.entrada,
			E.usuarioEnt,
			D.salida,
			D.usuarioSal,
			D.estadia,
			E.numBoleta,
			D.monto,
			CASE 
				WHEN ISNULL(salida_ok,0)=1 THEN 'SI'
				ELSE 'NO'
			END AS salida_ok,
	
			CASE
				WHEN ISNULL(lpr_in,0)= 1 THEN 'SI'
				ELSE 'NO'
			END AS lpr_in,
	
			CASE
			   WHEN ISNULL (lpr_out,0)= 1 THEN 'SI'
			   ELSE 'NO'
			END AS lpr_out,

			CASE
				WHEN ISNULL (blanco,0)=1 THEN 'BLANCO'
				WHEN ISNULL (negro,0)=1 THEN 'NEGRO'
				ELSE 'NORMAL'
			END AS tipo
		FROM VenEstacionamientoDeuda D LEFT OUTER JOIN
		venEstacionamiento E ON D.codEstacionamiento = E.codEstacionamiento
		WHERE (D.entrada between @fecini and @fecfin)
		AND (D.patente=@patente or @patente='')
	END