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