rework suspension delays, RT#6956
[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('Setup'),
13                                      emt('Base Recur'),
14                                      emt('Freq.'),
15                                      emt('Setup'),
16                                      emt('Last bill'),
17                                      emt('Next bill'),
18                                      emt('Adjourn'),
19                                      emt('Susp.'),
20                                      emt('Susp. delay'),
21                                      emt('Expire'),
22                                      emt('Contract end'),
23                                      emt('Cancel'),
24                                      emt('Reason'),
25                                      FS::UI::Web::cust_header(
26                                        $cgi->param('cust_fields')
27                                      ),
28                                      emt('Services'),
29                                    ],
30                   'fields'      => [
31                     'pkgnum',
32                     'quantity',
33                     sub { $_[0]->pkg; },
34                     'classname',
35                     sub { ucfirst(shift->status); },
36                     sub { sprintf( $money_char.'%.2f',
37                                    shift->part_pkg->option('setup_fee'),
38                                  );
39                         },
40                     sub { my $c = shift;
41                           sprintf( $money_char.'%.2f',
42                                    $c->part_pkg->base_recur($c)
43                                  );
44                         },
45                     sub { FS::part_pkg::freq_pretty(shift); },
46
47                     ( map { time_or_blank($_) }
48           qw( setup last_bill bill adjourn susp dundate expire contract_end cancel ) ),
49
50                     sub { my $self = shift;
51                           my $return = '';
52                           foreach my $action ( qw ( cancel susp ) ) {
53                             my $reason = $self->last_reason($action);
54                             $return = $reason->reason if $reason;
55                             last if $return;
56                           }
57                           $return;
58                         },
59
60                     \&FS::UI::Web::cust_fields,
61                     sub {
62                       my $cust_pkg = shift;
63                       my $type = $cgi->param('_type') || '';
64                       if ($type =~ /xls|csv/) {
65                         my $cust_svc = $cust_pkg->primary_cust_svc;
66                         if($cust_svc) {
67                           return join ": ",($cust_svc->label)[0,1];
68                         }
69                         else {
70                           return '';
71                         }
72                       }
73                       else {
74                           [ map {
75                                   [ 
76                                     { 'data' => $_->[0]. ':',
77                                       'align'=> 'right',
78                                     },
79                                     { 'data' => $_->[1],
80                                       'align'=> 'left',
81                                       'link' => $p. 'view/' .
82                                                 $_->[2]. '.cgi?'. $_->[3],
83                                     },
84                                   ];
85                                 } $cust_pkg->labels
86                           ];
87                       }
88                     }
89                   ],
90                   'color' => [
91                     '',
92                     '',
93                     '',
94                     '',
95                     sub { shift->statuscolor; },
96                     '',
97                     '',
98                     '',
99                     '',
100                     '',
101                     '',
102                     '',
103                     '',
104                     '',
105                     '',
106                     '',
107                     '',
108                     '',
109                     FS::UI::Web::cust_colors(),
110                     '',
111                   ],
112                   'style' => [ '', '', '', '', 'b', '', '', '', '', '', '', '', '', '', '', '', '', '',
113                                FS::UI::Web::cust_styles() ],
114                   'size'  => [ '', '', '', '', '-1' ],
115                   'align' => 'rrlccrrlrrrrrrrrrl'. FS::UI::Web::cust_aligns(). 'r',
116                   'links' => [
117                     $link,
118                     $link,
119                     $link,
120                     '',
121                     '',
122                     '',
123                     '',
124                     '',
125                     '',
126                     '',
127                     '',
128                     '',
129                     '',
130                     '',
131                     '',
132                     '',
133                     '',
134                     '',
135                     ( map { $_ ne 'Cust. Status' ? $clink : '' }
136                           FS::UI::Web::cust_header(
137                                                     $cgi->param('cust_fields')
138                                                   )
139                     ),
140                     '',
141                   ],
142 &>
143 <%init>
144
145 my $curuser = $FS::CurrentUser::CurrentUser;
146
147 die "access denied"
148   unless $curuser->access_right('List packages');
149
150 my $conf = new FS::Conf;
151 my $money_char = $conf->config('money_char') || '$';
152
153 my %search_hash = ();
154
155 #some false laziness w/misc/bulk_change_pkg.cgi
156   
157 $search_hash{'query'} = $cgi->keywords;
158
159 #scalars
160 for (qw( agentnum custnum magic status classnum custom cust_fields pkgbatch )) {
161   $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
162 }
163
164 $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
165
166 for my $param ( qw(censustract) ) {
167   $search_hash{$param} = $cgi->param($param) || ''
168     if ( grep { /$param/ } $cgi->param );
169 }
170
171 my @report_option = $cgi->param('report_option')
172   if $cgi->param('report_option');
173 $search_hash{report_option} = join(',', @report_option) if @report_option;
174
175 ###
176 # parse dates
177 ###
178
179 #false laziness w/report_cust_pkg.html
180 my %disable = (
181   'all'             => {},
182   'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, 'contract_end'=>1, 'dundate'=>1, },
183   'active'          => { 'susp'=>1, 'cancel'=>1 },
184   'suspended'       => { 'cancel' =>1, 'dundate'=>1, },
185   'cancelled'       => {},
186   ''                => {},
187 );
188
189 foreach my $field (qw( setup last_bill bill adjourn susp expire contract_end cancel active )) {
190
191   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
192
193   next if $beginning == 0 && $ending == 4294967295
194        or $disable{$cgi->param('status')}->{$field};
195
196   $search_hash{$field} = [ $beginning, $ending ];
197
198 }
199
200 my $sql_query = FS::cust_pkg->search(\%search_hash);
201 my $count_query = delete($sql_query->{'count_query'});
202
203 my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
204              ? ''
205              : ';show=packages';
206
207 my $link = sub {
208   my $self = shift;
209   my $frag = 'cust_pkg'. $self->pkgnum; #hack for IE ignoring real #fragment
210   [ "${p}view/cust_main.cgi?custnum=".$self->custnum.
211                            "$show;fragment=$frag#cust_pkg",
212     'pkgnum'
213   ];
214 };
215
216 my $clink = sub {
217   my $cust_pkg = shift;
218   $cust_pkg->cust_main_custnum
219     ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
220     : '';
221 };
222
223 sub time_or_blank {
224    my $column = shift;
225    return sub {
226      my $record = shift;
227      my $value = $record->get($column); #mmm closures
228      $value ? time2str('%b %d %Y', $value ) : '';
229    };
230 }
231
232 my $html_init = sub {
233   my $query = shift;
234   my $text = '';
235   my $curuser = $FS::CurrentUser::CurrentUser;
236
237   if ( $curuser->access_right('Bulk change customer packages') ) {
238     $text .= include('/elements/init_overlib.html').
239              include( '/elements/popup_link.html',
240                'label'       => emt('Change these packages'),
241                'action'      => "${p}misc/bulk_change_pkg.cgi?$query",
242                'actionlabel' => emt('Change Packages'),
243                'width'       => 569,
244                'height'      => 210,
245              ). '<BR>';
246
247     if ( $curuser->access_right('Edit customer package dates') ) {
248       $text .= include( '/elements/popup_link.html',
249                  'label'       => emt('Increment next bill date'),
250                  'action'      => "${p}misc/bulk_pkg_increment_bill.cgi?$query",
251                  'actionlabel' => emt('Increment Bill Date'),
252                  'width'       => 569,
253                  'height'      => 210,
254               ). '<BR>';
255     }
256     $text .= include( '/elements/email-link.html',
257                 'search_hash' => \%search_hash,
258                 'table'       => 'cust_pkg',
259                 );
260   }
261   return $text;
262 };
263
264 </%init>