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