[BACK]Return to pta_import.pl CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / pta

Diff for /pta/pta_import.pl between version 1.11 and 1.12

version 1.11, 2020/11/17 14:13:31 version 1.12, 2020/11/17 15:31:02
Line 23  our ($opt_I);
Line 23  our ($opt_I);
   
 # === SUBROUTINES  =====================================================  # === SUBROUTINES  =====================================================
   
 sub import_account ($$$$$) {  sub import_account ($$$$) {
         my ($account_name, $accounts_ref, $bookings_ref,          my ($account_name, $compiled_ref, $delim, $csv_account) = @_;
             $compiled_ref, $csv_account) = @_;  
         my ($account, $amount, $booking, $credit, $date, $debit,          my ($account, $amount, $booking, $credit, $date, $debit,
             $description);              $description);
         chomp(my $header = <>);          chomp(my $header = <>);
Line 33  sub import_account ($$$$$) {
Line 32  sub import_account ($$$$$) {
                 next if (/^$/);                  next if (/^$/);
                 chomp;                  chomp;
                 my $line = $_;                  my $line = $_;
                 foreach my $regex (@$compiled_ref) {                  my @fields = split /$delim/, $line;
                         if ($line =~ /$regex/) {                  my $matches = 0;
                                 $account = %$accounts_ref{$regex};                  foreach my $selector (@$compiled_ref) {
                                 $booking = %$bookings_ref{$regex};                          $matches = 1;
                           for (my $i = 0; $i <= $#{$selector->{re}}; $i++) {
                                   next if $fields[$i] =~ $selector->{re}[$i];
                                   $matches = 0;
                                 last;                                  last;
                         }                          }
                           if ($matches) {
                                   $account = $selector->{ac};
                                   $booking = $selector->{id};
                                   last;
                           }
                 }                  }
                   $matches or die "unmatched CSV line: $line";
                 if ($account_name eq "chase_credit") {                  if ($account_name eq "chase_credit") {
                         ($date, $amount, $debit, $credit,                          ($date, $amount, $debit, $credit,
                             $description) =                              $description) =
Line 161  sub usage () {
Line 169  sub usage () {
   
 # === MAIN PROGRAM =====================================================  # === MAIN PROGRAM =====================================================
   
 my ($csv_account, $fn, $in, $account_name, %accounts, %bookings, @compiled);  my ($csv_account, $fn, $in, $account_name, $delim, @compiled);
 getopts 'I:' or usage;  getopts 'I:' or usage;
 if ($opt_I) {  if ($opt_I) {
         $account_name = $opt_I;          $account_name = $opt_I;
Line 174  while (<$in>) {
Line 182  while (<$in>) {
         chomp;          chomp;
         next if /^(?:#|$)/;          next if /^(?:#|$)/;
         my $line = $_;          my $line = $_;
         if (/^ACCOUNT\s+(\d+)$/) {          if (/^ACCOUNT\s+(\S+)$/) {
                 $csv_account and die "duplicate ACCOUNT line: $1";                  $csv_account and die "duplicate ACCOUNT line: $1";
                 $csv_account = $1;                  $csv_account = $1;
                 next;                  next;
         }          }
         /^(.*)[,;]\s+(\d+)\s+(\S+)$/ or          if (/^DELIM\s+(\S)$/) {
             die "$fn import parse error: $line";                  $delim and die "duplicate DELIM line: $1";
         my ($reg, $account, $booking) = ($1, $2, $3);                  $delim = $1;
         if ($account_name eq 'sparkasse_camt') {                  next;
                 $reg =~ s/(?:^|(?<=;))(?:$|(?=;))/"[^"]*"/g;  
         } else {  
                 $reg =~ s/(?:^|(?<=,))(?:$|(?=,))/[^,]*/g;  
         }          }
         $reg = qr/$reg/;          $delim or die "no DELIM line in $fn";
         push @compiled, $reg;          s/^(.*)$delim\s+(\d+)\s+(\S+)// or
         $bookings{$reg} = $booking;              die "$fn import parse error: $line";
         $accounts{$reg} = $account;          push @compiled, {
                   re => [map { qr/$_/ } split /$delim/, $1],
                   ac => $2,
                   id => $3,
           };
 }  }
 close $in;  close $in;
 $csv_account or die "no ACCOUNT line in $fn";  $csv_account or die "no ACCOUNT line in $fn";
 import_account($account_name, \%accounts, \%bookings,  import_account($account_name, \@compiled, $delim, $csv_account);
     \@compiled, $csv_account);  

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.12

CVSweb