a more reasonalbe name (!)
[freeside.git] / httemplate / edit / cust_main.cgi
1 <%
2 #<!-- $Id: cust_main.cgi,v 1.14 2001-11-21 03:42:13 ivan Exp $ -->
3
4 use vars qw( $cgi $custnum $action $cust_main $p1 @agents $agentnum 
5              $last $first $ss $company $address1 $address2 $city $zip 
6              $daytime $night $fax @invoicing_list $invoicing_list $payinfo
7              $payname %payby %paybychecked $refnum $otaker $r );
8 use vars qw ( $conf $saved_pkgpart $username $password $popnum $ulen $ulen2 );
9 use vars qw ( $error );
10 #use CGI::Switch;
11 use CGI;
12 use CGI::Carp qw(fatalsToBrowser);
13 use FS::UID qw(cgisuidsetup getotaker);
14 #use FS::Record qw(qsearch qsearchs fields);
15 use FS::Record qw(qsearch qsearchs fields dbdef);
16 use FS::CGI qw(header popurl itable table);
17 use FS::cust_main;
18 use FS::agent;
19 use FS::part_referral;
20 use FS::cust_main_county;
21
22   #for misplaced logic below
23   use FS::part_pkg;
24
25   #for false laziness below (now more properly lazy)
26   use FS::svc_acct_pop;
27
28   #for (other) false laziness below
29   use FS::agent;
30   use FS::type_pkgs;
31
32 $conf = new FS::Conf;
33
34 #get record
35
36 $error = '';
37 if ( $cgi->param('error') ) {
38   $error = $cgi->param('error');
39   $cust_main = new FS::cust_main ( {
40     map { $_, scalar($cgi->param($_)) } fields('cust_main')
41   } );
42   $custnum = $cust_main->custnum;
43   $saved_pkgpart = $cgi->param('pkgpart_svcpart') || '';
44   if ( $saved_pkgpart =~ /^(\d+)_/ ) {
45     $saved_pkgpart = $1;
46   } else {
47     $saved_pkgpart = '';
48   }
49   $username = $cgi->param('username');
50   $password = $cgi->param('_password');
51   $popnum = $cgi->param('popnum');
52 } elsif ( $cgi->keywords ) { #editing
53   my( $query ) = $cgi->keywords;
54   $query =~ /^(\d+)$/;
55   $custnum=$1;
56   $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
57   $saved_pkgpart = 0;
58   $username = '';
59   $password = '';
60   $popnum = 0;
61 } else {
62   $custnum='';
63   $cust_main = new FS::cust_main ( {} );
64   $cust_main->otaker( &getotaker );
65   $cust_main->referral_custnum( $cgi->param('referral_custnum') );
66   $saved_pkgpart = 0;
67   $username = '';
68   $password = '';
69   $popnum = 0;
70 }
71 $cgi->delete_all();
72 $action = $custnum ? 'Edit' : 'Add';
73
74 # top
75
76 $p1 = popurl(1);
77 print header("Customer $action", '');
78 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $error, "</FONT>"
79   if $error;
80
81 print qq!<FORM ACTION="${p1}process/cust_main.cgi" METHOD=POST NAME="form1">!,
82       qq!<INPUT TYPE="hidden" NAME="custnum" VALUE="$custnum">!,
83       qq!Customer # !, ( $custnum ? "<B>$custnum</B>" : " (NEW)" ),
84       
85 ;
86
87 # agent
88
89 $r = qq!<font color="#ff0000">*</font>!;
90
91 @agents = qsearch( 'agent', {} );
92 #die "No agents created!" unless @agents;
93 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;
94 $agentnum = $cust_main->agentnum || $agents[0]->agentnum; #default to first
95 if ( scalar(@agents) == 1 ) {
96   print qq!<INPUT TYPE="hidden" NAME="agentnum" VALUE="$agentnum">!;
97 } else {
98   print qq!<BR><BR>${r}Agent <SELECT NAME="agentnum" SIZE="1">!;
99   my $agent;
100   foreach $agent (sort {
101     $a->agent cmp $b->agent;
102   } @agents) {
103       print '<OPTION VALUE="', $agent->agentnum, '"',
104       " SELECTED"x($agent->agentnum==$agentnum),
105       ">", $agent->agentnum,": ", $agent->agent;
106   }
107   print "</SELECT>";
108 }
109
110 #referral
111
112 $refnum = $cust_main->refnum || $conf->config('referraldefault') || 0;
113 if ( $custnum && ! $conf->exists('editreferrals') ) {
114   print qq!<INPUT TYPE="hidden" NAME="refnum" VALUE="$refnum">!;
115 } else {
116   my(@referrals) = qsearch('part_referral',{});
117   if ( scalar(@referrals) == 1 ) {
118     $refnum ||= $referrals[0]->refnum;
119     print qq!<INPUT TYPE="hidden" NAME="refnum" VALUE="$refnum">!;
120   } else {
121     print qq!<BR><BR>${r}Referral <SELECT NAME="refnum" SIZE="1">!;
122     print "<OPTION> " unless $refnum;
123     my($referral);
124     foreach $referral (sort {
125       $a->refnum <=> $b->refnum;
126     } @referrals) {
127       print "<OPTION" . " SELECTED"x($referral->refnum==$refnum),
128       ">", $referral->refnum, ": ", $referral->referral;
129     }
130     print "</SELECT>";
131   }
132 }
133
134 #referring customer
135
136 print qq!<BR><BR>Referring Customer: !;
137 if ( $cust_main->referral_custnum ) {
138   my $referring_cust_main =
139     qsearchs('cust_main', { custnum => $cust_main->referral_custnum } );
140   print '<A HREF="'. popurl(1). '/cust_main.cgi?'.
141         $cust_main->referral_custnum. '">'.
142         $cust_main->referral_custnum. ': '.
143         ( $referring_cust_main->company
144           || $referring_cust_main->last. ', '. $referring_cust_main->first ).
145         '</A><INPUT TYPE="hidden" NAME="referral_custnum" VALUE="'.
146         $cust_main->referral_custnum. '">';
147 } else {
148   print '(none)<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="">';
149 }
150
151 # contact info
152
153 ($last,$first,$ss,$company,$address1,$address2,$city,$zip)=(
154   $cust_main->last,
155   $cust_main->first,
156   $cust_main->ss,
157   $cust_main->company,
158   $cust_main->address1,
159   $cust_main->address2,
160   $cust_main->city,
161   $cust_main->zip,
162 );
163
164 print "<BR><BR>Billing address", &itable("#cccccc"), <<END;
165 <TR><TH ALIGN="right">${r}Contact name<BR>(last, first)</TH><TD COLSPAN=3>
166 END
167
168 print <<END;
169 <INPUT TYPE="text" NAME="last" VALUE="$last"> , 
170 <INPUT TYPE="text" NAME="first" VALUE="$first">
171 END
172
173 print <<END;
174 </TD><TD ALIGN="right">SS#</TD><TD><INPUT TYPE="text" NAME="ss" VALUE="$ss" SIZE=11></TD></TR>
175 <TR><TD ALIGN="right">Company</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="company" VALUE="$company" SIZE=70></TD></TR>
176 <TR><TH ALIGN="right">${r}Address</TH><TD COLSPAN=5><INPUT TYPE="text" NAME="address1" VALUE="$address1" SIZE=70></TD></TR>
177 <TR><TD ALIGN="right">&nbsp;</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="address2" VALUE="$address2" SIZE=70></TD></TR>
178 <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">
179 END
180
181 my $countrydefault = $conf->config('countrydefault') || 'US';
182 $cust_main->country( $countrydefault ) unless $cust_main->country;
183 $cust_main->state( $conf->config('statedefault') || 'CA' )
184   unless $cust_main->state || $cust_main->country ne 'US';
185 foreach ( sort {
186      ( $b->country eq $countrydefault ) <=> ( $a->country eq $countrydefault )
187   or $a->country                        cmp $b->country
188   or $a->state                          cmp $b->state
189   or $a->county                         cmp $b->county
190 } qsearch('cust_main_county',{}) ) {
191   print "<OPTION";
192   print " SELECTED" if ( $cust_main->state eq $_->state
193                          && $cust_main->county eq $_->county 
194                          && $cust_main->country eq $_->country
195                        );
196   print ">",$_->state;
197   print " (",$_->county,")" if $_->county;
198   print " / ", $_->country;
199 }
200 print qq!</SELECT></TD><TH>${r}Zip</TH><TD><INPUT TYPE="text" NAME="zip" VALUE="$zip" SIZE=10></TD></TR>!;
201
202 ($daytime,$night,$fax)=(
203   $cust_main->daytime,
204   $cust_main->night,
205   $cust_main->fax,
206 );
207
208 print <<END;
209 <TR><TD ALIGN="right">Day Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="daytime" VALUE="$daytime" SIZE=18></TD></TR>
210 <TR><TD ALIGN="right">Night Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="night" VALUE="$night" SIZE=18></TD></TR>
211 <TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="fax" VALUE="$fax" SIZE=12></TD></TR>
212 END
213
214 print "</TABLE>$r required fields<BR>";
215
216 # service address
217
218 if ( defined $cust_main->dbdef_table->column('ship_last') ) {
219
220   print "\n", <<END;
221   <SCRIPT>
222   function changed(what) {
223     what.form.same.checked = false;
224   }
225   function samechanged(what) {
226     if ( what.checked ) {
227 END
228 print "      what.form.ship_$_.value = what.form.$_.value;\n"
229   for (qw( last first company address1 address2 city zip daytime night fax ));
230 print <<END;
231       what.form.ship_state.selectedIndex = what.form.state.selectedIndex;
232     }
233   }
234   </SCRIPT>
235 END
236
237   print '<BR>Service address ',
238         '(<INPUT TYPE="checkbox" NAME="same" VALUE="Y" onClick="samechanged(this)"';
239   unless ( $cust_main->ship_last ) {
240     print ' CHECKED';
241     foreach (
242       qw( last first company address1 address2 city state zip daytime night fax)
243     ) {
244       $cust_main->set("ship_$_", $cust_main->get($_) );
245     }
246   }
247   print '>same as billing address)<BR>';
248
249   my($ship_last,$ship_first,$ship_company,$ship_address1,$ship_address2,$ship_city,$ship_zip)=(
250     $cust_main->ship_last,
251     $cust_main->ship_first,
252     $cust_main->ship_company,
253     $cust_main->ship_address1,
254     $cust_main->ship_address2,
255     $cust_main->ship_city,
256     $cust_main->ship_zip,
257   );
258
259   print &itable("#cccccc"), <<END;
260   <TR><TH ALIGN="right">${r}Contact name<BR>(last, first)</TH><TD COLSPAN=5>
261 END
262
263   print <<END;
264   <INPUT TYPE="text" NAME="ship_last" VALUE="$ship_last" onChange="changed(this)"> , 
265   <INPUT TYPE="text" NAME="ship_first" VALUE="$ship_first" onChange="changed(this)">
266 END
267
268   print <<END;
269   </TD></TR>
270   <TR><TD ALIGN="right">Company</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="ship_company" VALUE="$ship_company" SIZE=70 onChange="changed(this)"></TD></TR>
271   <TR><TH ALIGN="right">${r}Address</TH><TD COLSPAN=5><INPUT TYPE="text" NAME="ship_address1" VALUE="$ship_address1" SIZE=70 onChange="changed(this)"></TD></TR>
272   <TR><TD ALIGN="right">&nbsp;</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="ship_address2" VALUE="$ship_address2" SIZE=70 onChange="changed(this)"></TD></TR>
273   <TR><TH ALIGN="right">${r}City</TH><TD><INPUT TYPE="text" NAME="ship_city" VALUE="$ship_city" onChange="changed(this)"></TD><TH ALIGN="right">${r}State/Country</TH><TD><SELECT NAME="ship_state" SIZE="1" onChange="changed(this)">
274 END
275
276   $cust_main->ship_country( $conf->config('countrydefault') || 'US' )
277     unless $cust_main->ship_country;
278   $cust_main->ship_state( $conf->config('statedefault') || 'CA' )
279     unless $cust_main->ship_state || $cust_main->ship_country ne 'US';
280   foreach ( qsearch('cust_main_county',{}) ) {
281     print "<OPTION";
282     print " SELECTED" if ( $cust_main->ship_state eq $_->state
283                            && $cust_main->ship_county eq $_->county 
284                            && $cust_main->ship_country eq $_->country
285                          );
286     print ">",$_->state;
287     print " (",$_->county,")" if $_->county;
288     print " / ", $_->country;
289   }
290   print qq!</SELECT></TD><TH>${r}Zip</TH><TD><INPUT TYPE="text" NAME="ship_zip" VALUE="$ship_zip" SIZE=10 onChange="changed(this)"></TD></TR>!;
291
292   my($ship_daytime,$ship_night,$ship_fax)=(
293     $cust_main->ship_daytime,
294     $cust_main->ship_night,
295     $cust_main->ship_fax,
296   );
297
298   print <<END;
299   <TR><TD ALIGN="right">Day Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="ship_daytime" VALUE="$ship_daytime" SIZE=18 onChange="changed(this)"></TD></TR>
300   <TR><TD ALIGN="right">Night Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="ship_night" VALUE="$ship_night" SIZE=18 onChange="changed(this)"></TD></TR>
301   <TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="ship_fax" VALUE="$ship_fax" SIZE=12 onChange="changed(this)"></TD></TR>
302 END
303
304   print "</TABLE>$r required fields<BR>";
305
306 }
307
308 # billing info
309
310 sub expselect {
311   my $prefix = shift;
312   my( $m, $y ) = (0, 0);
313   if ( scalar(@_) ) {
314     my $date = shift || '01-2000';
315     if ( $date  =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #PostgreSQL date format
316       ( $m, $y ) = ( $2, $1 );
317     } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
318       ( $m, $y ) = ( $1, $3 );
319     } else {
320       die "unrecognized expiration date format: $date";
321     }
322   }
323
324   my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
325   for ( 1 .. 12 ) {
326     $return .= "<OPTION";
327     $return .= " SELECTED" if $_ == $m;
328     $return .= ">$_";
329   }
330   $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
331   for ( 2001 .. 2037 ) {
332     $return .= "<OPTION";
333     $return .= " SELECTED" if $_ == $y;
334     $return .= ">$_";
335   }
336   $return .= "</SELECT>";
337
338   $return;
339 }
340
341 print "<BR>Billing information", &itable("#cccccc"),
342       qq!<TR><TD><INPUT TYPE="checkbox" NAME="tax" VALUE="Y"!;
343 print qq! CHECKED! if $cust_main->tax eq "Y";
344 print qq!>Tax Exempt</TD></TR>!;
345 print qq!<TR><TD><INPUT TYPE="checkbox" NAME="invoicing_list_POST" VALUE="POST"!;
346 @invoicing_list = $cust_main->invoicing_list;
347 print qq! CHECKED!
348   if ( ! @invoicing_list && ! $conf->exists('disablepostalinvoicedefault') )
349      || grep { $_ eq 'POST' } @invoicing_list;
350 print qq!>Postal mail invoice</TD></TR>!;
351 $invoicing_list = join(', ', grep { $_ ne 'POST' } @invoicing_list );
352 print qq!<TR><TD>Email invoice <INPUT TYPE="text" NAME="invoicing_list" VALUE="$invoicing_list"></TD></TR>!;
353
354 print "<TR><TD>Billing type</TD></TR>",
355       "</TABLE>",
356       &table("#cccccc"), "<TR>";
357
358 ($payinfo, $payname)=(
359   $cust_main->payinfo,
360   $cust_main->payname,
361 );
362
363 %payby = (
364   '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="">!,
365   '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">!,
366   'COMP' => qq!Complimentary<BR>${r}Approved by<INPUT TYPE="text" NAME="COMP_payinfo" VALUE=""><BR>${r}Exp !. expselect("COMP"),
367 );
368 %paybychecked = (
369   '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">!,
370   '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">!,
371   'COMP' => qq!Complimentary<BR>${r}Approved by<INPUT TYPE="text" NAME="COMP_payinfo" VALUE="$payinfo"><BR>${r}Exp !. expselect("COMP", $cust_main->paydate),
372 );
373 for (qw(CARD BILL COMP)) {
374   print qq!<TD VALIGN=TOP><INPUT TYPE="radio" NAME="payby" VALUE="$_"!;
375   if ($cust_main->payby eq "$_") {
376     print qq! CHECKED> $paybychecked{$_}</TD>!;
377   } else {
378     print qq!> $payby{$_}</TD>!;
379   }
380 }
381
382 print "</TR></TABLE>$r required fields for each billing type";
383
384 if ( defined $cust_main->dbdef_table->column('comments') ) {
385     print "<BR><BR>Comments", &itable("#cccccc"),
386           qq!<TR><TD><TEXTAREA COLS=80 ROWS=5 WRAP="HARD" NAME="comments">!,
387           $cust_main->comments, "</TEXTAREA>",
388           "</TD></TR></TABLE>";
389 }
390
391 unless ( $custnum ) {
392   # pry the wrong place for this logic.  also pretty expensive
393   #use FS::part_pkg;
394
395   #false laziness, copied from FS::cust_pkg::order
396   my $pkgpart;
397   if ( scalar(@agents) == 1 ) {
398     # $pkgpart->{PKGPART} is true iff $custnum may purchase PKGPART
399     my($agent)=qsearchs('agent',{'agentnum'=> $agentnum });
400     $pkgpart = $agent->pkgpart_hashref;
401   } else {
402     #can't know (agent not chosen), so, allow all
403     my %typenum;
404     foreach my $agent ( @agents ) {
405       next if $typenum{$agent->typenum}++;
406       #fixed in 5.004_05 #$pkgpart->{$_}++ foreach keys %{ $agent->pkgpart_hashref }
407       foreach ( keys %{ $agent->pkgpart_hashref } ) { $pkgpart->{$_}++; } #5.004_04 workaround
408     }
409   }
410   #eslaf
411
412   my @part_pkg = grep { $_->svcpart('svc_acct') && $pkgpart->{ $_->pkgpart } }
413     qsearch( 'part_pkg', {} );
414
415   if ( @part_pkg ) {
416
417 #    print "<BR><BR>First package", &itable("#cccccc", "0 ALIGN=LEFT"),
418 #apiabuse & undesirable wrapping
419     print "<BR><BR>First package", &itable("#cccccc"),
420           qq!<TR><TD COLSPAN=2><SELECT NAME="pkgpart_svcpart">!;
421
422     print qq!<OPTION VALUE="">(none)!;
423
424     foreach my $part_pkg ( @part_pkg ) {
425       print qq!<OPTION VALUE="!,
426 #              $part_pkg->pkgpart. "_". $pkgpart{ $part_pkg->pkgpart }, '"';
427               $part_pkg->pkgpart. "_". $part_pkg->svcpart, '"';
428       print " SELECTED" if $saved_pkgpart && ( $part_pkg->pkgpart == $saved_pkgpart );
429       print ">", $part_pkg->pkg, " - ", $part_pkg->comment;
430     }
431     print "</SELECT></TD></TR>";
432
433     #false laziness: (mostly) copied from edit/svc_acct.cgi
434     #$ulen = $svc_acct->dbdef_table->column('username')->length;
435     $ulen = dbdef->table('svc_acct')->column('username')->length;
436     $ulen2 = $ulen+2;
437     print <<END;
438 <TR><TD ALIGN="right">Username</TD>
439 <TD><INPUT TYPE="text" NAME="username" VALUE="$username" SIZE=$ulen2 MAXLENGTH=$ulen></TD></TR>
440 <TR><TD ALIGN="right">Password</TD>
441 <TD><INPUT TYPE="text" NAME="_password" VALUE="$password" SIZE=10 MAXLENGTH=8>
442 (blank to generate)</TD></TR>
443 END
444
445     print '<TR><TD ALIGN="right">Access number</TD><TD WIDTH="100%">'
446           .
447           &FS::svc_acct_pop::popselector($popnum).
448           '</TD></TR></TABLE>'
449           ;
450   }
451 }
452
453 $otaker = $cust_main->otaker;
454 print qq!<INPUT TYPE="hidden" NAME="otaker" VALUE="$otaker">!,
455       qq!<BR><INPUT TYPE="submit" VALUE="!,
456       $custnum ?  "Apply Changes" : "Add Customer", qq!">!,
457       "</FORM></BODY></HTML>",
458 ;
459
460 %>