1 % if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
2 % if ( $cgi->param('quickpay') eq 'yes' ) {
3 <% $cgi->redirect(popurl(2). "edit/cust_pay.cgi?quickpay=yes;custnum=". $cust_main[0]->custnum) %>
5 <% $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum) %>
7 %} elsif ( scalar(@cust_main) == 0 ) {
8 % errorpage(emt("No matching customers found!"));
9 % } # errorpage quits, so we don't need an 'else' below
11 <& /elements/header.html, mt("Customer Search Results"), '' &>
12 % $total ||= scalar(@cust_main);
14 <% mt("[_1] matching customers found",$total) |h %>
16 % my $pager = include( '/elements/pager.html',
17 % 'offset' => $offset,
18 % 'num_rows' => scalar(@cust_main),
20 % 'maxrecords' => $maxrecords,
23 % unless ( $cgi->param('cancelled') ) {
25 % if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
26 % || ( $conf->exists('hidecancelledcustomers')
27 % && ! $cgi->param('showcancelledcustomers')
30 % $cgi->param('showcancelledcustomers', 1);
31 % $linklabel = 'show';
33 % $cgi->param('showcancelledcustomers', 0);
34 % $linklabel = 'hide';
36 % $cgi->param('offset', 0);
37 ( <a href="<% $cgi->self_url %>"><% mt("$linklabel canceled customers") |h %></a> )
40 % if ( $cgi->param('referral_custnum') ) {
41 % $cgi->param('referral_custnum') =~ /^(\d+)$/
42 % or errorpage(emt("Illegal referral_custnum"));
43 % my $referral_custnum = $1;
44 % my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
46 function changed(what) {
51 % # XXX: translate this section...hard due to "referrals of CUST SELECT levels deep" construction
53 <INPUT TYPE="hidden" NAME="referral_custnum" VALUE="<% $referral_custnum %>">
54 % my $refcustlabel = "$referral_custnum: " .
55 % ( $cust_main->company || $cust_main->last. ', '. $cust_main->first );
57 <A HREF="<% popurl(2)."view/cust_main.cgi?$referral_custnum" %>"><% $refcustlabel |h %></A>
58 <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
61 % $cgi->param('referral_depth') =~ /^(\d*)$/
62 % or errorpage(emt("Illegal referral_depth"));
63 % my $referral_depth = $1;
65 % foreach my $depth ( 1 .. $max ) {
66 % my $selected = ($depth == $referral_depth) ? 'SELECTED' : '';
67 <OPTION <% $selected %>><% $depth %>
73 % my @custom_priorities = ();
74 % if ( $conf->config('ticket_system-custom_priority_field')
75 % && @{[ $conf->config('ticket_system-custom_priority_field-values') ]} ) {
76 % @custom_priorities =
77 % $conf->config('ticket_system-custom_priority_field-values');
80 <BR><BR><% $pager.include('/elements/table-grid.html') %>
82 <TH CLASS="grid" BGCOLOR="#cccccc"><% mt('#') |h %></TH>
83 <TH CLASS="grid" BGCOLOR="#cccccc"><% mt('Status') |h %></TH>
84 <TH CLASS="grid" BGCOLOR="#cccccc"><% mt('Name') |h %></TH>
85 <TH CLASS="grid" BGCOLOR="#cccccc"><% mt('Company') |h %></TH>
87 %foreach my $addl_header ( @addl_headers ) {
88 <TH CLASS="grid" BGCOLOR="#cccccc"><% $addl_header %></TH>
91 <TH CLASS="grid" BGCOLOR="#cccccc"><% mt('Packages') |h %></TH>
92 <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN="2"><% mt('Services') |h %></TH>
95 % my $bgcolor1 = '#eeeeee';
96 % my $bgcolor2 = '#ffffff';
99 % my(%saw,$cust_main);
100 % foreach $cust_main (
101 % sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
104 % if ( $bgcolor eq $bgcolor1 ) {
105 % $bgcolor = $bgcolor2;
107 % $bgcolor = $bgcolor1;
110 % my($custnum,$last,$first,$company)=(
111 % $cust_main->custnum,
112 % $cust_main->getfield('last'),
113 % $cust_main->getfield('first'),
114 % $cust_main->company,
119 % foreach my $cust_pkg ( @{$all_pkgs{$custnum}} ) {
120 % my %cust_svc_by_svcpart;
122 % foreach my $part_svc ( $cust_pkg->part_svc ) {
123 % my $svcpart = $part_svc->svcpart;
124 % my $num_cust_svc = $cust_pkg->num_cust_svc($svcpart);
125 % if ( $large_pkg_size > 0 and $num_cust_svc >= $large_pkg_size ) {
126 % # don't retrieve the cust_svc records, just stash the
127 % # part_svc and num_cust_svc for later
128 % $cust_svc_by_svcpart{$svcpart} =
129 % [ 'summarize', $part_svc, $num_cust_svc ];
132 % elsif ( $num_cust_svc ) {
133 % $cust_svc_by_svcpart{$svcpart} = [ $cust_pkg->cust_svc($svcpart) ];
134 % $rows += $num_cust_svc;
136 % } #foreach $part_svc
137 % $rows ||= 1; # in case the package has no services
138 % push @all_cust_svc, \%cust_svc_by_svcpart;
139 % push @pkg_rowspans, $rows;
140 % } #foreach $cust_pkg
141 % my $rowspan = List::Util::sum(@pkg_rowspans) || 1;
144 % if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
145 % $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
147 % $view = $p. 'view/cust_main.cgi?'. $custnum;
149 % my $pcompany = $company
150 % ? qq!<A HREF="$view"><FONT SIZE=-1>!. encode_entities($company). '</FONT></A>'
151 % : '<FONT SIZE=-1> </FONT>';
153 % my $status = $cust_main->status;
154 % my $statuscol = $cust_main->statuscolor;
157 <TD CLASS="grid" ALIGN="right" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
158 <A HREF="<% $view %>"><FONT SIZE=-1><% $cust_main->display_custnum %></FONT></A>
160 <TD CLASS="grid" ALIGN="center" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
161 <FONT SIZE="-1" COLOR="#<% $statuscol %>"><B><% ucfirst($status) %></B></FONT>
163 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
164 <A HREF="<% $view %>"><FONT SIZE=-1><% "$last, $first" |h %></FONT></A>
166 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
170 % foreach my $addl_col ( @addl_cols ) {
171 % if ( $addl_col eq 'tickets' ) {
172 % if ( @custom_priorities ) {
174 <TD CLASS="inv" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %> ALIGN=right>
176 <TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>
177 % foreach my $priority ( @custom_priorities, '' ) {
179 % FS::TicketSystem->num_customer_tickets($custnum,$priority);
181 % $ahref= '<A HREF="'.
182 % FS::TicketSystem->href_customer_tickets($custnum,$priority).
188 <FONT SIZE=-1><% $ahref.$num %></A></FONT>
191 <FONT SIZE=-1><% $ahref %><% $priority || '<i>('.emt('none').')</i>' %></A></FONT>
197 <TH ALIGN=right STYLE="border-top: dashed 1px black">
200 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %> ALIGN=right>
204 % $ahref = '<A HREF="'.
205 % FS::TicketSystem->href_customer_tickets($custnum).
207 % if $cust_main->get($addl_col);
210 <% $ahref %><% $cust_main->get($addl_col) %></A>
211 % if ( @custom_priorities ) {
214 <TH ALIGN=left STYLE="border-top: dashed 1px black">
215 <FONT SIZE=-1><% ${ahref} %>Total</A><FONT>
224 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %> ALIGN=right><FONT SIZE=-1>
225 <% $cust_main->get($addl_col) %>
232 % foreach ( @{$all_pkgs{$custnum}} ) {
233 % my $pkgnum = $_->pkgnum;
234 % my $part_pkg = $_->part_pkg;
236 % my $pkg_comment = $part_pkg->pkg_comment(nopkgpart => 1);
237 % my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
239 % : ';show=packages';
240 % my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
241 % my $pkgview = "${p}view/cust_main.cgi?custnum=$custnum$show;fragment=$frag#$frag";
242 % # cust_svc stuff, built earlier
243 % my %cust_svc_by_svcpart = %{ shift @all_cust_svc };
244 % my $pkg_rowspan = shift @pkg_rowspans;
246 <% $n1 %><TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN="<% $pkg_rowspan%>">
247 <A HREF="<% $pkgview %>"><FONT SIZE=-1><% $pkg_comment |h %></FONT></A>
251 % my $td = '<TD CLASS="grid" BGCOLOR="'.$bgcolor.'">';
253 % foreach my $svcpart ( sort keys %cust_svc_by_svcpart ) { #sort order?
254 % my $these = $cust_svc_by_svcpart{$svcpart};
255 % if ( $these->[0] eq 'summarize' ) {
256 % my $part_svc = $these->[1];
257 % my $num_cust_svc = $these->[2];
260 % # link opens a new search for this pkgnum/svcpart combo
261 % my $href = $p.'search/cust_pkg_svc.html?svcpart='.$svcpart.
262 % ';pkgnum='.$pkgnum;
264 <A HREF="<% $href %>"><% $part_svc->svc %></A>
267 <A HREF="<% $href %>"><B>(<% mt("view all [_1]", $num_cust_svc) |h %>)</B></A>
271 <% $td %><& /elements/search-cust_svc.html,
272 'svcpart' => $svcpart,
274 'svcdb' => $part_svc->svcdb,
278 % elsif ( scalar @$these ) { # do not summarize
279 % foreach my $cust_svc ( @$these ) {
282 <% FS::UI::Web::svc_link($m, $cust_svc->part_svc, $cust_svc) %>
285 <% FS::UI::Web::svc_label_link($m, $cust_svc->part_svc, $cust_svc) %>
288 % } #foreach $cust_svc
290 % } # foreach $svcpart
292 % unless ( %cust_svc_by_svcpart ) {
293 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" COLSPAN=2> </TD>
299 % unless ( @{$all_pkgs{$custnum}} ) {
300 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" COLSPAN=3> </TD>
308 <& /elements/footer.html &>
310 my $curuser = $FS::CurrentUser::CurrentUser;
313 unless $curuser->access_right('List all customers');
315 my $conf = new FS::Conf;
316 my $maxrecords = $conf->config('maxsearchrecordsperpage');
317 # summarize more than this many services of the same svcpart
318 my $large_pkg_size = $conf->config('cust_pkg-large_pkg_size') || 0;
321 $limit .= "LIMIT $maxrecords" if $maxrecords;
323 my $offset = $cgi->param('offset') || 0;
324 $limit .= " OFFSET $offset" if $offset;
328 my(@cust_main, $sortby, $orderby);
330 my @addl_headers = ();
332 if ( $cgi->param('browse')
333 || $cgi->param('otaker_on')
334 || $cgi->param('agentnum_on')
339 if ( $cgi->param('browse') ) {
340 my $query = $cgi->param('browse');
341 if ( $query eq 'custnum' ) {
342 if ( $conf->exists('cust_main-default_agent_custid') ) {
343 $sortby=\*display_custnum_sort;
344 $orderby = "ORDER BY CASE WHEN agent_custid IS NOT NULL AND agent_custid != '' THEN CAST(agent_custid AS BIGINT) ELSE custnum END";
346 $sortby=\*custnum_sort;
347 $orderby = "ORDER BY custnum";
349 } elsif ( $query eq 'last' ) {
351 $orderby = "ORDER BY LOWER(last || ' ' || first)";
352 } elsif ( $query eq 'company' ) {
353 $sortby=\*company_sort;
354 $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
355 } elsif ( $query eq 'tickets' ) {
356 $sortby = \*tickets_sort;
357 $orderby = "ORDER BY tickets DESC";
358 push @select, FS::TicketSystem->sql_num_customer_tickets. " as tickets";
359 push @addl_headers, 'Tickets';
360 push @addl_cols, 'tickets';
361 } elsif ( $query eq 'uspsunvalid' ) {
362 $search{'country'} = 'US';
363 $sortby=\*custnum_sort;
364 $orderby = "ORDER BY custnum";
366 die "unknown browse field $query";
369 $sortby = \*last_sort; #??
370 $orderby = "ORDER BY LOWER(last || ' ' || first)"; #??
373 if ( $cgi->param('otaker_on') ) {
375 unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
376 $cgi->param('otaker') =~ /^(\w{1,32})$/ or errorpage("Illegal otaker");
377 $search{otaker} = $1;
378 } elsif ( $cgi->param('agentnum_on') ) {
379 $cgi->param('agentnum') =~ /^(\d+)$/ or errorpage("Illegal agentnum");
380 $search{agentnum} = $1;
387 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
388 || ( $conf->exists('hidecancelledcustomers')
389 && ! $cgi->param('showcancelledcustomers') )
391 push @qual, FS::cust_main->uncancel_sql;
394 push @qual, FS::cust_main->cancel_sql if $cgi->param('cancelled');
395 push @qual, FS::cust_main->prospect_sql if $cgi->param('prospect');
396 push @qual, FS::cust_main->active_sql if $cgi->param('active');
397 push @qual, FS::cust_main->inactive_sql if $cgi->param('inactive');
398 push @qual, FS::cust_main->susp_sql if $cgi->param('suspended');
401 my $qual = join(' AND ',
402 map { "$_ = ". dbh->quote($search{$_}) } keys %search );
404 my $addl_qual = join(' AND ', @qual);
406 #here is the agent virtualization
407 $addl_qual .= ( $addl_qual ? ' AND ' : '' ).
408 $FS::CurrentUser::CurrentUser->agentnums_sql;
410 if ( $cgi->param('browse') && $cgi->param('browse') eq 'uspsunvalid' ) {
411 $addl_qual .= ' AND ( length(zip) < 9 OR upper(address1) != address1 OR upper(city) != city ) ';
415 $qual .= ' AND ' if $qual;
419 $qual = " WHERE $qual" if $qual;
420 my $statement = "SELECT COUNT(*) FROM cust_main $qual";
421 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
422 $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
424 $total = $sth->fetchrow_arrayref->[0];
428 $addl_qual = " AND $addl_qual";
430 $addl_qual = " WHERE $addl_qual";
436 $select = 'cust_main.*, '. join (', ', @select);
441 @cust_main = qsearch('cust_main', \%search, $select,
442 "$addl_qual $orderby $limit" );
446 $sortby = \*last_sort;
448 push @cust_main, @{&custnumsearch}
449 if $cgi->param('custnum_on') && $cgi->param('custnum_text');
450 push @cust_main, @{&cardsearch}
451 if $cgi->param('card_on') && $cgi->param('card');
452 push @cust_main, @{&lastsearch}
453 if $cgi->param('last_on') && $cgi->param('last_text');
454 push @cust_main, @{&companysearch}
455 if $cgi->param('company_on') && $cgi->param('company_text');
456 push @cust_main, @{&address2search}
457 if $cgi->param('address2_on') && $cgi->param('address2_text');
458 push @cust_main, @{&phonesearch}
459 if $cgi->param('phone_on') && $cgi->param('phone_text');
460 push @cust_main, @{&referralsearch}
461 if $cgi->param('referral_custnum');
463 if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
464 $sortby = \*company_sort;
465 push @cust_main, @{&companysearch};
468 if ( $cgi->param('search_cust') ) {
469 $sortby = \*company_sort;
470 $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
471 push @cust_main, smart_search(
472 'search' => scalar($cgi->param('search_cust')),
473 'no_fuzzy_on_exact' => ! ( $curuser->option('enable_fuzzy_on_exact')
474 || $conf->exists('enable_fuzzy_on_exact')
479 @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
480 if ! $cgi->param('cancelled')
482 $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
483 || ( $conf->exists('hidecancelledcustomers')
484 && ! $cgi->param('showcancelledcustomers') )
488 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
492 if ( $conf->exists('hidecancelledpackages' ) ) {
493 %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
495 %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
499 lc($a->getfield('last')) cmp lc($b->getfield('last'))
500 || lc($a->first) cmp lc($b->first);
504 return -1 if $a->company && ! $b->company;
505 return 1 if ! $a->company && $b->company;
506 lc($a->company) cmp lc($b->company)
507 || lc($a->getfield('last')) cmp lc($b->getfield('last'))
508 || lc($a->first) cmp lc($b->first);;
511 sub display_custnum_sort {
512 $a->display_custnum <=> $b->display_custnum;
516 $a->getfield('custnum') <=> $b->getfield('custnum');
520 $b->getfield('tickets') <=> $a->getfield('tickets');
525 my $custnum = $cgi->param('custnum_text');
527 $custnum =~ /^(\d{1,23})$/ or errorpage(emt("Illegal customer number"));
530 [ qsearchs('cust_main', { 'custnum' => $custnum } ) ];
535 my($card)=$cgi->param('card');
537 $card =~ /^(\d{13,16}|\d{8,9})$/ or errorpage(emt("Illegal card number"));
540 [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}),
541 qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'DCRD'})
546 $cgi->param('referral_custnum') =~ /^(\d+)$/
547 or errorpage("Illegal referral_custnum");
548 my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
549 or errorpage(emt("Customer [_1] not found",$1));
551 if ( $cgi->param('referral_depth') ) {
552 $cgi->param('referral_depth') =~ /^(\d+)$/
553 or errorpage(emt("Illegal referral_depth"));
558 [ $cust_main->referral_cust_main($depth) ];
564 foreach ( $cgi->param('last_type') ) {
568 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
569 or errorpage(emt("Illegal last name"));
572 if ( $last_type{'Exact'} || $last_type{'Fuzzy'} ) {
573 push @cust_main, qsearch( 'cust_main',
574 { 'last' => { 'op' => 'ILIKE',
575 'value' => $last } } );
577 push @cust_main, qsearch( 'cust_main',
578 { 'ship_last' => { 'op' => 'ILIKE',
579 'value' => $last } } )
580 if defined dbdef->table('cust_main')->column('ship_last');
583 if ( $last_type{'Substring'} || $last_type{'All'} ) {
585 push @cust_main, qsearch( 'cust_main',
586 { 'last' => { 'op' => 'ILIKE',
587 'value' => "%$last%" } } );
589 push @cust_main, qsearch( 'cust_main',
590 { 'ship_last' => { 'op' => 'ILIKE',
591 'value' => "%$last%" } } )
592 if defined dbdef->table('cust_main')->column('ship_last');
596 if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
597 push @cust_main, FS::cust_main::Search->fuzzy_search( { 'last' => $last } );
607 foreach ( $cgi->param('company_type') ) {
611 $cgi->param('company_text') =~
612 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
613 or errorpage(emt("Illegal company"));
616 if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
617 push @cust_main, qsearch( 'cust_main',
618 { 'company' => { 'op' => 'ILIKE',
619 'value' => $company } } );
621 push @cust_main, qsearch( 'cust_main',
622 { 'ship_company' => { 'op' => 'ILIKE',
623 'value' => $company } } )
624 if defined dbdef->table('cust_main')->column('ship_last');
627 if ( $company_type{'Substring'} || $company_type{'All'} ) {
629 push @cust_main, qsearch( 'cust_main',
630 { 'company' => { 'op' => 'ILIKE',
631 'value' => "%$company%" } } );
633 push @cust_main, qsearch( 'cust_main',
634 { 'ship_company' => { 'op' => 'ILIKE',
635 'value' => "%$company%" } })
636 if defined dbdef->table('cust_main')->column('ship_last');
640 if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
641 push @cust_main, FS::cust_main::Search->fuzzy_search( { 'company' => $company } );
644 if ($company_type{'Sound-alike'}) {
653 $cgi->param('address2_text') =~
654 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
655 or errorpage(emt("Illegal address2"));
658 push @cust_main, qsearch( 'cust_main',
659 { 'address2' => { 'op' => 'ILIKE',
660 'value' => $address2 } } );
661 push @cust_main, qsearch( 'cust_main',
662 { 'ship_address2' => { 'op' => 'ILIKE',
663 'value' => $address2 } } );
671 my $phone = $cgi->param('phone_text');
674 if ( $phone =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/ ) {
676 $phone .= " x$4" if $4;
677 } elsif ( $phone =~ /^(\d{3})(\d{4})$/ ) {
679 } elsif ( $phone =~ /^(\d{3,4})$/ ) {
682 errorpage(gettext('illegal_phone'). ": $phone");
685 my @fields = qw(daytime night fax);
686 push @fields, qw(ship_daytime ship_night ship_fax)
687 if defined dbdef->table('cust_main')->column('ship_last');
689 for my $field ( @fields ) {
690 push @cust_main, qsearch ( 'cust_main',
691 { $field => { 'op' => 'LIKE',
692 'value' => "%$phone%" } } );