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