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')
61 if ( $cgi->param('browse') ) {
62 my $query = $cgi->param('browse');
63 if ( $query eq 'custnum' ) {
64 $sortby=\*custnum_sort;
65 $orderby = "ORDER BY custnum";
66 } elsif ( $query eq 'last' ) {
68 $orderby = "ORDER BY LOWER(last || ' ' || first)";
69 } elsif ( $query eq 'company' ) {
70 $sortby=\*company_sort;
71 $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
72 } elsif ( $query eq 'tickets' ) {
73 $sortby = \*tickets_sort;
74 $orderby = "ORDER BY tickets DESC";
75 push @select, FS::TicketSystem->sql_num_customer_tickets. " as tickets";
76 push @addl_headers, 'Tickets';
77 push @addl_cols, 'tickets';
79 die "unknown browse field $query";
82 $sortby = \*last_sort; #??
83 $orderby = "ORDER BY LOWER(last || ' ' || first)"; #??
86 if ( $cgi->param('otaker_on') ) {
87 $cgi->param('otaker') =~ /^(\w{1,32})$/ or eidiot "Illegal otaker\n";
89 } elsif ( $cgi->param('agentnum_on') ) {
90 $cgi->param('agentnum') =~ /^(\d+)$/ or eidiot "Illegal agentnum\n";
91 $search{agentnum} = $1;
93 # die "unknown query...";
100 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
101 || ( $conf->exists('hidecancelledcustomers')
102 && ! $cgi->param('showcancelledcustomers') )
104 #grep { $_->ncancelled_pkgs || ! $_->all_pkgs }
105 push @qual, FS::cust_main->uncancel_sql;
109 push @qual, FS::cust_main->cancel_sql if $cgi->param('cancelled');
110 push @qual, FS::cust_main->prospect_sql if $cgi->param('prospect');
111 push @qual, FS::cust_main->active_sql if $cgi->param('active');
112 push @qual, FS::cust_main->susp_sql if $cgi->param('suspended');
115 my $qual = join(' AND ',
116 map { "$_ = ". dbh->quote($search{$_}) } keys %search );
118 my $addl_qual = join(' AND ', @qual);
121 $qual .= ' AND ' if $qual;
125 $qual = " WHERE $qual" if $qual;
126 my $statement = "SELECT COUNT(*) FROM cust_main $qual";
127 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
128 $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
130 $total = $sth->fetchrow_arrayref->[0];
134 $addl_qual = " AND $addl_qual";
136 $addl_qual = " WHERE $addl_qual";
142 $select = 'cust_main.*, '. join (', ', @select);
147 @cust_main = qsearch('cust_main', \%search, $select,
148 "$addl_qual $orderby $limit" );
150 # foreach my $cust_main ( @just_cust_main ) {
153 # $FS::Record::DEBUG=1;
154 # ( $cache, @one_cust_main ) = jsearch(
156 # { 'custnum' => $cust_main->custnum },
162 # push @cust_main, @one_cust_main;
167 $sortby = \*last_sort;
169 push @cust_main, @{&custnumsearch}
170 if $cgi->param('custnum_on') && $cgi->param('custnum_text');
171 push @cust_main, @{&cardsearch}
172 if $cgi->param('card_on') && $cgi->param('card');
173 push @cust_main, @{&lastsearch}
174 if $cgi->param('last_on') && $cgi->param('last_text');
175 push @cust_main, @{&companysearch}
176 if $cgi->param('company_on') && $cgi->param('company_text');
177 push @cust_main, @{&address2search}
178 if $cgi->param('address2_on') && $cgi->param('address2_text');
179 push @cust_main, @{&phonesearch}
180 if $cgi->param('phone_on') && $cgi->param('phone_text');
181 push @cust_main, @{&referralsearch}
182 if $cgi->param('referral_custnum');
184 if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
185 $sortby = \*company_sort;
186 push @cust_main, @{&companysearch};
189 @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
190 if ! $cgi->param('cancelled')
192 $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
193 || ( $conf->exists('hidecancelledcustomers')
194 && ! $cgi->param('showcancelledcustomers') )
198 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
202 if ( $conf->exists('hidecancelledpackages' ) ) {
203 %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
205 %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
209 if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
210 if ( $cgi->param('quickpay') eq 'yes' ) {
211 print $cgi->redirect(popurl(2). "edit/cust_pay.cgi?quickpay=yes;custnum=". $cust_main[0]->custnum);
213 print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
216 } elsif ( scalar(@cust_main) == 0 ) {
218 <!-- mason kludge -->
220 eidiot "No matching customers found!\n";
223 <%= include('/elements/header.html', "Customer Search Results", '' ) %>
225 <% $total ||= scalar(@cust_main); %>
227 <%= $total %> matching customers found
232 if ( $total != scalar(@cust_main) && $maxrecords ) {
233 unless ( $offset == 0 ) {
234 $cgi->param('offset', $offset - $maxrecords);
235 $pager .= '<A HREF="'. $cgi->self_url.
236 '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
240 for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
242 if ( $offset == $poff ) {
243 $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
245 $cgi->param('offset', $poff);
246 $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
249 unless ( $offset + $maxrecords > $total ) {
250 $cgi->param('offset', $offset + $maxrecords);
251 $pager .= '<A HREF="'. $cgi->self_url.
252 '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
257 unless ( $cgi->param('cancelled') ) {
258 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
259 || ( $conf->exists('hidecancelledcustomers')
260 && ! $cgi->param('showcancelledcustomers')
263 $cgi->param('showcancelledcustomers', 1);
264 $cgi->param('offset', 0);
265 print qq!( <a href="!. $cgi->self_url. qq!">show!;
267 $cgi->param('showcancelledcustomers', 0);
268 $cgi->param('offset', 0);
269 print qq!( <a href="!. $cgi->self_url. qq!">hide!;
271 print ' cancelled customers</a> )';
274 if ( $cgi->param('referral_custnum') ) {
275 $cgi->param('referral_custnum') =~ /^(\d+)$/
276 or eidiot "Illegal referral_custnum\n";
277 my $referral_custnum = $1;
278 my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
279 print '<FORM METHOD="GET">'.
280 qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
281 'referrals of <A HREF="'. popurl(2).
282 "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
283 ( $cust_main->company
284 || $cust_main->last. ', '. $cust_main->first ).
288 function changed(what) {
293 print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
294 my $max = 8; #config file
295 $cgi->param('referral_depth') =~ /^(\d*)$/
296 or eidiot "Illegal referral_depth";
297 my $referral_depth = $1;
299 foreach my $depth ( 1 .. $max ) {
301 ' SELECTED'x($depth == $referral_depth),
304 print "</SELECT> levels deep".
305 '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
309 my @custom_priorities = ();
310 if ( $conf->config('ticket_system-custom_priority_field')
311 && @{[ $conf->config('ticket_system-custom_priority_field-values') ]} ) {
313 $conf->config('ticket_system-custom_priority_field-values');
316 print "<BR><BR>". $pager. &table(). <<END;
323 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
325 <TH>(service) name</TH>
330 foreach my $addl_header ( @addl_headers ) {
331 print "<TH>$addl_header</TH>";
336 <TH COLSPAN=2>Services</TH>
342 sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
344 my($custnum,$last,$first,$company)=(
346 $cust_main->getfield('last'),
347 $cust_main->getfield('first'),
352 my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
353 foreach ( @{$all_pkgs{$custnum}} ) {
354 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
355 my @cust_svc = $_->cust_svc;
356 push @lol_cust_svc, \@cust_svc;
357 $rowspan += scalar(@cust_svc) || 1;
360 #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
362 if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
363 $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
365 $view = $p. 'view/cust_main.cgi?'. $custnum;
367 my $pcompany = $company
368 ? qq!<A HREF="$view"><FONT SIZE=-1>$company</FONT></A>!
369 : '<FONT SIZE=-1> </FONT>';
373 <TD ROWSPAN=<%= $rowspan || 1 %>><A HREF="<%= $view %>"><FONT SIZE=-1><%= $custnum %></FONT></A></TD>
374 <TD ROWSPAN=<%= $rowspan || 1 %>><A HREF="<%= $view %>"><FONT SIZE=-1><%= "$last, $first" %></FONT></A></TD>
375 <TD ROWSPAN=<%= $rowspan || 1 %>><%= $pcompany %></TD>
378 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
379 my($ship_last,$ship_first,$ship_company)=(
380 $cust_main->ship_last || $cust_main->getfield('last'),
381 $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
382 $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
384 my $pship_company = $ship_company
385 ? qq!<A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A>!
386 : '<FONT SIZE=-1> </FONT>';
389 <TD ROWSPAN=<%= $rowspan || 1 %>><A HREF="<%= $view %>"><FONT SIZE=-1><%= "$ship_last, $ship_first" %></FONT></A></TD>
390 <TD ROWSPAN=<%= $rowspan || 1 %>><%= $pship_company %></A></TD>
394 foreach my $addl_col ( @addl_cols ) { %>
396 <TD ROWSPAN=<%= $rowspan || 1 %> ALIGN=right><FONT SIZE=-1>
398 <% if ( $addl_col eq 'tickets' ) {
399 if ( @custom_priorities ) {
400 print &itable('', 0);
401 foreach my $priority ( @custom_priorities, '' ) {
404 FS::TicketSystem->num_customer_tickets($custnum,$priority);
407 FS::TicketSystem->href_customer_tickets($custnum,$priority).
412 " <TD ALIGN=right><FONT SIZE=-1>$ahref$num</A></FONT></TD>".
413 "<TD ALIGN=left><FONT SIZE=-1>$ahref".
414 ( $priority || '<i>(none)</i>' ).
415 "</A></FONT></TD></TR>";
418 print '<TR><TD BGCOLOR="#000000" COLSPAN=2></TD></TR>'.
419 '<TR><TD ALIGN=right><FONT SIZE=-1>';
423 $ahref = '<A HREF="'.
424 FS::TicketSystem->href_customer_tickets($custnum).
426 if $cust_main->get($addl_col);
428 print $ahref. $cust_main->get($addl_col). '</A>';
429 print "</FONT></TD><TD ALIGN=left>".
430 "<FONT SIZE=-1>${ahref}Total</A><FONT>".
432 if @custom_priorities;
435 print $cust_main->get($addl_col);
437 print "</FONT></TD>";
441 foreach ( @{$all_pkgs{$custnum}} ) {
442 my $pkgnum = $_->pkgnum;
443 # my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
444 my $part_pkg = $_->part_pkg;
446 my $pkg = $part_pkg->pkg;
447 my $comment = $part_pkg->comment;
448 my $pkgview = "${p}view/cust_main.cgi?$custnum#cust_pkg$pkgnum";
449 my @cust_svc = @{shift @lol_cust_svc};
450 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
451 my $rowspan = scalar(@cust_svc) || 1;
453 print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
455 foreach my $cust_svc ( @cust_svc ) {
456 my($label, $value, $svcdb) = $cust_svc->label;
457 my($svcnum) = $cust_svc->svcnum;
458 my($sview) = $p.'view';
459 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
460 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
463 #print qq!</TR><TR>\n!;
471 </TABLE><%= $pager %>
473 <%= include('/elements/footer.html') %>
477 #undef $cache; #does this help?
482 lc($a->getfield('last')) cmp lc($b->getfield('last'))
483 || lc($a->first) cmp lc($b->first);
487 return -1 if $a->company && ! $b->company;
488 return 1 if ! $a->company && $b->company;
489 lc($a->company) cmp lc($b->company)
490 || lc($a->getfield('last')) cmp lc($b->getfield('last'))
491 || lc($a->first) cmp lc($b->first);;
495 $a->getfield('custnum') <=> $b->getfield('custnum');
499 $b->getfield('tickets') <=> $a->getfield('tickets');
504 my $custnum = $cgi->param('custnum_text');
506 $custnum =~ /^(\d{1,23})$/ or eidiot "Illegal customer number\n";
509 [ qsearchs('cust_main', { 'custnum' => $custnum } ) ];
514 my($card)=$cgi->param('card');
516 $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
519 [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}),
520 qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'DCRD'})
525 $cgi->param('referral_custnum') =~ /^(\d+)$/
526 or eidiot "Illegal referral_custnum";
527 my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
528 or eidiot "Customer $1 not found";
530 if ( $cgi->param('referral_depth') ) {
531 $cgi->param('referral_depth') =~ /^(\d+)$/
532 or eidiot "Illegal referral_depth";
537 [ $cust_main->referral_cust_main($depth) ];
543 foreach ( $cgi->param('last_type') ) {
547 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
548 or eidiot "Illegal last name";
551 if ( $last_type{'Exact'} || $last_type{'Fuzzy'} ) {
552 push @cust_main, qsearch( 'cust_main',
553 { 'last' => { 'op' => 'ILIKE',
554 'value' => $last } } );
556 push @cust_main, qsearch( 'cust_main',
557 { 'ship_last' => { 'op' => 'ILIKE',
558 'value' => $last } } )
559 if defined dbdef->table('cust_main')->column('ship_last');
562 if ( $last_type{'Substring'} || $last_type{'All'} ) {
564 push @cust_main, qsearch( 'cust_main',
565 { 'last' => { 'op' => 'ILIKE',
566 'value' => "%$last%" } } );
568 push @cust_main, qsearch( 'cust_main',
569 { 'ship_last' => { 'op' => 'ILIKE',
570 'value' => "%$last%" } } )
571 if defined dbdef->table('cust_main')->column('ship_last');
575 if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
576 push @cust_main, FS::cust_main->fuzzy_search( { 'last' => $last } );
579 #if ($last_type{'Sound-alike'}) {
589 foreach ( $cgi->param('company_type') ) {
593 $cgi->param('company_text') =~
594 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
595 or eidiot "Illegal company";
598 if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
599 push @cust_main, qsearch( 'cust_main',
600 { 'company' => { 'op' => 'ILIKE',
601 'value' => $company } } );
603 push @cust_main, qsearch( 'cust_main',
604 { 'ship_company' => { 'op' => 'ILIKE',
605 'value' => $company } } )
606 if defined dbdef->table('cust_main')->column('ship_last');
609 if ( $company_type{'Substring'} || $company_type{'All'} ) {
611 push @cust_main, qsearch( 'cust_main',
612 { 'company' => { 'op' => 'ILIKE',
613 'value' => "%$company%" } } );
615 push @cust_main, qsearch( 'cust_main',
616 { 'ship_company' => { 'op' => 'ILIKE',
617 'value' => "%$company%" } })
618 if defined dbdef->table('cust_main')->column('ship_last');
622 if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
623 push @cust_main, FS::cust_main->fuzzy_search( { 'company' => $company } );
626 if ($company_type{'Sound-alike'}) {
635 $cgi->param('address2_text') =~
636 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
637 or eidiot "Illegal address2";
640 push @cust_main, qsearch( 'cust_main',
641 { 'address2' => { 'op' => 'ILIKE',
642 'value' => $address2 } } );
643 push @cust_main, qsearch( 'cust_main',
644 { 'address2' => { 'op' => 'ILIKE',
645 'value' => $address2 } } )
646 if defined dbdef->table('cust_main')->column('ship_last');
654 my $phone = $cgi->param('phone_text');
656 #(no longer really) false laziness with Record::ut_phonen
657 #only works with US/CA numbers...
659 if ( $phone =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/ ) {
661 $phone .= " x$4" if $4;
662 } elsif ( $phone =~ /^(\d{3})(\d{4})$/ ) {
664 } elsif ( $phone =~ /^(\d{3,4})$/ ) {
667 eidiot gettext('illegal_phone'). ": $phone";
670 my @fields = qw(daytime night fax);
671 push @fields, qw(ship_daytime ship_night ship_fax)
672 if defined dbdef->table('cust_main')->column('ship_last');
674 for my $field ( @fields ) {
675 push @cust_main, qsearch ( 'cust_main',
676 { $field => { 'op' => 'LIKE',
677 'value' => "%$phone%" } } );