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