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') );
140 if ( $conf->exists('hidecancelledpackages' ) ) {
141 %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
143 %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
147 if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
148 if ( $cgi->param('quickpay') eq 'yes' ) {
149 print $cgi->redirect(popurl(2). "edit/cust_pay.cgi?quickpay=yes;custnum=". $cust_main[0]->custnum);
151 print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
154 } elsif ( scalar(@cust_main) == 0 ) {
156 <!-- mason kludge -->
158 eidiot "No matching customers found!\n";
161 <!-- mason kludge -->
164 $total ||= scalar(@cust_main);
165 print header("Customer Search Results",menubar(
166 'Main Menu', popurl(2)
167 )), "$total matching customers found ";
171 if ( $total != scalar(@cust_main) && $maxrecords ) {
172 unless ( $offset == 0 ) {
173 $cgi->param('offset', $offset - $maxrecords);
174 $pager .= '<A HREF="'. $cgi->self_url.
175 '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
179 for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
181 if ( $offset == $poff ) {
182 $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
184 $cgi->param('offset', $poff);
185 $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
188 unless ( $offset + $maxrecords > $total ) {
189 $cgi->param('offset', $offset + $maxrecords);
190 $pager .= '<A HREF="'. $cgi->self_url.
191 '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
196 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
197 || ( $conf->exists('hidecancelledcustomers')
198 && ! $cgi->param('showcancelledcustomers')
201 $cgi->param('showcancelledcustomers', 1);
202 $cgi->param('offset', 0);
203 print qq!( <a href="!. $cgi->self_url. qq!">show cancelled customers</a> )!;
205 $cgi->param('showcancelledcustomers', 0);
206 $cgi->param('offset', 0);
207 print qq!( <a href="!. $cgi->self_url. qq!">hide cancelled customers</a> )!;
209 if ( $cgi->param('referral_custnum') ) {
210 $cgi->param('referral_custnum') =~ /^(\d+)$/
211 or eidiot "Illegal referral_custnum\n";
212 my $referral_custnum = $1;
213 my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
214 print '<FORM METHOD=POST>'.
215 qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
216 'referrals of <A HREF="'. popurl(2).
217 "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
218 ( $cust_main->company
219 || $cust_main->last. ', '. $cust_main->first ).
223 function changed(what) {
228 print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
229 my $max = 8; #config file
230 $cgi->param('referral_depth') =~ /^(\d*)$/
231 or eidiot "Illegal referral_depth";
232 my $referral_depth = $1;
234 foreach my $depth ( 1 .. $max ) {
236 ' SELECTED'x($depth == $referral_depth),
239 print "</SELECT> levels deep".
240 '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
244 print "<BR><BR>". $pager. &table(). <<END;
251 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
253 <TH>(service) name</TH>
260 <TH COLSPAN=2>Services</TH>
267 sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
269 my($custnum,$last,$first,$company)=(
271 $cust_main->getfield('last'),
272 $cust_main->getfield('first'),
277 my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
278 foreach ( @{$all_pkgs{$custnum}} ) {
279 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
280 my @cust_svc = $_->cust_svc;
281 push @lol_cust_svc, \@cust_svc;
282 $rowspan += scalar(@cust_svc) || 1;
285 #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
287 if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
288 $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
290 $view = $p. 'view/cust_main.cgi?'. $custnum;
294 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
295 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
296 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$company</FONT></A></TD>
298 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
299 my($ship_last,$ship_first,$ship_company)=(
300 $cust_main->ship_last || $cust_main->getfield('last'),
301 $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
302 $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
305 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_last, $ship_first</FONT></A></TD>
306 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A></TD>
311 foreach ( @{$all_pkgs{$custnum}} ) {
312 my $pkgnum = $_->pkgnum;
313 # my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
314 my $part_pkg = $_->part_pkg;
316 my $pkg = $part_pkg->pkg;
317 my $comment = $part_pkg->comment;
318 my $pkgview = $p. 'view/cust_pkg.cgi?'. $pkgnum;
319 my @cust_svc = @{shift @lol_cust_svc};
320 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
321 my $rowspan = scalar(@cust_svc) || 1;
323 print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
325 foreach my $cust_svc ( @cust_svc ) {
326 my($label, $value, $svcdb) = $cust_svc->label;
327 my($svcnum) = $cust_svc->svcnum;
328 my($sview) = $p.'view';
329 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
330 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
333 #print qq!</TR><TR>\n!;
339 print "</TABLE>$pager</BODY></HTML>";
343 #undef $cache; #does this help?
348 $a->getfield('last') cmp $b->getfield('last');
352 return -1 if $a->company && ! $b->company;
353 return 1 if ! $a->company && $b->company;
354 $a->getfield('company') cmp $b->getfield('company');
358 $a->getfield('custnum') <=> $b->getfield('custnum');
363 my($card)=$cgi->param('card');
365 $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
368 [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}) ];
372 $cgi->param('referral_custnum') =~ /^(\d+)$/
373 or eidiot "Illegal referral_custnum";
374 my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
375 or eidiot "Customer $1 not found";
377 if ( $cgi->param('referral_depth') ) {
378 $cgi->param('referral_depth') =~ /^(\d+)$/
379 or eidiot "Illegal referral_depth";
384 [ $cust_main->referral_cust_main($depth) ];
390 foreach ( $cgi->param('last_type') ) {
394 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
395 or eidiot "Illegal last name";
398 # if ( $last_type{'Exact'}
399 # && ! $last_type{'Fuzzy'}
400 # # && ! $last_type{'Sound-alike'}
403 push @cust_main, qsearch('cust_main',{'last'=>$last});
405 push @cust_main, qsearch('cust_main',{'ship_last'=>$last})
406 if defined dbdef->table('cust_main')->column('ship_last');
409 if ( $last_type{'Fuzzy'} ) {
411 &FS::cust_main::check_and_rebuild_fuzzyfiles;
412 my $all_last = &FS::cust_main::all_last;
415 if ($last_type{'Fuzzy'}) {
416 foreach ( amatch($last, [ qw(i) ], @$all_last) ) {
421 #if ($last_type{'Sound-alike'}) {
424 foreach ( keys %last ) {
425 push @cust_main, qsearch('cust_main',{'last'=>$_});
426 push @cust_main, qsearch('cust_main',{'ship_last'=>$_})
427 if defined dbdef->table('cust_main')->column('ship_last');
438 foreach ( $cgi->param('company_type') ) {
442 $cgi->param('company_text') =~ /^([\w \,\.\-\']*)$/
443 or eidiot "Illegal company";
446 # if ( $company_type{'Exact'}
447 # && ! $company_type{'Fuzzy'}
448 # # && ! $company_type{'Sound-alike'}
451 push @cust_main, qsearch('cust_main',{'company'=>$company});
453 push @cust_main, qsearch('cust_main',{'ship_company'=>$company})
454 if defined dbdef->table('cust_main')->column('ship_last');
457 if ( $company_type{'Fuzzy'} ) {
459 &FS::cust_main::check_and_rebuild_fuzzyfiles;
460 my $all_company = &FS::cust_main::all_company;
463 if ($company_type{'Fuzzy'}) {
464 foreach ( amatch($company, [ qw(i) ], @$all_company ) ) {
469 #if ($company_type{'Sound-alike'}) {
472 foreach ( keys %company ) {
473 push @cust_main, qsearch('cust_main',{'company'=>$_});
474 push @cust_main, qsearch('cust_main',{'ship_company'=>$_})
475 if defined dbdef->table('cust_main')->column('ship_last');