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