add package class selection to package summary report, RT#23555
[freeside.git] / httemplate / search / cust_pkg_summary.cgi
1 <% include('/elements/header.html', $title) %>
2 <% include('/elements/table-grid.html') %>
3   <TR>
4 % foreach (@head) {
5     <TH CLASS="grid" BGCOLOR="#cccccc"><% $_ %></TH>
6 % }
7   </TR>
8 % my $r=0;
9 % foreach my $row (@rows) {
10   <TR>
11 %   foreach (@$row) {
12     <TD CLASS="grid" ALIGN="right" BGCOLOR="<% $r % 2 ? '#ffffff' : '#eeeeee' %>"><% $_ %></TD>
13 %   }
14   </TR>
15 %   $r++;
16 % }
17   <TR>
18 % foreach (@totals) {
19     <TD CLASS="grid" ALIGN="right" BGCOLOR="<% $r % 2 ? '#ffffff' : '#eeeeee' %>"><B><% $_ %></B></TD>
20 % }
21   </TR>
22 </TABLE>
23 <%init>
24
25 my $curuser = $FS::CurrentUser::CurrentUser;
26
27 die "access denied"
28   unless $curuser->access_right('Summarize packages');
29
30 my $title = 'Package Summary Report';
31 my ($begin, $end) = FS::UI::Web::parse_beginning_ending($cgi);
32 if($begin > 0) {
33   $title = "$title (".
34     $cgi->param('beginning').' - '.$cgi->param('ending').')';
35 }
36
37 my @h_sql = FS::h_cust_pkg->sql_h_search($end);
38
39 my ($end_sql, $addl_from) = @h_sql[1,3];
40 $end_sql =~ s/ORDER BY.*//; # breaks aggregate queries
41
42 my $begin_sql = $end_sql;
43 $begin_sql =~ s/$end/$begin/g;
44
45 my $active_sql = FS::cust_pkg->active_sql;
46 my $suspended_sql = FS::cust_pkg->suspended_sql;
47 my @conds = (
48   # SQL WHERE clauses for each column of the table.
49   " $begin_sql AND ($active_sql OR $suspended_sql)",
50   '',
51   " $end_sql AND ($active_sql OR $suspended_sql)",
52   " $end_sql AND $active_sql",
53   " $end_sql AND $suspended_sql",
54   );
55
56 $_ =~ s/\bcust_pkg/maintable/g foreach @conds;
57
58 my @head = ('Package', 'Before Period', 'Sales', 'Total', 'Active', 'Suspended');
59 my @rows = ();
60 my @totals = ('Total', 0, 0, 0, 0, 0);
61
62 if( !$begin ) {
63   splice @conds, 1, 1;
64   splice @head, 1, 1;
65 }
66
67 my $agentnums_sql = $curuser->agentnums_sql(
68                       'null'       => 1,
69                       'table'      => 'part_pkg',
70                     );
71
72 my $extra_sql = " WHERE $agentnums_sql";
73
74 #tiny bit of false laziness w/cust_pkg.pm::search
75 if ( grep { $_ eq 'classnum' } $cgi->param ) {
76   if ( $cgi->param('classnum') eq '' ) {
77     $extra_sql .= ' AND part_pkg.classnum IS NULL';
78   } elsif ( $cgi->param('classnum') =~ /^(\d+)$/ && $1 ne '0' ) {
79     $extra_sql .= " AND part_pkg.classnum = $1 ";
80   }
81 }
82
83 foreach my $part_pkg (qsearch({ 'table'     => 'part_pkg',
84                                 'hashref'   => {},
85                                 'extra_sql' => $extra_sql,
86                              })
87                      )
88 {
89   my @row = ();
90   next if !$part_pkg->freq; # exclude one-time packages
91   push @row, $part_pkg->pkg;
92   my $i=1;
93   foreach my $cond (@conds) {
94     if($cond) {
95       my $result = qsearchs({ 
96                             'table'     => 'h_cust_pkg',
97                             'addl_from' => $addl_from.
98                                            ' LEFT JOIN cust_main USING ( custnum )',
99
100                             'hashref'   => {},
101                             'select'    => 'count(*)',
102                             'extra_sql' => 'WHERE pkgpart = '.$part_pkg->pkgpart.$cond.
103                                            ' AND '. $curuser->agentnums_sql(
104                                                       'table' => 'cust_main',
105                                                     ),
106                             });
107       $row[$i] = $result->getfield('count');
108       $totals[$i] += $row[$i];
109     }
110     $i++;
111   }
112   $row[2] = $row[3]-$row[1];
113   $totals[2] += $row[2];
114   push @rows, \@row;
115 }
116 </%init>