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