CREATE PROCEDURE [dbo].[CP_Cbtes_Confirmar_Online] @LOTE VARCHAR(20)
AS 

DECLARE @Mensaje			VARCHAR(100)

BEGIN TRY
	DECLARE @trans_rec INTEGER,@trans_ND INT,@LoteND VARCHAR(20),@LoteRec VARCHAR(20)
	SET @LoteND = 'VW:' + @LOTE
	SET @LoteRec = 'CW:' + @LOTE

	SET @trans_ND = ISNULL((SELECT MAX(Id_trans) FROM Cbtes WHERE LOTE = @LoteND AND Id_Estado_Cbte = 0),0)
	IF (@trans_ND = 0)
	BEGIN
	    SET @Mensaje = 'No hay trans de Factura'
		PRINT @Mensaje
		SELECT	 1					AS ErrorNumber 
				,0					AS ErrorSeverity  
				,0					AS ErrorState 
				,0					AS ErrorProcedure  
				,0					AS ErrorLine		
				,@Mensaje			AS ErrorMessage
		RETURN 1
	END
	PRINT @trans_ND

	SET @trans_rec = ISNULL((SELECT MAX(Id_trans) FROM Cbtes WHERE LOTE = @LoteRec AND Id_Estado_Cbte = 0),0)
	IF (@trans_rec = 0)
	BEGIN
	    SET @Mensaje = 'No hay trans de recibo'
		PRINT @Mensaje
		SELECT	 1					AS ErrorNumber 
				,0					AS ErrorSeverity  
				,0					AS ErrorState 
				,0					AS ErrorProcedure  
				,0					AS ErrorLine		
				,@Mensaje			AS ErrorMessage
		RETURN 1
	END
	PRINT @trans_rec

	BEGIN TRAN

	UPDATE Cbtes SET Id_Estado_Cbte = 2 WHERE Id_Trans = @trans_ND
	UPDATE Cbtes_Items SET Id_Estado_Cbte = 2 WHERE Id_Trans = @trans_ND
	UPDATE Clientes_CtaCte SET Id_Estado_Cbte = 2 WHERE Id_Trans = @trans_ND
	UPDATE Cbtes SET Id_Estado_Cbte = 2 WHERE Id_Trans = @trans_rec
	UPDATE Cbtes_Items SET Id_Estado_Cbte = 2 WHERE Id_Trans = @trans_rec
	UPDATE Clientes_CtaCte SET Id_Estado_Cbte = 2 WHERE Id_Trans = @trans_rec

	COMMIT

	SELECT	 0					AS ErrorNumber 
			,0					AS ErrorSeverity  
			,0					AS ErrorState 
			,0					AS ErrorProcedure  
			,0					AS ErrorLine		
			,@Mensaje			AS ErrorMessage

END TRY  


BEGIN CATCH  
    EXECUTE usp_GetErrorInfo;  
	ROLLBACK
END CATCH;   
