comma is a bad delimiter choice according to ut_text()
[freeside.git] / httemplate / search / cust_main_ADV.cgi
1 <% include( 'elements/search.html',
2                   'title'       => 'Customer Search Results', 
3                   'name'        => 'customers',
4                   'query'       => $sql_query,
5                   'count_query' => $count_query,
6                   'header'      => [ '#',
7                                      'Name',
8                                      'Address',
9                                      'Phone',
10                                      'Night',
11                                      'Fax',
12                                      'Email',
13                                      'Payment Type',
14                                      @extra_headers,
15                                    ],
16                   'fields'      => [
17                     'custnum',
18                     'name',
19                     sub { my $c = shift;
20                           $c->address1 .
21                           ($c->address2 ? ' '.$c->address2 : '').
22                           $c->city. ', '. $c->state. ' '. $c->zip.
23                           ($c->country ne $countrydefault ? ' '. $c->country
24                                                           : ''
25                           );
26                         },
27                     'daytime',
28                     'night',
29                     'fax',
30                     'email',
31                     'payby',
32                     @extra_fields,
33                   ],
34               )
35 %>
36 <%init>
37
38 die "access denied"
39   unless ( $FS::CurrentUser::CurrentUser->access_right('List customers') &&
40            $FS::CurrentUser::CurrentUser->access_right('List packages')
41          );
42
43 my $dbh = dbh;
44 my $conf = new FS::Conf;
45 my $countrydefault = $conf->config('countrydefault');
46
47 my($query) = $cgi->keywords;
48
49 my @where = ();
50
51 ##
52 # parse agent
53 ##
54
55 if ( $cgi->param('agentnum') =~ /^(\d+)$/ and $1 ) {
56   push @where,
57     "agentnum = $1";
58 }
59
60 ##
61 # parse cancelled package checkbox
62 ##
63
64 my $pkgwhere = "";
65
66 $pkgwhere .= "AND (cancel = 0 or cancel is null)"
67   unless $cgi->param('cancelled_pkgs');
68
69 my $orderby;
70
71 ##
72 # dates
73 ##
74
75 foreach my $field (qw( signupdate )) {
76
77   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
78
79   next if $beginning == 0 && $ending == 4294967295;
80
81   push @where,
82     "cust_main.$field IS NOT NULL",
83     "cust_main.$field >= $beginning",
84     "cust_main.$field <= $ending";
85
86   $orderby ||= "ORDER BY cust_main.$field";
87
88 }
89
90 ##
91 # setup queries, subs, etc. for the search
92 ##
93
94 $orderby ||= 'ORDER BY custnum';
95
96 # here is the agent virtualization
97 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
98
99 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
100
101 my $addl_from = 'LEFT JOIN cust_pkg USING ( custnum  ) ';
102
103 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
104
105 my $select;
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";
110 }else{
111   warn "warning: unknown database type ". $dbh->{Driver}->{Name}. 
112        "omitting packing information from report.";
113 }
114 my $sql_query = {
115   'table'     => 'cust_main',
116   'select'    => $select,
117   'hashref'   => {},
118   'extra_sql' => "$extra_sql $orderby",
119 };
120
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";
122
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!];
134                                      $p;
135                                     };!;
136 }
137
138 </%init>