summaryrefslogtreecommitdiff
path: root/httemplate
diff options
context:
space:
mode:
authorjeff <jeff>2008-04-15 20:47:59 +0000
committerjeff <jeff>2008-04-15 20:47:59 +0000
commit6a24254d490f3d023728044daba0765f20f6971e (patch)
treec486026468a4e33092ae54925ff19b8e5dc7411b /httemplate
parentbdbfd5c5a3bb7bc193b82dc39b98ae9ffe99da44 (diff)
(finally) wrap up new tax rate engine (for now)
Diffstat (limited to 'httemplate')
-rw-r--r--httemplate/elements/file-upload.html80
-rw-r--r--httemplate/elements/header-minimal.html19
-rw-r--r--httemplate/misc/file-upload.html47
-rw-r--r--httemplate/misc/process/tax-import.cgi55
-rw-r--r--httemplate/misc/process/tax-upgrade.cgi147
-rw-r--r--httemplate/misc/tax-import.cgi70
6 files changed, 342 insertions, 76 deletions
diff --git a/httemplate/elements/file-upload.html b/httemplate/elements/file-upload.html
new file mode 100644
index 0000000..2859a67
--- /dev/null
+++ b/httemplate/elements/file-upload.html
@@ -0,0 +1,80 @@
+
+<script type="text/javascript">
+
+ function doUpload(form, callback) {
+ var name = 'form' + Math.floor(Math.random() * 99999); // perlize?
+ var d = document.createElement('DIV');
+ d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+name+'" name="'+name+'" onload="uploadComplete(\''+name+'\')"></iframe>';
+ document.body.appendChild(d);
+
+ var i = document.getElementById(name);
+ if (callback && typeof(callback) == 'function') {
+ i.onComplete = callback;
+ }
+
+ form.setAttribute('target', name);
+ return true;
+ }
+
+ function uploadComplete(id) {
+ var i = document.getElementById(id);
+ if (i.contentDocument) {
+ var d = i.contentDocument;
+ } else if (i.contentWindow) {
+ var d = i.contentWindow.document;
+ } else {
+ var d = window.frames[id].document;
+ }
+ if (d.location.href == "about:blank") {
+ return;
+ }
+
+ document.getElementById('r').innerHTML = d.body.innerHTML;
+ if (typeof(i.onComplete) == 'function') {
+ var p;
+ if (p = d.body.innerHTML.indexOf("Freeside File Upload Successful ") >= 0) {
+ var v = d.body.innerHTML.substr(p+33)
+ var u = document.getElementById('uploaded_files');
+ v = v.substr(0, v.indexOf(';'));
+ u.value = v;
+ i.onComplete(true, '');
+ }else{
+ i.onComplete(false, d.body.innerHTML);
+ }
+ }
+ }
+
+</script>
+
+ <input type="hidden" name="uploaded_files" id="uploaded_files" value="" />
+ <input type="hidden" name="upload_fields" value="<% join(',', @field) %>" />
+% foreach (@field) {
+ <tr>
+ <th><% shift @label %></th>
+ <td><input type="file" name="<% $_ %>" /></td>
+ </tr>
+% }
+ <div style="display:<% $debug ? 'visible' : 'none' %>">Debugging: <pre id="r"></pre></div>
+
+<%init>
+my %param = @_;
+
+my $debug = $param{'debug'};
+
+my $callback = $param{'callback'} || "''";
+
+my @label = ();
+if ( ref($param{'label'}) ) {
+ push @label, @{$param{'label'}};
+}else{
+ push @label, $param{'label'};
+}
+
+my @field = ();
+if ( ref($param{'field'}) ) {
+ push @field, @{$param{'field'}};
+}else{
+ push @field, $param{'field'};
+}
+
+</%init>
diff --git a/httemplate/elements/header-minimal.html b/httemplate/elements/header-minimal.html
new file mode 100644
index 0000000..f74a9cc
--- /dev/null
+++ b/httemplate/elements/header-minimal.html
@@ -0,0 +1,19 @@
+%
+% my($title, $menubar) = ( shift, shift ); #$menubar is unused here though
+% my $etc = @_ ? shift : ''; #$etc is for things like onLoad= etc.
+% my $head = @_ ? shift : ''; #$head is for things that go in the <HEAD> section
+% my $conf = new FS::Conf;
+%
+
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<HTML>
+ <HEAD>
+ <TITLE>
+ <% $title %>
+ </TITLE>
+ <META HTTP-Equiv="Cache-Control" Content="no-cache">
+ <META HTTP-Equiv="Pragma" Content="no-cache">
+ <META HTTP-Equiv="Expires" Content="0">
+ <% $head %>
+ </HEAD>
+ <BODY BGCOLOR="#e8e8e8" <% $etc %>>
diff --git a/httemplate/misc/file-upload.html b/httemplate/misc/file-upload.html
new file mode 100644
index 0000000..9649d36
--- /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);
+
+</%init>
diff --git a/httemplate/misc/process/tax-import.cgi b/httemplate/misc/process/tax-import.cgi
index 77fba61..f66d6db 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;
</%init>
diff --git a/httemplate/misc/process/tax-upgrade.cgi b/httemplate/misc/process/tax-upgrade.cgi
new file mode 100644
index 0000000..8782282
--- /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;
+}
+
+</%init>
diff --git a/httemplate/misc/tax-import.cgi b/httemplate/misc/tax-import.cgi
index 6bdea6a..9044ac9 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.
<BR><BR>
-<FORM ACTION="process/tax-import.cgi" METHOD="post" ENCTYPE="multipart/form-data">
+<% include( '/elements/progress-init.html',
+ 'TaxRateUpload',
+ [ 'format', 'uploaded_files' ],
+ 'process/tax-import.cgi',
+ { 'message' => 'Tax rates imported' },
+ )
+%>
-<% &ntable("#cccccc", 2) %>
+<SCRIPT>
+
+ function gotLoaded(success, message) {
+
+ var uploaded = document.getElementById('uploaded_files');
+ var a = uploaded.value.split(',');
+ if (uploaded.value.split(',').length == 4){
+ process();
+ }else{
+ var p = document.getElementById('uploadError');
+ p.innerHTML='<FONT SIZE="+1" COLOR="#ff0000">Error: '+message+'</FONT><BR><BR>';
+ p.style='display:visible';
+ return false;
+ }
+
+ }
+
+</SCRIPT>
+
+<div style="display:none:" id="uploadError"></div>
+<FORM NAME="TaxRateUpload" ACTION="<% $fsurl %>misc/file-upload.html" METHOD="post" ENCTYPE="multipart/form-data" onsubmit="return doUpload(this, gotLoaded )">
+<% &ntable("#cccccc", 2) %>
<TR>
<TH ALIGN="right">Format</TH>
<TD>
<SELECT NAME="format">
- <OPTION VALUE="cch" SELECTED>CCH
+ <OPTION VALUE="cch-update" SELECTED>CCH update
+ <OPTION VALUE="cch">CCH initial import
</SELECT>
</TD>
</TR>
-<TR>
- <TH ALIGN="right">code CSV filename</TH>
- <TD><INPUT TYPE="file" NAME="codefile"></TD>
-</TR>
-
-<TR>
- <TH ALIGN="right">plus4 CSV filename</TH>
- <TD><INPUT TYPE="file" NAME="plus4file"></TD>
-</TR>
-
-<TR>
- <TH ALIGN="right">txmatrix CSV filename</TH>
- <TD><INPUT TYPE="file" NAME="txmatrix"></TD>
-</TR>
-
-<TR>
- <TH ALIGN="right">detail CSV filename</TH>
- <TD><INPUT TYPE="file" NAME="detail"></TD>
-</TR>
-
+<% 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,
+ )
+%>
-<TR><TD COLSPAN=2 ALIGN="center" STYLE="padding-top:6px"><INPUT TYPE="submit" VALUE="Import CSV files"></TD></TR>
+<TR><TD COLSPAN=2 ALIGN="center" STYLE="padding-top:6px"><INPUT TYPE="submit" VALUE="Import CSV files" onClick="document.TaxRateUpload.submit.disabled=true;"></TD></TR>
</TABLE>