3 my $conf = new FS::Conf;
4 my $maxrecords = $conf->config('maxsearchrecordsperpage');
8 #my $monsterjoin = <<END;
9 #cust_main left outer join (
10 # ( cust_pkg left outer join part_pkg using(pkgpart)
14 # ( cust_svc left outer join part_svc using (svcpart)
15 # ) left outer join svc_acct using (svcnum)
16 # ) left outer join svc_domain using(svcnum)
17 # ) left outer join svc_forward using(svcnum)
22 #my $monsterjoin = <<END;
23 #cust_main left outer join (
24 # ( cust_pkg left outer join part_pkg using(pkgpart)
28 # ( cust_svc left outer join part_svc using (svcpart)
30 # svc_acct left outer join (
31 # select svcnum, domain, catchall from svc_domain
32 # ) as svc_acct_domsvc (
33 # svc_acct_svcnum, svc_acct_domain, svc_acct_catchall
34 # ) on svc_acct.domsvc = svc_acct_domsvc.svc_acct_svcnum
36 # ) left outer join svc_domain using(svcnum)
37 # ) left outer join svc_forward using(svcnum)
43 $limit .= "LIMIT $maxrecords" if $maxrecords;
45 my $offset = $cgi->param('offset') || 0;
46 $limit .= " OFFSET $offset" if $offset;
50 my(@cust_main, $sortby, $orderby);
51 if ( $cgi->param('browse') ) {
52 my $query = $cgi->param('browse');
53 if ( $query eq 'custnum' ) {
54 $sortby=\*custnum_sort;
55 $orderby = 'ORDER BY custnum';
56 } elsif ( $query eq 'last' ) {
58 $orderby = 'ORDER BY last';
59 } elsif ( $query eq 'company' ) {
60 $sortby=\*company_sort;
61 $orderby = 'ORDER BY company';
63 die "unknown browse field $query";
68 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
69 || ( $conf->exists('hidecancelledcustomers')
70 && ! $cgi->param('showcancelledcustomers') )
72 #grep { $_->ncancelled_pkgs || ! $_->all_pkgs }
73 #needed for MySQL??? OR cust_pkg.cancel = \"\"
75 WHERE 0 < ( SELECT COUNT(*) FROM cust_pkg
76 WHERE cust_pkg.custnum = cust_main.custnum
77 AND ( cust_pkg.cancel IS NULL
78 OR cust_pkg.cancel = 0
81 OR 0 = ( SELECT COUNT(*) FROM cust_pkg
82 WHERE cust_pkg.custnum = cust_main.custnum
87 my $statement = "SELECT COUNT(*) FROM cust_main $ncancelled";
88 my $sth = dbh->prepare($statement)
89 or die dbh->errstr. " doing $statement";
90 $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
92 $total = $sth->fetchrow_arrayref->[0];
94 my @just_cust_main = qsearch('cust_main',{}, '',
95 "$ncancelled $orderby $limit"
98 @cust_main = @just_cust_main;
100 # foreach my $cust_main ( @just_cust_main ) {
103 # $FS::Record::DEBUG=1;
104 # ( $cache, @one_cust_main ) = jsearch(
106 # { 'custnum' => $cust_main->custnum },
112 # push @cust_main, @one_cust_main;
117 $sortby = \*last_sort;
119 push @cust_main, @{&cardsearch}
120 if $cgi->param('card_on') && $cgi->param('card');
121 push @cust_main, @{&lastsearch}
122 if $cgi->param('last_on') && $cgi->param('last_text');
123 push @cust_main, @{&companysearch}
124 if $cgi->param('company_on') && $cgi->param('company_text');
125 push @cust_main, @{&referralsearch}
126 if $cgi->param('referral_custnum');
128 if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
129 $sortby = \*company_sort;
130 push @cust_main, @{&companysearch};
133 @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
134 if $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
135 || ( $conf->exists('hidecancelledcustomers')
136 && ! $cgi->param('showcancelledcustomers') );
139 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
143 if ( $conf->exists('hidecancelledpackages' ) ) {
144 %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
146 %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
150 if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
151 if ( $cgi->param('quickpay') eq 'yes' ) {
152 print $cgi->redirect(popurl(2). "edit/cust_pay.cgi?quickpay=yes;custnum=". $cust_main[0]->custnum);
154 print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
157 } elsif ( scalar(@cust_main) == 0 ) {
159 <!-- mason kludge -->
161 eidiot "No matching customers found!\n";
164 <!-- mason kludge -->
167 $total ||= scalar(@cust_main);
168 print header("Customer Search Results",menubar(
169 'Main Menu', popurl(2)
170 )), "$total matching customers found ";
174 if ( $total != scalar(@cust_main) && $maxrecords ) {
175 unless ( $offset == 0 ) {
176 $cgi->param('offset', $offset - $maxrecords);
177 $pager .= '<A HREF="'. $cgi->self_url.
178 '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
182 for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
184 if ( $offset == $poff ) {
185 $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
187 $cgi->param('offset', $poff);
188 $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
191 unless ( $offset + $maxrecords > $total ) {
192 $cgi->param('offset', $offset + $maxrecords);
193 $pager .= '<A HREF="'. $cgi->self_url.
194 '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
199 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
200 || ( $conf->exists('hidecancelledcustomers')
201 && ! $cgi->param('showcancelledcustomers')
204 $cgi->param('showcancelledcustomers', 1);
205 $cgi->param('offset', 0);
206 print qq!( <a href="!. $cgi->self_url. qq!">show cancelled customers</a> )!;
208 $cgi->param('showcancelledcustomers', 0);
209 $cgi->param('offset', 0);
210 print qq!( <a href="!. $cgi->self_url. qq!">hide cancelled customers</a> )!;
212 if ( $cgi->param('referral_custnum') ) {
213 $cgi->param('referral_custnum') =~ /^(\d+)$/
214 or eidiot "Illegal referral_custnum\n";
215 my $referral_custnum = $1;
216 my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
217 print '<FORM METHOD=POST>'.
218 qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
219 'referrals of <A HREF="'. popurl(2).
220 "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
221 ( $cust_main->company
222 || $cust_main->last. ', '. $cust_main->first ).
226 function changed(what) {
231 print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
232 my $max = 8; #config file
233 $cgi->param('referral_depth') =~ /^(\d*)$/
234 or eidiot "Illegal referral_depth";
235 my $referral_depth = $1;
237 foreach my $depth ( 1 .. $max ) {
239 ' SELECTED'x($depth == $referral_depth),
242 print "</SELECT> levels deep".
243 '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
247 print "<BR><BR>". $pager. &table(). <<END;
254 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
256 <TH>(service) name</TH>
263 <TH COLSPAN=2>Services</TH>
270 sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
272 my($custnum,$last,$first,$company)=(
274 $cust_main->getfield('last'),
275 $cust_main->getfield('first'),
280 my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
281 foreach ( @{$all_pkgs{$custnum}} ) {
282 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
283 my @cust_svc = $_->cust_svc;
284 push @lol_cust_svc, \@cust_svc;
285 $rowspan += scalar(@cust_svc) || 1;
288 #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
290 if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
291 $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
293 $view = $p. 'view/cust_main.cgi?'. $custnum;
297 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
298 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
299 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$company</FONT></A></TD>
301 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
302 my($ship_last,$ship_first,$ship_company)=(
303 $cust_main->ship_last || $cust_main->getfield('last'),
304 $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
305 $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
308 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_last, $ship_first</FONT></A></TD>
309 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A></TD>
314 foreach ( @{$all_pkgs{$custnum}} ) {
315 my $pkgnum = $_->pkgnum;
316 # my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
317 my $part_pkg = $_->part_pkg;
319 my $pkg = $part_pkg->pkg;
320 my $comment = $part_pkg->comment;
321 my $pkgview = $p. 'view/cust_pkg.cgi?'. $pkgnum;
322 my @cust_svc = @{shift @lol_cust_svc};
323 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
324 my $rowspan = scalar(@cust_svc) || 1;
326 print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
328 foreach my $cust_svc ( @cust_svc ) {
329 my($label, $value, $svcdb) = $cust_svc->label;
330 my($svcnum) = $cust_svc->svcnum;
331 my($sview) = $p.'view';
332 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
333 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
336 #print qq!</TR><TR>\n!;
342 print "</TABLE>$pager</BODY></HTML>";
346 #undef $cache; #does this help?
351 $a->getfield('last') cmp $b->getfield('last');
355 return -1 if $a->company && ! $b->company;
356 return 1 if ! $a->company && $b->company;
357 $a->getfield('company') cmp $b->getfield('company');
361 $a->getfield('custnum') <=> $b->getfield('custnum');
366 my($card)=$cgi->param('card');
368 $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
371 [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}) ];
375 $cgi->param('referral_custnum') =~ /^(\d+)$/
376 or eidiot "Illegal referral_custnum";
377 my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
378 or eidiot "Customer $1 not found";
380 if ( $cgi->param('referral_depth') ) {
381 $cgi->param('referral_depth') =~ /^(\d+)$/
382 or eidiot "Illegal referral_depth";
387 [ $cust_main->referral_cust_main($depth) ];
393 foreach ( $cgi->param('last_type') ) {
397 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
398 or eidiot "Illegal last name";
401 # if ( $last_type{'Exact'}
402 # && ! $last_type{'Fuzzy'}
403 # # && ! $last_type{'Sound-alike'}
406 push @cust_main, qsearch('cust_main',{'last'=>$last});
408 push @cust_main, qsearch('cust_main',{'ship_last'=>$last})
409 if defined dbdef->table('cust_main')->column('ship_last');
412 if ( $last_type{'Fuzzy'} ) {
414 &FS::cust_main::check_and_rebuild_fuzzyfiles;
415 my $all_last = &FS::cust_main::all_last;
418 if ($last_type{'Fuzzy'}) {
419 foreach ( amatch($last, [ qw(i) ], @$all_last) ) {
424 #if ($last_type{'Sound-alike'}) {
427 foreach ( keys %last ) {
428 push @cust_main, qsearch('cust_main',{'last'=>$_});
429 push @cust_main, qsearch('cust_main',{'ship_last'=>$_})
430 if defined dbdef->table('cust_main')->column('ship_last');
441 foreach ( $cgi->param('company_type') ) {
445 $cgi->param('company_text') =~ /^([\w \,\.\-\']*)$/
446 or eidiot "Illegal company";
449 # if ( $company_type{'Exact'}
450 # && ! $company_type{'Fuzzy'}
451 # # && ! $company_type{'Sound-alike'}
454 push @cust_main, qsearch('cust_main',{'company'=>$company});
456 push @cust_main, qsearch('cust_main',{'ship_company'=>$company})
457 if defined dbdef->table('cust_main')->column('ship_last');
460 if ( $company_type{'Fuzzy'} ) {
462 &FS::cust_main::check_and_rebuild_fuzzyfiles;
463 my $all_company = &FS::cust_main::all_company;
466 if ($company_type{'Fuzzy'}) {
467 foreach ( amatch($company, [ qw(i) ], @$all_company ) ) {
472 #if ($company_type{'Sound-alike'}) {
475 foreach ( keys %company ) {
476 push @cust_main, qsearch('cust_main',{'company'=>$_});
477 push @cust_main, qsearch('cust_main',{'ship_company'=>$_})
478 if defined dbdef->table('cust_main')->column('ship_last');