Class: Invoice

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth = nil) ⇒ Invoice

Returns a new instance of Invoice.



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

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

Instance Attribute Details

#currency_codeObject

Returns the value of attribute currency_code.



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

def currency_code
  @currency_code
end

#customer_costcenter_idObject

Returns the value of attribute customer_costcenter_id.



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

def customer_costcenter_id
  @customer_costcenter_id
end

#customer_idObject

Returns the value of attribute customer_id.



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

def customer_id
  @customer_id
end

#delivery_dateObject

Returns the value of attribute delivery_date.



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

def delivery_date
  @delivery_date
end

#due_dateObject

Returns the value of attribute due_date.



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

def due_date
  @due_date
end

#eu_deliveryObject

Returns the value of attribute eu_delivery.



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

def eu_delivery
  @eu_delivery
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#intro_textObject

Returns the value of attribute intro_text.



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

def intro_text
  @intro_text
end

#invoice_dateObject

Returns the value of attribute invoice_date.



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

def invoice_date
  @invoice_date
end

#invoice_itemsObject

Returns the value of attribute invoice_items.



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

def invoice_items
  @invoice_items
end

#invoice_numberObject

Returns the value of attribute invoice_number.



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

def invoice_number
  @invoice_number
end

#invoice_typeObject

Returns the value of attribute invoice_type.



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

def invoice_type
  @invoice_type
end

#is_canceledObject

Returns the value of attribute is_canceled.



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

def is_canceled
  @is_canceled
end

#is_newObject

Returns the value of attribute is_new.



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

def is_new
  @is_new
end

#payed_dateObject

Returns the value of attribute payed_date.



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

def payed_date
  @payed_date
end

#sub_totalObject

Returns the value of attribute sub_total.



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

def sub_total
  @sub_total
end

#template_idObject

Returns the value of attribute template_id.



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

def template_id
  @template_id
end

#totalObject

Returns the value of attribute total.



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

def total
  @total
end

#vat_itemsObject

Returns the value of attribute vat_items.



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

def vat_items
  @vat_items
end

#vat_totalObject

Returns the value of attribute vat_total.



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

def vat_total
  @vat_total
end

Instance Method Details

#completeObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/fastbill/invoice.rb', line 160

def complete
  options = {
    :basic_auth => @auth,
    :headers => {
      "Content-Type" => "application/xml"
    },
    :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>invoice.complete</SERVICE><DATA><INVOICE_ID>' + @id + '</INVOICE_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

#delete!Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/fastbill/invoice.rb', line 302

def delete!
  options = {
    :basic_auth => @auth,
    :headers => {
      "Content-Type" => "application/xml"
    },
    :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>invoice.delete</SERVICE><DATA><INVOICE_ID>' + @id + '</INVOICE_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 = nil, customer_id = nil, year = nil, month = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fastbill/invoice.rb', line 11

def get(id = nil, customer_id = nil, year = nil, month = nil)
  invoices = []
  body = '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>invoice.get</SERVICE><FILTER>'
  if id
    body = body + '<INVOICE_ID>' + id.to_s + '</INVOICE_ID>'
  elsif customer_id
    body = body + '<CUSTOMER_ID>' + customer_id.to_s + '</CUSTOMER_ID>'
  elsif year
    body = body + '<YEAR>' + year.to_s + '</YEAR>'
  elsif month
    body = body + '<MONTH>' + month.to_s + '</MONTH>'
  end
  body = body + '</FILTER></FBAPI>'
  options = {
    :basic_auth => @auth,
    :headers => {
      "Content-Type" => "application/xml"
    },
    :body => body
  }
  r = self.class.post('/api/0.1/api.php', options)
  body = Crack::XML.parse r.body
  if body['FBAPI']["RESPONSE"]["INVOICES"]["INVOICE"].class.to_s == 'Hash'
    inv = Invoice.new(@auth)
    inv.hydrate(body['FBAPI']["RESPONSE"]["INVOICES"]["INVOICE"])
    invoices.push inv
  else
    for invoice in body['FBAPI']["RESPONSE"]["INVOICES"]["INVOICE"].each
      inv = Invoice.new(@auth)
      inv.hydrate(invoice)
      invoices.push inv
    end
  end
  invoices
end

#hydrate(body) ⇒ Object



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
71
72
73
74
75
76
77
78
# File 'lib/fastbill/invoice.rb', line 46

def hydrate(body)
  @is_new = false
  @id = body["INVOICE_ID"]
  @invoice_type = body["INVOICE_TYPE"]
  @customer_id = body["CUSTOMER_ID"]
  @customer_costcenter_id = body["CUSTOMER_COSTCENTER_ID"]
  @currency_code = body["CURRENCY_CODE"]
  @template_id = body["TEMPLATE_ID"]
  @invoice_number = body["INVOICE_NUMBER"]
  @introtext = body["INTROTEXT"]
  @payed_date = parse_date body["PAYED_DATE"]
  @is_canceled = body["IS_CANCELED"] == "1" ? true : false
  @invoice_date = parse_date body["INVOICE_DATE"]
  @due_date = parse_date body["INVOICE_DATE"]
  @delivery_date = parse_date body["DELIVERY_DATE"]
  @sub_total = body["SUB_TOTAL"]
  @vat_total = body["VAT_TOTAL"]
  @eu_delivery = body["EU_DELIVERY"] == "1" ? true : false
  @vat_items = []
  @total = body["TOTAL"]
  for vat_item in body["VAT_ITEMS"].each
    @vat_items.push InvoiceVatItem.new vat_item.last
  end
  @invoice_items = []
  for item in body["ITEMS"].each
    begin
      i =  InvoiceItem.new(@auth)
      i.hydrate(item.last)
      @invoice_items.push i
    rescue
    end
  end
end

#parse_date(date) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/fastbill/invoice.rb', line 79

def parse_date(date)
  if date != nil && date != "0000-00-00 00:00:00"
    Time.parse date
  else
    false
  end
end

#safe!Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/fastbill/invoice.rb', line 259

def safe!
  if @is_new
    #create
    options = {
      :basic_auth => @auth,
      :headers => {
        "Content-Type" => "application/xml"
      },
      :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>invoice.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"]["INVOICE_ID"].nil?
        @id = body['FBAPI']["RESPONSE"]["STATUS"]["INVOICE_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>invoice.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"]["INVOICE_ID"].nil?
        @id = body['FBAPI']["RESPONSE"]["STATUS"]["INVOICE_ID"]
      end
      @is_new = false
      self
    else
      false
    end
  end
end

#send_by_email(from = nil, to = nil, cc = nil, subject = nil, message = nil, confirmation = false) ⇒ Object



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
# File 'lib/fastbill/invoice.rb', line 192

def send_by_email(from = nil, to = nil, cc = nil, subject = nil, message = nil, confirmation = false)
  unless to.nil? && cc.nil?
    xml = "<INVOICE_ID>#{@id}</INVOICE_ID>"
    xml = xml + "<RECIPIENT>"
    unless to.nil?
      xml = xml + "<TO>#{to}</TO>"
    end
    unless cc.nil?
      xml = xml + "<CC>#{cc}</CC>"
    end
    xml = xml + "</RECIPIENT>"
    unless subject.nil?
      xml = xml + "<SUBJECT>#{subject}</SUBJECT>"
    end
    unless message.nil?
      xml = xml + "<MESSAGE>#{message}</MESSAGE>"
    end
    c = confirmation ? 1 : 0
    xml = xml + "<CONFIRMATION>#{c}</CONFIRMATION>"
    options = {
      :basic_auth => @auth,
      :headers => {
        "Content-Type" => "application/xml"
      },
      :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>invoice.sendbyemail</SERVICE><DATA><INVOICE_ID>' + @id + '</INVOICE_ID></DATA>'+ xml +'</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
end

#send_by_ground_mailObject



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/fastbill/invoice.rb', line 227

def send_by_ground_mail
  options = {
    :basic_auth => @auth,
    :headers => {
      "Content-Type" => "application/xml"
    },
    :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>invoice.sendbypost</SERVICE><DATA><INVOICE_ID>' + @id + '</INVOICE_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

#set_paid!Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/fastbill/invoice.rb', line 243

def set_paid!
  options = {
    :basic_auth => @auth,
    :headers => {
      "Content-Type" => "application/xml"
    },
    :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>invoice.setpaid</SERVICE><DATA><INVOICE_ID>' + @id + '</INVOICE_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

#sign!Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/fastbill/invoice.rb', line 176

def sign!
  options = {
    :basic_auth => @auth,
    :headers => {
      "Content-Type" => "application/xml"
    },
    :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>invoice.sign</SERVICE><DATA><INVOICE_ID>' + @id + '</INVOICE_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

#to_xmlObject



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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/fastbill/invoice.rb', line 86

def to_xml
  xml = "<INVOICE>"
  unless @id.nil?
    xml = xml + "<INVOICE_ID>#{@id}</INVOICE_ID>"
  end
  unless @invoice_type.nil?
    xml = xml + "<INVOICE_TYPE>#{@invoice_type}</INVOICE_TYPE>"
  end
  unless @customer_id.nil?
    xml = xml + "<CUSTOMER_ID>#{@customer_id}</CUSTOMER_ID>"
  end
  unless @customer_costcenter_id.nil?
    xml = xml + "<CUSTOMER_COSTCENTER_ID>#{@customer_costcenter_id}</CUSTOMER_COSTCENTER_ID>"
  end
  unless @currency_code.nil?
    xml = xml + "<CURRENCY_CODE>#{@currency_code}</CURRENCY_CODE>"
  end
  unless @template_id.nil?
    xml = xml + "<TEMPLATE_ID>#{@template_id}</TEMPLATE_ID>"
  end
  unless @invoice_number.nil?
    xml = xml + "<INVOICE_NUMBER>#{@invoice_number}</INVOICE_NUMBER>"
  end
  unless @introtext.nil?
    xml = xml + "<INTROTEXT>#{@introtext}</INTROTEXT>"
  end
  unless @eu_delivery.nil?
    eu = @eu_delivery ? 1 : 0
    xml = xml + "<EU_DELIVERY>#{eu}</EU_DELIVERY>"
  end
  if @payed_date
    xml = xml + "<PAYED_DATE>#{@payed_date.strftime("%Y-%m-%d")}</PAYED_DATE>"
  end
  unless @is_canceled.nil?
    c = @is_canceled ? 1 : 0
    xml = xml + "<IS_CANCELED>#{c}</IS_CANCELED>"
  end
  if @invoice_date
    xml = xml + "<IVOICE_DATE>#{@invoice_date.strftime("%Y-%m-%d")}</INVOICE_DATE>"
  end
  if @due_date
    xml = xml + "<DUE_DATE>#{@due_date.strftime("%Y-%m-%d")}</DUE_DATE>"
  end
  if @delivery_date
    xml = xml + "<DELIVERY_DATE>#{@delivery_date.strftime("%Y-%m-%d")}</DELIVERY_DATE>"
  end
  if @payed_date
    xml = xml + "<PAYED_DATE>#{@payed_date.strftime("%Y-%m-%d")}</PAYED_DATE>"
  end
  unless @sub_total.nil?
    xml = xml + "<SUB_TOTAL>#{@sub_total}</SUB_TOTAL>"
  end
  unless @vat_total.nil?
    xml = xml + "<VAT_TOTAL>#{@vat_total}</VAT_TOTAL>"
  end
  unless @total.nil?
    xml = xml + "<TOTAL>#{@total}</TOTAL>"
  end
  unless @vat_items.length == 0
    xml = xml + "<VAT_ITEMS>"
    for vat_item in @vat_items.each
      xml = xml + vat_item.to_xml
    end
    xml = xml + "</VAT_ITEMS>"
  end
  unless @invoice_items.length == 0
    xml = xml + "<ITEMS>"
    for item in @invoice_items.each
      xml = xml + item.to_xml
    end
    xml = xml + "</ITEMS>"
  end
  xml + "</INVOICE>"
end