Class: Quickbooks::Adapters::OleAdapter::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/quickbooks/adapters/ole_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application_name = 'RubyApplication', file = '', connection_type = 'localQBD', connection_mode = 'DoNotCare') ⇒ Connection

Initializes an instance of Quickbooks::Connection.

  • application_name is required.

  • file is optional, in which case the connection will be made to the currently open company file in the Quickbooks application.

  • user and password may be required if you have specified a specific file to open.

  • connection_type can be one of: [‘unknown’, ‘localQBD’, ‘remoteQBD’, ‘localQBDLaunchUI’, ‘remoteQBOE’]

  • connection_mode can be one of: [‘SingleUser’, ‘MultiUser’, ‘DoNotCare’]



87
88
89
90
91
92
93
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 87

def initialize(application_name='RubyApplication', file='', connection_type='localQBD', connection_mode='DoNotCare')
  @file = file
  @application_name = application_name
  @quickbooks = Ole.new('QBXMLRP2.RequestProcessor', 'QBXMLRP2 1.0 Type Library')
  @connection_type = @quickbooks.get_variable(connection_type)
  @connection_mode = @quickbooks.get_variable('qbFileOpen'+connection_mode)
end

Class Method Details

.connectionsObject



66
67
68
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 66

def connections
  @connections ||= []
end

.connections=(v) ⇒ Object



69
70
71
72
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 69

def connections=(v)
  # raise ArgumentError, "Quickbooks::Connection.connections can only contain Quickbooks::Connection objects, but contains #{v.collect {|c| c.class.name}.uniq.join(', ')} objects" unless v.collect {|c| c.class.name}.uniq == ['Quickbooks::Connection']
  @connections = v
end

Instance Method Details

#closeObject

Close the connection to Quickbooks. Automatically ends the session, if there is one.



107
108
109
110
111
112
113
114
115
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 107

def close
  end_session
  if connected? && connection.CloseConnection.nil?
    @connected = false
    @connection = nil
    Connection.connections = Connection.connections - [self]
  end
  return !@connected # Returns false if CloseConnection failed.
end

#connected?Boolean

Returns true if there is an open connection to Quickbooks, false if not. Use session? to determine an open session.

Returns:



102
103
104
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 102

def connected?
  @connected ||= false
end

#connectionObject

Returns the active connection to Quickbooks, or creates a new one if none exists.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 139

def connection
  (@connection ||= nil) || begin
    @connection = @quickbooks.ole
    begin
      @connection.OpenConnection2('123',@application_name,@connection_type)
    rescue WIN32OLERuntimeError => e
      if e.inspect =~ /unknown property or method `OpenConnection2'/
        @connection.OpenConnection('123',@application_name)
      else
        raise e
      end
    end
    Connection.connections << self
    @connected = true
    @connection
  end
end

#end_sessionObject

End the current Quickbooks session. After ending a session, a new session may be reopened if desired.



167
168
169
170
171
172
173
174
175
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 167

def end_session
  if !@session.nil?
    connection.EndSession(@session)
    @session = nil
    true
  else
    false
  end
end

#latest_qbxml_versionObject



134
135
136
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 134

def latest_qbxml_version
  @latest_qbxml_version ||= qbxml_versions.sort.last
end

#login(username, password) ⇒ Object

Sets up the login to the QuickBooks company file.



96
97
98
99
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 96

def (username, password)
  @user = username
  @password = password
end

#qbxml_versionsObject

Check the version of QB



131
132
133
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 131

def qbxml_versions
  connection.QBXMLVersionsForSession(session)
end

#send_xml(xml) ⇒ Object

Sends a request to Quickbooks. This request should be a valid QBXML request. Use Qbxml::Request to generate valid requests.



123
124
125
126
127
128
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 123

def send_xml(xml)
  warn "Sending XML:\n#{xml}" if OleAdapter.debug?
  connection.ProcessRequest(session, xml)
rescue => e
  raise e, "ERROR processing request:\n#{xml}\n -> #{e.inspect}" # Reraises the original error, only this way we got the xml output
end

#sessionObject

Begin a session to Quickbooks.



158
159
160
161
162
163
164
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 158

def session
  (@session ||= nil) || begin
    # Should set personal-data-pref to pdpNotNeeded by default, so that it doesn't ask.
    # connection.PutPersonalDataPref(@quickbooks.get_variable('pdpNotNeeded'))
    @session = connection.BeginSession(@file,@connection_mode)
  end
end

#session?Boolean

Returns true if there is an active session. Use connected? to determine whether you are connected or not.

Returns:



118
119
120
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 118

def session?
  !@session.nil?
end