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