version 1.6, 2020/11/10 17:25:58 |
version 1.9, 2020/11/12 13:08:13 |
|
|
use warnings; |
use warnings; |
use strict; |
use strict; |
|
|
# === SUBROUTINES ===================================================== |
use Getopt::Std qw(getopts); |
|
|
sub parse_import_file ($) { |
our ($opt_I); |
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/) { |
|
$csv_account = $line; |
|
$csv_account =~ s/^ACCOUNT\s+(\d+)$/$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; |
|
die "CSV account not defined in $fn" if ($csv_account eq ""); |
|
return (\%bookings, \%accounts, \@compiled, $csv_account, $import_type); |
|
} |
|
|
|
sub import_chase_credit_card ($$$$) { |
# === SUBROUTINES ===================================================== |
|
|
|
sub import_chase_credit ($$$$) { |
my ($bookings_ref, $accounts_ref, $compiled_ref, |
my ($bookings_ref, $accounts_ref, $compiled_ref, |
$csv_account) = @_; |
$csv_account) = @_; |
my ($trans_date, $post_date, $description, $category, |
my ($trans_date, $post_date, $description, $category, |
Line 94 sub import_chase_credit_card ($$$$) { |
|
Line 63 sub import_chase_credit_card ($$$$) { |
|
} |
} |
} |
} |
|
|
sub import_capital_one_credit_card ($$$$) { |
sub import_capital_one_credit ($$$$) { |
my ($bookings_ref, $accounts_ref, $compiled_ref, |
my ($bookings_ref, $accounts_ref, $compiled_ref, |
$csv_account) = @_; |
$csv_account) = @_; |
my ($trans_date, $post_date, $card_num, $description, |
my ($trans_date, $post_date, $card_num, $description, |
Line 134 sub import_capital_one_credit_card ($$$$) { |
|
Line 103 sub import_capital_one_credit_card ($$$$) { |
|
"$description\n"; |
"$description\n"; |
} |
} |
} |
} |
# === MAIN PROGRAM ===================================================== |
|
|
|
# This begins the user-editable section to select the formatting file. |
sub import_optum_hsa { |
my $fn = "import_capital_one_credit.txt"; |
my ($bookings_ref, $accounts_ref, $compiled_ref, |
# my $fn = "import_chase_credit.txt"; |
$csv_account) = @_; |
# This ends the user-editable section to select the formatting file. |
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) = |
sub usage () { |
parse_import_file $fn; |
printf STDERR "usage: %s -I bankname csvfilename\n", $0; |
if ($import_type eq "chase_credit") { |
exit 1; |
import_chase_credit_card $bookings_ref, $accounts_ref, |
} |
$compiled_ref, $csv_account; |
|
} elsif ($import_type eq "capital_one_credit") { |
# === MAIN PROGRAM ===================================================== |
import_capital_one_credit_card $bookings_ref, $accounts_ref, |
|
$compiled_ref, $csv_account; |
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 { |
} 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); |