Last active
August 21, 2020 21:20
-
-
Save dav/57da9348199d1935ad3a906cd006a4ae to your computer and use it in GitHub Desktop.
A test script using the QuickBooks Ruby gem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'quickbooks-ruby' | |
require 'launchy' | |
require 'yaml' | |
require 'text-table' | |
require 'pp' | |
require 'byebug' | |
Quickbooks.sandbox_mode = true | |
CREDENTIALS_FILE = 'credentials.yml' | |
client_id = '' | |
client_secret = '' | |
# this should match at https://developer.intuit.com/app/developer/appdetail/ | |
redirect_uri = 'https://developer.intuit.com/v2/OAuth2Playground/RedirectUrl' | |
redirect_uri = 'http://localhost:8085/oauth2redirect' | |
oauth_params = { | |
site: "https://appcenter.intuit.com/connect/oauth2", | |
authorize_url: "https://appcenter.intuit.com/connect/oauth2", | |
token_url: "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer" | |
} | |
oauth2_client = OAuth2::Client.new(client_id, client_secret, oauth_params) | |
if File.exists?(CREDENTIALS_FILE) | |
credentials = YAML.load(File.read(CREDENTIALS_FILE)) | |
access_token_from_credentials = credentials[:access_token] | |
@refresh_token = credentials[:refresh_token] | |
@realm_id = credentials[:realm_id] | |
end | |
puts "[#{access_token_from_credentials}]" | |
if access_token_from_credentials.blank? | |
url = oauth2_client.auth_code.authorize_url(redirect_uri: redirect_uri, response_type: "code", state: SecureRandom.hex(12), scope: "com.intuit.quickbooks.accounting") | |
puts "Loading AUTH URL #{url}" | |
Launchy.open(url) | |
puts | |
puts 'Copy and paste the URL the browser was redirected to:' | |
redirected_url = $stdin.gets.chomp | |
if /code=([^&]+).*realmId=(.+)/ =~ redirected_url | |
code = Regexp.last_match 1 | |
@realm_id = Regexp.last_match 2 | |
if resp = oauth2_client.auth_code.get_token(code, redirect_uri: redirect_uri) | |
File.open(CREDENTIALS_FILE, 'w') do |f| | |
f.puts(YAML.dump({access_token: resp.token, refresh_token: resp.refresh_token, realm_id: @realm_id})) | |
end | |
@access_token = OAuth2::AccessToken.new(oauth2_client, resp.token, refresh_token: resp.refresh_token) | |
end | |
else | |
warn 'Cannot continue without access token and realm id' | |
end | |
else | |
@access_token = OAuth2::AccessToken.new(oauth2_client, access_token_from_credentials, refresh_token: @refresh_token) | |
end | |
puts "Access token: #{@access_token}" | |
puts " Realm ID: #{@realm_id}" | |
def create_service(klass) | |
service = klass.new | |
service.company_id = @realm_id | |
service.access_token = @access_token | |
service | |
end | |
@customer_service = create_service Quickbooks::Service::Customer | |
@account_service = create_service Quickbooks::Service::Account | |
@invoice_service = create_service Quickbooks::Service::Invoice | |
@item_service = create_service Quickbooks::Service::Item | |
@reports_service = create_service Quickbooks::Service::Reports | |
def dump_customers | |
customers = @customer_service.query() # Called without args you get the first page of results | |
pp customers | |
end | |
def dump_accounts | |
accounts = @account_service.query() # Called without args you get the first page of results | |
accounts.each do |account| | |
puts "-- #{account.id} #{account.account_type} #{account.classification} #{account.name}" | |
end | |
end | |
def dump_invoices | |
items = @invoice_service.query() # Called without args you get the first page of results | |
items.each do |item| | |
puts "-- #{item.doc_number} $#{item.balance.to_f} #{item.bill_email.address} " | |
end | |
end | |
def dump_items | |
items = @item_service.query() # Called without args you get the first page of results | |
items.each do |item| | |
puts "-- #{item.type}\t#{item.name}\t#{item.description}" | |
end | |
end | |
def dump_lines | |
lines = @line_service.query() # Called without args you get the first page of results | |
lines.each do |line| | |
byebug | |
puts "-- #{item.type}\t#{item.name}\t#{item.description}" | |
end | |
end | |
def to_currency(value) | |
"$#{value.to_f}" | |
end | |
begin | |
options = { | |
date_macro: URI.encode_www_form_component('This Fiscal Year-to-date'), | |
# account_name*, create_by, create_date, cust_msg, due_date, doc_num*, inv_date, is_ap_paid, is_cleared, is_no_post*, last_mod_by, | |
# memo*, name*, other_account*, pmt_mthd, printed, sales_cust1, sales_cust2, sales_cust3, term_name, tracking_num, tx_date*, txn_type*, term_name | |
columns: 'tx_date,txn_type,doc_num,is_no_post,name,dept_name,memo,account_name,other_account,subt_nat_home_amount', | |
# minorVersion: 53 | |
} | |
transactions_report = @reports_service.query('TransactionList', nil, options) | |
# transactions_report = @reports_service.query('TransactionList') | |
all_rows = transactions_report.all_rows | |
rows = all_rows | |
output_table = Text::Table.new | |
output_table.head = %w[Date Type DocNum ? Name Memo Account OtherAccount Balance] | |
rows.each do |row| | |
row[2] = row[2].to_i | |
row[8] = to_currency(row[8]) | |
output_table.rows << row | |
end | |
puts output_table | |
rescue Exception => e | |
puts e | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment