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,
123 % my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
124 % foreach ( @{$all_pkgs{$custnum}} ) {
125 % my @cust_svc = $_->cust_svc;
126 % push @lol_cust_svc, \@cust_svc;
127 % $rowspan += scalar(@cust_svc) || 1;
131 % if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
132 % $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
134 % $view = $p. 'view/cust_main.cgi?'. $custnum;
136 % my $pcompany = $company
137 % ? qq!<A HREF="$view"><FONT SIZE=-1>$company</FONT></A>!
138 % : '<FONT SIZE=-1> </FONT>';
140 % my $status = $cust_main->status;
141 % my $statuscol = $cust_main->statuscolor;
144 <TD CLASS="grid" ALIGN="right" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>>
145 <A HREF="<% $view %>"><FONT SIZE=-1><% $cust_main->display_custnum %></FONT></A>
147 <TD CLASS="grid" ALIGN="center" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>>
148 <FONT SIZE="-1" COLOR="#<% $statuscol %>"><B><% ucfirst($status) %></B></FONT>
150 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>>
151 <A HREF="<% $view %>"><FONT SIZE=-1><% "$last, $first" %></FONT></A>
153 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>>
157 % if ( defined dbdef->table('cust_main')->column('ship_last') ) {
158 % my($ship_last,$ship_first,$ship_company)=(
159 % $cust_main->ship_last || $cust_main->getfield('last'),
160 % $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
161 % $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
163 % my $pship_company = $ship_company
164 % ? qq!<A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A>!
165 % : '<FONT SIZE=-1> </FONT>';
168 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>>
169 <A HREF="<% $view %>"><FONT SIZE=-1><% "$ship_last, $ship_first" %></FONT></A>
171 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>>
172 <% $pship_company %></A>
176 % foreach my $addl_col ( @addl_cols ) {
177 % if ( $addl_col eq 'tickets' ) {
178 % if ( @custom_priorities ) {
180 <TD CLASS="inv" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %> ALIGN=right>
182 <TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>
183 % foreach my $priority ( @custom_priorities, '' ) {
185 % FS::TicketSystem->num_customer_tickets($custnum,$priority);
187 % $ahref= '<A HREF="'.
188 % FS::TicketSystem->href_customer_tickets($custnum,$priority).
194 <FONT SIZE=-1><% $ahref.$num %></A></FONT>
197 <FONT SIZE=-1><% $ahref %><% $priority || '<i>('.emt('none').')</i>' %></A></FONT>
203 <TH ALIGN=right STYLE="border-top: dashed 1px black">
206 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %> ALIGN=right>
210 % $ahref = '<A HREF="'.
211 % FS::TicketSystem->href_customer_tickets($custnum).
213 % if $cust_main->get($addl_col);
216 <% $ahref %><% $cust_main->get($addl_col) %></A>
217 % if ( @custom_priorities ) {
220 <TH ALIGN=left STYLE="border-top: dashed 1px black">
221 <FONT SIZE=-1><% ${ahref} %>Total</A><FONT>
230 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %> ALIGN=right><FONT SIZE=-1>
231 <% $cust_main->get($addl_col) %>
238 % foreach ( @{$all_pkgs{$custnum}} ) {
239 % my $pkgnum = $_->pkgnum;
240 % my $part_pkg = $_->part_pkg;
242 % my $pkg_comment = $part_pkg->pkg_comment(nopkgpart => 1);
243 % my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
245 % : ';show=packages';
246 % my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
247 % my $pkgview = "${p}view/cust_main.cgi?custnum=$custnum$show;fragment=$frag#$frag";
248 % my @cust_svc = @{shift @lol_cust_svc};
249 % my $rowspan = scalar(@cust_svc) || 1;
251 <% $n1 %><TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN="<% $rowspan%>">
252 <A HREF="<% $pkgview %>"><FONT SIZE=-1><% $pkg_comment %></FONT></A>
256 % foreach my $cust_svc ( @cust_svc ) {
257 % my($label, $value, $svcdb) = $cust_svc->label;
258 % my($svcnum) = $cust_svc->svcnum;
259 % my($sview) = $p.'view';
261 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
262 <% FS::UI::Web::svc_link($m, $cust_svc->part_svc, $cust_svc) %>
264 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
265 <% FS::UI::Web::svc_label_link($m, $cust_svc->part_svc, $cust_svc) %>
270 % unless ( @cust_svc ) {
271 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" COLSPAN=2> </TD>
277 % unless ( @{$all_pkgs{$custnum}} ) {
278 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" COLSPAN=3> </TD>!;
286 <& /elements/footer.html &>
288 my $curuser = $FS::CurrentUser::CurrentUser;
291 unless $curuser->access_right('List customers');
293 my $conf = new FS::Conf;
294 my $maxrecords = $conf->config('maxsearchrecordsperpage');
297 $limit .= "LIMIT $maxrecords" if $maxrecords;
299 my $offset = $cgi->param('offset') || 0;
300 $limit .= " OFFSET $offset" if $offset;
304 my(@cust_main, $sortby, $orderby);
306 my @addl_headers = ();
308 if ( $cgi->param('browse')
309 || $cgi->param('otaker_on')
310 || $cgi->param('agentnum_on')
315 if ( $cgi->param('browse') ) {
316 my $query = $cgi->param('browse');
317 if ( $query eq 'custnum' ) {
318 if ( $conf->exists('cust_main-default_agent_custid') ) {
319 $sortby=\*display_custnum_sort;
320 $orderby = "ORDER BY CASE WHEN agent_custid IS NOT NULL AND agent_custid != '' THEN CAST(agent_custid AS BIGINT) ELSE custnum END";
322 $sortby=\*custnum_sort;
323 $orderby = "ORDER BY custnum";
325 } elsif ( $query eq 'last' ) {
327 $orderby = "ORDER BY LOWER(last || ' ' || first)";
328 } elsif ( $query eq 'company' ) {
329 $sortby=\*company_sort;
330 $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
331 } elsif ( $query eq 'tickets' ) {
332 $sortby = \*tickets_sort;
333 $orderby = "ORDER BY tickets DESC";
334 push @select, FS::TicketSystem->sql_num_customer_tickets. " as tickets";
335 push @addl_headers, 'Tickets';
336 push @addl_cols, 'tickets';
337 } elsif ( $query eq 'uspsunvalid' ) {
338 $search{'country'} = 'US';
339 $sortby=\*custnum_sort;
340 $orderby = "ORDER BY custnum";
342 die "unknown browse field $query";
345 $sortby = \*last_sort; #??
346 $orderby = "ORDER BY LOWER(last || ' ' || first)"; #??
349 if ( $cgi->param('otaker_on') ) {
351 unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
352 $cgi->param('otaker') =~ /^(\w{1,32})$/ or errorpage("Illegal otaker");
353 $search{otaker} = $1;
354 } elsif ( $cgi->param('agentnum_on') ) {
355 $cgi->param('agentnum') =~ /^(\d+)$/ or errorpage("Illegal agentnum");
356 $search{agentnum} = $1;
363 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
364 || ( $conf->exists('hidecancelledcustomers')
365 && ! $cgi->param('showcancelledcustomers') )
367 push @qual, FS::cust_main->uncancel_sql;
370 push @qual, FS::cust_main->cancel_sql if $cgi->param('cancelled');
371 push @qual, FS::cust_main->prospect_sql if $cgi->param('prospect');
372 push @qual, FS::cust_main->active_sql if $cgi->param('active');
373 push @qual, FS::cust_main->inactive_sql if $cgi->param('inactive');
374 push @qual, FS::cust_main->susp_sql if $cgi->param('suspended');
377 my $qual = join(' AND ',
378 map { "$_ = ". dbh->quote($search{$_}) } keys %search );
380 my $addl_qual = join(' AND ', @qual);
382 #here is the agent virtualization
383 $addl_qual .= ( $addl_qual ? ' AND ' : '' ).
384 $FS::CurrentUser::CurrentUser->agentnums_sql;
386 if ( $cgi->param('browse') && $cgi->param('browse') eq 'uspsunvalid' ) {
387 $addl_qual .= ' AND ( length(zip) < 9 OR upper(address1) != address1 OR upper(city) != city ) ';
391 $qual .= ' AND ' if $qual;
395 $qual = " WHERE $qual" if $qual;
396 my $statement = "SELECT COUNT(*) FROM cust_main $qual";
397 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
398 $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
400 $total = $sth->fetchrow_arrayref->[0];
404 $addl_qual = " AND $addl_qual";
406 $addl_qual = " WHERE $addl_qual";
412 $select = 'cust_main.*, '. join (', ', @select);
417 @cust_main = qsearch('cust_main', \%search, $select,
418 "$addl_qual $orderby $limit" );
422 $sortby = \*last_sort;
424 push @cust_main, @{&custnumsearch}
425 if $cgi->param('custnum_on') && $cgi->param('custnum_text');
426 push @cust_main, @{&cardsearch}
427 if $cgi->param('card_on') && $cgi->param('card');
428 push @cust_main, @{&lastsearch}
429 if $cgi->param('last_on') && $cgi->param('last_text');
430 push @cust_main, @{&companysearch}
431 if $cgi->param('company_on') && $cgi->param('company_text');
432 push @cust_main, @{&address2search}
433 if $cgi->param('address2_on') && $cgi->param('address2_text');
434 push @cust_main, @{&phonesearch}
435 if $cgi->param('phone_on') && $cgi->param('phone_text');
436 push @cust_main, @{&referralsearch}
437 if $cgi->param('referral_custnum');
439 if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
440 $sortby = \*company_sort;
441 push @cust_main, @{&companysearch};
444 if ( $cgi->param('search_cust') ) {
445 $sortby = \*company_sort;
446 $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
447 push @cust_main, smart_search( 'search' => scalar($cgi->param('search_cust')),
448 'no_fuzzy_on_exact' => 1, #pref?
452 @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
453 if ! $cgi->param('cancelled')
455 $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
456 || ( $conf->exists('hidecancelledcustomers')
457 && ! $cgi->param('showcancelledcustomers') )
461 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
465 if ( $conf->exists('hidecancelledpackages' ) ) {
466 %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
468 %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
472 lc($a->getfield('last')) cmp lc($b->getfield('last'))
473 || lc($a->first) cmp lc($b->first);
477 return -1 if $a->company && ! $b->company;
478 return 1 if ! $a->company && $b->company;
479 lc($a->company) cmp lc($b->company)
480 || lc($a->getfield('last')) cmp lc($b->getfield('last'))
481 || lc($a->first) cmp lc($b->first);;
484 sub display_custnum_sort {
485 $a->display_custnum <=> $b->display_custnum;
489 $a->getfield('custnum') <=> $b->getfield('custnum');
493 $b->getfield('tickets') <=> $a->getfield('tickets');
498 my $custnum = $cgi->param('custnum_text');
500 $custnum =~ /^(\d{1,23})$/ or errorpage(emt("Illegal customer number"));
503 [ qsearchs('cust_main', { 'custnum' => $custnum } ) ];
508 my($card)=$cgi->param('card');
510 $card =~ /^(\d{13,16})$/ or errorpage(emt("Illegal card number"));
513 [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}),
514 qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'DCRD'})
519 $cgi->param('referral_custnum') =~ /^(\d+)$/
520 or errorpage("Illegal referral_custnum");
521 my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
522 or errorpage(emt("Customer [_1] not found",$1));
524 if ( $cgi->param('referral_depth') ) {
525 $cgi->param('referral_depth') =~ /^(\d+)$/
526 or errorpage(emt("Illegal referral_depth"));
531 [ $cust_main->referral_cust_main($depth) ];
537 foreach ( $cgi->param('last_type') ) {
541 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
542 or errorpage(emt("Illegal last name"));
545 if ( $last_type{'Exact'} || $last_type{'Fuzzy'} ) {
546 push @cust_main, qsearch( 'cust_main',
547 { 'last' => { 'op' => 'ILIKE',
548 'value' => $last } } );
550 push @cust_main, qsearch( 'cust_main',
551 { 'ship_last' => { 'op' => 'ILIKE',
552 'value' => $last } } )
553 if defined dbdef->table('cust_main')->column('ship_last');
556 if ( $last_type{'Substring'} || $last_type{'All'} ) {
558 push @cust_main, qsearch( 'cust_main',
559 { 'last' => { 'op' => 'ILIKE',
560 'value' => "%$last%" } } );
562 push @cust_main, qsearch( 'cust_main',
563 { 'ship_last' => { 'op' => 'ILIKE',
564 'value' => "%$last%" } } )
565 if defined dbdef->table('cust_main')->column('ship_last');
569 if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
570 push @cust_main, FS::cust_main::Search->fuzzy_search( { 'last' => $last } );
580 foreach ( $cgi->param('company_type') ) {
584 $cgi->param('company_text') =~
585 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
586 or errorpage(emt("Illegal company"));
589 if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
590 push @cust_main, qsearch( 'cust_main',
591 { 'company' => { 'op' => 'ILIKE',
592 'value' => $company } } );
594 push @cust_main, qsearch( 'cust_main',
595 { 'ship_company' => { 'op' => 'ILIKE',
596 'value' => $company } } )
597 if defined dbdef->table('cust_main')->column('ship_last');
600 if ( $company_type{'Substring'} || $company_type{'All'} ) {
602 push @cust_main, qsearch( 'cust_main',
603 { 'company' => { 'op' => 'ILIKE',
604 'value' => "%$company%" } } );
606 push @cust_main, qsearch( 'cust_main',
607 { 'ship_company' => { 'op' => 'ILIKE',
608 'value' => "%$company%" } })
609 if defined dbdef->table('cust_main')->column('ship_last');
613 if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
614 push @cust_main, FS::cust_main::Search->fuzzy_search( { 'company' => $company } );
617 if ($company_type{'Sound-alike'}) {
626 $cgi->param('address2_text') =~
627 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
628 or errorpage(emt("Illegal address2"));
631 push @cust_main, qsearch( 'cust_main',
632 { 'address2' => { 'op' => 'ILIKE',
633 'value' => $address2 } } );
634 push @cust_main, qsearch( 'cust_main',
635 { 'ship_address2' => { 'op' => 'ILIKE',
636 'value' => $address2 } } );
644 my $phone = $cgi->param('phone_text');
647 if ( $phone =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/ ) {
649 $phone .= " x$4" if $4;
650 } elsif ( $phone =~ /^(\d{3})(\d{4})$/ ) {
652 } elsif ( $phone =~ /^(\d{3,4})$/ ) {
655 errorpage(gettext('illegal_phone'). ": $phone");
658 my @fields = qw(daytime night fax);
659 push @fields, qw(ship_daytime ship_night ship_fax)
660 if defined dbdef->table('cust_main')->column('ship_last');
662 for my $field ( @fields ) {
663 push @cust_main, qsearch ( 'cust_main',
664 { $field => { 'op' => 'LIKE',
665 'value' => "%$phone%" } } );