working CSV import for crcmn
authorivan <ivan>
Fri, 30 Aug 2002 17:34:06 +0000 (17:34 +0000)
committerivan <ivan>
Fri, 30 Aug 2002 17:34:06 +0000 (17:34 +0000)
FS/FS/cust_main.pm
httemplate/index.html
httemplate/misc/cust_main-import.cgi [new file with mode: 0644]
httemplate/misc/process/cust_main-import.cgi [new file with mode: 0644]

index eb468d9..eae760b 100644 (file)
@@ -713,7 +713,8 @@ sub check {
     my $y = length($2) == 4 ? $2 : "20$2";
     $self->paydate("$y-$1-01");
     my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
-    return gettext('expired_card') if $y<$nowy || ( $y==$nowy && $1<$nowm );
+    return gettext('expired_card')
+      if !$import && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
   }
 
   if ( $self->payname eq '' &&
@@ -1964,6 +1965,94 @@ sub append_fuzzyfiles {
   1;
 }
 
+=item batch_import
+
+=cut
+
+sub batch_import {
+  my $param = shift;
+  #warn join('-',keys %$param);
+  my $fh = $param->{filehandle};
+  my $agentnum = $param->{agentnum};
+  my $refnum = $param->{refnum};
+  my $pkgpart = $param->{pkgpart};
+  my @fields = @{$param->{fields}};
+
+  eval "use Date::Parse;";
+  die $@ if $@;
+  eval "use Text::CSV_XS;";
+  die $@ if $@;
+
+  my $csv = new Text::CSV_XS;
+  #warn $csv;
+  #warn $fh;
+
+  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;
+  
+  #while ( $columns = $csv->getline($fh) ) {
+  my $line;
+  while ( defined($line=<$fh>) ) {
+
+    $csv->parse($line) or do {
+      $dbh->rollback if $oldAutoCommit;
+      return "can't parse: ". $csv->error_input();
+    };
+
+    my @columns = $csv->fields();
+    #warn join('-',@columns);
+
+    my %cust_main = (
+      agentnum => $agentnum,
+      refnum   => $refnum,
+      country  => 'US', #default
+      payby    => 'BILL', #default
+      paydate  => '12/2037', #default
+    );
+    my %cust_pkg = ( pkgpart => $pkgpart );
+    foreach my $field ( @fields ) {
+      if ( $field =~ /^cust_pkg\.(setup|bill|susp|expire|cancel)$/ ) {
+        #$cust_pkg{$1} = str2time( shift @$columns );
+        $cust_pkg{$1} = str2time( shift @columns );
+      } else {
+        #$cust_main{$field} = shift @$columns; 
+        $cust_main{$field} = shift @columns; 
+      }
+    }
+
+    my $cust_pkg = new FS::cust_pkg ( \%cust_pkg ) if $pkgpart;
+    my $cust_main = new FS::cust_main ( \%cust_main );
+    use Tie::RefHash;
+    tie my %hash, 'Tie::RefHash'; #this part is important
+    $hash{$cust_pkg} = [] if $pkgpart;
+    my $error = $cust_main->insert( \%hash );
+
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "can't insert customer for $line: $error";
+    }
+    $imported++;
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+
+  return "Empty file!" unless $imported;
+
+  ''; #no error
+
+}
+
 =back
 
 =head1 BUGS
index 29dd3b4..cd9a203 100644 (file)
       <BR>
       <A HREF="browse/nas.cgi">View active NAS ports</A>
       <BR><A HREF="browse/queue.cgi">View pending job queue</A>
+      <BR><A HREF="misc/cust_main-import.cgi">Batch import customers from CSV file</A>
       <BR><BR><CENTER><HR WIDTH="94%" NOSHADE></CENTER><BR>
       <A NAME="config" HREF="config/config-view.cgi">Configuration</a><!-- - <font size="+2" color="#ff0000">start here</font> -->
       <BR><BR><A NAME="admin">Administration</a>
diff --git a/httemplate/misc/cust_main-import.cgi b/httemplate/misc/cust_main-import.cgi
new file mode 100644 (file)
index 0000000..6b36f47
--- /dev/null
@@ -0,0 +1,51 @@
+<!-- mason kludge -->
+<%= header('Batch Customer Import') %>
+<FORM ACTION="process/cust_main-import.cgi" METHOD="post" ENCTYPE="multipart/form-data">
+Import a CSV file containing customer records.<BR><BR>
+Default file format is CSV, with the following field order: <i>cust_pkg.setup, dayphone, first, last, address1, address2, city, state, zip, comments</i><BR><BR>
+
+<%
+  #false laziness with edit/cust_main.cgi
+  my @agents = qsearch( 'agent', {} );
+  die "No agents created!" unless @agents;
+  my $agentnum = $agents[0]->agentnum; #default to first
+
+  if ( scalar(@agents) == 1 ) {
+%>
+    <INPUT TYPE="hidden" NAME="agentnum" VALUE="<%= $agentnum %>">
+<% } else { %>
+    <BR><BR>Agent <SELECT NAME="agentnum" SIZE="1">
+  <% foreach my $agent (sort { $a->agent cmp $b->agent } @agents) { %>
+    <OPTION VALUE="<%= $agent->agentnum %>" <%= " SELECTED"x($agent->agentnum==$agentnum) %>><%= $agent->agent %></OPTION>
+  <% } %>
+    </SELECT><BR><BR>
+<% } %>
+
+<%
+  my @referrals = qsearch('part_referral',{});
+  die "No advertising sources created!" unless @referrals;
+  my $refnum = $referrals[0]->refnum; #default to first
+
+  if ( scalar(@referrals) == 1 ) {
+%>
+    <INPUT TYPE="hidden" NAME="refnum" VALUE="<%= $refnum %>">
+<% } else { %>
+    <BR><BR>Advertising source <SELECT NAME="refnum" SIZE="1">
+  <% foreach my $referral ( sort { $a->referral <=> $b->referral } @referrals) { %>
+    <OPTION VALUE="<%= $referral->refnum %>" <%= " SELECTED"x($referral->refnum==$refnum) %>><%= $referral->refnum %>: <%= $referral->referral %></OPTION>
+  <% } %>
+    </SELECT><BR><BR>
+<% } %>
+
+    First package: <SELECT NAME="pkgpart"><OPTION VALUE="">(none)</OPTION>
+<% foreach my $part_pkg ( qsearch('part_pkg',{'disabled'=>'' }) ) { %>
+     <OPTION VALUE="<%= $part_pkg->pkgpart %>"><%= $part_pkg->pkg. ' - '. $part_pkg->comment %></OPTION>
+<% } %>
+</SELECT><BR><BR>
+
+    CSV Filename: <INPUT TYPE="file" NAME="csvfile"><BR><BR>
+    <INPUT TYPE="submit" VALUE="Import">
+    </FORM>
+  </BODY>
+<HTML>
+
diff --git a/httemplate/misc/process/cust_main-import.cgi b/httemplate/misc/process/cust_main-import.cgi
new file mode 100644 (file)
index 0000000..9e1adce
--- /dev/null
@@ -0,0 +1,30 @@
+<%
+
+  my $fh = $cgi->upload('csvfile');
+  #warn $cgi;
+  #warn $fh;
+
+  my $error = defined($fh)
+    ? FS::cust_main::batch_import( {
+        filehandle => $fh,
+        agentnum   => scalar($cgi->param('agentnum')),
+        refnum     => scalar($cgi->param('refnum')),
+        pkgpart    => scalar($cgi->param('pkgpart')),
+        'fields'    => [qw( cust_pkg.setup dayphone first last address1 address2
+                           city state zip comments                          )],
+      } )
+    : 'No file';
+
+  if ( $error ) {
+    %>
+    <!-- mason kludge -->
+    <%
+    eidiot($error);
+#    $cgi->param('error', $error);
+#    print $cgi->redirect( "${p}cust_main-import.cgi
+  } else {
+    %>
+    <!-- mason kludge -->
+    <%= header('Import sucessful') %> <%
+  }
+%>