dbaaaab611db8372d33486c366cad5b849b2d5ca
[freeside.git] / httemplate / search / 477partVI_census.html
1 <% include( 'elements/search.html',
2                   'html_init'     => $html_init,
3                   'html_foot'     => $html_foot,,
4                   'name'          => 'regions',
5                   'query'         => [ @sql_query ],
6                   'count_query'   =>  $count_query,
7                   'order_by'      => 'ORDER BY censustract',
8                   'avoid_quote'   => 1,
9                   'no_csv_header' => 1,
10                   'nohtmlheader'  => 1,
11                   'header'        => [
12                                        'County code',
13                                        'Census tract code',
14                                        'Upload rate',
15                                        'Download rate',
16                                        'Technology code',
17                                        'Technology code other',
18                                        'Quantity',
19                                        'Percentage residential',
20                                      ],
21                   'xml_elements'  => [
22                                        'county_fips',
23                                        'census_tract',
24                                        'upload_rate_code',
25                                        'download_rate_code',
26                                        'technology_code',
27                                        'technology_code_other',
28                                        'value',
29                                        'percentage',
30                                      ],
31                   'fields'        => [
32                     sub { my $row = shift; 
33                           my $state = substr($row->censustract, 0, 2);
34                           $state_hash{$state}++;
35                           substr($row->censustract, 2, 3) || 'None'
36                         },
37                     sub { my $row = shift;
38                           substr($row->censustract, 5) || 'None'
39                         },
40                     'upload',
41                     'download',
42                     'technology_code',
43                     sub { $cgi->param('_type') eq 'xml' ? '0' : '' },#broken
44                     sub { my $row = shift;
45                           my $state = substr($row->censustract, 0, 2);
46                           $state_pkgcount{$state} += $row->quantity;
47                           $row->quantity;
48                         },
49                     sub { my $row = shift; sprintf "%.2f", $row->residential },
50                   ],
51                   'links'        => [
52                     [ $link, $link_suffix ],
53                     [ $link, $link_suffix ],
54                     [ $link, $link_suffix ],
55                     [ $link, $link_suffix ],
56                     [ $link, $link_suffix ],
57                     [ $link, $link_suffix ],
58                     [ $link, $link_suffix ],
59                     [ $link, $link_suffix ],
60                   ],
61                   'url'          => $opt{url} || '',
62                   'xml_row_element' => 'Datarow',
63               )
64 %>
65 <%init>
66
67 my $curuser = $FS::CurrentUser::CurrentUser;
68
69 die "access denied"
70   unless $curuser->access_right('List packages');
71
72 my %opt = @_;
73
74 my $html_init = '<H2>Part VI</H2>';
75
76 my %search_hash = ();
77 my @sql_query = ();
78 my %state_hash = ();
79 my %state_pkgcount = ();
80   
81 for ( qw(agentnum magic) ) {
82   $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
83 }
84 $search_hash{'classnum'} = [ $cgi->param('classnum') ];
85
86 my @column_option = grep { /^\d+$/ } $cgi->param('part1_column_option')
87   if $cgi->param('part1_column_option');
88
89 my @row_option = grep { /^\d+$/ } $cgi->param('part1_row_option')
90   if $cgi->param('part1_row_option');
91
92 my @technology_option = &FS::Report::FCC_477::parse_technology_option($cgi);
93
94 my $rowcount = 1;
95 foreach my $row ( @row_option ) {
96   my $columncount = 2;
97   foreach my $column ( @column_option ) {
98     my $tech_code = 0;
99     foreach my $technology ( @technology_option ) {
100       $tech_code++;
101       next unless $technology;
102       my @report_option = ();
103       push @report_option, $row if $row;
104       push @report_option, $column if $column;
105       push @report_option, $technology;
106       my $report_option = join(',', @report_option) if @report_option;
107  
108       my $sql_query = FS::cust_pkg->search(
109         { %search_hash,
110           ($report_option ? ( 'report_option' => $report_option ) : () ),
111         }
112       );
113       my $extracolumns = "$rowcount AS upload, $columncount AS download, $tech_code as technology_code";
114       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";
115       $sql_query->{select} = "count(*) AS quantity, $extracolumns, censustract, $percent";
116       $sql_query->{extra_sql} =~ /^(.*)(ORDER BY pkgnum)(.*)$/s
117         or die "couldn't parse extra_sql";
118       $sql_query->{extra_sql} = "$1 GROUP BY censustract $3";
119       push @sql_query, $sql_query;
120     }
121     $columncount++;
122   }
123   $rowcount++;
124 }
125
126 my $count_query = 'SELECT count(*) FROM ( ('.
127    join( ') UNION ALL (',
128           map { my $extra = $_->{extra_sql};  my $addl = $_->{addl_from};
129                 "SELECT censustract from cust_pkg $addl $extra";
130               }
131           @sql_query
132        ). ') ) AS foo';
133
134 my $link = 'cust_pkg.cgi?'. join(';',
135                                       map { my $key = $_;
136                                             my @values = ref($search_hash{$_}) 
137                                               ? @{ $search_hash{$_} }
138                                               : $search_hash{$_};
139                                             map { "$key=$_" } @values;
140                                           }
141                                           keys %search_hash        
142                                 ). ';';
143
144 my $link_suffix = sub { my $row = shift;
145                         my $result = 'censustract='. $row->censustract. ';';
146                         $result .= 'report_option='. @row_option[$row->upload - 1]
147                           if @row_option[$row->upload - 1];
148                         $result .= 'report_option='. @column_option[$row->download - 1]
149                           if @column_option[$row->download - 1];
150                         $result;
151                       };
152
153 my $html_foot = sub {
154   if (scalar(keys %state_hash) > 1) {
155     '<BR><B>WARNING: multiple states found</B><BR>'.
156     '<TABLE BORDER=0>'. #nicer formatting someday
157     join('', map { '<TR>'.
158                      '<TD>'.
159                        ( &FS::Report::FCC_477::statenum2state($_) || 'None' ).
160                      '</TD>'.
161                      '<TD>'.
162                        qq(<A HREF="${link}censustract2=$_">).
163                        $state_pkgcount{$_}. ' packages</A> in '.
164                        $state_hash{$_}. ' census tracts'.
165                      '</TD>'.
166                    '</TR>';
167                  }
168                  keys %state_hash
169         ).
170     '</TABLE>';
171   } else { 
172     '';
173   }
174 };
175
176 </%init>