batch charge/credit import
authorivan <ivan>
Thu, 5 Sep 2002 13:28:00 +0000 (13:28 +0000)
committerivan <ivan>
Thu, 5 Sep 2002 13:28:00 +0000 (13:28 +0000)
FS/FS/cust_main.pm
httemplate/index.html
httemplate/misc/cust_main-import_charges.cgi [new file with mode: 0644]
httemplate/misc/process/cust_main-import_charges.cgi [new file with mode: 0644]

index 8e47f23..cfa6b8b 100644 (file)
@@ -2075,6 +2075,91 @@ sub batch_import {
 
 }
 
 
 }
 
+=item batch_charge
+
+=cut
+
+sub batch_charge {
+  my $param = shift;
+  #warn join('-',keys %$param);
+  my $fh = $param->{filehandle};
+  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 %row = ();
+    foreach my $field ( @fields ) {
+      $row{$field} = shift @columns;
+    }
+
+    my $cust_main = qsearchs('cust_main', { 'custnum' => $row{'custnum'} } );
+    unless ( $cust_main ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "unknown custnum $row{'custnum'}";
+    }
+
+    if ( $row{'amount'} > 0 ) {
+      my $error = $cust_main->charge($row{'amount'}, $row{'pkg'});
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $error;
+      }
+      $imported++;
+    } elsif ( $row{'amount'} < 0 ) {
+      my $error = $cust_main->credit( sprintf( "%.2f", 0-$row{'amount'} ),
+                                      $row{'pkg'}                         );
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $error;
+      }
+      $imported++;
+    } else {
+      #hmm?
+    }
+
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+
+  return "Empty file!" unless $imported;
+
+  ''; #no error
+
+}
+
 =back
 
 =head1 BUGS
 =back
 
 =head1 BUGS
index 35a5fc1..dce020b 100644 (file)
       <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>
       <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><A HREF="misc/cust_main-import_charges.cgi">Batch import charges 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>
       <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_charges.cgi b/httemplate/misc/cust_main-import_charges.cgi
new file mode 100644 (file)
index 0000000..0822b9e
--- /dev/null
@@ -0,0 +1,14 @@
+<!-- mason kludge -->
+<%= header('Batch Customer Charge') %>
+<FORM ACTION="process/cust_main-import_charges.cgi" METHOD="post" ENCTYPE="multipart/form-data">
+Import a CSV file containing customer charges.<BR><BR>
+Default file format is CSV, with the following field order: <i>custnum, amount, description</i><BR><BR>
+If <i>amount</i> is negative, a credit will be applied instead.<BR><BR>
+<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_charges.cgi b/httemplate/misc/process/cust_main-import_charges.cgi
new file mode 100644 (file)
index 0000000..14df1bd
--- /dev/null
@@ -0,0 +1,26 @@
+<%
+
+  my $fh = $cgi->upload('csvfile');
+  #warn $cgi;
+  #warn $fh;
+
+  my $error = defined($fh)
+    ? FS::cust_main::batch_charge( {
+        filehandle => $fh,
+        'fields'    => [qw( custnum amount pkg )],
+      } )
+    : 'No file';
+
+  if ( $error ) {
+    %>
+    <!-- mason kludge -->
+    <%
+    eidiot($error);
+#    $cgi->param('error', $error);
+#    print $cgi->redirect( "${p}cust_main-import_charges.cgi
+  } else {
+    %>
+    <!-- mason kludge -->
+    <%= header('Import sucessful') %> <%
+  }
+%>