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