Last active
December 20, 2015 13:09
-
-
Save reidab/6136965 to your computer and use it in GitHub Desktop.
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
# Export from FreshBooks for import into Fusion Invoice | |
# | |
# This will produce four CSV files in the import format specified at: | |
# http://docs.fusioninvoice.com/1.2/system-setup.html#importing-data | |
# | |
# To run this, you'll need ruby 1.9+ and the ruby-freshbooks gem installed. | |
# | |
# Customize the contstants below with your FreshBooks information, | |
# then run with `ruby freshbooks_to_fusion_invoices.rb`. | |
# | |
# It will create four CSV files in the same directory, which should then | |
# be placed in Fusion Invoice's uploads/import directory. | |
require 'ruby-freshbooks' | |
require 'csv' | |
FRESHBOOKS_SUBDOMAIN = "your_freshbooks_subdomain" | |
FRESHBOOKS_API_TOKEN = "your_freshbooks_api_token" | |
USER_EMAIL = "fusion_invoices_account_to_own_new_invoices@yourcomapny.com" | |
c = FreshBooks::Client.new("#{FRESHBOOKS_SUBDOMAIN}.freshbooks.com", FRESHBOOKS_API_TOKEN) | |
def fetch_all(type, c) | |
items = [] | |
loop do | |
page = (page || 0) + 1 | |
puts "Fetching #{type}s, page #{page}..." | |
response = c.send(type).list(per_page: 100, page: page)["#{type}s"] | |
items += response[type.to_s] | |
break if response["page"] == response["pages"] | |
end | |
return items | |
end | |
clients = fetch_all(:client, c) | |
invoices = fetch_all(:invoice, c) | |
payments = fetch_all(:payment, c) | |
CSV.open("clients.csv", "wb") do |csv| | |
csv << %w(client_name client_address_1 client_address_2 client_city client_state client_zip client_country client_phone client_fax client_mobile client_email client_web client_active) | |
clients.each do |client| | |
csv << [client["organization"], client["p_street1"], client["p_street2"], client["p_city"], client["p_state"], client["p_code"], client["p_country"], client["work_phone"], client["fax"], client["mobile"], client["email"], "", 1] | |
end | |
end | |
CSV.open("invoice_items.csv", "wb") do |items_csv| | |
items_csv << %w(invoice_number item_tax_rate item_date_added item_name item_description item_quantity item_price) | |
CSV.open("invoices.csv", "wb") do |invoice_csv| | |
invoice_csv << %w(user_email client_name invoice_date_created invoice_date_due invoice_number invoice_terms) | |
invoices.each do |invoice| | |
invoice_csv << [USER_EMAIL, invoice["organization"], invoice["date"], (Date.parse(invoice["date"])+30).to_s, invoice["number"], invoice["terms"]] | |
invoice["lines"]["line"].each do |item| | |
items_csv << [invoice["number"], 0, invoice["date"], item["name"], item["description"], item["quantity"], item["unit_cost"]] | |
end | |
end | |
end | |
end | |
CSV.open("payments.csv", "wb") do |csv| | |
csv << %w(invoice_number payment_method payment_date payment_amount payment_note) | |
payments.each do |payment| | |
invoice = invoices.find{|i| i["invoice_id"] == payment["invoice_id"]} | |
if invoice | |
csv << [invoice["number"], payment["type"], Date.parse(payment["date"]).to_s, payment["amount"], ""] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment