fastbill 0.0.8 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,7 @@ require "fastbill/customer"
5
5
  require "fastbill/invoice"
6
6
  require "fastbill/invoice_item"
7
7
  require "fastbill/invoice_vat_item"
8
+ require "fastbill/recurring"
8
9
 
9
10
  module Fastbill
10
11
  attr_reader :auth
@@ -24,5 +25,8 @@ module Fastbill
24
25
  def invoice
25
26
  Invoice.new(@auth)
26
27
  end
28
+ def recurring
29
+ Recurring.new(@auth)
30
+ end
27
31
  end
28
32
  end
@@ -0,0 +1,194 @@
1
+ class Recurring
2
+ include HTTParty
3
+ base_uri 'https://portal.fastbill.com'
4
+
5
+ attr_accessor :id, :invoice_type, :customer_id, :customer_costcenter_id, :currency_code, :template_id, :invoice_number, :introtext, :start_date, :frequency, :occurences, :output_type, :email_notify, :delivery_date, :eu_delivery, :vat_items, :invoice_items, :is_new
6
+
7
+ def initialize(auth = nil)
8
+ @auth = auth
9
+ @is_new = true
10
+ end
11
+
12
+ def get(id = nil)
13
+ body = '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>recurring.get</SERVICE>'
14
+ if id
15
+ body = body + "<FILTER><INVOICE_ID>#{id}</INVOICE_ID></FILTER>"
16
+ end
17
+ body = body + "</FBAPI>"
18
+ options = {
19
+ :basic_auth => @auth,
20
+ :headers => {
21
+ "Content-Type" => "application/xml"
22
+ },
23
+ :body => body
24
+ }
25
+ r = self.class.post('/api/0.1/api.php', options)
26
+ body = Crack::XML.parse r.body
27
+ invoices = []
28
+ if body['FBAPI']["RESPONSE"]["INVOICES"]["INVOICE"].class.to_s == 'Hash'
29
+ inv = Recurring.new(@auth)
30
+ inv.hydrate(body['FBAPI']["RESPONSE"]["INVOICES"]["INVOICE"])
31
+ invoices.push inv
32
+ else
33
+ for invoice in body['FBAPI']["RESPONSE"]["INVOICES"]["INVOICE"].each
34
+ inv = Recurring.new(@auth)
35
+ inv.hydrate(invoice)
36
+ invoices.push inv
37
+ end
38
+ end
39
+ invoices
40
+ end
41
+ def save!
42
+ if @is_new
43
+ #create
44
+ options = {
45
+ :basic_auth => @auth,
46
+ :headers => {
47
+ "Content-Type" => "application/xml"
48
+ },
49
+ :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>recurring.create</SERVICE><DATA>' + self.to_xml + '</DATA></FBAPI>'
50
+ }
51
+ r = self.class.post('/api/0.1/api.php', options)
52
+ body = Crack::XML.parse r.body
53
+ if !body['FBAPI']["RESPONSE"]["STATUS"].nil? && body['FBAPI']["RESPONSE"]["STATUS"] == "success"
54
+ unless body['FBAPI']["RESPONSE"]["STATUS"]["INVOICE_ID"].nil?
55
+ @id = body['FBAPI']["RESPONSE"]["STATUS"]["INVOICE_ID"]
56
+ end
57
+ @is_new = false
58
+ self
59
+ else
60
+ false
61
+ end
62
+ else
63
+ #update
64
+ options = {
65
+ :basic_auth => @auth,
66
+ :headers => {
67
+ "Content-Type" => "application/xml"
68
+ },
69
+ :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>recurring.update</SERVICE><DATA>' + self.to_xml + '</DATA></FBAPI>'
70
+ }
71
+ r = self.class.post('/api/0.1/api.php', options)
72
+ body = Crack::XML.parse r.body
73
+ if !body['FBAPI']["RESPONSE"]["STATUS"].nil? && body['FBAPI']["RESPONSE"]["STATUS"] == "success"
74
+ unless body['FBAPI']["RESPONSE"]["STATUS"]["INVOICE_ID"].nil?
75
+ @id = body['FBAPI']["RESPONSE"]["STATUS"]["INVOICE_ID"]
76
+ end
77
+ @is_new = false
78
+ self
79
+ else
80
+ false
81
+ end
82
+ end
83
+ end
84
+ def delete!
85
+ options = {
86
+ :basic_auth => @auth,
87
+ :headers => {
88
+ "Content-Type" => "application/xml"
89
+ },
90
+ :body => '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>recurring.delete</SERVICE><DATA><INVOICE_ID>' + @id + '</INVOICE_ID></DATA></FBAPI>'
91
+ }
92
+ r = self.class.post('/api/0.1/api.php', options)
93
+ body = Crack::XML.parse r.body
94
+ if !body['FBAPI']["RESPONSE"]["STATUS"].nil? && body['FBAPI']["RESPONSE"]["STATUS"] == "success"
95
+ true
96
+ else
97
+ false
98
+ end
99
+ end
100
+ def to_xml
101
+ xml = ""
102
+ unless @id.nil?
103
+ xml = xml + "<INVOICE_ID>#{@id}</INVOICE_ID>"
104
+ end
105
+ unless @customer_id.nil?
106
+ xml = xml + "<CUSTOMER_ID>#{@customer_id}</CUSTOMER_ID>"
107
+ end
108
+ unless @customer_costcenter_id.nil?
109
+ xml = xml + "<CUSTOMER_COSTCENTER_ID>#{@customer_costcenter_id}</CUSTOMER_COSTCENTER_ID>"
110
+ end
111
+ unless @currency_code.nil?
112
+ xml = xml + "<CURRENCY_CODE>#{@currency_code}</CURRENCY_CODE>"
113
+ end
114
+ unless @template_id.nil?
115
+ xml = xml + "<TEMPLATE_ID>#{@template_id}</TEMPLATE_ID>"
116
+ end
117
+ unless @invoice_number.nil?
118
+ xml = xml + "<INVOICE_NUMBER>#{@invoice_number}</INVOICE_NUMBER>"
119
+ end
120
+ unless @introtext.nil?
121
+ xml = xml + "<INTROTEXT>#{@introtext}</INTROTEXT>"
122
+ end
123
+ if @start_date
124
+ xml = xml + "<START_DATE>#{@start_date.strftime("%Y-%m-%d")}</START_DATE>"
125
+ end
126
+ unless @frequency.nil?
127
+ xml = xml + "<FREQUENCY>#{@frequency}</FREQUENCY>"
128
+ end
129
+ unless @occurrences.nil?
130
+ xml = xml + "<OCCURENCES>#{@occurences}</OCCURENCES>"
131
+ end
132
+ unless @output_type.nil?
133
+ xml = xml + "<OUTPUT_TYPE>#{@output_type}</OUTPUT_TYPE>"
134
+ end
135
+ unless @email_notify.nil?
136
+ xml = xml + "<EMAIL_NOTIFY>#{@email_notify}</EMAIL_NOTIFY>"
137
+ end
138
+ unless @delivery_date.nil?
139
+ xml = xml + "<DELIVERY_DATE>#{@delivery_date.strftime("%Y-%m-%d")}</DELIVERY_DATE>"
140
+ end
141
+ unless @eu_delivery.nil?
142
+ eu = @eu_delivery ? 1 : 0
143
+ xml = xml + "<EU_DELIVERY>#{@eu}</EU_DELIVERY>"
144
+ end
145
+ unless @invoice_items.length == 0
146
+ xml = xml + "<ITEMS>"
147
+ for item in @invoice_items.each
148
+ xml = xml + item.to_xml
149
+ end
150
+ xml = xml + "</ITEMS>"
151
+ end
152
+ xml
153
+ end
154
+ def hydrate(body)
155
+ @is_new = false
156
+ @id = body["INVOICE_ID"]
157
+ @invoice_type = body["INVOICE_TYPE"]
158
+ @customer_id = body["CUSTOMER_ID"]
159
+ @customer_costcenter_id = body["CUSTOMER_COSTCENTER_ID"]
160
+ @currency_code = body["CURRENCY_CODE"]
161
+ @template_id = body["TEMPLATE_ID"]
162
+ @occurances = body["OCCURANCES"]
163
+ @frequency = body["FREQUENCY"]
164
+ @start_date = parse_date body["START_DATE"]
165
+ @email_notify = body["EMAIL_NOTIFY"]
166
+ @output_type = body["OUTPUT_TYPE"]
167
+ @introtext = body["INTROTEXT"]
168
+ #@delivery_date = parse_date body["DELIVERY_DATE"]
169
+ @sub_total = body["SUB_TOTAL"]
170
+ @vat_total = body["VAT_TOTAL"]
171
+ #@eu_delivery = body["EU_DELIVERY"] == "1" ? true : false
172
+ @vat_items = []
173
+ @total = body["TOTAL"]
174
+ for vat_item in body["VAT_ITEMS"].each
175
+ @vat_items.push InvoiceVatItem.new vat_item.last
176
+ end
177
+ @invoice_items = []
178
+ for item in body["ITEMS"].each
179
+ begin
180
+ i = InvoiceItem.new(@auth)
181
+ i.hydrate(item.last)
182
+ @invoice_items.push i
183
+ rescue
184
+ end
185
+ end
186
+ end
187
+ def parse_date(date)
188
+ if date != nil && date != "0000-00-00 00:00:00"
189
+ Time.parse date
190
+ else
191
+ false
192
+ end
193
+ end
194
+ end
@@ -1,3 +1,3 @@
1
1
  module Fastbill
2
- VERSION = "0.0.8"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastbill
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-11 00:00:00.000000000Z
12
+ date: 2011-10-12 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70142062719680 !ruby/object:Gem::Requirement
16
+ requirement: &70239449126520 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70142062719680
24
+ version_requirements: *70239449126520
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: httparty
27
- requirement: &70142062864700 !ruby/object:Gem::Requirement
27
+ requirement: &70239449126100 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70142062864700
35
+ version_requirements: *70239449126100
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: crack
38
- requirement: &70142062952820 !ruby/object:Gem::Requirement
38
+ requirement: &70239449125640 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70142062952820
46
+ version_requirements: *70239449125640
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: httparty
49
- requirement: &70142067218200 !ruby/object:Gem::Requirement
49
+ requirement: &70239449125200 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70142067218200
57
+ version_requirements: *70239449125200
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: crack
60
- requirement: &70142067276940 !ruby/object:Gem::Requirement
60
+ requirement: &70239449124780 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70142067276940
68
+ version_requirements: *70239449124780
69
69
  description: a basic ruby wrapper for the methods provided by the fastbill API
70
70
  email:
71
71
  - kai@4ware.net
@@ -85,6 +85,7 @@ files:
85
85
  - lib/fastbill/invoice.rb
86
86
  - lib/fastbill/invoice_item.rb
87
87
  - lib/fastbill/invoice_vat_item.rb
88
+ - lib/fastbill/recurring.rb
88
89
  - lib/fastbill/version.rb
89
90
  homepage: ''
90
91
  licenses: []