4724ce61fac43d9476b1bb406a030b2ded8c89d5
[freeside.git] / httemplate / search / cust_pkg.cgi
1 <& elements/search.html,
2                   'html_init'   => $html_init, 
3                   'title'       => emt('Package Search Results'), 
4                   'name'        => 'packages',
5                   'query'       => $sql_query,
6                   'count_query' => $count_query,
7                   'header'      => [ emt('#'),
8                                      emt('Quan.'),
9                                      emt('Package'),
10                                      emt('Class'),
11                                      emt('Status'),
12                                      emt('Sales Person'),
13                                      emt('Ordered by'),
14                                      emt('Setup'),
15                                      emt('Base Recur'),
16                                      emt('Freq.'),
17                                      emt('Setup'),
18                                      emt('Last bill'),
19                                      emt('Next bill'),
20                                      emt('Adjourn'),
21                                      emt('Susp.'),
22                                      emt('Susp. delay'),
23                                      emt('Expire'),
24                                      emt('Contract end'),
25                                      emt('Changed'),
26                                      emt('Cancel'),
27                                      emt('Reason'),
28                                      FS::UI::Web::cust_header(
29                                        $cgi->param('cust_fields')
30                                      ),
31                                      emt('Services'),
32                                    ],
33                   'fields'      => [
34                     'pkgnum',
35                     'quantity',
36                     sub { $_[0]->pkg; },
37                     'classname',
38                     sub { ucfirst(shift->status); },
39                     'salesperson',
40                     'otaker',
41                     sub { sprintf( $money_char.'%.2f',
42                                    shift->part_pkg->option('setup_fee'),
43                                  );
44                         },
45                     sub { my $c = shift;
46                           sprintf( $money_char.'%.2f',
47                                    $c->base_recur
48                                  );
49                         },
50                     sub { FS::part_pkg::freq_pretty(shift); },
51
52                     ( map { time_or_blank($_) }
53           qw( setup last_bill bill adjourn susp dundate expire contract_end change_date cancel ) ),
54
55                     sub { my $self = shift;
56                           my $return = '';
57                           foreach my $action ( qw ( cancel susp ) ) {
58                             my $reason = $self->last_reason($action);
59                             $return = $reason->reason if $reason;
60                             last if $return;
61                           }
62                           $return;
63                         },
64
65                     \&FS::UI::Web::cust_fields,
66                     sub {
67                       my $cust_pkg = shift;
68                       my $type = $cgi->param('_type') || '';
69                       if ($type =~ /xls|csv/) {
70                         my $cust_svc = $cust_pkg->primary_cust_svc;
71                         if($cust_svc) {
72                           return join ": ",($cust_svc->label)[0,1];
73                         }
74                         else {
75                           return '';
76                         }
77                       }
78                       else {
79                           [ $process_svc_labels->( $cust_pkg ) ]
80                       }
81                     }
82                   ],
83                   'color' => [
84                     '',
85                     '',
86                     '',
87                     '',
88                     sub { shift->statuscolor; },
89                     '',
90                     '',
91                     '',
92                     '',
93                     '',
94                     '',
95                     '',
96                     '',
97                     '',
98                     '',
99                     '',
100                     '',
101                     '',
102                     '',
103                     '',
104                     '',
105                     FS::UI::Web::cust_colors(),
106                     '',
107                   ],
108                   'style' => [ '', '', '', '', 'b', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
109                                FS::UI::Web::cust_styles() ],
110                   'size'  => [ '', '', '', '', '-1' ],
111                   'align' => 'rrlccccrrlrrrrrrrrrrl'. FS::UI::Web::cust_aligns(). 'r',
112                   'links' => [
113                     $link,
114                     $link,
115                     $link,
116                     '',
117                     '',
118                     '',
119                     '',
120                     '',
121                     '',
122                     '',
123                     '',
124                     '',
125                     '',
126                     '',
127                     '',
128                     '',
129                     '',
130                     '', # link to changed-from package?
131                     '',
132                     '',
133                     '',
134                     ( map { $_ ne 'Cust. Status' ? $clink : '' }
135                           FS::UI::Web::cust_header(
136                                                     $cgi->param('cust_fields')
137                                                   )
138                     ),
139                     '',
140                   ],
141 &>
142 <%init>
143
144 my $curuser = $FS::CurrentUser::CurrentUser;
145
146 die "access denied"
147   unless $curuser->access_right('List packages');
148
149 my $conf = new FS::Conf;
150 my $money_char = $conf->config('money_char') || '$';
151
152 my %search_hash = ();
153
154 #some false laziness w/misc/bulk_change_pkg.cgi and bulk_pkg_increment_bill.cgi
155 # and misc/process/
156 # with shitty results (bulk doing things to wrong packages) if you don't update
157 # those too :/
158   
159 $search_hash{'query'} = $cgi->keywords;
160
161 #scalars
162 for (qw( agentnum cust_status cust_main_salesnum salesnum custnum magic status
163          custom cust_fields pkgbatch zip
164          477part 477rownum date 
165     )) 
166 {
167   $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
168 }
169
170 #arrays
171 for my $param (qw( pkgpart classnum refnum towernum )) {
172   $search_hash{$param} = [ $cgi->param($param) ]
173     if grep { $_ eq $param } $cgi->param;
174 }
175
176 #scalars that need to be passed if empty
177 for my $param (qw( censustract censustract2 )) {
178   $search_hash{$param} = $cgi->param($param) || ''
179     if grep { $_ eq $param } $cgi->param;
180 }
181
182 #location flags (checkboxes)
183 my @loc = grep /^\w+$/, $cgi->param('loc');
184 $search_hash{"location_$_"} = 1 foreach @loc;
185
186 my $report_option = $cgi->param('report_option');
187 $search_hash{report_option} = $report_option if $report_option;
188
189 for my $param (grep /^report_option_any/, $cgi->param) {
190   $search_hash{$param} = $cgi->param($param);
191 }
192
193 ###
194 # parse dates
195 ###
196
197 #false laziness w/report_cust_pkg.html
198 my %disable = (
199   'all'             => {},
200   'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, 'contract_end'=>1, 'dundate'=>1, },
201   'active'          => { 'susp'=>1, 'cancel'=>1 },
202   'suspended'       => { 'cancel' =>1, 'dundate'=>1, },
203   'cancelled'       => {},
204   ''                => {},
205 );
206
207 foreach my $field (qw( setup last_bill bill adjourn susp expire contract_end change_date cancel active )) {
208
209   $search_hash{$field.'_null'} = scalar( $cgi->param($field.'_null') );
210
211   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
212
213   next if $beginning == 0 && $ending == 4294967295
214        or $disable{$cgi->param('status')}->{$field};
215
216   $search_hash{$field} = [ $beginning, $ending ];
217
218 }
219
220 my $sql_query = FS::cust_pkg->search(\%search_hash);
221 my $count_query = delete($sql_query->{'count_query'});
222
223 my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
224              ? ''
225              : ';show=packages';
226
227 my $link = sub {
228   my $self = shift;
229   my $frag = 'cust_pkg'. $self->pkgnum; #hack for IE ignoring real #fragment
230   [ "${p}view/cust_main.cgi?custnum=".$self->custnum.
231                            "$show;fragment=$frag#cust_pkg",
232     'pkgnum'
233   ];
234 };
235
236 my $clink = sub {
237   my $cust_pkg = shift;
238   $cust_pkg->cust_main_custnum
239     ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
240     : '';
241 };
242
243 sub time_or_blank {
244    my $column = shift;
245    return sub {
246      my $record = shift;
247      my $value = $record->get($column); #mmm closures
248      $value ? time2str('%b %d %Y', $value ) : '';
249    };
250 }
251
252 my $html_init = sub {
253   my $query = shift;
254   my $text = '';
255   my $curuser = $FS::CurrentUser::CurrentUser;
256
257   if ( $curuser->access_right('Bulk change customer packages') ) {
258     $text .= include('/elements/init_overlib.html').
259              include( '/elements/popup_link.html',
260                'label'       => emt('Change these packages'),
261                'action'      => "${p}misc/bulk_change_pkg.cgi?$query",
262                'actionlabel' => emt('Change Packages'),
263                'width'       => 569,
264                'height'      => 210,
265              ). '<BR>';
266
267     if ( $curuser->access_right('Edit customer package dates') ) {
268       $text .= include( '/elements/popup_link.html',
269                  'label'       => emt('Increment next bill date'),
270                  'action'      => "${p}misc/bulk_pkg_increment_bill.cgi?$query",
271                  'actionlabel' => emt('Increment Bill Date'),
272                  'width'       => 569,
273                  'height'      => 210,
274               ). '<BR>';
275     }
276     $text .= include( '/elements/email-link.html',
277                 'search_hash' => \%search_hash,
278                 'table'       => 'cust_pkg',
279                 ). '<BR><BR>';
280   }
281   return $text;
282 };
283
284 my $large_pkg_size = $conf->config('cust_pkg-large_pkg_size');
285
286 my $process_svc_labels = sub {
287   my $cust_pkg = shift;
288   my @out;
289   foreach my $part_svc ( $cust_pkg->part_svc) {
290     # some false laziness with view/cust_main/packages/services.html
291
292     my $num_cust_svc = $cust_pkg->num_cust_svc( $part_svc->svcpart );
293
294     if ( $large_pkg_size > 0 and $large_pkg_size <= $num_cust_svc ) {
295       my $href = $p.'search/cust_pkg_svc.html?svcpart='.$part_svc->svcpart.
296           ';pkgnum='.$cust_pkg->pkgnum;
297       push @out, [
298         { 'data'  => $part_svc->svc . ':',
299           'align' => 'right',
300           'rowspan' => 2 },
301         { 'data'  => mt('(view all [_1])', $num_cust_svc),
302           'data_style' => 'b',
303           'align' => 'left',
304           'link'  => $href, },
305       ],
306       [
307         { 'data'  => include('/elements/search-cust_svc.html',
308                         'svcpart' => $part_svc->svcpart,
309                         'pkgnum'  => $cust_pkg->pkgnum,
310                     ),
311           'align' => 'left' },
312       ];
313     }
314     else {
315       foreach ( map { [ $_->label ] } @{ $part_svc->cust_pkg_svc } ) {
316         push @out, [ 
317         { 'data' => $_->[0]. ':',
318           'align'=> 'right',
319         },
320
321         { 'data' => $_->[1],
322           'align'=> 'left',
323           'link' => $p. 'view/cust_svc.cgi?' . $_->[3],
324         },
325
326         ];
327       }
328     }
329   } #foreach $cust_pkg
330   return @out;
331 };
332
333 </%init>