Class: Quickeebooks::Windows::Service::Customer

Inherits:
ServiceBase
  • Object
show all
Defined in:
lib/quickeebooks/windows/service/customer.rb

Constant Summary

Constants inherited from ServiceBase

ServiceBase::XML_NS

Instance Attribute Summary

Attributes inherited from ServiceBase

#base_uri, #last_response_body, #last_response_xml, #oauth, #realm_id

Instance Method Summary collapse

Methods inherited from ServiceBase

#access_token=, #guid, #initialize, #url_for_base, #url_for_resource

Constructor Details

This class inherits a constructor from Quickeebooks::Windows::Service::ServiceBase

Instance Method Details

#create(customer) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/quickeebooks/windows/service/customer.rb', line 21

def create(customer)
  raise InvalidModelException unless customer.valid_for_create?
  
  # XML is a wrapped 'object' where the type is specified as an attribute
  #    <Object xsi:type="Invoice">
  xml_node = customer.to_xml(:name => 'Object')
  xml_node.set_attribute('xsi:type', 'Customer')
  xml = <<-XML
  <Add xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" RequestId="#{guid}" xmlns="http://www.intuit.com/sb/cdm/v2">
  <ExternalRealmId>#{self.realm_id}</ExternalRealmId>
  #{xml_node}
  </Add>
  XML
  perform_write(Quickeebooks::Windows::Model::Customer, xml)
end

#fetch_by_id(id, idDomain = 'QB', options = {}) ⇒ Object



16
17
18
19
# File 'lib/quickeebooks/windows/service/customer.rb', line 16

def fetch_by_id(id, idDomain = 'QB', options = {})
  url = "#{url_for_resource(Quickeebooks::Windows::Model::Customer::REST_RESOURCE)}/#{id}"
  fetch_object(Quickeebooks::Windows::Model::Customer, url, {:idDomain => idDomain})
end

#list(filters = [], page = 1, per_page = 20, sort = nil, options = {}) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/quickeebooks/windows/service/customer.rb', line 8

def list(filters = [], page = 1, per_page = 20, sort = nil, options = {})
  custom_field_query = '<?xml version="1.0" encoding="utf-8"?>'
  custom_field_query += '<CustomerQuery xmlns="http://www.intuit.com/sb/cdm/v2">'
  custom_field_query += "<StartPage>#{page}</StartPage><ChunkSize>#{per_page}</ChunkSize>"
  custom_field_query += '</CustomerQuery>'
  fetch_collection(Quickeebooks::Windows::Model::Customer, custom_field_query.strip, filters, page, per_page, sort, options)
end

#update(customer) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/quickeebooks/windows/service/customer.rb', line 37

def update(customer)
  # XML is a wrapped 'object' where the type is specified as an attribute
  #    <Object xsi:type="Invoice">
  
  # Intuit requires that some fields are unset / do not exist.
  customer. = nil
  customer.external_key = nil

  # unset Id fields in addresses, phones, email
  if customer.addresses
    customer.addresses.each {|address| address.id = nil }
  end
  if customer.email
    customer.email.id = nil
  end
  
  if customer.phones
    customer.phones.each {|phone| phone.id = nil }
  end
  
  if customer.web_site
    customer.web_site.id = nil
  end
  
  xml_node = customer.to_xml(:name => 'Object')
  xml_node.set_attribute('xsi:type', 'Customer')
  xml = <<-XML
  <Mod xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" RequestId="#{guid}" xmlns="http://www.intuit.com/sb/cdm/v2">
  <ExternalRealmId>#{self.realm_id}</ExternalRealmId>
  #{xml_node}
  </Mod>
  XML
  perform_write(Quickeebooks::Windows::Model::Customer, xml)
end