stray closing /TABLE in the no-ticket case
[freeside.git] / httemplate / search / 477_cust_pkg.html
1 <& elements/search.html,
2                   'html_init'   => $html_init,
3                   'html_form'   => $html_form,
4                   'html_foot'   => '</FORM>',
5                   'title'       => emt('Package Search Results'), 
6                   'name'        => 'packages',
7                   'query'       => $query,
8                   'count_query' => $count_query,
9                   'header'      => [ emt('#'),
10                                      emt('Quan.'),
11                                      emt('Package'),
12                                      emt('Class'),
13                                      emt('Status'),
14                                      emt('Freq.'),
15                                      emt('Setup'),
16                                      emt('Next bill'),
17                                      emt('Susp.'),
18                                      emt('Changed'),
19                                      emt('Cancel'),
20                                      FS::UI::Web::cust_header(),
21                                      emt('Census tract'),
22                                      emt('Package options'),
23                                    ],
24                   'fields'      => [
25                     'pkgnum',
26                     'quantity',
27                     sub { $_[0]->pkg; },
28                     'classname',
29                     sub { ucfirst(shift->status); },
30                     sub { FS::part_pkg::freq_pretty(shift); },
31
32                     ( map { time_or_blank($_) }
33                       qw( setup bill susp change_date cancel ) ),
34
35                     \&FS::UI::Web::cust_fields,
36
37                     sub { # census tract
38                       my $cust_pkg = shift;
39                       my $cust_location = $cust_pkg->cust_location;
40                       ($cust_location->censustract || '<b>unknown</b>').
41                         '<font size="-1"> (edit)</font>';
42                     },
43
44                     # a hidden input in each row with the pkgnum, so that
45                     # we can refresh back to this list of pkgnums
46                     sub {
47                       my $cust_pkg = shift;
48                       my $part_pkg = $cust_pkg->part_pkg;
49                       my %hash = $part_pkg->fcc_options;
50                       '<INPUT NAME="pkgnum" TYPE="hidden" VALUE="' .
51                         $cust_pkg->pkgnum . '">' .
52                       include('/elements/input-fcc_options.html',
53                         id          => 'pkgnum'.$cust_pkg->pkgnum.
54                                        'pkgpart'.$part_pkg->pkgpart,
55                         curr_value  => encode_json(\%hash),
56                         html_only   => 1
57                       )
58                     },
59                   ],
60                   'color' => [
61                     '',
62                     '',
63                     '',
64                     '',
65                     sub { shift->statuscolor; },
66                     '', '', '', '', '', '',
67                     FS::UI::Web::cust_colors(),
68                     '',
69                     '',
70                   ],
71                   'style' => [ '', '', '', '', 'b', 
72                                '', '', '', '', '', '',
73                                FS::UI::Web::cust_styles() ],
74                   'size'  => [ '', '', '', '', '-1' ],
75                   'align' => 'rrlcccrrrrr'. FS::UI::Web::cust_aligns(). 'cl',
76                   'links' => [
77                     $link,
78                     $link,
79                     $link,
80                     '', '', '', '', '', '', '', '',
81                     ( map { $_ ne 'Cust. Status' ? $clink : '' }
82                           FS::UI::Web::cust_header()
83                     ),
84                     '',
85                     '',
86                   ],
87                   'link_onclicks' => [
88                     (('') x 11),
89                     (map { '' } FS::UI::Web::cust_header()),
90                     $pkg_edit_location_link,
91                     '',
92                   ],
93
94 &>
95 <%init>
96
97 my $curuser = $FS::CurrentUser::CurrentUser;
98 my $edit        = 'Edit FCC report configuration';
99 my $edit_global = 'Edit FCC report configuration for all agents';
100 my $acl_edit        = $curuser->access_right($edit);
101 my $acl_edit_global = $curuser->access_right($edit_global);
102
103 die "access denied"
104   unless $acl_edit || $acl_edit_global;
105
106 my $conf = new FS::Conf;
107
108 my $session;
109
110 my ($query, $count_query);
111
112 if ( $cgi->param('redirect') ) { # then restore the pkgnum list
113   $session = $cgi->param('redirect');
114   my $pref = $curuser->option("redirect$session"); # contains a list of pkgnums
115   die "unknown redirect session $session\n" unless length($pref);
116   my @pkgnums = grep /^\d+$/, split(',', $pref);
117   
118   $query = FS::cust_pkg->search({});
119   $count_query = delete($query->{count_query});
120
121   my $where = "cust_pkg.pkgnum IN (".join(',', @pkgnums).")";
122   if ( $count_query =~ /WHERE/i ) {
123     $where = " AND ($where) ";
124   } else {
125     $where = " WHERE ($where) ";
126   }
127   $query->{extra_sql} .= $where;
128   $count_query .= $where;
129 } else {
130   # build and run the query right now, and then cache the pkgnums it returned
131   my %search_hash = ();
132     
133   #scalars
134   for (qw( agentnum 477part 477rownum date )) {
135     $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
136   }
137
138   $query = FS::cust_pkg->search(\%search_hash);
139   $count_query = delete($query->{'count_query'});
140
141   my @cust_pkg = qsearch($query);
142
143   my $pkgnums = join(',', map { $_->pkgnum } @cust_pkg);
144   $session = int(rand(4294967296)); #XXX
145   my $pref = new FS::access_user_pref({
146     'usernum'    => $FS::CurrentUser::CurrentUser->usernum,
147     'prefname'   => "redirect$session",
148     'prefvalue'  => $pkgnums,
149     'expiration' => time + 3600, #1h?  1m?
150   });
151   my $pref_error = $pref->insert;
152   if ($pref_error) {
153     die "couldn't even set redirect cookie: $pref_error\n";
154   }
155
156   # and then bail out and reload using the redirect cookie
157   $cgi->delete_all();
158   $cgi->param("redirect", $session);
159   $m->clear_buffer;
160   $m->print( $cgi->redirect($cgi->self_url) );
161   $m->abort;
162 }
163
164 my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
165              ? ''
166              : ';show=packages';
167
168 my $link = sub {
169   my $self = shift;
170   my $frag = 'cust_pkg'. $self->pkgnum; #hack for IE ignoring real #fragment
171   [ "${p}view/cust_main.cgi?custnum=".$self->custnum.
172                            "$show;fragment=$frag#cust_pkg",
173     'pkgnum'
174   ];
175 };
176
177 my $html_init =
178   include('/elements/init_overlib.html') .
179   include('/elements/input-fcc_options.html', js_only => 1) .
180   include('.style') .
181   include('.script');
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 my $html_form = qq!
191   <FORM ACTION="${p}edit/process/bulk-477_cust_pkg.html" METHOD="POST" NAME="477_cust_pkg">
192   <INPUT NAME="redirect" TYPE="hidden" VALUE="$session">
193 !;
194
195 my $pkg_edit_location_link = sub {
196   my $cust_pkg = shift;
197   my $locationnum = $cust_pkg->locationnum;
198   include('/elements/popup_link_onclick.html',
199     'action'      => $p. "edit/cust_location-censustract.html?locationnum=$locationnum",
200     'actionlabel' => emt('Edit census tract'),
201     'width'       => 700,
202     'height'      => 355,
203   );
204 };
205
206 sub time_or_blank {
207    my $column = shift;
208    return sub {
209      my $record = shift;
210      my $value = $record->get($column); #mmm closures
211      $value ? time2str('%b %d %Y', $value ) : '';
212    };
213 }
214
215 </%init>
216 <%def .style>
217 <style>
218   button.edit_fcc_button { float: right; }
219 </style>
220 </%def>
221 <%def .script>
222 <script type="text/javascript">
223   function finish_edit_fcc(id) {
224     cClick();
225     document.forms['477_cust_pkg'].submit(); //immediately save/refresh
226   }
227 </script>
228 </%def>