fix 477 censustract links, add links for states, RT#13922
[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                           $state_hash{substr($row->censustract, 0, 2)}++;
34                           substr($row->censustract, 2, 3) || 'none'
35                         },
36                     sub { my $row = shift;
37                           substr($row->censustract, 5) || 'none'
38                         },
39                     'upload',
40                     'download',
41                     'technology_code',
42                     sub { $cgi->param('_type') eq 'xml' ? '0' : '' },#broken
43                     'quantity',
44                     sub { my $row = shift; sprintf "%.2f", $row->residential },
45                   ],
46                   'links'        => [
47                     [ $link, $link_suffix ],
48                     [ $link, $link_suffix ],
49                     [ $link, $link_suffix ],
50                     [ $link, $link_suffix ],
51                     [ $link, $link_suffix ],
52                     [ $link, $link_suffix ],
53                     [ $link, $link_suffix ],
54                     [ $link, $link_suffix ],
55                   ],
56                   'url'          => $opt{url} || '',
57                   'xml_row_element' => 'Datarow',
58               )
59 %>
60 <%init>
61
62 my $curuser = $FS::CurrentUser::CurrentUser;
63
64 die "access denied"
65   unless $curuser->access_right('List packages');
66
67 my %opt = @_;
68
69 my $html_init = '<H2>Part VI</H2>';
70
71 my %search_hash = ();
72 my @sql_query = ();
73 my %state_hash = ();
74   
75 for ( qw(agentnum magic) ) {
76   $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
77 }
78 $search_hash{'classnum'} = [ $cgi->param('classnum') ];
79
80 my @column_option = grep { /^\d+$/ } $cgi->param('part1_column_option')
81   if $cgi->param('part1_column_option');
82
83 my @row_option = grep { /^\d+$/ } $cgi->param('part1_row_option')
84   if $cgi->param('part1_row_option');
85
86 my @technology_option = &FS::Report::FCC_477::parse_technology_option($cgi);
87
88 my $rowcount = 1;
89 foreach my $row ( @row_option ) {
90   my $columncount = 2;
91   foreach my $column ( @column_option ) {
92     my $tech_code = 0;
93     foreach my $technology ( @technology_option ) {
94       $tech_code++;
95       next unless $technology;
96       my @report_option = ();
97       push @report_option, $row if $row;
98       push @report_option, $column if $column;
99       push @report_option, $technology;
100       my $report_option = join(',', @report_option) if @report_option;
101  
102       my $sql_query = FS::cust_pkg->search(
103         { %search_hash,
104           ($report_option ? ( 'report_option' => $report_option ) : () ),
105         }
106       );
107       my $extracolumns = "$rowcount AS upload, $columncount AS download, $tech_code as technology_code";
108       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";
109       $sql_query->{select} = "count(*) AS quantity, $extracolumns, censustract, $percent";
110       $sql_query->{extra_sql} =~ /^(.*)(ORDER BY pkgnum)(.*)$/s
111         or die "couldn't parse extra_sql";
112       $sql_query->{extra_sql} = "$1 GROUP BY censustract $3";
113       push @sql_query, $sql_query;
114     }
115     $columncount++;
116   }
117   $rowcount++;
118 }
119
120 my $count_query = 'SELECT count(*) FROM ( ('.
121    join( ') UNION ALL (',
122           map { my $extra = $_->{extra_sql};  my $addl = $_->{addl_from};
123                 "SELECT censustract from cust_pkg $addl $extra";
124               }
125           @sql_query
126        ). ') ) AS foo';
127
128 my $link = 'cust_pkg.cgi?'. join(';',
129                                       map { my $key = $_;
130                                             my @values = ref($search_hash{$_}) 
131                                               ? @{ $search_hash{$_} }
132                                               : $search_hash{$_};
133                                             map { "$key=$_" } @values;
134                                           }
135                                           keys %search_hash        
136                                 ). ';';
137
138 my $link_suffix = sub { my $row = shift;
139                         my $result = 'censustract='. $row->censustract. ';';
140                         $result .= 'report_option='. @row_option[$row->upload - 1]
141                           if @row_option[$row->upload - 1];
142                         $result .= 'report_option='. @column_option[$row->download - 1]
143                           if @column_option[$row->download - 1];
144                         $result;
145                       };
146
147 my $html_foot = sub {
148   if (scalar(keys %state_hash) > 1) {
149     '<BR><B>WARNING: multiple states found</B><BR>'.
150     '<TABLE BORDER=0>'. #nicer formatting someday
151     join('', map { '<TR>'.
152                      "<TD>$_</TD>".
153                      qq(<TD><A HREF="${link}state=$_">).
154                        $state_hash{$_}. '</A></TD>'.
155                    '</TR>';
156                  }
157                  keys %state_hash
158         ).
159     '</TABLE>';
160   } else { 
161     '';
162   }
163 };
164
165 </%init>