Class: TFSO::Company

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/tfso/company.rb

Constant Summary collapse

URL =
'https://api.24sevenoffice.com/CRM/Company/V001/CompanyService.asmx?wsdl'

Instance Method Summary collapse

Methods included from Helpers

#ensure_authenticated, #intialize_savon_client, #savon_client, #session_id, #session_id=

Constructor Details

#initialize(auth) ⇒ Company

Returns a new instance of Company.



8
9
10
11
12
# File 'lib/tfso/company.rb', line 8

def initialize(auth)
  ensure_authenticated(auth)
  self.session_id = auth.session_id
  intialize_savon_client
end

Instance Method Details

#create(company_info) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tfso/company.rb', line 38

def create(company_info)
  response = savon_client.call(:save_companies, message: {companies: [{Company: company_info}]}, cookies: @cookies)
  result = response.body[:save_companies_response][:save_companies_result]
  if result
    if result[:company].class == Hash
      if result[:company][:api_exception]
        raise result[:company][:api_exception]
      else
        result[:company]
      end
    else
      result[:company]
    end
  else
    false
  end
end

#find(search_params) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tfso/company.rb', line 14

def find(search_params)
  response = savon_client.call(:get_companies, message: {searchParams: search_params, returnProperties: {string: ['Id', 'OrganizationNumber', 'NickName', 'Country', 'Addresses', 'EmailAddresses', 'PhoneNumbers', 'InvoiceLanguage', 'TypeGroup', 'DistributionMethod', 'Currency']}}, cookies: @cookies)
  result = response.body[:get_companies_response][:get_companies_result]
  if result
    if result[:company].class == Hash
      [result[:company]]
    else
      result[:company]
    end
  else
    []
  end
end

#find_by_id(id) ⇒ Object



28
29
30
31
32
# File 'lib/tfso/company.rb', line 28

def find_by_id(id)
  if company = find(CompanyId: id)
    company.first
  end
end

#find_by_name(name) ⇒ Object



34
35
36
# File 'lib/tfso/company.rb', line 34

def find_by_name(name)
  find(CompanyName: name)
end

#transform_attributes(company) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/tfso/company.rb', line 56

def transform_attributes(company)
  company.compact!
  company[:EmailAddresses] ||= []
  company[:PhoneNumbers] ||= []
  company[:Addresses] ||= []
  if company[:billing_id]
    company[:id] = company.delete(:billing_id)
  else
    company[:Type] = 'Business'
  end
  if company[:billing_name]
    company[:nickname] = company.delete(:name)
    company[:name] = company.delete(:billing_name)
  end
  if company[:billing_email]
    company[:EmailAddresses] << {:Invoice => {
        :Value => company.delete(:billing_email)
      }
    }
    company.delete(:billing_email)
  end
  if company[:email]
    company[:EmailAddresses] << {:Work => {
        :Value => company.delete(:email)
      }
    }
    company.delete(:email)
  end
  if company[:phone_number]
    company[:PhoneNumbers] << {:Work => {
        :Value => company.delete(:phone_number)
      }
    }
    company.delete(:phone_number)
  end
  if company[:mobile_phone_number]
    company[:PhoneNumbers] << {:Mobile => {
        :Value => company.delete(:mobile_phone_number)
      }
    }
    company.delete(:mobile_phone_number)
  end
  if company[:billing_address]
    address = {
      :Name => company.delete(:billing_name) || company[:name],
      :Street => company.delete(:billing_street),
      :PostalCode => company.delete(:billing_postal_code),
      :PostalArea => company.delete(:billing_city),
      :State => company.delete(:billing_state),
      :Country => company.delete(:billing_country_code)
    }
    company[:Addresses] << {:Invoice => address}
    company[:Addresses] << {:Delivery => address.merge(:Name => company[:name])}

    company.delete(:billing_address)
  end
  if company[:tfso]
    company[:tfso].keys.each{|k| company[k] = company[:tfso].delete(k)}
    company.delete(:tfso)
  end

  mappings = {:name => :Name, :nickname => :NickName, :gov_no => :OrganizationNumber, :country_code => :Country}

  company.keys.each {|k| company[ mappings[k] ] = company.delete(k) if mappings[k]}
  company
end