This commit was generated by cvs2svn to compensate for changes in r11022,
[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                           [ map {
97                                   [ 
98                                     { 'data' => $_->[0]. ':',
99                                       'align'=> 'right',
100                                     },
101                                     { 'data' => $_->[1],
102                                       'align'=> 'left',
103                                       'link' => $p. 'view/' .
104                                                 $_->[2]. '.cgi?'. $_->[3],
105                                     },
106                                   ];
107                                 } $cust_pkg->labels
108                           ];
109                       }
110                     }
111                   ],
112                   'color' => [
113                     '',
114                     '',
115                     '',
116                     '',
117                     sub { shift->statuscolor; },
118                     '',
119                     '',
120                     '',
121                     '',
122                     '',
123                     '',
124                     '',
125                     '',
126                     '',
127                     '',
128                     '',
129                     '',
130                     FS::UI::Web::cust_colors(),
131                     '',
132                   ],
133                   'style' => [ '', '', '', '', 'b', '', '', '', '', '', '', '', '', '', '', '', '',
134                                FS::UI::Web::cust_styles() ],
135                   'size'  => [ '', '', '', '', '-1' ],
136                   'align' => 'rrlccrrlrrrrrrrrl'. FS::UI::Web::cust_aligns(). 'r',
137                   'links' => [
138                     $link,
139                     $link,
140                     $link,
141                     '',
142                     '',
143                     '',
144                     '',
145                     '',
146                     '',
147                     '',
148                     '',
149                     '',
150                     '',
151                     '',
152                     '',
153                     '',
154                     '',
155                     ( map { $_ ne 'Cust. Status' ? $clink : '' }
156                           FS::UI::Web::cust_header(
157                                                     $cgi->param('cust_fields')
158                                                   )
159                     ),
160                     '',
161                   ],
162               )
163 %>
164 <%init>
165
166 my $curuser = $FS::CurrentUser::CurrentUser;
167
168 die "access denied"
169   unless $curuser->access_right('List packages');
170
171 my $conf = new FS::Conf;
172 my $money_char = $conf->config('money_char') || '$';
173
174 # my %part_pkg = map { $_->pkgpart => $_ } qsearch('part_pkg', {});
175
176 my %search_hash = ();
177
178 #some false laziness w/misc/bulk_change_pkg.cgi
179   
180 $search_hash{'query'} = $cgi->keywords;
181
182 #scalars
183 for (qw( agentnum custnum magic status classnum custom cust_fields pkgbatch )) {
184   $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
185 }
186
187 $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
188
189 for my $param ( qw(censustract) ) {
190   $search_hash{$param} = $cgi->param($param) || ''
191     if ( grep { /$param/ } $cgi->param );
192 }
193
194 my @report_option = $cgi->param('report_option')
195   if $cgi->param('report_option');
196 $search_hash{report_option} = join(',', @report_option) if @report_option;
197
198 ###
199 # parse dates
200 ###
201
202 #false laziness w/report_cust_pkg.html
203 my %disable = (
204   'all'             => {},
205   'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, },
206   'active'          => { 'susp'=>1, 'cancel'=>1 },
207   'suspended'       => { 'cancel' => 1 },
208   'cancelled'       => {},
209   ''                => {},
210 );
211
212 foreach my $field (qw( setup last_bill bill adjourn susp expire contract_end cancel active )) {
213
214   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
215
216   next if $beginning == 0 && $ending == 4294967295
217        or $disable{$cgi->param('status')}->{$field};
218
219   $search_hash{$field} = [ $beginning, $ending ];
220
221 }
222
223 my $sql_query = FS::cust_pkg->search(\%search_hash);
224 my $count_query = delete($sql_query->{'count_query'});
225
226 my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
227              ? ''
228              : ';show=packages';
229
230 my $link = sub {
231   my $self = shift;
232   my $frag = 'cust_pkg'. $self->pkgnum; #hack for IE ignoring real #fragment
233   [ "${p}view/cust_main.cgi?custnum=".$self->custnum.
234                            "$show;fragment=$frag#cust_pkg",
235     'pkgnum'
236   ];
237 };
238
239 my $clink = sub {
240   my $cust_pkg = shift;
241   $cust_pkg->cust_main_custnum
242     ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
243     : '';
244 };
245
246 #if ( scalar(@cust_pkg) == 1 ) {
247 #  print $cgi->redirect("${p}view/cust_main.cgi?". $cust_pkg[0]->custnum.
248 #                       "#cust_pkg". $cust_pkg[0]->pkgnum );
249
250 #    my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
251 #    my $rowspan = scalar(@cust_svc) || 1;
252
253 #    my $n2 = '';
254 #    foreach my $cust_svc ( @cust_svc ) {
255 #      my($label, $value, $svcdb) = $cust_svc->label;
256 #      my $svcnum = $cust_svc->svcnum;
257 #      my $sview = $p. "view";
258 #      print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
259 #            qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
260 #      $n2="</TR><TR>";
261 #    }
262
263 sub time_or_blank {
264    my $column = shift;
265    return sub {
266      my $record = shift;
267      my $value = $record->get($column); #mmm closures
268      $value ? time2str('%b %d %Y', $value ) : '';
269    };
270 }
271
272 my $html_init = sub {
273   my $query = shift;
274   my $text = '';
275   my $curuser = $FS::CurrentUser::CurrentUser;
276
277   if ( $curuser->access_right('Bulk change customer packages') ) {
278     $text .= include('/elements/init_overlib.html').
279              include( '/elements/popup_link.html',
280                'label'       => 'Change these packages',
281                'action'      => "${p}misc/bulk_change_pkg.cgi?$query",
282                'actionlabel' => 'Change Packages',
283                'width'       => 569,
284                'height'      => 210,
285              ). '<BR>';
286
287     if ( $curuser->access_right('Edit customer package dates') ) {
288       $text .= include( '/elements/popup_link.html',
289                  'label'       => 'Increment next bill date',
290                  'action'      => "${p}misc/bulk_pkg_increment_bill.cgi?$query",
291                  'actionlabel' => 'Increment Bill Date',
292                  'width'       => 569,
293                  'height'      => 210,
294               ). '<BR>';
295     }
296     $text .= include( '/elements/email-link.html',
297                 'search_hash' => \%search_hash,
298                 'table'       => 'cust_pkg',
299                 );
300   }
301   return $text;
302 };
303
304 </%init>