[freeside-commits] freeside/FS/FS cust_main.pm, 1.271.2.52, 1.271.2.53 Schema.pm, 1.44.2.28, 1.44.2.29 UID.pm, 1.32, 1.32.2.1

Ivan,,, ivan at wavetail.420.am
Thu Aug 14 04:54:12 PDT 2008


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

Modified Files:
      Tag: FREESIDE_1_7_BRANCH
	cust_main.pm Schema.pm UID.pm 
Log Message:
customer import: add progress bar & redirect to a search of the imported customers, #3475

Index: Schema.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Schema.pm,v
retrieving revision 1.44.2.28
retrieving revision 1.44.2.29
diff -u -d -r1.44.2.28 -r1.44.2.29
--- Schema.pm	1 Jul 2008 05:01:27 -0000	1.44.2.28
+++ Schema.pm	14 Aug 2008 11:54:10 -0000	1.44.2.29
@@ -486,6 +486,7 @@
         'custnum',  'serial',  '',     '', '', '', 
         'agentnum', 'int',  '',     '', '', '', 
         'agent_custid', 'varchar', 'NULL', $char_d, '', '',
+        'custbatch', 'varchar', 'NULL', $char_d, '', '',
 #        'titlenum', 'int',  'NULL',   '', '', '', 
         'last',     'varchar', '',     $char_d, '', '', 
 #        'middle',   'varchar', 'NULL', $char_d, '', '', 
@@ -548,7 +549,7 @@
                    [ 'county' ], [ 'state' ], [ 'country' ], [ 'zip' ],
                    [ 'ship_last' ], [ 'ship_company' ],
                    [ 'payby' ], [ 'paydate' ],
-
+                   [ 'agentnum' ], [ 'custbatch' ],
                  ],
     },
 

Index: cust_main.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_main.pm,v
retrieving revision 1.271.2.52
retrieving revision 1.271.2.53
diff -u -d -r1.271.2.52 -r1.271.2.53
--- cust_main.pm	14 Aug 2008 01:58:42 -0000	1.271.2.52
+++ cust_main.pm	14 Aug 2008 11:54:10 -0000	1.271.2.53
@@ -1211,6 +1211,7 @@
     || $self->ut_number('agentnum')
     || $self->ut_textn('agent_custid')
     || $self->ut_number('refnum')
+    || $self->ut_textn('custbatch')
     || $self->ut_name('last')
     || $self->ut_name('first')
     || $self->ut_snumbern('birthdate')
@@ -4810,6 +4811,15 @@
                    @{ $params->{'current_balance'} };
 
   ##
+  # custbatch
+  ##
+
+  if ( $params->{'custbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
+    push @where,
+      "cust_main.custbatch = '$1'";
+  }
+
+  ##
   # setup queries, subs, etc. for the search
   ##
 
@@ -5470,6 +5480,58 @@
   1;
 }
 
+=item process_batch_import
+
+Load a batch import as a queued JSRPC job
+
+=cut
+
+use Storable qw(thaw);
+use Data::Dumper;
+use MIME::Base64;
+sub process_batch_import {
+  my $job = shift;
+
+  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::cust_main::batch_import( {
+      job       => $job,
+      file      => $file,
+      type      => $type,
+      custbatch => $param->{custbatch},
+      agentnum  => $param->{'agentnum'},
+      refnum    => $param->{'refnum'},
+      pkgpart   => $param->{'pkgpart'},
+      #'fields'  => [qw( cust_pkg.setup dayphone first last address1 address2
+      #                 city state zip comments                          )],
+      'format'  => $param->{'format'},
+    } );
+
+  unlink $file;
+
+  die "$error\n" if $error;
+
+}
+
 =item batch_import
 
 =cut
@@ -5478,14 +5540,18 @@
 sub batch_import {
   my $param = shift;
 
-  my $fh       = $param->{filehandle};
-  my $type     = $param->{type} || 'csv';
+  my $job       = $param->{job};
 
-  my $agentnum = $param->{agentnum};
-  my $refnum   = $param->{refnum};
-  my $pkgpart  = $param->{pkgpart};
+  my $filename  = $param->{file};
+  my $type      = $param->{type} || 'csv';
 
-  my $format   = $param->{'format'};
+  my $custbatch = $param->{custbatch};
+
+  my $agentnum  = $param->{agentnum};
+  my $refnum    = $param->{refnum};
+  my $pkgpart   = $param->{pkgpart};
+
+  my $format    = $param->{'format'};
 
   my @fields;
   my $payby;
@@ -5521,28 +5587,30 @@
     die "unknown format $format";
   }
 
+  my $count;
   my $parser;
-  my $spoolfile = '';
+  my @buffer = ();
   if ( $type eq 'csv' ) {
+
     eval "use Text::CSV_XS;";
     die $@ if $@;
+
     $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 $spool_fh, $spoolfile ) =
-      tempfile('cust_main-batch_import-XXXXXXXXXXXX',
-                 DIR    => '%%%FREESIDE_CACHE%%%',
-                 SUFFIX => '.xls',
-              );
-    print $spool_fh slurp($fh);
-    close $spool_fh or die $!;
-
-    my $excel = new Spreadsheet::ParseExcel::Workbook->Parse($spoolfile);
+    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";
   }
@@ -5562,12 +5630,14 @@
   
   my $line;
   my $row = 0;
+  my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
   while (1) {
 
     my @columns = ();
     if ( $type eq 'csv' ) {
 
-      last unless defined($line=<$fh>);
+      last unless scalar(@buffer);
+      $line = shift(@buffer);
 
       $parser->parse($line) or do {
         $dbh->rollback if $oldAutoCommit;
@@ -5592,11 +5662,12 @@
     #warn join('-', at columns);
 
     my %cust_main = (
-      agentnum => $agentnum,
-      refnum   => $refnum,
-      country  => $conf->config('countrydefault') || 'US',
-      payby    => $payby, #default
-      paydate  => '12/2037', #default
+      custbatch => $custbatch,
+      agentnum  => $agentnum,
+      refnum    => $refnum,
+      country   => $conf->config('countrydefault') || 'US',
+      payby     => $payby, #default
+      paydate   => '12/2037', #default
     );
     my $billtime = time;
     my %cust_pkg = ( pkgpart => $pkgpart );
@@ -5648,7 +5719,9 @@
       }
     }
 
-    $cust_main{'payby'} = 'CARD' if length($cust_main{'payinfo'});
+    $cust_main{'payby'} = 'CARD'
+      if defined $cust_main{'payinfo'}
+      && length  $cust_main{'payinfo'};
 
     my $invoicing_list = $cust_main{'invoicing_list'}
                            ? [ delete $cust_main{'invoicing_list'} ]
@@ -5680,7 +5753,7 @@
 
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
-      return "can't insert customer ". ( $line ? "for $line" : '' ). ": $error";
+      return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
     }
 
     if ( $format eq 'simple' ) {
@@ -5707,11 +5780,15 @@
     }
 
     $row++;
-  }
 
-  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+    if ( $job && time - $min_sec > $last ) { #progress bar
+      $job->update_statustext( int(100 * $row / $count) );
+      $last = time;
+    }
 
-  unlink($spoolfile) if $spoolfile;
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
 
   return "Empty file!" unless $row;
 

Index: UID.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/UID.pm,v
retrieving revision 1.32
retrieving revision 1.32.2.1
diff -u -d -r1.32 -r1.32.2.1
--- UID.pm	14 Dec 2006 09:27:26 -0000	1.32
+++ UID.pm	14 Aug 2008 11:54:10 -0000	1.32.2.1
@@ -3,7 +3,7 @@
 use strict;
 use vars qw(
   @ISA @EXPORT_OK $cgi $dbh $freeside_uid $user 
-  $conf_dir $secrets $datasrc $db_user $db_pass %callback @callback
+  $conf_dir $cache_dir $secrets $datasrc $db_user $db_pass %callback @callback
   $driver_name $AutoCommit
 );
 use subs qw(
@@ -22,6 +22,7 @@
 $freeside_uid = scalar(getpwnam('freeside'));
 
 $conf_dir = "%%%FREESIDE_CONF%%%/";
+$cache_dir = "%%%FREESIDE_CACHE%%%";
 
 $AutoCommit = 1; #ours, not DBI
 



More information about the freeside-commits mailing list