=================================================================== RCS file: /cvs/pta/pta_import.pl,v retrieving revision 1.8 retrieving revision 1.9 diff -u -p -r1.8 -r1.9 --- pta/pta_import.pl 2020/11/10 17:42:32 1.8 +++ pta/pta_import.pl 2020/11/12 13:08:13 1.9 @@ -17,44 +17,13 @@ use warnings; use strict; -# === SUBROUTINES ===================================================== +use Getopt::Std qw(getopts); -sub parse_import_file ($) { - my $fn = shift; - my (@compiled, %bookings, %accounts, $import_type); - my $csv_account; - if ($fn eq "import_chase_credit.txt") { - $import_type = "chase_credit"; - } elsif ($fn eq "import_capital_one_credit.txt") { - $import_type = "capital_one_credit"; - } else { - die "Undefined import type for: $fn"; - } - open my $in, $fn or die "$fn: $!"; - while (<$in>) { - chomp; - next if /^(?:#|$)/; - my $line = $_; - if (/^ACCOUNT\s+(\d+)$/) { - $csv_account and die "duplicate ACCOUNT line: $1"; - $csv_account = $1; - next; - } - 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; - } - close $in; - $csv_account or die "no ACCOUNT line in $fn"; - return (\%bookings, \%accounts, \@compiled, $csv_account, $import_type); -} +our ($opt_I); -sub import_chase_credit_card ($$$$) { +# === SUBROUTINES ===================================================== + +sub import_chase_credit ($$$$) { my ($bookings_ref, $accounts_ref, $compiled_ref, $csv_account) = @_; my ($trans_date, $post_date, $description, $category, @@ -94,7 +63,7 @@ sub import_chase_credit_card ($$$$) { } } -sub import_capital_one_credit_card ($$$$) { +sub import_capital_one_credit ($$$$) { my ($bookings_ref, $accounts_ref, $compiled_ref, $csv_account) = @_; my ($trans_date, $post_date, $card_num, $description, @@ -134,21 +103,83 @@ sub import_capital_one_credit_card ($$$$) { "$description\n"; } } -# === MAIN PROGRAM ===================================================== -# This begins the user-editable section to select the formatting file. -my $fn = "import_capital_one_credit.txt"; -# my $fn = "import_chase_credit.txt"; -# This ends the user-editable section to select the formatting file. +sub import_optum_hsa { + my ($bookings_ref, $accounts_ref, $compiled_ref, + $csv_account) = @_; + my ($date, $description, $amount, + $type); + chomp(my $header = <>); + while (<>) { + next if (/^$/); + chomp; + s#(\d+)-(\d+)-(\d+)#$1$2$3#; + my $line = $_; + ($date, $description, $amount, + $type) = split /,/; + $description =~ s/#//g; + $amount =~ s/\$//; + my ($debit, $credit); + my $booking = ""; + my $account = ""; + foreach my $regex (@$compiled_ref) { + if ($line =~ /$regex/) { + $account = %$accounts_ref{$regex}; + $booking = %$bookings_ref{$regex}; + last; + } + } + if ($booking eq "" || $account eq "") { + die "import parse error: $line"; + } + if ($amount <= 0) { + $amount = substr $amount, 1; + $credit = $csv_account; + $debit = $account; + } else { + $debit = $csv_account; + $credit = $account; + } + print "$date $booking $debit $credit $amount " . + "$description\n"; + } +} -my ($bookings_ref, $accounts_ref, $compiled_ref, $csv_account, $import_type) = - parse_import_file $fn; -if ($import_type eq "chase_credit") { - import_chase_credit_card $bookings_ref, $accounts_ref, - $compiled_ref, $csv_account; -} elsif ($import_type eq "capital_one_credit") { - import_capital_one_credit_card $bookings_ref, $accounts_ref, - $compiled_ref, $csv_account; +sub usage () { + printf STDERR "usage: %s -I bankname csvfilename\n", $0; + exit 1; +} + +# === MAIN PROGRAM ===================================================== + +my ($csv_account, $fn, $in, $sub_name, %accounts, %bookings, @compiled); +getopts 'I:' or usage; +if ($opt_I) { + $sub_name = "import_" . $opt_I; + $fn = $sub_name . ".txt"; + open $in, $fn or die "$fn: $!"; } else { - die "Undefined import type: $import_type"; + usage; } +while (<$in>) { + chomp; + next if /^(?:#|$)/; + my $line = $_; + if (/^ACCOUNT\s+(\d+)$/) { + $csv_account and die "duplicate ACCOUNT line: $1"; + $csv_account = $1; + next; + } + 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; +} +close $in; +$csv_account or die "no ACCOUNT line in $fn"; +my $sub_ref = \&$sub_name; +&$sub_ref(\%bookings, \%accounts, \@compiled, $csv_account);