3 # $Id: cust_main.cgi,v 1.16 1999-07-20 10:37:05 ivan Exp $
5 # Usage: cust_main.cgi custnum
6 # http://server.name/path/cust_main.cgi?custnum
8 # ivan@voicenet.com 96-nov-29 -> 96-dec-04
10 # Blank custnum for new customer.
11 # ivan@voicenet.com 96-dec-16
13 # referral defaults to blank, to force people to pick something
14 # ivan@voicenet.com 97-jun-4
17 # ivan@voicenet.com 97-jul-28
19 # new customer is null, not '#'
20 # otaker gotten from &getotaker instead of $ENV{REMOTE_USER}
21 # ivan@sisd.com 97-nov-12
24 # no need for old_ fields.
25 # now state+county is a select field (took out PA hack)
26 # used autoloaded $cust_main->field methods
27 # ivan@sisd.com 97-dec-17
29 # fixed quoting problems ivan@sisd.com 98-feb-23
31 # paydate sql update ivan@sisd.com 98-mar-5
33 # Changes to allow page to work at a relative position in server
34 # Changed 'day' to 'daytime' because Pg6.3 reserves the day word
35 # Added test for paydate in mm-dd-yyyy format for Pg6.3 default format
36 # bmccane@maxbaud.net 98-apr-3
38 # fixed one missed day->daytime ivan@sisd.com 98-jul-13
40 # $Log: cust_main.cgi,v $
41 # Revision 1.16 1999-07-20 10:37:05 ivan
42 # cleaned up the new one-screen signup bits in htdocs/edit/cust_main.cgi to
43 # prepare for a signup server
45 # Revision 1.15 1999/04/14 13:14:54 ivan
46 # configuration option to edit referrals of existing customers
48 # Revision 1.14 1999/04/14 07:47:53 ivan
51 # Revision 1.13 1999/04/09 03:52:55 ivan
52 # explicit & for table/itable/ntable
54 # Revision 1.12 1999/04/06 11:16:16 ivan
55 # give a meaningful error message if you try to create a customer before you've
58 # Revision 1.11 1999/03/25 13:55:10 ivan
59 # one-screen new customer entry (including package and service) for simple
60 # packages with one svc_acct service
62 # Revision 1.10 1999/02/28 00:03:34 ivan
63 # removed misleading comments
65 # Revision 1.9 1999/02/23 08:09:20 ivan
66 # beginnings of one-screen new customer entry and some other miscellania
68 # Revision 1.8 1999/01/25 12:09:53 ivan
69 # yet more mod_perl stuff
71 # Revision 1.7 1999/01/19 05:13:34 ivan
72 # for mod_perl: no more top-level my() variables; use vars instead
73 # also the last s/create/new/;
75 # Revision 1.6 1999/01/18 09:41:24 ivan
76 # all $cgi->header calls now include ( '-expires' => 'now' ) for mod_perl
79 # Revision 1.5 1999/01/18 09:22:30 ivan
80 # changes to track email addresses for email invoicing
82 # Revision 1.4 1998/12/23 08:08:15 ivan
85 # Revision 1.3 1998/12/17 06:17:00 ivan
86 # fix double // in relative URLs, s/CGI::Base/CGI/;
90 use vars qw( $cgi $custnum $action $cust_main $p1 @agents $agentnum
91 $last $first $ss $company $address1 $address2 $city $zip
92 $daytime $night $fax @invoicing_list $invoicing_list $payinfo
93 $payname %payby %paybychecked $refnum $otaker $r );
94 use vars qw ( $conf $pkgpart $username $password $popnum $ulen $ulen2 );
96 use CGI::Carp qw(fatalsToBrowser);
97 use FS::UID qw(cgisuidsetup getotaker);
98 #use FS::Record qw(qsearch qsearchs fields);
99 use FS::Record qw(qsearch qsearchs fields dbdef);
100 use FS::CGI qw(header popurl itable table);
103 use FS::part_referral;
104 use FS::cust_main_county;
106 #for misplaced logic below
109 #for false laziness below
110 use FS::svc_acct_pop;
112 #for (other) false laziness below
119 $conf = new FS::Conf;
123 if ( $cgi->param('error') ) {
124 $cust_main = new FS::cust_main ( {
125 map { $_, scalar($cgi->param($_)) } fields('cust_main')
127 $custnum = $cust_main->custnum;
128 $pkgpart = $cgi->param('pkgpart_svcpart') || '';
129 if ( $pkgpart =~ /^(\d+)_/ ) {
134 $username = $cgi->param('username');
135 $password = $cgi->param('_password');
136 $popnum = $cgi->param('popnum');
137 } elsif ( $cgi->keywords ) { #editing
138 my( $query ) = $cgi->keywords;
141 $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
148 $cust_main = new FS::cust_main ( {} );
149 $cust_main->setfield('otaker',&getotaker);
155 $action = $custnum ? 'Edit' : 'Add';
160 print $cgi->header( '-expires' => 'now' ), header("Customer $action", '');
161 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
163 if $cgi->param('error');
164 print qq!<FORM ACTION="${p1}process/cust_main.cgi" METHOD=POST>!,
165 qq!<INPUT TYPE="hidden" NAME="custnum" VALUE="$custnum">!,
166 qq!Customer # !, ( $custnum ? $custnum : " (NEW)" ),
172 $r = qq!<font color="#ff0000">*</font>!;
174 @agents = qsearch( 'agent', {} );
175 die "No agents created!" unless @agents;
176 $agentnum = $cust_main->agentnum || $agents[0]->agentnum; #default to first
177 if ( scalar(@agents) == 1 ) {
178 print qq!<INPUT TYPE="hidden" NAME="agentnum" VALUE="$agentnum">!;
180 print qq!<BR><BR>${r}Agent <SELECT NAME="agentnum" SIZE="1">!;
182 foreach $agent (sort {
183 $a->agent cmp $b->agent;
185 print '<OPTION VALUE="', $agent->agentnum, '"',
186 " SELECTED"x($agent->agentnum==$agentnum),
187 ">", $agent->agentnum,": ", $agent->agent;
194 $refnum = $cust_main->refnum || 0;
195 if ( $custnum && ! $conf->exists('editreferrals') ) {
196 print qq!<INPUT TYPE="hidden" NAME="refnum" VALUE="$refnum">!;
198 my(@referrals) = qsearch('part_referral',{});
199 if ( scalar(@referrals) == 1 ) {
200 $refnum ||= $referrals[0]->refnum;
201 print qq!<INPUT TYPE="hidden" NAME="refnum" VALUE="$refnum">!;
203 print qq!<BR><BR>${r}Referral <SELECT NAME="refnum" SIZE="1">!;
204 print "<OPTION> " unless $refnum;
206 foreach $referral (sort {
207 $a->refnum <=> $b->refnum;
209 print "<OPTION" . " SELECTED"x($referral->refnum==$refnum),
210 ">", $referral->refnum, ": ", $referral->referral;
219 ($last,$first,$ss,$company,$address1,$address2,$city,$zip)=(
224 $cust_main->address1,
225 $cust_main->address2,
230 print "<BR><BR>Contact information", &itable("#c0c0c0"), <<END;
231 <TR><TH ALIGN="right">${r}Contact name<BR>(last, first)</TH><TD COLSPAN=3><INPUT TYPE="text" NAME="last" VALUE="$last">, <INPUT TYPE="text" NAME="first" VALUE="$first"></TD><TD ALIGN="right">SS#</TD><TD><INPUT TYPE="text" NAME="ss" VALUE="$ss" SIZE=11></TD></TR>
232 <TR><TD ALIGN="right">Company</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="company" VALUE="$company" SIZE=70></TD></TR>
233 <TR><TH ALIGN="right">${r}Address</TH><TD COLSPAN=5><INPUT TYPE="text" NAME="address1" VALUE="$address1" SIZE=70></TD></TR>
234 <TR><TD ALIGN="right"> </TD><TD COLSPAN=5><INPUT TYPE="text" NAME="address2" VALUE="$address2" SIZE=70></TD></TR>
235 <TR><TH ALIGN="right">${r}City</TH><TD><INPUT TYPE="text" NAME="city" VALUE="$city"><TH ALIGN="right">${r}State/Country</TH><TD><SELECT NAME="state" SIZE="1">
238 $cust_main->country('US') unless $cust_main->country; #eww
239 foreach ( qsearch('cust_main_county',{}) ) {
241 print " SELECTED" if ( $cust_main->state eq $_->state
242 && $cust_main->county eq $_->county
243 && $cust_main->country eq $_->country
246 print " (",$_->county,")" if $_->county;
247 print " / ", $_->country;
249 print qq!</SELECT></TD><TH>${r}Zip</TH><TD><INPUT TYPE="text" NAME="zip" VALUE="$zip" SIZE=10></TD></TR>!;
251 ($daytime,$night,$fax)=(
258 <TR><TD ALIGN="right">Day Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="daytime" VALUE="$daytime" SIZE=18></TD></TR>
259 <TR><TD ALIGN="right">Night Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="night" VALUE="$night" SIZE=18></TD></TR>
260 <TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="fax" VALUE="$fax" SIZE=12></TD></TR>
263 print "</TABLE>$r required fields<BR>";
269 my $date = shift || '';
270 my( $m, $y ) = ( 0, 0 );
271 if ( $date =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #PostgreSQL date format
272 ( $m, $y ) = ( $2, $1 );
273 } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
274 ( $m, $y ) = ( $1, $3 );
276 my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
278 $return .= "<OPTION";
279 $return .= " SELECTED" if $_ == $m;
282 $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
283 for ( 1999 .. 2037 ) {
284 $return .= "<OPTION";
285 $return .= " SELECTED" if $_ == $y;
288 $return .= "</SELECT>";
293 print "<BR>Billing information", &itable("#c0c0c0"),
294 qq!<TR><TD><INPUT TYPE="checkbox" NAME="tax" VALUE="Y"!;
295 print qq! CHECKED! if $cust_main->tax eq "Y";
296 print qq!>Tax Exempt</TD></TR>!;
297 print qq!<TR><TD><INPUT TYPE="checkbox" NAME="invoicing_list_POST" VALUE="POST"!;
298 @invoicing_list = $cust_main->invoicing_list;
300 if ! @invoicing_list || grep { $_ eq 'POST' } @invoicing_list;
301 print qq!>Postal mail invoice</TD></TR>!;
302 $invoicing_list = join(', ', grep { $_ ne 'POST' } @invoicing_list );
303 print qq!<TR><TD>Email invoice <INPUT TYPE="text" NAME="invoicing_list" VALUE="$invoicing_list"></TD></TR>!;
305 print "<TR><TD>Billing type</TD></TR>",
307 &table("#c0c0c0"), "<TR>";
309 ($payinfo, $payname)=(
315 'CARD' => qq!Credit card<BR>${r}<INPUT TYPE="text" NAME="CARD_payinfo" VALUE="" MAXLENGTH=19><BR>${r}Exp !. expselect("CARD"). qq!<BR>${r}Name on card<BR><INPUT TYPE="text" NAME="CARD_payname" VALUE="">!,
316 'BILL' => qq!Billing<BR>P.O. <INPUT TYPE="text" NAME="BILL_payinfo" VALUE=""><BR>${r}Exp !. expselect("BILL", "12-2037"). qq!<BR>${r}Attention<BR><INPUT TYPE="text" NAME="BILL_payname" VALUE="Accounts Payable">!,
317 'COMP' => qq!Complimentary<BR>${r}Approved by<INPUT TYPE="text" NAME="COMP_payinfo" VALUE=""><BR>${r}Exp !. expselect("COMP"),
320 'CARD' => qq!Credit card<BR>${r}<INPUT TYPE="text" NAME="CARD_payinfo" VALUE="$payinfo" MAXLENGTH=19><BR>${r}Exp !. expselect("CARD", $cust_main->paydate). qq!<BR>${r}Name on card<BR><INPUT TYPE="text" NAME="CARD_payname" VALUE="$payname">!,
321 'BILL' => qq!Billing<BR>P.O. <INPUT TYPE="text" NAME="BILL_payinfo" VALUE="$payinfo"><BR>${r}Exp !. expselect("BILL", $cust_main->paydate). qq!<BR>${r}Attention<BR><INPUT TYPE="text" NAME="BILL_payname" VALUE="$payname">!,
322 'COMP' => qq!Complimentary<BR>${r}Approved by<INPUT TYPE="text" NAME="COMP_payinfo" VALUE="$payinfo"><BR>${r}Exp !. expselect("COMP", $cust_main->paydate),
324 for (qw(CARD BILL COMP)) {
325 print qq!<TD VALIGN=TOP><INPUT TYPE="radio" NAME="payby" VALUE="$_"!;
326 if ($cust_main->payby eq "$_") {
327 print qq! CHECKED> $paybychecked{$_}</TD>!;
329 print qq!> $payby{$_}</TD>!;
333 print "</TR></TABLE>$r required fields for each billing type";
335 unless ( $custnum ) {
336 # pry the wrong place for this logic. also pretty expensive
339 #false laziness, copied from FS::cust_pkg::order
341 if ( scalar(@agents) == 1 ) {
342 # $pkgpart->{$pkgpart} is true iff $custnum may purchase $pkgpart
343 my($agent)=qsearchs('agent',{'agentnum'=> $agentnum });
344 $pkgpart = $agent->pkgpart_hashref;
346 #can't know (agent not chosen), so, allow all
348 foreach my $agent ( @agents ) {
349 next if $typenum{$agent->typenum}++;
350 $pkgpart->{$_}++ foreach keys %{ $agent->pkgpart_hashref }
355 my @part_pkg = grep { $_->svcpart('svc_acct') && $pkgpart->{ $_->pkgpart } }
356 qsearch( 'part_pkg', {} );
360 print "<BR><BR>First package", &itable("#c0c0c0"),
361 qq!<TR><TD COLSPAN=2><SELECT NAME="pkgpart_svcpart">!;
363 print qq!<OPTION VALUE="">(none)!;
365 foreach my $part_pkg ( @part_pkg ) {
366 print qq!<OPTION VALUE="!,
367 # $part_pkg->pkgpart. "_". $pkgpart{ $part_pkg->pkgpart }, '"';
368 $part_pkg->pkgpart. "_". $part_pkg->svcpart, '"';
369 print " SELECTED" if $pkgpart && ( $part_pkg->pkgpart == $pkgpart );
370 print ">", $part_pkg->pkg, " - ", $part_pkg->comment;
372 print "</SELECT></TD></TR>";
374 #false laziness: (mostly) copied from edit/svc_acct.cgi
375 #$ulen = $svc_acct->dbdef_table->column('username')->length;
376 $ulen = dbdef->table('svc_acct')->column('username')->length;
379 <TR><TD ALIGN="right">Username</TD>
380 <TD><INPUT TYPE="text" NAME="username" VALUE="$username" SIZE=$ulen2 MAXLENGTH=$ulen></TD></TR>
381 <TR><TD ALIGN="right">Password</TD>
382 <TD><INPUT TYPE="text" NAME="_password" VALUE="$password" SIZE=10 MAXLENGTH=8>
383 (blank to generate)</TD></TR>
385 print qq!<TR><TD ALIGN="right">POP</TD><TD><SELECT NAME="popnum" SIZE=1><OPTION> !;
387 foreach $svc_acct_pop ( qsearch ('svc_acct_pop',{} ) ) {
388 print qq!<OPTION VALUE="!, $svc_acct_pop->popnum, '"',
389 ( $popnum && $svc_acct_pop->popnum == $popnum ) ? ' SELECTED' : '', ">",
390 $svc_acct_pop->popnum, ": ",
391 $svc_acct_pop->city, ", ",
392 $svc_acct_pop->state,
393 " (", $svc_acct_pop->ac, ")/",
394 $svc_acct_pop->exch, "\n"
397 print "</SELECT></TD></TR></TABLE>";
401 $otaker = $cust_main->otaker;
402 print qq!<INPUT TYPE="hidden" NAME="otaker" VALUE="$otaker">!,
403 qq!<BR><BR><INPUT TYPE="submit" VALUE="!,
404 $custnum ? "Apply Changes" : "Add Customer", qq!">!,
405 "</FORM></BODY></HTML>",