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