4 #for misplaced logic below
7 #for false laziness below (now more properly lazy)
10 #for (other) false laziness below
14 my $conf = new FS::Conf;
19 my($custnum, $username, $password, $popnum, $cust_main, $saved_pkgpart);
20 if ( $cgi->param('error') ) {
21 $error = $cgi->param('error');
22 $cust_main = new FS::cust_main ( {
23 map { $_, scalar($cgi->param($_)) } fields('cust_main')
25 $custnum = $cust_main->custnum;
26 $saved_pkgpart = $cgi->param('pkgpart_svcpart') || '';
27 if ( $saved_pkgpart =~ /^(\d+)_/ ) {
32 $username = $cgi->param('username');
33 $password = $cgi->param('_password');
34 $popnum = $cgi->param('popnum');
35 } elsif ( $cgi->keywords ) { #editing
36 my( $query ) = $cgi->keywords;
39 $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
46 $cust_main = new FS::cust_main ( {} );
47 $cust_main->otaker( &getotaker );
48 $cust_main->referral_custnum( $cgi->param('referral_custnum') );
55 my $action = $custnum ? 'Edit' : 'Add';
60 print header("Customer $action", '');
61 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $error, "</FONT>"
64 print qq!<FORM ACTION="${p1}process/cust_main.cgi" METHOD=POST NAME="form1">!,
65 qq!<INPUT TYPE="hidden" NAME="custnum" VALUE="$custnum">!,
66 qq!Customer # !, ( $custnum ? "<B>$custnum</B>" : " (NEW)" ),
72 my $r = qq!<font color="#ff0000">*</font> !;
74 my @agents = qsearch( 'agent', {} );
75 #die "No agents created!" unless @agents;
76 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;
77 my $agentnum = $cust_main->agentnum || $agents[0]->agentnum; #default to first
78 if ( scalar(@agents) == 1 ) {
79 print qq!<INPUT TYPE="hidden" NAME="agentnum" VALUE="$agentnum">!;
81 print qq!<BR><BR>${r}Agent <SELECT NAME="agentnum" SIZE="1">!;
83 foreach $agent (sort {
84 $a->agent cmp $b->agent;
86 print '<OPTION VALUE="', $agent->agentnum, '"',
87 " SELECTED"x($agent->agentnum==$agentnum),
89 #">", $agent->agentnum,": ", $agent->agent;
96 my $refnum = $cust_main->refnum || $conf->config('referraldefault') || 0;
97 if ( $custnum && ! $conf->exists('editreferrals') ) {
98 print qq!<INPUT TYPE="hidden" NAME="refnum" VALUE="$refnum">!;
100 my(@referrals) = qsearch('part_referral',{});
101 if ( scalar(@referrals) == 0 ) {
102 die "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.";
103 } elsif ( scalar(@referrals) == 1 ) {
104 $refnum ||= $referrals[0]->refnum;
105 print qq!<INPUT TYPE="hidden" NAME="refnum" VALUE="$refnum">!;
107 print qq!<BR><BR>${r}Advertising source <SELECT NAME="refnum" SIZE="1">!;
108 print "<OPTION> " unless $refnum;
110 foreach $referral (sort {
111 $a->refnum <=> $b->refnum;
113 print "<OPTION" . " SELECTED"x($referral->refnum==$refnum),
114 ">", $referral->refnum, ": ", $referral->referral;
122 #print qq!<BR><BR>Referring Customer: !;
123 if ( $cust_main->referral_custnum ) {
124 my $referring_cust_main =
125 qsearchs('cust_main', { custnum => $cust_main->referral_custnum } );
126 print '<BR><BR>Referring Customer: <A HREF="'. popurl(1). '/cust_main.cgi?'.
127 $cust_main->referral_custnum. '">'.
128 $cust_main->referral_custnum. ': '.
129 ( $referring_cust_main->company
130 || $referring_cust_main->last. ', '. $referring_cust_main->first ).
131 '</A><INPUT TYPE="hidden" NAME="referral_custnum" VALUE="'.
132 $cust_main->referral_custnum. '">';
133 } elsif ( ! $conf->exists('disable_customer_referrals') ) {
134 print '<BR><BR>Referring customer number: <INPUT TYPE="text" NAME="referral_custnum" VALUE="">';
136 print '<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="">';
141 my($last,$first,$ss,$company,$address1,$address2,$city,$zip)=(
146 $cust_main->address1,
147 $cust_main->address2,
152 print "<BR><BR>Billing address", &itable("#cccccc"), <<END;
153 <TR><TH ALIGN="right">${r}Contact name<BR>(last, first)</TH><TD COLSPAN=3>
157 <INPUT TYPE="text" NAME="last" VALUE="$last"> ,
158 <INPUT TYPE="text" NAME="first" VALUE="$first">
162 if ( $conf->exists('show_ss') ) {
163 print qq!<TD ALIGN="right">SS#</TD><TD><INPUT TYPE="text" NAME="ss" VALUE="$ss" SIZE=11></TD>!;
165 print qq!<TD><INPUT TYPE="hidden" NAME="ss" VALUE="$ss"></TD>!;
170 <TR><TD ALIGN="right">Company</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="company" VALUE="$company" SIZE=70></TD></TR>
171 <TR><TH ALIGN="right">${r}Address</TH><TD COLSPAN=5><INPUT TYPE="text" NAME="address1" VALUE="$address1" SIZE=70></TD></TR>
172 <TR><TD ALIGN="right"> </TD><TD COLSPAN=5><INPUT TYPE="text" NAME="address2" VALUE="$address2" SIZE=70></TD></TR>
173 <TR><TH ALIGN="right">${r}City</TH><TD><INPUT TYPE="text" NAME="city" VALUE="$city"></TD><TH ALIGN="right">${r}State</TH><TD>
176 #false laziness with ship state
177 my $countrydefault = $conf->config('countrydefault') || 'US';
178 $cust_main->country( $countrydefault ) unless $cust_main->country;
180 $cust_main->state( $conf->config('statedefault') || 'CA' )
181 unless $cust_main->state || $cust_main->country ne 'US';
183 my($county_html, $state_html, $country_html) =
184 FS::cust_main_county::regionselector( $cust_main->county,
186 $cust_main->country );
188 print "$county_html $state_html";
190 print qq!</TD><TH>${r}Zip</TH><TD><INPUT TYPE="text" NAME="zip" VALUE="$zip" SIZE=10></TD></TR>!;
192 my($daytime,$night,$fax)=(
199 <TR><TH ALIGN="right">${r}Country</TH><TD>$country_html</TD></TR>
200 <TR><TD ALIGN="right">Day Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="daytime" VALUE="$daytime" SIZE=18></TD></TR>
201 <TR><TD ALIGN="right">Night Phone</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="night" VALUE="$night" SIZE=18></TD></TR>
202 <TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5><INPUT TYPE="text" NAME="fax" VALUE="$fax" SIZE=12></TD></TR>
205 print "</TABLE>${r}required fields<BR>";
209 if ( defined $cust_main->dbdef_table->column('ship_last') ) {
213 function changed(what) {
214 what.form.same.checked = false;
216 function samechanged(what) {
217 if ( what.checked ) {
219 print " what.form.ship_$_.value = what.form.$_.value;\n"
220 for (qw( last first company address1 address2 city zip daytime night fax ));
222 what.form.ship_country.selectedIndex = what.form.country.selectedIndex;
223 ship_country_changed(what.form.ship_country);
224 what.form.ship_state.selectedIndex = what.form.state.selectedIndex;
225 ship_state_changed(what.form.ship_state);
226 what.form.ship_county.selectedIndex = what.form.county.selectedIndex;
232 print '<BR>Service address ',
233 '(<INPUT TYPE="checkbox" NAME="same" VALUE="Y" onClick="samechanged(this)"';
234 unless ( $cust_main->ship_last ) {
237 qw( last first company address1 address2 city county state zip country
240 $cust_main->set("ship_$_", $cust_main->get($_) );
243 print '>same as billing address)<BR>';
245 my($ship_last,$ship_first,$ship_company,$ship_address1,$ship_address2,$ship_city,$ship_zip)=(
246 $cust_main->ship_last,
247 $cust_main->ship_first,
248 $cust_main->ship_company,
249 $cust_main->ship_address1,
250 $cust_main->ship_address2,
251 $cust_main->ship_city,
252 $cust_main->ship_zip,
255 print &itable("#cccccc"), <<END;
256 <TR><TH ALIGN="right">${r}Contact name<BR>(last, first)</TH><TD COLSPAN=5>
260 <INPUT TYPE="text" NAME="ship_last" VALUE="$ship_last" onChange="changed(this)"> ,
261 <INPUT TYPE="text" NAME="ship_first" VALUE="$ship_first" onChange="changed(this)">
266 <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>
267 <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>
268 <TR><TD ALIGN="right"> </TD><TD COLSPAN=5><INPUT TYPE="text" NAME="ship_address2" VALUE="$ship_address2" SIZE=70 onChange="changed(this)"></TD></TR>
269 <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>
272 #false laziness with regular state
273 $cust_main->ship_country( $countrydefault ) unless $cust_main->ship_country;
275 $cust_main->ship_state( $conf->config('statedefault') || 'CA' )
276 unless $cust_main->ship_state || $cust_main->ship_country ne 'US';
278 my($ship_county_html, $ship_state_html, $ship_country_html) =
279 FS::cust_main_county::regionselector( $cust_main->ship_county,
280 $cust_main->ship_state,
281 $cust_main->ship_country,
285 print "$ship_county_html $ship_state_html";
287 print qq!</TD><TH>${r}Zip</TH><TD><INPUT TYPE="text" NAME="ship_zip" VALUE="$ship_zip" SIZE=10 onChange="changed(this)"></TD></TR>!;
289 my($ship_daytime,$ship_night,$ship_fax)=(
290 $cust_main->ship_daytime,
291 $cust_main->ship_night,
292 $cust_main->ship_fax,
296 <TR><TH ALIGN="right">${r}Country</TH><TD>$ship_country_html</TD></TR>
297 <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>
298 <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>
299 <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 print "</TABLE>${r}required fields<BR>";
310 my( $m, $y ) = (0, 0);
312 my $date = shift || '01-2000';
313 if ( $date =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #PostgreSQL date format
314 ( $m, $y ) = ( $2, $1 );
315 } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
316 ( $m, $y ) = ( $1, $3 );
318 die "unrecognized expiration date format: $date";
322 my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
324 $return .= "<OPTION";
325 $return .= " SELECTED" if $_ == $m;
328 $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
329 for ( 2001 .. 2037 ) {
330 $return .= "<OPTION";
331 $return .= " SELECTED" if $_ == $y;
334 $return .= "</SELECT>";
339 print "<BR>Billing information", &itable("#cccccc"),
340 qq!<TR><TD><INPUT TYPE="checkbox" NAME="tax" VALUE="Y"!;
341 print qq! CHECKED! if $cust_main->tax eq "Y";
342 print qq!>Tax Exempt</TD></TR>!;
343 print qq!<TR><TD><INPUT TYPE="checkbox" NAME="invoicing_list_POST" VALUE="POST"!;
344 my @invoicing_list = $cust_main->invoicing_list;
346 if ( ! @invoicing_list && ! $conf->exists('disablepostalinvoicedefault') )
347 || grep { $_ eq 'POST' } @invoicing_list;
348 print qq!>Postal mail invoice</TD></TR>!;
349 my $invoicing_list = join(', ', grep { $_ ne 'POST' } @invoicing_list );
350 print qq!<TR><TD>Email invoice <INPUT TYPE="text" NAME="invoicing_list" VALUE="$invoicing_list"></TD></TR>!;
352 print "<TR><TD>Billing type</TD></TR>",
354 &table("#cccccc"), "<TR>";
356 my($payinfo, $payname)=(
362 '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="">!,
363 '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="">!,
364 'COMP' => qq!Complimentary<BR>${r}Approved by<INPUT TYPE="text" NAME="COMP_payinfo" VALUE=""><BR>${r}Exp !. expselect("COMP"),
367 '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">!,
368 '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">!,
369 'COMP' => qq!Complimentary<BR>${r}Approved by<INPUT TYPE="text" NAME="COMP_payinfo" VALUE="$payinfo"><BR>${r}Exp !. expselect("COMP", $cust_main->paydate),
371 for (qw(CARD BILL COMP)) {
372 print qq!<TD VALIGN=TOP><INPUT TYPE="radio" NAME="payby" VALUE="$_"!;
373 if ($cust_main->payby eq "$_") {
374 print qq! CHECKED> $paybychecked{$_}</TD>!;
376 print qq!> $payby{$_}</TD>!;
380 print "</TR></TABLE>$r required fields for each billing type";
382 if ( defined $cust_main->dbdef_table->column('comments') ) {
383 print "<BR><BR>Comments", &itable("#cccccc"),
384 qq!<TR><TD><TEXTAREA COLS=80 ROWS=5 WRAP="HARD" NAME="comments">!,
385 $cust_main->comments, "</TEXTAREA>",
386 "</TD></TR></TABLE>";
389 unless ( $custnum ) {
390 # pry the wrong place for this logic. also pretty expensive
393 #false laziness, copied from FS::cust_pkg::order
395 if ( scalar(@agents) == 1 ) {
396 # $pkgpart->{PKGPART} is true iff $custnum may purchase PKGPART
397 my($agent)=qsearchs('agent',{'agentnum'=> $agentnum });
398 $pkgpart = $agent->pkgpart_hashref;
400 #can't know (agent not chosen), so, allow all
402 foreach my $agent ( @agents ) {
403 next if $typenum{$agent->typenum}++;
404 #fixed in 5.004_05 #$pkgpart->{$_}++ foreach keys %{ $agent->pkgpart_hashref }
405 foreach ( keys %{ $agent->pkgpart_hashref } ) { $pkgpart->{$_}++; } #5.004_04 workaround
410 my @part_pkg = grep { $_->svcpart('svc_acct') && $pkgpart->{ $_->pkgpart } }
411 qsearch( 'part_pkg', { 'disabled' => '' } );
415 # print "<BR><BR>First package", &itable("#cccccc", "0 ALIGN=LEFT"),
416 #apiabuse & undesirable wrapping
417 print "<BR><BR>First package", &itable("#cccccc"),
418 qq!<TR><TD COLSPAN=2><SELECT NAME="pkgpart_svcpart">!;
420 print qq!<OPTION VALUE="">(none)!;
422 foreach my $part_pkg ( @part_pkg ) {
423 print qq!<OPTION VALUE="!,
424 # $part_pkg->pkgpart. "_". $pkgpart{ $part_pkg->pkgpart }, '"';
425 $part_pkg->pkgpart. "_". $part_pkg->svcpart, '"';
426 print " SELECTED" if $saved_pkgpart && ( $part_pkg->pkgpart == $saved_pkgpart );
427 print ">", $part_pkg->pkg, " - ", $part_pkg->comment;
429 print "</SELECT></TD></TR>";
431 #false laziness: (mostly) copied from edit/svc_acct.cgi
432 #$ulen = $svc_acct->dbdef_table->column('username')->length;
433 my $ulen = dbdef->table('svc_acct')->column('username')->length;
435 my $passwordmax = $conf->config('passwordmax') || 8;
436 my $pmax2 = $passwordmax + 2;
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=$pmax2 MAXLENGTH=$passwordmax>
442 (blank to generate)</TD></TR>
445 print '<TR><TD ALIGN="right">Access number</TD><TD WIDTH="100%">'
447 &FS::svc_acct_pop::popselector($popnum).
453 my $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>",