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