CREATE PROCEDURE [dbo].[CP_Cbtes_Fac_Fin] @Id_Trans VARCHAR(20)
AS 

DECLARE @Mensaje			VARCHAR(100),@trans_ND INT

BEGIN TRY
	BEGIN TRAN
	SET @trans_ND = ISNULL((SELECT MAX(Id_trans) FROM Cbtes WHERE Id_Estado_Cbte = 0 AND Id_Trans = @Id_Trans),0)
	IF (@trans_ND = 0)
	BEGIN
	    SET @Mensaje = 'No hay trans de Factura'
		COMMIT
		SELECT	 1099					AS ErrorNumber 
				,0					AS ErrorSeverity  
				,0					AS ErrorState 
				,0					AS ErrorProcedure  
				,0					AS ErrorLine		
				,@Mensaje			AS ErrorMessage
				,@trans_ND			AS Id_Trans		
		RETURN 1
	END
	ELSE
	BEGIN
		PRINT @trans_ND
		UPDATE Cbtes SET Id_Estado_Cbte = 2,Saldo = 0			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,Saldo = 0 WHERE Id_Trans = @trans_ND
		UPDATE Cajas_Mov		SET Id_Estado_Mov_Caja = 2		WHERE Id_Trans = @trans_ND  --en siempre va en completo
	END

	COMMIT

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

END TRY  


BEGIN CATCH  
    EXECUTE usp_GetErrorInfo;  
	ROLLBACK
END CATCH;   
