RT# 77532 - Updated advanced customer report to search contacts
[freeside.git] / httemplate / search / cust_main.html
1 <& elements/search.html,
2                   'title'       => emt('Customer Search Results'), 
3                   'menubar'     => $menubar,
4                   'name'        => emt('customers'),
5                   'query'       => $sql_query,
6                   'count_query' => $count_query,
7                   'header'      => [ FS::UI::Web::cust_header(
8                                        $cgi->param('cust_fields')
9                                      ),
10                                      @extra_headers,
11                                    ],
12                   'fields'      => [
13                     \&FS::UI::Web::cust_fields,
14                     @extra_fields,
15                   ],
16                   'sort_fields' => [
17                     FS::UI::Web::cust_sort_fields(),
18                     @extra_sort_fields,
19                   ],
20                   'color'       => [ FS::UI::Web::cust_colors(),
21                                      map '', @extra_fields
22                                    ],
23                   'style'       => [ FS::UI::Web::cust_styles(),
24                                      map '', @extra_fields
25                                    ],
26                   'align'       => [ FS::UI::Web::cust_aligns(),
27                                      map '', @extra_fields
28                                    ],
29                   'links'       => [ ( map { $_ ne 'Cust. Status' ? $link : '' }
30                                            FS::UI::Web::cust_header(
31                                                       $cgi->param('cust_fields')
32                                                                    )
33                                      ),
34                                      map  { $_ eq 'num_referrals'
35                                               ? $referral_link
36                                               : ''
37                                           }
38                                        @extra_fields
39                                    ],
40 &>
41 <%init>
42
43 die "access denied"
44   unless $FS::CurrentUser::CurrentUser->access_right('Advanced customer search');
45
46 my %search_hash = ();
47
48 #$search_hash{'query'} = $cgi->keywords;
49
50 #scalars
51 my @scalars = qw (
52   agentnum salesnum status
53   address city county state zip country location_history
54   paydate_year paydate_month invoice_terms
55   no_censustract with_geocode with_email tax no_tax POST no_POST
56   custbatch usernum
57   cancelled_pkgs
58   cust_fields flattened_pkgs
59   all_tags
60   all_pkg_classnums
61   any_pkg_status
62   with_referrals referral_status
63 );
64
65 for my $param ( @scalars ) {
66   $search_hash{$param} = scalar( $cgi->param($param) )
67     if length($cgi->param($param));
68 }
69
70 #lists
71 for my $param (qw( classnum refnum payby tagnum pkg_classnum )) {
72   $search_hash{$param} = [ $cgi->param($param) ];
73 }
74
75 #contacts
76 $search_hash{'contacts'} = {
77   map { $_ => $cgi->param($_), } grep { /^(contacts_*)/ && $cgi->param($_) } keys $cgi->Vars
78 };
79
80 ###
81 # parse dates
82 ###
83
84 foreach my $field (qw( signupdate birthdate spouse_birthdate anniversary_date )) {
85
86   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
87
88   next if $beginning == 0 && $ending == 4294967295 && ( $field ne 'signupdate' || !defined($cgi->param('signuphour')) );
89        #or $disable{$cgi->param('status')}->{$field};
90
91   unless ( $field eq 'signupdate' ) {
92     $beginning -= 43200;
93     $ending    -= 43200;
94   }
95
96   my @ary = ( $beginning, $ending );
97   push @ary, scalar($cgi->param('signuphour')) if $field eq 'signupdate';
98
99   $search_hash{$field} = \@ary;
100
101 }
102
103 ##
104 # amounts
105 ##
106
107 $search_hash{'current_balance'} =
108   [ FS::UI::Web::parse_lt_gt($cgi, 'current_balance') ];
109
110 ###
111 # etc
112 ###
113
114 my $sql_query = FS::cust_main::Search->search(\%search_hash);
115 my $count_query = delete($sql_query->{'count_query'});
116 my @extra_headers     = @{ delete($sql_query->{'extra_headers'}) };
117 my @extra_fields      = @{ delete($sql_query->{'extra_fields'})  };
118 my @extra_sort_fields = @{ delete($sql_query->{'extra_sort_fields'})  };
119
120 my $link = [ "${p}view/cust_main.cgi?", 'custnum' ];
121
122 my $referral_link = [ "${p}search/cust_main.cgi?referral_custnum=", 'custnum' ];
123
124 ###
125 # email links
126 ###
127
128 my $menubar = [];
129
130 if ( $FS::CurrentUser::CurrentUser->access_right('Bulk send customer notices') ) {
131
132   my $uri = new URI;
133   $uri->query_form( \%search_hash );
134   my $query = $uri->query;
135
136   push @$menubar, emt('Email a notice to these customers') =>
137                     "${p}misc/email-customers.html?table=cust_main&$query",
138
139 }
140
141 </%init>