Skip to content

Commit

Permalink
Merge pull request #36 from BlackEdder/broker_canada
Browse files Browse the repository at this point in the history
Correctly identify a debit/credit column after the money column.

Also added spec for csv files from Broker Canada and Intuit Mint.
  • Loading branch information
BlackEdder committed Feb 18, 2014
2 parents 6ee3688 + 169ad61 commit 9db91a8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/reckon/csv_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,22 @@ def found_double_money_column( id1, id2 )
end
end

# Some csv files negative/positive amounts are inicated in separate account
# Some csv files negative/positive amounts are indicated in separate account
def detect_sign_column
# For now only look at preceding column
return false if @money_column_indices[0] == 0
column = columns[ @money_column_indices[0] - 1 ]
signs = column.uniq
return if columns[0].length <= 2 # This test needs requires more than two rows otherwise will lead to false positives
signs = []
if @money_column_indices[0] > 0
column = columns[ @money_column_indices[0] - 1 ]
signs = column.uniq
end
if (signs.length != 2 &&
(@money_column_indices[0] + 1 < columns.length))
column = columns[ @money_column_indices[0] + 1 ]
signs = column.uniq
end
if signs.length == 2
negative_first = false
negative_first = true if signs[0] == "Af" || signs[0].downcase =~ /^cr/ # look for known debit indicators
negative_first = true
negative_first = false if signs[0] == "Bij" || signs[0].downcase =~ /^cr/ # look for known debit indicators
@money_column.each_with_index do |money, i|
if negative_first && column[i] == signs[0]
@money_column[i] = -money
Expand Down
41 changes: 41 additions & 0 deletions spec/reckon/csv_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
@ing_csv = Reckon::CSVParser.new(:string => ING_CSV, :comma_separates_cents => true )
@austrian_csv = Reckon::CSVParser.new(:string => AUSTRIAN_EXAMPLE, :comma_separates_cents => true, :csv_separator => ';' )
@french_csv = Reckon::CSVParser.new(:string => FRENCH_EXAMPLE, :csv_separator => ';', :comma_separates_cents => true)
@broker_canada = Reckon::CSVParser.new(:string => BROKER_CANADA_EXAMPLE)
@intuit_mint = Reckon::CSVParser.new(:string => INTUIT_MINT_EXAMPLE)
end

it "should be in testing mode" do
Expand Down Expand Up @@ -70,6 +72,8 @@
@ing_csv.money_column_indices.should == [6]
@austrian_csv.money_column_indices.should == [4]
@french_csv.money_column_indices.should == [6]
@broker_canada.money_column_indices.should == [8]
@intuit_mint.money_column_indices.should == [3]
end

it "should detect the date column" do
Expand All @@ -80,6 +84,8 @@
@danish_kroner_nordea.date_column_index.should == 0
@yyyymmdd_date.date_column_index.should == 1
@french_csv.date_column_index.should == 2
@broker_canada.date_column_index.should == 0
@intuit_mint.date_column_index.should == 0
end

it "should consider all other columns to be description columns" do
Expand Down Expand Up @@ -120,6 +126,10 @@
@austrian_csv.money_for(2).should == 120.00
@french_csv.money_for(0).should == -10.00
@french_csv.money_for(1).should == -5.76
@broker_canada.money_for(0).should == 12.55
@broker_canada.money_for(1).should == -81.57
@intuit_mint.money_for(0).should == 0.01
@intuit_mint.money_for(1).should == -331.63
end

it "should handle the comma_separates_cents option correctly" do
Expand Down Expand Up @@ -161,6 +171,12 @@
@nationwide.date_for(1).month.should == 10
@ing_csv.date_for(1).month.should == Time.parse("2012/11/12").month
@ing_csv.date_for(1).day.should == Time.parse("2012/11/12").day
@broker_canada.date_for(5).year.should == 2014
@broker_canada.date_for(5).month.should == 1
@broker_canada.date_for(5).day.should == 7
@intuit_mint.date_for(1).year.should == 2014
@intuit_mint.date_for(1).month.should == 2
@intuit_mint.date_for(1).day.should == 3
end
end

Expand Down Expand Up @@ -349,4 +365,29 @@
01234567890;21/01/2014;21/01/2014;CARTE 012345 CB:*0123456 XXXXXX XXXXXXXX33ST ANDRE 935/;0POBUEQ;21/01/2014;-25,65;
CSV

BROKER_CANADA_EXAMPLE = (<<-CSV).strip
2014-02-10,2014-02-10,Interest,ISHARES S&P/TSX CAPPED REIT IN,XRE,179,,,12.55,CAD
2014-01-16,2014-01-16,Reinvestment,ISHARES GLOBAL AGRICULTURE IND,COW,3,,,-81.57,CAD
2014-01-16,2014-01-16,Contribution,CONTRIBUTION,,,,,600.00,CAD
2014-01-16,2014-01-16,Interest,ISHARES GLOBAL AGRICULTURE IND,COW,200,,,87.05,CAD
2014-01-14,2014-01-14,Reinvestment,BMO NASDAQ 100 EQTY HEDGED TO,ZQQ,2,,,-54.72,CAD
2014-01-07,2014-01-10,Sell,BMO NASDAQ 100 EQTY HEDGED TO,ZQQ,-300,27.44,CDN,8222.05,CAD
2014-01-07,2014-01-07,Interest,BMO S&P/TSX EQUAL WEIGHT BKS I,ZEB,250,,,14.00,CAD
2013-07-02,2013-07-02,Dividend,SELECT SECTOR SPDR FD SHS BEN,XLB,130,,,38.70,USD
2013-06-27,2013-06-27,Dividend,ICICI BK SPONSORED ADR,IBN,100,,,66.70,USD
2013-06-19,2013-06-24,Buy,ISHARES S&P/TSX CAPPED REIT IN,XRE,300,15.90,CDN,-4779.95,CAD
2013-06-17,2013-06-17,Contribution,CONTRIBUTION,,,,,600.00,CAD
2013-05-22,2013-05-22,Dividend,NATBK,NA,70,,,58.10,CAD
CSV

INTUIT_MINT_EXAMPLE = (<<-CSV).strip
"12/10/2014","Dn Ing Inv","[DN]ING INV/PLA","0.01","credit","Investments","Chequing","",""
"2/03/2014","Ds Lms Msp Condo","[DS]LMS598 MSP/DIV","331.63","debit","Condo Fees","Chequing","",""
"2/10/2014","Ib Granville","[IB] 2601 GRANVILLE","100.00","debit","Uncategorized","Chequing","",""
"2/06/2014","So Pa","[SO]PA 0005191230116379851","140.72","debit","Mortgage & Rent","Chequing","",""
"2/03/2014","Dn Sun Life","[DN]SUN LIFE MSP/DIV","943.34","credit","Income","Chequing","",""
"1/30/2014","Transfer to CBT (Savings)","[CW] TF 0004#3409-797","500.00","debit","Transfer","Chequing","",""
"1/30/2014","Costco","[PR]COSTCO WHOLESAL","559.96","debit","Business Services","Chequing","",""
CSV

end

0 comments on commit 9db91a8

Please sign in to comment.