s/create/new/g; and use fields('table_name')
[freeside.git] / htdocs / edit / process / cust_main_county-expand.cgi
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: cust_main_county-expand.cgi,v 1.4 1999-01-18 22:47:52 ivan Exp $
4 #
5 # ivan@sisd.com 97-dec-16
6 #
7 # Changes to allow page to work at a relative position in server
8 # Added import of datasrc from UID.pm for Pg6.3
9 # Default tax to 0.0 if using Pg6.3
10 #       bmccane@maxbaud.net     98-apr-3
11 #
12 # lose background, FS::CGI
13 # undo default tax to 0.0 if using Pg6.3: comes from pre-expanded record
14 # for that state
15 # ivan@sisd.com 98-sep-2
16 #
17 # $Log: cust_main_county-expand.cgi,v $
18 # Revision 1.4  1999-01-18 22:47:52  ivan
19 # s/create/new/g; and use fields('table_name')
20 #
21 # Revision 1.3  1998/12/17 08:40:20  ivan
22 # s/CGI::Request/CGI.pm/; etc
23 #
24 # Revision 1.2  1998/11/18 09:01:40  ivan
25 # i18n! i18n!
26 #
27
28 use strict;
29 use CGI;
30 use CGI::Carp qw(fatalsToBrowser);
31 use FS::UID qw(cgisuidsetup datasrc);
32 use FS::Record qw(qsearch qsearchs);
33 use FS::CGI qw(eidiot popurl);
34 use FS::cust_main_county;
35 use FS::cust_main;
36
37 my($cgi)=new CGI;
38
39 &cgisuidsetup($cgi);
40
41 $cgi->param('taxnum') =~ /^(\d+)$/ or die "Illegal taxnum!";
42 my($taxnum)=$1;
43 my($cust_main_county)=qsearchs('cust_main_county',{'taxnum'=>$taxnum})
44   or die ("Unknown taxnum!");
45
46 my(@expansion);
47 if ( $cgi->param('delim') eq 'n' ) {
48   @expansion=split(/\n/,$cgi->param('expansion'));
49 } elsif ( $cgi->param('delim') eq 's' ) {
50   @expansion=split(/\s+/,$cgi->param('expansion'));
51 } else {
52   die "Illegal delim!";
53 }
54
55 @expansion=map {
56   /^\s*([\w\- ]+)\s*$/ or eidiot("Illegal expansion");
57   $1;
58 } @expansion;
59
60 my($expansion);
61 foreach ( @expansion) {
62   my(%hash)=$cust_main_county->hash;
63   my($new)=new FS::cust_main_county \%hash;
64   $new->setfield('taxnum','');
65   if ( ! $cust_main_county->state ) {
66     $new->setfield('state',$_);
67   } else {
68     $new->setfield('county',$_);
69   }
70   #if (datasrc =~ m/Pg/)
71   #{
72   #    $new->setfield('tax',0.0);
73   #}
74   my($error)=$new->insert;
75   die $error if $error;
76 }
77
78 unless ( qsearch('cust_main',{
79   'state'  => $cust_main_county->getfield('state'),
80   'county' => $cust_main_county->getfield('county'),
81   'country' =>  $cust_main_county->getfield('country'),
82 } ) ) {
83   my($error)=($cust_main_county->delete);
84   die $error if $error;
85 }
86
87 print $cgi->redirect(popurl(3). "edit/cust_main_county.cgi");
88