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