Class: Customer

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/fastbill/customer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth = nil) ⇒ Customer

Returns a new instance of Customer.



7
8
9
10
# File 'lib/fastbill/customer.rb', line 7

def initialize(auth = nil)
  @auth = auth
  @is_new = true
end

Instance Attribute Details

#account_receiveableObject

Returns the value of attribute account_receiveable.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def 
  @account_receiveable
end

#addressObject

Returns the value of attribute address.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def address
  @address
end

#address_2Object

Returns the value of attribute address_2.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def address_2
  @address_2
end

#bank_account_numberObject

Returns the value of attribute bank_account_number.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def 
  @bank_account_number
end

#bank_account_ownerObject

Returns the value of attribute bank_account_owner.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def 
  @bank_account_owner
end

#bank_codeObject

Returns the value of attribute bank_code.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def bank_code
  @bank_code
end

#bank_nameObject

Returns the value of attribute bank_name.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def bank_name
  @bank_name
end

#cityObject

Returns the value of attribute city.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def city
  @city
end

#commentObject

Returns the value of attribute comment.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def comment
  @comment
end

#country_codeObject

Returns the value of attribute country_code.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def country_code
  @country_code
end

#created_atObject

Returns the value of attribute created_at.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def created_at
  @created_at
end

#currency_codeObject

Returns the value of attribute currency_code.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def currency_code
  @currency_code
end

#customer_typeObject

Returns the value of attribute customer_type.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def customer_type
  @customer_type
end

#days_for_paymentObject

Returns the value of attribute days_for_payment.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def days_for_payment
  @days_for_payment
end

#emailObject

Returns the value of attribute email.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def email
  @email
end

#faxObject

Returns the value of attribute fax.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def fax
  @fax
end

#first_nameObject

Returns the value of attribute first_name.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def first_name
  @first_name
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def id
  @id
end

#last_nameObject

Returns the value of attribute last_name.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def last_name
  @last_name
end

#mobileObject

Returns the value of attribute mobile.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def mobile
  @mobile
end

#numberObject

Returns the value of attribute number.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def number
  @number
end

#organizationObject

Returns the value of attribute organization.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def organization
  @organization
end

#payment_typeObject

Returns the value of attribute payment_type.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def payment_type
  @payment_type
end

#phoneObject

Returns the value of attribute phone.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def phone
  @phone
end

#phone_2Object

Returns the value of attribute phone_2.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def phone_2
  @phone_2
end

#positionObject

Returns the value of attribute position.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def position
  @position
end

#saltuationObject

Returns the value of attribute saltuation.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def saltuation
  @saltuation
end

#show_payment_noticeObject

Returns the value of attribute show_payment_notice.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def show_payment_notice
  @show_payment_notice
end

#topObject

Returns the value of attribute top.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def top
  @top
end

#vat_idObject

Returns the value of attribute vat_id.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def vat_id
  @vat_id
end

#zipcodeObject

Returns the value of attribute zipcode.



5
6
7
# File 'lib/fastbill/customer.rb', line 5

def zipcode
  @zipcode
end

Instance Method Details

#customersObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fastbill/customer.rb', line 12

def customers
  options = {
    :basic_auth => @auth,
    :headers => {
      "Content-Type" => "application/xml"
    },
    :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>customer.get</SERVICE></FBAPI>'
  }
  r = self.class.post('/api/0.1/api.php', options)
  unless r.body.nil?
    body = Crack::XML.parse r.body
    unless body['FBAPI']["RESPONSE"]["CUSTOMERS"].nil?
      customers = []
      for customer in body['FBAPI']["RESPONSE"]["CUSTOMERS"]["CUSTOMER"].each
        c = Customer.new(@auth)
        c.hydrate(customer)
        customers.push c
      end
      customers
    end
  end
end

#delete!Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fastbill/customer.rb', line 94

def delete!
  options = {
    :basic_auth => @auth,
    :headers => {
      "Content-Type" => "application/xml"
    },
    :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>customer.delete</SERVICE><DATA><CUSTOMER_ID>' + @id + '</CUSTOMER_ID></DATA></FBAPI>'
  }
  r = self.class.post('/api/0.1/api.php', options)
  body = Crack::XML.parse r.body
  if !body['FBAPI']["RESPONSE"]["STATUS"].nil? && body['FBAPI']["RESPONSE"]["STATUS"] == "success"
    true
  else
    false
  end
  
end

#get(id) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fastbill/customer.rb', line 35

def get(id)
  options = {
    :basic_auth => @auth,
    :headers => {
      "Content-Type" => "application/xml"
    },
    :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>customer.get</SERVICE><FILTER><CUSTOMER_ID>' + id.to_s + '</CUSTOMER_ID></FILTER></FBAPI>'
  }
  r = self.class.post('/api/0.1/api.php', options)
  body = Crack::XML.parse r.body
  if !body['FBAPI']["RESPONSE"]["CUSTOMERS"].nil?
    hydrate(body['FBAPI']["RESPONSE"]["CUSTOMERS"]["CUSTOMER"])
    self
  else
    false
  end
end

#hydrate(body) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/fastbill/customer.rb', line 112

def hydrate(body)
  @is_new = false
  @id = body["CUSTOMER_ID"]
  @number = body["CUSTOMER_NUMBER"]
  @days_for_payment = body["DAYS_FOR_PAYMENT"]
  @created_at = Time.parse body["CREATED"]
  @payment_type = body["PAYMENT_TYPE"]
  @bank_name = body["BANK_NAME"]
  @bank_account_number = body["BANK_ACCOUNT_NUMBER"]
  @bank_code = body["BANK_CODE"]
  @bank_account_owner = body["BANK_ACCOUNT_OWNER"]
  @show_payment_notice = body["SHOW_PAYMENT_NOTICE"]
  @account_receiveable = body["ACCOUNT_RECEIVEABLE"]
  @customer_type = body["CUSTOMER_TYPE"]
  @top = body["TOP"] == "1" ? true : false
  @organization = body["ORGANIZATION"]
  @position = body["POSITION"]
  @saltuation = body["SALUATION"]
  @first_name = body["FIRST_NAME"]
  @last_name = body["LAST_NAME"]
  @address = body["ADDRESS"]
  @address_2 = body["ADDRESS_2"]
  @zipcode = body["ZIPCODE"]
  @city = body["CITY"]
  @country_code = body["COUNTRY_CODE"]
  @phone = body["PHONE"]
  @phone_2 = body["PHONE_2"]
  @fax = body["FAX"]
  @mobile = body["MOBILE"]
  @email = body["EMAIL"]
  @vat_id = body["VAT_ID"]
  @currency_code = body["CURRENCY_CODE"]
  @comment = body["COMMENT"]    
end

#saveObject



52
53
54
55
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
# File 'lib/fastbill/customer.rb', line 52

def save
  if @is_new
    #create
    options = {
      :basic_auth => @auth,
      :headers => {
        "Content-Type" => "application/xml"
      },
      :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>customer.create</SERVICE><DATA>' + self.to_xml + '</DATA></FBAPI>'
    }
    r = self.class.post('/api/0.1/api.php', options)
    body = Crack::XML.parse r.body
    if !body['FBAPI']["RESPONSE"]["STATUS"].nil? && body['FBAPI']["RESPONSE"]["STATUS"] == "success"
      unless body['FBAPI']["RESPONSE"]["STATUS"]["CUSTOMER_ID"].nil?
        @id = body['FBAPI']["RESPONSE"]["STATUS"]["CUSTOMER_ID"]
      end
      @is_new = false
      self
    else
      false
    end
  else
    #update
    options = {
      :basic_auth => @auth,
      :headers => {
        "Content-Type" => "application/xml"
      },
      :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>customer.update</SERVICE><DATA>' + self.to_xml + '</DATA></FBAPI>'
    }
    r = self.class.post('/api/0.1/api.php', options)
    body = Crack::XML.parse r.body
    if !body['FBAPI']["RESPONSE"]["STATUS"].nil? && body['FBAPI']["RESPONSE"]["STATUS"] == "success"
      unless body['FBAPI']["RESPONSE"]["STATUS"]["CUSTOMER_ID"].nil?
        @id = body['FBAPI']["RESPONSE"]["STATUS"]["CUSTOMER_ID"]
      end
      self
    else
      false
    end
  end
end

#to_xmlObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/fastbill/customer.rb', line 146

def to_xml
  xml = ""
  unless @id.nil?
    xml = xml + "<CUSTOMER_ID>#{@id}</CUSTOMER_ID>"
  end
  unless @number.nil?
    xml = xml + "<CUSTOMER_NUMBER>#{@number}</CUSTOMER_NUMBER>"
  end
  unless @days_for_payment.nil?
    xml = xml + "<DAYS_FOR_PAYMENT>#{@days_for_payment}</DAYS_FOR_PAYMENT>"
  end
  unless @payment_type.nil?
    xml = xml + "<PAYMENT_TYPE>#{@payment_type}</PAYMENT_TYPE>"
  end
  unless @bank_name.nil?
    xml = xml + "<BANK_NAME>#{@bank_name}</BANK_NAME>"
  end
  unless @bank_account_number.nil?
    xml = xml + "<BANK_ACCOUNT_NUMBER>#{@bank_account_number}</BANK_ACCOUNT_NUMBER>"
  end
  unless @bank_code.nil?
    xml = xml + "<BANK_CODE>#{@bank_code}</BANK_CODE>"
  end
  unless @bank_account_owner.nil?
    xml = xml + "<BANK_ACCOUNT_OWNER>#{@bank_account_owner}</BANK_ACCOUNT_OWNER>"
  end
  unless @show_payment_notice.nil?
    xml = xml + "<SHOW_PAYMENT_NOTICE>#{@show_payment_notice}</SHOW_PAYMENT_NOTICE>"
  end
  unless @account_receivable.nil?
    xml = xml + "<ACCOUNT_RECEIVABLE>#{@account_receivable}</ACCOUNT_RECEIVABLE>"
  end
  unless @customer_type.nil?
    xml = xml + "<CUSTOMER_TYPE>#{@customer_type}</CUSTOMER_TYPE>"
  end
  unless @top.nil?
    t = @top ? 1 : 0
    xml = xml + "<TOP>#{t}</TOP>"
  end
  unless @organization.nil?
    xml = xml + "<ORGANIZATION>#{@organization}</ORGANIZATION>"
  end
  unless @position.nil?
    xml = xml + "<POSITION>#{@position}</POSITION>"
  end
  unless @saltuation.nil?
    xml = xml + "<SALUATION>#{@saltuation}</SALUATION>"
  end
  unless @first_name.nil?
    xml = xml + "<FIRST_NAME>#{@first_name}</FIRST_NAME>"
  end
  unless @last_name.nil?
    xml = xml + "<LAST_NAME>#{@last_name}</LAST_NAME>"
  end
  unless @address.nil?
    xml = xml + "<ADDRESS>#{@address}</ADDRESS>"
  end
  unless @address_2.nil?
    xml = xml + "<ADDRESS_2>#{@address_2}</ADDRESS_2>"
  end
  unless @zipcode.nil?
    xml = xml + "<ZIPCODE>#{@zipcode}</ZIPCODE>"
  end
  unless @city.nil?
    xml = xml + "<CITY>#{@city}</CITY>"
  end
  unless @country_code.nil?
    xml = xml + "<COUNTRY_CODE>#{@country_code}</COUNTRY_CODE>"
  end
  unless @phone.nil?
    xml = xml + "<PHONE>#{@phone}</PHONE>"
  end
  unless @phone_2.nil?
    xml = xml + "<PHONE_2>#{@phone_2}</PHONE_2>"
  end
  unless @fax.nil?
    xml = xml + "<FAX>#{@fax}</FAX>"
  end
  unless @mobile.nil?
    xml = xml + "<MOBILE>#{@mobile}</MOBILE>"
  end
  unless @email.nil?
    xml = xml + "<EMAIL>#{@email}</EMAIL>"
  end
  unless @vat_id.nil?
    xml = xml + "<VAT_ID>#{@vat_id}</VAT_ID>"
  end
  unless @currency_code.nil?
    xml = xml + "<CURRENCY_CODE>#{@currency_code}</CURRENCY_CODE>"
  end
  unless @comment.nil?
    xml = xml + "<COMMENT>#{@comment}</COMMENT>"
  end
  xml
end