2 # $Id: cust_main.cgi,v 1.2 2001-08-12 00:07:55 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 if ( $payby = $cgi->param('payby') ) {
34 $cgi->param('payinfo', $cgi->param( $payby. '_payinfo' ) );
35 $cgi->param('paydate',
36 $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' ) );
37 $cgi->param('payname', $cgi->param( $payby. '_payname' ) );
40 $cgi->param('otaker', &getotaker );
42 @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
43 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
45 #create new record object
47 $new = new FS::cust_main ( {
49 $_, scalar($cgi->param($_))
50 # } qw(custnum agentnum last first ss company address1 address2 city county
51 # state zip daytime night fax payby payinfo paydate payname tax
56 if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
57 $new->setfield("ship_$_", '') foreach qw(
58 last first company address1 address2 city county state zip
59 country daytime night fax
63 #perhaps this stuff should go to cust_main.pm
66 if ( $new->custnum eq '' ) {
68 if ( $cgi->param('pkgpart_svcpart') ) {
69 my $x = $cgi->param('pkgpart_svcpart');
70 $x =~ /^(\d+)_(\d+)$/;
71 my($pkgpart, $svcpart) = ($1, $2);
72 #false laziness: copied from FS::cust_pkg::order (which should become a
73 #FS::cust_main method)
76 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
77 my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
79 #foreach $type_pkgs ( qsearch('type_pkgs',{'typenum'=> $agent->typenum }) ) {
80 # my($pkgpart)=$type_pkgs->pkgpart;
81 # $part_pkg{$pkgpart}++;
83 # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
84 my $pkgpart_href = $agent->pkgpart_hashref;
87 # this should wind up in FS::cust_pkg!
88 $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum. ") can't".
89 "purchase pkgpart ". $pkgpart
90 #unless $part_pkg{ $pkgpart };
91 unless $pkgpart_href->{ $pkgpart };
93 $cust_pkg = new FS::cust_pkg ( {
94 #later 'custnum' => $custnum,
95 'pkgpart' => $pkgpart,
97 $error ||= $cust_pkg->check;
99 #$cust_svc = new FS::cust_svc ( { 'svcpart' => $svcpart } );
101 #$error ||= $cust_svc->check;
103 $svc_acct = new FS::svc_acct ( {
104 'svcpart' => $svcpart,
105 'username' => $cgi->param('username'),
106 '_password' => $cgi->param('_password'),
107 'popnum' => $cgi->param('popnum'),
110 my $y = $svc_acct->setdefault; # arguably should be in new method
111 $error ||= $y unless ref($y);
112 #and just in case you were silly
113 $svc_acct->svcpart($svcpart);
114 $svc_acct->username($cgi->param('username'));
115 $svc_acct->_password($cgi->param('_password'));
116 $svc_acct->popnum($cgi->param('popnum'));
118 $error ||= $svc_acct->check;
120 } elsif ( $cgi->param('username') ) { #good thing to catch
121 $error = "Can't assign username without a package!";
125 tie my %hash, 'Tie::RefHash';
126 %hash = ( $cust_pkg => [ $svc_acct ] );
127 $error ||= $new->insert( \%hash, \@invoicing_list );
128 } else { #create old record object
129 my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } );
130 $error ||= "Old record not found!" unless $old;
131 $error ||= $new->replace($old, \@invoicing_list);
135 $cgi->param('error', $error);
136 print $cgi->redirect(popurl(2). "cust_main.cgi?". $cgi->query_string );
138 $custnum = $new->custnum;
139 print $cgi->redirect(popurl(3). "view/cust_main.cgi?$custnum");