Annotation of pta/pta_import.pl, Revision 1.28
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;
1.28 ! freda 67: s/\s+$//;
1.9 schwarze 68: next if /^(?:#|$)/;
69: my $line = $_;
1.28 ! freda 70: if (s/^ACCOUNT\s+//) {
! 71: $csv_account and die "duplicate ACCOUNT line: $_";
! 72: /^(\d+)$/ or die "ACCOUNT parse error: $_";
1.9 schwarze 73: $csv_account = $1;
74: next;
75: }
1.25 freda 76: if (s/^AMOUNT\s+//) {
1.28 ! freda 77: @amount_fields and die "duplicate AMOUNT line: $_";
1.25 freda 78: push @amount_fields, $1 - 1 while s/(\d+)\s*//;
79: $_ eq '' or die "trailing garbage: AMOUNT ... $_";
80: next;
81: }
1.28 ! freda 82: if (s/^COSTCENTER\s+//) {
! 83: $cost_center_field and
! 84: die "duplicate COSTCENTER line: $_";
! 85: /^(\d+)$/ or die "COSTCENTER parse error: $_";
1.25 freda 86: $cost_center_field = $1 - 1;
87: next;
88: }
1.28 ! freda 89: if (s/^DATE\s+//) {
! 90: $date_field || $date_regex and
! 91: die "duplicate DATE line: $_";
! 92: s/^(\d+)\s+(\S+)$//;
! 93: $date_field = $1 or
! 94: die "DATE date_field parse error: $_";
! 95: $date_field -= 1;
1.27 schwarze 96: $date_regex = $date_formats{$2}
97: or die "unknown date format: $2";
1.25 freda 98: next;
99: }
1.28 ! freda 100: if (s/^DELIM\s+//) {
1.12 schwarze 101: $delim and die "duplicate DELIM line: $1";
1.28 ! freda 102: /^([^|\^\$\*\+\?\(\)\[\]\{\}\\])$/ or
! 103: die "DELIM parse error: $_";
1.12 schwarze 104: $delim = $1;
105: next;
106: }
1.25 freda 107: if (s/^DESCRIPTION\s+//) {
1.28 ! freda 108: @description_fields and die "duplicate DESCRIPTION line: $_";
! 109: push @description_fields, $1 - 1 while s/(\d+)\s*//;
! 110: $_ eq '' or die "trailing garbage: DESCRIPTION ... $_";
! 111: next;
1.25 freda 112: }
1.28 ! freda 113: if (s/^HEADER\s+//) {
1.14 schwarze 114: $header and die "duplicate HEADER line: $1";
1.28 ! freda 115: $header = $_;
1.14 schwarze 116: next;
117: }
1.28 ! freda 118: if (s/^IGNORE\s+//) {
! 119: push @ignored, qr/$_/;
1.25 freda 120: next;
121: }
1.28 ! freda 122: if (s/^QUANTITY\s+//) {
! 123: $quantity_field and die "duplicate QUANTITY line: $1";
! 124: /^(\d+)$/ or die "QUANTITY parse error: $_";
1.25 freda 125: $quantity_field = $1 - 1;
126: next;
127: }
1.28 ! freda 128: if (s/^QUOTE\s+//) {
1.18 freda 129: $quote and die "duplicate QUOTE line: $1";
1.28 ! freda 130: /^([^|\^\$\*\+\?\(\)\[\]\{\}\\])$/ or
! 131: die "QUOTE parse error: $_";
1.18 freda 132: $quote = $1;
133: next;
134: }
1.12 schwarze 135: $delim or die "no DELIM line in $fn";
136: s/^(.*)$delim\s+(\d+)\s+(\S+)// or
1.9 schwarze 137: die "$fn import parse error: $line";
1.12 schwarze 138: push @compiled, {
139: re => [map { qr/$_/ } split /$delim/, $1],
140: ac => $2,
141: id => $3,
142: };
1.1 schwarze 143: }
1.9 schwarze 144: close $in;
145: $csv_account or die "no ACCOUNT line in $fn";
1.13 schwarze 146:
147: # Parse the CSV file from the bank.
1.21 schwarze 148: if (@ARGV) {
149: open STDIN, '<', $ARGV[0] or die "$ARGV[0]: $!";
150: }
151: LINE: while (<STDIN>) {
1.13 schwarze 152: chomp;
1.28 ! freda 153: s/\s*$//;
1.13 schwarze 154: next if (/^$/);
1.20 freda 155: foreach my $ignore (@ignored) {
156: next LINE if /$ignore/;
157: }
1.13 schwarze 158: my $line = $_;
1.14 schwarze 159: if ($header) {
160: $line eq $header
161: or die "expected HEADER $header\nbut got $line";
162: undef $header;
163: next;
164: }
1.18 freda 165: my $copy_line = $line;
166: my @fields;
167: if ($quote) {
168: push @fields, $1 while $copy_line =~ s/$quote([^$quote]*)$quote$delim?//;
169: } else {
170: @fields = split /$delim/, $line;
171: }
1.13 schwarze 172: my $matches = 0;
173: my ($account, $booking);
174: foreach my $selector (@compiled) {
175: $matches = 1;
176: for (my $i = 0; $i <= $#{$selector->{re}}; $i++) {
177: next if $fields[$i] =~ $selector->{re}[$i];
178: $matches = 0;
179: last;
180: }
181: if ($matches) {
182: $account = $selector->{ac};
183: $booking = $selector->{id};
184: last;
185: }
186: }
187: $matches or die "unmatched CSV line: $line";
1.25 freda 188: my $date = $fields[$date_field] or
189: die "date parse error: $line";
1.27 schwarze 190: eval '$date =~ ' . $date_regex;
1.25 freda 191: foreach my $i (@amount_fields) {
192: if (defined($fields[$i])) {
193: $fields[$i] =~ s/,/./;
194: $fields[$i] =~ s/\$//;
195: $fields[$i] = "-$1" if $fields[$i] =~ /^\((\d+\.\d+)\)/;
196: }
197: }
1.26 schwarze 198: my $debit = $csv_account;
199: my $credit = $account;
200: my $amount = $fields[$amount_fields[-1]] || -$fields[$amount_fields[0]];
201: if ($amount < 0) {
202: $amount *= -1;
203: $credit = $csv_account;
204: $debit = $account;
205: }
206: $amount = sprintf "%.2f", $amount;
1.25 freda 207: my $description = join ' ', @fields[@description_fields];
1.13 schwarze 208: $date && $amount && $debit && $credit && $description
209: or die "import parse error: $line";
210: $description =~ s/#//g;
1.25 freda 211: $description .= " [$fields[$cost_center_field]]"
212: if $cost_center_field;
213: $description .= " quantity $fields[$quantity_field]"
214: if $quantity_field;
1.13 schwarze 215: print "$date $booking $debit $credit $amount $description\n";
216: }
CVSweb