From 6a24254d490f3d023728044daba0765f20f6971e Mon Sep 17 00:00:00 2001 From: jeff Date: Tue, 15 Apr 2008 20:47:59 +0000 Subject: (finally) wrap up new tax rate engine (for now) --- httemplate/misc/file-upload.html | 47 ++++++++++ httemplate/misc/process/tax-import.cgi | 55 +----------- httemplate/misc/process/tax-upgrade.cgi | 147 ++++++++++++++++++++++++++++++++ httemplate/misc/tax-import.cgi | 70 +++++++++------ 4 files changed, 243 insertions(+), 76 deletions(-) create mode 100644 httemplate/misc/file-upload.html create mode 100644 httemplate/misc/process/tax-upgrade.cgi (limited to 'httemplate/misc') diff --git a/httemplate/misc/file-upload.html b/httemplate/misc/file-upload.html new file mode 100644 index 000000000..9649d3663 --- /dev/null +++ b/httemplate/misc/file-upload.html @@ -0,0 +1,47 @@ +<% include('/elements/header-minimal.html', 'File Upload') %> +% if ($error) { +Error: <% $error %> +% }else{ +Freeside File Upload Successful <% join(',', @filenames) %>; +% } +<% include('/elements/footer.html') %> +<%init> + +die "access denied" + unless $FS::CurrentUser::CurrentUser->access_right('Import'); #? + +my @filenames = (); +my $error = ''; # could be extended to the access control + +$cgi->param('upload_fields') =~ /^([,\w]+)$/ + or $error = "invalid upload_fields"; +my $fields = $1; + +my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc; + +foreach my $field (split /,/, $fields) { + next if $error; + + my $fh = $cgi->upload($field) + or $error = "No valid file was provided."; + + my $sh = new File::Temp( TEMPLATE => 'upload.XXXXXXXX', + DIR => $dir, + UNLINK => 0, + ) + or $error ||= "can't open temporary file to store upload: $!\n"; + + unless ($error) { + while(<$fh>) { + print $sh $_; + } + $sh->filename =~ m!.*/([.\w]+)$!; + push @filenames, "$field:$1"; + close $sh + } + +} + +$error = "No files" unless scalar(@filenames); + + diff --git a/httemplate/misc/process/tax-import.cgi b/httemplate/misc/process/tax-import.cgi index 77fba61f5..f66d6db29 100644 --- a/httemplate/misc/process/tax-import.cgi +++ b/httemplate/misc/process/tax-import.cgi @@ -1,58 +1,9 @@ -% if ( $error ) { -% warn $error; -% errorpage($error); -% } else { - <% include('/elements/header.html','Import successful') %> - <% include('/elements/footer.html') %> -% } +<% $server->process %> <%init> die "access denied" - unless $FS::CurrentUser::CurrentUser->access_right('Import'); + unless $FS::CurrentUser::CurrentUser->access_right('Resend invoices'); -my $cfh = $cgi->upload('codefile'); -my $zfh = $cgi->upload('plus4file'); -my $tfh = $cgi->upload('txmatrix'); -my $dfh = $cgi->upload('detail'); -#warn $cgi; -#warn $fh; - -my $oldAutoCommit = $FS::UID::AutoCommit; -local $FS::UID::AutoCommit = 0; -my $dbh = dbh; - -my $error = defined($cfh) - ? FS::tax_class::batch_import( { - filehandle => $cfh, - 'format' => scalar($cgi->param('format')), - } ) - : 'No code file'; - -$error ||= defined($zfh) - ? FS::cust_tax_location::batch_import( { - filehandle => $zfh, - 'format' => scalar($cgi->param('format')), - } ) - : 'No plus4 file'; - -$error ||= defined($tfh) - ? FS::part_pkg_taxrate::batch_import( { - filehandle => $tfh, - 'format' => scalar($cgi->param('format')), - } ) - : 'No tax matrix file'; - -$error ||= defined($dfh) - ? FS::tax_rate::batch_import( { - filehandle => $dfh, - 'format' => scalar($cgi->param('format')), - } ) - : 'No tax detail file'; - -if ($error) { - $dbh->rollback or die $dbh->errstr if $oldAutoCommit; -}else{ - $dbh->commit or die $dbh->errstr if $oldAutoCommit; -} +my $server = new FS::UI::Web::JSRPC 'FS::tax_rate::process_batch', $cgi; diff --git a/httemplate/misc/process/tax-upgrade.cgi b/httemplate/misc/process/tax-upgrade.cgi new file mode 100644 index 000000000..8782282bd --- /dev/null +++ b/httemplate/misc/process/tax-upgrade.cgi @@ -0,0 +1,147 @@ +% if ( $error ) { +% warn $error; +% errorpage($error); +% } else { + <% include('/elements/header.html','Import successful') %> + <% include('/elements/footer.html') %> +% } +<%init> + +die "access denied" + unless $FS::CurrentUser::CurrentUser->access_right('Import'); + +my $cfh = $cgi->upload('codefile'); +my $zfh = $cgi->upload('plus4file'); +my $tfh = $cgi->upload('txmatrix'); +my $dfh = $cgi->upload('detail'); +#warn $cgi; +#warn $fh; + +my $oldAutoCommit = $FS::UID::AutoCommit; +local $FS::UID::AutoCommit = 0; +my $dbh = dbh; + +my $error = ''; + +my ($cifh, $cdfh, $zifh, $zdfh, $tifh, $tdfh); + +if (defined($cfh)) { + $cifh = new File::Temp( TEMPLATE => 'code.insert.XXXXXXXX', + DIR => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc, + ) or die "can't open temp file: $!\n"; + + $cdfh = new File::Temp( TEMPLATE => 'code.insert.XXXXXXXX', + DIR => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc, + ) or die "can't open temp file: $!\n"; + + while(<$cfh>) { + my $fh = ''; + $fh = $cifh if $_ =~ /"I"\s*$/; + $fh = $cdfh if $_ =~ /"D"\s*$/; + die "bad input line: $_" unless $fh; + print $fh $_; + } + seek $cifh, 0, 0; + seek $cdfh, 0, 0; + +}else{ + $error = 'No code file'; +} + +$error ||= FS::tax_class::batch_import( { + filehandle => $cifh, + 'format' => scalar($cgi->param('format')), + } ); + +close $cifh if $cifh; + +if (defined($zfh)) { + $zifh = new File::Temp( TEMPLATE => 'plus4.insert.XXXXXXXX', + DIR => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc, + ) or die "can't open temp file: $!\n"; + + $zdfh = new File::Temp( TEMPLATE => 'plus4.insert.XXXXXXXX', + DIR => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc, + ) or die "can't open temp file: $!\n"; + + while(<$zfh>) { + my $fh = ''; + $fh = $zifh if $_ =~ /"I"\s*$/; + $fh = $zdfh if $_ =~ /"D"\s*$/; + die "bad input line: $_" unless $fh; + print $fh $_; + } + seek $zifh, 0, 0; + seek $zdfh, 0, 0; + +}else{ + $error = 'No plus4 file'; +} + +$error ||= FS::cust_tax_location::batch_import( { + filehandle => $zifh, + 'format' => scalar($cgi->param('format')), + } ); +close $zifh if $zifh; + +if (defined($tfh)) { + $tifh = new File::Temp( TEMPLATE => 'txmatrix.insert.XXXXXXXX', + DIR => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc, + ) or die "can't open temp file: $!\n"; + + $tdfh = new File::Temp( TEMPLATE => 'txmatrix.insert.XXXXXXXX', + DIR => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc, + ) or die "can't open temp file: $!\n"; + + while(<$tfh>) { + my $fh = ''; + $fh = $tifh if $_ =~ /"I"\s*$/; + $fh = $tdfh if $_ =~ /"D"\s*$/; + die "bad input line: $_" unless $fh; + print $fh $_; + } + seek $tifh, 0, 0; + seek $tdfh, 0, 0; + +}else{ + $error = 'No tax matrix file'; +} + +$error ||= FS::part_pkg_taxrate::batch_import( { + filehandle => $tifh, + 'format' => scalar($cgi->param('format')), + } ); +close $tifh if $tifh; + +$error ||= defined($dfh) + ? FS::tax_rate::batch_update( { + filehandle => $dfh, + 'format' => scalar($cgi->param('format')), + } ) + : 'No tax detail file'; + +$error ||= FS::part_pkg_taxrate::batch_import( { + filehandle => $tdfh, + 'format' => scalar($cgi->param('format')), + } ); +close $tdfh if $tdfh; + +$error ||= FS::cust_tax_location::batch_import( { + filehandle => $zdfh, + 'format' => scalar($cgi->param('format')), + } ); +close $zdfh if $zdfh; + +$error ||= FS::tax_class::batch_import( { + filehandle => $cdfh, + 'format' => scalar($cgi->param('format')), + } ); +close $cdfh if $cdfh; + +if ($error) { + $dbh->rollback or die $dbh->errstr if $oldAutoCommit; +}else{ + $dbh->commit or die $dbh->errstr if $oldAutoCommit; +} + + diff --git a/httemplate/misc/tax-import.cgi b/httemplate/misc/tax-import.cgi index 6bdea6a56..9044ac9eb 100644 --- a/httemplate/misc/tax-import.cgi +++ b/httemplate/misc/tax-import.cgi @@ -3,41 +3,63 @@ Import a CSV file set containing tax rate records.

-
+<% include( '/elements/progress-init.html', + 'TaxRateUpload', + [ 'format', 'uploaded_files' ], + 'process/tax-import.cgi', + { 'message' => 'Tax rates imported' }, + ) +%> -<% &ntable("#cccccc", 2) %> + + +
+ +<% &ntable("#cccccc", 2) %> Format - - code CSV filename - - - - - plus4 CSV filename - - - - - txmatrix CSV filename - - - - - detail CSV filename - - - +<% include('/elements/file-upload.html', 'field' => [ 'codefile', + 'plus4file', + 'txmatrix', + 'detail', + ], + 'label' => [ 'code CSV filename', + 'plus4 CSV filename', + 'txmatrix CSV filename', + 'detail CSV filename', + ], + 'callback' => 'gotLoaded', + 'debug' => 0, + ) +%> - + -- cgit v1.2.1