[freeside-commits] freeside/FS/FS Schema.pm, 1.107, 1.108 phone_avail.pm, 1.1, 1.2 Record.pm, 1.168, 1.169

Ivan,,, ivan at wavetail.420.am
Mon Sep 15 00:18:58 PDT 2008


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

Modified Files:
	Schema.pm phone_avail.pm Record.pm 
Log Message:
add internal did database & ability to query for availability, plus upload tool

Index: phone_avail.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/phone_avail.pm,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- phone_avail.pm	29 Jun 2008 00:41:05 -0000	1.1
+++ phone_avail.pm	15 Sep 2008 07:18:56 -0000	1.2
@@ -57,6 +57,18 @@
 
 nxx
 
+=item station
+
+station
+
+=item svcnum
+
+svcnum
+
+=item availbatch
+
+availbatch
+
 =back
 
 =head1 METHODS
@@ -123,12 +135,34 @@
     || $self->ut_alphan('state')
     || $self->ut_number('npa')
     || $self->ut_numbern('nxx')
+    || $self->ut_numbern('station')
+    || $self->ut_foreign_keyn('svcnum', 'cust_svc', 'svcnum' )
+    || $self->ut_textn('availbatch')
   ;
   return $error if $error;
 
   $self->SUPER::check;
 }
 
+sub process_batch_import {
+  my $job = shift;
+
+  my $numsub = sub {
+    my( $hash, $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 );
+  };
+
+  my $opt = { 'table'   => 'phone_avail',
+              'params'  => [ 'availbatch', 'exportnum', 'countrycode' ],
+              'formats' => { 'default' => [ 'state', $numsub ] },
+            };
+
+  FS::Record::process_batch_import( $job, $opt, @_ );
+
+}
+
 =back
 
 =head1 BUGS

Index: Schema.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Schema.pm,v
retrieving revision 1.107
retrieving revision 1.108
diff -u -d -r1.107 -r1.108
--- Schema.pm	12 Sep 2008 02:28:45 -0000	1.107
+++ Schema.pm	15 Sep 2008 07:18:55 -0000	1.108
@@ -2112,10 +2112,19 @@
         'state',       'char', 'NULL',  2, '', '', 
         'npa',         'char',     '',  3, '', '', 
         'nxx',         'char', 'NULL',  3, '', '', 
+        'station',     'char', 'NULL',  4, '', '',
+        'svcnum',      'int',     'NULL',      '', '', '',
+        'availbatch', 'varchar', 'NULL', $char_d, '', '',
       ],
       'primary_key' => 'availnum',
       'unique' => [],
-      'index'  => [ [ 'exportnum', 'countrycode', 'state' ] ],
+      'index'  => [ [ 'exportnum', 'countrycode', 'state' ],     #npa search
+                    [ 'exportnum', 'countrycode', 'npa' ],       #nxx search
+                    [ 'exportnum', 'countrycode', 'npa', 'nxx' ],#station search
+                    [ 'exportnum', 'countrycode', 'npa', 'nxx', 'station' ], # #
+                    [ 'svcnum' ],
+                    [ 'availbatch' ],
+                  ],
     },
 
     'reason_type' => {

Index: Record.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Record.pm,v
retrieving revision 1.168
retrieving revision 1.169
diff -u -d -r1.168 -r1.169
--- Record.pm	23 Aug 2008 03:29:17 -0000	1.168
+++ Record.pm	15 Sep 2008 07:18:56 -0000	1.169
@@ -11,6 +11,8 @@
 use Scalar::Util qw( blessed );
 use File::CounterFile;
 use Locale::Country;
+use Text::CSV_XS;
+use File::Slurp qw( slurp );
 use DBI qw(:sql_types);
 use DBIx::DBSchema 0.33;
 use FS::UID qw(dbh getotaker datasrc driver_name);
@@ -1294,6 +1296,194 @@
   '';
 }
 
+=item batch_import PARAM_HASHREF
+
+Class method for batch imports.  Available params:
+
+=over 4
+
+=item job
+
+FS::queue object, will be updated with progress
+
+=back
+
+=cut
+
+use Storable qw(thaw);
+use Data::Dumper;
+use MIME::Base64;
+sub process_batch_import {
+  my($job, $opt) = ( shift, shift );
+
+  my $table = $opt->{table};
+  my @pass_params = @{ $opt->{params} };
+  my %formats = %{ $opt->{formats} };
+
+  my $param = thaw(decode_base64(shift));
+  warn Dumper($param) if $DEBUG;
+  
+  my $files = $param->{'uploaded_files'}
+    or die "No files provided.\n";
+
+  my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
+
+  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 $error =
+    FS::Record::batch_import( {
+      table     => $table,
+      formats   => \%formats,
+      job       => $job,
+      file      => $file,
+      type      => $type,
+      format    => $param->{format},
+      params    => { map { $_ => $param->{$_} } @pass_params },
+    } );
+
+  unlink $file;
+
+  die "$error\n" if $error;
+}
+
+sub batch_import {
+  my $param = shift;
+
+  my $table     = $param->{table};
+  my $formats   = $param->{formats};
+  my $params    = $param->{params};
+
+  my $job       = $param->{job};
+
+  my $filename  = $param->{file};
+  my $type      = $param->{type} || 'csv';
+
+  my $format = $param->{'format'};
+
+  die "unknown format $format" unless exists $formats->{ $format };
+  my @fields    = @{ $formats->{ $format } };
+
+  my $count;
+  my $parser;
+  my @buffer = ();
+  if ( $type eq 'csv' ) {
+
+    $parser = new Text::CSV_XS;
+
+    @buffer = split(/\r?\n/, slurp($filename) );
+    $count = scalar(@buffer);
+
+  } elsif ( $type eq 'xls' ) {
+
+    eval "use Spreadsheet::ParseExcel;";
+    die $@ if $@;
+
+    my $excel = new Spreadsheet::ParseExcel::Workbook->Parse($filename);
+    $parser = $excel->{Worksheet}[0]; #first sheet
+
+    $count = $parser->{MaxRow} || $parser->{MinRow};
+    $count++;
+
+  } else {
+    die "Unknown file type $type\n";
+  }
+
+  #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 $line;
+  my $row = 0;
+  my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
+  while (1) {
+
+    my @columns = ();
+    if ( $type eq 'csv' ) {
+
+      last unless scalar(@buffer);
+      $line = shift(@buffer);
+
+      $parser->parse($line) or do {
+        $dbh->rollback if $oldAutoCommit;
+        return "can't parse: ". $parser->error_input();
+      };
+      @columns = $parser->fields();
+
+    } elsif ( $type eq 'xls' ) {
+
+      last if $row > ($parser->{MaxRow} || $parser->{MinRow});
+
+      my @row = @{ $parser->{Cells}[$row] };
+      @columns = map $_->{Val}, @row;
+
+      #my $z = 'A';
+      #warn $z++. ": $_\n" for @columns;
+
+    } else {
+      die "Unknown file type $type\n";
+    }
+
+    my %hash = %$params;
+
+    foreach my $field ( @fields ) {
+
+      my $value = shift @columns;
+     
+      if ( ref($field) eq 'CODE' ) {
+        &{$field}(\%hash, $value);
+      } else {
+        $hash{$field} = $value if length($value);
+      }
+
+    }
+
+    my $class = "FS::$table";
+
+    my $record = $class->new( \%hash );
+
+    my $error = $record->insert;
+
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "can't insert record". ( $line ? " for $line" : '' ). ": $error";
+    }
+
+    $row++;
+
+    if ( $job && time - $min_sec > $last ) { #progress bar
+      $job->update_statustext( int(100 * $row / $count) );
+      $last = time;
+    }
+
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
+
+  return "Empty file!" unless $row;
+
+  ''; #no error
+
+}
+
 sub _h_statement {
   my( $self, $action, $time ) = @_;
 



More information about the freeside-commits mailing list