[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.9 and 1.10

version 1.9, 2020/11/12 13:08:13 version 1.10, 2020/11/12 21:54:23
Line 23  our ($opt_I);
Line 23  our ($opt_I);
   
 # === SUBROUTINES  =====================================================  # === SUBROUTINES  =====================================================
   
 sub import_chase_credit ($$$$) {  sub import_account ($$$$$) {
         my ($bookings_ref, $accounts_ref, $compiled_ref,          my ($account_name, $accounts_ref, $bookings_ref,
             $csv_account) = @_;              $compiled_ref, $csv_account) = @_;
         my ($trans_date, $post_date, $description, $category,          my ($account, $amount, $booking, $credit, $date, $debit,
             $type, $amount);              $description);
         chomp(my $header = <>);          chomp(my $header = <>);
         while (<>) {          while (<>) {
                 next if (/^$/);                  next if (/^$/);
                 chomp;                  chomp;
                 s#(\d+)/(\d+)/(\d+)#$3$1$2#g;  
                 my $line = $_;                  my $line = $_;
                 ($trans_date, $post_date, $description, $category,  
                     $type, $amount) = split /,/;  
                 $description =~ s/#//g;  
                 my ($debit, $credit);  
                 my $booking = "";  
                 my $account = "";  
                 foreach my $regex (@$compiled_ref) {                  foreach my $regex (@$compiled_ref) {
                         if ($line =~ /$regex/) {                          if ($line =~ /$regex/) {
                                 $account = %$accounts_ref{$regex};                                  $account = %$accounts_ref{$regex};
Line 47  sub import_chase_credit ($$$$) {
Line 40  sub import_chase_credit ($$$$) {
                                 last;                                  last;
                         }                          }
                 }                  }
                 if ($booking eq "" || $account eq "") {                  if ($account_name eq "chase_credit") {
                         die "import parse error: $line";                          ($date, $amount, $debit, $credit,
                 }                              $description) =
                 if ($amount <= 0) {                              import_chase_credit($account,
                         $amount = substr $amount, 1;                              $csv_account, $line);
                         $credit = $csv_account;                  } elsif ($account_name eq "capital_one_credit") {
                         $debit = $account;                          ($date, $amount, $debit, $credit,
                               $description) =
                               import_capital_one_credit($account,
                               $csv_account, $line);
                   } elsif ($account_name eq "optum_hsa") {
                           ($date, $amount, $debit, $credit,
                               $description) =
                               import_optum_hsa($account,
                               $csv_account, $line);
                 } else {                  } else {
                         $debit = $csv_account;                          die "undefined format: $account_name";
                         $credit = $account;  
                 }                  }
                 print "$post_date $booking $debit $credit $amount " .                  unless ($date && $booking && $debit && $credit &&
                     "$description\n";                      $amount && $description) {
         }  
 }  
   
 sub import_capital_one_credit ($$$$) {  
         my ($bookings_ref, $accounts_ref, $compiled_ref,  
             $csv_account) = @_;  
         my ($trans_date, $post_date, $card_num, $description,  
             $category, $csv_debit, $csv_credit, $amount);  
         chomp(my $header = <>);  
         while (<>) {  
                 next if (/^$/);  
                 chomp;  
                 s/(\d+)-(\d+)-(\d+)/$1$2$3/g;  
                 my $line = $_;  
                 ($trans_date, $post_date, $card_num, $description,  
                     $category, , $csv_debit, $csv_credit) = split /,/;  
                 $description =~ s/#//g;  
                 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";                          die "import parse error: $line";
                 }                  }
                 if ($csv_debit =~ /^$/) {                  $description =~ s/#//g;
                         $amount = $csv_credit;                  print "$date $booking $debit $credit $amount " .
                         $credit = $account;  
                         $debit = $csv_account;  
                 } else {  
                         $amount = $csv_debit;  
                         $credit = $csv_account;  
                         $debit = $account;  
                 }  
                 print "$post_date $booking $debit $credit $amount " .  
                     "$description\n";                      "$description\n";
         }          }
 }  }
   
 sub import_optum_hsa {  sub import_chase_credit ($$$) {
         my ($bookings_ref, $accounts_ref, $compiled_ref,          my ($account, $csv_account, $line) = @_;
             $csv_account) = @_;          my ($trans_date, $post_date, $description, $category,
               $type, $amount) = split /,/, $line;
           my ($debit, $credit);
           $post_date =~ s#(\d+)/(\d+)/(\d+)#$3$1$2#;
           ($amount, $debit, $credit) =
               get_accounts_by_amount_sign($amount, $account,
               $csv_account);
           return ($post_date, $amount, $debit, $credit, $description);
   }
   
   sub import_capital_one_credit ($$$) {
           my ($account, $csv_account, $line) = @_;
           my ($trans_date, $post_date, $card_num,
               $description, $category, $csv_debit,
               $csv_credit) = split /,/, $line;
           $post_date =~ s/(\d+)-(\d+)-(\d+)/$1$2$3/;
           my ($amount, $debit, $credit) =
               get_accounts_by_csv_col($account, $csv_account,
               $csv_debit, $csv_credit);
           return ($post_date, $amount, $debit, $credit, $description);
   }
   
   sub import_optum_hsa ($$$) {
           my ($account, $csv_account, $line) = @_;
         my ($date, $description, $amount,          my ($date, $description, $amount,
             $type);              $type) = split /,/, $line;
         chomp(my $header = <>);          my ($debit, $credit);
         while (<>) {          $date =~ s/(\d+)-(\d+)-(\d+)/$1$2$3/;
                 next if (/^$/);          $amount =~ s/\$//;
                 chomp;          ($amount, $debit, $credit) =
                 s#(\d+)-(\d+)-(\d+)#$1$2$3#;              get_accounts_by_amount_sign($amount, $account,
                 my $line = $_;              $csv_account);
                 ($date, $description, $amount,          return ($date, $amount, $debit, $credit, $description);
                     $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";  
         }  
 }  }
   
   sub get_accounts_by_amount_sign ($$$) {
           my ($amount, $account, $csv_account) = @_;
           my ($debit, $credit);
           if ($amount <= 0) {
                   $amount = substr $amount, 1;
                   $credit = $csv_account;
                   $debit = $account;
           } else {
                   $debit = $csv_account;
                   $credit = $account;
           }
           return ($amount, $debit, $credit);
   }
   
   sub get_accounts_by_csv_col ($$$$) {
           my ($account, $csv_account, $csv_debit, $csv_credit) = @_;
           my ($amount, $debit, $credit);
           if ($csv_debit eq "") {
                   $amount = $csv_credit;
                   $credit = $account;
                   $debit = $csv_account;
           } else {
                   $amount = $csv_debit;
                   $credit = $csv_account;
                   $debit = $account;
           }
           return ($amount, $debit, $credit);
   }
   
 sub usage () {  sub usage () {
         printf STDERR "usage: %s -I bankname csvfilename\n", $0;          printf STDERR "usage: %s -I accountname csvfilename\n", $0;
         exit 1;          exit 1;
 }  }
   
 # === MAIN PROGRAM =====================================================  # === MAIN PROGRAM =====================================================
   
 my ($csv_account, $fn, $in, $sub_name, %accounts, %bookings, @compiled);  my ($csv_account, $fn, $in, $account_name, %accounts, %bookings, @compiled);
 getopts 'I:' or usage;  getopts 'I:' or usage;
 if ($opt_I) {  if ($opt_I) {
         $sub_name = "import_" . $opt_I;          $account_name = $opt_I;
         $fn = $sub_name . ".txt";          $fn = "import_" . $account_name . ".txt";
         open $in, $fn or die "$fn: $!";          open $in, $fn or die "$fn: $!";
 } else {  } else {
         usage;          usage;
Line 181  while (<$in>) {
Line 170  while (<$in>) {
 }  }
 close $in;  close $in;
 $csv_account or die "no ACCOUNT line in $fn";  $csv_account or die "no ACCOUNT line in $fn";
 my $sub_ref = \&$sub_name;  import_account($account_name, \%accounts, \%bookings,
 &$sub_ref(\%bookings, \%accounts, \@compiled, $csv_account);      \@compiled, $csv_account);

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

CVSweb