Merge branch 'master' of https://github.com/jgoodman/Freeside
[freeside.git] / httemplate / search / 477partIIA.html
1 % if ( $cgi->param('_type') eq 'xml' ) {
2 %   my @cols = qw(a b c d);
3 %   for ( my $row = 0; $row < scalar(@rows); $row++ ) {
4 %     for my $col (0..3) {
5 %       if ( exists($data[$col][$row]) and $data[$col][$row] > 0 ) {
6 <PartII_<% $row + 1 %><% $cols[$col] %>>\
7 <% $data[$col][$row] %>\
8 </PartII_<% $row + 1 %><% $cols[$col] %>>
9 %       }
10 %     } #for $col
11 %   } #for $row
12 % } else { # HTML mode
13 % # fake up the search-html.html header
14 <H2>Part IIA</H2>
15 <TABLE>
16   <TR><TD VALIGN="bottom"><BR></TD></TR>
17   <TR><TD COLSPAN=2>
18   <TABLE CLASS="grid" CELLSPACING=0>
19     <TR>
20 % foreach (@row1_headers) {
21       <TH><% $_ %></TH>
22 % }
23     </TR>
24 % my $row = 0;
25 % foreach my $rowhead (@rows) {
26     <TR CLASS="row<%$row % 2%>"> 
27       <TD STYLE="text-align: left; font-weight: bold"><% $rowhead %></TD>
28 %     for my $col (0..3) {
29       <TD>
30 %       if ( exists($data[$col][$row]) ) {
31       <% $data[$col][$row] %>
32 %       }
33       </TD>
34 %     } # for $col
35     </TR>
36 %   $row++;
37 % } #for $rowhead
38   </TABLE>
39   </TD></TR>
40 </TABLE>
41 % } #XML/HTML
42 <%init>
43 my $curuser = $FS::CurrentUser::CurrentUser;
44
45 die "access denied"
46   unless $curuser->access_right('List packages');
47
48 my %opt = @_;
49 my %search_hash = ();
50
51 $search_hash{'agentnum'} = $cgi->param('agentnum');
52 $search_hash{'state'}    = $cgi->param('state');
53 $search_hash{'classnum'} = [ $cgi->param('classnum') ];
54 $search_hash{'active'}   = [ $opt{date}, $opt{date} ];
55
56 my @row_option;
57 foreach ($cgi->param('part2a_row_option')) {
58   push @row_option, (/^\d+$/ ? $_ : undef);
59 }
60
61 my $is_residential = " AND COALESCE(cust_main.company, '') = ''";
62 my $has_report_option = sub {
63   map {
64     defined($row_option[$_]) ?
65     " AND EXISTS(
66       SELECT 1 FROM part_pkg_option 
67       WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
68       AND optionname = 'report_option_" . $row_option[$_]."'
69       AND optionvalue = '1'
70     )" : ' AND FALSE'
71   } @_
72 };
73
74 # an arrayref for each column
75 my @data;
76 # get the skeleton of the query
77 my $sql_query = FS::cust_pkg->search(\%search_hash);
78 my $from_where = $sql_query->{'count_query'};
79 $from_where =~ s/^SELECT COUNT\(\*\) //;
80
81 # for row 1
82 my $query_ds0 = "SELECT SUM(COALESCE(part_pkg.fcc_ds0s, pkg_class.fcc_ds0s, 0))
83   $from_where AND fcc_voip_class = '4'"; # 4 = Local Exchange
84
85 my $total_lines = FS::Record->scalar_sql($query_ds0);
86 # always return zero for the number of resold lines, until an actual ILEC
87 # starts using this report
88
89 @data = (
90   [ $total_lines ],
91   [ 0 ],
92   [ 0 ],
93   [ 0 ],
94 );
95
96 my @row_conds = (
97   $is_residential,
98   $has_report_option->(0), # LD carrier
99   ($has_report_option->(0))[0] . $is_residential,
100   $has_report_option->(1..7),
101 );
102
103 if ( $total_lines > 0 ) {
104   foreach (@row_conds) {
105     my $sql = $query_ds0 . $_;
106     my $lines = FS::Record->scalar_sql($sql);
107     my $percent = sprintf('%.3f', 100 * $lines / $total_lines);
108     push @{ $data[0] }, $percent;
109   }
110 }
111
112 my @rows = (
113   'lines',
114   '% residential',
115   '% LD carrier',
116   '% residential and LD',
117   '% owned loops',
118   '% unswitched UNE',
119   '% UNE-P',
120   '% UNE-P replacement',
121   '% FTTP',
122   '% coax',
123   '% wireless',
124 );
125
126 my @row1_headers = (
127   '',
128   'End user lines',
129   'UNE-P replacement',
130   'unswitched UNE',
131   'UNE-P',
132 );
133
134 </%init>