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,
15 'color' => [ FS::UI::Web::cust_colors(),
18 'style' => [ FS::UI::Web::cust_styles(),
21 'align' => [ FS::UI::Web::cust_aligns(),
24 'links' => [ ( map { $_ ne 'Cust. Status' ? $link : '' }
25 FS::UI::Web::cust_header(
26 $cgi->param('cust_fields')
35 my $link = [ "${p}view/cust_main.cgi?", 'custnum' ];
41 unless ( $FS::CurrentUser::CurrentUser->access_right('List customers') &&
42 $FS::CurrentUser::CurrentUser->access_right('List packages')
46 my $conf = new FS::Conf;
47 my $countrydefault = $conf->config('countrydefault');
49 my($query) = $cgi->keywords;
57 if ( $cgi->param('agentnum') =~ /^(\d+)$/ and $1 ) {
63 # parse cancelled package checkbox
68 $pkgwhere .= "AND (cancel = 0 or cancel is null)"
69 unless $cgi->param('cancelled_pkgs');
77 foreach my $field (qw( signupdate )) {
79 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
81 next if $beginning == 0 && $ending == 4294967295;
84 "cust_main.$field IS NOT NULL",
85 "cust_main.$field >= $beginning",
86 "cust_main.$field <= $ending";
88 $orderby ||= "ORDER BY cust_main.$field";
96 my @payby = grep /^([A-Z]{4})$/, $cgi->param('payby');
98 push @where, '( '. join(' OR ', map "cust_main.payby = '$_'", @payby). ' )';
105 my $balance_sql = FS::cust_main->balance_sql();
107 push @where, map { s/current_balance/$balance_sql/; $_ }
108 FS::UI::Web::parse_lt_gt($cgi, 'current_balance');
111 # setup queries, subs, etc. for the search
114 $orderby ||= 'ORDER BY custnum';
116 # here is the agent virtualization
117 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
119 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
121 my $addl_from = 'LEFT JOIN cust_pkg USING ( custnum ) ';
123 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
125 my $select = join(', ',
127 FS::UI::Web::cust_sql_fields($cgi->param('cust_fields')),
130 my (@extra_headers) = ();
131 my (@extra_fields) = ();
133 if ($cgi->param('flattened_pkgs')) {
135 if ($dbh->{Driver}->{Name} eq 'Pg') {
137 $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";
139 }elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
140 $select .= ", GROUP_CONCAT(pkg SEPARATOR '|') as magic";
141 $addl_from .= " LEFT JOIN part_pkg using ( pkgpart )";
143 warn "warning: unknown database type ". $dbh->{Driver}->{Name}.
144 "omitting packing information from report.";
147 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";
149 my $sth = dbh->prepare($header_query) or die dbh->errstr;
150 $sth->execute() or die $sth->errstr;
151 my $headerrow = $sth->fetchrow_arrayref;
152 my $headercount = $headerrow ? $headerrow->[0] : 0;
153 while($headercount) {
154 unshift @extra_headers, "Package ". $headercount;
155 unshift @extra_fields, eval q!sub {my $c = shift;
156 my @a = split '\|', $c->magic;
157 my $p = $a[!.--$headercount. q!];
165 'table' => 'cust_main',
168 'extra_sql' => "$extra_sql $orderby",