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