more consistent naming: cust_main_ADV.cgi becomes cust_main.html
[freeside.git] / httemplate / search / cust_main.html
1 <% include( 'elements/search.html',
2                   'title'       => 'Customer Search Results', 
3                   'name'        => 'customers',
4                   'query'       => $sql_query,
5                   'count_query' => $count_query,
6                   'header'      => [ FS::UI::Web::cust_header(
7                                        $cgi->param('cust_fields')
8                                      ),
9                                      @extra_headers,
10                                    ],
11                   'fields'      => [
12                     \&FS::UI::Web::cust_fields,
13                     @extra_fields,
14                   ],
15               )
16 %>
17 <%init>
18
19 die "access denied"
20   unless ( $FS::CurrentUser::CurrentUser->access_right('List customers') &&
21            $FS::CurrentUser::CurrentUser->access_right('List packages')
22          );
23
24 my $dbh = dbh;
25 my $conf = new FS::Conf;
26 my $countrydefault = $conf->config('countrydefault');
27
28 my($query) = $cgi->keywords;
29
30 my @where = ();
31
32 ##
33 # parse agent
34 ##
35
36 if ( $cgi->param('agentnum') =~ /^(\d+)$/ and $1 ) {
37   push @where,
38     "agentnum = $1";
39 }
40
41 ##
42 # parse cancelled package checkbox
43 ##
44
45 my $pkgwhere = "";
46
47 $pkgwhere .= "AND (cancel = 0 or cancel is null)"
48   unless $cgi->param('cancelled_pkgs');
49
50 my $orderby;
51
52 ##
53 # dates
54 ##
55
56 foreach my $field (qw( signupdate )) {
57
58   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
59
60   next if $beginning == 0 && $ending == 4294967295;
61
62   push @where,
63     "cust_main.$field IS NOT NULL",
64     "cust_main.$field >= $beginning",
65     "cust_main.$field <= $ending";
66
67   $orderby ||= "ORDER BY cust_main.$field";
68
69 }
70
71 ##
72 # setup queries, subs, etc. for the search
73 ##
74
75 $orderby ||= 'ORDER BY custnum';
76
77 # here is the agent virtualization
78 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
79
80 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
81
82 my $addl_from = 'LEFT JOIN cust_pkg USING ( custnum  ) ';
83
84 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
85
86 my $select = '*';
87 my (@extra_headers) = ();
88 my (@extra_fields) = ();
89
90 if ($cgi->param('flattened_pkgs')) {
91
92   if ($dbh->{Driver}->{Name} eq 'Pg') {
93
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";
95
96   }elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
97     $select .= ", GROUP_CONCAT(pkg SEPARATOR '|') as magic";
98     $addl_from .= " LEFT JOIN part_pkg using ( pkgpart )";
99   }else{
100     warn "warning: unknown database type ". $dbh->{Driver}->{Name}. 
101          "omitting packing information from report.";
102   }
103   
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";
105
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!];
115                                        $p;
116                                       };!;
117   }
118
119 }
120
121 my $sql_query = {
122   'table'     => 'cust_main',
123   'select'    => $select,
124   'hashref'   => {},
125   'extra_sql' => "$extra_sql $orderby",
126 };
127
128
129 </%init>