CREATE PROCEDURE CP_Clientes_Temp_Actualizar_Cbte @LOTE VARCHAR(20) , @Id_Cliente INT
AS 
DECLARE @Mensaje			VARCHAR(100)

IF @Id_Cliente = 0 OR ISNULL(@LOTE,'') = ''
BEGIN
	SELECT	 0					AS ErrorNumber 
			,0					AS ErrorSeverity  
			,0					AS ErrorState 
			,0					AS ErrorProcedure  
			,0					AS ErrorLine		
			,'No se especifico cliente o lote'			AS ErrorMessage
	RETURN 
END
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),0)
	IF (@trans_ND = 0)
	BEGIN
		PRINT 'No hay trans de Factura'
	END

	SET @trans_rec = ISNULL((SELECT MAX(Id_trans) FROM Cbtes WHERE LOTE = @LoteRec),0)
	IF (@trans_rec = 0)
	BEGIN
		PRINT 'No hay trans de recibo'
	END



	BEGIN TRAN

	IF (@trans_ND != 0)
	BEGIN
		UPDATE Clientes_CtaCte	SET Id_cliente = @Id_Cliente WHERE Id_Trans = @trans_ND
		UPDATE Cbtes_Items		SET Id_cliente = @Id_Cliente WHERE Id_Trans = @trans_ND
		UPDATE Cbtes			SET Id_cliente = @Id_Cliente WHERE Id_Trans = @trans_ND
	END

	IF (@trans_rec != 0)
	BEGIN
		UPDATE Clientes_CtaCte	SET Id_cliente = @Id_Cliente WHERE Id_Trans = @trans_rec
		UPDATE Cbtes_Items		SET Id_cliente = @Id_Cliente WHERE Id_Trans = @trans_rec
		UPDATE Cbtes			SET Id_cliente = @Id_Cliente WHERE Id_Trans = @trans_rec
	END
	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;   
