=================================================================== RCS file: /cvs/pta/pta_import.pl,v retrieving revision 1.3 retrieving revision 1.30 diff -u -p -r1.3 -r1.30 --- pta/pta_import.pl 2020/11/02 19:36:53 1.3 +++ pta/pta_import.pl 2021/05/19 21:43:47 1.30 @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/perl # Copyright (c) 2020 Freda Bundchen @@ -17,68 +17,189 @@ use warnings; use strict; +use Getopt::Std qw(getopts); + +our ($opt_I); + +my %date_formats = ( + 'MM/DD/YYYY' => 's#(\d+)/(\d+)/(\d+)#$3$1$2#', + 'MM/DD/YY' => 's#(\d+)/(\d+)/(\d+)#20$3$1$2#', + 'YYYY-MM-DD' => 's#(\d+)-(\d+)-(\d+)#$1$2$3#', + 'DD.MM.YY' => 's#(\d+)\.(\d+)\.(\d+)#20$3$2$1#', +); + # === SUBROUTINES ===================================================== -sub import_chase_credit_card { - my $csv_account = "1234"; # DEBUG. hard-coded for now - my $fn = 'import_chase_credit.txt'; - my (@compiled, %bookings, %accounts); - open my $in, $fn or die "$fn: $!"; - while (<$in>) { - chomp; - next if /^(?:#|$)/; - my $line = $_; - s/^(.*),\s+(\d+)\s+(\S+)// or - die "$fn import parse error: $line"; - my ($reg, $account, $booking) = ($1, $2, $3); - $reg =~ s/,,/,.*?/g; - $reg = qr/$reg/; - push @compiled, $reg; - $bookings{$reg} = $booking; - $accounts{$reg} = $account; +sub usage () { + printf STDERR "usage: %s -I accountname csvfilename\n", $0; + exit 1; +} + +# === MAIN PROGRAM ===================================================== + +getopts 'I:' or usage; +unless ($opt_I) { + warn "The option -I is required."; + usage; +} +my $account_name = $opt_I; + +# Parse the configuration file. +my $fn = "import/" . $account_name . ".txt"; +open my $in, '<', $fn or die "$fn: $!"; +my ($cost_center_field, $csv_account, $date_field, $date_regex, + $delim, $header, $quantity_field, $quote, @amount_fields, + @compiled, @description_fields, @ignored); +while (<$in>) { + chomp; + s/\s+$//; + next if /^(?:#|$)/; + my $line = $_; + if (s/^ACCOUNT\s+//) { + $csv_account and die "duplicate ACCOUNT line: $_"; + /^(\d+)$/ or die "ACCOUNT parse error: $_"; + $csv_account = $1; + next; } - close $in; - while (<>) { - chomp; - s#(\d+)/(\d+)/(\d+)#$3$1$2#g; - my $line = $_; - my ($trans_date, $post_date, $description, $category, - $type, $amount) = split /,/; - $description =~ s/#//g; - my ($debit, $credit); - my $booking = ""; - my $account = ""; - foreach my $regex (@compiled) { - if ($line =~ /$regex/) { - $account = $accounts{$regex}; - $booking = $bookings{$regex}; - next; - } + if (s/^AMOUNT\s+//) { + @amount_fields and die "duplicate AMOUNT line: $_"; + push @amount_fields, $1 - 1 while s/(\d+)\s*//; + $_ eq '' or die "trailing garbage: AMOUNT ... $_"; + next; + } + if (s/^COSTCENTER\s+//) { + $cost_center_field and + die "duplicate COSTCENTER line: $_"; + /^(\d+)$/ or die "COSTCENTER parse error: $_"; + $cost_center_field = $1 - 1; + next; + } + if (s/^DATE\s+//) { + $date_field || $date_regex and + die "duplicate DATE line: $_"; + s/^(\d+)\s+(\S+)$//; + $date_field = $1 or + die "DATE date_field parse error: $_"; + $date_field -= 1; + $date_regex = $date_formats{$2} + or die "unknown date format: $2"; + next; + } + if (s/^DELIM\s+//) { + $delim and die "duplicate DELIM line: $1"; + /^([^|\^\$\*\+\?\(\)\[\]\{\}\\])$/ or + die "DELIM parse error: $_"; + $delim = $1; + next; + } + if (s/^DESCRIPTION\s+//) { + @description_fields and die "duplicate DESCRIPTION line: $_"; + push @description_fields, $1 - 1 while s/(\d+)\s*//; + $_ eq '' or die "trailing garbage: DESCRIPTION ... $_"; + next; + } + if (s/^HEADER\s+//) { + $header and die "duplicate HEADER line: $1"; + $header = $_; + next; + } + if (s/^IGNORE\s+//) { + push @ignored, qr/$_/; + next; + } + if (s/^QUANTITY\s+//) { + $quantity_field and die "duplicate QUANTITY line: $1"; + /^(\d+)$/ or die "QUANTITY parse error: $_"; + $quantity_field = $1 - 1; + next; + } + if (s/^QUOTE\s+//) { + $quote and die "duplicate QUOTE line: $1"; + /^([^|\^\$\*\+\?\(\)\[\]\{\}\\])$/ or + die "QUOTE parse error: $_"; + $quote = $1; + next; + } + $delim or die "no DELIM line in $fn"; + s/^(.*)$delim\s+(\d+)\s+(\S+)// or + die "$fn import parse error: $line"; + push @compiled, { + re => [map { qr/$_/ } split /$delim/, $1], + ac => $2, + id => $3, + }; +} +close $in; +$csv_account or die "no ACCOUNT line in $fn"; + +# Parse the CSV file from the bank. +if (@ARGV) { + open STDIN, '<', $ARGV[0] or die "$ARGV[0]: $!"; +} +LINE: while () { + chomp; + s/\s*$//; + next if (/^$/); + foreach my $ignore (@ignored) { + next LINE if /$ignore/; + } + my $line = $_; + if ($header) { + $line eq $header + or die "expected HEADER $header\nbut got $line"; + undef $header; + next; + } + my $copy_line = $line; + my @fields; + if ($quote) { + push @fields, $1 while $copy_line =~ s/$quote([^$quote]*)$quote$delim?//; + } else { + @fields = split /$delim/, $line; + } + my $matches = 0; + my ($account, $booking); + foreach my $selector (@compiled) { + $matches = 1; + for (my $i = 0; $i <= $#{$selector->{re}}; $i++) { + next if $fields[$i] =~ $selector->{re}[$i]; + $matches = 0; + last; } - if ($booking eq "" || $account eq "") { - die "$fn import parse error: $line"; + if ($matches) { + $account = $selector->{ac}; + $booking = $selector->{id}; + last; } - if ($amount <= 0) { - $amount = substr $amount, 1; - $credit = $csv_account; - $debit = $account; - } else { - $debit = $csv_account; - $credit = $account; + } + $matches or die "unmatched CSV line: $line"; + my $date = $fields[$date_field] or + die "date parse error: $line"; + eval '$date =~ ' . $date_regex; + foreach my $i (@amount_fields) { + if (defined($fields[$i])) { + $fields[$i] =~ s/,/./; + $fields[$i] =~ s/\$//; + $fields[$i] = "-$1" if $fields[$i] =~ /^\((\d+\.\d+)\)/; } - print "$post_date $booking $debit $credit $amount " . - "$description\n"; } -} - -# === MAIN PROGRAM ===================================================== - -my ($chase_credit_card_header, $firstLine); -$chase_credit_card_header = "Transaction Date,Post Date," . - "Description,Category,Type,Amount"; -chomp($firstLine = <>); -if ($firstLine eq $chase_credit_card_header) { - import_chase_credit_card; -} else { - print "Undefined header\n"; + my $debit = $csv_account; + my $credit = $account; + my $amount = $fields[$amount_fields[-1]] || -$fields[$amount_fields[0]]; + if ($amount < 0) { + $amount *= -1; + $credit = $csv_account; + $debit = $account; + } + $amount = sprintf "%.2f", $amount; + my $description = join ' ', @fields[@description_fields]; + defined($date) && defined($amount) && defined($debit) && + defined($credit) && defined($description) + or die "import parse error: $line"; + $description =~ s/#//g; + $description .= " [$fields[$cost_center_field]]" + if $cost_center_field; + $description .= " quantity $fields[$quantity_field]" + if $quantity_field; + print "$date $booking $debit $credit $amount $description\n"; }