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 (
123 % $cust_pkg->part_svc( summarize_size=>$large_pkg_size )
125 % my $svcpart = $part_svc->svcpart;
126 % my $num_cust_svc = $part_svc->num_cust_svc;
127 % if ( $large_pkg_size > 0 and $num_cust_svc >= $large_pkg_size ) {
128 % # don't retrieve the cust_svc records, just stash the
129 % # part_svc and num_cust_svc for later
130 % $cust_svc_by_svcpart{$svcpart} =
131 % [ 'summarize', $part_svc, $num_cust_svc ];
134 % elsif ( $num_cust_svc ) {
135 % $cust_svc_by_svcpart{$svcpart} = $part_svc->cust_pkg_svc;
136 % $rows += $num_cust_svc;
138 % } #foreach $part_svc
139 % $rows ||= 1; # in case the package has no services
140 % push @all_cust_svc, \%cust_svc_by_svcpart;
141 % push @pkg_rowspans, $rows;
142 % } #foreach $cust_pkg
143 % my $rowspan = List::Util::sum(@pkg_rowspans) || 1;
146 % if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
147 % $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
149 % $view = $p. 'view/cust_main.cgi?'. $custnum;
151 % my $pcompany = $company
152 % ? qq!<A HREF="$view"><FONT SIZE=-1>!. encode_entities($company). '</FONT></A>'
153 % : '<FONT SIZE=-1> </FONT>';
155 % my $status_label = $cust_main->status_label;
156 % my $statuscolor = $cust_main->statuscolor;
159 <TD CLASS="grid" ALIGN="right" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
160 <A HREF="<% $view %>"><FONT SIZE=-1><% $cust_main->display_custnum %></FONT></A>
162 <TD CLASS="grid" ALIGN="center" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
163 <FONT SIZE="-1" COLOR="#<% $statuscolor %>"><B><% $status_label %></B></FONT>
165 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
166 <A HREF="<% $view %>"><FONT SIZE=-1><% "$last, $first" |h %></FONT></A>
168 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
172 % foreach my $addl_col ( @addl_cols ) {
173 % if ( $addl_col eq 'tickets' ) {
174 % if ( @custom_priorities ) {
176 <TD CLASS="inv" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %> ALIGN=right>
178 <TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>
179 % foreach my $priority ( @custom_priorities, '' ) {
181 % FS::TicketSystem->num_customer_tickets($custnum,$priority);
183 % $ahref= '<A HREF="'.
184 % FS::TicketSystem->href_customer_tickets($custnum,$priority).
190 <FONT SIZE=-1><% $ahref.$num %></A></FONT>
193 <FONT SIZE=-1><% $ahref %><% $priority || '<i>('.emt('none').')</i>' %></A></FONT>
199 <TH ALIGN=right STYLE="border-top: dashed 1px black">
202 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %> ALIGN=right>
206 % $ahref = '<A HREF="'.
207 % FS::TicketSystem->href_customer_tickets($custnum).
209 % if $cust_main->get($addl_col);
212 <% $ahref %><% $cust_main->get($addl_col) %></A>
213 % if ( @custom_priorities ) {
216 <TH ALIGN=left STYLE="border-top: dashed 1px black">
217 <FONT SIZE=-1><% ${ahref} %>Total</A><FONT>
226 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %> ALIGN=right><FONT SIZE=-1>
227 <% $cust_main->get($addl_col) %>
234 % foreach ( @{$all_pkgs{$custnum}} ) {
235 % my $pkgnum = $_->pkgnum;
236 % my $part_pkg = $_->part_pkg;
238 % my $pkg_comment = $part_pkg->pkg_comment( cust_pkg=>$_, nopkgpart=>1 );
239 % my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
241 % : ';show=packages';
242 % my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
243 % my $pkgview = "${p}view/cust_main.cgi?custnum=$custnum$show;fragment=$frag#$frag";
244 % # cust_svc stuff, built earlier
245 % my %cust_svc_by_svcpart = %{ shift @all_cust_svc };
246 % my $pkg_rowspan = shift @pkg_rowspans;
248 <% $n1 %><TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN="<% $pkg_rowspan%>">
249 <A HREF="<% $pkgview %>"><FONT SIZE=-1><% $pkg_comment |h %></FONT></A>
253 % my $td = '<TD CLASS="grid" BGCOLOR="'.$bgcolor.'">';
255 % foreach my $svcpart ( sort keys %cust_svc_by_svcpart ) { #sort order?
256 % my $these = $cust_svc_by_svcpart{$svcpart};
257 % if ( $these->[0] eq 'summarize' ) {
258 % my $part_svc = $these->[1];
259 % my $num_cust_svc = $these->[2];
262 % # link opens a new search for this pkgnum/svcpart combo
263 % my $href = $p.'search/cust_pkg_svc.html?svcpart='.$svcpart.
264 % ';pkgnum='.$pkgnum;
266 <A HREF="<% $href %>"><% $part_svc->svc %></A>
269 <A HREF="<% $href %>"><B>(<% mt("view all [_1]", $num_cust_svc) |h %>)</B></A>
273 <% $td %><& /elements/search-cust_svc.html,
274 'svcpart' => $svcpart,
276 'svcdb' => $part_svc->svcdb,
280 % elsif ( scalar @$these ) { # do not summarize
281 % foreach my $cust_svc ( @$these ) {
284 <% FS::UI::Web::svc_link($m, $cust_svc->part_svc, $cust_svc) %>
287 <% FS::UI::Web::svc_label_link($m, $cust_svc->part_svc, $cust_svc) %>
290 % } #foreach $cust_svc
292 % } # foreach $svcpart
294 % unless ( %cust_svc_by_svcpart ) {
295 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" COLSPAN=2> </TD>
301 % unless ( @{$all_pkgs{$custnum}} ) {
302 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" COLSPAN=3> </TD>
310 <& /elements/footer.html &>
312 my $curuser = $FS::CurrentUser::CurrentUser;
315 unless $curuser->access_right('List all customers');
317 my $conf = new FS::Conf;
318 my $maxrecords = $conf->config('maxsearchrecordsperpage');
319 # summarize more than this many services of the same svcpart
320 my $large_pkg_size = $conf->config('cust_pkg-large_pkg_size') || 0;
323 $limit .= "LIMIT $maxrecords" if $maxrecords;
325 my $offset = $cgi->param('offset') || 0;
326 $limit .= " OFFSET $offset" if $offset;
330 my(@cust_main, $sortby, $orderby);
332 my @addl_headers = ();
334 if ( $cgi->param('browse')
335 || $cgi->param('otaker_on')
336 || $cgi->param('agentnum_on')
341 if ( $cgi->param('browse') ) {
342 my $query = $cgi->param('browse');
343 if ( $query eq 'custnum' ) {
344 if ( $conf->exists('cust_main-default_agent_custid') ) {
345 $sortby=\*display_custnum_sort;
346 $orderby = "ORDER BY CASE WHEN agent_custid IS NOT NULL
347 AND agent_custid != ''
348 AND agent_custid ". regexp_sql. " '^[0-9]+\$'
349 THEN CAST(agent_custid AS BIGINT)
353 $sortby=\*custnum_sort;
354 $orderby = "ORDER BY custnum";
356 } elsif ( $query eq 'last' ) {
358 $orderby = "ORDER BY LOWER(last || ' ' || first)";
359 } elsif ( $query eq 'company' ) {
360 $sortby=\*company_sort;
361 $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
362 } elsif ( $query eq 'tickets' ) {
363 $sortby = \*tickets_sort;
364 $orderby = "ORDER BY tickets DESC";
365 push @select, FS::TicketSystem->sql_num_customer_tickets. " as tickets";
366 push @addl_headers, 'Tickets';
367 push @addl_cols, 'tickets';
368 } elsif ( $query eq 'uspsunvalid' ) {
369 $search{'country'} = 'US';
370 $sortby=\*custnum_sort;
371 $orderby = "ORDER BY custnum";
373 die "unknown browse field $query";
376 $sortby = \*last_sort; #??
377 $orderby = "ORDER BY LOWER(last || ' ' || first)"; #??
380 if ( $cgi->param('otaker_on') ) {
382 unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
383 $cgi->param('otaker') =~ /^(\w{1,32})$/ or errorpage("Illegal otaker");
384 $search{otaker} = $1;
385 } elsif ( $cgi->param('agentnum_on') ) {
386 $cgi->param('agentnum') =~ /^(\d+)$/ or errorpage("Illegal agentnum");
387 $search{agentnum} = $1;
394 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
395 || ( $conf->exists('hidecancelledcustomers')
396 && ! $cgi->param('showcancelledcustomers') )
398 push @qual, FS::cust_main->uncancel_sql;
401 push @qual, FS::cust_main->cancel_sql if $cgi->param('cancelled');
402 push @qual, FS::cust_main->prospect_sql if $cgi->param('prospect');
403 push @qual, FS::cust_main->ordered_sql if $cgi->param('ordered');
404 push @qual, FS::cust_main->active_sql if $cgi->param('active');
405 push @qual, FS::cust_main->inactive_sql if $cgi->param('inactive');
406 push @qual, FS::cust_main->susp_sql if $cgi->param('suspended');
409 my $qual = join(' AND ',
410 map { "$_ = ". dbh->quote($search{$_}) } keys %search );
412 my $addl_qual = join(' AND ', @qual);
414 #here is the agent virtualization
415 $addl_qual .= ( $addl_qual ? ' AND ' : '' ).
416 $FS::CurrentUser::CurrentUser->agentnums_sql;
418 if ( $cgi->param('browse') && $cgi->param('browse') eq 'uspsunvalid' ) {
419 $addl_qual .= ' AND ( length(zip) < 9 OR upper(address1) != address1 OR upper(city) != city ) ';
423 $qual .= ' AND ' if $qual;
427 $qual = " WHERE $qual" if $qual;
428 my $statement = "SELECT COUNT(*) FROM cust_main $qual";
429 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
430 $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
432 $total = $sth->fetchrow_arrayref->[0];
436 $addl_qual = " AND $addl_qual";
438 $addl_qual = " WHERE $addl_qual";
444 $select = 'cust_main.*, '. join (', ', @select);
449 @cust_main = qsearch('cust_main', \%search, $select,
450 "$addl_qual $orderby $limit" );
454 $sortby = \*last_sort;
456 push @cust_main, @{&custnumsearch}
457 if $cgi->param('custnum_on') && $cgi->param('custnum_text');
458 push @cust_main, @{&cardsearch}
459 if $cgi->param('card_on') && $cgi->param('card');
460 push @cust_main, @{&lastsearch}
461 if $cgi->param('last_on') && $cgi->param('last_text');
462 push @cust_main, @{&companysearch}
463 if $cgi->param('company_on') && $cgi->param('company_text');
464 push @cust_main, @{&address2search}
465 if $cgi->param('address2_on') && $cgi->param('address2_text');
466 push @cust_main, @{&phonesearch}
467 if $cgi->param('phone_on') && $cgi->param('phone_text');
468 push @cust_main, @{&referralsearch}
469 if $cgi->param('referral_custnum');
471 if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
472 $sortby = \*company_sort;
473 push @cust_main, @{&companysearch};
476 if ( $cgi->param('search_cust') ) {
477 $sortby = \*company_sort;
478 $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
479 push @cust_main, smart_search(
480 'search' => scalar($cgi->param('search_cust')),
484 @cust_main = grep { $_->num_ncancelled_pkgs || ! $_->num_pkgs } @cust_main
485 if ! $cgi->param('cancelled')
487 $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
488 || ( $conf->exists('hidecancelledcustomers')
489 && ! $cgi->param('showcancelledcustomers') )
493 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
496 my $pkgs_method = $conf->exists('hidecancelledpackages')
500 #false laziness w/httemplate/view/cust_main/packages.html
501 my $select = '*, setup_option.optionvalue AS _opt_setup_fee, '.
502 'recur_option.optionvalue AS _opt_recur_fee',
504 LEFT JOIN part_pkg USING ( pkgpart )
505 LEFT JOIN part_pkg_option AS setup_option
506 ON ( cust_pkg.pkgpart = setup_option.pkgpart
507 AND setup_option.optionname = 'setup_fee' )
508 LEFT JOIN part_pkg_option AS recur_option
509 ON ( cust_pkg.pkgpart = recur_option.pkgpart
510 AND recur_option.optionname = 'recur_fee' )
513 my %all_pkgs = map { $_->custnum =>
514 [ $_->$pkgs_method({ select => $select,
515 addl_from => $addl_from,
522 lc($a->getfield('last')) cmp lc($b->getfield('last'))
523 || lc($a->first) cmp lc($b->first);
527 return -1 if $a->company && ! $b->company;
528 return 1 if ! $a->company && $b->company;
529 lc($a->company) cmp lc($b->company)
530 || lc($a->getfield('last')) cmp lc($b->getfield('last'))
531 || lc($a->first) cmp lc($b->first);;
534 sub display_custnum_sort {
535 $a->display_custnum <=> $b->display_custnum;
539 $a->getfield('custnum') <=> $b->getfield('custnum');
543 $b->getfield('tickets') <=> $a->getfield('tickets');
548 my $custnum = $cgi->param('custnum_text');
550 $custnum =~ /^(\d{1,23})$/ or errorpage(emt("Illegal customer number"));
553 [ qsearchs('cust_main', { 'custnum' => $custnum } ) ];
558 my($card)=$cgi->param('card');
560 $card =~ /^(\d{13,16}|\d{8,9})$/ or errorpage(emt("Illegal card number"));
563 [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}),
564 qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'DCRD'})
569 $cgi->param('referral_custnum') =~ /^(\d+)$/
570 or errorpage("Illegal referral_custnum");
571 my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
572 or errorpage(emt("Customer [_1] not found",$1));
574 if ( $cgi->param('referral_depth') ) {
575 $cgi->param('referral_depth') =~ /^(\d+)$/
576 or errorpage(emt("Illegal referral_depth"));
581 [ $cust_main->referral_cust_main($depth) ];
587 foreach ( $cgi->param('last_type') ) {
591 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
592 or errorpage(emt("Illegal last name"));
595 if ( $last_type{'Exact'} || $last_type{'Fuzzy'} ) {
596 push @cust_main, qsearch( 'cust_main',
597 { 'last' => { 'op' => 'ILIKE',
598 'value' => $last } } );
600 push @cust_main, qsearch( 'cust_main',
601 { 'ship_last' => { 'op' => 'ILIKE',
602 'value' => $last } } )
603 if defined dbdef->table('cust_main')->column('ship_last');
606 if ( $last_type{'Substring'} || $last_type{'All'} ) {
608 push @cust_main, qsearch( 'cust_main',
609 { 'last' => { 'op' => 'ILIKE',
610 'value' => "%$last%" } } );
612 push @cust_main, qsearch( 'cust_main',
613 { 'ship_last' => { 'op' => 'ILIKE',
614 'value' => "%$last%" } } )
615 if defined dbdef->table('cust_main')->column('ship_last');
619 if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
620 push @cust_main, FS::cust_main::Search->fuzzy_search( { 'last' => $last } );
630 foreach ( $cgi->param('company_type') ) {
634 $cgi->param('company_text') =~
635 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
636 or errorpage(emt("Illegal company"));
639 if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
640 push @cust_main, qsearch( 'cust_main',
641 { 'company' => { 'op' => 'ILIKE',
642 'value' => $company } } );
644 push @cust_main, qsearch( 'cust_main',
645 { 'ship_company' => { 'op' => 'ILIKE',
646 'value' => $company } } )
647 if defined dbdef->table('cust_main')->column('ship_last');
650 if ( $company_type{'Substring'} || $company_type{'All'} ) {
652 push @cust_main, qsearch( 'cust_main',
653 { 'company' => { 'op' => 'ILIKE',
654 'value' => "%$company%" } } );
656 push @cust_main, qsearch( 'cust_main',
657 { 'ship_company' => { 'op' => 'ILIKE',
658 'value' => "%$company%" } })
659 if defined dbdef->table('cust_main')->column('ship_last');
663 if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
664 push @cust_main, FS::cust_main::Search->fuzzy_search( { 'company' => $company } );
667 if ($company_type{'Sound-alike'}) {
676 $cgi->param('address2_text') =~
677 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
678 or errorpage(emt("Illegal address2"));
681 push @cust_main, qsearch( 'cust_main',
682 { 'address2' => { 'op' => 'ILIKE',
683 'value' => $address2 } } );
684 push @cust_main, qsearch( 'cust_main',
685 { 'ship_address2' => { 'op' => 'ILIKE',
686 'value' => $address2 } } );
694 my $phone = $cgi->param('phone_text');
697 if ( $phone =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/ ) {
699 $phone .= " x$4" if $4;
700 } elsif ( $phone =~ /^(\d{3})(\d{4})$/ ) {
702 } elsif ( $phone =~ /^(\d{3,4})$/ ) {
705 errorpage(gettext('illegal_phone'). ": $phone");
708 my @fields = qw(daytime night fax);
709 push @fields, qw(ship_daytime ship_night ship_fax)
710 if defined dbdef->table('cust_main')->column('ship_last');
712 for my $field ( @fields ) {
713 push @cust_main, qsearch ( 'cust_main',
714 { $field => { 'op' => 'LIKE',
715 'value' => "%$phone%" } } );