fix urls
[freeside.git] / httemplate / search / 477partVI.html
1 <% include( 'elements/search.html',
2                   'html_init'     => $html_init,
3                   'name'          => 'regions',
4                   'query'         => [ @sql_query ],
5                   'count_query'   =>  $count_query,
6                   'order_by'      => 'ORDER BY censustract',
7                   'avoid_quote'   => 1,
8                   'no_csv_header' => 1,
9                   'nohtmlheader'  => 1,
10                   'header'        => [
11                                        'County code',
12                                        'Census tract code',
13                                        'Upload rate',
14                                        'Download rate',
15                                        'Technology code',
16                                        'Technology code other',
17                                        'Quantity',
18                                        'Percentage residential',
19                                      ],
20                   'xml_elements'  => [
21                                        'county_fips',
22                                        'census_tract',
23                                        'upload_rate_code',
24                                        'download_rate_code',
25                                        'technology_code',
26                                        'technology_code_other',
27                                        'value',
28                                        'percentage',
29                                      ],
30                   'fields'        => [
31                     sub { my $row = shift; substr($row->censustract, 2, 3) },
32                     sub { my $row = shift; substr($row->censustract, 5) },
33                     'upload',
34                     'download',
35                     'technology_code',
36                     sub { '' },  # doesn't really work
37                     'quantity',
38                     sub { my $row = shift; sprintf "%.2f", $row->residential },
39                   ],
40                   'links'        => [
41                     [ $link, $link_suffix ],
42                     [ $link, $link_suffix ],
43                     [ $link, $link_suffix ],
44                     [ $link, $link_suffix ],
45                     [ $link, $link_suffix ],
46                     [ $link, $link_suffix ],
47                     [ $link, $link_suffix ],
48                     [ $link, $link_suffix ],
49                   ],
50                   'url'          => $opt{url} || '',
51                   'xml_row_element' => 'Datarow',
52               )
53 %>
54 <%init>
55
56 my $curuser = $FS::CurrentUser::CurrentUser;
57
58 die "access denied"
59   unless $curuser->access_right('List packages');
60
61 my %opt = @_;
62
63 my $html_init = '<H2>Part VI</H2>';
64
65 my %search_hash = ();
66 my @sql_query = ();
67   
68 for ( qw(agentnum magic classnum) ) {
69   $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
70 }
71
72 my @column_option = grep { /^\d+$/ } $cgi->param('part1_column_option')
73   if $cgi->param('part1_column_option');
74
75 my @row_option = grep { /^\d+$/ } $cgi->param('part1_row_option')
76   if $cgi->param('part1_row_option');
77
78 my @technology_option = &FS::Report::FCC_477::parse_technology_option($cgi);
79
80 my $rowcount = 1;
81 foreach my $row ( @row_option ) {
82   my $columncount = 2;
83   foreach my $column ( @column_option ) {
84     my $tech_code = 0;
85     foreach my $technology ( @technology_option ) {
86       $tech_code++;
87       next unless $technology;
88       my @report_option = ();
89       push @report_option, $row if $row;
90       push @report_option, $column if $column;
91       push @report_option, $technology;
92       my $report_option = join(',', @report_option) if @report_option;
93  
94       my $sql_query = FS::cust_pkg->search(
95         { %search_hash,
96           ($report_option ? ( 'report_option' => $report_option ) : () ),
97         }
98       );
99       my $extracolumns = "$rowcount AS upload, $columncount AS download, $tech_code as technology_code";
100       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";
101       $sql_query->{select} = "count(*) AS quantity, $extracolumns, censustract, $percent";
102       $sql_query->{extra_sql} =~ /^(.*)(ORDER BY pkgnum)(.*)$/s
103         or die "couldn't parse extra_sql";
104       $sql_query->{extra_sql} = "$1 GROUP BY censustract $3";
105       push @sql_query, $sql_query;
106     }
107     $columncount++;
108   }
109   $rowcount++;
110 }
111
112 my $count_query = 'SELECT count(*) FROM ( ('.
113    join( ') UNION ALL (',
114           map { my $extra = $_->{extra_sql};  my $addl = $_->{addl_from};
115                 "SELECT censustract from cust_pkg $addl $extra";
116               }
117           @sql_query
118        ). ') ) AS foo';
119
120 my $link = 'cust_pkg.cgi?'.
121            join(';', map{ "$_=". $search_hash{$_} } keys %search_hash). ';';
122 my $link_suffix = sub { my $row = shift;
123                         my $result = 'censustract='. $row->censustract. ';';
124                         $result .= 'report_option='. @row_option[$row->upload - 1]
125                           if @row_option[$row->upload - 1];
126                         $result .= 'report_option='. @column_option[$row->download - 1]
127                           if @column_option[$row->download - 1];
128                         $result;
129                       };
130 </%init>