2 # $Id: cust_main.cgi,v 1.5 2001-10-20 12:18:00 ivan Exp $
5 use vars qw( $cgi $payby @invoicing_list $new $custnum $error );
6 use vars qw( $cust_pkg $cust_svc $svc_acct );
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 );
23 $cgi->param('tax','') unless defined $cgi->param('tax');
25 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
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);
33 $cgi->param('ship_state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/
34 or $cgi->param('ship_state') =~ /^(((())))$/
35 or die "Oops, illegal \"ship_state\" param: ". $cgi->param('ship_state');
36 $cgi->param('ship_state', $1);
37 $cgi->param('ship_county', $3 || '');
38 $cgi->param('ship_country', $4);
40 if ( $payby = $cgi->param('payby') ) {
41 $cgi->param('payinfo', $cgi->param( $payby. '_payinfo' ) );
42 $cgi->param('paydate',
43 $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' ) );
44 $cgi->param('payname', $cgi->param( $payby. '_payname' ) );
47 $cgi->param('otaker', &getotaker );
49 @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
50 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
52 #create new record object
54 $new = new FS::cust_main ( {
56 $_, scalar($cgi->param($_))
57 # } qw(custnum agentnum last first ss company address1 address2 city county
58 # state zip daytime night fax payby payinfo paydate payname tax
63 if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
64 $new->setfield("ship_$_", '') foreach qw(
65 last first company address1 address2 city county state zip
66 country daytime night fax
70 #perhaps this stuff should go to cust_main.pm
73 if ( $new->custnum eq '' ) {
75 if ( $cgi->param('pkgpart_svcpart') ) {
76 my $x = $cgi->param('pkgpart_svcpart');
77 $x =~ /^(\d+)_(\d+)$/;
78 my($pkgpart, $svcpart) = ($1, $2);
79 #false laziness: copied from FS::cust_pkg::order (which should become a
80 #FS::cust_main method)
83 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
84 my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
86 #foreach $type_pkgs ( qsearch('type_pkgs',{'typenum'=> $agent->typenum }) ) {
87 # my($pkgpart)=$type_pkgs->pkgpart;
88 # $part_pkg{$pkgpart}++;
90 # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
91 my $pkgpart_href = $agent->pkgpart_hashref;
94 # this should wind up in FS::cust_pkg!
95 $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum. ") can't".
96 "purchase pkgpart ". $pkgpart
97 #unless $part_pkg{ $pkgpart };
98 unless $pkgpart_href->{ $pkgpart };
100 $cust_pkg = new FS::cust_pkg ( {
101 #later 'custnum' => $custnum,
102 'pkgpart' => $pkgpart,
104 $error ||= $cust_pkg->check;
106 #$cust_svc = new FS::cust_svc ( { 'svcpart' => $svcpart } );
108 #$error ||= $cust_svc->check;
110 $svc_acct = new FS::svc_acct ( {
111 'svcpart' => $svcpart,
112 'username' => $cgi->param('username'),
113 '_password' => $cgi->param('_password'),
114 'popnum' => $cgi->param('popnum'),
117 my $y = $svc_acct->setdefault; # arguably should be in new method
118 $error ||= $y unless ref($y);
119 #and just in case you were silly
120 $svc_acct->svcpart($svcpart);
121 $svc_acct->username($cgi->param('username'));
122 $svc_acct->_password($cgi->param('_password'));
123 $svc_acct->popnum($cgi->param('popnum'));
125 $error ||= $svc_acct->check;
127 } elsif ( $cgi->param('username') ) { #good thing to catch
128 $error = "Can't assign username without a package!";
132 tie my %hash, 'Tie::RefHash';
133 %hash = ( $cust_pkg => [ $svc_acct ] ) if $cust_pkg;
134 $error ||= $new->insert( \%hash, \@invoicing_list );
135 } else { #create old record object
136 my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } );
137 $error ||= "Old record not found!" unless $old;
138 $error ||= $new->replace($old, \@invoicing_list);
142 $cgi->param('error', $error);
143 print $cgi->redirect(popurl(2). "cust_main.cgi?". $cgi->query_string );
145 $custnum = $new->custnum;
146 print $cgi->redirect(popurl(3). "view/cust_main.cgi?$custnum");