allow enabling and disabling if tax_rate rows in groups (RT 3566)
[freeside.git] / httemplate / misc / process / enable_or_disable_tax.html
1 %if ($error) {
2 <% $cgi->redirect(popurl(2).'enable_or_disable_tax.html?'.$cgi->query_string) %>
3 %}else{
4   <% include('/elements/header-popup.html', $title) %>
5
6   <SCRIPT TYPE="text/javascript">
7     window.top.location.reload();
8   </SCRIPT>
9
10   </BODY>
11   </HTML>
12 %}
13 <%init>
14
15 die "access denied"
16   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
17
18 my $action = '';
19 if ( $cgi->param('action') =~ /^(\w+)$/ ) {
20   $action = $1;
21 }
22
23 my $data_vendor = '';
24 if ( $cgi->param('data_vendor') =~ /^(\w+)$/ ) {
25   $data_vendor = $1;
26 }
27
28 my $geocode = '';
29 if ( $cgi->param('geocode') =~ /^(\w+)$/ ) {
30   $geocode = $1;
31 }
32
33 my $taxclassnum = '';
34 if ( $cgi->param('taxclassnum') =~ /^(\d+)$/ ) {
35   $taxclassnum = $1;
36   my $tax_class = qsearchs('tax_class', {'taxclassnum' => $taxclassnum});
37   $taxclassnum = ''
38     unless ($tax_class);
39 }
40
41 my $tax_type = $1
42   if ( $cgi->param('tax_type') =~ /^(\d+)$/ );
43 my $tax_cat = $1
44   if ( $cgi->param('tax_cat') =~ /^(\d+)$/ );
45
46 my @taxclassnum = ();
47 if ($tax_type || $tax_cat ) {
48   my $compare = "LIKE '". ( $tax_type || "%" ). ":". ( $tax_cat || "%" ). "'";
49   $compare = "= '$tax_type:$tax_cat'" if ($tax_type && $tax_cat);
50   my @tax_class =
51     qsearch({ 'table'     => 'tax_class',
52               'hashref'   => {},
53               'extra_sql' => "WHERE taxclass $compare",
54            });
55   if (@tax_class) {
56     @taxclassnum = map { $_->taxclassnum } @tax_class;
57     $tax_class[0]->description =~ /^(.*):(.*)/;
58   }else{
59     $tax_type = '';
60     $tax_cat = '';
61   }
62 }
63
64 my $extra_sql = '';
65 if ( $data_vendor ) {
66   $extra_sql .= ' WHERE data_vendor = '. dbh->quote($data_vendor);
67 }
68
69 if ( $geocode ) {
70   $extra_sql .= ( $extra_sql =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
71                 ' geocode LIKE '. dbh->quote($geocode.'%');
72 }
73
74 if ( $taxclassnum ) {
75   $extra_sql .= ( $extra_sql =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
76                 ' taxclassnum  = '. dbh->quote($taxclassnum);
77 }
78
79 if ( @taxclassnum ) {
80   $extra_sql .= ( $extra_sql =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
81                 join(' OR ', map { " taxclassnum  = $_ " } @taxclassnum );
82 }
83
84 my @tax_rate = qsearch({ 'table' => 'tax_rate',
85                          'hashref' => {},
86                          'extra_sql' => $extra_sql,
87                       });
88
89 #transaction?
90 my $error;
91 $error = "Invalid action" unless ($action =~ /enable|disable/);
92
93 foreach my $tax_rate (@tax_rate) {
94   $action eq 'enable' ? $tax_rate->setuptax('') : $tax_rate->setuptax('Y');
95   $action eq 'enable' ? $tax_rate->recurtax('') : $tax_rate->recurtax('Y');
96   # $tax_rate->manual('Y');
97   $error ||= $tax_rate->replace;
98   last if $error;
99 }
100 $cgi->param('error', $error) if $error;
101
102 my $title = scalar(@tax_rate) == 1 ? 'Tax rate ' : 'Tax rates ';
103 $title .= lc($action). 'd';
104
105 </%init>