ALTER PROCEDURE [dbo].[sp_finSaldoInicial]
(
@id_misbancos  int=0,
@fecha DATETIME = '01-06-2013',
@tipoSaldo int=0
)	
AS 
DECLARE @saldoinicial decimal(18,4) 
DECLARE @cargos decimal(18,4)
DECLARE @abonos decimal(18,4)
DECLARE @chequesgnc decimal(18,4)
DECLARE @cheques decimal(18,4)
DECLARE @c1 decimal(18,4)
DECLARE @c2 decimal(18,4)
DECLARE @c3 decimal(18,4)

--	SALDO INICIAL CARTOLA
----------------------------
SELECT @saldoinicial= [saldoInicial]
FROM [cobMisBancos]
WHERE id_misbancos = @id_misbancos

--	CHEQUES GIRADOS NO COBRADOS
--------------------------------
SELECT  @chequesgnc = SUM(monto)
FROM         finCheques
WHERE Num_Cheque NOT IN (SELECT numDocumento 
								FROM  finCartola 
								WHERE finCartola.id_MisBancos = finCheques.id_MisBancos
								AND   finCartola.fecha < @fecha)
AND id_MisBancos = @id_MisBancos
AND Num_Cheque <> 0
AND estado <> 2
AND fecha_ingreso < @fecha

--	CARGOS DE MESES ANTERIORES
-------------------------------
SELECT @cargos = SUM(finCartola.monto)
FROM         finCartola 
WHERE     finCartola.id_MisBancos = @id_misbancos
AND finCartola.fecha < @fecha
AND finCartola.tipoConcepto = 2

--	ABONOS DE MESES ANTERIORES
-------------------------------
SELECT @abonos = SUM(finCartola.monto)
FROM         finCartola 
WHERE     (finCartola.id_MisBancos = @id_misbancos)
AND (finCartola.fecha < @fecha)
AND finCartola.tipoConcepto = 3 

IF @tipoSaldo = 1 BEGIN
	--	CHEQUES COBRADOS DE MESES ANTERIORES PARA LA CARTOLA
	--------------------------------------------------------
	SELECT @cheques = SUM(finCartola.monto)
	FROM         finCartola 
	WHERE     finCartola.id_MisBancos = @id_misbancos
	AND finCartola.fecha < @fecha
	AND finCartola.tipoConcepto = 1
END ELSE BEGIN
	--	CHEQUES INGRESADOS DE MESES ANTERIORES PARA EL LIBRO
	--------------------------------------------------------
	SELECT  @cheques = SUM(monto)
	FROM         finCheques
	WHERE   id_MisBancos = @id_MisBancos
	AND		Num_Cheque <> 0
	AND     finCheques.fecha_ingreso < @fecha
	AND		estado <> 2
END

--SELECT  @saldoinicial as saldoinicial , 
--		@cargos as cargos , 
--		@abonos as abonos , 
--		@chequesgnc as chequesgnc , 
--		@cheques as cheques 

IF @tipoSaldo = 1 BEGIN
	--	CHEQUES COBRADOS DEL MES PARA CARTOLA
	------------------------------------------
	SELECT   @c1 = SUM(monto)
	FROM          finCartola
	WHERE       id_MisBancos = @id_misbancos
	AND         finCartola.mes = MONTH(@fecha)
	AND		    finCartola.agno = YEAR(@fecha)
	AND			finCartola.tipoConcepto=1
END ELSE BEGIN
	--	CHEQUES GIRADOS DEL MES PARA EL LIBRO
	-----------------------------------------
	SELECT  @c1 = SUM(monto)
	FROM         finCheques
	WHERE id_MisBancos = @id_MisBancos
	AND Num_Cheque <> 0
	AND MONTH(finCheques.fecha_ingreso) = MONTH(@fecha)
	AND	YEAR(finCheques.fecha_ingreso) = YEAR(@fecha)
	AND estado <> 2
END

--	CARGOS DEL MES
-------------------
SELECT   @c2=  SUM(monto)
FROM          finCartola
WHERE      (id_MisBancos = @id_misbancos)
AND         finCartola.mes = MONTH(@fecha)
AND		    finCartola.agno = YEAR(@fecha)
AND			finCartola.tipoConcepto=2

--	ABONOS DEL MES
------------------
SELECT    @c3= SUM(monto)
FROM          finCartola
WHERE      (id_MisBancos = @id_misbancos)
AND         finCartola.mes = MONTH(@fecha)
AND		    finCartola.agno = YEAR(@fecha)
AND			finCartola.tipoConcepto=3

--SELECT COALESCE(@saldoinicial,0) - COALESCE(@cargos,0)  - COALESCE(@cheques,0) - CASE @tipoSaldo WHEN 0 THEN COALESCE(@chequesgnc,0) ELSE 0 END + COALESCE(@abonos,0) AS Saldo_Inicial, 
SELECT COALESCE(@saldoinicial,0) - COALESCE(@cargos,0)  - COALESCE(@cheques,0) + COALESCE(@abonos,0) AS Saldo_Inicial, 
		COALESCE(@cargos,0) AS cargos_ant, 
		COALESCE(@cheques,0) AS cheques_ant,
		COALESCE(@abonos,0) AS abonos_ant,
		COALESCE(@c1,0) AS cheques_mes, 
		COALESCE(@c2,0) AS cargos_mes, 
		COALESCE(@c3,0) AS abonos_mes,
		COALESCE(@chequesgnc,0) AS chequesgnc,
	   (COALESCE(@saldoinicial,0) - COALESCE(@cargos,0) - COALESCE(@cheques,0) + COALESCE(@abonos,0)) - 
		--COALESCE(@chequesgnc,0) -
		COALESCE(@c1,0) - 
		COALESCE(@c2,0) +
		COALESCE(@c3,0) AS Saldo_Final