-
-
Save dblack/4988254 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Forked from trevoke | |
require 'csv' | |
require 'yaml' | |
require 'rails/all' | |
require 'active_record' | |
require 'active_support' | |
require 'sqlite3' | |
class Transaction < ActiveRecord::Base | |
has_one :transaction_type | |
has_one :category | |
has_one :account | |
end | |
class TransactionType < ActiveRecord::Base | |
end | |
class Category < ActiveRecord::Base | |
end | |
class Account < ActiveRecord::Base | |
end | |
ActiveRecord::Base.establish_connection( | |
adapter: 'sqlite3', | |
database: 'mint.db' | |
) | |
def dateobj string | |
# DAB | |
Date.strptime(str, "%m/%d/%Y").strftime("%d-%m-%Y") | |
end | |
def create_database_from_csv | |
# DAB | |
FileUtils.rm_rf("mint.db") | |
ActiveRecord::Migration.class_eval do | |
create_table :transaction_types do |t| | |
t.string :name | |
end | |
create_table :categories do |t| | |
t.string :name | |
end | |
create_table :accounts do |t| | |
t.string :name | |
end | |
create_table :transactions do |t| | |
t.date :date | |
t.string :description | |
t.string :original_description | |
t.integer :amount | |
t.integer :transaction_type_id | |
t.integer :category_id | |
t.integer :account_id | |
t.string :labels | |
t.string :notes | |
end | |
end | |
# "Date","Description","Original Description","Amount","Transaction Type","Category","Account Name","Labels","Notes" | |
CSV.open('transactions.csv', headers: true) do |csv| | |
csv.each do |t| | |
x = t.to_hash | |
x['Date'] = dateobj(x['Date']) | |
x['Amount'] = x['Amount'].to_i | |
Transaction.create! date: x['Date'], | |
description: x['Description'], | |
original_description: x['Original Description'], | |
amount: x['Amount'], | |
transaction_type_id: TransactionType.find_or_create_by_name(x['Transaction Type']).id, | |
category_id: Category.find_or_create_by_name(x['Category']).id, | |
account_id: Account.find_or_create_by_name(x['Account Name']).id, | |
labels: x['Labels'], | |
notes: x['Notes'] | |
end | |
end | |
end | |
puts "For each month, credit - debit:" | |
first_transaction = Transaction.order('date ASC').first | |
last_transaction = Transaction.order('date DESC').first | |
done_months = [] | |
(first_transaction.date..last_transaction.date).each do |date| | |
# DAB | |
flag = date.strftime("%Y-%m") | |
next if done_months.include? flag | |
b = date.beginning_of_month.to_s(:db) | |
e = date.end_of_month.to_s(:db) | |
t_this_month = Transaction.where "date > ? and date < ?", b, e | |
credits = t_this_month.where 'transaction_type_id = 2 and account_id in (2, 10)' | |
debits = t_this_month.where 'transaction_type_id = 1 and account_id in (1, 15)' | |
# inject always returns an array, so the || 0 side will never be executed | |
credits_sum = credits.map(&:amount).inject(&:+) || 0 | |
debits_sum = debits.map(&:amount).inject(&:+) || 0 | |
puts "#{flag} =>".rjust(10, ' ') | |
puts "\t#{credits_sum}".rjust(6, ' ') | |
puts "\t#{debits_sum}".rjust(6, ' ') | |
puts "\t" + (credits_sum - debits_sum).to_s | |
done_months << flag | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment