Created
December 16, 2015 11:02
-
-
Save TomK32/2dcb66bc238cc11ba475 to your computer and use it in GitHub Desktop.
Convert from GnuCash Sqlite format to ledger/hledger format
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
#!/bin/env ruby | |
# (C) 2015 Thomas R. Koll, <info@ananasblau.com> | |
# Licensed under WTFPL | |
# In GnuCash save your file as sqlite3 and pass the filename as argument to this script | |
# The script only requires active_record to be installed | |
require 'active_record' | |
ActiveRecord::Base.establish_connection( | |
:adapter => "sqlite3", | |
:database => ARGV[0] | |
) | |
class Account < ActiveRecord::Base | |
self.primary_key = 'guid' | |
has_many :splits | |
belongs_to :parent, class_name: 'Account', foreign_key: 'parent_guid' | |
end | |
class Transaction < ActiveRecord::Base | |
self.primary_key = 'guid' | |
has_many :splits, foreign_key: 'tx_guid' | |
end | |
class Split < ActiveRecord::Base | |
self.primary_key = 'guid' | |
belongs_to :account, foreign_key: 'account_guid' | |
end | |
accounts = {} | |
Account.all.each do |account| | |
next if account.parent.nil? | |
name = [account.name] | |
accounts[account.guid] = name | |
while account.parent | |
account = account.parent | |
unless account.parent.nil? | |
name.unshift(account.name) | |
end | |
end | |
end | |
accounts.each_pair{|k,v| accounts[k] = v.join(':') } | |
Transaction.all.to_a.each do |transaction| | |
puts "%s %s" % [Date.parse(transaction.post_date).to_s("%Y/%m/%d"), transaction.description] | |
transaction.splits.each do |split| | |
account = accounts[split.account_guid] | |
puts (" %s %s" % [account, split.value_num / split.value_denom.to_f]).gsub(/\.0$/, '') | |
end | |
puts "" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment