combine ticket notification scrips, #15353
[freeside.git] / httemplate / search / cust_pkg_susp.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" STYLE="border: 1px solid #cccccc" ALIGN="right" BGCOLOR="<% $r % 2 ? '#ffffff' : '#eeeeee' %>"><% $_ %></TD>
13 %   }
14   </TR>
15 %   $r++;
16 % }
17   <TR>
18 % foreach (@totals) {
19     <TD CLASS="grid" STYLE="border: 1px solid #cccccc" 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 $money_char = FS::Conf->new()->config('money_char') || '$';
28
29 $FS::Record::DEBUG=0;
30
31 my $title = 'Suspension/Unsuspension Report';
32 my ($begin, $end) = FS::UI::Web::parse_beginning_ending($cgi);
33 if($begin > 0) {
34   $title = "$title (".
35     ($cgi->param('beginning') || 'beginning').' - '.
36     ($cgi->param('ending') || 'present').')';
37 }
38
39
40 my $begin_sql = $begin ? "AND h2.history_date > $begin" : '';
41 my $end_sql   = $end   ? "AND h2.history_date < $end" : '';
42
43 my $h_sql = # self-join FTW!
44 "SELECT h1.pkgpart, count(h1.pkgnum) as pkgcount
45   FROM h_cust_pkg AS h1 INNER JOIN h_cust_pkg AS h2 ON (h1.pkgnum = h2.pkgnum)
46   WHERE h1.history_action = 'replace_old' AND h2.history_action = 'replace_new'
47   AND h2.historynum - h1.historynum = 1
48   $begin_sql $end_sql";
49 # This assumes replace_old and replace_new records get consecutive 
50 # numbers. That's true in every case I've seen but is not actually 
51 # enforced anywhere.  If this is a problem we can match them up 
52 # line by line but that's cumbersome.
53
54 my @conds = (
55   '(h1.susp is null OR h1.susp = 0) AND (h2.susp is not null AND h2.susp != 0)',
56   '(h1.susp is not null AND h1.susp != 0) AND (h2.susp is null OR h2.susp = 0)',
57 );
58
59 my @results;
60 foreach my $cond (@conds) {
61   my $sql = "$h_sql AND $cond GROUP BY h1.pkgpart";
62   my $sth = dbh->prepare($sql) or die dbh->errstr;
63   $sth->execute() or die $sth->errstr;
64   push @results, { map { @$_ } @{ $sth->fetchall_arrayref() } };
65
66  
67 my @pay_cond;
68 push @pay_cond, "cust_bill_pay._date < $end" if $end;
69 push @pay_cond, "cust_bill_pay._date > $begin" if $begin;
70
71 my $pay_cond = '';
72 $pay_cond = 'WHERE '.join(' AND ', @pay_cond) if @pay_cond;
73
74 my $pkg_payments = {
75   map { $_->pkgpart => $_->total_pay }
76   qsearch({
77     'table'     => 'cust_pkg',
78     'select'    => 'pkgpart, sum(cust_bill_pay_pkg.amount) AS total_pay',
79     'addl_from' => 'INNER JOIN cust_bill_pkg USING (pkgnum)
80                     INNER JOIN cust_bill_pay_pkg USING (billpkgnum)
81                     INNER JOIN cust_bill_pay USING (billpaynum)',
82     'extra_sql' => $pay_cond . ' GROUP BY pkgpart',
83 }) };
84
85 my @head = ('Package', 'Suspended', 'Unsuspended', 'Payments');
86 my @rows = ();
87 my @totals = map {0} @head;
88 $totals[0] = 'Total';
89
90 foreach my $part_pkg (qsearch('part_pkg', {} )) {
91   my @row = ();
92   next if !$part_pkg->freq; # exclude one-time packages
93   my $pkgpart = $part_pkg->pkgpart;
94   push @row, 
95     $part_pkg->pkg,
96     $results[0]->{$pkgpart} || 0,
97     $results[1]->{$pkgpart} || 0,
98     sprintf("%.02f",$pkg_payments->{$pkgpart});
99
100   $totals[$_] += $row[$_] foreach (1..3);
101   $row[3] = $money_char.$row[3];
102
103   push @rows, \@row;
104 }
105 $totals[3] = $money_char.$totals[3];
106
107 </%init>