=================================================================== RCS file: /cvs/pta/pta_import.pl,v retrieving revision 1.29 retrieving revision 1.32 diff -u -p -r1.29 -r1.32 --- pta/pta_import.pl 2021/05/19 17:10:53 1.29 +++ pta/pta_import.pl 2021/06/04 13:50:06 1.32 @@ -48,8 +48,8 @@ my $account_name = $opt_I; 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); + $delim, $header, $quantity_field, $quote, $subaccount, + @amount_fields, @compiled, @description_fields, @ignored); while (<$in>) { chomp; s/\s+$//; @@ -121,12 +121,13 @@ while (<$in>) { next; } $delim or die "no DELIM line in $fn"; - s/^(.*)$delim\s+(\d+)\s+(\S+)// or + s/^(.*)$delim\s+(\d+)\s+(\S+)\s*(\S*)// or die "$fn import parse error: $line"; push @compiled, { re => [map { qr/$_/ } split /$delim/, $1], ac => $2, id => $3, + su => $4, }; } close $in; @@ -152,10 +153,10 @@ LINE: while () { } my $copy_line = $line; my @fields; - if ($quote) { - push @fields, $1 while $copy_line =~ s/$quote([^$quote]*)$quote$delim?//; - } else { - @fields = split /$delim/, $line; + while ($copy_line =~ /./) { + $quote && $copy_line =~ s/^$quote(.*?)$quote(?:$delim|$)// + or $copy_line =~ s/^(.*?)(?:$delim|$)//; + push @fields, $1; } my $matches = 0; my ($account, $booking); @@ -169,6 +170,7 @@ LINE: while () { if ($matches) { $account = $selector->{ac}; $booking = $selector->{id}; + $subaccount = $selector->{su}; last; } } @@ -193,12 +195,16 @@ LINE: while () { } $amount = sprintf "%.2f", $amount; 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"; $description =~ s/#//g; $description .= " [$fields[$cost_center_field]]" if $cost_center_field; $description .= " quantity $fields[$quantity_field]" if $quantity_field; + $description .= " ($subaccount)" + if $subaccount; + print "$date $booking $debit $credit $amount $description\n"; }