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
davidsklar
wants to merge
6
commits into
jemmyw:master
Choose a base branch
from
davidsklar:master
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
55e00b0
Add support for parsing accounts
davidsklar 4e1d437
write Account transaction type
a7f0537
account tests
5078fb4
Merge pull request #1 from jonbartlett/master
davidsklar 362fc4d
support for writing multiple Account entries
fb0e2db
Merge pull request #3 from jonbartlett/master
davidsklar File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
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
35 changes: 35 additions & 0 deletions
35
spec/fixtures/quicken_non_investement_account_with_account_block.qif
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
^ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.