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