combine ticket notification scrips, #15353
[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                           my $state = substr($row->censustract, 0, 2);
34                           $state_hash{$state}++;
35                           substr($row->censustract, 2, 3) || 'None';
36                         },
37                     sub { my $row = shift;
38                           substr($row->censustract, 5) || 'None';
39                         },
40                     'upload',
41                     'download',
42                     'technology_code',
43                     sub { $cgi->param('_type') eq 'xml' ? '0' : '' },#broken
44                     sub { my $row = shift;
45                           my $state = substr($row->censustract, 0, 2);
46                           $state_pkgcount{$state} += $row->quantity;
47                           $row->quantity;
48                         },
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 my %state_pkgcount = ();
80   
81 for ( qw(agentnum magic) ) {
82   $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
83 }
84 $search_hash{'classnum'} = [ $cgi->param('classnum') ]
85   if grep { $_ eq 'classnum' } $cgi->param;
86
87 my @column_option = grep { /^\d+$/ } $cgi->param('part1_column_option')
88   if $cgi->param('part1_column_option');
89
90 my @row_option = grep { /^\d+$/ } $cgi->param('part1_row_option')
91   if $cgi->param('part1_row_option');
92
93 my @technology_option = &FS::Report::FCC_477::parse_technology_option($cgi);
94
95 my $rowcount = 1;
96 foreach my $row ( @row_option ) {
97   my $columncount = 2;
98   foreach my $column ( @column_option ) {
99     my $tech_code = 0;
100     foreach my $technology ( @technology_option ) {
101       $tech_code++;
102       next unless $technology;
103       my @report_option = ();
104       push @report_option, $row if $row;
105       push @report_option, $column if $column;
106       push @report_option, $technology;
107       my $report_option = join(',', @report_option) if @report_option;
108  
109       my $sql_query = FS::cust_pkg->search(
110         { %search_hash,
111           ($report_option ? ( 'report_option' => $report_option ) : () ),
112         }
113       );
114       my $extracolumns = "$rowcount AS upload, $columncount AS download, $tech_code as technology_code";
115       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";
116       $sql_query->{select} = "count(*) AS quantity, $extracolumns, censustract, $percent";
117       $sql_query->{extra_sql} =~ /^(.*)(ORDER BY pkgnum)(.*)$/s
118         or die "couldn't parse extra_sql";
119       $sql_query->{extra_sql} = "$1 GROUP BY censustract $3";
120       push @sql_query, $sql_query;
121     }
122     $columncount++;
123   }
124   $rowcount++;
125 }
126
127 my $count_query = 'SELECT count(*) FROM ( ('.
128    join( ') UNION ALL (',
129           map { my $extra = $_->{extra_sql};  my $addl = $_->{addl_from};
130                 "SELECT censustract from cust_pkg $addl $extra";
131               }
132           @sql_query
133        ). ') ) AS foo';
134
135 my $link = 'cust_pkg.cgi?'. join(';',
136                                       map { my $key = $_;
137                                             my @values = ref($search_hash{$_}) 
138                                               ? @{ $search_hash{$_} }
139                                               : $search_hash{$_};
140                                             map { "$key=$_" } @values;
141                                           }
142                                           keys %search_hash        
143                                 ). ';';
144
145 my $link_suffix = sub { my $row = shift;
146                         my $result = 'censustract='. $row->censustract. ';';
147                         my @ro = grep $_,
148                           @row_option[$row->upload - 1],
149                           @column_option[$row->download - 2],
150                           @technology_option[$row->technology_code - 1],
151                         ;
152                         $result .= 'report_option='. join(',',@ro) if @ro;
153                         $result;
154                       };
155
156 my $html_foot = sub {
157   if (scalar(keys %state_hash) > 1) {
158
159     my $roa_r = join(',', grep $_, @row_option);
160     $roa_r = ";report_option_any_r=$roa_r" if $roa_r;
161
162     my $roa_c = join(',', grep $_, @column_option);
163     $roa_c = ";report_option_any_c=$roa_c" if $roa_c;
164
165     my $roa_t = join(',', grep $_, @technology_option);
166     $roa_t = ";report_option_any_t=$roa_t" if $roa_t;
167     
168     '<BR><B>WARNING: multiple states found</B><BR>'.
169     '<TABLE BORDER=0>'. #nicer formatting someday
170     join('', map { '<TR>'.
171                      '<TD>'.
172                        ( &FS::Report::FCC_477::statenum2state($_) || 'None' ).
173                      '</TD>'.
174                      '<TD>'.
175                        qq(<A HREF="${link}censustract2=$_$roa_r$roa_c$roa_t">).
176                        $state_pkgcount{$_}.
177                       ' packages</A> in '.
178                        $state_hash{$_}. ' census tracts'.
179                      '</TD>'.
180                    '</TR>';
181                  }
182                  keys %state_hash
183         ).
184     '</TABLE>';
185   } else { 
186     '';
187   }
188 };
189
190 </%init>