477 report tweaks
[freeside.git] / httemplate / browse / part_pkg-fcc.html
1 <& elements/browse.html,
2   'title'                 => 'Package Definitions - FCC Options',
3   'menubar'               => \@menubar,
4   'html_init'             => $html_init,
5   'html_form'             => $html_form,
6   'name'                  => 'package definitions',
7   'disableable'           => 1,
8   'disabled_statuspos'    => 4,
9   'agent_virt'            => 1,
10   'agent_null_right'      => $edit_global,
11   'agent_pos'             => 3,
12   'query'                 =>
13                             { 'select'    => $select,
14                               'table'     => 'part_pkg',
15                               'addl_from' => $addl_from,
16                               'hashref'   => \%hash,
17                               'extra_sql' => $extra_sql,
18                               'order_by'  => "ORDER BY $orderby"
19                             },
20   'count_query'           => $count_query,
21   'header'                => \@header,
22   'fields'                => \@fields,
23   'links'                 => \@links,
24   'align'                 => $align,
25   'link_field'            => 'pkgpart',
26   'html_foot'             => $html_foot,
27 &>
28 <%init>
29
30 my $curuser = $FS::CurrentUser::CurrentUser;
31
32 my $edit        = 'Edit FCC report configuration';
33 my $edit_global = 'Edit FCC report configuration for all agents';
34 my $acl_edit        = $curuser->access_right($edit);
35 my $acl_edit_global = $curuser->access_right($edit_global);
36
37 die "access denied"
38   unless $acl_edit || $acl_edit_global;
39
40 if ( $cgi->param('redirect') ) {
41   my $session = $cgi->param('redirect');
42   my $pref = $curuser->option("redirect$session");
43   die "unknown redirect session $session\n" unless length($pref);
44   $cgi = new CGI($pref);
45   $cgi->param('redirect', $session);
46 }
47
48 my $conf = new FS::Conf;
49
50 my $orderby = 'pkgpart';
51 my %hash = ();
52 my $extra_count = '';
53
54 my @where = ();
55
56 # only ever show recurring packages here
57 $hash{'freq'} = { op=>'!=', value=>'0' };
58 $extra_count = " freq != '0' ";
59
60 # filter by classnum
61 my $classnum = '';
62 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
63   $classnum = $1;
64   push @where, $classnum ? "classnum =  $classnum"
65                          : "classnum IS NULL";
66 }
67 $cgi->delete('classnum');
68
69 # filter by agent permissions
70 push @where, FS::part_pkg->curuser_pkgs_sql
71   unless $acl_edit_global;
72
73 my $extra_sql = scalar(@where)
74                 ? ( scalar(keys %hash) ? ' AND ' : ' WHERE ' ).
75                   join( 'AND ', @where)
76                 : '';
77
78 # pull option values into the select
79 my @optionnames = ( qw(
80   media
81   is_consumer
82   is_broadband technology broadband_upstream broadband_downstream
83   is_phone phone_wholesale phone_vges phone_circuits 
84   phone_lines phone_longdistance phone_localloop
85   is_voip voip_lastmile voip_sessions
86 ) );
87
88 my $select = join(',',
89   'part_pkg.*',
90   '(SELECT classname FROM pkg_class WHERE pkg_class.classnum = part_pkg.classnum) AS classname', # grr, disableable...
91   @optionnames
92 );
93
94 my $addl_from = 
95   FS::Report::FCC_477::join_optionnames(@optionnames);
96
97 $cgi->param('classnum', $classnum) if length($classnum);
98
99 my $link = [ $p.'edit/part_pkg.cgi?', 'pkgpart' ];
100
101 my @header = ( '#', 'Package', 'Comment' );
102 my @fields = ( 'pkgpart', 'pkg', 'comment' ,);
103 my $align = 'rll';
104 my @links = ( $link, $link, '', );
105
106 unless ( length($classnum) ) {
107   push @header, 'Class';
108   push @fields, 'classname';
109   $align .= 'l';
110 }
111
112 # still include the report_option classes, to help with migration
113 # but not other plan options
114
115 my %report_optionname_name = map { 'report_option_'.$_->num, $_->name }
116   qsearch('part_pkg_report_option', { disabled => '' });
117
118 push @header, 'Report classes';
119
120 push @fields, 
121               sub {
122                     my $part_pkg = shift;
123                     my %options = $part_pkg->options;
124                     # gather any options that are really report options,
125                     # convert them to their user-friendly names,
126                     # and sort them (I think?)
127                     my @report_options =
128                       sort { $a cmp $b }
129                       map { $report_optionname_name{$_} }
130                       grep { $options{$_}
131                              and exists($report_optionname_name{$_}) }
132                       keys %options;
133
134                     my @rows;
135                     foreach (@report_options) {
136                       push @rows, [
137                         { 'data'  => $_,
138                           'align' => 'center',
139                           'colspan' => 2
140                         }
141                       ];
142                     } # foreach @report_options
143                     \@rows;
144                   };
145
146 $align .= 'cr';
147
148 # --------
149 # now the FCC option part
150 # --------
151
152 my @pkgparts;
153 push @header, 'FCC report parameters';
154 push @fields, sub {
155   my $part_pkg = shift;
156   my %hash = $part_pkg->fcc_options;
157   include('/elements/input-fcc_options.html',
158             id          => 'pkgpart'.$part_pkg->pkgpart,
159             curr_value  => encode_json(\%hash),
160             html_only   => 1
161   );
162 };
163 $align .= 'l';
164
165 my $count_extra_sql = $extra_sql;
166 $count_extra_sql =~ s/^\s*AND /WHERE /i;
167 $extra_count = ( $count_extra_sql ? ' AND ' : ' WHERE ' ). $extra_count
168   if $extra_count;
169 my $count_query = "SELECT COUNT(*) FROM part_pkg $count_extra_sql $extra_count";
170
171 # in case of error redirect
172 if ( $cgi->param('redirect') ) {
173   push @header, '';
174   push @fields, sub {
175     my $part_pkg = shift;
176     my $pkgpart = $part_pkg->pkgpart;
177     '<B><FONT COLOR="#ffffff">' . $cgi->param("error$pkgpart") || '' . '</FONT></B>'
178   };
179   $align .= 'l';
180 }
181
182 my $html_init = 
183   include('/elements/init_overlib.html') .
184   include('/elements/input-fcc_options.html', js_only => 1) .
185   include('.style');
186
187 my $html_form = qq!<FORM ACTION="${p}edit/process/bulk-part_pkg-fcc.html" METHOD="POST">
188   ( show class: !.
189   include('/elements/select-pkg_class.html',
190             #'curr_value'    => $classnum,
191             'value'         => $classnum, #insist on 0 :/
192             'onchange'      => 'filter_change()',
193             'pre_options'   => [ '-1' => 'all',
194                                  '0'  => '(none)', ],
195             'disable_empty' => 1,
196          ).
197   ' )
198   <BR><BR>' .
199   qq!<SCRIPT TYPE="text/javascript">
200   function filter_change() {
201     window.location = '! . $cgi->self_url . qq!?classnum='
202       + document.getElementById('classnum').value;
203   }
204   </SCRIPT>!;
205
206 my $html_foot = qq!
207   <INPUT TYPE="submit" VALUE="Save changes">
208   </FORM>!;
209
210 my @menubar =
211   ( 'Package definitions' => $p.'browse/part_pkg.cgi' );
212
213 </%init>
214 <%def .style>
215 <style>
216   ul.fcc_options {
217     text-align: left;
218   }
219   ul.fcc_options li {
220   }
221   button.edit_fcc_options {
222     float: right;
223   }
224 </style>
225 </%def>