import customer from Excel file too
[freeside.git] / httemplate / misc / process / cust_main-import.cgi
index a5ede99..2568d1c 100644 (file)
@@ -1,35 +1,42 @@
-%
-%
-%  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                          )],
-%        'format'   => scalar($cgi->param('format')),
-%      } )
-%    : 'No file';
-%
-%  if ( $error ) {
-%    
-
-    <!-- mason kludge -->
-%
-%    eidiot($error);
-%#    $cgi->param('error', $error);
-%#    print $cgi->redirect( "${p}cust_main-import.cgi
+% if ( $error ) {
+%   errorpage($error);
 %  } else {
-%    
-
-    <!-- mason kludge -->
-    <% include("/elements/header.html",'Import successful') %> 
-%
+    <% include('/elements/header.html','Import successful') %> 
+    <% include('/elements/footer.html') %> 
 %  }
-%
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Import');
+
+my $fh = $cgi->upload('file');
+my $error = '';
+if ( defined($fh) ) {
+
+  my $type;
+  if ( $cgi->param('file') =~ /\.(\w+)$/i ) {
+    $type = lc($1);
+  } else {
+    #or error out???
+    warn "can't parse file type from filename ". $cgi->param('file').
+         '; defaulting to CSV';
+    $type = 'csv';
+  }
+
+  $error =
+    FS::cust_main::batch_import( {
+      filehandle => $fh,
+      type       => $type,
+      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                          )],
+      'format'   => scalar($cgi->param('format')),
+    } );
+
+} else {
+  $error = 'No file';
+}
 
+</%init>