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