1 <% include( 'elements/search.html',
2 'title' => 'FCC Form 477 Results',
3 'html_init' => $html_init,
5 'query' => [ @sql_query ],
6 'count_query' => $count_query,
7 'order_by' => 'ORDER BY censustract',
16 'Technology code other',
18 'Percentage residential',
21 sub { my $row = shift; substr($row->censustract, 2, 3) },
22 sub { my $row = shift; substr($row->censustract, 5) },
28 sub { my $row = shift; sprintf "%.2f", $row->residential },
31 [ $link, $link_suffix ],
32 [ $link, $link_suffix ],
33 [ $link, $link_suffix ],
34 [ $link, $link_suffix ],
35 [ $link, $link_suffix ],
36 [ $link, $link_suffix ],
37 [ $link, $link_suffix ],
38 [ $link, $link_suffix ],
44 my $curuser = $FS::CurrentUser::CurrentUser;
47 unless $curuser->access_right('List packages');
52 for ( qw(agentnum magic classnum) ) {
53 $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
56 my @column_option = $cgi->param('column_option')
57 if $cgi->param('column_option');
59 my @row_option = $cgi->param('row_option')
60 if $cgi->param('row_option');
62 my $where = join(' OR ', map { "num = $_" } grep { /^\d+$/ } @column_option );
63 my %column_option_name = $where ?
64 ( map { $_->name => $_->num }
65 qsearch({ 'table' => 'part_pkg_report_option',
67 'extra_sql' => "WHERE $where",
70 ( 'all packages' => '' );
72 $where = join(' OR ', map { "num = $_" } grep { /^\d+$/ } @row_option );
73 my %row_option_name = $where ?
74 ( map { $_->name => $_->num }
75 qsearch({ 'table' => 'part_pkg_report_option',
77 'extra_sql' => "WHERE $where",
80 ( 'all packages' => '' );
82 @row_option = map { $row_option_name{$_} } sort keys %row_option_name;
83 @column_option = map { $column_option_name{$_} } sort keys %column_option_name;
85 #$search_hash{row_option} = join(',', @row_option) if @row_option;
86 my $summary .= '<H2>Summary</H2>'. include('/elements/table.html');
87 $summary .= '<TR><TH></TH>';
88 foreach my $column ( sort keys %column_option_name ) {
89 $summary .= "<TH>$column</TH>";
94 my $total_residential = 0;
96 foreach my $row ( sort keys %row_option_name ) {
98 $summary .= "<TR><TH>$row</TH>";
101 foreach my $column ( sort keys %column_option_name ) {
102 my @report_option = ();
103 push @report_option, $row_option_name{$row}
104 if $row_option_name{$row};
105 push @report_option, $column_option_name{$column}
106 if $column_option_name{$column};
107 my $report_option = join(',', @report_option) if @report_option;
109 my $sql_query = FS::cust_pkg->search(
111 ($report_option ? ( 'report_option' => $report_option ) : () ),
114 my $extracolumns = "$rowcount AS upload, $columncount AS download";
115 my $percent = "CASE WHEN count(*) > 0 THEN 100-100*cast(count(cust_main.company) as numeric)/cast(count(*) as numeric) ELSE cast(0 as numeric) END AS residential";
116 $sql_query->{select} = "count(*) AS quantity, $extracolumns, censustract, $percent";
117 $sql_query->{extra_sql} =~ /^(.*)(ORDER BY pkgnum)(.*)$/s
118 or die "couldn't parse extra_sql";
119 $sql_query->{extra_sql} = "$1 GROUP BY censustract $3";
121 my $count_sql = delete($sql_query->{'count_query'});
122 $count_sql =~ s/ FROM/,count(cust_main.company) FROM/
123 or die "couldn't parse count_sql";
125 my $count_sth = dbh->prepare($count_sql)
126 or die "Error preparing $count_sql: ". dbh->errstr;
128 or die "Error executing $count_sql: ". $count_sth->errstr;
129 my $count_arrayref = $count_sth->fetchrow_arrayref;
130 my $count = $count_arrayref->[0];
131 $total_count += $count;
132 my $residential = $count_arrayref->[1];
133 $total_residential += $residential;
134 my $percentage = sprintf('%.2f', $count ? 100-100*$residential/$count : 0);
136 $summary .= "<TD>$count<BR>$percentage% residential</TD>";
137 push @sql_query, $sql_query;
145 my $total_percentage =
146 sprintf("%.2f", $total_count ? 100-100*$total_residential/$total_count : 0);
148 my $html_init = '<H2>Totals</H2>'. include('/elements/table.html'). "<TR>";
149 $html_init .= "<TH>Total Connections</TH>";
150 $html_init .= "<TH>% owned loop</TH>";
151 $html_init .= "<TH>% billed to end users</TH>";
152 $html_init .= "<TH>% residential</TH>";
153 $html_init .= "<TH>% residential > 200kbps</TH>";
154 $html_init .= "</TR><TR>";
155 $html_init .= "<TD>$total_count</TD>";
156 $html_init .= "<TD>100.00</TD>";
157 $html_init .= "<TD>100.00</TD>";
158 $html_init .= "<TD>$total_percentage</TD>";
159 $html_init .= "<TD>$total_percentage</TD>";
160 $html_init .= "</TR></TABLE><BR>";
161 $html_init .= $summary;
162 $html_init .= "</TABLE><BR><H2>Details</H2>";
164 my $count_query = 'SELECT count(*) FROM ( ('.
166 map { my $extra = $_->{extra_sql}; my $addl = $_->{addl_from};
167 "SELECT censustract from cust_pkg $addl $extra";
172 my $link = 'cust_pkg.cgi?'.
173 join(';', map{ "$_=". $search_hash{$_} } keys %search_hash). ';';
174 my $link_suffix = sub { my $row = shift;
175 my $result = 'censustract='. $row->censustract. ';';
176 $result .= 'report_option='. @row_option[$row->upload - 1]
177 if @row_option[$row->upload - 1];
178 $result .= 'report_option='. @column_option[$row->download - 1]
179 if @column_option[$row->download - 1];