package web import from CSV/XLS, RT#9529
[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 { my $c = shift;
43                           sprintf( $money_char.'%.2f',
44                                    $c->part_pkg->base_recur($c)
45                                  );
46                         },
47                     sub { #shift->part_pkg->freq_pretty;
48
49                           #my $part_pkg = $part_pkg{shift->pkgpart};
50                           #$part_pkg->freq_pretty;
51
52                           FS::part_pkg::freq_pretty(shift);
53                         },
54
55                     #sub { time2str('%b %d %Y', shift->setup); },
56                     #sub { time2str('%b %d %Y', shift->last_bill); },
57                     #sub { time2str('%b %d %Y', shift->bill); },
58                     #sub { time2str('%b %d %Y', shift->susp); },
59                     #sub { time2str('%b %d %Y', shift->expire); },
60                     #sub { time2str('%b %d %Y', shift->get('cancel')); },
61                     ( map { time_or_blank($_) }
62                           qw( setup last_bill bill adjourn susp expire cancel ) ),
63
64                     sub { my $self = shift;
65                           my $return = '';
66                           foreach my $action ( qw ( cancel susp ) ) {
67                             my $reason = $self->last_reason($action);
68                             $return = $reason->reason if $reason;
69                             last if $return;
70                           }
71                           $return;
72                         },
73
74                     \&FS::UI::Web::cust_fields,
75                     #sub { '<table border=0 cellspacing=0 cellpadding=0 STYLE="border:none">'.
76                     #      join('', map { '<tr><td align="right" style="border:none">'. $_->[0].
77                     #                     ':</td><td style="border:none">'. $_->[1]. '</td></tr>' }
78                     #                   shift->labels
79                     #          ).
80                     #      '</table>';
81                     #    },
82                     sub {
83                       my $cust_pkg = shift;
84                       my $type = $cgi->param('_type') || '';
85                       if ($type =~ /xls|csv/) {
86                         my $cust_svc = $cust_pkg->primary_cust_svc;
87                         if($cust_svc) {
88                           return join ": ",($cust_svc->label)[0,1];
89                         }
90                         else {
91                           return '';
92                         }
93                       }
94                       else {
95                           [ map {
96                                   [ 
97                                     { 'data' => $_->[0]. ':',
98                                       'align'=> 'right',
99                                     },
100                                     { 'data' => $_->[1],
101                                       'align'=> 'left',
102                                       'link' => $p. 'view/' .
103                                                 $_->[2]. '.cgi?'. $_->[3],
104                                     },
105                                   ];
106                                 } $cust_pkg->labels
107                           ];
108                       }
109                     }
110                   ],
111                   'color' => [
112                     '',
113                     '',
114                     '',
115                     '',
116                     sub { shift->statuscolor; },
117                     '',
118                     '',
119                     '',
120                     '',
121                     '',
122                     '',
123                     '',
124                     '',
125                     '',
126                     '',
127                     '',
128                     FS::UI::Web::cust_colors(),
129                     '',
130                   ],
131                   'style' => [ '', '', '', '', 'b', '', '', '', '', '', '', '', '', '', '', '',
132                                FS::UI::Web::cust_styles() ],
133                   'size'  => [ '', '', '', '', '-1' ],
134                   'align' => 'rrlccrrlrrrrrrrl'. FS::UI::Web::cust_aligns(). 'r',
135                   'links' => [
136                     $link,
137                     $link,
138                     $link,
139                     '',
140                     '',
141                     '',
142                     '',
143                     '',
144                     '',
145                     '',
146                     '',
147                     '',
148                     '',
149                     '',
150                     '',
151                     '',
152                     ( map { $_ ne 'Cust. Status' ? $clink : '' }
153                           FS::UI::Web::cust_header(
154                                                     $cgi->param('cust_fields')
155                                                   )
156                     ),
157                     '',
158                   ],
159               )
160 %>
161 <%init>
162
163 my $curuser = $FS::CurrentUser::CurrentUser;
164
165 die "access denied"
166   unless $curuser->access_right('List packages');
167
168 my $conf = new FS::Conf;
169 my $money_char = $conf->config('money_char') || '$';
170
171 # my %part_pkg = map { $_->pkgpart => $_ } qsearch('part_pkg', {});
172
173 my %search_hash = ();
174
175 #some false laziness w/misc/bulk_change_pkg.cgi
176   
177 $search_hash{'query'} = $cgi->keywords;
178
179 #scalars
180 for (qw( agentnum custnum magic status classnum custom cust_fields pkgbatch )) {
181   $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
182 }
183
184 $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
185
186 for my $param ( qw(censustract) ) {
187   $search_hash{$param} = $cgi->param($param) || ''
188     if ( grep { /$param/ } $cgi->param );
189 }
190
191 my @report_option = $cgi->param('report_option')
192   if $cgi->param('report_option');
193 $search_hash{report_option} = join(',', @report_option) if @report_option;
194
195 ###
196 # parse dates
197 ###
198
199 #false laziness w/report_cust_pkg.html
200 my %disable = (
201   'all'             => {},
202   'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'adjourn'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, },
203   'active'          => { 'susp'=>1, 'cancel'=>1 },
204   'suspended'       => { 'cancel' => 1 },
205   'cancelled'       => {},
206   ''                => {},
207 );
208
209 foreach my $field (qw( setup last_bill bill adjourn susp expire cancel active )) {
210
211   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
212
213   next if $beginning == 0 && $ending == 4294967295
214        or $disable{$cgi->param('status')}->{$field};
215
216   $search_hash{$field} = [ $beginning, $ending ];
217
218 }
219
220 my $sql_query = FS::cust_pkg->search(\%search_hash);
221 my $count_query = delete($sql_query->{'count_query'});
222
223 my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
224              ? ''
225              : ';show=packages';
226
227 my $link = sub {
228   my $self = shift;
229   my $frag = 'cust_pkg'. $self->pkgnum; #hack for IE ignoring real #fragment
230   [ "${p}view/cust_main.cgi?custnum=".$self->custnum.
231                            "$show;fragment=$frag#cust_pkg",
232     'pkgnum'
233   ];
234 };
235
236 my $clink = sub {
237   my $cust_pkg = shift;
238   $cust_pkg->cust_main_custnum
239     ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
240     : '';
241 };
242
243 #if ( scalar(@cust_pkg) == 1 ) {
244 #  print $cgi->redirect("${p}view/cust_main.cgi?". $cust_pkg[0]->custnum.
245 #                       "#cust_pkg". $cust_pkg[0]->pkgnum );
246
247 #    my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
248 #    my $rowspan = scalar(@cust_svc) || 1;
249
250 #    my $n2 = '';
251 #    foreach my $cust_svc ( @cust_svc ) {
252 #      my($label, $value, $svcdb) = $cust_svc->label;
253 #      my $svcnum = $cust_svc->svcnum;
254 #      my $sview = $p. "view";
255 #      print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
256 #            qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
257 #      $n2="</TR><TR>";
258 #    }
259
260 sub time_or_blank {
261    my $column = shift;
262    return sub {
263      my $record = shift;
264      my $value = $record->get($column); #mmm closures
265      $value ? time2str('%b %d %Y', $value ) : '';
266    };
267 }
268
269 my $html_init = sub {
270   my $query = shift;
271   my $text = '';
272   my $curuser = $FS::CurrentUser::CurrentUser;
273
274   if ( $curuser->access_right('Bulk change customer packages') ) {
275     $text .= include('/elements/init_overlib.html').
276              include( '/elements/popup_link.html',
277                'label'       => 'Change these packages',
278                'action'      => "${p}misc/bulk_change_pkg.cgi?$query",
279                'actionlabel' => 'Change Packages',
280                'width'       => 569,
281                'height'      => 210,
282              ). '<BR>';
283
284     if ( $curuser->access_right('Edit customer package dates') ) {
285       $text .= include( '/elements/popup_link.html',
286                  'label'       => 'Increment next bill date',
287                  'action'      => "${p}misc/bulk_pkg_increment_bill.cgi?$query",
288                  'actionlabel' => 'Increment Bill Date',
289                  'width'       => 569,
290                  'height'      => 210,
291               ). '<BR>';
292     }
293   }
294   return $text;
295 };
296
297 </%init>