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