improve speed in customer search, #13364
[freeside.git] / httemplate / search / 477partVI_census.html
1 <% include( 'elements/search.html',
2                   'html_init'     => $html_init,
3                   'html_foot'     => sub { if (scalar(keys %state_hash) > 1) {
4                                              '<BR><B>'.
5                                                'WARNING: multiple states found'.
6                                              '</B><BR>';
7                                            } else { 
8                                              '';
9                                            }
10                                          },
11                   'name'          => 'regions',
12                   'query'         => [ @sql_query ],
13                   'count_query'   =>  $count_query,
14                   'order_by'      => 'ORDER BY censustract',
15                   'avoid_quote'   => 1,
16                   'no_csv_header' => 1,
17                   'nohtmlheader'  => 1,
18                   'header'        => [
19                                        'County code',
20                                        'Census tract code',
21                                        'Upload rate',
22                                        'Download rate',
23                                        'Technology code',
24                                        'Technology code other',
25                                        'Quantity',
26                                        'Percentage residential',
27                                      ],
28                   'xml_elements'  => [
29                                        'county_fips',
30                                        'census_tract',
31                                        'upload_rate_code',
32                                        'download_rate_code',
33                                        'technology_code',
34                                        'technology_code_other',
35                                        'value',
36                                        'percentage',
37                                      ],
38                   'fields'        => [
39                     sub { my $row = shift; 
40                           $state_hash{substr($row->censustract, 0, 2)} = 1;
41                           substr($row->censustract, 2, 3)
42                         },
43                     sub { my $row = shift; substr($row->censustract, 5) },
44                     'upload',
45                     'download',
46                     'technology_code',
47                     sub { $cgi->param('_type') eq 'xml' ? '0' : '' },  # doesn't really work
48                     'quantity',
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   
80 for ( qw(agentnum magic) ) {
81   $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
82 }
83 $search_hash{'classnum'} = [ $cgi->param('classnum') ];
84
85 my @column_option = grep { /^\d+$/ } $cgi->param('part1_column_option')
86   if $cgi->param('part1_column_option');
87
88 my @row_option = grep { /^\d+$/ } $cgi->param('part1_row_option')
89   if $cgi->param('part1_row_option');
90
91 my @technology_option = &FS::Report::FCC_477::parse_technology_option($cgi);
92
93 my $rowcount = 1;
94 foreach my $row ( @row_option ) {
95   my $columncount = 2;
96   foreach my $column ( @column_option ) {
97     my $tech_code = 0;
98     foreach my $technology ( @technology_option ) {
99       $tech_code++;
100       next unless $technology;
101       my @report_option = ();
102       push @report_option, $row if $row;
103       push @report_option, $column if $column;
104       push @report_option, $technology;
105       my $report_option = join(',', @report_option) if @report_option;
106  
107       my $sql_query = FS::cust_pkg->search(
108         { %search_hash,
109           ($report_option ? ( 'report_option' => $report_option ) : () ),
110         }
111       );
112       my $extracolumns = "$rowcount AS upload, $columncount AS download, $tech_code as technology_code";
113       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";
114       $sql_query->{select} = "count(*) AS quantity, $extracolumns, censustract, $percent";
115       $sql_query->{extra_sql} =~ /^(.*)(ORDER BY pkgnum)(.*)$/s
116         or die "couldn't parse extra_sql";
117       $sql_query->{extra_sql} = "$1 GROUP BY censustract $3";
118       push @sql_query, $sql_query;
119     }
120     $columncount++;
121   }
122   $rowcount++;
123 }
124
125 my $count_query = 'SELECT count(*) FROM ( ('.
126    join( ') UNION ALL (',
127           map { my $extra = $_->{extra_sql};  my $addl = $_->{addl_from};
128                 "SELECT censustract from cust_pkg $addl $extra";
129               }
130           @sql_query
131        ). ') ) AS foo';
132
133 my $link = 'cust_pkg.cgi?'.
134            join(';', map{ "$_=". $search_hash{$_} } keys %search_hash). ';';
135 my $link_suffix = sub { my $row = shift;
136                         my $result = 'censustract='. $row->censustract. ';';
137                         $result .= 'report_option='. @row_option[$row->upload - 1]
138                           if @row_option[$row->upload - 1];
139                         $result .= 'report_option='. @column_option[$row->download - 1]
140                           if @column_option[$row->download - 1];
141                         $result;
142                       };
143 </%init>