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