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