Annotation of pta/pta_import.pl, Revision 1.22
1.17 schwarze 1: #!/usr/bin/perl
1.1 schwarze 2:
3: # Copyright (c) 2020 Freda Bundchen
4:
5: # Permission to use, copy, modify, and distribute this software for any
6: # purpose with or without fee is hereby granted, provided that the above
7: # copyright notice and this permission notice appear in all copies.
8: #
9: # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16:
17: use warnings;
18: use strict;
19:
1.9 schwarze 20: use Getopt::Std qw(getopts);
21:
22: our ($opt_I);
23:
1.13 schwarze 24: my %banks = (
1.16 schwarze 25: "bbva_usa" => \&import_bbva_usa,
1.13 schwarze 26: "capital_one_credit" => \&import_capital_one_credit,
27: "chase_credit" => \&import_chase_credit,
1.22 ! freda 28: "etrade_ira" => \&import_etrade_ira,
1.13 schwarze 29: "optum_hsa" => \&import_optum_hsa,
30: "sparkasse_camt" => \&import_sparkasse_camt,
1.15 schwarze 31: "wellsfargo" => \&import_wellsfargo,
1.13 schwarze 32: );
33:
1.1 schwarze 34: # === SUBROUTINES =====================================================
1.16 schwarze 35:
36: sub import_bbva_usa ($$$) {
37: my ($account, $csv_account, $line) = @_;
1.17 schwarze 38: $line =~
1.16 schwarze 39: m#(\d+)/(\d+)/(\d+),(.*?),\S+,(\(?\d+\.\d+\)?),# or
40: die "parse error: $line";
41: my ($month, $day, $year, $description, $amount) =
42: ($1, $2, $3, $4, $5);
43: $amount =~ s/\((\d+\.\d+)\)/-$1/;
44: return "$year$month$day",
45: get_accounts_by_amount_sign($amount, $account, $csv_account),
46: $description;
47: }
1.1 schwarze 48:
1.10 schwarze 49: sub import_chase_credit ($$$) {
50: my ($account, $csv_account, $line) = @_;
51: my ($trans_date, $post_date, $description, $category,
52: $type, $amount) = split /,/, $line;
53: my ($debit, $credit);
1.17 schwarze 54: $post_date =~ s#(\d+)/(\d+)/(\d+)#$3$1$2#;
1.10 schwarze 55: ($amount, $debit, $credit) =
56: get_accounts_by_amount_sign($amount, $account,
57: $csv_account);
58: return ($post_date, $amount, $debit, $credit, $description);
59: }
60:
61: sub import_capital_one_credit ($$$) {
62: my ($account, $csv_account, $line) = @_;
63: my ($trans_date, $post_date, $card_num,
64: $description, $category, $csv_debit,
65: $csv_credit) = split /,/, $line;
1.17 schwarze 66: $post_date =~ s/(\d+)-(\d+)-(\d+)/$1$2$3/;
67: my ($amount, $debit, $credit) =
1.10 schwarze 68: get_accounts_by_csv_col($account, $csv_account,
69: $csv_debit, $csv_credit);
70: return ($post_date, $amount, $debit, $credit, $description);
1.22 ! freda 71: }
! 72:
! 73: sub import_etrade_ira ($$$) {
! 74: my ($account, $csv_account, $line) = @_;
! 75: my @fields = split /,/, $line;
! 76: $fields[0] =~ s#(\d+)/(\d+)/(\d+)#20$3$1$2#;
! 77: my ($amount, $debit, $credit) =
! 78: get_accounts_by_amount_sign($fields[5], $account,
! 79: $csv_account);
! 80: if ($fields[2] eq 'EQ') {
! 81: $fields[8] .= " [$fields[3]]";
! 82: }
! 83: return ($fields[0], $amount, $debit, $credit,
! 84: join ' quantity ', $fields[8], $fields[4]);
1.10 schwarze 85: }
86:
87: sub import_optum_hsa ($$$) {
88: my ($account, $csv_account, $line) = @_;
89: my ($date, $description, $amount,
90: $type) = split /,/, $line;
91: my ($debit, $credit);
92: $date =~ s/(\d+)-(\d+)-(\d+)/$1$2$3/;
93: $amount =~ s/\$//;
94: ($amount, $debit, $credit) =
95: get_accounts_by_amount_sign($amount, $account,
96: $csv_account);
97: return ($date, $amount, $debit, $credit, $description);
98: }
99:
1.11 schwarze 100: sub import_sparkasse_camt ($$$) {
101: my ($account, $csv_account, $line) = @_;
102: my @fields;
103: $_ = $line;
104: push @fields, $1 while s/"([^"]*)";?//;
105: $_ eq "" or die "CAMT parse error before $_ in $line";
106: @fields == 17 or die "not 17 but @fields fields: $line";
107: $fields[1] =~ s/^(\d\d)\.(\d\d)\.(\d\d)$/20$3$2$1/
108: or die "date parse error: $line";
109: $fields[14] =~ s/,/./;
110: return $fields[1],
111: get_accounts_by_amount_sign($fields[14], $account, $csv_account),
112: (join ' ', $fields[11], $fields[4]);
1.15 schwarze 113: }
114:
115: sub import_wellsfargo ($$$) {
116: my ($account, $csv_account, $line) = @_;
117: $line =~
1.19 freda 118: m#^"(\d+)/(\d+)/(\d+)","(-?\d+\.\d+)","\*",".*","(.*?)"$# or
1.15 schwarze 119: die "parse error: $line" ;
120: my ($month, $day, $year, $amount, $description) =
121: ($1, $2, $3, $4, $5);
122: return "$year$month$day",
123: get_accounts_by_amount_sign($amount, $account, $csv_account),
124: $description;
1.11 schwarze 125: }
126:
1.10 schwarze 127: sub get_accounts_by_amount_sign ($$$) {
128: my ($amount, $account, $csv_account) = @_;
129: my ($debit, $credit);
130: if ($amount <= 0) {
131: $amount = substr $amount, 1;
1.17 schwarze 132: $credit = $csv_account;
1.10 schwarze 133: $debit = $account;
134: } else {
1.17 schwarze 135: $debit = $csv_account;
1.10 schwarze 136: $credit = $account;
1.5 schwarze 137: }
1.10 schwarze 138: return ($amount, $debit, $credit);
1.5 schwarze 139: }
1.9 schwarze 140:
1.10 schwarze 141: sub get_accounts_by_csv_col ($$$$) {
142: my ($account, $csv_account, $csv_debit, $csv_credit) = @_;
143: my ($amount, $debit, $credit);
144: if ($csv_debit eq "") {
145: $amount = $csv_credit;
146: $credit = $account;
147: $debit = $csv_account;
148: } else {
149: $amount = $csv_debit;
150: $credit = $csv_account;
151: $debit = $account;
152: }
153: return ($amount, $debit, $credit);
1.9 schwarze 154: }
155:
156: sub usage () {
1.10 schwarze 157: printf STDERR "usage: %s -I accountname csvfilename\n", $0;
1.9 schwarze 158: exit 1;
159: }
160:
1.1 schwarze 161: # === MAIN PROGRAM =====================================================
162:
1.9 schwarze 163: getopts 'I:' or usage;
1.14 schwarze 164: unless ($opt_I) {
165: warn "The option -I is required.";
1.9 schwarze 166: usage;
167: }
1.14 schwarze 168: my $account_name = $opt_I;
169: $banks{$account_name} or die "unknown accountname: $account_name";
1.13 schwarze 170:
171: # Parse the configuration file.
1.14 schwarze 172: my $fn = "import_" . $account_name . ".txt";
173: open my $in, '<', $fn or die "$fn: $!";
1.20 freda 174: my ($csv_account, $delim, $header, $quote, @compiled, @ignored);
1.9 schwarze 175: while (<$in>) {
176: chomp;
177: next if /^(?:#|$)/;
178: my $line = $_;
1.12 schwarze 179: if (/^ACCOUNT\s+(\S+)$/) {
1.9 schwarze 180: $csv_account and die "duplicate ACCOUNT line: $1";
181: $csv_account = $1;
182: next;
183: }
1.12 schwarze 184: if (/^DELIM\s+(\S)$/) {
185: $delim and die "duplicate DELIM line: $1";
186: $delim = $1;
187: next;
188: }
1.14 schwarze 189: if (/^HEADER\s+(.*)$/) {
190: $header and die "duplicate HEADER line: $1";
191: $header = $1;
192: next;
193: }
1.18 freda 194: if (/^QUOTE\s+(\S)$/) {
195: $quote and die "duplicate QUOTE line: $1";
196: $quote = $1;
197: next;
198: }
1.20 freda 199: if (/^IGNORE\s+(.*)/) {
200: push @ignored, qr/$1/;
201: next;
202: }
1.12 schwarze 203: $delim or die "no DELIM line in $fn";
204: s/^(.*)$delim\s+(\d+)\s+(\S+)// or
1.9 schwarze 205: die "$fn import parse error: $line";
1.12 schwarze 206: push @compiled, {
207: re => [map { qr/$_/ } split /$delim/, $1],
208: ac => $2,
209: id => $3,
210: };
1.1 schwarze 211: }
1.9 schwarze 212: close $in;
213: $csv_account or die "no ACCOUNT line in $fn";
1.13 schwarze 214:
215: # Parse the CSV file from the bank.
1.21 schwarze 216: if (@ARGV) {
217: open STDIN, '<', $ARGV[0] or die "$ARGV[0]: $!";
218: }
219: LINE: while (<STDIN>) {
1.13 schwarze 220: chomp;
221: next if (/^$/);
1.20 freda 222: foreach my $ignore (@ignored) {
223: next LINE if /$ignore/;
224: }
1.13 schwarze 225: my $line = $_;
1.14 schwarze 226: if ($header) {
227: $line eq $header
228: or die "expected HEADER $header\nbut got $line";
229: undef $header;
230: next;
231: }
1.18 freda 232: my $copy_line = $line;
233: my @fields;
234: if ($quote) {
235: push @fields, $1 while $copy_line =~ s/$quote([^$quote]*)$quote$delim?//;
236: } else {
237: @fields = split /$delim/, $line;
238: }
1.13 schwarze 239: my $matches = 0;
240: my ($account, $booking);
241: foreach my $selector (@compiled) {
242: $matches = 1;
243: for (my $i = 0; $i <= $#{$selector->{re}}; $i++) {
244: next if $fields[$i] =~ $selector->{re}[$i];
245: $matches = 0;
246: last;
247: }
248: if ($matches) {
249: $account = $selector->{ac};
250: $booking = $selector->{id};
251: last;
252: }
253: }
254: $matches or die "unmatched CSV line: $line";
255: my ($date, $amount, $debit, $credit, $description) =
256: $banks{$account_name}->($account, $csv_account, $line);
257: $date && $amount && $debit && $credit && $description
258: or die "import parse error: $line";
259: $description =~ s/#//g;
260: print "$date $booking $debit $credit $amount $description\n";
261: }
CVSweb