improved fcc 477 report #7783
[freeside.git] / httemplate / search / 477partIA_detail.html
1 <% include( 'elements/search.html',
2                   'html_init'        => $html_init,
3                   'name'             => 'lines',
4                   'query'            => $query,
5                   'count_query'      => $count_query,
6                   'really_disable_download' => 1,
7                   'disable_download' => 1,
8                   'nohtmlheader'     => 1,
9                   'disable_total'    => 1,
10                   'header'           => [ '', @column_option_name ],
11                   'xml_elements'     => [ @xml_elements ],
12                   'fields'           => [  @fields ],
13               )
14 %>
15 <%init>
16
17 my $curuser = $FS::CurrentUser::CurrentUser;
18
19 die "access denied"
20   unless $curuser->access_right('List packages');
21
22 my %opt = @_;
23 my %search_hash = ();
24   
25 for ( qw(agentnum magic classnum) ) {
26   $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
27 }
28
29 my @column_option = grep { /^\d+/ } $cgi->param('part1_column_option')
30   if $cgi->param('part1_column_option');
31
32 my @row_option = grep { /^\d+/ } $cgi->param('part1_row_option')
33   if $cgi->param('part1_row_option');
34
35 my @technology_option = &FS::Report::FCC_477::parse_technology_option($cgi);
36
37 my @column_option_name = scalar(@column_option)
38   ? ( map { my $part_pkg_report_option = 
39               qsearchs({ 'table' => 'part_pkg_report_option',
40                          'hashref' => { num => $_ },
41                       });
42             $part_pkg_report_option ? $part_pkg_report_option->name
43                                     : 'no such report option';
44           } @column_option
45     )
46   : ( 'all packages' );
47
48 my $where = join(' OR ', map { "num = $_" } @row_option );
49 my %row_option_name = $where ?
50                         ( map { $_->num => $_->name }
51                           qsearch({ 'table' => 'part_pkg_report_option',
52                                     'hashref' => {},
53                                     'extra_sql' => "WHERE $where",
54                                  })
55                         ) :
56                         ();
57
58 my $tech_code = $opt{tech_code};
59 my $technology = $FS::Report::FCC_477::technology[$tech_code] || 'unknown';
60 my $html_init = "<H2>Part IA $technology breakdown by speeds</H2>";
61 my $xml_prefix = 'PartIA_'. chr(65 + $tech_code);
62
63 my $query = 'SELECT '. join(' UNION ALL SELECT ',@row_option);
64 my $count_query = 'SELECT '. scalar(@row_option);
65
66 my $value = sub {
67   my ($rowref, $column) = (shift, shift);
68   my $row = $rowref->[0];
69
70   if ($column eq 'name') {
71     return $row_option_name{$row} || 'no such report option';
72   } elsif ( $column =~ /^(\d+)$/ ) {
73     my @report_option = ( $row || '',
74                           $column_option[$1 - 2] || '',
75                           $technology_option[$tech_code] || '',
76                         );
77
78     my ( $count, $residential ) = FS::cust_pkg->fcc_477_count(
79       { %search_hash, 'report_option' => join(',', @report_option) }
80     );
81
82     my $percentage = sprintf('%.2f', $count ? 100 * $residential / $count : 0);
83     my $return = $count;
84     $return .= "<BR>$percentage% residential"
85       unless $cgi->param('_type') eq 'xml';
86     return $return;
87   } else {
88     return '<FONT SIZE="+1" COLOR="#ff0000">Bad call to column_value</FONT>';
89   }
90 };
91
92 my @fields = (
93   sub { &{$value}(shift, 'name');},
94   sub { &{$value}(shift, 2);},
95   sub { &{$value}(shift, 3);},
96   sub { &{$value}(shift, 4);},
97   sub { &{$value}(shift, 5);},
98   sub { &{$value}(shift, 6);},
99   sub { &{$value}(shift, 7);},
100   sub { &{$value}(shift, 8);},
101   sub { &{$value}(shift, 9);},
102  );
103 shift @fields if $cgi->param('_type') eq 'xml';
104
105 my $xml_element = sub {
106   my ($rowref, $column) = (shift, shift);
107   my $row = $rowref->[0];
108
109   $row++;
110   $xml_prefix. $column. $row;
111
112 };
113
114 my @xml_elements = (
115   sub { &{$xml_element}(shift, 'f') },
116   sub { &{$xml_element}(shift, 'g') },
117   sub { &{$xml_element}(shift, 'h') },
118   sub { &{$xml_element}(shift, 'i') },
119   sub { &{$xml_element}(shift, 'j') },
120   sub { &{$xml_element}(shift, 'k') },
121   sub { &{$xml_element}(shift, 'l') },
122   sub { &{$xml_element}(shift, 'm') },
123 );
124
125 </%init>