This commit was generated by cvs2svn to compensate for changes in r6252,
[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                   'color'       => [ FS::UI::Web::cust_colors(),
16                                      map '', @extra_fields
17                                    ],
18                   'style'       => [ FS::UI::Web::cust_styles(),
19                                      map '', @extra_fields
20                                    ],
21                   'align'       => [ FS::UI::Web::cust_aligns(),
22                                      map '', @extra_fields
23                                    ],
24                   'links'       => [ ( map { $_ ne 'Cust. Status' ? $link : '' }
25                                            FS::UI::Web::cust_header(
26                                                       $cgi->param('cust_fields')
27                                                                    )
28                                      ),
29                                      map '', @extra_fields
30                                    ],
31               )
32 %>
33 <%once>
34
35 my $link = [ "${p}view/cust_main.cgi?", 'custnum' ];
36
37 </%once>
38 <%init>
39
40 die "access denied"
41   unless ( $FS::CurrentUser::CurrentUser->access_right('List customers') &&
42            $FS::CurrentUser::CurrentUser->access_right('List packages')
43          );
44
45 my $dbh = dbh;
46 my $conf = new FS::Conf;
47 my $countrydefault = $conf->config('countrydefault');
48
49 my($query) = $cgi->keywords;
50
51 my @where = ();
52
53 ##
54 # parse agent
55 ##
56
57 if ( $cgi->param('agentnum') =~ /^(\d+)$/ and $1 ) {
58   push @where,
59     "agentnum = $1";
60 }
61
62 ##
63 # parse cancelled package checkbox
64 ##
65
66 my $pkgwhere = "";
67
68 $pkgwhere .= "AND (cancel = 0 or cancel is null)"
69   unless $cgi->param('cancelled_pkgs');
70
71 my $orderby;
72
73 ##
74 # dates
75 ##
76
77 foreach my $field (qw( signupdate )) {
78
79   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
80
81   next if $beginning == 0 && $ending == 4294967295;
82
83   push @where,
84     "cust_main.$field IS NOT NULL",
85     "cust_main.$field >= $beginning",
86     "cust_main.$field <= $ending";
87
88   $orderby ||= "ORDER BY cust_main.$field";
89
90 }
91
92 ###
93 # payby
94 ###
95
96 my @payby = grep /^([A-Z]{4})$/, $cgi->param('payby');
97 if ( @payby ) {
98   push @where, '( '. join(' OR ', map "cust_main.payby = '$_'", @payby). ' )';
99 }
100
101 ##
102 # amounts
103 ##
104
105 my $balance_sql = FS::cust_main->balance_sql();
106
107 push @where, map { s/current_balance/$balance_sql/; $_ }
108                  FS::UI::Web::parse_lt_gt($cgi, 'current_balance');
109
110 ##
111 # setup queries, subs, etc. for the search
112 ##
113
114 $orderby ||= 'ORDER BY custnum';
115
116 # here is the agent virtualization
117 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
118
119 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
120
121 my $addl_from = 'LEFT JOIN cust_pkg USING ( custnum  ) ';
122
123 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
124
125 my $select = join(', ', 
126                'cust_main.custnum',
127                FS::UI::Web::cust_sql_fields($cgi->param('cust_fields')),
128              );
129
130 my (@extra_headers) = ();
131 my (@extra_fields) = ();
132
133 if ($cgi->param('flattened_pkgs')) {
134
135   if ($dbh->{Driver}->{Name} eq 'Pg') {
136
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";
138
139   }elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
140     $select .= ", GROUP_CONCAT(pkg SEPARATOR '|') as magic";
141     $addl_from .= " LEFT JOIN part_pkg using ( pkgpart )";
142   }else{
143     warn "warning: unknown database type ". $dbh->{Driver}->{Name}. 
144          "omitting packing information from report.";
145   }
146   
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";
148
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!];
158                                        $p;
159                                       };!;
160   }
161
162 }
163
164 my $sql_query = {
165   'table'     => 'cust_main',
166   'select'    => $select,
167   'hashref'   => {},
168   'extra_sql' => "$extra_sql $orderby",
169 };
170
171
172 </%init>