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 % local($FS::part_pkg::cache_enabled) = 1; #for $cust_pkg->part_svc
123 % foreach my $part_svc (
124 % $cust_pkg->part_svc( summarize_size=>$large_pkg_size )
126 % my $svcpart = $part_svc->svcpart;
127 % my $num_cust_svc = $part_svc->num_cust_svc;
128 % if ( $large_pkg_size > 0 and $num_cust_svc >= $large_pkg_size ) {
129 % # don't retrieve the cust_svc records, just stash the
130 % # part_svc and num_cust_svc for later
131 % $cust_svc_by_svcpart{$svcpart} =
132 % [ 'summarize', $part_svc, $num_cust_svc ];
135 % elsif ( $num_cust_svc ) {
136 % $cust_svc_by_svcpart{$svcpart} = $part_svc->cust_pkg_svc;
137 % $rows += $num_cust_svc;
139 % } #foreach $part_svc
140 % $rows ||= 1; # in case the package has no services
141 % push @all_cust_svc, \%cust_svc_by_svcpart;
142 % push @pkg_rowspans, $rows;
143 % } #foreach $cust_pkg
144 % my $rowspan = List::Util::sum(@pkg_rowspans) || 1;
147 % if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
148 % $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
150 % $view = $p. 'view/cust_main.cgi?'. $custnum;
152 % my $pcompany = $company
153 % ? qq!<A HREF="$view"><FONT SIZE=-1>!. encode_entities($company). '</FONT></A>'
154 % : '<FONT SIZE=-1> </FONT>';
156 % my $status_label = $cust_main->status_label;
157 % my $statuscolor = $cust_main->statuscolor;
160 <TD CLASS="grid" ALIGN="right" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
161 <A HREF="<% $view %>"><FONT SIZE=-1><% $cust_main->display_custnum %></FONT></A>
163 <TD CLASS="grid" ALIGN="center" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
164 <FONT SIZE="-1" COLOR="#<% $statuscolor %>"><B><% $status_label %></B></FONT>
166 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
167 <A HREF="<% $view %>"><FONT SIZE=-1><% "$last, $first" |h %></FONT></A>
169 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
173 % foreach my $addl_col ( @addl_cols ) {
174 % if ( $addl_col eq 'tickets' ) {
175 % if ( @custom_priorities ) {
177 <TD CLASS="inv" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %> ALIGN=right>
179 <TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>
180 % foreach my $priority ( @custom_priorities, '' ) {
182 % FS::TicketSystem->num_customer_tickets($custnum,$priority);
184 % $ahref= '<A HREF="'.
185 % FS::TicketSystem->href_customer_tickets($custnum,$priority).
191 <FONT SIZE=-1><% $ahref.$num %></A></FONT>
194 <FONT SIZE=-1><% $ahref %><% $priority || '<i>('.emt('none').')</i>' %></A></FONT>
200 <TH ALIGN=right STYLE="border-top: dashed 1px black">
203 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %> ALIGN=right>
207 % $ahref = '<A HREF="'.
208 % FS::TicketSystem->href_customer_tickets($custnum).
210 % if $cust_main->get($addl_col);
213 <% $ahref %><% $cust_main->get($addl_col) %></A>
214 % if ( @custom_priorities ) {
217 <TH ALIGN=left STYLE="border-top: dashed 1px black">
218 <FONT SIZE=-1><% ${ahref} %>Total</A><FONT>
227 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %> ALIGN=right><FONT SIZE=-1>
228 <% $cust_main->get($addl_col) %>
235 % foreach ( @{$all_pkgs{$custnum}} ) {
236 % my $pkgnum = $_->pkgnum;
237 % my $part_pkg = $_->part_pkg;
239 % my $pkg_comment = $part_pkg->pkg_comment( cust_pkg=>$_, nopkgpart=>1 );
240 % my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
242 % : ';show=packages';
243 % my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
244 % my $pkgview = "${p}view/cust_main.cgi?custnum=$custnum$show;fragment=$frag#$frag";
245 % # cust_svc stuff, built earlier
246 % my %cust_svc_by_svcpart = %{ shift @all_cust_svc };
247 % my $pkg_rowspan = shift @pkg_rowspans;
249 <% $n1 %><TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN="<% $pkg_rowspan%>">
250 <A HREF="<% $pkgview %>"><FONT SIZE=-1><% $pkg_comment |h %></FONT></A>
254 % my $td = '<TD CLASS="grid" BGCOLOR="'.$bgcolor.'">';
256 % foreach my $svcpart ( sort keys %cust_svc_by_svcpart ) { #sort order?
257 % my $these = $cust_svc_by_svcpart{$svcpart};
258 % if ( $these->[0] eq 'summarize' ) {
259 % my $part_svc = $these->[1];
260 % my $num_cust_svc = $these->[2];
263 % # link opens a new search for this pkgnum/svcpart combo
264 % my $href = $p.'search/cust_pkg_svc.html?svcpart='.$svcpart.
265 % ';pkgnum='.$pkgnum;
267 <A HREF="<% $href %>"><% $part_svc->svc %></A>
270 <A HREF="<% $href %>"><B>(<% mt("view all [_1]", $num_cust_svc) |h %>)</B></A>
274 <% $td %><& /elements/search-cust_svc.html,
275 'svcpart' => $svcpart,
277 'svcdb' => $part_svc->svcdb,
281 % elsif ( scalar @$these ) { # do not summarize
282 % foreach my $cust_svc ( @$these ) {
285 <% FS::UI::Web::svc_link($m, $cust_svc->part_svc, $cust_svc) %>
288 <% FS::UI::Web::svc_label_link($m, $cust_svc->part_svc, $cust_svc) %>
291 % } #foreach $cust_svc
293 % } # foreach $svcpart
295 % unless ( %cust_svc_by_svcpart ) {
296 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" COLSPAN=2> </TD>
302 % unless ( @{$all_pkgs{$custnum}} ) {
303 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" COLSPAN=3> </TD>
311 <& /elements/footer.html &>
313 my $curuser = $FS::CurrentUser::CurrentUser;
316 unless $curuser->access_right('List all customers');
318 my $conf = new FS::Conf;
319 my $maxrecords = $conf->config('maxsearchrecordsperpage');
320 # summarize more than this many services of the same svcpart
321 my $large_pkg_size = $conf->config('cust_pkg-large_pkg_size') || 0;
324 $limit .= "LIMIT $maxrecords" if $maxrecords;
326 my $offset = $cgi->param('offset') || 0;
327 $limit .= " OFFSET $offset" if $offset;
331 my(@cust_main, $sortby, $orderby);
333 my @addl_headers = ();
335 if ( $cgi->param('browse')
336 || $cgi->param('otaker_on')
337 || $cgi->param('agentnum_on')
342 if ( $cgi->param('browse') ) {
343 my $query = $cgi->param('browse');
344 if ( $query eq 'custnum' ) {
345 if ( $conf->exists('cust_main-default_agent_custid') ) {
346 $sortby=\*display_custnum_sort;
347 $orderby = "ORDER BY CASE WHEN agent_custid IS NOT NULL
348 AND agent_custid != ''
349 AND agent_custid ". regexp_sql. " '^[0-9]+\$'
350 THEN CAST(agent_custid AS BIGINT)
354 $sortby=\*custnum_sort;
355 $orderby = "ORDER BY custnum";
357 } elsif ( $query eq 'last' ) {
359 $orderby = "ORDER BY LOWER(last || ' ' || first)";
360 } elsif ( $query eq 'company' ) {
361 $sortby=\*company_sort;
362 $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
363 } elsif ( $query eq 'tickets' ) {
364 $sortby = \*tickets_sort;
365 $orderby = "ORDER BY tickets DESC";
366 push @select, FS::TicketSystem->sql_num_customer_tickets. " as tickets";
367 push @addl_headers, 'Tickets';
368 push @addl_cols, 'tickets';
369 } elsif ( $query eq 'uspsunvalid' ) {
370 $search{'country'} = 'US';
371 $sortby=\*custnum_sort;
372 $orderby = "ORDER BY custnum";
374 die "unknown browse field $query";
377 $sortby = \*last_sort; #??
378 $orderby = "ORDER BY LOWER(last || ' ' || first)"; #??
381 if ( $cgi->param('otaker_on') ) {
383 unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
384 $cgi->param('otaker') =~ /^(\w{1,32})$/ or errorpage("Illegal otaker");
385 $search{otaker} = $1;
386 } elsif ( $cgi->param('agentnum_on') ) {
387 $cgi->param('agentnum') =~ /^(\d+)$/ or errorpage("Illegal agentnum");
388 $search{agentnum} = $1;
395 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
396 || ( $conf->exists('hidecancelledcustomers')
397 && ! $cgi->param('showcancelledcustomers') )
399 push @qual, FS::cust_main->uncancel_sql;
402 push @qual, FS::cust_main->cancel_sql if $cgi->param('cancelled');
403 push @qual, FS::cust_main->prospect_sql if $cgi->param('prospect');
404 push @qual, FS::cust_main->ordered_sql if $cgi->param('ordered');
405 push @qual, FS::cust_main->active_sql if $cgi->param('active');
406 push @qual, FS::cust_main->inactive_sql if $cgi->param('inactive');
407 push @qual, FS::cust_main->susp_sql if $cgi->param('suspended');
410 my $qual = join(' AND ',
411 map { "$_ = ". dbh->quote($search{$_}) } keys %search );
413 my $addl_qual = join(' AND ', @qual);
415 #here is the agent virtualization
416 $addl_qual .= ( $addl_qual ? ' AND ' : '' ).
417 $FS::CurrentUser::CurrentUser->agentnums_sql;
419 if ( $cgi->param('browse') && $cgi->param('browse') eq 'uspsunvalid' ) {
420 $addl_qual .= ' AND ( length(zip) < 9 OR upper(address1) != address1 OR upper(city) != city ) ';
424 $qual .= ' AND ' if $qual;
428 $qual = " WHERE $qual" if $qual;
429 my $statement = "SELECT COUNT(*) FROM cust_main $qual";
430 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
431 $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
433 $total = $sth->fetchrow_arrayref->[0];
437 $addl_qual = " AND $addl_qual";
439 $addl_qual = " WHERE $addl_qual";
445 $select = 'cust_main.*, '. join (', ', @select);
450 @cust_main = qsearch('cust_main', \%search, $select,
451 "$addl_qual $orderby $limit" );
455 $sortby = \*last_sort;
457 push @cust_main, @{&custnumsearch}
458 if $cgi->param('custnum_on') && $cgi->param('custnum_text');
459 push @cust_main, @{&cardsearch}
460 if $cgi->param('card_on') && $cgi->param('card');
461 push @cust_main, @{&lastsearch}
462 if $cgi->param('last_on') && $cgi->param('last_text');
463 push @cust_main, @{&companysearch}
464 if $cgi->param('company_on') && $cgi->param('company_text');
465 push @cust_main, @{&address2search}
466 if $cgi->param('address2_on') && $cgi->param('address2_text');
467 push @cust_main, @{&phonesearch}
468 if $cgi->param('phone_on') && $cgi->param('phone_text');
469 push @cust_main, @{&referralsearch}
470 if $cgi->param('referral_custnum');
472 if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
473 $sortby = \*company_sort;
474 push @cust_main, @{&companysearch};
477 if ( $cgi->param('search_cust') ) {
478 $sortby = \*company_sort;
479 $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
480 push @cust_main, smart_search(
481 'search' => scalar($cgi->param('search_cust')),
485 @cust_main = grep { $_->num_ncancelled_pkgs || ! $_->num_pkgs } @cust_main
486 if ! $cgi->param('cancelled')
488 $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
489 || ( $conf->exists('hidecancelledcustomers')
490 && ! $cgi->param('showcancelledcustomers') )
494 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
497 my $pkgs_method = $conf->exists('hidecancelledpackages')
501 #false laziness w/httemplate/view/cust_main/packages.html
502 my $select = join(',',
505 'setup_option.optionvalue AS _opt_setup_fee',
506 'recur_option.optionvalue AS _opt_recur_fee',
510 LEFT JOIN part_pkg USING ( pkgpart )
511 LEFT JOIN part_pkg_option AS setup_option
512 ON ( cust_pkg.pkgpart = setup_option.pkgpart
513 AND setup_option.optionname = 'setup_fee' )
514 LEFT JOIN part_pkg_option AS recur_option
515 ON ( cust_pkg.pkgpart = recur_option.pkgpart
516 AND recur_option.optionname = 'recur_fee' )
519 my %all_pkgs = map { $_->custnum =>
520 [ $_->$pkgs_method({ select => $select,
521 addl_from => $addl_from,
528 lc($a->getfield('last')) cmp lc($b->getfield('last'))
529 || lc($a->first) cmp lc($b->first);
533 return -1 if $a->company && ! $b->company;
534 return 1 if ! $a->company && $b->company;
535 lc($a->company) cmp lc($b->company)
536 || lc($a->getfield('last')) cmp lc($b->getfield('last'))
537 || lc($a->first) cmp lc($b->first);;
540 sub display_custnum_sort {
541 $a->display_custnum <=> $b->display_custnum;
545 $a->getfield('custnum') <=> $b->getfield('custnum');
549 $b->getfield('tickets') <=> $a->getfield('tickets');
554 my $custnum = $cgi->param('custnum_text');
556 $custnum =~ /^(\d{1,23})$/ or errorpage(emt("Illegal customer number"));
559 [ qsearchs('cust_main', { 'custnum' => $custnum } ) ];
564 my($card)=$cgi->param('card');
566 $card =~ /^(\d{13,16}|\d{8,9})$/ or errorpage(emt("Illegal card number"));
569 [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}),
570 qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'DCRD'})
575 $cgi->param('referral_custnum') =~ /^(\d+)$/
576 or errorpage("Illegal referral_custnum");
577 my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
578 or errorpage(emt("Customer [_1] not found",$1));
580 if ( $cgi->param('referral_depth') ) {
581 $cgi->param('referral_depth') =~ /^(\d+)$/
582 or errorpage(emt("Illegal referral_depth"));
587 [ $cust_main->referral_cust_main($depth) ];
593 foreach ( $cgi->param('last_type') ) {
597 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
598 or errorpage(emt("Illegal last name"));
601 if ( $last_type{'Exact'} || $last_type{'Fuzzy'} ) {
602 push @cust_main, qsearch( 'cust_main',
603 { 'last' => { 'op' => 'ILIKE',
604 'value' => $last } } );
606 push @cust_main, qsearch( 'cust_main',
607 { 'ship_last' => { 'op' => 'ILIKE',
608 'value' => $last } } )
609 if defined dbdef->table('cust_main')->column('ship_last');
612 if ( $last_type{'Substring'} || $last_type{'All'} ) {
614 push @cust_main, qsearch( 'cust_main',
615 { 'last' => { 'op' => 'ILIKE',
616 'value' => "%$last%" } } );
618 push @cust_main, qsearch( 'cust_main',
619 { 'ship_last' => { 'op' => 'ILIKE',
620 'value' => "%$last%" } } )
621 if defined dbdef->table('cust_main')->column('ship_last');
625 if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
626 push @cust_main, FS::cust_main::Search->fuzzy_search( { 'last' => $last } );
636 foreach ( $cgi->param('company_type') ) {
640 $cgi->param('company_text') =~
641 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
642 or errorpage(emt("Illegal company"));
645 if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
646 push @cust_main, qsearch( 'cust_main',
647 { 'company' => { 'op' => 'ILIKE',
648 'value' => $company } } );
650 push @cust_main, qsearch( 'cust_main',
651 { 'ship_company' => { 'op' => 'ILIKE',
652 'value' => $company } } )
653 if defined dbdef->table('cust_main')->column('ship_last');
656 if ( $company_type{'Substring'} || $company_type{'All'} ) {
658 push @cust_main, qsearch( 'cust_main',
659 { 'company' => { 'op' => 'ILIKE',
660 'value' => "%$company%" } } );
662 push @cust_main, qsearch( 'cust_main',
663 { 'ship_company' => { 'op' => 'ILIKE',
664 'value' => "%$company%" } })
665 if defined dbdef->table('cust_main')->column('ship_last');
669 if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
670 push @cust_main, FS::cust_main::Search->fuzzy_search( { 'company' => $company } );
673 if ($company_type{'Sound-alike'}) {
682 $cgi->param('address2_text') =~
683 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
684 or errorpage(emt("Illegal address2"));
687 push @cust_main, qsearch( 'cust_main',
688 { 'address2' => { 'op' => 'ILIKE',
689 'value' => $address2 } } );
690 push @cust_main, qsearch( 'cust_main',
691 { 'ship_address2' => { 'op' => 'ILIKE',
692 'value' => $address2 } } );
700 my $phone = $cgi->param('phone_text');
703 if ( $phone =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/ ) {
705 $phone .= " x$4" if $4;
706 } elsif ( $phone =~ /^(\d{3})(\d{4})$/ ) {
708 } elsif ( $phone =~ /^(\d{3,4})$/ ) {
711 errorpage(gettext('illegal_phone'). ": $phone");
714 my @fields = qw(daytime night fax);
715 push @fields, qw(ship_daytime ship_night ship_fax)
716 if defined dbdef->table('cust_main')->column('ship_last');
718 for my $field ( @fields ) {
719 push @cust_main, qsearch ( 'cust_main',
720 { $field => { 'op' => 'LIKE',
721 'value' => "%$phone%" } } );