one-screen new customer entry (including package and service) for simple
[freeside.git] / htdocs / edit / process / cust_main.cgi
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: cust_main.cgi,v 1.8 1999-03-25 13:55:10 ivan Exp $
4 #
5 # Usage: post form to:
6 #        http://server.name/path/cust_main.cgi
7 #
8 # ivan@voicenet.com 96-dec-04
9 #
10 # added referral check
11 # ivan@voicenet.com 97-jun-4
12 #
13 # rewrote for new API
14 # ivan@voicenet.com 97-jul-28
15 #
16 # same as above (again) and clean up some stuff ivan@sisd.com 98-feb-23
17 #
18 # Changes to allow page to work at a relative position in server
19 # Changed 'day' to 'daytime' because Pg6.3 reserves the day word
20 #       bmccane@maxbaud.net     98-apr-3
21 #
22 # $Log: cust_main.cgi,v $
23 # Revision 1.8  1999-03-25 13:55:10  ivan
24 # one-screen new customer entry (including package and service) for simple
25 # packages with one svc_acct service
26 #
27 # Revision 1.7  1999/02/28 00:03:42  ivan
28 # removed misleading comments
29 #
30 # Revision 1.6  1999/01/25 12:10:00  ivan
31 # yet more mod_perl stuff
32 #
33 # Revision 1.5  1999/01/19 05:13:50  ivan
34 # for mod_perl: no more top-level my() variables; use vars instead
35 # also the last s/create/new/;
36 #
37 # Revision 1.4  1999/01/18 09:22:32  ivan
38 # changes to track email addresses for email invoicing
39 #
40 # Revision 1.3  1998/12/17 08:40:19  ivan
41 # s/CGI::Request/CGI.pm/; etc
42 #
43 # Revision 1.2  1998/11/18 08:57:36  ivan
44 # i18n, s/CGI-modules/CGI.pm/, FS::CGI::idiot instead of inline, FS::CGI::popurl
45 #
46
47 use strict;
48 use vars qw( $cgi $payby @invoicing_list $new $custnum $error );
49 use vars qw( $cust_pkg $cust_svc $svc_acct );
50 use CGI;
51 use CGI::Carp qw(fatalsToBrowser);
52 use FS::UID qw(cgisuidsetup getotaker);
53 use FS::CGI qw( popurl );
54 use FS::Record qw( qsearch qsearchs fields );
55 use FS::cust_main;
56 use FS::type_pkgs;
57 use FS::agent;
58
59 $cgi = new CGI;
60 &cgisuidsetup($cgi);
61
62 #unmunge stuff
63
64 $cgi->param('tax','') unless defined($cgi->param('tax'));
65
66 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
67
68 $cgi->param('state') =~ /^(\w+)( \((\w+)\))? \/ (\w+)$/;
69 $cgi->param('state', $1);
70 $cgi->param('county', $3 || '');
71 $cgi->param('country', $4);
72
73 if ( $payby = $cgi->param('payby') ) {
74   $cgi->param('payinfo', $cgi->param( $payby. '_payinfo' ) );
75   $cgi->param('paydate',
76   $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' ) );
77   $cgi->param('payname', $cgi->param( $payby. '_payname' ) );
78 }
79
80 $cgi->param('otaker', &getotaker );
81
82 @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
83 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
84
85 #create new record object
86
87 $new = new FS::cust_main ( {
88   map {
89     $_, scalar($cgi->param($_))
90 #  } qw(custnum agentnum last first ss company address1 address2 city county
91 #       state zip daytime night fax payby payinfo paydate payname tax
92 #       otaker refnum)
93   } fields('cust_main')
94 } );
95
96 #perhaps the invocing_list magic should move to cust_main.pm?
97 $error = $new->check_invoicing_list( \@invoicing_list );
98
99 #perhaps this stuff should go to cust_main.pm as well
100 $cust_pkg = '';
101 $svc_acct = '';
102 if ( $new->custnum eq '' ) {
103
104   if ( $cgi->param('pkgpart_svcpart') ) {
105     my $x = $cgi->param('pkgpart_svcpart');
106     $x =~ /^(\d+)_(\d+)$/;
107     my($pkgpart, $svcpart) = ($1, $2);
108     #false laziness: copied from FS::cust_pkg::order (which should become a
109     #FS::cust_main method)
110     my(%part_pkg);
111     # generate %part_pkg
112     # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
113     my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
114     my($type_pkgs);
115     foreach $type_pkgs ( qsearch('type_pkgs',{'typenum'=> $agent->typenum }) ) {
116       my($pkgpart)=$type_pkgs->pkgpart;
117       $part_pkg{$pkgpart}++;
118     }
119     #eslaf
120
121     $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum. ") can't".
122                "purchase pkgpart ". $pkgpart
123       unless $part_pkg{ $pkgpart };
124
125     $cust_pkg = new FS::cust_pkg ( {
126                             #later         'custnum' => $custnum,
127                                      'pkgpart' => $pkgpart,
128                                    } );
129     $error ||= $cust_pkg->check;
130
131     #$cust_svc = new FS::cust_svc ( { 'svcpart' => $svcpart } );
132
133     #$error ||= $cust_svc->check;
134
135     $svc_acct = new FS::svc_acct ( {
136                                      'svcpart'   => $svcpart,
137                                      'username'  => $cgi->param('username'),
138                                      '_password' => $cgi->param('_password'),
139                                      'popnum'    => $cgi->param('popnum'),
140                                    } );
141
142     my $y = $svc_acct->setdefault; # arguably should be in new method
143     $error ||= $y unless ref($y);
144     #and just in case you were silly
145     $svc_acct->svcpart($svcpart);
146     $svc_acct->username($cgi->param('username'));
147     $svc_acct->_password($cgi->param('_password'));
148     $svc_acct->popnum($cgi->param('popnum'));
149
150     $error ||= $svc_acct->check;
151
152   } elsif ( $cgi->param('username') ) { #good thing to catch
153     $error = "Can't assign username without a package!";
154   }
155
156   $error ||= $new->insert;
157   if ( $cust_pkg && ! $error ) {
158     $cust_pkg->custnum( $new->custnum );
159     $error ||= $cust_pkg->insert; 
160     warn "WARNING: $error on pre-checked cust_pkg record!" if $error;
161     $svc_acct->pkgnum( $cust_pkg->pkgnum );
162     $error ||= $svc_acct->insert;
163     warn "WARNING: $error on pre-checked svc_acct record!" if $error;
164   }
165 } else { #create old record object
166   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
167   $error ||= "Old record not found!" unless $old;
168   $error ||= $new->replace($old);
169 }
170
171 if ( $error ) {
172   $cgi->param('error', $error);
173   print $cgi->redirect(popurl(2). "cust_main.cgi?". $cgi->query_string );
174 } else { 
175   $new->invoicing_list( \@invoicing_list );
176   $custnum = $new->custnum;
177   print $cgi->redirect(popurl(3). "view/cust_main.cgi?$custnum#cust_main");
178