d3251c2b9a8797f9abd04b1afe491f0d95e07e23
[freeside.git] / httemplate / edit / cust_main.cgi
1 <%
2 #<!-- $Id: cust_main.cgi,v 1.18 2001-12-28 14:40:35 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 '<BR><BR>Referring Customer: <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 } elsif ( ! $conf->exists('disable_customer_referrals') ) {
148   print '<BR><BR>Referring customer number: <INPUT TYPE="text" NAME="referral_custnum" VALUE="">';
149 } else {
150   print '<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="">';
151 }
152
153 # contact info
154
155 ($last,$first,$ss,$company,$address1,$address2,$city,$zip)=(
156   $cust_main->last,
157   $cust_main->first,
158   $cust_main->ss,
159   $cust_main->company,
160   $cust_main->address1,
161   $cust_main->address2,
162   $cust_main->city,
163   $cust_main->zip,
164 );
165
166 print "<BR><BR>Billing address", &itable("#cccccc"), <<END;
167 <TR><TH ALIGN="right">${r}Contact name<BR>(last, first)</TH><TD COLSPAN=3>
168 END
169
170 print <<END;
171 <INPUT TYPE="text" NAME="last" VALUE="$last"> , 
172 <INPUT TYPE="text" NAME="first" VALUE="$first">
173 END
174
175 print <<END;
176 </TD><TD ALIGN="right">SS#</TD><TD><INPUT TYPE="text" NAME="ss" VALUE="$ss" SIZE=11></TD></TR>
177 <TR><TD ALIGN="right">Company</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="company" VALUE="$company" SIZE=70></TD></TR>
178 <TR><TH ALIGN="right">${r}Address</TH><TD COLSPAN=5><INPUT TYPE="text" NAME="address1" VALUE="$address1" SIZE=70></TD></TR>
179 <TR><TD ALIGN="right">&nbsp;</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="address2" VALUE="$address2" SIZE=70></TD></TR>
180 <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">
181 END
182
183 my $countrydefault = $conf->config('countrydefault') || 'US';
184 $cust_main->country( $countrydefault ) unless $cust_main->country;
185 $cust_main->state( $conf->config('statedefault') || 'CA' )
186   unless $cust_main->state || $cust_main->country ne 'US';
187 foreach ( sort {
188      ( $b->country eq $countrydefault ) <=> ( $a->country eq $countrydefault )
189   or $a->country                        cmp $b->country
190   or $a->state                          cmp $b->state
191   or $a->county                         cmp $b->county
192 } qsearch('cust_main_county',{}) ) {
193   print "<OPTION";
194   print " SELECTED" if ( $cust_main->state eq $_->state
195                          && $cust_main->county eq $_->county 
196                          && $cust_main->country eq $_->country
197                        );
198   print ">",$_->state;
199   print " (",$_->county,")" if $_->county;
200   print " / ", $_->country;
201 }
202 print qq!</SELECT></TD><TH>${r}Zip</TH><TD><INPUT TYPE="text" NAME="zip" VALUE="$zip" SIZE=10></TD></TR>!;
203
204 ($daytime,$night,$fax)=(
205   $cust_main->daytime,
206   $cust_main->night,
207   $cust_main->fax,
208 );
209
210 print <<END;
211 <TR><TD ALIGN="right">Day Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="daytime" VALUE="$daytime" SIZE=18></TD></TR>
212 <TR><TD ALIGN="right">Night Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="night" VALUE="$night" SIZE=18></TD></TR>
213 <TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="fax" VALUE="$fax" SIZE=12></TD></TR>
214 END
215
216 print "</TABLE>$r required fields<BR>";
217
218 # service address
219
220 if ( defined $cust_main->dbdef_table->column('ship_last') ) {
221
222   print "\n", <<END;
223   <SCRIPT>
224   function changed(what) {
225     what.form.same.checked = false;
226   }
227   function samechanged(what) {
228     if ( what.checked ) {
229 END
230 print "      what.form.ship_$_.value = what.form.$_.value;\n"
231   for (qw( last first company address1 address2 city zip daytime night fax ));
232 print <<END;
233       what.form.ship_state.selectedIndex = what.form.state.selectedIndex;
234     }
235   }
236   </SCRIPT>
237 END
238
239   print '<BR>Service address ',
240         '(<INPUT TYPE="checkbox" NAME="same" VALUE="Y" onClick="samechanged(this)"';
241   unless ( $cust_main->ship_last ) {
242     print ' CHECKED';
243     foreach (
244       qw( last first company address1 address2 city state zip daytime night fax)
245     ) {
246       $cust_main->set("ship_$_", $cust_main->get($_) );
247     }
248   }
249   print '>same as billing address)<BR>';
250
251   my($ship_last,$ship_first,$ship_company,$ship_address1,$ship_address2,$ship_city,$ship_zip)=(
252     $cust_main->ship_last,
253     $cust_main->ship_first,
254     $cust_main->ship_company,
255     $cust_main->ship_address1,
256     $cust_main->ship_address2,
257     $cust_main->ship_city,
258     $cust_main->ship_zip,
259   );
260
261   print &itable("#cccccc"), <<END;
262   <TR><TH ALIGN="right">${r}Contact name<BR>(last, first)</TH><TD COLSPAN=5>
263 END
264
265   print <<END;
266   <INPUT TYPE="text" NAME="ship_last" VALUE="$ship_last" onChange="changed(this)"> , 
267   <INPUT TYPE="text" NAME="ship_first" VALUE="$ship_first" onChange="changed(this)">
268 END
269
270   print <<END;
271   </TD></TR>
272   <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>
273   <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>
274   <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>
275   <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)">
276 END
277
278   $cust_main->ship_country( $conf->config('countrydefault') || 'US' )
279     unless $cust_main->ship_country;
280   $cust_main->ship_state( $conf->config('statedefault') || 'CA' )
281     unless $cust_main->ship_state || $cust_main->ship_country ne 'US';
282   foreach ( qsearch('cust_main_county',{}) ) {
283     print "<OPTION";
284     print " SELECTED" if ( $cust_main->ship_state eq $_->state
285                            && $cust_main->ship_county eq $_->county 
286                            && $cust_main->ship_country eq $_->country
287                          );
288     print ">",$_->state;
289     print " (",$_->county,")" if $_->county;
290     print " / ", $_->country;
291   }
292   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>!;
293
294   my($ship_daytime,$ship_night,$ship_fax)=(
295     $cust_main->ship_daytime,
296     $cust_main->ship_night,
297     $cust_main->ship_fax,
298   );
299
300   print <<END;
301   <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>
302   <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>
303   <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>
304 END
305
306   print "</TABLE>$r required fields<BR>";
307
308 }
309
310 # billing info
311
312 sub expselect {
313   my $prefix = shift;
314   my( $m, $y ) = (0, 0);
315   if ( scalar(@_) ) {
316     my $date = shift || '01-2000';
317     if ( $date  =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #PostgreSQL date format
318       ( $m, $y ) = ( $2, $1 );
319     } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
320       ( $m, $y ) = ( $1, $3 );
321     } else {
322       die "unrecognized expiration date format: $date";
323     }
324   }
325
326   my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
327   for ( 1 .. 12 ) {
328     $return .= "<OPTION";
329     $return .= " SELECTED" if $_ == $m;
330     $return .= ">$_";
331   }
332   $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
333   for ( 2001 .. 2037 ) {
334     $return .= "<OPTION";
335     $return .= " SELECTED" if $_ == $y;
336     $return .= ">$_";
337   }
338   $return .= "</SELECT>";
339
340   $return;
341 }
342
343 print "<BR>Billing information", &itable("#cccccc"),
344       qq!<TR><TD><INPUT TYPE="checkbox" NAME="tax" VALUE="Y"!;
345 print qq! CHECKED! if $cust_main->tax eq "Y";
346 print qq!>Tax Exempt</TD></TR>!;
347 print qq!<TR><TD><INPUT TYPE="checkbox" NAME="invoicing_list_POST" VALUE="POST"!;
348 @invoicing_list = $cust_main->invoicing_list;
349 print qq! CHECKED!
350   if ( ! @invoicing_list && ! $conf->exists('disablepostalinvoicedefault') )
351      || grep { $_ eq 'POST' } @invoicing_list;
352 print qq!>Postal mail invoice</TD></TR>!;
353 $invoicing_list = join(', ', grep { $_ ne 'POST' } @invoicing_list );
354 print qq!<TR><TD>Email invoice <INPUT TYPE="text" NAME="invoicing_list" VALUE="$invoicing_list"></TD></TR>!;
355
356 print "<TR><TD>Billing type</TD></TR>",
357       "</TABLE>",
358       &table("#cccccc"), "<TR>";
359
360 ($payinfo, $payname)=(
361   $cust_main->payinfo,
362   $cust_main->payname,
363 );
364
365 %payby = (
366   '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="">!,
367   'BILL' => qq!Billing<BR>P.O. <INPUT TYPE="text" NAME="BILL_payinfo" VALUE=""><BR>${r}Exp !. expselect("BILL", "12-2037"). qq!<BR>Attention<BR><INPUT TYPE="text" NAME="BILL_payname" VALUE="">!,
368   'COMP' => qq!Complimentary<BR>${r}Approved by<INPUT TYPE="text" NAME="COMP_payinfo" VALUE=""><BR>${r}Exp !. expselect("COMP"),
369 );
370 %paybychecked = (
371   '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">!,
372   'BILL' => qq!Billing<BR>P.O. <INPUT TYPE="text" NAME="BILL_payinfo" VALUE="$payinfo"><BR>${r}Exp !. expselect("BILL", $cust_main->paydate). qq!<BR>Attention<BR><INPUT TYPE="text" NAME="BILL_payname" VALUE="$payname">!,
373   'COMP' => qq!Complimentary<BR>${r}Approved by<INPUT TYPE="text" NAME="COMP_payinfo" VALUE="$payinfo"><BR>${r}Exp !. expselect("COMP", $cust_main->paydate),
374 );
375 for (qw(CARD BILL COMP)) {
376   print qq!<TD VALIGN=TOP><INPUT TYPE="radio" NAME="payby" VALUE="$_"!;
377   if ($cust_main->payby eq "$_") {
378     print qq! CHECKED> $paybychecked{$_}</TD>!;
379   } else {
380     print qq!> $payby{$_}</TD>!;
381   }
382 }
383
384 print "</TR></TABLE>$r required fields for each billing type";
385
386 if ( defined $cust_main->dbdef_table->column('comments') ) {
387     print "<BR><BR>Comments", &itable("#cccccc"),
388           qq!<TR><TD><TEXTAREA COLS=80 ROWS=5 WRAP="HARD" NAME="comments">!,
389           $cust_main->comments, "</TEXTAREA>",
390           "</TD></TR></TABLE>";
391 }
392
393 unless ( $custnum ) {
394   # pry the wrong place for this logic.  also pretty expensive
395   #use FS::part_pkg;
396
397   #false laziness, copied from FS::cust_pkg::order
398   my $pkgpart;
399   if ( scalar(@agents) == 1 ) {
400     # $pkgpart->{PKGPART} is true iff $custnum may purchase PKGPART
401     my($agent)=qsearchs('agent',{'agentnum'=> $agentnum });
402     $pkgpart = $agent->pkgpart_hashref;
403   } else {
404     #can't know (agent not chosen), so, allow all
405     my %typenum;
406     foreach my $agent ( @agents ) {
407       next if $typenum{$agent->typenum}++;
408       #fixed in 5.004_05 #$pkgpart->{$_}++ foreach keys %{ $agent->pkgpart_hashref }
409       foreach ( keys %{ $agent->pkgpart_hashref } ) { $pkgpart->{$_}++; } #5.004_04 workaround
410     }
411   }
412   #eslaf
413
414   my @part_pkg = grep { $_->svcpart('svc_acct') && $pkgpart->{ $_->pkgpart } }
415     qsearch( 'part_pkg', { 'disabled' => '' } );
416
417   if ( @part_pkg ) {
418
419 #    print "<BR><BR>First package", &itable("#cccccc", "0 ALIGN=LEFT"),
420 #apiabuse & undesirable wrapping
421     print "<BR><BR>First package", &itable("#cccccc"),
422           qq!<TR><TD COLSPAN=2><SELECT NAME="pkgpart_svcpart">!;
423
424     print qq!<OPTION VALUE="">(none)!;
425
426     foreach my $part_pkg ( @part_pkg ) {
427       print qq!<OPTION VALUE="!,
428 #              $part_pkg->pkgpart. "_". $pkgpart{ $part_pkg->pkgpart }, '"';
429               $part_pkg->pkgpart. "_". $part_pkg->svcpart, '"';
430       print " SELECTED" if $saved_pkgpart && ( $part_pkg->pkgpart == $saved_pkgpart );
431       print ">", $part_pkg->pkg, " - ", $part_pkg->comment;
432     }
433     print "</SELECT></TD></TR>";
434
435     #false laziness: (mostly) copied from edit/svc_acct.cgi
436     #$ulen = $svc_acct->dbdef_table->column('username')->length;
437     $ulen = dbdef->table('svc_acct')->column('username')->length;
438     $ulen2 = $ulen+2;
439     my $passwordmax = $conf->config('passwordmax') || 8;
440     my $pmax2 = $passwordmax + 2;
441     print <<END;
442 <TR><TD ALIGN="right">Username</TD>
443 <TD><INPUT TYPE="text" NAME="username" VALUE="$username" SIZE=$ulen2 MAXLENGTH=$ulen></TD></TR>
444 <TR><TD ALIGN="right">Password</TD>
445 <TD><INPUT TYPE="text" NAME="_password" VALUE="$password" SIZE=$pmax2 MAXLENGTH=$passwordmax>
446 (blank to generate)</TD></TR>
447 END
448
449     print '<TR><TD ALIGN="right">Access number</TD><TD WIDTH="100%">'
450           .
451           &FS::svc_acct_pop::popselector($popnum).
452           '</TD></TR></TABLE>'
453           ;
454   }
455 }
456
457 $otaker = $cust_main->otaker;
458 print qq!<INPUT TYPE="hidden" NAME="otaker" VALUE="$otaker">!,
459       qq!<BR><INPUT TYPE="submit" VALUE="!,
460       $custnum ?  "Apply Changes" : "Add Customer", qq!">!,
461       "</FORM></BODY></HTML>",
462 ;
463
464 %>