version 1.26, 2020/11/30 14:38:35 |
version 1.30, 2021/05/19 21:43:47 |
Line 21 use Getopt::Std qw(getopts); |
|
Line 21 use Getopt::Std qw(getopts); |
|
|
|
our ($opt_I); |
our ($opt_I); |
|
|
my %banks = ( |
my %date_formats = ( |
"bbva_usa" => \&import_bbva_usa, |
'MM/DD/YYYY' => 's#(\d+)/(\d+)/(\d+)#$3$1$2#', |
"capital_one_360" => \&import_capital_one_360, |
'MM/DD/YY' => 's#(\d+)/(\d+)/(\d+)#20$3$1$2#', |
"capital_one_credit" => \&import_capital_one_credit, |
'YYYY-MM-DD' => 's#(\d+)-(\d+)-(\d+)#$1$2$3#', |
"chase_credit" => \&import_chase_credit, |
'DD.MM.YY' => 's#(\d+)\.(\d+)\.(\d+)#20$3$2$1#', |
"etrade_ira" => \&import_etrade_ira, |
|
"optum_hsa" => \&import_optum_hsa, |
|
"sparkasse_camt" => \&import_sparkasse_camt, |
|
"wellsfargo" => \&import_wellsfargo, |
|
); |
); |
|
|
# === SUBROUTINES ===================================================== |
# === SUBROUTINES ===================================================== |
Line 47 unless ($opt_I) { |
|
Line 43 unless ($opt_I) { |
|
usage; |
usage; |
} |
} |
my $account_name = $opt_I; |
my $account_name = $opt_I; |
$banks{$account_name} or die "unknown accountname: $account_name"; |
|
|
|
# Parse the configuration file. |
# Parse the configuration file. |
my $fn = "import/" . $account_name . ".txt"; |
my $fn = "import/" . $account_name . ".txt"; |
open my $in, '<', $fn or die "$fn: $!"; |
open my $in, '<', $fn or die "$fn: $!"; |
my ($cost_center_field, $csv_account, $date_field, $date_format, |
my ($cost_center_field, $csv_account, $date_field, $date_regex, |
$delim, $header, $quantity_field, $quote, @amount_fields, |
$delim, $header, $quantity_field, $quote, @amount_fields, |
@compiled, @description_fields, @ignored); |
@compiled, @description_fields, @ignored); |
while (<$in>) { |
while (<$in>) { |
chomp; |
chomp; |
|
s/\s+$//; |
next if /^(?:#|$)/; |
next if /^(?:#|$)/; |
my $line = $_; |
my $line = $_; |
if (/^ACCOUNT\s+(\S+)$/) { |
if (s/^ACCOUNT\s+//) { |
$csv_account and die "duplicate ACCOUNT line: $1"; |
$csv_account and die "duplicate ACCOUNT line: $_"; |
|
/^(\d+)$/ or die "ACCOUNT parse error: $_"; |
$csv_account = $1; |
$csv_account = $1; |
next; |
next; |
} |
} |
if (s/^AMOUNT\s+//) { |
if (s/^AMOUNT\s+//) { |
|
@amount_fields and die "duplicate AMOUNT line: $_"; |
push @amount_fields, $1 - 1 while s/(\d+)\s*//; |
push @amount_fields, $1 - 1 while s/(\d+)\s*//; |
$_ eq '' or die "trailing garbage: AMOUNT ... $_"; |
$_ eq '' or die "trailing garbage: AMOUNT ... $_"; |
next; |
next; |
} |
} |
if (/^COSTCENTER\s+(\d+)/) { |
if (s/^COSTCENTER\s+//) { |
|
$cost_center_field and |
|
die "duplicate COSTCENTER line: $_"; |
|
/^(\d+)$/ or die "COSTCENTER parse error: $_"; |
$cost_center_field = $1 - 1; |
$cost_center_field = $1 - 1; |
next; |
next; |
} |
} |
if (/^DATE\s+(\d+)\s+(\S+)/) { |
if (s/^DATE\s+//) { |
$date_field = $1 - 1; |
$date_field || $date_regex and |
$date_format = $2; |
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; |
next; |
} |
} |
if (/^DELIM\s+(\S)$/) { |
if (s/^DELIM\s+//) { |
$delim and die "duplicate DELIM line: $1"; |
$delim and die "duplicate DELIM line: $1"; |
|
/^([^|\^\$\*\+\?\(\)\[\]\{\}\\])$/ or |
|
die "DELIM parse error: $_"; |
$delim = $1; |
$delim = $1; |
next; |
next; |
} |
} |
if (s/^DESCRIPTION\s+//) { |
if (s/^DESCRIPTION\s+//) { |
push @description_fields, $1 - 1 while s/(\d+)\s*//; |
@description_fields and die "duplicate DESCRIPTION line: $_"; |
$_ eq '' or die "trailing garbage: DESCRIPTION ... $_"; |
push @description_fields, $1 - 1 while s/(\d+)\s*//; |
next; |
$_ eq '' or die "trailing garbage: DESCRIPTION ... $_"; |
|
next; |
} |
} |
if (/^HEADER\s+(.*)$/) { |
if (s/^HEADER\s+//) { |
$header and die "duplicate HEADER line: $1"; |
$header and die "duplicate HEADER line: $1"; |
$header = $1; |
$header = $_; |
next; |
next; |
} |
} |
if (/^IGNORE\s+(.*)/) { |
if (s/^IGNORE\s+//) { |
push @ignored, qr/$1/; |
push @ignored, qr/$_/; |
next; |
next; |
} |
} |
if (/^QUANTITY\s+(\d+)/) { |
if (s/^QUANTITY\s+//) { |
|
$quantity_field and die "duplicate QUANTITY line: $1"; |
|
/^(\d+)$/ or die "QUANTITY parse error: $_"; |
$quantity_field = $1 - 1; |
$quantity_field = $1 - 1; |
next; |
next; |
} |
} |
if (/^QUOTE\s+(\S)$/) { |
if (s/^QUOTE\s+//) { |
$quote and die "duplicate QUOTE line: $1"; |
$quote and die "duplicate QUOTE line: $1"; |
|
/^([^|\^\$\*\+\?\(\)\[\]\{\}\\])$/ or |
|
die "QUOTE parse error: $_"; |
$quote = $1; |
$quote = $1; |
next; |
next; |
} |
} |
|
|
} |
} |
LINE: while (<STDIN>) { |
LINE: while (<STDIN>) { |
chomp; |
chomp; |
|
s/\s*$//; |
next if (/^$/); |
next if (/^$/); |
foreach my $ignore (@ignored) { |
foreach my $ignore (@ignored) { |
next LINE if /$ignore/; |
next LINE if /$ignore/; |
Line 160 LINE: while (<STDIN>) { |
|
Line 175 LINE: while (<STDIN>) { |
|
$matches or die "unmatched CSV line: $line"; |
$matches or die "unmatched CSV line: $line"; |
my $date = $fields[$date_field] or |
my $date = $fields[$date_field] or |
die "date parse error: $line"; |
die "date parse error: $line"; |
if ($date_format eq "MM/DD/YYYY") { |
eval '$date =~ ' . $date_regex; |
$date =~ s#(\d+)/(\d+)/(\d+)#$3$1$2#; |
|
} elsif ($date_format eq "MM/DD/YY") { |
|
$date =~ s#(\d+)/(\d+)/(\d+)#20$3$1$2#; |
|
} elsif ($date_format eq "YYYY-MM-DD") { |
|
$date =~ s#(\d+)-(\d+)-(\d+)#$1$2$3#; |
|
} elsif ($date_format eq "DD.MM.YY") { |
|
$date =~ s#(\d+)\.(\d+)\.(\d+)#20$3$2$1#; |
|
} else { |
|
die "unhandled date format: $line"; |
|
} |
|
foreach my $i (@amount_fields) { |
foreach my $i (@amount_fields) { |
if (defined($fields[$i])) { |
if (defined($fields[$i])) { |
$fields[$i] =~ s/,/./; |
$fields[$i] =~ s/,/./; |
Line 188 LINE: while (<STDIN>) { |
|
Line 193 LINE: while (<STDIN>) { |
|
} |
} |
$amount = sprintf "%.2f", $amount; |
$amount = sprintf "%.2f", $amount; |
my $description = join ' ', @fields[@description_fields]; |
my $description = join ' ', @fields[@description_fields]; |
$date && $amount && $debit && $credit && $description |
defined($date) && defined($amount) && defined($debit) && |
|
defined($credit) && defined($description) |
or die "import parse error: $line"; |
or die "import parse error: $line"; |
$description =~ s/#//g; |
$description =~ s/#//g; |
$description .= " [$fields[$cost_center_field]]" |
$description .= " [$fields[$cost_center_field]]" |