link new tax report to cust_credit_bill_pkg for credits, RT#12332
[freeside.git] / httemplate / search / 477partIIA.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 IIA</H2>';
23 my %search_hash = ();
24   
25 for ( qw(agentnum magic classnum) ) {
26   $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
27 }
28
29 my @row_option = grep { /^\d+$/ } $cgi->param('part2a_row_option')
30   if $cgi->param('part2a_row_option');
31
32 # fudge in two rows of LD carrier
33 unshift @row_option, $row_option[0];
34
35 # fudge in the first pair of rows
36 unshift @row_option, '';
37 unshift @row_option, '';
38
39 my $query = 'SELECT '. join(' UNION SELECT ', 1..11);
40
41 my $total_count = 0;
42 my $column_value = sub {
43   my $row = shift;
44
45   my @report_option = ( $row_option[$row - 1] || '' );
46
47   my $sql_query = FS::cust_pkg->search(
48     { %search_hash, 'report_option' => join(',', @report_option) }
49   );
50
51   my $count_sql = delete($sql_query->{'count_query'});
52   if ( $row == 2 || $row == 4 ) {
53     $count_sql =~ s/COUNT\(\*\) FROM/COALESCE( sum(CASE WHEN cust_main.company IS NULL OR cust_main.company = '' THEN fcc_ds0s END), 0 ) FROM/
54       or die "couldn't parse count_sql";
55   } else {
56     $count_sql =~ s/COUNT\(\*\) FROM/COALESCE( sum(fcc_ds0s), 0 ) FROM/
57       or die "couldn't parse count_sql";
58   }
59
60   my $count_sth = dbh->prepare($count_sql)
61     or die "Error preparing $count_sql: ". dbh->errstr;
62   $count_sth->execute
63     or die "Error executing $count_sql: ". $count_sth->errstr;
64   my $count_arrayref = $count_sth->fetchrow_arrayref;
65   my $count = $count_arrayref->[0];
66
67   $total_count = $count if $row == 1;
68   $count = sprintf('%.2f', $total_count ? 100*$count/$total_count : 0)
69     if $row != 1;
70
71   return "$count";
72 };
73
74 my @headers = (
75   '',
76   'End user lines',
77   'UNE-P replacement',
78   'UNE (unswitched)',
79   'UNE-P',
80 );
81
82 my @xml_elements = (
83   sub { my $row = shift; my $rownum = $row->[0] + 1; "PartII_${rownum}a" },
84   sub { my $row = shift; my $rownum = $row->[0] + 1; "PartII_${rownum}b" },
85   sub { my $row = shift; my $rownum = $row->[0] + 1; "PartII_${rownum}c" },
86   sub { my $row = shift; my $rownum = $row->[0] + 1; "PartII_${rownum}d" },
87 );
88
89
90 my @rows = (
91   'lines',
92   '% residential',
93   '% LD carrier',
94   '% residential and LD carrier',
95   '% own loops',
96   '% obtained unswitched UNE loops',
97   '% UNE-P',
98   '% UNE-P replacement',
99   '% FTTP',
100   '% coax',
101   '% wireless',
102 );
103
104 my @fields = (
105   sub { my $row = shift; $rows[$row->[0] - 1]; },
106   sub { my $row = shift; &{$column_value}($row->[0]); },
107   sub { 0; },
108   sub { 0; },
109   sub { 0; },
110 );
111
112 shift @fields if $cgi->param('_type') eq 'xml';
113 </%init>