Class: CrunchAPI
- Inherits:
-
Object
- Object
- CrunchAPI
- Defined in:
- lib/crunchaccounting-api.rb
Instance Attribute Summary collapse
-
#oauth_token ⇒ Object
readonly
Returns the value of attribute oauth_token.
-
#oauth_token_secret ⇒ Object
readonly
Returns the value of attribute oauth_token_secret.
Instance Method Summary collapse
- #account_by_name(name) ⇒ Object
- #accounts(type: nil) ⇒ Object
- #add_client_payment(client_id:, date:, payment_method: nil, bank_account_id:, amount:, invoice_id:) ⇒ Object
- #add_expense(supplier_id:, date:, payment_date:, payment_method: nil, bank_account_id:, amount:, expense_type:, description:, director_id: nil, invoice: nil) ⇒ Object
- #authenticated? ⇒ Boolean
- #client_payments ⇒ Object
- #clients ⇒ Object
- #delete(uri) ⇒ Object
- #delete_supplier(supplier_id) ⇒ Object
- #expense_types ⇒ Object
- #expenses ⇒ Object
- #file_mimetype(filename) ⇒ Object
- #find_client(name) ⇒ Object
- #find_client_payment(client_id:, date:, payment_method: nil, bank_account_id:, amount:, invoice_id:) ⇒ Object
- #find_draft_invoice(client_id, date) ⇒ Object
- #find_expense(supplier_id:, date:, payment_date:, payment_method: nil, bank_account_id:, amount:, expense_type:, ignore_ids: []) ⇒ Object
- #find_invoice(client_id, date) ⇒ Object
- #find_outstanding_client_invoice(client_id, amount) ⇒ Object
- #get(uri) ⇒ Object
- #get_auth_url ⇒ Object
- #get_consumer(endpoint) ⇒ Object
- #get_next_client_ref_for_client(client) ⇒ Object
- #initialise_consumer ⇒ Object
-
#initialize(params = {}) ⇒ CrunchAPI
constructor
A new instance of CrunchAPI.
- #invoices ⇒ Object
- #issue_invoice(invoice) ⇒ Object
- #post(uri, params = {}) ⇒ Object
- #put(uri, params = {}) ⇒ Object
- #raise_invoice(client_id:, date:, client_ref: nil, description:, rate:, quantity:, add_vat: true) ⇒ Object
- #subject_to_vat?(expense_type) ⇒ Boolean
- #supplier_by_name(name) ⇒ Object
- #suppliers ⇒ Object
- #verify_token(oauth_verifier) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ CrunchAPI
Returns a new instance of CrunchAPI.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/crunchaccounting-api.rb', line 13 def initialize(params={}) @vat_rate = VAT_RATE_DEFAULT params.each do |key, value| instance_variable_set("@" + key.to_s, value) end if @oauth_token and @oauth_token_secret initialise_consumer end end |
Instance Attribute Details
#oauth_token ⇒ Object (readonly)
Returns the value of attribute oauth_token.
10 11 12 |
# File 'lib/crunchaccounting-api.rb', line 10 def oauth_token @oauth_token end |
#oauth_token_secret ⇒ Object (readonly)
Returns the value of attribute oauth_token_secret.
11 12 13 |
# File 'lib/crunchaccounting-api.rb', line 11 def oauth_token_secret @oauth_token_secret end |
Instance Method Details
#account_by_name(name) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/crunchaccounting-api.rb', line 129 def account_by_name(name) if @accounts return @accounts[name] end accounts account_by_name name end |
#accounts(type: nil) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/crunchaccounting-api.rb', line 111 def accounts(type: nil) if type return get("/rest/v2/accounts/#{type}") else @accounts = {} resp = get("/rest/v2/accounts") resp["bankAccounts"].each do |account| if !@accounts[account["account"]] @accounts[account["account"]] = account end end return resp end end |
#add_client_payment(client_id:, date:, payment_method: nil, bank_account_id:, amount:, invoice_id:) ⇒ Object
142 143 144 |
# File 'lib/crunchaccounting-api.rb', line 142 def add_client_payment(params={}) post "/rest/v2/client_payments", params end |
#add_expense(supplier_id:, date:, payment_date:, payment_method: nil, bank_account_id:, amount:, expense_type:, description:, director_id: nil, invoice: nil) ⇒ Object
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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/crunchaccounting-api.rb', line 211 def add_expense(supplier_id:, date:, payment_date:, payment_method: nil, bank_account_id:, amount:, expense_type:, description:, director_id:nil, invoice:nil) !payment_method and payment_method = "EFT" if subject_to_vat?(expense_type) gross_amount = amount vat_amount = ((amount / 100) * @vat_rate).round(2) net_amount = (amount - vat_amount).round(2) else gross_amount = amount vat_amount = 0 net_amount = amount end expense = { "amount" => amount, "expenseDetails" => { "supplier" => { "supplierId" => supplier_id }, "postingDate" => date }, "paymentDetails" => { "payment" => [{ "paymentDate" => payment_date, "paymentMethod" => payment_method, "bankAccount" => { "accountId" => bank_account_id }, "amount" => amount }] }, "expenseLineItems" => { "count": 1, "lineItemGrossTotal": gross_amount, "expenseLineItems" => [ { "expenseType" => expense_type, "benefitingDirector": director_id, "lineItemDescription" => description, "lineItemAmount" => { "currencyCode": "GBP", "netAmount" => net_amount, "grossAmount" => gross_amount, "vatAmount" => vat_amount } } ] } } if invoice mimetype = file_mimetype(invoice) filename = invoice.split("/").last expense["receipts"] = { "count" => 1, "receipt" => [ { "fileName" => filename, "contentType" => mimetype, "fileData": Base64.encode64(File.read(invoice)) } ] } end post "/rest/v2/expenses", expense end |
#authenticated? ⇒ Boolean
36 37 38 |
# File 'lib/crunchaccounting-api.rb', line 36 def authenticated? @access_token ? true : false end |
#client_payments ⇒ Object
138 139 140 |
# File 'lib/crunchaccounting-api.rb', line 138 def client_payments get "/rest/v2/client_payments" end |
#clients ⇒ Object
312 313 314 |
# File 'lib/crunchaccounting-api.rb', line 312 def clients get "/rest/v2/clients" end |
#delete(uri) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/crunchaccounting-api.rb', line 103 def delete(uri) !@consumer and @consumer = get_consumer(@api_endpoint) resp = @access_token.delete(uri, { "Accept" => "application/json" } ) JSON.parse(resp.body) end |
#delete_supplier(supplier_id) ⇒ Object
186 187 188 |
# File 'lib/crunchaccounting-api.rb', line 186 def delete_supplier(supplier_id) delete "/rest/v2/suppliers/#{supplier_id}" end |
#expense_types ⇒ Object
146 147 148 |
# File 'lib/crunchaccounting-api.rb', line 146 def expense_types get "/rest/v2/expense_types" end |
#expenses ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/crunchaccounting-api.rb', line 150 def expenses resp = get "/rest/v2/expenses" @expenses = [] resp["expense"].each do |expense| @expenses.push expense end @expenses end |
#file_mimetype(filename) ⇒ Object
280 281 282 283 |
# File 'lib/crunchaccounting-api.rb', line 280 def file_mimetype(filename) esc = Shellwords.escape(filename) `/usr/bin/file -bi #{esc}`.chomp.gsub(/;.*\z/, '') end |
#find_client(name) ⇒ Object
316 317 318 319 320 321 322 323 324 |
# File 'lib/crunchaccounting-api.rb', line 316 def find_client(name) clients["client"].each do |client| if client["name"] == name return client end end false end |
#find_client_payment(client_id:, date:, payment_method: nil, bank_account_id:, amount:, invoice_id:) ⇒ Object
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/crunchaccounting-api.rb', line 378 def find_client_payment(client_id:, date:, payment_method: nil, bank_account_id:, amount:, invoice_id:) !payment_method and payment_method = "EFT" client_payments.each do |client_payment| if client_payment["paymentDate"] == date and client_payment["paymentMethod"] == payment_method and client_payment["bankAccount"]["accountId"] == bank_account_id and client_payment["amount"] == amount and client_payment["client"]["clientId"] == client_id and client_payment["salesInvoices"]["salesInvoice"][0]["salesInvoiceId"] == invoice_id return client_payment end end false end |
#find_draft_invoice(client_id, date) ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/crunchaccounting-api.rb', line 349 def find_draft_invoice(client_id, date) invoices["salesInvoice"].each do |invoice| if invoice["salesInvoiceDetails"]["client"]["clientId"] == client_id and invoice["salesInvoiceDetails"]["issuedDate"] == date and invoice["salesInvoiceDetails"]["state"] == "DRAFT" return invoice end end false end |
#find_expense(supplier_id:, date:, payment_date:, payment_method: nil, bank_account_id:, amount:, expense_type:, ignore_ids: []) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/crunchaccounting-api.rb', line 285 def find_expense(supplier_id:, date:, payment_date:, payment_method: nil, bank_account_id:, amount:, expense_type:, ignore_ids:[]) !payment_method and payment_method = "EFT" if !@expenses expenses end @expenses.each do |expense| if ignore_ids.include?(expense["expenseId"]) next end if expense["expenseDetails"]["supplier"]["supplierId"] == supplier_id and expense["expenseDetails"]["postingDate"] == date and expense["paymentDetails"]["payment"][0]["paymentDate"] == payment_date and expense["paymentDetails"]["payment"][0]["paymentMethod"] == payment_method and expense["paymentDetails"]["payment"][0]["bankAccount"]["accountId"] == bank_account_id and expense["paymentDetails"]["payment"][0]["amount"] == amount and expense["expenseLineItems"]["expenseLineItems"][0]["expenseType"] == expense_type return expense end end false end |
#find_invoice(client_id, date) ⇒ Object
362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/crunchaccounting-api.rb', line 362 def find_invoice(client_id, date) invoices["salesInvoice"].each do |invoice| if invoice["salesInvoiceDetails"]["client"]["clientId"] == client_id and invoice["salesInvoiceDetails"]["issuedDate"] == date return invoice end end false end |
#find_outstanding_client_invoice(client_id, amount) ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/crunchaccounting-api.rb', line 330 def find_outstanding_client_invoice(client_id, amount) invoices["salesInvoice"].each do |invoice| total = 0 invoice["salesInvoiceLineItems"]["salesInvoiceLineItem"].each do |item| total += item["lineItemAmount"]["grossAmount"] end if invoice["salesInvoiceDetails"]["client"]["clientId"] == client_id and total == amount and invoice["salesInvoiceDetails"]["state"] != "SETTLED" return invoice end end false end |
#get(uri) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/crunchaccounting-api.rb', line 81 def get(uri) !@consumer and @consumer = get_consumer(@api_endpoint) resp = @access_token.get(uri, { "Accept" => "application/json" } ) JSON.parse(resp.body) end |
#get_auth_url ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/crunchaccounting-api.rb', line 56 def get_auth_url @consumer = get_consumer(@auth_endpoint) request_token = @consumer.get_request_token(:oauth_callback => "") @request_token = request_token.token @request_secret = request_token.secret return request_token.(:oauth_callback => "") end |
#get_consumer(endpoint) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/crunchaccounting-api.rb', line 40 def get_consumer(endpoint) consumer = OAuth::Consumer.new( @consumer_key, @consumer_secret, :scheme => :header, :site => endpoint, :request_token_path => "/crunch-core/oauth/request_token", :authorize_path => "/crunch-core/login/oauth-login.seam", :access_token_path => "/crunch-core/oauth/access_token" ) @debug and consumer.http.set_debug_output($stdout) consumer end |
#get_next_client_ref_for_client(client) ⇒ Object
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/crunchaccounting-api.rb', line 423 def get_next_client_ref_for_client(client) abbr = "" for i in 0...client.length if client["name"][i].match(/[A-Z]/) abbr += client["name"][i] end end used = [] invoices["salesInvoice"].each do |invoice| if invoice["salesInvoiceDetails"]["client"]["clientId"] == client["clientId"] used.push invoice["salesInvoiceDetails"]["clientReference"] end end n = 1 ref = "#{abbr}#{n.to_s.rjust(3,'0')}" while used.include? ref n += 1 ref = "#{abbr}#{n.to_s.rjust(3,'0')}" end ref end |
#initialise_consumer ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/crunchaccounting-api.rb', line 25 def initialise_consumer @consumer = get_consumer(@api_endpoint) @access_token = OAuth::AccessToken.from_hash( @consumer, { oauth_token: @oauth_token, oauth_token_secret: @oauth_token_secret } ) end |
#invoices ⇒ Object
326 327 328 |
# File 'lib/crunchaccounting-api.rb', line 326 def invoices get "/rest/v2/sales_invoices" end |
#issue_invoice(invoice) ⇒ Object
374 375 376 |
# File 'lib/crunchaccounting-api.rb', line 374 def issue_invoice(invoice) put "/rest/v2/sales_invoices/#{invoice["salesInvoiceId"]}/issue" end |
#post(uri, params = {}) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/crunchaccounting-api.rb', line 89 def post(uri, params={}) !@consumer and @consumer = get_consumer(@api_endpoint) resp = @access_token.post(uri, params.to_json, { "Content-Type" => "application/json", "Accept" => "application/json" } ) JSON.parse(resp.body) end |
#put(uri, params = {}) ⇒ Object
97 98 99 100 101 |
# File 'lib/crunchaccounting-api.rb', line 97 def put(uri, params={}) !@consumer and @consumer = get_consumer(@api_endpoint) resp = @access_token.put(uri, params.to_json, { "Content-Type" => "application/json", "Accept" => "application/json" } ) end |
#raise_invoice(client_id:, date:, client_ref: nil, description:, rate:, quantity:, add_vat: true) ⇒ Object
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/crunchaccounting-api.rb', line 451 def raise_invoice(client_id:,date:,client_ref:nil,description:,rate:,quantity:,add_vat:true) amount = rate * quantity if add_vat vat = ((amount / 100) * @vat_rate).round(2) vat_type = "STANDARD" else vat = 0 vat_type = "OUTSIDE_SCOPE" end client = get "/rest/v2/clients/#{client_id}" if !client_ref client_ref = get_next_client_ref_for_client(client) end invoice = { "currency" => "GBP", "salesInvoiceLineItems" => { "salesInvoiceLineItem" => [ { "lineItemDescription" => description, "quantity" => quantity, "rate" => rate, "lineItemAmount" => { "netAmount" => amount, "grossAmount" => amount + vat, "vatAmount" => vat, "vatRate" => @vat_rate }, "vatType" => vat_type }, ], "count" => 1 }, "salesInvoiceDetails" => { "client" => { "clientId" => client_id, }, "clientReference" => client_ref, "issuedDate" => date, "paymentTermsDays" => client["paymentTermsDays"], } } post "/rest/v2/sales_invoices", invoice end |
#subject_to_vat?(expense_type) ⇒ Boolean
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/crunchaccounting-api.rb', line 190 def subject_to_vat?(expense_type) if [ "GENERAL_INSURANCE", "MILEAGE_ALLOWANCE", "MEDICAL_INSURANCE_CONTRIBUTIONS", "BANK_CHARGES", ].include? expense_type return false end if [ "ACCOUNTANCY", "CHILDCARE_VOUCHER_ADMIN_FEES", "WEB_HOSTING_CLOUD_SERVICES" ].include? expense_type return true end raise "Don't know if #{expense_type} is subject to VAT" end |
#supplier_by_name(name) ⇒ Object
176 177 178 179 180 181 182 183 184 |
# File 'lib/crunchaccounting-api.rb', line 176 def supplier_by_name(name) if @suppliers return @suppliers[name] end suppliers supplier_by_name(name) end |
#suppliers ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/crunchaccounting-api.rb', line 162 def suppliers resp = get "/rest/v2/suppliers" @suppliers = {} resp["supplier"].each do |supplier| if !@suppliers[supplier["name"]] @suppliers[supplier["name"]] = supplier end end @suppliers end |
#verify_token(oauth_verifier) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/crunchaccounting-api.rb', line 67 def verify_token(oauth_verifier) hash = { oauth_token: @request_token, oauth_token_secret: @request_secret } request_token = OAuth::RequestToken.from_hash(@consumer, hash) @access_token = request_token.get_access_token(:oauth_verifier => oauth_verifier) @oauth_token = @access_token.params[:oauth_token] @oauth_token_secret = @access_token.params[:oauth_token_secret] initialise_consumer true end |