RT# 74523 Added fusionpbx import script, updated Import.pm to handle different import...
[freeside.git] / FS / FS / cdr / Import.pm
index e788712..5046caf 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
 
@@ -48,9 +50,16 @@ sub dbi_import {
 
   $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}) 
@@ -81,7 +90,7 @@ sub dbi_import {
   #      )
 
   #my @cols = values %{ $args{column_map} };
-  my $sql = "SELECT * FROM $table "; # join(',', @cols). " FROM $table ".
+  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};
@@ -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;
@@ -117,13 +126,15 @@ sub dbi_import {
 
     $cdr->cdrtypenum($opt{c}) if $opt{c};
 
-    #print $row->{$pkey},"\n" if $opt{v};
+    my $pkey_value = $row->{$pkey};
+
+    #print "$pkey_value\n" if $opt{v};
     my $error = $cdr->insert;
 
     if ($error) {
 
-      #die $row->{$pkey} . ": failed import: $error\n";
-      print $row->{$pkey} . ": failed import: $error\n";
+      #die "$pkey_value: failed import: $error\n";
+      print "$pkey_value: failed import: $error\n";
 
     } else {
 
@@ -142,7 +153,7 @@ sub dbi_import {
 
       }
 
-      my $updated = $dbi->do($st_sql, undef, $row->{$pkey} );
+      my $updated = $dbi->do($st_sql, undef, $pkey_value );
       #$updates += $updated;
       die "failed to set status: ".$dbi->errstr."\n" unless $updated;