diff options
-rw-r--r-- | FS/FS/cust_main.pm | 85 | ||||
-rw-r--r-- | httemplate/index.html | 1 | ||||
-rw-r--r-- | httemplate/misc/cust_main-import_charges.cgi | 14 | ||||
-rw-r--r-- | httemplate/misc/process/cust_main-import_charges.cgi | 26 |
4 files changed, 126 insertions, 0 deletions
diff --git a/FS/FS/cust_main.pm b/FS/FS/cust_main.pm index 8e47f23d5..cfa6b8bb6 100644 --- a/FS/FS/cust_main.pm +++ b/FS/FS/cust_main.pm @@ -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 diff --git a/httemplate/index.html b/httemplate/index.html index 35a5fc19c..dce020b1b 100644 --- a/httemplate/index.html +++ b/httemplate/index.html @@ -167,6 +167,7 @@ <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> diff --git a/httemplate/misc/cust_main-import_charges.cgi b/httemplate/misc/cust_main-import_charges.cgi new file mode 100644 index 000000000..0822b9eb6 --- /dev/null +++ b/httemplate/misc/cust_main-import_charges.cgi @@ -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 index 000000000..14df1bd8d --- /dev/null +++ b/httemplate/misc/process/cust_main-import_charges.cgi @@ -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') %> <% + } +%> |