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);
52 my @addl_headers = ();
54 if ( $cgi->param('browse')
55 || $cgi->param('otaker_on')
56 || $cgi->param('agentnum_on')
60 if ( $cgi->param('browse') ) {
61 my $query = $cgi->param('browse');
62 if ( $query eq 'custnum' ) {
63 $sortby=\*custnum_sort;
64 $orderby = "ORDER BY custnum";
65 } elsif ( $query eq 'last' ) {
67 $orderby = "ORDER BY LOWER(last || ' ' || first)";
68 } elsif ( $query eq 'company' ) {
69 $sortby=\*company_sort;
70 $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
71 } elsif ( $query eq 'tickets' ) {
72 $sortby = \*tickets_sort;
73 $orderby = "ORDER BY tickets DESC";
74 push @select, FS::TicketSystem->sql_num_customer_tickets. " as tickets";
75 push @addl_headers, 'Tickets';
76 push @addl_cols, 'tickets';
78 die "unknown browse field $query";
81 $sortby = \*last_sort; #??
82 $orderby = "ORDER BY LOWER(last || ' ' || first)"; #??
83 if ( $cgi->param('otaker_on') ) {
84 $cgi->param('otaker') =~ /^(\w{1,32})$/ or eidiot "Illegal otaker\n";
86 } elsif ( $cgi->param('agentnum_on') ) {
87 $cgi->param('agentnum') =~ /^(\d+)$/ or eidiot "Illegal agentnum\n";
88 $search{agentnum} = $1;
90 die "unknown query...";
98 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
99 || ( $conf->exists('hidecancelledcustomers')
100 && ! $cgi->param('showcancelledcustomers') )
102 #grep { $_->ncancelled_pkgs || ! $_->all_pkgs }
104 ( 0 < ( SELECT COUNT(*) FROM cust_pkg
105 WHERE cust_pkg.custnum = cust_main.custnum
106 AND ( cust_pkg.cancel IS NULL
107 OR cust_pkg.cancel = 0
110 OR 0 = ( SELECT COUNT(*) FROM cust_pkg
111 WHERE cust_pkg.custnum = cust_main.custnum
117 push @qual, FS::cust_main->cancel_sql if $cgi->param('cancelled');
118 push @qual, FS::cust_main->prospect_sql if $cgi->param('prospect');
119 push @qual, FS::cust_main->active_sql if $cgi->param('active');
120 push @qual, FS::cust_main->susp_sql if $cgi->param('suspended');
123 my $qual = join(' AND ',
124 map { "$_ = ". dbh->quote($search{$_}) } keys %search );
126 my $addl_qual = join(' AND ', @qual);
129 $qual .= ' AND ' if $qual;
133 $qual = " WHERE $qual" if $qual;
134 my $statement = "SELECT COUNT(*) FROM cust_main $qual";
135 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
136 $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
138 $total = $sth->fetchrow_arrayref->[0];
142 $addl_qual = " AND $addl_qual";
144 $addl_qual = " WHERE $addl_qual";
150 $select = 'cust_main.*, '. join (', ', @select);
155 @cust_main = qsearch('cust_main', \%search, $select,
156 "$addl_qual $orderby $limit" );
158 # foreach my $cust_main ( @just_cust_main ) {
161 # $FS::Record::DEBUG=1;
162 # ( $cache, @one_cust_main ) = jsearch(
164 # { 'custnum' => $cust_main->custnum },
170 # push @cust_main, @one_cust_main;
175 $sortby = \*last_sort;
177 push @cust_main, @{&custnumsearch}
178 if $cgi->param('custnum_on') && $cgi->param('custnum_text');
179 push @cust_main, @{&cardsearch}
180 if $cgi->param('card_on') && $cgi->param('card');
181 push @cust_main, @{&lastsearch}
182 if $cgi->param('last_on') && $cgi->param('last_text');
183 push @cust_main, @{&companysearch}
184 if $cgi->param('company_on') && $cgi->param('company_text');
185 push @cust_main, @{&address2search}
186 if $cgi->param('address2_on') && $cgi->param('address2_text');
187 push @cust_main, @{&phonesearch}
188 if $cgi->param('phone_on') && $cgi->param('phone_text');
189 push @cust_main, @{&referralsearch}
190 if $cgi->param('referral_custnum');
192 if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
193 $sortby = \*company_sort;
194 push @cust_main, @{&companysearch};
197 @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
198 if ! $cgi->param('cancelled')
200 $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
201 || ( $conf->exists('hidecancelledcustomers')
202 && ! $cgi->param('showcancelledcustomers') )
206 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
210 if ( $conf->exists('hidecancelledpackages' ) ) {
211 %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
213 %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
217 if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
218 if ( $cgi->param('quickpay') eq 'yes' ) {
219 print $cgi->redirect(popurl(2). "edit/cust_pay.cgi?quickpay=yes;custnum=". $cust_main[0]->custnum);
221 print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
224 } elsif ( scalar(@cust_main) == 0 ) {
226 <!-- mason kludge -->
228 eidiot "No matching customers found!\n";
231 <!-- mason kludge -->
234 $total ||= scalar(@cust_main);
235 print header("Customer Search Results",menubar(
236 'Main Menu', popurl(2)
237 )), "$total matching customers found ";
241 if ( $total != scalar(@cust_main) && $maxrecords ) {
242 unless ( $offset == 0 ) {
243 $cgi->param('offset', $offset - $maxrecords);
244 $pager .= '<A HREF="'. $cgi->self_url.
245 '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
249 for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
251 if ( $offset == $poff ) {
252 $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
254 $cgi->param('offset', $poff);
255 $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
258 unless ( $offset + $maxrecords > $total ) {
259 $cgi->param('offset', $offset + $maxrecords);
260 $pager .= '<A HREF="'. $cgi->self_url.
261 '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
266 unless ( $cgi->param('cancelled') ) {
267 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
268 || ( $conf->exists('hidecancelledcustomers')
269 && ! $cgi->param('showcancelledcustomers')
272 $cgi->param('showcancelledcustomers', 1);
273 $cgi->param('offset', 0);
274 print qq!( <a href="!. $cgi->self_url. qq!">show!;
276 $cgi->param('showcancelledcustomers', 0);
277 $cgi->param('offset', 0);
278 print qq!( <a href="!. $cgi->self_url. qq!">hide!;
280 print ' cancelled customers</a> )';
283 if ( $cgi->param('referral_custnum') ) {
284 $cgi->param('referral_custnum') =~ /^(\d+)$/
285 or eidiot "Illegal referral_custnum\n";
286 my $referral_custnum = $1;
287 my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
288 print '<FORM METHOD=POST>'.
289 qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
290 'referrals of <A HREF="'. popurl(2).
291 "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
292 ( $cust_main->company
293 || $cust_main->last. ', '. $cust_main->first ).
297 function changed(what) {
302 print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
303 my $max = 8; #config file
304 $cgi->param('referral_depth') =~ /^(\d*)$/
305 or eidiot "Illegal referral_depth";
306 my $referral_depth = $1;
308 foreach my $depth ( 1 .. $max ) {
310 ' SELECTED'x($depth == $referral_depth),
313 print "</SELECT> levels deep".
314 '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
318 my @custom_priorities = ();
319 if ( $conf->config('ticket_system-custom_priority_field')
320 && @{[ $conf->config('ticket_system-custom_priority_field-values') ]} ) {
322 $conf->config('ticket_system-custom_priority_field-values');
325 print "<BR><BR>". $pager. &table(). <<END;
332 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
334 <TH>(service) name</TH>
339 foreach my $addl_header ( @addl_headers ) {
340 print "<TH>$addl_header</TH>";
345 <TH COLSPAN=2>Services</TH>
351 sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
353 my($custnum,$last,$first,$company)=(
355 $cust_main->getfield('last'),
356 $cust_main->getfield('first'),
361 my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
362 foreach ( @{$all_pkgs{$custnum}} ) {
363 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
364 my @cust_svc = $_->cust_svc;
365 push @lol_cust_svc, \@cust_svc;
366 $rowspan += scalar(@cust_svc) || 1;
369 #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
371 if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
372 $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
374 $view = $p. 'view/cust_main.cgi?'. $custnum;
376 my $pcompany = $company
377 ? qq!<A HREF="$view"><FONT SIZE=-1>$company</FONT></A>!
378 : '<FONT SIZE=-1> </FONT>';
381 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
382 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
383 <TD ROWSPAN=$rowspan>$pcompany</TD>
385 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
386 my($ship_last,$ship_first,$ship_company)=(
387 $cust_main->ship_last || $cust_main->getfield('last'),
388 $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
389 $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
391 my $pship_company = $ship_company
392 ? qq!<A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A>!
393 : '<FONT SIZE=-1> </FONT>';
395 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_last, $ship_first</FONT></A></TD>
396 <TD ROWSPAN=$rowspan>$pship_company</A></TD>
400 foreach my $addl_col ( @addl_cols ) {
401 print "<TD ROWSPAN=$rowspan ALIGN=right>";
402 if ( $addl_col eq 'tickets' ) {
403 if ( @custom_priorities ) {
405 foreach my $priority ( @custom_priorities, '' ) {
408 FS::TicketSystem->href_customer_tickets($custnum,$priority).
410 print "<TR><TD ALIGN=right>$ahref".
411 FS::TicketSystem->num_customer_tickets($custnum,$priority).
413 "<TD ALIGN=left>$ahref".
414 ( $priority || '<i>(none)</i>' ).
417 print '<TR><TD BGCOLOR="#000000" COLSPAN=2> </TD></TR>'.
418 '<TR><TD ALIGN=right>';
422 FS::TicketSystem->href_customer_tickets($custnum).
424 print $ahref. $cust_main->get($addl_col). '</A>';
425 print "</TD><TD ALIGN=left>${ahref}Total</A></TD></TR></TABLE>"
426 if @custom_priorities;
428 print $cust_main->get($addl_col);
434 foreach ( @{$all_pkgs{$custnum}} ) {
435 my $pkgnum = $_->pkgnum;
436 # my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
437 my $part_pkg = $_->part_pkg;
439 my $pkg = $part_pkg->pkg;
440 my $comment = $part_pkg->comment;
441 my $pkgview = "${p}view/cust_main.cgi?$custnum#cust_pkg$pkgnum";
442 my @cust_svc = @{shift @lol_cust_svc};
443 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
444 my $rowspan = scalar(@cust_svc) || 1;
446 print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
448 foreach my $cust_svc ( @cust_svc ) {
449 my($label, $value, $svcdb) = $cust_svc->label;
450 my($svcnum) = $cust_svc->svcnum;
451 my($sview) = $p.'view';
452 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
453 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
456 #print qq!</TR><TR>\n!;
462 print "</TABLE>$pager</BODY></HTML>";
466 #undef $cache; #does this help?
471 lc($a->getfield('last')) cmp lc($b->getfield('last'))
472 || lc($a->first) cmp lc($b->first);
476 return -1 if $a->company && ! $b->company;
477 return 1 if ! $a->company && $b->company;
478 lc($a->company) cmp lc($b->company)
479 || lc($a->getfield('last')) cmp lc($b->getfield('last'))
480 || lc($a->first) cmp lc($b->first);;
484 $a->getfield('custnum') <=> $b->getfield('custnum');
488 $b->getfield('tickets') <=> $a->getfield('tickets');
493 my $custnum = $cgi->param('custnum_text');
495 $custnum =~ /^(\d{1,23})$/ or eidiot "Illegal customer number\n";
498 [ qsearchs('cust_main', { 'custnum' => $custnum } ) ];
503 my($card)=$cgi->param('card');
505 $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
508 [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}),
509 qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'DCRD'})
514 $cgi->param('referral_custnum') =~ /^(\d+)$/
515 or eidiot "Illegal referral_custnum";
516 my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
517 or eidiot "Customer $1 not found";
519 if ( $cgi->param('referral_depth') ) {
520 $cgi->param('referral_depth') =~ /^(\d+)$/
521 or eidiot "Illegal referral_depth";
526 [ $cust_main->referral_cust_main($depth) ];
532 foreach ( $cgi->param('last_type') ) {
536 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
537 or eidiot "Illegal last name";
540 if ( $last_type{'Exact'} || $last_type{'Fuzzy'} ) {
541 push @cust_main, qsearch( 'cust_main',
542 { 'last' => { 'op' => 'ILIKE',
543 'value' => $last } } );
545 push @cust_main, qsearch( 'cust_main',
546 { 'ship_last' => { 'op' => 'ILIKE',
547 'value' => $last } } )
548 if defined dbdef->table('cust_main')->column('ship_last');
551 if ( $last_type{'Substring'} || $last_type{'All'} ) {
553 push @cust_main, qsearch( 'cust_main',
554 { 'last' => { 'op' => 'ILIKE',
555 'value' => "%$last%" } } );
557 push @cust_main, qsearch( 'cust_main',
558 { 'ship_last' => { 'op' => 'ILIKE',
559 'value' => "%$last%" } } )
560 if defined dbdef->table('cust_main')->column('ship_last');
564 if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
565 push @cust_main, FS::cust_main->fuzzy_search( { 'last' => $last } );
568 #if ($last_type{'Sound-alike'}) {
578 foreach ( $cgi->param('company_type') ) {
582 $cgi->param('company_text') =~
583 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
584 or eidiot "Illegal company";
587 if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
588 push @cust_main, qsearch( 'cust_main',
589 { 'company' => { 'op' => 'ILIKE',
590 'value' => $company } } );
592 push @cust_main, qsearch( 'cust_main',
593 { 'ship_company' => { 'op' => 'ILIKE',
594 'value' => $company } } )
595 if defined dbdef->table('cust_main')->column('ship_last');
598 if ( $company_type{'Substring'} || $company_type{'All'} ) {
600 push @cust_main, qsearch( 'cust_main',
601 { 'company' => { 'op' => 'ILIKE',
602 'value' => "%$company%" } } );
604 push @cust_main, qsearch( 'cust_main',
605 { 'ship_company' => { 'op' => 'ILIKE',
606 'value' => "%$company%" } })
607 if defined dbdef->table('cust_main')->column('ship_last');
611 if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
612 push @cust_main, FS::cust_main->fuzzy_search( { 'company' => $company } );
615 if ($company_type{'Sound-alike'}) {
624 $cgi->param('address2_text') =~
625 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
626 or eidiot "Illegal address2";
629 push @cust_main, qsearch( 'cust_main',
630 { 'address2' => { 'op' => 'ILIKE',
631 'value' => $address2 } } );
632 push @cust_main, qsearch( 'cust_main',
633 { 'address2' => { 'op' => 'ILIKE',
634 'value' => $address2 } } )
635 if defined dbdef->table('cust_main')->column('ship_last');
643 my $phone = $cgi->param('phone_text');
645 #(no longer really) false laziness with Record::ut_phonen
646 #only works with US/CA numbers...
648 if ( $phone =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/ ) {
650 $phone .= " x$4" if $4;
651 } elsif ( $phone =~ /^(\d{3})(\d{4})$/ ) {
653 } elsif ( $phone =~ /^(\d{3,4})$/ ) {
656 eidiot gettext('illegal_phone'). ": $phone";
659 my @fields = qw(daytime night fax);
660 push @fields, qw(ship_daytime ship_night ship_fax)
661 if defined dbdef->table('cust_main')->column('ship_last');
663 for my $field ( @fields ) {
664 push @cust_main, qsearch ( 'cust_main',
665 { $field => { 'op' => 'LIKE',
666 'value' => "%$phone%" } } );