ALTER      PROCEDURE [dbo].[sp_finListadoCartola]
(
     --@id_cheque int,
     @fecini		datetime,
     @fecfin		datetime,
     @id_misbancos int = 0,
     @estado int = 0,
     @contabilizado int = 0,
     @afecha int= 0,
	 @idconcepto int = 0,
	 @rut int = 0
      )

AS

IF @estado <> 1  --CARGOS Y ABONOS
BEGIN 

		SELECT     finCartola.id, finCartola.id_MisBancos, finCartola.mes, finCartola.agno, finCartola.fecha, finCartola.tipoConcepto, finCartola.concepto, finCartola.descripcion, 
                      finCartola.numDocumento, finCartola.rut, finCartola.monto, finCartola.tipoCompro, finCartola.numCompro, finCartola.fecCompro, finCartola.contabilizado, 
                      cobMisBancos.codban, cobMisBancos.nomban, cobMisBancos.ctacontable, cobMisBancos.ndepositos, cobMisBancos.nomejecutivo, cobMisBancos.ctacorriente, 
                      cobMisBancos.fonoejecutivo, cobMisBancos.saldoInicial, cobMisBancos.mes AS mesmisbancos, cobMisBancos.agno AS agnomisbancos, cobMisBancos.saldoLibro, 
                      cobMisBancos.fechaSaldo, conBancos.nombre, conBancos.codSBIF, finConceptos.tipo AS tipoconcepto, finConceptos.codigo AS codconcepto, 
                      finConceptos.descripcion AS desc_concepto, finConceptos.cuentaContable AS ctaconcepto, COALESCE (conCtacte.rutcte, finCartola.rut) AS rutcte, conCtacte.digcte, 
                      COALESCE (conCtacte.nomcte, 'CLIENTE NO EXISTE EN MAESTRO') AS nomcte, conTipoComprobante.descripcion AS desc_tipocomprobante, 0 AS afecha,
					  coalesce(finCartola.id_rendicion,0) AS id_rendicion,finCartola.flujo, finCartola.idAnticipo
FROM         finCartola INNER JOIN
                      cobMisBancos ON finCartola.id_MisBancos = cobMisBancos.id_misbancos INNER JOIN
                      conBancos ON cobMisBancos.codban = conBancos.idBanco INNER JOIN
                      finConceptos ON finCartola.tipoConcepto = finConceptos.tipo AND finCartola.concepto = finConceptos.codigo LEFT OUTER JOIN
                      conTipoComprobante ON finCartola.tipoCompro = conTipoComprobante.codigo LEFT OUTER JOIN
                      conCtacte ON finCartola.rut = conCtacte.rutcte
		WHERE     (finCartola.tipoConcepto = @estado) AND (@id_misbancos =0 OR finCartola.id_MisBancos=@id_misbancos)
        AND ((@contabilizado = 1 AND finCartola.tipocompro <> 0)
		OR  (@contabilizado = 2 AND finCartola.tipocompro = 0)
		OR  (@contabilizado = 0))
		AND (finCartola.fecha BETWEEN @fecini AND @fecfin)
		AND (@idconcepto=0 OR finCartola.concepto=@idconcepto)
		AND (@rut=0 OR finCartola.rut=@rut)
RETURN 1
END

IF @estado=1 --CHEQUES EN CARTOLA
BEGIN 


		                    
SELECT     finCartola.id, finCartola.id_MisBancos, finCartola.mes, finCartola.agno, finCartola.fecha, finCartola.tipoConcepto, finCartola.concepto, finCartola.descripcion, 
                      finCartola.numDocumento, finCartola.rut, finCartola.monto, finCartola.tipoCompro, finCartola.numCompro, finCartola.fecCompro, finCartola.contabilizado, 
                      cobMisBancos.codban, cobMisBancos.nomban, cobMisBancos.ctacontable, cobMisBancos.ndepositos, cobMisBancos.nomejecutivo, cobMisBancos.ctacorriente, 
                      cobMisBancos.fonoejecutivo, cobMisBancos.saldoInicial, cobMisBancos.mes AS mesmisbancos, cobMisBancos.agno AS agnomisbancos, cobMisBancos.saldoLibro, 
                      cobMisBancos.fechaSaldo, conBancos.nombre, conBancos.codSBIF, finConceptos.tipo AS tipoconcepto, finConceptos.codigo AS codconcepto, 
                      finConceptos.descripcion AS desc_concepto, finConceptos.cuentaContable AS ctaconcepto, COALESCE (conCtacte.rutcte, finCartola.rut) AS rutcte, conCtacte.digcte, 
                      COALESCE (conCtacte.nomcte, 'CLIENTE NO EXISTE EN MAESTRO') AS nomcte, COALESCE (finCheques.afecha, 0) AS afecha, conTipoComprobante.codigo, 
                      conTipoComprobante.descripcion AS desc_tipocomprobante, finCheques.id_misbancos AS Expr1,
					  coalesce(finCartola.id_rendicion,0) AS id_rendicion,finCheques.uniNeg,finCartola.flujo, finCartola.idAnticipo
FROM         conBancos INNER JOIN
                      cobMisBancos ON conBancos.idBanco = cobMisBancos.codban INNER JOIN
                      finConceptos INNER JOIN
                      finCartola ON finConceptos.tipo = finCartola.tipoConcepto AND finConceptos.codigo = finCartola.concepto ON 
                      cobMisBancos.id_misbancos = finCartola.id_MisBancos LEFT OUTER JOIN
                      finCheques ON cobMisBancos.id_misbancos = finCheques.id_misbancos AND finCartola.numDocumento = finCheques.Num_Cheque LEFT OUTER JOIN
                      conTipoComprobante ON finCartola.tipoCompro = conTipoComprobante.codigo LEFT OUTER JOIN
                      conCtacte ON finCheques.rut = conCtacte.rutcte
WHERE     (finCartola.tipoConcepto = 1) AND (@id_misbancos =0 OR finCartola.id_MisBancos=@id_misbancos)
        AND ((@contabilizado = 1 AND finCartola.tipocompro <> 0)
		OR  (@contabilizado = 2 AND finCartola.tipocompro = 0)
		OR  (@contabilizado = 0))
        AND (finCheques.afecha=1 OR @afecha=0)
        AND finCartola.fecha BETWEEN @fecini AND @fecfin

RETURN 1
END