add "Summarize packages" ACL, RT#16534
[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 foreach my $part_pkg (qsearch({ 'table'     => 'part_pkg',
75                                 'hashref'   => {},
76                                 'extra_sql' => $extra_sql,
77                              })
78                      )
79 {
80   my @row = ();
81   next if !$part_pkg->freq; # exclude one-time packages
82   push @row, $part_pkg->pkg;
83   my $i=1;
84   foreach my $cond (@conds) {
85     if($cond) {
86       my $result = qsearchs({ 
87                             'table'     => 'h_cust_pkg',
88                             'addl_from' => $addl_from.
89                                            ' LEFT JOIN cust_main USING ( custnum )',
90
91                             'hashref'   => {},
92                             'select'    => 'count(*)',
93                             'extra_sql' => 'WHERE pkgpart = '.$part_pkg->pkgpart.$cond.
94                                            ' AND '. $curuser->agentnums_sql(
95                                                       'table' => 'cust_main',
96                                                     ),
97                             });
98       $row[$i] = $result->getfield('count');
99       $totals[$i] += $row[$i];
100     }
101     $i++;
102   }
103   $row[2] = $row[3]-$row[1];
104   $totals[2] += $row[2];
105   push @rows, \@row;
106 }
107 </%init>