xero_gateway 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/CHANGELOG.textile +51 -0
  2. data/LICENSE +14 -0
  3. data/README.textile +289 -0
  4. data/Rakefile +14 -0
  5. data/examples/oauth.rb +25 -0
  6. data/init.rb +1 -0
  7. data/lib/xero_gateway/account.rb +78 -0
  8. data/lib/xero_gateway/accounts_list.rb +77 -0
  9. data/lib/xero_gateway/address.rb +97 -0
  10. data/lib/xero_gateway/ca-certificates.crt +2560 -0
  11. data/lib/xero_gateway/contact.rb +206 -0
  12. data/lib/xero_gateway/currency.rb +56 -0
  13. data/lib/xero_gateway/dates.rb +25 -0
  14. data/lib/xero_gateway/error.rb +18 -0
  15. data/lib/xero_gateway/exceptions.rb +41 -0
  16. data/lib/xero_gateway/gateway.rb +363 -0
  17. data/lib/xero_gateway/http.rb +128 -0
  18. data/lib/xero_gateway/http_encoding_helper.rb +49 -0
  19. data/lib/xero_gateway/invoice.rb +278 -0
  20. data/lib/xero_gateway/line_item.rb +123 -0
  21. data/lib/xero_gateway/money.rb +16 -0
  22. data/lib/xero_gateway/oauth.rb +56 -0
  23. data/lib/xero_gateway/organisation.rb +61 -0
  24. data/lib/xero_gateway/payment.rb +40 -0
  25. data/lib/xero_gateway/phone.rb +77 -0
  26. data/lib/xero_gateway/private_app.rb +17 -0
  27. data/lib/xero_gateway/response.rb +37 -0
  28. data/lib/xero_gateway/tax_rate.rb +63 -0
  29. data/lib/xero_gateway/tracking_category.rb +62 -0
  30. data/lib/xero_gateway.rb +33 -0
  31. data/test/integration/accounts_list_test.rb +109 -0
  32. data/test/integration/create_contact_test.rb +66 -0
  33. data/test/integration/create_invoice_test.rb +49 -0
  34. data/test/integration/get_accounts_test.rb +23 -0
  35. data/test/integration/get_contact_test.rb +28 -0
  36. data/test/integration/get_contacts_test.rb +40 -0
  37. data/test/integration/get_currencies_test.rb +25 -0
  38. data/test/integration/get_invoice_test.rb +48 -0
  39. data/test/integration/get_invoices_test.rb +90 -0
  40. data/test/integration/get_organisation_test.rb +24 -0
  41. data/test/integration/get_tax_rates_test.rb +25 -0
  42. data/test/integration/get_tracking_categories_test.rb +26 -0
  43. data/test/integration/update_contact_test.rb +31 -0
  44. data/test/stub_responses/accounts.xml +1 -0
  45. data/test/stub_responses/api_exception.xml +153 -0
  46. data/test/stub_responses/contact.xml +1 -0
  47. data/test/stub_responses/contacts.xml +2189 -0
  48. data/test/stub_responses/create_invoice.xml +64 -0
  49. data/test/stub_responses/currencies.xml +16 -0
  50. data/test/stub_responses/invalid_api_key_error.xml +1 -0
  51. data/test/stub_responses/invalid_consumer_key +1 -0
  52. data/test/stub_responses/invalid_request_token +1 -0
  53. data/test/stub_responses/invoice.xml +1 -0
  54. data/test/stub_responses/invoice_not_found_error.xml +1 -0
  55. data/test/stub_responses/invoices.xml +1 -0
  56. data/test/stub_responses/organisation.xml +14 -0
  57. data/test/stub_responses/tax_rates.xml +52 -0
  58. data/test/stub_responses/token_expired +1 -0
  59. data/test/stub_responses/tracking_categories.xml +1 -0
  60. data/test/stub_responses/unknown_error.xml +1 -0
  61. data/test/test_helper.rb +81 -0
  62. data/test/unit/account_test.rb +34 -0
  63. data/test/unit/contact_test.rb +97 -0
  64. data/test/unit/currency_test.rb +31 -0
  65. data/test/unit/gateway_test.rb +79 -0
  66. data/test/unit/invoice_test.rb +302 -0
  67. data/test/unit/oauth_test.rb +110 -0
  68. data/test/unit/organisation_test.rb +34 -0
  69. data/test/unit/tax_rate_test.rb +38 -0
  70. data/test/unit/tracking_category_test.rb +30 -0
  71. data/test/xsd/README +2 -0
  72. data/test/xsd/create_contact.xsd +61 -0
  73. data/test/xsd/create_invoice.xsd +107 -0
  74. data/xero_gateway.gemspec +87 -0
  75. metadata +172 -0
data/CHANGELOG.textile ADDED
@@ -0,0 +1,51 @@
1
+ h2. Upcoming
2
+
3
+ * Xero Api version 2.0
4
+ * As get_invoices doesn't download the attached line items, add code to automatically request line items from Xero the first time XeroGateway::Invoice#line_items is accessed. *
5
+ * Populate XeroGateway::Payment class with results from invoice/s including new invoice fields for payment status. *
6
+
7
+ h2. 1.0.5, released 25/09/2009
8
+
9
+ * This will be the final release before switching to the Xero API version 2.0
10
+ * Allow multiple tracking options per line item
11
+
12
+ h2. 1.0.4, released 13/08/2009
13
+
14
+ * Auto-assign Contact#contact_id and Invoice#invoice_id on creation of either of these records.
15
+ * Add Xero::Gateway#build_contact and Xero::Gateway#build_invoice factory methods to create Contact/Invoice objects associated with the gateway.
16
+ * Add Contact#save method to create/update a contact record based on if it has a contact_id or contact_number.
17
+ * Add Invoice#save method to create (insert only currently supported by Xero API) the invoice.
18
+ * Add #valid? method to each model class to validate it's contents with the minimum required by the API (without calling the API).
19
+ * Add useful API constants throughout the library.
20
+ * Allow Invoice and LineItem to auto-calculate line_amount, sub_total, total_tax and total fields. Existing setters for these fields remain (for backwards compatability) but will be deprecated in the future.
21
+ * Create XeroGateway::AccountList class to make working with the accounts response more powerful and faster (as it caches the last result).
22
+ * Add :updated_after parameter to XeroGateway::Gateway#get_contacts
23
+ * Add XeroGateway::Gateway#update_contacts
24
+ * Add XeroGateway::Gateway#create_invoices
25
+
26
+ h2. 1.0.3, released 09/12/2008
27
+
28
+ * Major refactorings to DRY out gateway.rb
29
+ * Removed all messages classes in favour of invoice.to_xml and Invoice.from_xml(element)
30
+ * Added unit tests for the handing of errors from Xero
31
+
32
+ h2. 1.0.2, released 04/12/2008
33
+
34
+ * Added implementation of GET /api.xro/1.0/tracking
35
+
36
+
37
+ h2. 1.0.1, released 02/12/2008
38
+
39
+ * Added implementation of GET /api.xro/1.0/accounts
40
+ * Replaced Invoice.id, Contact.id etc with Invoice.invoice_id, Contact.contact_id to avoid Object.id errors
41
+
42
+
43
+ h2. 1.0.0, released 01/12/2008
44
+
45
+ * Initial release, including:
46
+ * PUT /api.xro/1.0/contact
47
+ * GET /api.xro/1.0/contact
48
+ * GET /api.xro/1.0/contacts
49
+ * PUT /api.xro/1.0/invoice
50
+ * GET /api.xro/1.0/invoice
51
+ * GET /api.xro/1.0/invoices
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ Copyright (c) 2008 Tim Connor <tlconnor@gmail.com>
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any
4
+ purpose with or without fee is hereby granted, provided that the above
5
+ copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
+
data/README.textile ADDED
@@ -0,0 +1,289 @@
1
+ h1. Xero API wrapper
2
+
3
+ h2. Introduction
4
+
5
+ This library is designed to help ruby / rails based applications communicate with the publicly available API for Xero. If you are unfamiliar with the API, you should first read the documentation, located here "http://blog.xero.com/developer/":http://blog.xero.com/developer/
6
+
7
+ h2. Usage
8
+
9
+ <pre><code> require 'xero_gateway'
10
+ gateway = XeroGateway::Gateway.new(YOUR_OAUTH_CONSUMER_KEY, YOUR_OAUTH_CONSUMER_SECRET)
11
+ </code></pre>
12
+
13
+ h2. Authenticating with OAuth
14
+
15
+ OAuth is built into this library in a very similar manner to the Twitter gem by John Nunemaker ("http://github.com/jnunemaker/twitter":http://github.com/jnunemaker/twitter). So if you've used that before this will all seem familiar.
16
+
17
+ h3. Consumer Key & Secret
18
+
19
+ First off, you'll need to get a Consumer Key/Secret pair for your application from Xero.
20
+ Head to "http://api.xero.com":http://api.xero.com, log in and then click My Applications > Add Application.
21
+
22
+ If you want to create a private application (that accesses your own Xero account rather than your users), you'll need to generate an RSA keypair and an X509 certificate. This can be done with OpenSSL as below:
23
+ <pre><code> openssl genrsa –out privatekey.pem 1024
24
+ openssl req –newkey rsa:1024 –x509 –key privatekey.pem –out publickey.cer –days 365
25
+ openssl pkcs12 –export –out public_privatekey.pfx –inkey privatekey.pem –in publickey.cer
26
+ </code></pre>
27
+
28
+ On the right-hand-side of your application's page there's a box titled "OAuth Credentials". Use the Key and Secret from this box in order to set up a new Gateway instance.
29
+
30
+ (If you're unsure about the Callback URL, specify nothing - it will become clear a bit later)
31
+
32
+ h3. Xero Gateway Initialization
33
+
34
+ <pre><code> require 'xero_gateway'
35
+ gateway = XeroGateway::Gateway.new(YOUR_OAUTH_CONSUMER_KEY, YOUR_OAUTH_CONSUMER_SECRET)
36
+ </code></pre>
37
+
38
+ or for private applications
39
+
40
+ <pre><code> require 'xero_gateway'
41
+ gateway = XeroGateway::PrivateApp.new(YOUR_OAUTH_CONSUMER_KEY, YOUR_OAUTH_CONSUMER_SECRET, PATH_TO_YOUR_PRIVATE_KEY)
42
+ </code></pre>
43
+
44
+
45
+ h3. Request Token
46
+
47
+ You'll then need to get a Request Token from Xero.
48
+
49
+ <pre><code>
50
+ request_token = gateway.request_token
51
+ </code></pre>
52
+
53
+ You should keep this around - you'll need it to exchange for an Access Token later. (If you're using Rails, this means storing it in the session or something similar)
54
+
55
+ Next, you need to redirect your user to the authorisation url for this request token. In Rails, that looks something like this:
56
+
57
+ <pre><code>
58
+ redirect_to request_token.authorize_url
59
+ </code></pre>
60
+
61
+ (Note that Xero doesn't support the OAuth Callback parameter here)
62
+
63
+ h3. Retrieving an Access Token
64
+
65
+ If you've specified a Callback URL when setting up your application, your user will be redirected to that URL with an OAuth Verifier as a GET parameter. You can then exchange your Request Token for an Access Token like this (assuming Rails, once again):
66
+
67
+ <pre><code>
68
+ gateway.authorize_from_request(request_token.token, request_token.secret, :oauth_verifier => params[:oauth_verifier])
69
+ </code></pre>
70
+
71
+ (If you haven't specified a Callback URL, the user will be presented with a numeric verifier which they must copy+paste into your application; see examples/oauth.rb for an example)
72
+
73
+ Now you can access Xero API methods:
74
+
75
+ <pre><code>
76
+ gateway.get_contacts
77
+ </code></pre>
78
+
79
+ h3. Storing Access Tokens
80
+
81
+ You can also store the Access Token/Secret pair so that you can access the API without user intervention. Currently, these access tokens are only valid for 30 minutes, and will raise a XeroGateway::OAuth::TokenExpired exception if you attempt to access the API beyond the token's expiry time.
82
+
83
+ <pre><code>
84
+ access_token, access_secret = gateway.access_token
85
+ </code></pre>
86
+
87
+ h3. Examples
88
+
89
+ Open examples/oauth.rb and change CONSUMER_KEY and CONSUMER_SECRET to the values for a Test OAuth Application in order to see an example of OAuth at work.
90
+
91
+ If you're working with Rails, a controller similar to this might come in handy:
92
+
93
+ <pre><code>
94
+
95
+ class XeroSessionsController < ApplicationController
96
+
97
+ before_filter :get_xero_gateway
98
+
99
+ def new
100
+ session[:request_token] = @xero_gateway.request_token.token
101
+ session[:request_secret] = @xero_gateway.request_token.secret
102
+
103
+ redirect_to @xero_gateway.request_token.authorize_url
104
+ end
105
+
106
+ def create
107
+ @xero_gateway.authorize_from_request(session[:request_token], session[:request_secret],
108
+ :oauth_verifier => params[:oauth_verifier])
109
+
110
+ session[:xero_auth] = { :access_token => @xero_gateway.access_token.token,
111
+ :access_secret => @xero_gateway.access_token.secret }
112
+
113
+ session.data.delete(:request_token); session.data.delete(:request_secret)
114
+ end
115
+
116
+ def destroy
117
+ session.data.delete(:xero_auth)
118
+ end
119
+
120
+ private
121
+
122
+ def get_xero_gateway
123
+ @xero_gateway = XeroGateway::Gateway.new(YOUR_OAUTH_CONSUMER_KEY, YOUR_OAUTH_CONSUMER_SECRET)
124
+ end
125
+
126
+ end
127
+
128
+ </code></pre>
129
+
130
+ Note that I'm just storing the Access Token + Secret in the session here - you could equally store them in the database if you felt like refreshing them every 30 minutes ;)
131
+
132
+ h2. Implemented interface methods
133
+
134
+ h3. GET /api.xro/2.0/contact (get_contact_by_id)
135
+
136
+ Gets a contact record for a specific Xero organisation
137
+ <pre><code> result = gateway.get_contact_by_id(contact_id)
138
+ contact = result.contact if result.success?</code></pre>
139
+
140
+ h3. GET /api.xro/2.0/contact (get_contact_by_number)
141
+
142
+ Gets a contact record for a specific Xero organisation
143
+ <pre><code> gateway.get_contact_by_number(contact_number)</code></pre>
144
+
145
+
146
+ h3. GET /api.xro/2.0/contacts (get_contacts)
147
+
148
+ Gets all contact records for a particular Xero customer.
149
+ <pre><code> gateway.get_contacts(:type => :all, :sort => :name, :direction => :desc)
150
+ gateway.get_contacts(:type => :all, :updated_after => 1.month.ago) # modified since 1 month ago</code></pre>
151
+
152
+
153
+
154
+ h3. PUT /api.xro/2.0/contact
155
+
156
+ Saves a contact record for a particular Xero customer.
157
+ <pre><code> contact = gateway.build_contact
158
+ contact.name = "The contacts name"
159
+ contact.email = "whoever@something.com"
160
+ contact.phone.number = "555 123 4567"
161
+ contact.address.line_1 = "LINE 1 OF THE ADDRESS"
162
+ contact.address.line_2 = "LINE 2 OF THE ADDRESS"
163
+ contact.address.city = "WELLINGTON"
164
+ contact.address.region = "WELLINGTON"
165
+ contact.address.country = "NEW ZEALAND"
166
+ contact.address.post_code = "6021"
167
+
168
+ contact.save</code></pre>
169
+
170
+
171
+ h3. POST /api.xro/2.0/contact
172
+
173
+ Updates an existing contact record.
174
+ <pre><code> contact_retrieved_from_xero.email = "something_new@something.com"
175
+ contact_retrieved_from_xero.save</code></pre>
176
+
177
+
178
+ h3. POST /api.xro/2.0/contacts
179
+
180
+ Creates a list of contacts or updates them if they have a matching contact_id, contact_number or name.
181
+ This method uses only a single API request to create/update multiple contacts.
182
+ <pre><code> contacts = [XeroGateway::Contact.new(:name => 'Joe Bloggs'), XeroGateway::Contact.new(:name => 'Jane Doe')]
183
+ result = gateway.update_contacts(contacts)</code></pre>
184
+
185
+
186
+ h3. GET /api.xro/2.0/invoice (get_invoice_by_id)
187
+
188
+ Gets an invoice record for a specific Xero organisation
189
+ <pre><code> gateway.get_invoice_by_id(invoice_id)</code></pre>
190
+
191
+
192
+ h3. GET /api.xro/2.0/invoice (get_invoice_by_number)
193
+
194
+ Gets an invoice record for a specific Xero organisation
195
+ <pre><code> gateway.get_invoice_by_number(invoice_number)</code></pre>
196
+
197
+
198
+
199
+ h3. GET /api.xro/2.0/invoices (get_invoices)
200
+
201
+ Gets all invoice records for a particular Xero customer.
202
+ <pre><code> gateway.get_invoices
203
+ gateway.get_invoices(1.month.ago) # modified since 1 month ago</code></pre>
204
+
205
+
206
+
207
+ h3. PUT /api.xro/2.0/invoice
208
+
209
+ Inserts an invoice for a specific organization in Xero (Currently only adding new invoices is allowed).
210
+
211
+ Invoice and line item totals are calculated automatically.
212
+ <pre><code> invoice = gateway.build_invoice({
213
+ :invoice_type => "ACCREC",
214
+ :due_date => 1.month.from_now,
215
+ :invoice_number => "YOUR INVOICE NUMBER",
216
+ :reference => "YOUR REFERENCE (NOT NECESSARILY UNIQUE!)",
217
+ :tax_inclusive => true,
218
+ :includes_tax => false
219
+ })
220
+ invoice.contact.name = "THE NAME OF THE CONTACT"
221
+ invoice.contact.phone.number = "12345"
222
+ invoice.contact.address.line_1 = "LINE 1 OF THE ADDRESS"
223
+ invoice.add_line_item({
224
+ :description => "THE DESCRIPTION OF THE LINE ITEM",
225
+ :unit_amount => 1000,
226
+ :tax_amount => 125,
227
+ :tracking_category => "THE TRACKING CATEGORY FOR THE LINE ITEM",
228
+ :tracking_option => "THE TRACKING OPTION FOR THE LINE ITEM"
229
+ })
230
+
231
+ invoice.create</code></pre>
232
+
233
+
234
+ h3. PUT /api.xro/2.0/invoices
235
+
236
+ Inserts multiple invoices for a specific organization in Xero (currently only adding new invoices is allowed).
237
+ This method uses only a single API request to create/update multiple contacts.
238
+ <pre><code> invoices = [XeroGateway::Invoice.new(...), XeroGateway::Invoice.new(...)]
239
+ result = gateway.create_invoices(invoices)</code></pre>
240
+
241
+ h3. GET /api.xro/2.0/accounts
242
+
243
+ Gets all accounts for a specific organization in Xero.
244
+ <pre><code> gateway.get_accounts</code></pre>
245
+
246
+ For more advanced (and cached) access to the accounts list, use the following.
247
+ <pre><code> accounts_list = gateway.get_accounts_list</code></pre>
248
+
249
+ Finds account with code of '200'
250
+ <pre><code> sales_account = accounts_list.find_by_code(200)</code></pre>
251
+
252
+ Finds all EXPENSE accounts. For a list of valid account types see <code>XeroGateway::Account::TYPE</code>
253
+ <pre><code> all_expense_accounts = accounts_list.find_all_by_type('EXPENSE')</code></pre>
254
+
255
+ Finds all accounts with tax_type == 'OUTPUT'. For a list of valid tax types see <code>XeroGateway::Account::TAX_TYPE</code>
256
+ <pre><code> all_output_tax_accounts = accounts_list.find_all_by_tax_type('OUTPUT')</code></pre>
257
+
258
+
259
+ h3. GET /api.xro/2.0/tracking
260
+
261
+ Gets all tracking categories and their options for a specific organization in Xero.
262
+ <pre><code> gateway.get_tracking_categories</code></pre>
263
+
264
+ h3. GET /api.xero/2.0/Organisation
265
+
266
+ Retrieves organisation details for the authorised application.
267
+ <pre><code> gateway.get_organisation.organisation</code></pre>
268
+
269
+
270
+ h3. GET /api.xero/2.0/Currencies
271
+
272
+ Retrieves currencies in use for the authorised application.
273
+ <pre><code> gateway.get_currencies.currencies</code></pre>
274
+
275
+
276
+ h3. GET /api.xero/2.0/TaxRates
277
+
278
+ Retrieves Tax Rates in use for the authorised application.
279
+ <pre><code> gateway.get_tax_rates.tax_rates</code></pre>
280
+
281
+ h2. Logging
282
+
283
+ You can specify a logger to use (so you can track down those tricky exceptions) by using:
284
+
285
+ <pre>
286
+ gateway.logger = ActiveSupport::BufferedLogger.new("log_file_name.log")
287
+ </pre>
288
+
289
+ It doesn't have to be a buffered logger - anything that responds to "info" will do just fine.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the xero gateway.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
data/examples/oauth.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ require 'pp'
3
+
4
+ require File.dirname(__FILE__) + '/../lib/xero_gateway.rb'
5
+
6
+ XERO_CONSUMER_KEY = "YOUR-OAUTH-CONSUMER-KEY"
7
+ XERO_CONSUMER_SECRET = "YOUR-OAUTH-CONSUMER-SECRET"
8
+
9
+ gateway = XeroGateway::Gateway.new(XERO_CONSUMER_KEY, XERO_CONSUMER_SECRET)
10
+
11
+ # authorize in browser
12
+ %x(open #{gateway.request_token.authorize_url})
13
+
14
+ puts "Enter the verification code from Xero?"
15
+ oauth_verifier = gets.chomp
16
+
17
+ gateway.authorize_from_request(gateway.request_token.token, gateway.request_token.secret, :oauth_verifier => oauth_verifier)
18
+
19
+ puts "Your access token: #{gateway.access_token}"
20
+ puts "(Good for 30 minutes)"
21
+
22
+ # Example API Call
23
+ pp gateway.get_contacts.contacts
24
+
25
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'xero_gateway'
@@ -0,0 +1,78 @@
1
+ module XeroGateway
2
+ class Account
3
+
4
+ TYPE = {
5
+ 'CURRENT' => '',
6
+ 'FIXED' => '',
7
+ 'PREPAYMENT' => '',
8
+ 'EQUITY' => '',
9
+ 'DEPRECIATN' => '',
10
+ 'DIRECTCOSTS' => '',
11
+ 'EXPENSE' => '',
12
+ 'OVERHEADS' => '',
13
+ 'CURRLIAB' => '',
14
+ 'LIABILITY' => '',
15
+ 'TERMLIAB' => '',
16
+ 'OTHERINCOME' => '',
17
+ 'REVENUE' => '',
18
+ 'SALES' => ''
19
+ } unless defined?(TYPE)
20
+
21
+ TAX_TYPE = {
22
+ 'NONE' => 'No GST',
23
+ 'EXEMPTINPUT' => 'VAT on expenses exempt from VAT (UK only)',
24
+ 'INPUT' => 'GST on expenses',
25
+ 'SRINPUT' => 'VAT on expenses',
26
+ 'ZERORATEDINPUT' => 'Expense purchased from overseas (UK only)',
27
+ 'RRINPUT' => 'Reduced rate VAT on expenses (UK Only)',
28
+ 'EXEMPTOUTPUT' => 'VAT on sales exempt from VAT (UK only)',
29
+ 'OUTPUT' => 'OUTPUT',
30
+ 'SROUTPUT' => 'SROUTPUT',
31
+ 'ZERORATEDOUTPUT' => 'Sales made from overseas (UK only)',
32
+ 'RROUTPUT' => 'Reduced rate VAT on sales (UK Only)',
33
+ 'ZERORATED' => 'Zero-rated supplies/sales from overseas (NZ Only)'
34
+ } unless defined?(TAX_TYPE)
35
+
36
+ attr_accessor :code, :name, :type, :tax_type, :description
37
+
38
+ def initialize(params = {})
39
+ params.each do |k,v|
40
+ self.send("#{k}=", v)
41
+ end
42
+ end
43
+
44
+ def ==(other)
45
+ [:code, :name, :type, :tax_type, :description].each do |field|
46
+ return false if send(field) != other.send(field)
47
+ end
48
+ return true
49
+ end
50
+
51
+ def to_xml
52
+ b = Builder::XmlMarkup.new
53
+
54
+ b.Account {
55
+ b.Code self.code
56
+ b.Name self.name
57
+ b.Type self.type
58
+ b.TaxType self.tax_type
59
+ b.Description self.description
60
+ }
61
+ end
62
+
63
+ def self.from_xml(account_element)
64
+ account = Account.new
65
+ account_element.children.each do |element|
66
+ case(element.name)
67
+ when "Code" then account.code = element.text
68
+ when "Name" then account.name = element.text
69
+ when "Type" then account.type = element.text
70
+ when "TaxType" then account.tax_type = element.text
71
+ when "Description" then account.description = element.text
72
+ end
73
+ end
74
+ account
75
+ end
76
+
77
+ end
78
+ end