1 <% include('/elements/header.html', $title) %>
2 <% include('/elements/table-grid.html') %>
5 <TH CLASS="grid" BGCOLOR="#cccccc"><% $_ %></TH>
9 % foreach my $row (@rows) {
12 <TD CLASS="grid" STYLE="border: 1px solid #cccccc" ALIGN="right" BGCOLOR="<% $r % 2 ? '#ffffff' : '#eeeeee' %>"><% $_ %></TD>
19 <TD CLASS="grid" STYLE="border: 1px solid #cccccc" ALIGN="right" BGCOLOR="<% $r % 2 ? '#ffffff' : '#eeeeee' %>"><B><% $_ %></B></TD>
25 unless $FS::CurrentUser::CurrentUser->access_right('List packages');
27 my $money_char = FS::Conf->new()->config('money_char') || '$';
31 my $title = 'Suspension/Unsuspension Report';
32 my ($begin, $end) = FS::UI::Web::parse_beginning_ending($cgi);
35 ($cgi->param('beginning') || 'beginning').' - '.
36 ($cgi->param('ending') || 'present').')';
40 my $begin_sql = $begin ? "AND h2.history_date > $begin" : '';
41 my $end_sql = $end ? "AND h2.history_date < $end" : '';
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
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.
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)',
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() } };
68 push @pay_cond, "cust_bill_pay._date < $end" if $end;
69 push @pay_cond, "cust_bill_pay._date > $begin" if $begin;
72 $pay_cond = 'WHERE '.join(' AND ', @pay_cond) if @pay_cond;
75 map { $_->pkgpart => $_->total_pay }
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',
85 my @head = ('Package', 'Suspended', 'Unsuspended', 'Payments');
87 my @totals = map {0} @head;
90 foreach my $part_pkg (qsearch('part_pkg', {} )) {
92 next if !$part_pkg->freq; # exclude one-time packages
93 my $pkgpart = $part_pkg->pkgpart;
96 $results[0]->{$pkgpart} || 0,
97 $results[1]->{$pkgpart} || 0,
98 sprintf("%.02f",$pkg_payments->{$pkgpart});
100 $totals[$_] += $row[$_] foreach (1..3);
101 $row[3] = $money_char.$row[3];
105 $totals[3] = $money_char.$totals[3];