Annotation of pta/pta_import.pl, Revision 1.20
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,
28: "optum_hsa" => \&import_optum_hsa,
29: "sparkasse_camt" => \&import_sparkasse_camt,
1.15 schwarze 30: "wellsfargo" => \&import_wellsfargo,
1.13 schwarze 31: );
32:
1.1 schwarze 33: # === SUBROUTINES =====================================================
1.16 schwarze 34:
35: sub import_bbva_usa ($$$) {
36: my ($account, $csv_account, $line) = @_;
1.17 schwarze 37: $line =~
1.16 schwarze 38: m#(\d+)/(\d+)/(\d+),(.*?),\S+,(\(?\d+\.\d+\)?),# or
39: die "parse error: $line";
40: my ($month, $day, $year, $description, $amount) =
41: ($1, $2, $3, $4, $5);
42: $amount =~ s/\((\d+\.\d+)\)/-$1/;
43: return "$year$month$day",
44: get_accounts_by_amount_sign($amount, $account, $csv_account),
45: $description;
46: }
1.1 schwarze 47:
1.10 schwarze 48: sub import_chase_credit ($$$) {
49: my ($account, $csv_account, $line) = @_;
50: my ($trans_date, $post_date, $description, $category,
51: $type, $amount) = split /,/, $line;
52: my ($debit, $credit);
1.17 schwarze 53: $post_date =~ s#(\d+)/(\d+)/(\d+)#$3$1$2#;
1.10 schwarze 54: ($amount, $debit, $credit) =
55: get_accounts_by_amount_sign($amount, $account,
56: $csv_account);
57: return ($post_date, $amount, $debit, $credit, $description);
58: }
59:
60: sub import_capital_one_credit ($$$) {
61: my ($account, $csv_account, $line) = @_;
62: my ($trans_date, $post_date, $card_num,
63: $description, $category, $csv_debit,
64: $csv_credit) = split /,/, $line;
1.17 schwarze 65: $post_date =~ s/(\d+)-(\d+)-(\d+)/$1$2$3/;
66: my ($amount, $debit, $credit) =
1.10 schwarze 67: get_accounts_by_csv_col($account, $csv_account,
68: $csv_debit, $csv_credit);
69: return ($post_date, $amount, $debit, $credit, $description);
70: }
71:
72: sub import_optum_hsa ($$$) {
73: my ($account, $csv_account, $line) = @_;
74: my ($date, $description, $amount,
75: $type) = split /,/, $line;
76: my ($debit, $credit);
77: $date =~ s/(\d+)-(\d+)-(\d+)/$1$2$3/;
78: $amount =~ s/\$//;
79: ($amount, $debit, $credit) =
80: get_accounts_by_amount_sign($amount, $account,
81: $csv_account);
82: return ($date, $amount, $debit, $credit, $description);
83: }
84:
1.11 schwarze 85: sub import_sparkasse_camt ($$$) {
86: my ($account, $csv_account, $line) = @_;
87: my @fields;
88: $_ = $line;
89: push @fields, $1 while s/"([^"]*)";?//;
90: $_ eq "" or die "CAMT parse error before $_ in $line";
91: @fields == 17 or die "not 17 but @fields fields: $line";
92: $fields[1] =~ s/^(\d\d)\.(\d\d)\.(\d\d)$/20$3$2$1/
93: or die "date parse error: $line";
94: $fields[14] =~ s/,/./;
95: return $fields[1],
96: get_accounts_by_amount_sign($fields[14], $account, $csv_account),
97: (join ' ', $fields[11], $fields[4]);
1.15 schwarze 98: }
99:
100: sub import_wellsfargo ($$$) {
101: my ($account, $csv_account, $line) = @_;
102: $line =~
1.19 freda 103: m#^"(\d+)/(\d+)/(\d+)","(-?\d+\.\d+)","\*",".*","(.*?)"$# or
1.15 schwarze 104: die "parse error: $line" ;
105: my ($month, $day, $year, $amount, $description) =
106: ($1, $2, $3, $4, $5);
107: return "$year$month$day",
108: get_accounts_by_amount_sign($amount, $account, $csv_account),
109: $description;
1.11 schwarze 110: }
111:
1.10 schwarze 112: sub get_accounts_by_amount_sign ($$$) {
113: my ($amount, $account, $csv_account) = @_;
114: my ($debit, $credit);
115: if ($amount <= 0) {
116: $amount = substr $amount, 1;
1.17 schwarze 117: $credit = $csv_account;
1.10 schwarze 118: $debit = $account;
119: } else {
1.17 schwarze 120: $debit = $csv_account;
1.10 schwarze 121: $credit = $account;
1.5 schwarze 122: }
1.10 schwarze 123: return ($amount, $debit, $credit);
1.5 schwarze 124: }
1.9 schwarze 125:
1.10 schwarze 126: sub get_accounts_by_csv_col ($$$$) {
127: my ($account, $csv_account, $csv_debit, $csv_credit) = @_;
128: my ($amount, $debit, $credit);
129: if ($csv_debit eq "") {
130: $amount = $csv_credit;
131: $credit = $account;
132: $debit = $csv_account;
133: } else {
134: $amount = $csv_debit;
135: $credit = $csv_account;
136: $debit = $account;
137: }
138: return ($amount, $debit, $credit);
1.9 schwarze 139: }
140:
141: sub usage () {
1.10 schwarze 142: printf STDERR "usage: %s -I accountname csvfilename\n", $0;
1.9 schwarze 143: exit 1;
144: }
145:
1.1 schwarze 146: # === MAIN PROGRAM =====================================================
147:
1.9 schwarze 148: getopts 'I:' or usage;
1.14 schwarze 149: unless ($opt_I) {
150: warn "The option -I is required.";
1.9 schwarze 151: usage;
152: }
1.14 schwarze 153: my $account_name = $opt_I;
154: $banks{$account_name} or die "unknown accountname: $account_name";
1.13 schwarze 155:
156: # Parse the configuration file.
1.14 schwarze 157: my $fn = "import_" . $account_name . ".txt";
158: open my $in, '<', $fn or die "$fn: $!";
1.20 ! freda 159: my ($csv_account, $delim, $header, $quote, @compiled, @ignored);
1.9 schwarze 160: while (<$in>) {
161: chomp;
162: next if /^(?:#|$)/;
163: my $line = $_;
1.12 schwarze 164: if (/^ACCOUNT\s+(\S+)$/) {
1.9 schwarze 165: $csv_account and die "duplicate ACCOUNT line: $1";
166: $csv_account = $1;
167: next;
168: }
1.12 schwarze 169: if (/^DELIM\s+(\S)$/) {
170: $delim and die "duplicate DELIM line: $1";
171: $delim = $1;
172: next;
173: }
1.14 schwarze 174: if (/^HEADER\s+(.*)$/) {
175: $header and die "duplicate HEADER line: $1";
176: $header = $1;
177: next;
178: }
1.18 freda 179: if (/^QUOTE\s+(\S)$/) {
180: $quote and die "duplicate QUOTE line: $1";
181: $quote = $1;
182: next;
183: }
1.20 ! freda 184: if (/^IGNORE\s+(.*)/) {
! 185: push @ignored, qr/$1/;
! 186: next;
! 187: }
1.12 schwarze 188: $delim or die "no DELIM line in $fn";
189: s/^(.*)$delim\s+(\d+)\s+(\S+)// or
1.9 schwarze 190: die "$fn import parse error: $line";
1.12 schwarze 191: push @compiled, {
192: re => [map { qr/$_/ } split /$delim/, $1],
193: ac => $2,
194: id => $3,
195: };
1.1 schwarze 196: }
1.9 schwarze 197: close $in;
198: $csv_account or die "no ACCOUNT line in $fn";
1.13 schwarze 199:
200: # Parse the CSV file from the bank.
1.20 ! freda 201: LINE: while (<>) {
1.13 schwarze 202: chomp;
203: next if (/^$/);
1.20 ! freda 204: foreach my $ignore (@ignored) {
! 205: next LINE if /$ignore/;
! 206: }
1.13 schwarze 207: my $line = $_;
1.14 schwarze 208: if ($header) {
209: $line eq $header
210: or die "expected HEADER $header\nbut got $line";
211: undef $header;
212: next;
213: }
1.18 freda 214: my $copy_line = $line;
215: my @fields;
216: if ($quote) {
217: push @fields, $1 while $copy_line =~ s/$quote([^$quote]*)$quote$delim?//;
218: } else {
219: @fields = split /$delim/, $line;
220: }
1.13 schwarze 221: my $matches = 0;
222: my ($account, $booking);
223: foreach my $selector (@compiled) {
224: $matches = 1;
225: for (my $i = 0; $i <= $#{$selector->{re}}; $i++) {
226: next if $fields[$i] =~ $selector->{re}[$i];
227: $matches = 0;
228: last;
229: }
230: if ($matches) {
231: $account = $selector->{ac};
232: $booking = $selector->{id};
233: last;
234: }
235: }
236: $matches or die "unmatched CSV line: $line";
237: my ($date, $amount, $debit, $credit, $description) =
238: $banks{$account_name}->($account, $csv_account, $line);
239: $date && $amount && $debit && $credit && $description
240: or die "import parse error: $line";
241: $description =~ s/#//g;
242: print "$date $booking $debit $credit $amount $description\n";
243: }
CVSweb