1 <% include( 'elements/search.html',
2 'title' => 'Customer Search Results',
5 'count_query' => $count_query,
21 ($c->address2 ? ' '.$c->address2 : '').
22 $c->city. ', '. $c->state. ' '. $c->zip.
23 ($c->country ne $countrydefault ? ' '. $c->country
39 unless ( $FS::CurrentUser::CurrentUser->access_right('List customers') &&
40 $FS::CurrentUser::CurrentUser->access_right('List packages')
44 my $conf = new FS::Conf;
45 my $countrydefault = $conf->config('countrydefault');
47 my($query) = $cgi->keywords;
55 if ( $cgi->param('agentnum') =~ /^(\d+)$/ and $1 ) {
61 # parse cancelled package checkbox
66 $pkgwhere .= "AND (cancel = 0 or cancel is null)"
67 unless $cgi->param('cancelled_pkgs');
75 foreach my $field (qw( signupdate )) {
77 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
79 next if $beginning == 0 && $ending == 4294967295;
82 "cust_main.$field IS NOT NULL",
83 "cust_main.$field >= $beginning",
84 "cust_main.$field <= $ending";
86 $orderby ||= "ORDER BY cust_main.$field";
91 # setup queries, subs, etc. for the search
94 $orderby ||= 'ORDER BY custnum';
96 # here is the agent virtualization
97 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
99 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
101 my $addl_from = 'LEFT JOIN cust_pkg USING ( custnum ) ';
103 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
106 if ($dbh->{Driver}->{Name} eq 'Pg') {
107 $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";
108 }elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
109 $select = "*, GROUP_CONCAT(pkg SEPARATOR '|') as magic";
111 warn "warning: unknown database type ". $dbh->{Driver}->{Name}.
112 "omitting packing information from report.";
115 'table' => 'cust_main',
118 'extra_sql' => "$extra_sql $orderby",
121 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";
123 my $sth = dbh->prepare($header_query) or die dbh->errstr;
124 $sth->execute() or die $sth->errstr;
125 my $headerrow = $sth->fetchrow_arrayref;
126 my $headercount = $headerrow ? $headerrow->[0] : 0;
127 my (@extra_headers) = ();
128 my (@extra_fields) = ();
129 while($headercount) {
130 unshift @extra_headers, "Package ". $headercount;
131 unshift @extra_fields, eval q!sub {my $c = shift;
132 my @a = split '\|', $c->magic;
133 my $p = $a[!.--$headercount. q!];