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