combine ticket notification scrips, #15353
[freeside.git] / httemplate / search / 477partIIB.html
1 <% include( 'elements/search.html',
2                   'html_init'        => $html_init,
3                   'name'             => 'lines',
4                   'query'            => $query,
5                   'count_query'      => 'SELECT 11',
6                   'really_disable_download' => 1,
7                   'disable_download' => 1,
8                   'nohtmlheader'     => 1,
9                   'disable_total'    => 1,
10                   'header'           => [ @headers ],
11                   'xml_elements'     => [ @xml_elements ],
12                   'fields'           => [ @fields ],
13               )
14 %>
15 <%init>
16
17 my $curuser = $FS::CurrentUser::CurrentUser;
18
19 die "access denied"
20   unless $curuser->access_right('List packages');
21
22 my $html_init = '<H2>Part IIB</H2>';
23 my %search_hash = ();
24   
25 for ( qw(agentnum magic) ) {
26   $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
27 }
28 $search_hash{'classnum'} = [ $cgi->param('classnum') ];
29
30 my @row_option = grep { /^\d+$/ } $cgi->param('part2b_row_option')
31   if $cgi->param('part2b_row_option');
32
33 # fudge in 2nd row
34 unshift @row_option, $row_option[0];
35
36 my $query = 'SELECT '. join(' UNION SELECT ', 1..8);
37
38 my $total_count = 0;
39 my $column_value = sub {
40   my $row = shift;
41
42   my @report_option = ( $row_option[$row - 1] || '' );
43
44   my $sql_query = FS::cust_pkg->search(
45     { %search_hash, 'report_option' => join(',', @report_option) }
46   );
47
48   my $count_sql = delete($sql_query->{'count_query'});
49   if ( $row == 2 ) {
50     $count_sql =~ s/COUNT\(\*\) FROM/sum(COALESCE(CASE WHEN cust_main.company IS NULL OR cust_main.company = '' THEN   CASE WHEN part_pkg.fcc_ds0s IS NOT NULL AND part_pkg.fcc_ds0s > 0 THEN part_pkg.fcc_ds0s WHEN pkg_class.fcc_ds0s IS NOT NULL AND pkg_class.fcc_ds0s > 0 THEN pkg_class.fcc_ds0s ELSE 0 END   ELSE 0 END, 0) ) FROM/
51       or die "couldn't parse count_sql";
52   } else {
53     $count_sql =~ s/COUNT\(\*\) FROM/sum(COALESCE(CASE WHEN part_pkg.fcc_ds0s IS NOT NULL AND part_pkg.fcc_ds0s > 0 THEN part_pkg.fcc_ds0s WHEN pkg_class.fcc_ds0s IS NOT NULL AND pkg_class.fcc_ds0s > 0 THEN pkg_class.fcc_ds0s ELSE 0 END, 0)) FROM/
54       or die "couldn't parse count_sql";
55   }
56
57   my $count_sth = dbh->prepare($count_sql)
58     or die "Error preparing $count_sql: ". dbh->errstr;
59   $count_sth->execute
60     or die "Error executing $count_sql: ". $count_sth->errstr;
61   my $count_arrayref = $count_sth->fetchrow_arrayref;
62   my $count = $count_arrayref->[0];
63
64   $total_count = $count if $row == 1;
65   $count = sprintf('%.2f', $total_count ? 100*$count/$total_count : 0)
66     if $row != 1;
67
68   return "$count";
69 };
70
71 my @headers = (
72   '',
73   'without broadband',
74   'with broadband',
75   'wholesale',
76 );
77
78 my @xml_elements = (
79   sub { my $row = shift; my $rownum = $row->[0] + 1; "PartII_${rownum}a" },
80   sub { my $row = shift; my $rownum = $row->[0] + 1; "PartII_${rownum}b" },
81   sub { my $row = shift; my $rownum = $row->[0] + 1; "PartII_${rownum}c" },
82 );
83
84 my @rows = (
85   'total number',
86   '% residential',
87   '% nomadic',
88   '% copper',
89   '% FTTP',
90   '% coax',
91   '% wireless',
92   '% other broadband',
93 );
94
95 my @fields = (
96   sub { my $row = shift; $rows[$row->[0] - 1]; },
97   sub { 0; },
98   sub { my $row = shift; &{$column_value}($row->[0]); },
99   sub { 0; },
100 );
101
102 shift @fields if $cgi->param('_type') eq 'xml';
103 </%init>