clean up tax rate editing:
[freeside.git] / httemplate / edit / process / cust_main.cgi
1 <%
2 # $Id: cust_main.cgi,v 1.3 2001-08-13 23:10:34 ivan Exp $
3
4 use strict;
5 use vars qw( $cgi $payby @invoicing_list $new $custnum $error );
6 use vars qw( $cust_pkg $cust_svc $svc_acct );
7 use CGI;
8 use CGI::Carp qw(fatalsToBrowser);
9 use FS::UID qw(cgisuidsetup getotaker);
10 use FS::CGI qw( popurl );
11 use FS::Record qw( qsearch qsearchs fields );
12 use FS::cust_main;
13 use FS::type_pkgs;
14 use FS::agent;
15
16 $cgi = new CGI;
17 &cgisuidsetup($cgi);
18
19 $error = '';
20
21 #unmunge stuff
22
23 $cgi->param('tax','') unless defined($cgi->param('tax'));
24
25 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
26
27 $cgi->param('state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/
28   or die "Oops, illegal \"state\" param: ". $cgi->param('state');
29 $cgi->param('state', $1);
30 $cgi->param('county', $3 || '');
31 $cgi->param('country', $4);
32
33 $cgi->param('ship_state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/
34   or die "Oops, illegal \"ship_state\" param: ". $cgi->param('ship_state');
35 $cgi->param('ship_state', $1);
36 $cgi->param('ship_county', $3 || '');
37 $cgi->param('ship_country', $4);
38
39 if ( $payby = $cgi->param('payby') ) {
40   $cgi->param('payinfo', $cgi->param( $payby. '_payinfo' ) );
41   $cgi->param('paydate',
42   $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' ) );
43   $cgi->param('payname', $cgi->param( $payby. '_payname' ) );
44 }
45
46 $cgi->param('otaker', &getotaker );
47
48 @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
49 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
50
51 #create new record object
52
53 $new = new FS::cust_main ( {
54   map {
55     $_, scalar($cgi->param($_))
56 #  } qw(custnum agentnum last first ss company address1 address2 city county
57 #       state zip daytime night fax payby payinfo paydate payname tax
58 #       otaker refnum)
59   } fields('cust_main')
60 } );
61
62 if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
63   $new->setfield("ship_$_", '') foreach qw(
64     last first company address1 address2 city county state zip
65     country daytime night fax
66   );
67 }
68
69 #perhaps this stuff should go to cust_main.pm
70 $cust_pkg = '';
71 $svc_acct = '';
72 if ( $new->custnum eq '' ) {
73
74   if ( $cgi->param('pkgpart_svcpart') ) {
75     my $x = $cgi->param('pkgpart_svcpart');
76     $x =~ /^(\d+)_(\d+)$/;
77     my($pkgpart, $svcpart) = ($1, $2);
78     #false laziness: copied from FS::cust_pkg::order (which should become a
79     #FS::cust_main method)
80     my(%part_pkg);
81     # generate %part_pkg
82     # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
83     my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
84         #my($type_pkgs);
85         #foreach $type_pkgs ( qsearch('type_pkgs',{'typenum'=> $agent->typenum }) ) {
86         #  my($pkgpart)=$type_pkgs->pkgpart;
87         #  $part_pkg{$pkgpart}++;
88         #}
89     # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
90     my $pkgpart_href = $agent->pkgpart_hashref;
91     #eslaf
92
93     # this should wind up in FS::cust_pkg!
94     $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum. ") can't".
95                "purchase pkgpart ". $pkgpart
96       #unless $part_pkg{ $pkgpart };
97       unless $pkgpart_href->{ $pkgpart };
98
99     $cust_pkg = new FS::cust_pkg ( {
100       #later         'custnum' => $custnum,
101       'pkgpart' => $pkgpart,
102     } );
103     $error ||= $cust_pkg->check;
104
105     #$cust_svc = new FS::cust_svc ( { 'svcpart' => $svcpart } );
106
107     #$error ||= $cust_svc->check;
108
109     $svc_acct = new FS::svc_acct ( {
110                                      'svcpart'   => $svcpart,
111                                      'username'  => $cgi->param('username'),
112                                      '_password' => $cgi->param('_password'),
113                                      'popnum'    => $cgi->param('popnum'),
114                                    } );
115
116     my $y = $svc_acct->setdefault; # arguably should be in new method
117     $error ||= $y unless ref($y);
118     #and just in case you were silly
119     $svc_acct->svcpart($svcpart);
120     $svc_acct->username($cgi->param('username'));
121     $svc_acct->_password($cgi->param('_password'));
122     $svc_acct->popnum($cgi->param('popnum'));
123
124     $error ||= $svc_acct->check;
125
126   } elsif ( $cgi->param('username') ) { #good thing to catch
127     $error = "Can't assign username without a package!";
128   }
129
130   use Tie::RefHash;
131   tie my %hash, 'Tie::RefHash';
132   %hash = ( $cust_pkg => [ $svc_acct ] ) if $cust_pkg;
133   $error ||= $new->insert( \%hash, \@invoicing_list );
134 } else { #create old record object
135   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
136   $error ||= "Old record not found!" unless $old;
137   $error ||= $new->replace($old, \@invoicing_list);
138 }
139
140 if ( $error ) {
141   $cgi->param('error', $error);
142   print $cgi->redirect(popurl(2). "cust_main.cgi?". $cgi->query_string );
143 } else { 
144   $custnum = $new->custnum;
145   print $cgi->redirect(popurl(3). "view/cust_main.cgi?$custnum");
146
147 %>