-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcibc2csv.pl
executable file
·132 lines (120 loc) · 4.01 KB
/
cibc2csv.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/opt/local/bin/perl -W
use strict;
use warnings;
open( FILE, "<../cibc.csv" )
or die( "Can't open file cibc.csv $!" );
my $notdonefile = '>../notdone.csv';
open my $ndinfo, $notdonefile or die "Could not open $notdonefile: $!";
my $donefile = '>../done.ldg';
open my $doneinfo, $donefile or die "Could not open $donefile: $!";
my @lines = reverse <FILE>;
close FILE;
foreach my $line (@lines) {
chomp($line);
my ($date,$desc,$value) = split(',',$line);
$value =~ s/\s*$//g;
if ($value eq "" || $line !~ /Point of Sale/) {
print $ndinfo $line."\n";
next;
}
my ($keyWord, $expenseWord) = &processDesc($desc);
if($keyWord eq "" || $expenseWord eq "") {
print $ndinfo $line."\n";
next;
}
$date =~ s/-/\//g;
print $doneinfo $date." $keyWord\n";
print $doneinfo "\t$expenseWord\t\t\t\t\$$value\t;$keyWord\n";
print $doneinfo "\tAssets:Checking\t\t\t\t\t\t\t\t\$-$value\n\n";
}
sub processDesc{
my $desc = shift;
if($desc =~ /Point of Sale - INTERAC RETAIL PURCHASE \d{12} (.*)/) {
my $matched = $1;
$matched =~ s/\s*$//g;
my $length = length($matched);
my $lastchar = lc(substr $matched, -1);
if ($lastchar =~ /[a-z]/) {
return &processMatchedWord($matched);
} else {
#$matched =~ s/\s+\w+$//;
my @splitWords = split(/ +/, $matched);
pop(@splitWords);
$matched = join(" ", @splitWords);
return &processMatchedWord($matched);
}
#return ("Key Word", "Expense:TestExpense:Dummy");
}
return ("", "");
}
sub processMatchedWord {
my ($keyWord, $expenseWord, $regexInput) = ("","","");
my $matched = shift;
my @splitWords = split(/ +/, $matched);
if(scalar(@splitWords)>2) {
#if first two sum is greater than 6; then good, else bad. include until end?
if((length($splitWords[0])+length($splitWords[1]))>6) {
#greater than 6. Use the first two.
$keyWord = $splitWords[0]." ".$splitWords[1];
($keyWord, $regexInput) = &getKeyWordRegex($keyWord);
$expenseWord = &SearchForKeyWord($regexInput);
return ($keyWord, $expenseWord);
} else {
#not greater than 6. Add until greater than 6 or runs out of words to ad..
$keyWord = $splitWords[0]." ".$splitWords[1];
my $lengthArray = scalar(@splitWords);
my $i=2;
for(; $i<$lengthArray; $i++) {
$keyWord .= " ".$splitWords[$i];
if(length($keyWord)>8) {
last;
}
}
($keyWord, $regexInput) = &getKeyWordRegex($keyWord);
$expenseWord = &SearchForKeyWord($regexInput);
return ($keyWord, $expenseWord);
}
} else {
$keyWord = $matched;
($keyWord, $regexInput) = &getKeyWordRegex($keyWord);
$expenseWord = &SearchForKeyWord($regexInput);
return ($keyWord, $expenseWord);
}
}
sub getKeyWordRegex {
my $keyWord = shift;
my $regexInput = "";
$keyWord = (lc($keyWord));
$keyWord =~ s/([\w']+)/\u\L$1/g;
$regexInput = "Expenses ".$keyWord;
$regexInput =~ s/ /\.\*/g;
$regexInput = lc($regexInput);
return ($keyWord, $regexInput);
}
sub SearchForKeyWord {
my $regexInput = shift;
my $expenseWord = "";
open( FILE, "<../finance.ldg" )
or die( "Can't open file finance.ldg $!" );
my @lines = reverse <FILE>;
close FILE;
foreach my $line (@lines) {
chomp($line);
$line =~ s/\s*$//g;
if($line =~ /($regexInput)/gi) {
if($line =~ /gift/i) {
#TODO fix.
next;
}
my $matched = $1;
$matched =~ s/\s*$//g;
return (split(/[\t ]+/, $matched))[0];
}
}
#TODO Add documentation.
print "Returning empty ('$expenseWord') expenseWord for $regexInput in SearchForKeyWord\n";
return $expenseWord;
}
close $ndinfo;
close $doneinfo;
exit 0;