a108276214a78e8160c96f90f4409f66b6272585
[freeside.git] / httemplate / edit / process / cust_main_county-expand.cgi
1 <% include('/elements/header-popup.html', 'Addition successful' ) %>
2
3 <SCRIPT TYPE="text/javascript">
4   window.top.location.reload();
5 </SCRIPT>
6
7 </BODY>
8 </HTML>
9 <%init>
10
11 die "access denied"
12   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
13
14 $cgi->param('taxnum') =~ /^(\d+)$/ or die "Illegal taxnum!";
15 my $taxnum = $1;
16 my $cust_main_county = qsearchs('cust_main_county',{'taxnum'=>$taxnum})
17   or die ("Unknown taxnum!");
18
19 my @expansion;
20 if ( $cgi->param('taxclass') ) {
21   my $sth = dbh->prepare('SELECT taxclass FROM part_pkg_taxclass')
22     or die dbh->errstr;
23   $sth->execute or die $sth->errstr;
24   @expansion = map $_->[0], @{$sth->fetchall_arrayref};
25   errorpage "No taxclasses - add one first" unless @expansion;
26 } else {
27   @expansion = split /[\n\r]{1,2}/, $cgi->param('expansion');
28
29   #warn scalar(@expansion);
30   #warn "$_: $expansion[$_]\n" foreach (0..$#expansion);
31
32   @expansion=map {
33     unless ( /^\s*([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]+)\s*$/ ) {
34       $cgi->param('error', "Illegal item in expansion: $_");
35       print $cgi->redirect(popurl(2). "cust_main_county-expand.cgi?". $cgi->query_string );
36       myexit();
37     }
38     $1;
39   } @expansion;
40
41 }
42
43 foreach ( @expansion) {
44   my(%hash)=$cust_main_county->hash;
45   my($new)=new FS::cust_main_county \%hash;
46   $new->setfield('taxnum','');
47   if ( $cgi->param('taxclass') ) {
48     $new->setfield('taxclass', $_);
49   } elsif ( ! $cust_main_county->state ) {
50     $new->setfield('state',$_);
51   } elsif ( ! $cust_main_county->county ) {
52     $new->setfield('county',$_);
53   } else {
54     #uppercase cities in the US to try and agree with USPS validation
55     $new->setfield('city', $new->country eq 'US' ? uc($_) : $_ );
56   }
57   my $error = $new->insert;
58   die $error if $error;
59 }
60
61 unless ( qsearch( 'cust_main', {
62                                  'city'    => $cust_main_county->city,
63                                  'county'  => $cust_main_county->county,
64                                  'state'   => $cust_main_county->state,
65                                  'country' => $cust_main_county->country,
66                                } )
67          || ! @expansion
68 ) {
69   my $error = $cust_main_county->delete;
70   die $error if $error;
71 }
72
73 if ( $cgi->param('taxclass') ) {
74   print $cgi->redirect(popurl(3). "browse/cust_main_county.cgi?".
75                          'city='.    uri_escape($cust_main_county->city   ).';'.
76                          'county='.  uri_escape($cust_main_county->county ).';'.
77                          'state='.   uri_escape($cust_main_county->state  ).';'.
78                          'country='. uri_escape($cust_main_county->country)
79                       );
80   myexit;
81 }
82
83 </%init>