add options for balance over/under to advanced customer report
[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 # amounts
73 ##
74
75 my $balance_sql = FS::cust_main->balance_sql();
76
77 push @where, map { s/current_balance/$balance_sql/; $_ }
78                  FS::UI::Web::parse_lt_gt($cgi, 'current_balance');
79
80 ##
81 # setup queries, subs, etc. for the search
82 ##
83
84 $orderby ||= 'ORDER BY custnum';
85
86 # here is the agent virtualization
87 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
88
89 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
90
91 my $addl_from = 'LEFT JOIN cust_pkg USING ( custnum  ) ';
92
93 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
94
95 my $select = join(', ', 
96                'cust_main.custnum',
97                FS::UI::Web::cust_sql_fields($cgi->param('cust_fields')),
98              );
99
100 my (@extra_headers) = ();
101 my (@extra_fields) = ();
102
103 if ($cgi->param('flattened_pkgs')) {
104
105   if ($dbh->{Driver}->{Name} eq 'Pg') {
106
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
109   }elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
110     $select .= ", GROUP_CONCAT(pkg SEPARATOR '|') as magic";
111     $addl_from .= " LEFT JOIN part_pkg using ( pkgpart )";
112   }else{
113     warn "warning: unknown database type ". $dbh->{Driver}->{Name}. 
114          "omitting packing information from report.";
115   }
116   
117   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";
118
119   my $sth = dbh->prepare($header_query) or die dbh->errstr;
120   $sth->execute() or die $sth->errstr;
121   my $headerrow = $sth->fetchrow_arrayref;
122   my $headercount = $headerrow ? $headerrow->[0] : 0;
123   while($headercount) {
124     unshift @extra_headers, "Package ". $headercount;
125     unshift @extra_fields, eval q!sub {my $c = shift;
126                                        my @a = split '\|', $c->magic;
127                                        my $p = $a[!.--$headercount. q!];
128                                        $p;
129                                       };!;
130   }
131
132 }
133
134 my $sql_query = {
135   'table'     => 'cust_main',
136   'select'    => $select,
137   'hashref'   => {},
138   'extra_sql' => "$extra_sql $orderby",
139 };
140
141
142 </%init>