1 %my $conf = new FS::Conf;
2 %my $maxrecords = $conf->config('maxsearchrecordsperpage');
6 %#my $monsterjoin = <<END;
7 %#cust_main left outer join (
8 %# ( cust_pkg left outer join part_pkg using(pkgpart)
12 %# ( cust_svc left outer join part_svc using (svcpart)
13 %# ) left outer join svc_acct using (svcnum)
14 %# ) left outer join svc_domain using(svcnum)
15 %# ) left outer join svc_forward using(svcnum)
20 %#my $monsterjoin = <<END;
21 %#cust_main left outer join (
22 %# ( cust_pkg left outer join part_pkg using(pkgpart)
23 %# ) left outer join (
26 %# ( cust_svc left outer join part_svc using (svcpart)
27 %# ) left outer join (
28 %# svc_acct left outer join (
29 %# select svcnum, domain, catchall from svc_domain
30 %# ) as svc_acct_domsvc (
31 %# svc_acct_svcnum, svc_acct_domain, svc_acct_catchall
32 %# ) on svc_acct.domsvc = svc_acct_domsvc.svc_acct_svcnum
34 %# ) left outer join svc_domain using(svcnum)
35 %# ) left outer join svc_forward using(svcnum)
41 %$limit .= "LIMIT $maxrecords" if $maxrecords;
43 %my $offset = $cgi->param('offset') || 0;
44 %$limit .= " OFFSET $offset" if $offset;
48 %my(@cust_main, $sortby, $orderby);
50 %my @addl_headers = ();
52 %if ( $cgi->param('browse')
53 % || $cgi->param('otaker_on')
54 % || $cgi->param('agentnum_on')
59 % if ( $cgi->param('browse') ) {
60 % my $query = $cgi->param('browse');
61 % if ( $query eq 'custnum' ) {
62 % $sortby=\*custnum_sort;
63 % $orderby = "ORDER BY custnum";
64 % } elsif ( $query eq 'last' ) {
65 % $sortby=\*last_sort;
66 % $orderby = "ORDER BY LOWER(last || ' ' || first)";
67 % } elsif ( $query eq 'company' ) {
68 % $sortby=\*company_sort;
69 % $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
70 % } elsif ( $query eq 'tickets' ) {
71 % $sortby = \*tickets_sort;
72 % $orderby = "ORDER BY tickets DESC";
73 % push @select, FS::TicketSystem->sql_num_customer_tickets. " as tickets";
74 % push @addl_headers, 'Tickets';
75 % push @addl_cols, 'tickets';
77 % die "unknown browse field $query";
80 % $sortby = \*last_sort; #??
81 % $orderby = "ORDER BY LOWER(last || ' ' || first)"; #??
84 % if ( $cgi->param('otaker_on') ) {
85 % $cgi->param('otaker') =~ /^(\w{1,32})$/ or eidiot "Illegal otaker\n";
86 % $search{otaker} = $1;
87 % } elsif ( $cgi->param('agentnum_on') ) {
88 % $cgi->param('agentnum') =~ /^(\d+)$/ or eidiot "Illegal agentnum\n";
89 % $search{agentnum} = $1;
91 %# die "unknown query...";
96 % my $ncancelled = '';
98 % if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
99 % || ( $conf->exists('hidecancelledcustomers')
100 % && ! $cgi->param('showcancelledcustomers') )
102 % #grep { $_->ncancelled_pkgs || ! $_->all_pkgs }
103 % push @qual, FS::cust_main->uncancel_sql;
107 % push @qual, FS::cust_main->cancel_sql if $cgi->param('cancelled');
108 % push @qual, FS::cust_main->prospect_sql if $cgi->param('prospect');
109 % push @qual, FS::cust_main->active_sql if $cgi->param('active');
110 % push @qual, FS::cust_main->inactive_sql if $cgi->param('inactive');
111 % push @qual, FS::cust_main->susp_sql if $cgi->param('suspended');
114 % my $qual = join(' AND ',
115 % map { "$_ = ". dbh->quote($search{$_}) } keys %search );
117 % my $addl_qual = join(' AND ', @qual);
119 % #here is the agent virtualization
120 % $addl_qual .= ( $addl_qual ? ' AND ' : '' ).
121 % $FS::CurrentUser::CurrentUser->agentnums_sql;
123 % if ( $addl_qual ) {
124 % $qual .= ' AND ' if $qual;
125 % $qual .= $addl_qual;
128 % $qual = " WHERE $qual" if $qual;
129 % my $statement = "SELECT COUNT(*) FROM cust_main $qual";
130 % my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
131 % $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
133 % $total = $sth->fetchrow_arrayref->[0];
135 % if ( $addl_qual ) {
137 % $addl_qual = " AND $addl_qual";
139 % $addl_qual = " WHERE $addl_qual";
145 % $select = 'cust_main.*, '. join (', ', @select);
150 % @cust_main = qsearch('cust_main', \%search, $select,
151 % "$addl_qual $orderby $limit" );
153 %# foreach my $cust_main ( @just_cust_main ) {
155 %# my @one_cust_main;
156 %# $FS::Record::DEBUG=1;
157 %# ( $cache, @one_cust_main ) = jsearch(
159 %# { 'custnum' => $cust_main->custnum },
165 %# push @cust_main, @one_cust_main;
170 % $sortby = \*last_sort;
172 % push @cust_main, @{&custnumsearch}
173 % if $cgi->param('custnum_on') && $cgi->param('custnum_text');
174 % push @cust_main, @{&cardsearch}
175 % if $cgi->param('card_on') && $cgi->param('card');
176 % push @cust_main, @{&lastsearch}
177 % if $cgi->param('last_on') && $cgi->param('last_text');
178 % push @cust_main, @{&companysearch}
179 % if $cgi->param('company_on') && $cgi->param('company_text');
180 % push @cust_main, @{&address2search}
181 % if $cgi->param('address2_on') && $cgi->param('address2_text');
182 % push @cust_main, @{&phonesearch}
183 % if $cgi->param('phone_on') && $cgi->param('phone_text');
184 % push @cust_main, @{&referralsearch}
185 % if $cgi->param('referral_custnum');
187 % if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
188 % $sortby = \*company_sort;
189 % push @cust_main, @{&companysearch};
192 % if ( $cgi->param('search_cust') ) {
193 % $sortby = \*company_sort;
194 % $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
195 % push @cust_main, smart_search( 'search' => $cgi->param('search_cust') );
198 % @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
199 % if ! $cgi->param('cancelled')
201 % $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
202 % || ( $conf->exists('hidecancelledcustomers')
203 % && ! $cgi->param('showcancelledcustomers') )
207 % @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
211 %if ( $conf->exists('hidecancelledpackages' ) ) {
212 % %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
214 % %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
218 %if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
219 % if ( $cgi->param('quickpay') eq 'yes' ) {
220 % print $cgi->redirect(popurl(2). "edit/cust_pay.cgi?quickpay=yes;custnum=". $cust_main[0]->custnum);
222 % print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
225 %} elsif ( scalar(@cust_main) == 0 ) {
228 <!-- mason kludge -->
230 % eidiot "No matching customers found!\n";
234 <% include('/elements/header.html', "Customer Search Results", '' ) %>
235 % $total ||= scalar(@cust_main);
238 <% $total %> matching customers found
240 % my $pager = include( '/elements/pager.html',
241 % 'offset' => $offset,
242 % 'num_rows' => scalar(@cust_main),
244 % 'maxrecords' => $maxrecords,
247 % unless ( $cgi->param('cancelled') ) {
248 % if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
249 % || ( $conf->exists('hidecancelledcustomers')
250 % && ! $cgi->param('showcancelledcustomers')
253 % $cgi->param('showcancelledcustomers', 1);
254 % $cgi->param('offset', 0);
255 % print qq!( <a href="!. $cgi->self_url. qq!">show!;
257 % $cgi->param('showcancelledcustomers', 0);
258 % $cgi->param('offset', 0);
259 % print qq!( <a href="!. $cgi->self_url. qq!">hide!;
261 % print ' cancelled customers</a> )';
264 % if ( $cgi->param('referral_custnum') ) {
265 % $cgi->param('referral_custnum') =~ /^(\d+)$/
266 % or eidiot "Illegal referral_custnum\n";
267 % my $referral_custnum = $1;
268 % my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
269 % print '<FORM METHOD="GET">'.
270 % qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
271 % 'referrals of <A HREF="'. popurl(2).
272 % "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
273 % ( $cust_main->company
274 % || $cust_main->last. ', '. $cust_main->first ).
278 % function changed(what) {
279 % what.form.submit();
283 % print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
284 % my $max = 8; #config file
285 % $cgi->param('referral_depth') =~ /^(\d*)$/
286 % or eidiot "Illegal referral_depth";
287 % my $referral_depth = $1;
289 % foreach my $depth ( 1 .. $max ) {
291 % ' SELECTED'x($depth == $referral_depth),
294 % print "</SELECT> levels deep".
295 % '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
299 % my @custom_priorities = ();
300 % if ( $conf->config('ticket_system-custom_priority_field')
301 % && @{[ $conf->config('ticket_system-custom_priority_field-values') ]} ) {
302 % @custom_priorities =
303 % $conf->config('ticket_system-custom_priority_field-values');
306 % print "<BR><BR>". $pager. include('/elements/table-grid.html'). <<END;
308 % <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
309 % <TH CLASS="grid" BGCOLOR="#cccccc">(bill) name</TH>
310 % <TH CLASS="grid" BGCOLOR="#cccccc">company</TH>
313 %if ( defined dbdef->table('cust_main')->column('ship_last') ) {
315 % <TH CLASS="grid" BGCOLOR="#cccccc">(service) name</TH>
316 % <TH CLASS="grid" BGCOLOR="#cccccc">company</TH>
320 %foreach my $addl_header ( @addl_headers ) {
321 % print '<TH CLASS="grid" BGCOLOR="#cccccc">'. "$addl_header</TH>";
325 % <TH CLASS="grid" BGCOLOR="#cccccc">Packages</TH>
326 % <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=2>Services</TH>
330 % my $bgcolor1 = '#eeeeee';
331 % my $bgcolor2 = '#ffffff';
334 % my(%saw,$cust_main);
335 % foreach $cust_main (
336 % sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
339 % if ( $bgcolor eq $bgcolor1 ) {
340 % $bgcolor = $bgcolor2;
342 % $bgcolor = $bgcolor1;
345 % my($custnum,$last,$first,$company)=(
346 % $cust_main->custnum,
347 % $cust_main->getfield('last'),
348 % $cust_main->getfield('first'),
349 % $cust_main->company,
353 % my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
354 % foreach ( @{$all_pkgs{$custnum}} ) {
355 % #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
356 % my @cust_svc = $_->cust_svc;
357 % push @lol_cust_svc, \@cust_svc;
358 % $rowspan += scalar(@cust_svc) || 1;
361 % #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
363 % if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
364 % $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
366 % $view = $p. 'view/cust_main.cgi?'. $custnum;
368 % my $pcompany = $company
369 % ? qq!<A HREF="$view"><FONT SIZE=-1>$company</FONT></A>!
370 % : '<FONT SIZE=-1> </FONT>';
375 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><A HREF="<% $view %>"><FONT SIZE=-1><% $custnum %></FONT></A></TD>
376 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><A HREF="<% $view %>"><FONT SIZE=-1><% "$last, $first" %></FONT></A></TD>
377 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><% $pcompany %></TD>
379 % if ( defined dbdef->table('cust_main')->column('ship_last') ) {
380 % my($ship_last,$ship_first,$ship_company)=(
381 % $cust_main->ship_last || $cust_main->getfield('last'),
382 % $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
383 % $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
385 % my $pship_company = $ship_company
386 % ? qq!<A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A>!
387 % : '<FONT SIZE=-1> </FONT>';
391 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><A HREF="<% $view %>"><FONT SIZE=-1><% "$ship_last, $ship_first" %></FONT></A></TD>
392 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><% $pship_company %></A></TD>
395 % foreach my $addl_col ( @addl_cols ) {
396 % if ( $addl_col eq 'tickets' ) {
397 % if ( @custom_priorities ) {
400 <TD CLASS="inv" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %> ALIGN=right><FONT SIZE=-1>
402 <TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>
403 % foreach my $priority ( @custom_priorities, '' ) {
406 % FS::TicketSystem->num_customer_tickets($custnum,$priority);
408 % $ahref= '<A HREF="'.
409 % FS::TicketSystem->href_customer_tickets($custnum,$priority).
417 <FONT SIZE=-1><% $ahref.$num %></A></FONT>
420 <FONT SIZE=-1><% $ahref %><% $priority || '<i>(none)</i>' %></A></FONT>
427 <TH ALIGN=right STYLE="border-top: dashed 1px black">
432 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %> ALIGN=right><FONT SIZE=-1>
436 % $ahref = '<A HREF="'.
437 % FS::TicketSystem->href_customer_tickets($custnum).
439 % if $cust_main->get($addl_col);
443 <% $ahref %><% $cust_main->get($addl_col) %></A>
444 % if ( @custom_priorities ) {
448 <TH ALIGN=left STYLE="border-top: dashed 1px black">
449 <FONT SIZE=-1><% ${ahref} %>Total</A><FONT>
460 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %> ALIGN=right><FONT SIZE=-1>
461 <% $cust_main->get($addl_col) %>
468 % foreach ( @{$all_pkgs{$custnum}} ) {
469 % my $pkgnum = $_->pkgnum;
470 %# my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
471 % my $part_pkg = $_->part_pkg;
473 % my $pkg = $part_pkg->pkg;
474 % my $comment = $part_pkg->comment;
475 % my $pkgview = "${p}view/cust_main.cgi?$custnum#cust_pkg$pkgnum";
476 % my @cust_svc = @{shift @lol_cust_svc};
477 % #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
478 % my $rowspan = scalar(@cust_svc) || 1;
480 % print $n1, qq!<TD CLASS="grid" BGCOLOR="$bgcolor" ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
483 % foreach my $cust_svc ( @cust_svc ) {
484 % my($label, $value, $svcdb) = $cust_svc->label;
485 % my($svcnum) = $cust_svc->svcnum;
486 % my($sview) = $p.'view';
488 % qq!<TD CLASS="grid" BGCOLOR="$bgcolor" >!. FS::UI::Web::svc_link($m, $cust_svc->part_svc, $cust_svc) . qq!</TD> !.
489 % qq!<TD CLASS="grid" BGCOLOR="$bgcolor" >!. FS::UI::Web::svc_label_link($m, $cust_svc->part_svc, $cust_svc) . qq!</TD> !;
493 % unless ( @cust_svc ) {
494 % print qq!<TD CLASS="grid" BGCOLOR="$bgcolor" COLSPAN=2> </TD>!;
497 % #print qq!</TR><TR>\n!;
501 % unless ( @{$all_pkgs{$custnum}} ) {
502 % print qq!<TD CLASS="grid" BGCOLOR="$bgcolor" COLSPAN=3> </TD>!;
513 <% include('/elements/footer.html') %>
516 %#undef $cache; #does this help?
521 % lc($a->getfield('last')) cmp lc($b->getfield('last'))
522 % || lc($a->first) cmp lc($b->first);
526 % return -1 if $a->company && ! $b->company;
527 % return 1 if ! $a->company && $b->company;
528 % lc($a->company) cmp lc($b->company)
529 % || lc($a->getfield('last')) cmp lc($b->getfield('last'))
530 % || lc($a->first) cmp lc($b->first);;
534 % $a->getfield('custnum') <=> $b->getfield('custnum');
538 % $b->getfield('tickets') <=> $a->getfield('tickets');
543 % my $custnum = $cgi->param('custnum_text');
544 % $custnum =~ s/\D//g;
545 % $custnum =~ /^(\d{1,23})$/ or eidiot "Illegal customer number\n";
548 % [ qsearchs('cust_main', { 'custnum' => $custnum } ) ];
553 % my($card)=$cgi->param('card');
555 % $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
558 % [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}),
559 % qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'DCRD'})
563 %sub referralsearch {
564 % $cgi->param('referral_custnum') =~ /^(\d+)$/
565 % or eidiot "Illegal referral_custnum";
566 % my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
567 % or eidiot "Customer $1 not found";
569 % if ( $cgi->param('referral_depth') ) {
570 % $cgi->param('referral_depth') =~ /^(\d+)$/
571 % or eidiot "Illegal referral_depth";
576 % [ $cust_main->referral_cust_main($depth) ];
582 % foreach ( $cgi->param('last_type') ) {
586 % $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
587 % or eidiot "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->fuzzy_search( { 'last' => $last } );
618 % #if ($last_type{'Sound-alike'}) {
628 % foreach ( $cgi->param('company_type') ) {
629 % $company_type{$_}++
632 % $cgi->param('company_text') =~
633 % /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
634 % or eidiot "Illegal company";
637 % if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
638 % push @cust_main, qsearch( 'cust_main',
639 % { 'company' => { 'op' => 'ILIKE',
640 % 'value' => $company } } );
642 % push @cust_main, qsearch( 'cust_main',
643 % { 'ship_company' => { 'op' => 'ILIKE',
644 % 'value' => $company } } )
645 % if defined dbdef->table('cust_main')->column('ship_last');
648 % if ( $company_type{'Substring'} || $company_type{'All'} ) {
650 % push @cust_main, qsearch( 'cust_main',
651 % { 'company' => { 'op' => 'ILIKE',
652 % 'value' => "%$company%" } } );
654 % push @cust_main, qsearch( 'cust_main',
655 % { 'ship_company' => { 'op' => 'ILIKE',
656 % 'value' => "%$company%" } })
657 % if defined dbdef->table('cust_main')->column('ship_last');
661 % if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
662 % push @cust_main, FS::cust_main->fuzzy_search( { 'company' => $company } );
665 % if ($company_type{'Sound-alike'}) {
671 %sub address2search {
674 % $cgi->param('address2_text') =~
675 % /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
676 % or eidiot "Illegal address2";
679 % push @cust_main, qsearch( 'cust_main',
680 % { 'address2' => { 'op' => 'ILIKE',
681 % 'value' => $address2 } } );
682 % push @cust_main, qsearch( 'cust_main',
683 % { 'address2' => { 'op' => 'ILIKE',
684 % 'value' => $address2 } } )
685 % if defined dbdef->table('cust_main')->column('ship_last');
693 % my $phone = $cgi->param('phone_text');
695 % #(no longer really) false laziness with Record::ut_phonen
696 % #only works with US/CA numbers...
698 % if ( $phone =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/ ) {
699 % $phone = "$1-$2-$3";
700 % $phone .= " x$4" if $4;
701 % } elsif ( $phone =~ /^(\d{3})(\d{4})$/ ) {
703 % } elsif ( $phone =~ /^(\d{3,4})$/ ) {
706 % eidiot gettext('illegal_phone'). ": $phone";
709 % my @fields = qw(daytime night fax);
710 % push @fields, qw(ship_daytime ship_night ship_fax)
711 % if defined dbdef->table('cust_main')->column('ship_last');
713 % for my $field ( @fields ) {
714 % push @cust_main, qsearch ( 'cust_main',
715 % { $field => { 'op' => 'LIKE',
716 % 'value' => "%$phone%" } } );