Class: Clearbooks::Client

Inherits:
Object
  • Object
show all
Extended by:
Savon::Model
Defined in:
lib/clearbooks/library/client.rb

Overview

Note:

You should not use it directly. Use static methods in Clearbooks module instead.

Examples:

Clearbooks.list_invoices

Clearbooks.create_entity(Clearbooks::Entity.new(...))

Instance Method Summary collapse

Instance Method Details

#allocate_payment(params) ⇒ Hash

Returns [:success, :msg] Operation results and brief message.

Parameters:

Returns:

  • (Hash)
    :success, :msg

    Operation results and brief message.

See Also:



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/clearbooks/library/client.rb', line 291

def allocate_payment params
  invoices = params[:invoices].map do |i|
    {
        :@id      => i[:id],
        :@amount  => i[:amount]
    }
  end

  payment = {
      :@paymentId => params[:payment_id],
      :@entityId  => params[:entity_id],
      :@type      => params[:type],
      :invoices   => { invoice: invoices }
  }

  response = super message: {payment: payment}
  response = response.to_hash
  response = response[:allocate_payment_response][:status]
  {
      success:  response[:@success].to_b,
      msg:      response[:@msg]
  }
end

#create_entity(entity) ⇒ Hash

Returns [:entity_id] ID of the created entity.

Examples:

Clearbooks.create_entity Clearbooks::Entity.new(company_name: 'Example Inc.',
       contact_name: 'John Doe',
           address1: 'London',
           country: 'UK',
           postcode: '01100',
           email: '[email protected]',
           website: 'http://example.com',
           phone1: '01234 567890',
           supplier: {
           default_account_code: '1001001',
           default_credit_terms: 30,
           default_vat_rate: 0
       })

Parameters:

Returns:

  • (Hash)
    :entity_id

    ID of the created entity.



136
137
138
139
140
# File 'lib/clearbooks/library/client.rb', line 136

def create_entity entity
  response = super message: entity.to_savon
  response = response.to_hash
  { entity_id: response[:create_entity_response][:create_entity_return].to_i }
end

#create_invoice(invoice) ⇒ Hash

Returns [:due, :invoice_id, :invoice_prefix, :invoice_number] according to official API docs.

Examples:

Clearbooks.create_invoice Clearbooks::Invoice.new(date_created: Date.today,
  credit_terms: 30,
  entity_id: 1,
  type: 'purchases',
  items: [
      Clearbooks::Item.new(description: 'abcd', unit_price: 10,
                           quantity: 5, type: '1001001', vat: 0, vat_rate: '0.00:Out')
  ])

Parameters:

Returns:

  • (Hash)
    :due, :invoice_id, :invoice_prefix, :invoice_number

    according to official API docs.



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/clearbooks/library/client.rb', line 85

def create_invoice invoice
  response = super message: invoice.to_savon
  response = response.to_hash
  response = response[:create_invoice_response][:create_invoice_return]
  {
      due:            BigDecimal.new(response[:@due]),
      invoice_id:     response[:@invoice_id].to_i,
      invoice_prefix: response[:@invoice_prefix],
      invoice_number: response[:@invoice_number]
  }
end

#create_journal(journal) ⇒ Hash

Returns [:journal_id] ID of the created journal.

Parameters:

Returns:

  • (Hash)
    :journal_id

    ID of the created journal.



249
250
251
252
253
# File 'lib/clearbooks/library/client.rb', line 249

def create_journal journal
  response = super message: journal.to_savon
  response = response.to_hash
  { journal_id: response[:create_journal_response][:create_journal_return][:@journal_id].to_i }
end

#create_payment(payment) ⇒ Hash

Returns [:payment_id] ID of the created payment.

Parameters:

Returns:

  • (Hash)
    :payment_id

    ID of the created payment.

See Also:



278
279
280
281
282
# File 'lib/clearbooks/library/client.rb', line 278

def create_payment payment
  response = super message: payment.to_savon
  response = response.to_hash
  { payment_id: response[:create_payment_response][:create_payment_return][:@payment_id].to_i }
end

#create_project(project) ⇒ Hash

Returns [:project_id] ID of the created project.

Examples:

Clearbooks.create_project Project.new(description: 'Project 1 description',
            project_name: 'Project 1 name',
            status: 'open')

Parameters:

Returns:

  • (Hash)
    :project_id

    ID of the created project.



208
209
210
211
212
# File 'lib/clearbooks/library/client.rb', line 208

def create_project project
  response = super message: project.to_savon
  response = response.to_hash
  { project_id: response[:create_project_response][:create_project_return][:@project_id].to_i }
end

#delete_entity(entity_id) ⇒ Boolean

Returns True if the request was successful, otherwise false.

Examples:

Clearbooks.delete_entity 10

Parameters:

Returns:

  • (Boolean)

    True if the request was successful, otherwise false.



191
192
193
194
195
# File 'lib/clearbooks/library/client.rb', line 191

def delete_entity entity_id
  response = super message: {entityId: entity_id}
  response = response.to_hash
  response[:delete_entity_response][:delete_entity_success]
end

#delete_journal(journal_id) ⇒ Boolean

Returns True if the request was successful, otherwise false.

Examples:

Clearbooks.delete_journal 10

Parameters:

  • journal_id (Fixnum)

    Id of the journal to be deleted.

Returns:

  • (Boolean)

    True if the request was successful, otherwise false.

See Also:



264
265
266
267
268
# File 'lib/clearbooks/library/client.rb', line 264

def delete_journal journal_id
  response = super message: {journalId: journal_id}
  response = response.to_hash
  response[:delete_journal_response][:journal_success]
end

#list_account_codesArray, AccountCode

Returns An array or projects.

Examples:

Clearbooks.

Returns:



236
237
238
239
240
# File 'lib/clearbooks/library/client.rb', line 236

def 
  response = super
  response = response.to_hash
  AccountCode.build response[:list_account_codes_response][:account_codes][:account_code]
end

#list_entities(query = {}) ⇒ Array, Entity

Returns An array or entities.

Examples:

Clearbooks.list_entities

Parameters:

Returns:



174
175
176
177
178
179
# File 'lib/clearbooks/library/client.rb', line 174

def list_entities query = {}
  attributes = {query: query.except(:id)}
  response = super message: {query: {id: query[:id]}, attributes!: attributes}
  response = response.to_hash
  Entity.build response[:list_entities_response][:entities][:entity]
end

#list_invoices(query = {}) ⇒ Array, Invoice

Returns An array or invoices.

Examples:

Clearbooks.list_invoices

Parameters:

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/clearbooks/library/client.rb', line 55

def list_invoices query = {}
  defaults = { ledger: :sales }
  attributes = defaults.merge(query)
  entity_id = attributes.delete :entity_id
  invoice_id = attributes.delete :id
  children = {}
  children[:entityId] = {'xsd:integer' => entity_id} if entity_id
  children[:id] = {'xsd:integer' => invoice_id} if invoice_id
  response = super message: {query: children, attributes!: {query: attributes}}
  response = response.to_hash
  Invoice.build response[:list_invoices_response][:create_invoices_return].andand[:invoice]
end

#list_projects(query = {}) ⇒ Array, Project

Returns An array or projects.

Examples:

Clearbooks.list_projects

Parameters:

Returns:



223
224
225
226
227
# File 'lib/clearbooks/library/client.rb', line 223

def list_projects query = {}
  response = super message: {query: query}
  response = response.to_hash
  Project.build response[:list_projects_response][:projects][:project]
end

#update_entity(entity) ⇒ Hash

Operation taken from WSDL schema secure.clearbooks.co.uk/api/wsdl/

Examples:

Clearbooks.create_entity Clearbooks::Entity.new(id: 10,
           company_name: 'Example Inc.',
           contact_name: 'John Doe',
           address1: 'London',
           country: 'UK',
           postcode: '01100',
           email: '[email protected]',
           website: 'http://example.com',
           phone1: '01234 567890',
           supplier: {
           default_account_code: '1001001',
           default_credit_terms: 30,
           default_vat_rate: 0
       })

Parameters:

Returns:

  • (Hash)
    :entity_id

    ID of the updated entity.



162
163
164
165
166
# File 'lib/clearbooks/library/client.rb', line 162

def update_entity entity
  response = super message: {entityId: entity.id}.merge(entity.to_savon)
  response = response.to_hash
  { entity_id: response[:update_entity_response][:update_entity_return].to_i }
end

#void_invoice(ledger, invoice_id) ⇒ Hash

Operation taken from WSDL schema secure.clearbooks.co.uk/api/wsdl/

Examples:

Clearbooks.void_invoice 'purchases', 10

Parameters:

  • ledger (String)

    ‘sales’ or ‘purchases’.

  • invoice_id (Fixnum)

    Invoice id.

Returns:

  • (Hash)
    :@success, :@msg

    Boolean result of the operation and a short message describing an error (if any).



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/clearbooks/library/client.rb', line 105

def void_invoice ledger, invoice_id
  message = {
      invoice: {
          :@type => ledger,
          :@id => invoice_id
      }
  }
  response = super message: message
  response = response.to_hash
  response[:void_invoice_response][:void_success]
end