improved fcc 477 report #7783
[freeside.git] / httemplate / search / 477partIIB.html
1 <% include( 'elements/search.html',
2                   'html_init'        => $html_init,
3                   'name'             => 'lines',
4                   'query'            => $query,
5                   'count_query'      => 'SELECT 11',
6                   'really_disable_download' => 1,
7                   'disable_download' => 1,
8                   'nohtmlheader'     => 1,
9                   'disable_total'    => 1,
10                   'header'           => [ @headers ],
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 $html_init = '<H2>Part IIB</H2>';
23 my %search_hash = ();
24   
25 for ( qw(agentnum magic classnum) ) {
26   $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
27 }
28
29 my @row_option = grep { /^\d+$/ } $cgi->param('part2b_row_option')
30   if $cgi->param('part2b_row_option');
31
32 # fudge in 2nd row
33 unshift @row_option, $row_option[0];
34
35 my $query = 'SELECT '. join(' UNION SELECT ', 1..8);
36
37 my $total_count = 0;
38 my $column_value = sub {
39   my $row = shift;
40
41   my @report_option = ( $row_option[$row - 1] || '' );
42
43   my $sql_query = FS::cust_pkg->search(
44     { %search_hash, 'report_option' => join(',', @report_option) }
45   );
46
47   my $count_sql = delete($sql_query->{'count_query'});
48   if ( $row == 2 ) {
49     $count_sql =~ s/COUNT\(\*\) FROM/COALESCE( sum(CASE WHEN cust_main.company IS NULL OR cust_main.company = '' THEN fcc_ds0s END), 0 ) FROM/
50       or die "couldn't parse count_sql";
51   } else {
52     $count_sql =~ s/COUNT\(\*\) FROM/COALESCE( sum(fcc_ds0s), 0 ) FROM/
53       or die "couldn't parse count_sql";
54   }
55
56   my $count_sth = dbh->prepare($count_sql)
57     or die "Error preparing $count_sql: ". dbh->errstr;
58   $count_sth->execute
59     or die "Error executing $count_sql: ". $count_sth->errstr;
60   my $count_arrayref = $count_sth->fetchrow_arrayref;
61   my $count = $count_arrayref->[0];
62
63   $total_count = $count if $row == 1;
64   $count = sprintf('%.2f', $total_count ? 100*$count/$total_count : 0)
65     if $row != 1;
66
67   return "$count";
68 };
69
70 my @headers = (
71   '',
72   'with broadband',
73   'without broadband',
74   'wholesale',
75 );
76
77 my @xml_elements = (
78   sub { my $row = shift; my $rownum = $row->[0] + 1; "PartII_${rownum}a" },
79   sub { my $row = shift; my $rownum = $row->[0] + 1; "PartII_${rownum}b" },
80   sub { my $row = shift; my $rownum = $row->[0] + 1; "PartII_${rownum}c" },
81 );
82
83 my @rows = (
84   'total number',
85   '% residential',
86   '% nomadic',
87   '% copper',
88   '% FTTP',
89   '% coax',
90   '% wireless',
91   '% other broadband',
92 );
93
94 my @fields = (
95   sub { my $row = shift; $rows[$row->[0] - 1]; },
96   sub { 0; },
97   sub { my $row = shift; &{$column_value}($row->[0]); },
98   sub { 0; },
99 );
100
101 shift @fields if $cgi->param('_type') eq 'xml';
102 </%init>