FCC 477 report: omit zero values in part 1A matrix, #24773
[freeside.git] / httemplate / search / 477partIA.html
1 % if ( $opt{'type'} eq 'xml' ) {
2 %# container element <Part_IA_$tech> is in 477.html
3 %   my $col = 'a';
4 %   foreach ( @summary_row ) {
5 %     my $el = $xml_prefix . $col . '1'; # PartIA_Aa1, PartIA_Ab1, etc.
6   <<% $el %>><% $_ %><<% "/$el" %>>
7 %     $col++;
8 %   }
9 %   foreach my $col_data ( @data ) { 
10 %     my $row = 1;
11 %     foreach my $cell ( @$col_data ) {
12 %       my $el = $xml_prefix . $col . $row; # PartIA_Af1, PartIA_Af2...
13 %       if ( $cell->[0] > 0 ) {
14   <<% $el %>><% $cell->[0] %><<% "/$el" %>>
15 %         if ( $percentages ) {
16 %           $el = $xml_percent . $col . $row; # Part_p_IA_Af1, ...
17   <<% $el %>><% $cell->[1] %><<% "/$el" %>>
18 %         }
19 %       }
20 %       $row++;
21 %     } # foreach $cell
22 %     $col++;
23 %   } # foreach $col_data
24 % } else { # not XML
25
26 <H2><% $title %> totals</H2>
27 <& /elements/table-grid.html &>
28   <TR>
29 %   foreach ( 'Total Connections',
30 %             '% owned loop',
31 %             '% billed to end users',
32 %             '% residential',
33 %             '% residential > 200 kbps') {
34     <TH WIDTH="20%"><% $_ |h %></TH>
35 %   }
36   </TR>
37   <TR CLASS="row0">
38 %   foreach ( @summary_row ) {
39     <TD><% $_ %></TD>
40 %   }
41   </TR>
42 </TABLE>
43 <H2><% $title %> breakdown by speed</H2>
44 <TABLE CLASS="grid" CELLSPACING=0>
45   <TR>
46     <TH WIDTH="12%"></TH>
47 %   for (my $col = 0; $col < scalar(@download_option); $col++) {
48     <TH WIDTH="11%">
49       <% $FS::Report::FCC_477::download[$col] |h %>
50     </TH>
51 %   }
52   </TR>
53 % for (my $row = 0; $row < scalar(@upload_option); $row++) {
54   <TR CLASS="row<% $row % 2%>">
55     <TD STYLE="text-align: left; font-weight: bold">
56 %     if ( $asymmetric ) {
57       <% $FS::Report::FCC_477::upload[$row] |h %>
58 %     }
59     </TD>
60 %   for (my $col = 0; $col < scalar(@download_option); $col++) {
61     <TD>
62 %     if ( $data[$col][$row][0] > 0 ) {
63       <% $data[$col][$row][0] %>
64 %       if ( $percentages ) {
65       <BR><% $data[$col][$row][1] %>
66 %       }
67 %     }
68     </TD>
69 %   } # for $col
70   </TR>
71 % } # for $row
72 </TABLE>
73 % }
74 <%init>
75
76 my $curuser = $FS::CurrentUser::CurrentUser;
77
78 die "access denied"
79   unless $curuser->access_right('List packages');
80
81 my %opt = @_;
82 my %search_hash;
83   
84 for ( qw(agentnum state) ) {
85   $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
86 }
87 $search_hash{'status'} = 'active';
88 $search_hash{'country'} = 'US';
89 $search_hash{'classnum'} = [ $cgi->param('classnum') ];
90
91 # arrays of report_option_ numbers, running parallel to 
92 # the download and upload speed arrays
93 my @download_option = $cgi->param('part1_column_option');
94 my @upload_option = $cgi->param('part1_row_option');
95
96 my @technology_option = &FS::Report::FCC_477::parse_technology_option($cgi);
97
98 my $total_count = 0;
99 my $total_residential = 0;
100 my $above_200 = 0;
101 my $tech_code = $opt{tech_code};
102 my $technology = $FS::Report::FCC_477::technology[$tech_code] || 'unknown';
103 my $title = "Part IA $technology";
104 my $xml_prefix = 'PartIA_'. chr(65 + $tech_code);
105 my $xml_percent = 'Part_p_IA_'. chr(65 + $tech_code); # yes, seriously
106
107 # whether to show the results as a matrix (upload speeds in rows) or a single
108 # row
109 my $asymmetric = 1;
110 if ( $technology eq 'Symmetric xDSL' or $technology eq 'Other Wireline' ) {
111   $asymmetric = 0;
112   @upload_option = ( undef );
113 }
114 # whether to show residential percentages in each cell of the matrix
115 my $percentages = ($technology eq 'Terrestrial Mobile Wireless');
116
117 my $query = FS::cust_pkg->search(\%search_hash);
118 my $count_query = $query->{'count_query'};
119
120 my $is_residential = " AND COALESCE(cust_main.company, '') = ''";
121 my $has_option = sub {
122   my $optionnum = shift;
123   $optionnum =~ /^\d+$/ ?
124   " AND EXISTS(
125     SELECT 1 FROM part_pkg_option
126     WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
127     AND optionname = 'report_option_$optionnum'
128     AND optionvalue = '1'
129   )" : '';
130 };
131
132 # limit to those that have technology option $tech_code
133 $count_query .= $has_option->($technology_option[$tech_code]);
134
135 my @data;
136 for ( my $row = 0; $row < scalar @upload_option; $row++ ) {
137   for ( my $col = 0; $col < scalar @download_option; $col++ ) {
138
139     my $this_count_query = $count_query .
140                            $has_option->($upload_option[$row]) .
141                            $has_option->($download_option[$col]);
142
143     my $count = FS::Record->scalar_sql($this_count_query);
144     my $residential = FS::Record->scalar_sql($this_count_query . $is_residential);
145
146     my $percent = sprintf('%.3f', $count ? 100 * $residential / $count : 0);
147     $data[$col][$row] = [ $count, $percent ];
148
149     $total_count += $count;
150     $total_residential += $residential;
151     $above_200 += $residential if $row > 0 or !$asymmetric;
152   }
153 }
154
155 my $total_percentage =
156   sprintf("%.3f", $total_count ? 100*$total_residential/$total_count : 0);
157
158 my $above_200_percentage =
159   sprintf("%.3f", $total_count ? 100*$above_200/$total_count : 0);
160
161 my @summary_row = (
162   $total_count,
163   100.00, # own local loop--consistent with previous practice, but probably wrong
164   100.00, # billed to end user--also wrong
165   $total_percentage, # residential percentage
166   $above_200_percentage,
167 );
168
169 </%init>