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