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