[freeside-commits] freeside/FS/FS Record.pm, 1.173, 1.174 cdr.pm, 1.26, 1.27 phone_avail.pm, 1.2, 1.3

Ivan,,, ivan at wavetail.420.am
Tue Dec 30 19:28:58 PST 2008


Update of /home/cvs/cvsroot/freeside/FS/FS
In directory wavetail.420.am:/tmp/cvs-serv28551/FS/FS

Modified Files:
	Record.pm cdr.pm phone_avail.pm 
Log Message:
bell west CDR format, RT#4403

Index: phone_avail.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/phone_avail.pm,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- phone_avail.pm	15 Sep 2008 07:18:56 -0000	1.2
+++ phone_avail.pm	31 Dec 2008 03:28:56 -0000	1.3
@@ -148,10 +148,13 @@
   my $job = shift;
 
   my $numsub = sub {
-    my( $hash, $value ) = @_;
+    my( $phone_avail, $value ) = @_;
     $value =~ s/\D//g;
     $value =~ /^(\d{3})(\d{3})(\d+)$/ or die "unparsable number $value\n";
-    ( $hash->{npa}, $hash->{nxx}, $hash->{station} ) = ( $1, $2, $3 );
+    #( $hash->{npa}, $hash->{nxx}, $hash->{station} ) = ( $1, $2, $3 );
+    $phone_avail->npa($1);
+    $phone_avail->nxx($2);
+    $phone_avail->station($3);
   };
 
   my $opt = { 'table'   => 'phone_avail',

Index: cdr.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cdr.pm,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -d -r1.26 -r1.27
--- cdr.pm	30 Dec 2008 21:45:02 -0000	1.26
+++ cdr.pm	31 Dec 2008 03:28:56 -0000	1.27
@@ -234,6 +234,15 @@
   $self->calldate( $self->startdate_sql )
     if !$self->calldate && $self->startdate;
 
+  #was just for $format eq 'taqua' but can't see the harm... add something to
+  #disable if it becomes a problem
+  if ( $self->duration eq '' && $self->enddate && $self->startdate ) {
+    $self->duration( $self->enddate - $self->startdate  );
+  }
+  if ( $self->billsec eq '' && $self->enddate && $self->answerdate ) {
+    $self->billsec(  $self->enddate - $self->answerdate );
+  } 
+
   my $conf = new FS::Conf;
 
   unless ( $self->charged_party ) {
@@ -671,138 +680,42 @@
 
 =cut
 
-sub batch_import {
-  my $param = shift;
-
-  my $fh = $param->{filehandle};
-  my $format = $param->{format};
-  my $cdrbatch = $param->{cdrbatch};
-
-  return "Unknown format $format"
-    unless exists( $cdr_info{$format} )
-        && exists( $cdr_info{$format}->{'import_fields'} );
-
-  my $info = $cdr_info{$format};
-
-  my $type = exists($info->{'type'}) ? lc($info->{'type'}) : 'csv';
-
-  my $parser;
-  if ( $type eq 'csv' ) {
-    eval "use Text::CSV_XS;";
-    die $@ if $@;
-    my %attr = ();
-    foreach ( grep exists($info->{$_}), qw( sep_char ) ) {
-      $attr{$_} = $info->{$_};
-    }
-    $parser = new Text::CSV_XS \%attr;
-  } elsif ( $type eq 'fixedlength' ) {
-    eval "use Parse::FixedLength;";
-    die $@ if $@;
-    $parser = new Parse::FixedLength $info->{'fixedlength_format'};
-  } else {
-    die "Unknown CDR format type $type for format $format\n";
-  }
-
-  my $imported = 0;
-  #my $columns;
-
-  local $SIG{HUP} = 'IGNORE';
-  local $SIG{INT} = 'IGNORE';
-  local $SIG{QUIT} = 'IGNORE';
-  local $SIG{TERM} = 'IGNORE';
-  local $SIG{TSTP} = 'IGNORE';
-  local $SIG{PIPE} = 'IGNORE';
-
-  my $oldAutoCommit = $FS::UID::AutoCommit;
-  local $FS::UID::AutoCommit = 0;
-  my $dbh = dbh;
-
-  my $header_lines = exists($info->{'header'}) ? $info->{'header'} : 0;
-
-  my $line;
-  while ( defined($line=<$fh>) ) {
-
-    next if $header_lines-- > 0; #&& $line =~ /^[\w, "]+$/ 
-
-    my @columns = ();
-    if ( $type eq 'csv' ) {
-
-      $parser->parse($line) or do {
-        $dbh->rollback if $oldAutoCommit;
-        return "can't parse: ". $parser->error_input();
-      };
-
-      @columns = $parser->fields();
-
-    } elsif ( $type eq 'fixedlength' ) {
-
-      @columns = $parser->parse($line);
-
-    } else {
-      die "Unknown CDR format type $type for format $format\n";
-    }
-
-    #warn join('-', at columns);
-
-    if ( $format eq 'simple' ) { #should be a callback or opt in FS::cdr::simple
-      @columns = map { s/^ +//; $_; } @columns;
-    }
-
-    my @later = ();
-    my %cdr =
-      map {
-
-        my $field_or_sub = $_;
-        if ( ref($field_or_sub) ) {
-          push @later, $field_or_sub, shift(@columns);
-          ();
-        } else {
-          ( $field_or_sub => shift @columns );
-        }
-
-      }
-      @{ $info->{'import_fields'} }
-    ;
- 
-    $cdr{cdrbatch} = $cdrbatch;
-
-    my $cdr = new FS::cdr ( \%cdr );
-
-    while ( scalar(@later) ) {
-      my $sub = shift @later;
-      my $data = shift @later;
-      &{$sub}($cdr, $data);  # $cdr->&{$sub}($data); 
-    }
+sub process_batch_import {
+  my $job = shift;
 
-    if ( $format eq 'taqua' ) { #should be a callback or opt in FS::cdr::taqua
-      if ( $cdr->enddate && $cdr->startdate  ) { #a bit more?
-        $cdr->duration( $cdr->enddate - $cdr->startdate  );
-      }
-      if ( $cdr->enddate && $cdr->answerdate ) { #a bit more?
-        $cdr->billsec(  $cdr->enddate - $cdr->answerdate );
-      } 
-    }
+  my $opt = {
+    'table'   => 'cdr',
+    'params'  => [ 'format', 'cdrbatch' ],
 
-    my $error = $cdr->insert;
-    if ( $error ) {
-      $dbh->rollback if $oldAutoCommit;
-      return $error;
+    'formats' => { map { $_ => $cdr_info{$_}->{'import_fields'}; }
+                       keys %cdr_info
+                 },
 
-      #or just skip?
-      #next;
-    }
+                            #drop the || 'csv' to allow auto xls for csv types?
+    'format_types' => { map { $_ => ( lc($cdr_info{$_}->{'type'}) || 'csv' ); }
+                            keys %cdr_info
+                      },
 
-    $imported++;
-  }
+    'format_headers' => { map { $_ => ( $cdr_info{$_}->{'header'} || 0 ); }
+                              keys %cdr_info
+                        },
 
-  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+    'format_sep_chars' => { map { $_ => $cdr_info{$_}->{'sep_char'}; }
+                                keys %cdr_info
+                          },
 
-  #might want to disable this if we skip records for any reason...
-  return "Empty file!" unless $imported || $param->{empty_ok};
+    'format_fixedlength_formats' =>
+      { map { $_ => $cdr_info{$_}->{'fixedlength_format'}; }
+            keys %cdr_info
+      },
+  };
 
-  '';
+  FS::Record::process_batch_import( $job, $opt, @_ );
 
 }
+#  if ( $format eq 'simple' ) { #should be a callback or opt in FS::cdr::simple
+#    @columns = map { s/^ +//; $_; } @columns;
+#  }
 
 =back
 

Index: Record.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Record.pm,v
retrieving revision 1.173
retrieving revision 1.174
diff -u -d -r1.173 -r1.174
--- Record.pm	21 Dec 2008 21:33:28 -0000	1.173
+++ Record.pm	31 Dec 2008 03:28:55 -0000	1.174
@@ -1340,18 +1340,74 @@
   '';
 }
 
-=item batch_import PARAM_HASHREF
+=item process_batch_import JOB OPTIONS_HASHREF PARAMS
 
-Class method for batch imports.  Available params:
+Processes a batch import as a queued JSRPC job
+
+JOB is an FS::queue entry.
+
+OPTIONS_HASHREF can have the following keys:
 
 =over 4
 
-=item job
+=item table
 
-FS::queue object, will be updated with progress
+Table name (required).
+
+=item params
+
+Listref of field names for static fields.  They will be given values from the
+PARAMS hashref and passed as a "params" hashref to batch_import.
+
+=item formats
+
+Formats hashref.  Keys are field names, values are listrefs that define the
+format.
+
+Each listref value can be a column name or a code reference.  Coderefs are run
+with the row object and data as the two parameters.  For example, this coderef
+does the same thing as using the "columnname" string:
+
+  sub {
+    my( $record, $data ) = @_;
+    $record->columnname( $data );
+  },
+
+=item format_types
+
+Optional format hashref of types.  Keys are field names, values are "csv",
+"xls" or "fixedlength".  Overrides automatic determination of file type
+from extension.
+
+=item format_headers
+
+Optional format hashref of header lines.  Keys are field names, values are 0
+for no header, 1 to ignore the first line, or to higher numbers to ignore that
+number of lines.
+
+=item format_sep_chars
+
+Optional format hashref of CSV sep_chars.  Keys are field names, values are the
+CSV separation character.
+
+=item format_fixedlenth_formats
+
+Optional format hashref of fixed length format defintiions.  Keys are field
+names, values Parse::FixedLength listrefs of field definitions.
+
+=item default_csv
+
+Set true to default to CSV file type if the filename does not contain a
+recognizable ".csv" or ".xls" extension (and type is not pre-specified by
+format_types).
 
 =back
 
+PARAMS is a base64-encoded Storable string containing the POSTed data as
+a hash ref.  It normally contains at least one field, "uploaded files",
+generated by /elements/file-upload.html and containing the list of uploaded
+files.  Currently only supports a single file named "file".
+
 =cut
 
 use Storable qw(thaw);
@@ -1375,26 +1431,47 @@
   my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
   my $file = $dir. $files{'file'};
 
-  my $type;
-  if ( $file =~ /\.(\w+)$/i ) {
-    $type = lc($1);
-  } else {
-    #or error out???
-    warn "can't parse file type from filename $file; defaulting to CSV";
-    $type = 'csv';
+  my $type = $opt->{'format_types'}
+             ? $opt->{'format_types'}{ $param->{'format'} }
+             : '';
+
+  unless ( $type ) {
+    if ( $file =~ /\.(\w+)$/i ) {
+      $type = lc($1);
+    } else {
+      #or error out???
+      warn "can't parse file type from filename $file; defaulting to CSV";
+      $type = 'csv';
+    }
+    $type = 'csv'
+      if $opt->{'default_csv'} && $type ne 'xls';
   }
-  $type = 'csv'
-    if $opt->{'default_csv'} && $type ne 'xls';
+
+  my $header = $opt->{'format_headers'}
+                 ? $opt->{'format_headers'}{ $param->{'format'} }
+                 : 0;
+
+  my $sep_char = $opt->{'format_sep_chars'}
+                   ? $opt->{'format_sep_chars'}{ $param->{'format'} }
+                   : ',';
+
+  my $fixedlength_format =
+    $opt->{'format_fixedlength_formats'}
+      ? $opt->{'format_fixedlength_formats'}{ $param->{'format'} }
+      : '';
 
   my $error =
     FS::Record::batch_import( {
-      table     => $table,
-      formats   => \%formats,
-      job       => $job,
-      file      => $file,
-      type      => $type,
-      format    => $param->{format},
-      params    => { map { $_ => $param->{$_} } @pass_params },
+      table              => $table,
+      formats            => \%formats,
+      job                => $job,
+      file               => $file,
+      type               => $type,
+      format             => $param->{format},
+      header             => $header,
+      sep_char           => $sep_char,
+      fixedlength_format => $fixedlength_format,
+      params             => { map { $_ => $param->{$_} } @pass_params },
     } );
 
   unlink $file;
@@ -1402,9 +1479,48 @@
   die "$error\n" if $error;
 }
 
+=item batch_import PARAM_HASHREF
+
+Class method for batch imports.  Available params:
+
+=over 4
+
+=item table
+
+=item formats
+
+=item params
+
+=item job
+
+FS::queue object, will be updated with progress
+
+=item filename
+
+=item type
+
+csv, xls or fixedlength
+
+=item format
+
+=item header
+
+=item sep_char
+
+=item fixedlength_format
+
+=item empty_ok
+
+=back
+
+=cut
+
 sub batch_import {
   my $param = shift;
 
+  warn "$me batch_import call with params: \n". Dumper($param)
+    if $DEBUG;
+
   my $table     = $param->{table};
   my $formats   = $param->{formats};
   my $params    = $param->{params};
@@ -1419,14 +1535,33 @@
   die "unknown format $format" unless exists $formats->{ $format };
   my @fields    = @{ $formats->{ $format } };
 
+  my $row = 0;
   my $count;
   my $parser;
   my @buffer = ();
-  if ( $type eq 'csv' ) {
+  if ( $type eq 'csv' || $type eq 'fixedlength' ) {
 
-    $parser = new Text::CSV_XS;
+    if ( $type eq 'csv' ) {
+
+      my %attr = ();
+      foreach ( grep exists($param->{$_}), qw( sep_char ) ) {
+        $attr{$_} = $param->{$_};
+      }
+
+      $parser = new Text::CSV_XS \%attr;
+
+    } elsif ( $type eq 'fixedlength' ) {
+
+      eval "use Parse::FixedLength;";
+      die $@ if $@;
+      $parser = new Parse::FixedLength $param->{'fixedlength_format'};
+ 
+    } else {
+      die "Unknown file type $type\n";
+    }
 
     @buffer = split(/\r?\n/, slurp($filename) );
+    splice(@buffer, 0, ($param->{'header'} || 0) );
     $count = scalar(@buffer);
 
   } elsif ( $type eq 'xls' ) {
@@ -1441,6 +1576,8 @@
     $count = $parser->{MaxRow} || $parser->{MinRow};
     $count++;
 
+    $row = $param->{'header'} || 0;
+
   } else {
     die "Unknown file type $type\n";
   }
@@ -1459,7 +1596,7 @@
   my $dbh = dbh;
   
   my $line;
-  my $row = 0;
+  my $imported = 0;
   my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
   while (1) {
 
@@ -1475,6 +1612,10 @@
       };
       @columns = $parser->fields();
 
+    } elsif ( $type eq 'fixedlength' ) {
+
+      @columns = $parser->parse($line);
+
     } elsif ( $type eq 'xls' ) {
 
       last if $row > ($parser->{MaxRow} || $parser->{MinRow})
@@ -1490,6 +1631,7 @@
       die "Unknown file type $type\n";
     }
 
+    my @later = ();
     my %hash = %$params;
 
     foreach my $field ( @fields ) {
@@ -1497,7 +1639,8 @@
       my $value = shift @columns;
      
       if ( ref($field) eq 'CODE' ) {
-        &{$field}(\%hash, $value);
+        #&{$field}(\%hash, $value);
+        push @later, $field, $value;
       } else {
         $hash{$field} = $value if length($value);
       }
@@ -1508,6 +1651,12 @@
 
     my $record = $class->new( \%hash );
 
+    while ( scalar(@later) ) {
+      my $sub = shift @later;
+      my $data = shift @later;
+      &{$sub}($record, $data);  # $record->&{$sub}($data); 
+    }
+
     my $error = $record->insert;
 
     if ( $error ) {
@@ -1515,10 +1664,10 @@
       return "can't insert record". ( $line ? " for $line" : '' ). ": $error";
     }
 
-    $row++;
+    $imported++;
 
     if ( $job && time - $min_sec > $last ) { #progress bar
-      $job->update_statustext( int(100 * $row / $count) );
+      $job->update_statustext( int(100 * $imported / $count) );
       $last = time;
     }
 
@@ -1526,7 +1675,7 @@
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
 
-  return "Empty file!" unless $row;
+  return "Empty file!" unless $imported || $param->{empty_ok};
 
   ''; #no error
 



More information about the freeside-commits mailing list