combine ticket notification scrips, #15353
[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 die "access denied"
25   unless $FS::CurrentUser::CurrentUser->access_right('List packages');
26
27 my $title = 'Package Summary Report';
28 my ($begin, $end) = FS::UI::Web::parse_beginning_ending($cgi);
29 if($begin > 0) {
30   $title = "$title (".
31     $cgi->param('beginning').' - '.$cgi->param('ending').')';
32 }
33
34 my @h_sql = FS::h_cust_pkg->sql_h_search($end);
35
36 my ($end_sql, $addl_from) = @h_sql[1,3];
37 $end_sql =~ s/ORDER BY.*//; # breaks aggregate queries
38
39 my $begin_sql = $end_sql;
40 $begin_sql =~ s/$end/$begin/g;
41
42 my $active_sql = FS::cust_pkg->active_sql;
43 my $suspended_sql = FS::cust_pkg->suspended_sql;
44 my @conds = (
45   # SQL WHERE clauses for each column of the table.
46   " $begin_sql AND ($active_sql OR $suspended_sql)",
47   '',
48   " $end_sql AND ($active_sql OR $suspended_sql)",
49   " $end_sql AND $active_sql",
50   " $end_sql AND $suspended_sql",
51   );
52
53 $_ =~ s/\bcust_pkg/maintable/g foreach @conds;
54
55 my @head = ('Package', 'Before Period', 'Sales', 'Total', 'Active', 'Suspended');
56 my @rows = ();
57 my @totals = ('Total', 0, 0, 0, 0, 0);
58
59 if( !$begin ) {
60   splice @conds, 1, 1;
61   splice @head, 1, 1;
62 }
63
64 foreach my $part_pkg (qsearch('part_pkg', {} )) {
65   my @row = ();
66   next if !$part_pkg->freq; # exclude one-time packages
67   push @row, $part_pkg->pkg;
68   my $i=1;
69   foreach my $cond (@conds) {
70     if($cond) {
71       my $result = qsearchs({ 
72                             'table'     => 'h_cust_pkg',
73                             'hashref'   => {},
74                             'select'    => 'count(*)',
75                             'addl_from' => $addl_from,
76                             'extra_sql' => 'WHERE pkgpart = '.$part_pkg->pkgpart.$cond,
77                             });
78       $row[$i] = $result->getfield('count');
79       $totals[$i] += $row[$i];
80     }
81     $i++;
82   }
83   $row[2] = $row[3]-$row[1];
84   $totals[2] += $row[2];
85   push @rows, \@row;
86 }
87 </%init>