planet telecom rate import, RT#83146
[freeside.git] / bin / aradial-sftp_and_import
index cc2238e..e763963 100755 (executable)
@@ -4,9 +4,11 @@
 
 use strict;
 use Getopt::Std;
+use Date::Parse;
 use Date::Format;
 use Text::CSV_XS;
 use DBI qw( :sql_types );
+use FS::DBI;
 use Net::SFTP::Foreign;
 #use FS::UID qw( adminsuidsetup datasrc );
 
@@ -65,7 +67,7 @@ our %status_type = (
 ###
 
 use vars qw( $opt_m $opt_a $opt_b $opt_r $opt_d $opt_v $opt_P );
-getopts('m:abr:dP:v:');
+getopts('m:abr:d:P:v:');
 
 my %options = ();
 
@@ -80,8 +82,8 @@ mkdir $cachedir unless -d $cachedir;
 my $servername = shift or die &usage;
 
 my( $datasrc, $db_user, $db_pass ) = ( shift, shift, shift );
-my $dbh = DBI->connect( $datasrc, $db_user, $db_pass)
-  or die "can't connect: $DBI::errstr\n";
+my $dbh = FS::DBI->connect( $datasrc, $db_user, $db_pass)
+  or die "can't connect: $FS::DBI::errstr\n";
 
 my $csv = Text::CSV_XS->new;
 
@@ -127,6 +129,8 @@ else {
 
 foreach my $filename ( @$ls ) {
 
+  next if $opt_d && $filename eq $opt_d;
+
   warn "Downloading $filename\n" if $opt_v;
 
   #get the file
@@ -157,6 +161,11 @@ foreach my $filename ( @$ls ) {
 
     my @keys = keys %dbhash;
 
+    #skip blank records
+    next unless grep defined($_), values %dbhash;
+
+    my $date = time2str( '%Y-%m-%d %X', str2time( $hash{'Date'} ) );
+
     $hash{'Status-Type'} = $status_type{ $hash{'Status-Type'} }
       if exists $status_type{ $hash{'Status-Type'} };
 
@@ -164,15 +173,18 @@ foreach my $filename ( @$ls ) {
     my @extra_values = ();
     if ( $hash{'Status-Type'} eq 'Start' ) {
 
-      $dbhash{'AcctStartTime'} = $hash{'Date'};
+      push @keys, 'AcctStartTime';
+      $dbhash{'AcctStartTime'} = $date;
 
-      $sql = 'INSERT INTO radacct ( ', join(',', @keys).
-             ' ) VALUES ( '. map( ' ? ', @keys ). ' )';
+      $sql = 'INSERT INTO radacct ( '. join(',', @keys).
+             ' ) VALUES ( '. join(',', map ' ? ', @keys ). ' )';
 
     } elsif ( $hash{'Status-Type'} eq 'Stop' ) {
 
       my $AcctSessionId = delete($dbhash{AcctSessionId});
-      $dbhash{'AcctStopTime'} = $hash{'Date'};
+
+      push @keys, 'AcctStopTime';
+      $dbhash{'AcctStopTime'} = $date;
 
       push @extra_values, $AcctSessionId;
 
@@ -194,6 +206,7 @@ foreach my $filename ( @$ls ) {
     foreach my $value ( map $dbhash{$_}, @keys ) {
       my $key = shift @keys;
       my $type = exists($bind_type{$key}) ? $bind_type{$key} : SQL_VARCHAR;
+      $value ||= 0 if $type == SQL_INTEGER;
       $sth->bind_param($p_num++, $value, $type);
     }
     foreach my $value ( @extra_values ) {