RT# 74523 - added command line option to use a status table
[freeside.git] / FS / FS / cdr / Import.pm
index a97472e..35069ae 100644 (file)
@@ -22,10 +22,12 @@ FS::cdr::Import - CDR importing
     'dbd'         => 'mysql', #Pg, Sybase, etc.
     'table'       => 'TABLE_NAME',
     'primary_key' => 'BILLING_ID',
+    'status_table' = > 'STATUS_TABLE_NAME', # if using a table rather than field in main table
     'column_map'  => { #freeside => remote_db
       'freeside_column' => 'remote_db_column',
       'freeside_column' => sub { my $row = shift; $row->{remote_db_column}; },
     },
+    'batch_name' => 'batch_name', # cdr_batch name -import-date gets appended.
   );
 
 =head1 DESCRIPTION
@@ -34,7 +36,7 @@ CDR importing
 
 =head1 CLASS METHODS
 
-=item do_cli_import
+=item dbi_import
 
 =cut
 
@@ -43,14 +45,21 @@ sub dbi_import {
   my %args = @_; #args are specifed by the script using this sub
 
   my %opt; #opt is specified for each install / run of the script
-  getopts('H:U:P:D:T:c:L:', \%opt);
+  getopts('H:U:P:D:T:c:L:S:', \%opt);
   my $user = shift(@ARGV) or die $class->cli_usage;
 
   $opt{D} ||= $args{database};
 
+  #do we want to add more types? or add as we go?
+  my %dbi_connect_types = {
+    'Sybase'  => ':server',
+    'Pg'      => ':host',
+  };
+
   my $dsn = 'dbi:'. $args{dbd};
-  #$dsn .= ":host=$opt{H}"; #if $opt{H};
-  $dsn .= ":server=$opt{H}"; #if $opt{H};
+
+  my $dbi_connect_type = $dbi_connect_types{$args{'dbd'}} ? $dbi_connect_types{$args{'dbd'}} : ':host';
+  $dsn .= $dbi_connect_type . "=$opt{H}";
   $dsn .= ";database=$opt{D}" if $opt{D};
 
   my $dbi = DBI->connect($dsn, $opt{U}, $opt{P}) 
@@ -82,9 +91,9 @@ sub dbi_import {
 
   #my @cols = values %{ $args{column_map} };
   my $sql = "SELECT $table.* FROM $table "; # join(',', @cols). " FROM $table ".
-  $sql .=  'LEFT JOIN '. $args{status_table}.
-           " ON ( $table.$pkey = ". $args{status_table}. ".$pkey )"
-    if $args{status_table};
+  $sql .=  'LEFT JOIN '. $opt{S}.
+           " ON ( $table.$pkey = ". $opt{S}. ".$pkey )"
+    if $opt{S};
   $sql .= ' WHERE freesidestatus IS NULL ';
 
   #$sql .= ' LIMIT '. $opt{L} if $opt{L};
@@ -93,7 +102,7 @@ sub dbi_import {
   #MySQL-specific print "Importing ".$sth->rows." records...\n";
 
   my $cdr_batch = new FS::cdr_batch({ 
-      'cdrbatch' => 'IVR-import-'. time2str('%Y/%m/%d-%T',time),
+      'cdrbatch' => $args{batch_name} . '-import-'. time2str('%Y/%m/%d-%T',time),
     });
   my $error = $cdr_batch->insert;
   die $error if $error;
@@ -132,10 +141,10 @@ sub dbi_import {
       $imported++;
 
       my $st_sql;
-      if ( $args{status_table} ) {
+      if ( $opt{S} ) {
 
         $st_sql = 
-          'INSERT INTO '. $args{status_table}. " ( $pkey, freesidestatus ) ".
+          'INSERT INTO '. $opt{S}. " ( $pkey, freesidestatus ) ".
             " VALUES ( ?, 'done' )";
 
       } else {
@@ -166,7 +175,7 @@ sub dbi_import {
 sub cli_usage {
   #"Usage: \n  $0\n\t[ -H hostname ]\n\t-D database\n\t-U user\n\t-P password\n\tfreesideuser\n";
   #"Usage: \n  $0\n\t-H hostname\n\t-D database\n\t-U user\n\t-P password\n\t[ -c cdrtypenum ]\n\tfreesideuser\n";
-  "Usage: \n  $0\n\t-H hostname\n\t[ -D database ]\n\t-U user\n\t-P password\n\t[ -c cdrtypenum ]\n\t[ -L num_cdrs_limit ]\n\tfreesideuser\n";
+  "Usage: \n  $0\n\t-H hostname\n\t[ -D database ]\n\t-U user\n\t-P password\n\t[ -c cdrtypenum ]\n\t[ -L num_cdrs_limit ]\n\t[ -T table ]\n\t[ -S status table ]\n\tfreesideuser\n";
 }
 
 =head1 BUGS