Annotation of pta/pta_import.pl, Revision 1.27
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.24 freda 26: "capital_one_360" => \&import_capital_one_360,
1.13 schwarze 27: "capital_one_credit" => \&import_capital_one_credit,
28: "chase_credit" => \&import_chase_credit,
1.22 freda 29: "etrade_ira" => \&import_etrade_ira,
1.13 schwarze 30: "optum_hsa" => \&import_optum_hsa,
31: "sparkasse_camt" => \&import_sparkasse_camt,
1.15 schwarze 32: "wellsfargo" => \&import_wellsfargo,
1.13 schwarze 33: );
34:
1.27 ! schwarze 35: my %date_formats = (
! 36: 'MM/DD/YYYY' => 's#(\d+)/(\d+)/(\d+)#$3$1$2#',
! 37: 'MM/DD/YY' => 's#(\d+)/(\d+)/(\d+)#20$3$1$2#',
! 38: 'YYYY-MM-DD' => 's#(\d+)-(\d+)-(\d+)#$1$2$3#',
! 39: 'DD.MM.YY' => 's#(\d+)\.(\d+)\.(\d+)#20$3$2$1#',
! 40: );
! 41:
1.1 schwarze 42: # === SUBROUTINES =====================================================
1.9 schwarze 43:
44: sub usage () {
1.10 schwarze 45: printf STDERR "usage: %s -I accountname csvfilename\n", $0;
1.9 schwarze 46: exit 1;
47: }
48:
1.1 schwarze 49: # === MAIN PROGRAM =====================================================
50:
1.9 schwarze 51: getopts 'I:' or usage;
1.14 schwarze 52: unless ($opt_I) {
53: warn "The option -I is required.";
1.9 schwarze 54: usage;
55: }
1.14 schwarze 56: my $account_name = $opt_I;
57: $banks{$account_name} or die "unknown accountname: $account_name";
1.13 schwarze 58:
59: # Parse the configuration file.
1.23 freda 60: my $fn = "import/" . $account_name . ".txt";
1.14 schwarze 61: open my $in, '<', $fn or die "$fn: $!";
1.27 ! schwarze 62: my ($cost_center_field, $csv_account, $date_field, $date_regex,
1.25 freda 63: $delim, $header, $quantity_field, $quote, @amount_fields,
64: @compiled, @description_fields, @ignored);
1.9 schwarze 65: while (<$in>) {
66: chomp;
67: next if /^(?:#|$)/;
68: my $line = $_;
1.12 schwarze 69: if (/^ACCOUNT\s+(\S+)$/) {
1.9 schwarze 70: $csv_account and die "duplicate ACCOUNT line: $1";
71: $csv_account = $1;
72: next;
73: }
1.25 freda 74: if (s/^AMOUNT\s+//) {
75: push @amount_fields, $1 - 1 while s/(\d+)\s*//;
76: $_ eq '' or die "trailing garbage: AMOUNT ... $_";
77: next;
78: }
79: if (/^COSTCENTER\s+(\d+)/) {
80: $cost_center_field = $1 - 1;
81: next;
82: }
83: if (/^DATE\s+(\d+)\s+(\S+)/) {
84: $date_field = $1 - 1;
1.27 ! schwarze 85: $date_regex = $date_formats{$2}
! 86: or die "unknown date format: $2";
1.25 freda 87: next;
88: }
1.12 schwarze 89: if (/^DELIM\s+(\S)$/) {
90: $delim and die "duplicate DELIM line: $1";
91: $delim = $1;
92: next;
93: }
1.25 freda 94: if (s/^DESCRIPTION\s+//) {
95: push @description_fields, $1 - 1 while s/(\d+)\s*//;
96: $_ eq '' or die "trailing garbage: DESCRIPTION ... $_";
97: next;
98: }
1.14 schwarze 99: if (/^HEADER\s+(.*)$/) {
100: $header and die "duplicate HEADER line: $1";
101: $header = $1;
102: next;
103: }
1.25 freda 104: if (/^IGNORE\s+(.*)/) {
105: push @ignored, qr/$1/;
106: next;
107: }
108: if (/^QUANTITY\s+(\d+)/) {
109: $quantity_field = $1 - 1;
110: next;
111: }
1.18 freda 112: if (/^QUOTE\s+(\S)$/) {
113: $quote and die "duplicate QUOTE line: $1";
114: $quote = $1;
115: next;
116: }
1.12 schwarze 117: $delim or die "no DELIM line in $fn";
118: s/^(.*)$delim\s+(\d+)\s+(\S+)// or
1.9 schwarze 119: die "$fn import parse error: $line";
1.12 schwarze 120: push @compiled, {
121: re => [map { qr/$_/ } split /$delim/, $1],
122: ac => $2,
123: id => $3,
124: };
1.1 schwarze 125: }
1.9 schwarze 126: close $in;
127: $csv_account or die "no ACCOUNT line in $fn";
1.13 schwarze 128:
129: # Parse the CSV file from the bank.
1.21 schwarze 130: if (@ARGV) {
131: open STDIN, '<', $ARGV[0] or die "$ARGV[0]: $!";
132: }
133: LINE: while (<STDIN>) {
1.13 schwarze 134: chomp;
135: next if (/^$/);
1.20 freda 136: foreach my $ignore (@ignored) {
137: next LINE if /$ignore/;
138: }
1.13 schwarze 139: my $line = $_;
1.14 schwarze 140: if ($header) {
141: $line eq $header
142: or die "expected HEADER $header\nbut got $line";
143: undef $header;
144: next;
145: }
1.18 freda 146: my $copy_line = $line;
147: my @fields;
148: if ($quote) {
149: push @fields, $1 while $copy_line =~ s/$quote([^$quote]*)$quote$delim?//;
150: } else {
151: @fields = split /$delim/, $line;
152: }
1.13 schwarze 153: my $matches = 0;
154: my ($account, $booking);
155: foreach my $selector (@compiled) {
156: $matches = 1;
157: for (my $i = 0; $i <= $#{$selector->{re}}; $i++) {
158: next if $fields[$i] =~ $selector->{re}[$i];
159: $matches = 0;
160: last;
161: }
162: if ($matches) {
163: $account = $selector->{ac};
164: $booking = $selector->{id};
165: last;
166: }
167: }
168: $matches or die "unmatched CSV line: $line";
1.25 freda 169: my $date = $fields[$date_field] or
170: die "date parse error: $line";
1.27 ! schwarze 171: eval '$date =~ ' . $date_regex;
1.25 freda 172: foreach my $i (@amount_fields) {
173: if (defined($fields[$i])) {
174: $fields[$i] =~ s/,/./;
175: $fields[$i] =~ s/\$//;
176: $fields[$i] = "-$1" if $fields[$i] =~ /^\((\d+\.\d+)\)/;
177: }
178: }
1.26 schwarze 179: my $debit = $csv_account;
180: my $credit = $account;
181: my $amount = $fields[$amount_fields[-1]] || -$fields[$amount_fields[0]];
182: if ($amount < 0) {
183: $amount *= -1;
184: $credit = $csv_account;
185: $debit = $account;
186: }
187: $amount = sprintf "%.2f", $amount;
1.25 freda 188: my $description = join ' ', @fields[@description_fields];
1.13 schwarze 189: $date && $amount && $debit && $credit && $description
190: or die "import parse error: $line";
191: $description =~ s/#//g;
1.25 freda 192: $description .= " [$fields[$cost_center_field]]"
193: if $cost_center_field;
194: $description .= " quantity $fields[$quantity_field]"
195: if $quantity_field;
1.13 schwarze 196: print "$date $booking $debit $credit $amount $description\n";
197: }
CVSweb