statedefault & referraldefault config files
[freeside.git] / htdocs / edit / cust_main.cgi
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: cust_main.cgi,v 1.28 2000-12-26 23:51:40 ivan Exp $
4 #
5 # Usage: cust_main.cgi custnum
6 #        http://server.name/path/cust_main.cgi?custnum
7 #
8 # ivan@voicenet.com 96-nov-29 -> 96-dec-04
9 #
10 # Blank custnum for new customer.
11 # ivan@voicenet.com 96-dec-16
12 #
13 # referral defaults to blank, to force people to pick something
14 # ivan@voicenet.com 97-jun-4
15 #
16 # rewrote for new API
17 # ivan@voicenet.com 97-jul-28
18 #
19 # new customer is null, not '#'
20 # otaker gotten from &getotaker instead of $ENV{REMOTE_USER}
21 # ivan@sisd.com 97-nov-12
22 #
23 # cgisuidsetup($cgi);
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
28 #
29 # fixed quoting problems ivan@sisd.com 98-feb-23
30 #
31 # paydate sql update ivan@sisd.com 98-mar-5
32 #
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
37 #
38 # fixed one missed day->daytime ivan@sisd.com 98-jul-13
39 #
40 # $Log: cust_main.cgi,v $
41 # Revision 1.28  2000-12-26 23:51:40  ivan
42 # statedefault & referraldefault config files
43 #
44 # Revision 1.27  2000/12/03 13:45:15  ivan
45 # patch from Jason Spence <thalakan@frys.com>: admin.html doc, autocapgen
46 #
47 # Revision 1.26  2000/06/27 12:15:50  ivan
48 # i18n
49 #
50 # Revision 1.25  2000/03/02 08:09:38  ivan
51 # still need to allow blank expiration dates
52 #
53 # Revision 1.24  2000/01/30 06:54:50  ivan
54 # credit card expiration dates not sticky bug fixed?
55 #
56 # Revision 1.23  2000/01/27 00:53:14  ivan
57 # 5.004_04 workaround
58 #
59 # Revision 1.22  1999/12/17 02:33:23  ivan
60 # argh
61 #
62 # Revision 1.21  1999/08/23 07:40:38  ivan
63 # missing </TD> flag
64 #
65 # Revision 1.20  1999/08/23 07:08:11  ivan
66 # no CGI::Switch for now
67 #
68 # Revision 1.19  1999/08/21 02:14:25  ivan
69 # better error message for no agents
70 #
71 # Revision 1.18  1999/08/11 15:38:33  ivan
72 # fix for perl 5.004_04
73 #
74 # Revision 1.17  1999/08/10 11:15:45  ivan
75 # corrected a misleading comment
76 #
77 # Revision 1.15  1999/04/14 13:14:54  ivan
78 # configuration option to edit referrals of existing customers
79 #
80 # Revision 1.14  1999/04/14 07:47:53  ivan
81 # i18n fixes
82 #
83 # Revision 1.13  1999/04/09 03:52:55  ivan
84 # explicit & for table/itable/ntable
85 #
86 # Revision 1.12  1999/04/06 11:16:16  ivan
87 # give a meaningful error message if you try to create a customer before you've
88 # created an agent
89 #
90 # Revision 1.11  1999/03/25 13:55:10  ivan
91 # one-screen new customer entry (including package and service) for simple
92 # packages with one svc_acct service
93 #
94 # Revision 1.10  1999/02/28 00:03:34  ivan
95 # removed misleading comments
96 #
97 # Revision 1.9  1999/02/23 08:09:20  ivan
98 # beginnings of one-screen new customer entry and some other miscellania
99 #
100 # Revision 1.8  1999/01/25 12:09:53  ivan
101 # yet more mod_perl stuff
102 #
103 # Revision 1.7  1999/01/19 05:13:34  ivan
104 # for mod_perl: no more top-level my() variables; use vars instead
105 # also the last s/create/new/;
106 #
107 # Revision 1.6  1999/01/18 09:41:24  ivan
108 # all $cgi->header calls now include ( '-expires' => 'now' ) for mod_perl
109 # (good idea anyway)
110 #
111 # Revision 1.5  1999/01/18 09:22:30  ivan
112 # changes to track email addresses for email invoicing
113 #
114 # Revision 1.4  1998/12/23 08:08:15  ivan
115 # fix typo
116 #
117 # Revision 1.3  1998/12/17 06:17:00  ivan
118 # fix double // in relative URLs, s/CGI::Base/CGI/;
119 #
120
121 use strict;
122 use vars qw( $cgi $custnum $action $cust_main $p1 @agents $agentnum 
123              $last $first $ss $company $address1 $address2 $city $zip 
124              $daytime $night $fax @invoicing_list $invoicing_list $payinfo
125              $payname %payby %paybychecked $refnum $otaker $r );
126 use vars qw ( $conf $pkgpart $username $password $popnum $ulen $ulen2 );
127 #use CGI::Switch;
128 use CGI;
129 use CGI::Carp qw(fatalsToBrowser);
130 use FS::UID qw(cgisuidsetup getotaker);
131 #use FS::Record qw(qsearch qsearchs fields);
132 use FS::Record qw(qsearch qsearchs fields dbdef);
133 use FS::CGI qw(header popurl itable table);
134 use FS::cust_main;
135 use FS::agent;
136 use FS::part_referral;
137 use FS::cust_main_county;
138
139   #for misplaced logic below
140   use FS::part_pkg;
141
142   #for false laziness below
143   use FS::svc_acct_pop;
144
145   #for (other) false laziness below
146   use FS::agent;
147   use FS::type_pkgs;
148
149 $cgi = new CGI;
150 cgisuidsetup($cgi);
151
152 $conf = new FS::Conf;
153
154 #get record
155
156 if ( $cgi->param('error') ) {
157   $cust_main = new FS::cust_main ( {
158     map { $_, scalar($cgi->param($_)) } fields('cust_main')
159   } );
160   $custnum = $cust_main->custnum;
161   $pkgpart = $cgi->param('pkgpart_svcpart') || '';
162   if ( $pkgpart =~ /^(\d+)_/ ) {
163     $pkgpart = $1;
164   } else {
165     $pkgpart = '';
166   }
167   $username = $cgi->param('username');
168   $password = $cgi->param('_password');
169   $popnum = $cgi->param('popnum');
170 } elsif ( $cgi->keywords ) { #editing
171   my( $query ) = $cgi->keywords;
172   $query =~ /^(\d+)$/;
173   $custnum=$1;
174   $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
175   $pkgpart = 0;
176   $username = '';
177   $password = '';
178   $popnum = 0;
179 } else {
180   $custnum='';
181   $cust_main = new FS::cust_main ( {} );
182   $cust_main->setfield('otaker',&getotaker);
183   $pkgpart = 0;
184   $username = '';
185   $password = '';
186   $popnum = 0;
187 }
188 $action = $custnum ? 'Edit' : 'Add';
189
190 # top
191
192 $p1 = popurl(1);
193 print $cgi->header( '-expires' => 'now' ), header("Customer $action", '');
194 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
195       "</FONT>"
196   if $cgi->param('error');
197
198 # JRS: Javascript to set up the form for us
199     if ( $conf->exists('autocapnames') ) {
200       print <<END;
201 <SCRIPT language="Javascript"><!--
202     
203 function capName(name) {
204     var temp = new String();
205     var n = name.toString();
206     
207 // Handle "Mc", "Mac", "Von", "Van", etc...
208  
209     if(n.substr(0,2).toLowerCase() == "mc") {
210      temp += "Mc";
211      temp += n.charAt(2).toUpperCase();
212      temp += n.substr(3).toLowerCase();
213      return temp;
214     }
215      
216     if(n.substr(0,3).toLowerCase() == "mac") {
217      temp += "Mac";
218      temp += n.charAt(3).toUpperCase();
219      temp += n.substr(4).toLowerCase();
220      return temp;
221     }
222     if(n.substr(0,3).toLowerCase() == "von") {
223      temp += "Von";
224      temp += n.charAt(3).toUpperCase();
225      temp += n.substr(4).toLowerCase();
226      return temp;
227     }
228     if(n.substr(0,3).toLowerCase() == "van") {
229      temp += "Van";
230      temp += n.charAt(3).toUpperCase();
231      temp += n.substr(4).toLowerCase();
232      return temp;
233     }
234     temp += n.charAt(0).toUpperCase();
235     temp += n.substr(1).toLowerCase();
236     return temp;
237 }
238    
239 //-->
240 </SCRIPT>
241 END
242 }
243
244 print qq!<FORM ACTION="${p1}process/cust_main.cgi" METHOD=POST NAME="form1">!,
245       qq!<INPUT TYPE="hidden" NAME="custnum" VALUE="$custnum">!,
246       qq!Customer # !, ( $custnum ? $custnum : " (NEW)" ),
247       
248 ;
249
250 # agent
251
252 $r = qq!<font color="#ff0000">*</font>!;
253
254 @agents = qsearch( 'agent', {} );
255 #die "No agents created!" unless @agents;
256 die "You have not created any agents.  You must create at least one agent before adding a customer.  Go to ". popurl(2). "browse/agent.cgi and create one or more agents." unless @agents;
257 $agentnum = $cust_main->agentnum || $agents[0]->agentnum; #default to first
258 if ( scalar(@agents) == 1 ) {
259   print qq!<INPUT TYPE="hidden" NAME="agentnum" VALUE="$agentnum">!;
260 } else {
261   print qq!<BR><BR>${r}Agent <SELECT NAME="agentnum" SIZE="1">!;
262   my $agent;
263   foreach $agent (sort {
264     $a->agent cmp $b->agent;
265   } @agents) {
266       print '<OPTION VALUE="', $agent->agentnum, '"',
267       " SELECTED"x($agent->agentnum==$agentnum),
268       ">", $agent->agentnum,": ", $agent->agent;
269   }
270   print "</SELECT>";
271 }
272
273 #referral
274
275 $refnum = $cust_main->refnum || $conf->config('referraldefault') || 0;
276 if ( $custnum && ! $conf->exists('editreferrals') ) {
277   print qq!<INPUT TYPE="hidden" NAME="refnum" VALUE="$refnum">!;
278 } else {
279   my(@referrals) = qsearch('part_referral',{});
280   if ( scalar(@referrals) == 1 ) {
281     $refnum ||= $referrals[0]->refnum;
282     print qq!<INPUT TYPE="hidden" NAME="refnum" VALUE="$refnum">!;
283   } else {
284     print qq!<BR><BR>${r}Referral <SELECT NAME="refnum" SIZE="1">!;
285     print "<OPTION> " unless $refnum;
286     my($referral);
287     foreach $referral (sort {
288       $a->refnum <=> $b->refnum;
289     } @referrals) {
290       print "<OPTION" . " SELECTED"x($referral->refnum==$refnum),
291       ">", $referral->refnum, ": ", $referral->referral;
292     }
293     print "</SELECT>";
294   }
295 }
296
297
298 # contact info
299
300 ($last,$first,$ss,$company,$address1,$address2,$city,$zip)=(
301   $cust_main->last,
302   $cust_main->first,
303   $cust_main->ss,
304   $cust_main->company,
305   $cust_main->address1,
306   $cust_main->address2,
307   $cust_main->city,
308   $cust_main->zip,
309 );
310
311 print "<BR><BR>Contact information", &itable("#c0c0c0"), <<END;
312 <TR><TH ALIGN="right">${r}Contact name<BR>(last, first)</TH><TD COLSPAN=3>
313 END
314
315 if ( $conf->exists('autocapnames') ) {
316   print <<END;
317 <INPUT TYPE="text" NAME="last" VALUE="$last" onChange="updateUsername();">, 
318 <INPUT TYPE="text" NAME="first" VALUE="$first" onChange="updateUsername();">
319 END
320 } else {
321   print <<END;
322 <INPUT TYPE="text" NAME="last" VALUE="$last">, 
323 <INPUT TYPE="text" NAME="first" VALUE="$first">
324 END
325 }
326
327 print <<END;
328 </TD><TD ALIGN="right">SS#</TD><TD><INPUT TYPE="text" NAME="ss" VALUE="$ss" SIZE=11></TD></TR>
329 <TR><TD ALIGN="right">Company</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="company" VALUE="$company" SIZE=70></TD></TR>
330 <TR><TH ALIGN="right">${r}Address</TH><TD COLSPAN=5><INPUT TYPE="text" NAME="address1" VALUE="$address1" SIZE=70></TD></TR>
331 <TR><TD ALIGN="right">&nbsp;</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="address2" VALUE="$address2" SIZE=70></TD></TR>
332 <TR><TH ALIGN="right">${r}City</TH><TD><INPUT TYPE="text" NAME="city" VALUE="$city"></TD><TH ALIGN="right">${r}State/Country</TH><TD><SELECT NAME="state" SIZE="1">
333 END
334
335 $cust_main->country( $conf->config('countrydefault') || 'US' )
336   unless $cust_main->country;
337 $cust_main->state( $conf->config('statedefault') || 'CA' )
338   unless $cust_main->state;
339 foreach ( qsearch('cust_main_county',{}) ) {
340   print "<OPTION";
341   print " SELECTED" if ( $cust_main->state eq $_->state
342                          && $cust_main->county eq $_->county 
343                          && $cust_main->country eq $_->country
344                        );
345   print ">",$_->state;
346   print " (",$_->county,")" if $_->county;
347   print " / ", $_->country;
348 }
349 print qq!</SELECT></TD><TH>${r}Zip</TH><TD><INPUT TYPE="text" NAME="zip" VALUE="$zip" SIZE=10></TD></TR>!;
350
351 ($daytime,$night,$fax)=(
352   $cust_main->daytime,
353   $cust_main->night,
354   $cust_main->fax,
355 );
356
357 print <<END;
358 <TR><TD ALIGN="right">Day Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="daytime" VALUE="$daytime" SIZE=18></TD></TR>
359 <TR><TD ALIGN="right">Night Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="night" VALUE="$night" SIZE=18></TD></TR>
360 <TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="fax" VALUE="$fax" SIZE=12></TD></TR>
361 END
362
363 print "</TABLE>$r required fields<BR>";
364
365 # billing info
366
367 sub expselect {
368   my $prefix = shift;
369   my( $m, $y ) = (0, 0);
370   if ( scalar(@_) ) {
371     my $date = shift || '01-2000';
372     if ( $date  =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #PostgreSQL date format
373       ( $m, $y ) = ( $2, $1 );
374     } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
375       ( $m, $y ) = ( $1, $3 );
376     } else {
377       die "unrecognized expiration date format: $date";
378     }
379   }
380
381   my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
382   for ( 1 .. 12 ) {
383     $return .= "<OPTION";
384     $return .= " SELECTED" if $_ == $m;
385     $return .= ">$_";
386   }
387   $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
388   for ( 2001 .. 2037 ) {
389     $return .= "<OPTION";
390     $return .= " SELECTED" if $_ == $y;
391     $return .= ">$_";
392   }
393   $return .= "</SELECT>";
394
395   $return;
396 }
397
398 print "<BR>Billing information", &itable("#c0c0c0"),
399       qq!<TR><TD><INPUT TYPE="checkbox" NAME="tax" VALUE="Y"!;
400 print qq! CHECKED! if $cust_main->tax eq "Y";
401 print qq!>Tax Exempt</TD></TR>!;
402 print qq!<TR><TD><INPUT TYPE="checkbox" NAME="invoicing_list_POST" VALUE="POST"!;
403 @invoicing_list = $cust_main->invoicing_list;
404 print qq! CHECKED!
405   if ! @invoicing_list || grep { $_ eq 'POST' } @invoicing_list;
406 print qq!>Postal mail invoice</TD></TR>!;
407 $invoicing_list = join(', ', grep { $_ ne 'POST' } @invoicing_list );
408 print qq!<TR><TD>Email invoice <INPUT TYPE="text" NAME="invoicing_list" VALUE="$invoicing_list"></TD></TR>!;
409
410 print "<TR><TD>Billing type</TD></TR>",
411       "</TABLE>",
412       &table("#c0c0c0"), "<TR>";
413
414 ($payinfo, $payname)=(
415   $cust_main->payinfo,
416   $cust_main->payname,
417 );
418
419 %payby = (
420   '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="">!,
421   '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">!,
422   'COMP' => qq!Complimentary<BR>${r}Approved by<INPUT TYPE="text" NAME="COMP_payinfo" VALUE=""><BR>${r}Exp !. expselect("COMP"),
423 );
424 %paybychecked = (
425   '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">!,
426   '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">!,
427   'COMP' => qq!Complimentary<BR>${r}Approved by<INPUT TYPE="text" NAME="COMP_payinfo" VALUE="$payinfo"><BR>${r}Exp !. expselect("COMP", $cust_main->paydate),
428 );
429 for (qw(CARD BILL COMP)) {
430   print qq!<TD VALIGN=TOP><INPUT TYPE="radio" NAME="payby" VALUE="$_"!;
431   if ($cust_main->payby eq "$_") {
432     print qq! CHECKED> $paybychecked{$_}</TD>!;
433   } else {
434     print qq!> $payby{$_}</TD>!;
435   }
436 }
437
438 print "</TR></TABLE>$r required fields for each billing type";
439
440 unless ( $custnum ) {
441   # pry the wrong place for this logic.  also pretty expensive
442   #use FS::part_pkg;
443
444   #false laziness, copied from FS::cust_pkg::order
445   my $pkgpart;
446   if ( scalar(@agents) == 1 ) {
447     # $pkgpart->{PKGPART} is true iff $custnum may purchase $pkgpart
448     my($agent)=qsearchs('agent',{'agentnum'=> $agentnum });
449     $pkgpart = $agent->pkgpart_hashref;
450   } else {
451     #can't know (agent not chosen), so, allow all
452     my %typenum;
453     foreach my $agent ( @agents ) {
454       next if $typenum{$agent->typenum}++;
455       #fixed in 5.004_05 #$pkgpart->{$_}++ foreach keys %{ $agent->pkgpart_hashref }
456       foreach ( keys %{ $agent->pkgpart_hashref } ) { $pkgpart->{$_}++; } #5.004_04 workaround
457     }
458   }
459   #eslaf
460
461   my @part_pkg = grep { $_->svcpart('svc_acct') && $pkgpart->{ $_->pkgpart } }
462     qsearch( 'part_pkg', {} );
463
464   if ( @part_pkg ) {
465
466     print "<BR><BR>First package", &itable("#c0c0c0"),
467           qq!<TR><TD COLSPAN=2><SELECT NAME="pkgpart_svcpart">!;
468
469     print qq!<OPTION VALUE="">(none)!;
470
471     foreach my $part_pkg ( @part_pkg ) {
472       print qq!<OPTION VALUE="!,
473 #              $part_pkg->pkgpart. "_". $pkgpart{ $part_pkg->pkgpart }, '"';
474               $part_pkg->pkgpart. "_". $part_pkg->svcpart, '"';
475       print " SELECTED" if $pkgpart && ( $part_pkg->pkgpart == $pkgpart );
476       print ">", $part_pkg->pkg, " - ", $part_pkg->comment;
477     }
478     print "</SELECT></TD></TR>";
479
480     #false laziness: (mostly) copied from edit/svc_acct.cgi
481     #$ulen = $svc_acct->dbdef_table->column('username')->length;
482     $ulen = dbdef->table('svc_acct')->column('username')->length;
483     $ulen2 = $ulen+2;
484     print <<END;
485 <TR><TD ALIGN="right">Username</TD>
486 <TD><INPUT TYPE="text" NAME="username" VALUE="$username" SIZE=$ulen2 MAXLENGTH=$ulen></TD></TR>
487 <TR><TD ALIGN="right">Password</TD>
488 <TD><INPUT TYPE="text" NAME="_password" VALUE="$password" SIZE=10 MAXLENGTH=8>
489 (blank to generate)</TD></TR>
490 END
491     print qq!<TR><TD ALIGN="right">POP</TD><TD><SELECT NAME="popnum" SIZE=1><OPTION> !;
492     my($svc_acct_pop);
493     foreach $svc_acct_pop ( qsearch ('svc_acct_pop',{} ) ) {
494     print qq!<OPTION VALUE="!, $svc_acct_pop->popnum, '"',
495           ( $popnum && $svc_acct_pop->popnum == $popnum ) ? ' SELECTED' : '', ">", 
496           $svc_acct_pop->popnum, ": ", 
497           $svc_acct_pop->city, ", ",
498           $svc_acct_pop->state,
499           " (", $svc_acct_pop->ac, ")/",
500           $svc_acct_pop->exch, "\n"
501         ;
502     }
503     print "</SELECT></TD></TR></TABLE>";
504   }
505 }
506
507 $otaker = $cust_main->otaker;
508 print qq!<INPUT TYPE="hidden" NAME="otaker" VALUE="$otaker">!,
509       qq!<BR><BR><INPUT TYPE="submit" VALUE="!,
510       $custnum ?  "Apply Changes" : "Add Customer", qq!">!,
511       "</FORM></BODY></HTML>",
512 ;
513