46e35da7fe7366e155b6c7da23aa052941ef662d
[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   daytime night mobile fax
55   invoice_terms
56   no_censustract with_geocode with_email tax no_tax POST no_POST
57   custbatch usernum
58   cancelled_pkgs
59   cust_fields flattened_pkgs
60   all_tags
61   all_pkg_classnums
62   any_pkg_status
63   with_referrals referral_status
64 );
65
66 for my $param ( @scalars ) {
67   $search_hash{$param} = scalar( $cgi->param($param) )
68     if length($cgi->param($param));
69 }
70
71 #lists
72 for my $param (qw( classnum refnum pkg_classnum )) {
73   $search_hash{$param} = [ $cgi->param($param) ];
74 }
75
76 my $params = $cgi->Vars;
77
78 #contacts
79 $search_hash{'contacts'} = {
80   map { $_ => $cgi->param($_), }
81     grep { /^(contacts_*)/ && $cgi->param($_) }
82       keys %$params
83 };
84
85 #tags
86 $search_hash{'tagnum'} = [
87   map { /^tagnum(\d+)/ && $1 }
88     grep { /^tagnum(\d+)/ && $cgi->param($_) }
89       keys %$params
90 ];
91
92 ###
93 # parse dates
94 ###
95
96 foreach my $field (qw( signupdate birthdate spouse_birthdate anniversary_date )) {
97
98   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
99
100   next if $beginning == 0 && $ending == 4294967295 && ( $field ne 'signupdate' || !defined($cgi->param('signuphour')) );
101        #or $disable{$cgi->param('status')}->{$field};
102
103   unless ( $field eq 'signupdate' ) {
104     $beginning -= 43200;
105     $ending    -= 43200;
106   }
107
108   my @ary = ( $beginning, $ending );
109   push @ary, scalar($cgi->param('signuphour')) if $field eq 'signupdate';
110
111   $search_hash{$field} = \@ary;
112
113 }
114
115 ##
116 # amounts
117 ##
118
119 $search_hash{'current_balance'} =
120   [ FS::UI::Web::parse_lt_gt($cgi, 'current_balance') ];
121
122 ###
123 # etc
124 ###
125
126 my $sql_query = FS::cust_main::Search->search(\%search_hash);
127 my $count_query = delete($sql_query->{'count_query'});
128 my @extra_headers     = @{ delete($sql_query->{'extra_headers'}) };
129 my @extra_fields      = @{ delete($sql_query->{'extra_fields'})  };
130 my @extra_sort_fields = @{ delete($sql_query->{'extra_sort_fields'})  };
131
132 my $link = [ "${p}view/cust_main.cgi?", 'custnum' ];
133
134 my $referral_link = [ "${p}search/cust_main.cgi?referral_custnum=", 'custnum' ];
135
136 ###
137 # email links
138 ###
139
140 my $menubar = [];
141
142 if ( $FS::CurrentUser::CurrentUser->access_right('Bulk send customer notices') ) {
143
144   # set so invoice emails are default if showing that field.
145   $search_hash{'classnums'} = 'invoice' if $cgi->param('cust_fields') =~ /Invoicing email/;
146
147   # URI::query_from does not support hashref
148   #   results in: ...&contacts=HASH(0x55e16cb81da8)&...
149   my %query_hash = %search_hash;
150   foreach (keys %{$query_hash{contacts}}) { $query_hash{$_} = $query_hash{contacts}{$_}; }
151
152   delete $query_hash{contacts}
153     if exists $query_hash{contacts} && ref $query_hash{contacts};
154
155   my $uri = new URI;
156   $uri->query_form( \%query_hash );
157   my $query = $uri->query;
158
159   push @$menubar, emt('Email a notice to these customers message recipients') =>
160                     "${p}misc/email-customers.html?table=cust_main&classnums=message&$query";
161   push @$menubar, emt('Email a notice to these customers invoice recipients') =>
162                     "${p}misc/email-customers.html?table=cust_main&classnums=invoice&$query";
163 }
164
165 ###
166 # agent transfer link
167 ###
168
169 my $agent_transfer_link = include('/elements/agent_transfer_link.html', {
170      'search' => \%search_hash,
171 });
172 push @$menubar, emt('popup') => '<div style="display:inline-block">'.$agent_transfer_link.'</div>';
173
174 </%init>