allow enabling and disabling if tax_rate rows in groups (RT 3566)
authorjeff <jeff>
Wed, 18 Jun 2008 18:50:46 +0000 (18:50 +0000)
committerjeff <jeff>
Wed, 18 Jun 2008 18:50:46 +0000 (18:50 +0000)
httemplate/browse/tax_rate.cgi
httemplate/misc/enable_or_disable_tax.html [new file with mode: 0755]
httemplate/misc/process/enable_or_disable_tax.html [new file with mode: 0755]

index 5d43d59..71cfb28 100755 (executable)
@@ -3,6 +3,7 @@
      'name_singular'  => 'tax rate',
      'menubar'        => \@menubar,
      'html_init'      => $html_init,
+     'html_form'      => $html_form,
      'query'          => {
                            'table'     => 'tax_rate',
                            'hashref'   => $hashref,
@@ -179,7 +180,6 @@ if ($tax_type || $tax_cat ) {
 $cgi->delete('tax_type');
 $cgi->delete('tax_cat');
 
-
 if ( $geocode || $taxclassnum ) {
   push @menubar, 'View all tax rates' => $p.'browse/tax_rate.cgi';
 }
@@ -193,6 +193,21 @@ $cgi->param('taxclassnum', $taxclassnum ) if $taxclassnum;
 $cgi->param('tax_type', $tax_type ) if $tax_type;
 $cgi->param('tax_cat', $tax_cat ) if $tax_cat;
 
+my $html_form = include('/elements/init_overlib.html'). '<BR><BR>'.
+  join(' ',
+    map {
+      include('/elements/popup_link.html',
+               {
+                 'action' => $p. "misc/enable_or_disable_tax.html?action=$_&".
+                             $cgi->query_string,
+                 'label' => ucfirst($_). ' all these taxes',
+                 'actionlabel' => ucfirst($_). ' taxes',
+               },
+             );
+    }
+    qw(disable enable)
+  );
+
 my $hashref = {};
 my $extra_sql = '';
 if ( $data_vendor ) {
diff --git a/httemplate/misc/enable_or_disable_tax.html b/httemplate/misc/enable_or_disable_tax.html
new file mode 100755 (executable)
index 0000000..0d4c051
--- /dev/null
@@ -0,0 +1,97 @@
+<% include('/elements/header-popup.html', ucfirst($action). ' Tax Rates') %>
+<% include('/elements/error.html') %>
+
+<FORM ACTION="<% popurl(1) %>process/enable_or_disable_tax.html" METHOD=POST>
+<INPUT TYPE="hidden" NAME="action" VALUE="<% $action %>">
+<INPUT TYPE="hidden" NAME="data_vendor" VALUE="<% $data_vendor %>">
+<INPUT TYPE="hidden" NAME="geocode" VALUE="<% $geocode %>">
+<INPUT TYPE="hidden" NAME="taxclassnum" VALUE="<% $taxclassnum %>">
+<INPUT TYPE="hidden" NAME="tax_type" VALUE="<% $tax_type %>">
+<INPUT TYPE="hidden" NAME="tax_cat" VALUE="<% $tax_cat %>">
+
+This will <B><% $action %></B> <% $count %> tax
+<% $count == 1 ? 'rate' : 'rates' %>.  Are you <B>certain</B> you want to do
+this?
+<BR><BR><INPUT TYPE="submit" VALUE="Yes">
+</FORM>
+
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
+
+my $action = '';
+if ( $cgi->param('action') =~ /^(\w+)$/ ) {
+  $action = $1;
+}
+
+my $data_vendor = '';
+if ( $cgi->param('data_vendor') =~ /^(\w+)$/ ) {
+  $data_vendor = $1;
+}
+
+my $geocode = '';
+if ( $cgi->param('geocode') =~ /^(\w+)$/ ) {
+  $geocode = $1;
+}
+
+my $taxclassnum = '';
+if ( $cgi->param('taxclassnum') =~ /^(\d+)$/ ) {
+  $taxclassnum = $1;
+  my $tax_class = qsearchs('tax_class', {'taxclassnum' => $taxclassnum});
+  $taxclassnum = ''
+    unless ($tax_class);
+}
+
+my $tax_type = $1
+  if ( $cgi->param('tax_type') =~ /^(\d+)$/ );
+my $tax_cat = $1
+  if ( $cgi->param('tax_cat') =~ /^(\d+)$/ );
+
+my @taxclassnum = ();
+if ($tax_type || $tax_cat ) {
+  my $compare = "LIKE '". ( $tax_type || "%" ). ":". ( $tax_cat || "%" ). "'";
+  $compare = "= '$tax_type:$tax_cat'" if ($tax_type && $tax_cat);
+  my @tax_class =
+    qsearch({ 'table'     => 'tax_class',
+              'hashref'   => {},
+              'extra_sql' => "WHERE taxclass $compare",
+           });
+  if (@tax_class) {
+    @taxclassnum = map { $_->taxclassnum } @tax_class;
+    $tax_class[0]->description =~ /^(.*):(.*)/;
+  }else{
+    $tax_type = '';
+    $tax_cat = '';
+  }
+}
+
+my $extra_sql = '';
+if ( $data_vendor ) {
+  $extra_sql .= ' WHERE data_vendor = '. dbh->quote($data_vendor);
+}
+
+if ( $geocode ) {
+  $extra_sql .= ( $extra_sql =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
+                ' geocode LIKE '. dbh->quote($geocode.'%');
+}
+
+if ( $taxclassnum ) {
+  $extra_sql .= ( $extra_sql =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
+                ' taxclassnum  = '. dbh->quote($taxclassnum);
+}
+
+if ( @taxclassnum ) {
+  $extra_sql .= ( $extra_sql =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
+                join(' OR ', map { " taxclassnum  = $_ " } @taxclassnum );
+}
+
+my $count_query = "SELECT COUNT(*) FROM tax_rate $extra_sql";
+
+my $count_sth = dbh->prepare($count_query)
+  or die "Error preparing $count_query: ". dbh->errstr;
+$count_sth->execute
+  or die "Error executing $count_query: ". $count_sth->errstr;
+my $count = $count_sth->fetchrow_arrayref->[0];
+
+</%init>
diff --git a/httemplate/misc/process/enable_or_disable_tax.html b/httemplate/misc/process/enable_or_disable_tax.html
new file mode 100755 (executable)
index 0000000..a46a35b
--- /dev/null
@@ -0,0 +1,105 @@
+%if ($error) {
+<% $cgi->redirect(popurl(2).'enable_or_disable_tax.html?'.$cgi->query_string) %>
+%}else{
+  <% include('/elements/header-popup.html', $title) %>
+
+  <SCRIPT TYPE="text/javascript">
+    window.top.location.reload();
+  </SCRIPT>
+
+  </BODY>
+  </HTML>
+%}
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
+
+my $action = '';
+if ( $cgi->param('action') =~ /^(\w+)$/ ) {
+  $action = $1;
+}
+
+my $data_vendor = '';
+if ( $cgi->param('data_vendor') =~ /^(\w+)$/ ) {
+  $data_vendor = $1;
+}
+
+my $geocode = '';
+if ( $cgi->param('geocode') =~ /^(\w+)$/ ) {
+  $geocode = $1;
+}
+
+my $taxclassnum = '';
+if ( $cgi->param('taxclassnum') =~ /^(\d+)$/ ) {
+  $taxclassnum = $1;
+  my $tax_class = qsearchs('tax_class', {'taxclassnum' => $taxclassnum});
+  $taxclassnum = ''
+    unless ($tax_class);
+}
+
+my $tax_type = $1
+  if ( $cgi->param('tax_type') =~ /^(\d+)$/ );
+my $tax_cat = $1
+  if ( $cgi->param('tax_cat') =~ /^(\d+)$/ );
+
+my @taxclassnum = ();
+if ($tax_type || $tax_cat ) {
+  my $compare = "LIKE '". ( $tax_type || "%" ). ":". ( $tax_cat || "%" ). "'";
+  $compare = "= '$tax_type:$tax_cat'" if ($tax_type && $tax_cat);
+  my @tax_class =
+    qsearch({ 'table'     => 'tax_class',
+              'hashref'   => {},
+              'extra_sql' => "WHERE taxclass $compare",
+           });
+  if (@tax_class) {
+    @taxclassnum = map { $_->taxclassnum } @tax_class;
+    $tax_class[0]->description =~ /^(.*):(.*)/;
+  }else{
+    $tax_type = '';
+    $tax_cat = '';
+  }
+}
+
+my $extra_sql = '';
+if ( $data_vendor ) {
+  $extra_sql .= ' WHERE data_vendor = '. dbh->quote($data_vendor);
+}
+
+if ( $geocode ) {
+  $extra_sql .= ( $extra_sql =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
+                ' geocode LIKE '. dbh->quote($geocode.'%');
+}
+
+if ( $taxclassnum ) {
+  $extra_sql .= ( $extra_sql =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
+                ' taxclassnum  = '. dbh->quote($taxclassnum);
+}
+
+if ( @taxclassnum ) {
+  $extra_sql .= ( $extra_sql =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
+                join(' OR ', map { " taxclassnum  = $_ " } @taxclassnum );
+}
+
+my @tax_rate = qsearch({ 'table' => 'tax_rate',
+                         'hashref' => {},
+                         'extra_sql' => $extra_sql,
+                      });
+
+#transaction?
+my $error;
+$error = "Invalid action" unless ($action =~ /enable|disable/);
+
+foreach my $tax_rate (@tax_rate) {
+  $action eq 'enable' ? $tax_rate->setuptax('') : $tax_rate->setuptax('Y');
+  $action eq 'enable' ? $tax_rate->recurtax('') : $tax_rate->recurtax('Y');
+  # $tax_rate->manual('Y');
+  $error ||= $tax_rate->replace;
+  last if $error;
+}
+$cgi->param('error', $error) if $error;
+
+my $title = scalar(@tax_rate) == 1 ? 'Tax rate ' : 'Tax rates ';
+$title .= lc($action). 'd';
+
+</%init>