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 %></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('(bill) name') |h %></TH>
85 <TH CLASS="grid" BGCOLOR="#cccccc"><% mt('company') |h %></TH>
87 %if ( defined dbdef->table('cust_main')->column('ship_last') ) {
88 <TH CLASS="grid" BGCOLOR="#cccccc"><% mt('(service) name') |h %></TH>
89 <TH CLASS="grid" BGCOLOR="#cccccc"><% mt('company') |h %></TH>
92 %foreach my $addl_header ( @addl_headers ) {
93 <TH CLASS="grid" BGCOLOR="#cccccc"><% $addl_header %></TH>
96 <TH CLASS="grid" BGCOLOR="#cccccc"><% mt('Packages') |h %></TH>
97 <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN="2"><% mt('Services') |h %></TH>
100 % my $bgcolor1 = '#eeeeee';
101 % my $bgcolor2 = '#ffffff';
104 % my(%saw,$cust_main);
105 % foreach $cust_main (
106 % sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
109 % if ( $bgcolor eq $bgcolor1 ) {
110 % $bgcolor = $bgcolor2;
112 % $bgcolor = $bgcolor1;
115 % my($custnum,$last,$first,$company)=(
116 % $cust_main->custnum,
117 % $cust_main->getfield('last'),
118 % $cust_main->getfield('first'),
119 % $cust_main->company,
124 % foreach my $cust_pkg ( @{$all_pkgs{$custnum}} ) {
125 % my %cust_svc_by_svcpart;
126 % foreach my $svc ( $cust_pkg->cust_svc ) {
127 % push @{ $cust_svc_by_svcpart{$svc->svcpart} ||= [] }, $svc;
129 % push @all_cust_svc, \%cust_svc_by_svcpart;
130 % if ( !keys %cust_svc_by_svcpart ) {
132 % push @pkg_rowspans, 1;
136 % foreach (values %cust_svc_by_svcpart) {
137 % # summarizing takes two rows per svcpart,
138 % # full display takes one per cust_svc
139 % $rows += ( $large_pkg_size > 0 && $large_pkg_size <= scalar @$_ ) ?
142 % push @pkg_rowspans, $rows;
145 % my $rowspan = List::Util::sum(@pkg_rowspans) || 1;
148 % if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
149 % $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
151 % $view = $p. 'view/cust_main.cgi?'. $custnum;
153 % my $pcompany = $company
154 % ? qq!<A HREF="$view"><FONT SIZE=-1>$company</FONT></A>!
155 % : '<FONT SIZE=-1> </FONT>';
157 % my $status = $cust_main->status;
158 % my $statuscol = $cust_main->statuscolor;
161 <TD CLASS="grid" ALIGN="right" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
162 <A HREF="<% $view %>"><FONT SIZE=-1><% $cust_main->display_custnum %></FONT></A>
164 <TD CLASS="grid" ALIGN="center" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
165 <FONT SIZE="-1" COLOR="#<% $statuscol %>"><B><% ucfirst($status) %></B></FONT>
167 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
168 <A HREF="<% $view %>"><FONT SIZE=-1><% "$last, $first" %></FONT></A>
170 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
174 % if ( defined dbdef->table('cust_main')->column('ship_last') ) {
175 % my($ship_last,$ship_first,$ship_company)=(
176 % $cust_main->ship_last || $cust_main->getfield('last'),
177 % $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
178 % $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
180 % my $pship_company = $ship_company
181 % ? qq!<A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A>!
182 % : '<FONT SIZE=-1> </FONT>';
185 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
186 <A HREF="<% $view %>"><FONT SIZE=-1><% "$ship_last, $ship_first" %></FONT></A>
188 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
189 <% $pship_company %></A>
193 % foreach my $addl_col ( @addl_cols ) {
194 % if ( $addl_col eq 'tickets' ) {
195 % if ( @custom_priorities ) {
197 <TD CLASS="inv" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %> ALIGN=right>
199 <TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>
200 % foreach my $priority ( @custom_priorities, '' ) {
202 % FS::TicketSystem->num_customer_tickets($custnum,$priority);
204 % $ahref= '<A HREF="'.
205 % FS::TicketSystem->href_customer_tickets($custnum,$priority).
211 <FONT SIZE=-1><% $ahref.$num %></A></FONT>
214 <FONT SIZE=-1><% $ahref %><% $priority || '<i>('.emt('none').')</i>' %></A></FONT>
220 <TH ALIGN=right STYLE="border-top: dashed 1px black">
223 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %> ALIGN=right>
227 % $ahref = '<A HREF="'.
228 % FS::TicketSystem->href_customer_tickets($custnum).
230 % if $cust_main->get($addl_col);
233 <% $ahref %><% $cust_main->get($addl_col) %></A>
234 % if ( @custom_priorities ) {
237 <TH ALIGN=left STYLE="border-top: dashed 1px black">
238 <FONT SIZE=-1><% ${ahref} %>Total</A><FONT>
247 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %> ALIGN=right><FONT SIZE=-1>
248 <% $cust_main->get($addl_col) %>
255 % foreach ( @{$all_pkgs{$custnum}} ) {
256 % my $pkgnum = $_->pkgnum;
257 % my $part_pkg = $_->part_pkg;
259 % my $pkg_comment = $part_pkg->pkg_comment(nopkgpart => 1);
260 % my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
262 % : ';show=packages';
263 % my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
264 % my $pkgview = "${p}view/cust_main.cgi?custnum=$custnum$show;fragment=$frag#$frag";
265 % # cust_svc stuff, built earlier
266 % my %cust_svc_by_svcpart = %{ shift @all_cust_svc };
267 % my $pkg_rowspan = shift @pkg_rowspans;
269 <% $n1 %><TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN="<% $pkg_rowspan%>">
270 <A HREF="<% $pkgview %>"><FONT SIZE=-1><% $pkg_comment %></FONT></A>
274 % my $td = '<TD CLASS="grid" BGCOLOR="'.$bgcolor.'">';
276 % foreach my $svcpart ( sort keys %cust_svc_by_svcpart ) { #sort order?
277 % my $these = $cust_svc_by_svcpart{$svcpart};
278 % my $num_cust_svc = scalar @$these; # always at least 1
279 % if ( $large_pkg_size > 0 && $num_cust_svc >= $large_pkg_size ) {
282 % # link opens a new search for this pkgnum/svcpart combo
283 % my $href = $p.'search/cust_pkg_svc.html?svcpart='.$svcpart.
284 % ';pkgnum='.$pkgnum;
286 <A HREF="<% $href %>"><% $these->[0]->part_svc->svc %></A>
289 <A HREF="<% $href %>"><B>(<% mt("view all [_1]", $num_cust_svc) |h %>)</B></A>
293 <% $td %><& /elements/search-cust_svc.html,
294 'svcpart' => $svcpart,
299 % else { # do not summarize
300 % foreach my $cust_svc ( @$these ) {
303 <% FS::UI::Web::svc_link($m, $cust_svc->part_svc, $cust_svc) %>
306 <% FS::UI::Web::svc_label_link($m, $cust_svc->part_svc, $cust_svc) %>
309 % } #foreach $cust_svc
311 % } # foreach $svcpart
313 % unless ( %cust_svc_by_svcpart ) {
314 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" COLSPAN=2> </TD>
320 % unless ( @{$all_pkgs{$custnum}} ) {
321 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" COLSPAN=3> </TD>!;
329 <& /elements/footer.html &>
331 my $curuser = $FS::CurrentUser::CurrentUser;
334 unless $curuser->access_right('List customers');
336 my $conf = new FS::Conf;
337 my $maxrecords = $conf->config('maxsearchrecordsperpage');
338 # summarize more than this many services of the same svcpart
339 my $large_pkg_size = $conf->config('cust_pkg-large_pkg_size') || 0;
342 $limit .= "LIMIT $maxrecords" if $maxrecords;
344 my $offset = $cgi->param('offset') || 0;
345 $limit .= " OFFSET $offset" if $offset;
349 my(@cust_main, $sortby, $orderby);
351 my @addl_headers = ();
353 if ( $cgi->param('browse')
354 || $cgi->param('otaker_on')
355 || $cgi->param('agentnum_on')
360 if ( $cgi->param('browse') ) {
361 my $query = $cgi->param('browse');
362 if ( $query eq 'custnum' ) {
363 if ( $conf->exists('cust_main-default_agent_custid') ) {
364 $sortby=\*display_custnum_sort;
365 $orderby = "ORDER BY CASE WHEN agent_custid IS NOT NULL AND agent_custid != '' THEN CAST(agent_custid AS BIGINT) ELSE custnum END";
367 $sortby=\*custnum_sort;
368 $orderby = "ORDER BY custnum";
370 } elsif ( $query eq 'last' ) {
372 $orderby = "ORDER BY LOWER(last || ' ' || first)";
373 } elsif ( $query eq 'company' ) {
374 $sortby=\*company_sort;
375 $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
376 } elsif ( $query eq 'tickets' ) {
377 $sortby = \*tickets_sort;
378 $orderby = "ORDER BY tickets DESC";
379 push @select, FS::TicketSystem->sql_num_customer_tickets. " as tickets";
380 push @addl_headers, 'Tickets';
381 push @addl_cols, 'tickets';
382 } elsif ( $query eq 'uspsunvalid' ) {
383 $search{'country'} = 'US';
384 $sortby=\*custnum_sort;
385 $orderby = "ORDER BY custnum";
387 die "unknown browse field $query";
390 $sortby = \*last_sort; #??
391 $orderby = "ORDER BY LOWER(last || ' ' || first)"; #??
394 if ( $cgi->param('otaker_on') ) {
396 unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
397 $cgi->param('otaker') =~ /^(\w{1,32})$/ or errorpage("Illegal otaker");
398 $search{otaker} = $1;
399 } elsif ( $cgi->param('agentnum_on') ) {
400 $cgi->param('agentnum') =~ /^(\d+)$/ or errorpage("Illegal agentnum");
401 $search{agentnum} = $1;
408 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
409 || ( $conf->exists('hidecancelledcustomers')
410 && ! $cgi->param('showcancelledcustomers') )
412 push @qual, FS::cust_main->uncancel_sql;
415 push @qual, FS::cust_main->cancel_sql if $cgi->param('cancelled');
416 push @qual, FS::cust_main->prospect_sql if $cgi->param('prospect');
417 push @qual, FS::cust_main->active_sql if $cgi->param('active');
418 push @qual, FS::cust_main->inactive_sql if $cgi->param('inactive');
419 push @qual, FS::cust_main->susp_sql if $cgi->param('suspended');
422 my $qual = join(' AND ',
423 map { "$_ = ". dbh->quote($search{$_}) } keys %search );
425 my $addl_qual = join(' AND ', @qual);
427 #here is the agent virtualization
428 $addl_qual .= ( $addl_qual ? ' AND ' : '' ).
429 $FS::CurrentUser::CurrentUser->agentnums_sql;
431 if ( $cgi->param('browse') && $cgi->param('browse') eq 'uspsunvalid' ) {
432 $addl_qual .= ' AND ( length(zip) < 9 OR upper(address1) != address1 OR upper(city) != city ) ';
436 $qual .= ' AND ' if $qual;
440 $qual = " WHERE $qual" if $qual;
441 my $statement = "SELECT COUNT(*) FROM cust_main $qual";
442 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
443 $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
445 $total = $sth->fetchrow_arrayref->[0];
449 $addl_qual = " AND $addl_qual";
451 $addl_qual = " WHERE $addl_qual";
457 $select = 'cust_main.*, '. join (', ', @select);
462 @cust_main = qsearch('cust_main', \%search, $select,
463 "$addl_qual $orderby $limit" );
467 $sortby = \*last_sort;
469 push @cust_main, @{&custnumsearch}
470 if $cgi->param('custnum_on') && $cgi->param('custnum_text');
471 push @cust_main, @{&cardsearch}
472 if $cgi->param('card_on') && $cgi->param('card');
473 push @cust_main, @{&lastsearch}
474 if $cgi->param('last_on') && $cgi->param('last_text');
475 push @cust_main, @{&companysearch}
476 if $cgi->param('company_on') && $cgi->param('company_text');
477 push @cust_main, @{&address2search}
478 if $cgi->param('address2_on') && $cgi->param('address2_text');
479 push @cust_main, @{&phonesearch}
480 if $cgi->param('phone_on') && $cgi->param('phone_text');
481 push @cust_main, @{&referralsearch}
482 if $cgi->param('referral_custnum');
484 if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
485 $sortby = \*company_sort;
486 push @cust_main, @{&companysearch};
489 if ( $cgi->param('search_cust') ) {
490 $sortby = \*company_sort;
491 $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
492 push @cust_main, smart_search( 'search' => scalar($cgi->param('search_cust')),
493 'no_fuzzy_on_exact' => 1, #pref?
497 @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
498 if ! $cgi->param('cancelled')
500 $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
501 || ( $conf->exists('hidecancelledcustomers')
502 && ! $cgi->param('showcancelledcustomers') )
506 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
510 if ( $conf->exists('hidecancelledpackages' ) ) {
511 %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
513 %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
517 lc($a->getfield('last')) cmp lc($b->getfield('last'))
518 || lc($a->first) cmp lc($b->first);
522 return -1 if $a->company && ! $b->company;
523 return 1 if ! $a->company && $b->company;
524 lc($a->company) cmp lc($b->company)
525 || lc($a->getfield('last')) cmp lc($b->getfield('last'))
526 || lc($a->first) cmp lc($b->first);;
529 sub display_custnum_sort {
530 $a->display_custnum <=> $b->display_custnum;
534 $a->getfield('custnum') <=> $b->getfield('custnum');
538 $b->getfield('tickets') <=> $a->getfield('tickets');
543 my $custnum = $cgi->param('custnum_text');
545 $custnum =~ /^(\d{1,23})$/ or errorpage(emt("Illegal customer number"));
548 [ qsearchs('cust_main', { 'custnum' => $custnum } ) ];
553 my($card)=$cgi->param('card');
555 $card =~ /^(\d{13,16})$/ or errorpage(emt("Illegal card number"));
558 [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}),
559 qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'DCRD'})
564 $cgi->param('referral_custnum') =~ /^(\d+)$/
565 or errorpage("Illegal referral_custnum");
566 my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
567 or errorpage(emt("Customer [_1] not found",$1));
569 if ( $cgi->param('referral_depth') ) {
570 $cgi->param('referral_depth') =~ /^(\d+)$/
571 or errorpage(emt("Illegal referral_depth"));
576 [ $cust_main->referral_cust_main($depth) ];
582 foreach ( $cgi->param('last_type') ) {
586 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
587 or errorpage(emt("Illegal last name"));
590 if ( $last_type{'Exact'} || $last_type{'Fuzzy'} ) {
591 push @cust_main, qsearch( 'cust_main',
592 { 'last' => { 'op' => 'ILIKE',
593 'value' => $last } } );
595 push @cust_main, qsearch( 'cust_main',
596 { 'ship_last' => { 'op' => 'ILIKE',
597 'value' => $last } } )
598 if defined dbdef->table('cust_main')->column('ship_last');
601 if ( $last_type{'Substring'} || $last_type{'All'} ) {
603 push @cust_main, qsearch( 'cust_main',
604 { 'last' => { 'op' => 'ILIKE',
605 'value' => "%$last%" } } );
607 push @cust_main, qsearch( 'cust_main',
608 { 'ship_last' => { 'op' => 'ILIKE',
609 'value' => "%$last%" } } )
610 if defined dbdef->table('cust_main')->column('ship_last');
614 if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
615 push @cust_main, FS::cust_main::Search->fuzzy_search( { 'last' => $last } );
625 foreach ( $cgi->param('company_type') ) {
629 $cgi->param('company_text') =~
630 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
631 or errorpage(emt("Illegal company"));
634 if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
635 push @cust_main, qsearch( 'cust_main',
636 { 'company' => { 'op' => 'ILIKE',
637 'value' => $company } } );
639 push @cust_main, qsearch( 'cust_main',
640 { 'ship_company' => { 'op' => 'ILIKE',
641 'value' => $company } } )
642 if defined dbdef->table('cust_main')->column('ship_last');
645 if ( $company_type{'Substring'} || $company_type{'All'} ) {
647 push @cust_main, qsearch( 'cust_main',
648 { 'company' => { 'op' => 'ILIKE',
649 'value' => "%$company%" } } );
651 push @cust_main, qsearch( 'cust_main',
652 { 'ship_company' => { 'op' => 'ILIKE',
653 'value' => "%$company%" } })
654 if defined dbdef->table('cust_main')->column('ship_last');
658 if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
659 push @cust_main, FS::cust_main::Search->fuzzy_search( { 'company' => $company } );
662 if ($company_type{'Sound-alike'}) {
671 $cgi->param('address2_text') =~
672 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
673 or errorpage(emt("Illegal address2"));
676 push @cust_main, qsearch( 'cust_main',
677 { 'address2' => { 'op' => 'ILIKE',
678 'value' => $address2 } } );
679 push @cust_main, qsearch( 'cust_main',
680 { 'ship_address2' => { 'op' => 'ILIKE',
681 'value' => $address2 } } );
689 my $phone = $cgi->param('phone_text');
692 if ( $phone =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/ ) {
694 $phone .= " x$4" if $4;
695 } elsif ( $phone =~ /^(\d{3})(\d{4})$/ ) {
697 } elsif ( $phone =~ /^(\d{3,4})$/ ) {
700 errorpage(gettext('illegal_phone'). ": $phone");
703 my @fields = qw(daytime night fax);
704 push @fields, qw(ship_daytime ship_night ship_fax)
705 if defined dbdef->table('cust_main')->column('ship_last');
707 for my $field ( @fields ) {
708 push @cust_main, qsearch ( 'cust_main',
709 { $field => { 'op' => 'LIKE',
710 'value' => "%$phone%" } } );