1 <% include( 'elements/search.html',
2 'title' => 'Customer Search Results',
5 'count_query' => $count_query,
6 'header' => [ FS::UI::Web::cust_header(
7 $cgi->param('cust_fields')
12 \&FS::UI::Web::cust_fields,
20 unless ( $FS::CurrentUser::CurrentUser->access_right('List customers') &&
21 $FS::CurrentUser::CurrentUser->access_right('List packages')
25 my $conf = new FS::Conf;
26 my $countrydefault = $conf->config('countrydefault');
28 my($query) = $cgi->keywords;
36 if ( $cgi->param('agentnum') =~ /^(\d+)$/ and $1 ) {
42 # parse cancelled package checkbox
47 $pkgwhere .= "AND (cancel = 0 or cancel is null)"
48 unless $cgi->param('cancelled_pkgs');
56 foreach my $field (qw( signupdate )) {
58 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
60 next if $beginning == 0 && $ending == 4294967295;
63 "cust_main.$field IS NOT NULL",
64 "cust_main.$field >= $beginning",
65 "cust_main.$field <= $ending";
67 $orderby ||= "ORDER BY cust_main.$field";
72 # setup queries, subs, etc. for the search
75 $orderby ||= 'ORDER BY custnum';
77 # here is the agent virtualization
78 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
80 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
82 my $addl_from = 'LEFT JOIN cust_pkg USING ( custnum ) ';
84 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
87 my (@extra_headers) = ();
88 my (@extra_fields) = ();
90 if ($cgi->param('flattened_pkgs')) {
92 if ($dbh->{Driver}->{Name} eq 'Pg') {
94 $select .= ", array_to_string(array(select pkg from cust_pkg left join part_pkg using ( pkgpart ) where cust_main.custnum = cust_pkg.custnum $pkgwhere),'|') as magic";
96 }elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
97 $select .= ", GROUP_CONCAT(pkg SEPARATOR '|') as magic";
98 $addl_from .= " LEFT JOIN part_pkg using ( pkgpart )";
100 warn "warning: unknown database type ". $dbh->{Driver}->{Name}.
101 "omitting packing information from report.";
104 my $header_query = "SELECT COUNT(cust_pkg.custnum = cust_main.custnum) AS count FROM cust_main $addl_from $extra_sql $pkgwhere group by cust_main.custnum order by count desc limit 1";
106 my $sth = dbh->prepare($header_query) or die dbh->errstr;
107 $sth->execute() or die $sth->errstr;
108 my $headerrow = $sth->fetchrow_arrayref;
109 my $headercount = $headerrow ? $headerrow->[0] : 0;
110 while($headercount) {
111 unshift @extra_headers, "Package ". $headercount;
112 unshift @extra_fields, eval q!sub {my $c = shift;
113 my @a = split '\|', $c->magic;
114 my $p = $a[!.--$headercount. q!];
122 'table' => 'cust_main',
125 'extra_sql' => "$extra_sql $orderby",