Merge branch 'master' of https://github.com/jgoodman/Freeside
[freeside.git] / httemplate / search / 477partIIB.html
1 % if ( $cgi->param('_type') eq 'xml' ) {
2 %   my @cols = qw(a b c);
3 %   for ( my $row = 0; $row < scalar(@rows); $row++ ) {
4 %     for my $col (0..2) {
5 %       if ( exists($data[$col][$row]) ) {
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 IIB</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 (@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..2) {
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
44 my $curuser = $FS::CurrentUser::CurrentUser;
45
46 die "access denied"
47   unless $curuser->access_right('List packages');
48
49 my %opt = @_;
50 my %search_hash = ();
51
52 $search_hash{'agentnum'} = $cgi->param('agentnum');
53 $search_hash{'state'}    = $cgi->param('state');
54 $search_hash{'classnum'} = [ $cgi->param('classnum') ];
55 $search_hash{'active'}   = [ $opt{date}, $opt{date} ];
56
57 my @row_option;
58 foreach ($cgi->param('part2b_row_option')) {
59   push @row_option, (/^\d+$/ ? $_ : undef);
60 }
61
62 my $is_residential = " AND COALESCE(cust_main.company, '') = ''";
63 my $has_report_option = sub {
64   map {
65     defined($row_option[$_]) ?
66     " AND EXISTS(
67       SELECT 1 FROM part_pkg_option 
68       WHERE part_pkg_option.pkgpart = part_pkg.pkgpart
69       AND optionname = 'report_option_" . $row_option[$_]."'
70       AND optionvalue = '1'
71     )" : ' AND FALSE'
72   } @_
73 };
74
75 # an arrayref for each column
76 my @data;
77 # get the skeleton of the query
78 my $sql_query = FS::cust_pkg->search(\%search_hash);
79 my $from_where = $sql_query->{'count_query'};
80 $from_where =~ s/^SELECT COUNT\(\*\) //;
81 # columns 1 and 2
82 my $query_ds0 = "SELECT SUM(COALESCE(part_pkg.fcc_ds0s, pkg_class.fcc_ds0s, 0))
83   $from_where";
84 # column 3
85 my $query_custnum = "SELECT COUNT(DISTINCT cust_pkg.custnum) $from_where";
86
87 my @base_queries = ($query_ds0, $query_ds0, $query_custnum);
88 my @col_conds = (
89   # column 1
90   [
91     '',
92     $is_residential,
93     $has_report_option->(0), # nomadic
94   ],
95   # column 2
96   [
97     '',
98     $is_residential,
99     $has_report_option->(0..5),
100   ],
101   # column 3
102   [
103     ''
104   ]
105 );
106
107 my $col = 0;
108 foreach (@col_conds) {
109   my @col_data;
110   my $row = 0;
111   foreach my $cond (@{ $col_conds[$col] }) {
112     # three parts: the select expression, the VoIP class (column selection),
113     # and the row selection
114     my $query = $base_queries[$col] . 
115                 " AND part_pkg.fcc_voip_class = '".($col+1)."'
116                 $cond";
117     my $count = FS::Record->scalar_sql($query) || 0;
118     if ( $row == 0 ) {
119       $col_data[$row] = $count; # the raw count
120     } else {
121       if ( $col_data[0] == 0 ) {
122         $col_data[$row] = ''; # show nothing in this row, then
123       } else {
124         $col_data[$row] = sprintf('%.3f', 100 * $count / $col_data[0]) . '%';
125       }
126     } #if $row == 0
127     $row++;
128   }
129   $data[$col] = \@col_data;
130   $col++;
131 }
132
133
134 my @rows = (
135   'total number',
136   '% residential',
137   '% nomadic',
138   '% copper',
139   '% FTTP',
140   '% coax',
141   '% wireless',
142   '% other broadband',
143 );
144
145 my @headers = (
146   '',
147   'without broadband',
148   'with broadband',
149   'wholesale',
150 );
151
152 </%init>