Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for parsing accounts #13

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/qif.rb
Expand Up @@ -2,4 +2,5 @@

require 'qif/reader'
require 'qif/writer'
require 'qif/transaction'
require 'qif/transaction'
require 'qif/account'
43 changes: 43 additions & 0 deletions lib/qif/account.rb
@@ -0,0 +1,43 @@
require 'qif/date_format'

module Qif
# The Qif::Account class represents an account in a qif file.
class Account
SUPPORTED_FIELDS = {
:name => {"N" => "Name" },
:type => {"T" => "Type" },
:description => {"D"=> "Description" },
:limit => {"L" => "Credit Limit (if applicable)" },
:balance_date => {"/" => "Statement balance date" },
:balance => {"\$" => "Statement balance", },
:end => {"^" => "End of entry" }
}

SUPPORTED_FIELDS.keys.each{|s| attr_accessor s}

def initialize(attributes = {})
SUPPORTED_FIELDS.keys.each{|s| instance_variable_set("@#{s.to_s}", attributes[s])}
end

# Returns a representation of the account as it
# would appear in a qif file.
def to_s(format = 'dd/mm/yyyy')
SUPPORTED_FIELDS.collect do |k,v|
next unless current = instance_variable_get("@#{k}")
field = v.keys.first
case current.class.to_s
when "Time", "Date", "DateTime"
"#{field}#{DateFormat.new(format).format(current)}"
when "Float"
"#{field}#{'%.2f'%current}"
when "String"
current.split("\n").collect {|x| "#{field}#{x}" }
else
"#{field}#{current}"
end
end.flatten.compact.unshift('!Account').join("\n")
end

private
end
end
29 changes: 29 additions & 0 deletions lib/qif/account/builder.rb
@@ -0,0 +1,29 @@
require_relative "../transaction/builderable"
require_relative "../amount_parser"

class Qif::Account::Builder
include Builderable

def initialize(date_parser = ->(date) { Time.parse(date) })
@txn = Qif::Account.new
@date_parser = date_parser
@splits = []
end

set_builder_method :name
set_builder_method :type
set_builder_method :description
set_builder_method :limit, ->(amt) { AmountParser.parse(amt) }
set_builder_method :balance_date, :parse_date
set_builder_method :balance, ->(amt) { AmountParser.parse(amt) }

def build
@txn
end

private

def parse_date(date)
@date_parser.call(date)
end
end
27 changes: 27 additions & 0 deletions lib/qif/reader.rb
Expand Up @@ -2,6 +2,8 @@
require 'qif/date_format'
require 'qif/transaction'
require 'qif/transaction/builder'
require 'qif/account'
require 'qif/account/builder'

module Qif
# The Qif::Reader class reads a qif file and provides access to
Expand Down Expand Up @@ -162,10 +164,35 @@ def cache_transaction(transaction)
transaction_cache[@index] = transaction
end

def read_account
builder = Qif::Account::Builder.new
begin
line = @data.readline.strip
key = line.slice!(0, 1)
builder =
case key
when 'N' then builder.set_name(line)
when 'T' then builder.set_type(line)
when 'D' then builder.set_description(line)
when 'L' then builder.set_limit(line)
when '/' then builder.set_balance_date(line)
when '$' then builder.set_balance(line)
else builder
end
end until key == "^"
builder.build
rescue EOFError => e
@data.close
nil
rescue Exception => e
nil
end

def read_record
builder = Qif::Transaction::Builder.new(->(dt){@format.parse(dt)})
begin
line = @data.readline.strip
return read_account if line =~ /^\!Account/
key = line.slice!(0, 1)
builder =
case key
Expand Down
17 changes: 15 additions & 2 deletions lib/qif/writer.rb
Expand Up @@ -52,20 +52,27 @@ def initialize(io, type = 'Bank', format = 'dd/mm/yyyy')
@type = type
@format = format
@transactions = []
@accounts = []

if block_given?
yield self
self.write
end
end

# Add a transaction for writing
# Add a QIF entry for writing
def <<(transaction)
@transactions << transaction
case transaction.class.to_s
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this part of the PR needs some work. If there can only be one account then it seems it should not be set using the add transaction method here. Nor should it use an array instance variable.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It would be better if it raised an exception rather than silently handling more than one account.
On that note should 'write' fail if there is no account?

Im not really sure how these files work im just going off the code and comments.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This did cross my mind when I wrote this. According to the QIF spec:

The account header !Account is used in two places-at the start of an account list and the start of a list of transactions to specify to which account they belong.
via http://www.respmech.com/mym2qifw/qif_new.htm

My take on the first use "at the start of an account list" is for when the QIF file contains an account structure and no transaction data, therefore not relevant.

In the second use case, at the "start of a list of transactions to specify to which account they belong", the !Account block is used to describe the subsequent transactions. Therefore the subsequent Transactions belong to the Account. I have seen multiple Account blocks in a QIF file where the transactions appear sequentially for multiple accounts. Think of the Account section being like a header for the transaction records.

To implement the above would need quite a re-write to both Account and Transaction classes and associated reader and writer methods.

For what I am working on I only need one Account per file but will look into making the other changes.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidsklar did say that his !Account was found at the end of the file. But if !Account is at the start of a transaction block something like this should work without too much of a refactor.

@accounts = {}

def <<(transaction)
  case transaction.class.name 
  when 'Qif::Account'     then @account = transaction.to_s
  when 'Qif::Transaction' 
    @accounts[@account] ||= []
    @accounts[@account] << transaction
  else raise "Cannot handle #{transaction.class.name.inspect}"
  end
end

def write
  @accounts.each do |account,transactions|
    write_record account
    write_header
    transactions.each {|t| write_transaction }
  end
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have also run across .QIF files with multiple account blocks. For example, in an export from Moneydance, there are a number of !Account blocks at the beginning of the file, one after another, describing/declaring each account, and then the !Account blocks recur later, followed by a !Type statement and then a bunch of transactions for that account.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we should support multiple accounts then, if someone has the time to update this PR?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I agree now. The QIF format seems very loose so good to let the developer define the format as per their use case. So we now have multiple Accounts and Transactions. No need to create any relationship between them in this gem.

I have just submitted a PR to @davidsklar that handles writing multiple accounts. davidsklar#3

@davidsklar Can you merge please.

when "Qif::Transaction"
@transactions << transaction
when "Qif::Account"
@accounts << transaction
end
end

# Write the qif file
def write
write_account
write_header
write_transactions
end
Expand All @@ -80,6 +87,12 @@ def close
def write_header
@io.write("!Type:%s\n" % @type)
end

def write_account
@accounts.each do |a|
write_record(a)
end
end

def write_transactions
@transactions.each do |t|
Expand Down
@@ -0,0 +1,35 @@
!Type:Bank
D6/1/94
T-1,000.00
CX
N1005
PBank Of Mortgage
MMemo
L[linda]
S[linda]
ECash
$-253.64
SMort Int
$=746.36
^
D6/2/94
T75.00
PDeposit
^
D6/3/94
T-10.00
PAnthony Hopkins
MFilm
LEntertain
AP.O. Box 27027
ATucson, AZ
A85726
A
A
A
^
!Account
NNice Account
TBank
DThis is a fine account
^
25 changes: 25 additions & 0 deletions spec/lib/account_spec.rb
@@ -0,0 +1,25 @@
require 'spec_helper'

describe Qif::Account do
describe '#to_s' do
before do
@instance = Qif::Account.new(
:name => 'Expenses:Eating and Drinking:Coffee',
:type => 'Expense',
:description => 'bean counting'
)
end

it 'should put the name in N' do
@instance.to_s.should include('NExpenses:Eating and Drinking:Coffee')
end

it 'should put the type in T' do
@instance.to_s.should include('TExpense')
end

it 'should put the description in D' do
@instance.to_s.should include('Dbean counting')
end
end
end
11 changes: 11 additions & 0 deletions spec/lib/reader_spec.rb
Expand Up @@ -145,4 +145,15 @@
it { expect(transaction.splits.first.amount).to eq(23) }
end
end

context "reading account blocks" do
it 'should parse the account block' do
@instance = Qif::Reader.new(open('spec/fixtures/quicken_non_investement_account_with_account_block.qif'))
account = @instance.transactions.last
expect(account).to be_a(Qif::Account)
expect(account.name).to eq("Nice Account")
expect(account.type).to eq("Bank")
expect(account.description).to eq("This is a fine account")
end
end
end
20 changes: 20 additions & 0 deletions spec/lib/writer_spec.rb
Expand Up @@ -51,6 +51,26 @@
ran.should be_true
end

it 'should write the account' do

Qif::Writer.new(@io) do |writer|
writer << Qif::Account.new(:name => 'Expenses:Eating and Drinking:Coffee')
end

@buffer.should include('NExpenses:Eating and Drinking:Coffee')
end

it 'should write multiple accounts' do

Qif::Writer.new(@io) do |writer|
writer << Qif::Account.new(:name => 'Expenses:Eating and Drinking:Coffee')
writer << Qif::Account.new(:name => 'Expenses:Eating and Drinking:Tea')
end

@buffer.should include('NExpenses:Eating and Drinking:Coffee')
@buffer.should include('NExpenses:Eating and Drinking:Tea')
end

it 'should write the transactions' do
date = Time.now

Expand Down