update the tax class editor to enable taxclass adding, RT#2929
[freeside.git] / httemplate / edit / process / part_pkg_taxclass.html
1 % if ( $error ) {
2 %  $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(2). "part_pkg_taxclass.html?". $cgi->query_string ) %>
4 %} else {
5 <% $cgi->redirect(popurl(3). "browse/cust_main_county.cgi?taxclass=". uri_escape($part_pkg_taxclass->taxclass) ) %>
6 %}
7 <%init>
8
9 die "access denied"
10   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
11
12 my $part_pkg_taxclass = new FS::part_pkg_taxclass {
13   'taxclass' => $cgi->param('taxclass'),
14 };
15
16 #maybe this whole thing should be in a transaction.  at some point, no biggie
17 #none of the follow-up stuff will fail unless there's a more serious problem
18 #than a hanging record in part_pkg_taxclass...
19
20 my $error = $part_pkg_taxclass->insert;
21
22 unless ( $error ) {
23   #auto-add the new taxclass to any regions that have taxclasses already
24
25   my $sth = dbh->prepare("
26     SELECT country, state, county FROM cust_main_county
27       WHERE taxclass IS NOT NULL AND taxclass != ''
28       GROUP BY country, state, county
29   ") or die dbh->errstr;
30   $sth->execute or die $sth->errstr;
31
32   while ( my $row = $sth->fetchrow_hashref ) {
33     warn "inserting for $row";
34     my $cust_main_county = new FS::cust_main_county {
35       'country'  => $row->{country},
36       'state'    => $row->{state},
37       'county'   => $row->{county},
38       'tax'      => 0,
39       'taxclass' => $part_pkg_taxclass->taxclass,
40       #exempt_amount
41       #taxname
42       #setuptax
43       #recurtax
44     };
45     $error = $cust_main_county->insert;
46     #last if $error;
47     die $error if $error;
48   }
49
50
51 }
52
53 </%init>