fix sorting of NULL companies differently than empty companies
[freeside.git] / httemplate / search / report_prepaid_income.cgi
1 <!-- mason kludge -->
2 <%
3
4   #doesn't yet deal with daily/weekly packages
5
6   #needs to be re-written in sql for efficiency
7
8   my $now = $cgi->param('date') && str2time($cgi->param('date')) || time;
9   $now =~ /^(\d+)$/ or die "unparsable date?";
10   $now = $1;
11
12   my %prepaid;
13
14   my @cust_bill_pkg =
15     grep { $_->cust_pkg && $_->cust_pkg->part_pkg->freq !~ /^([01]|\d+[dw])$/ }
16       qsearch( 'cust_bill_pkg', {
17                                   'recur' => { op=>'!=', value=>0 },
18                                   'edate' => { op=>'>', value=>$now },
19                                 }, );
20
21   foreach my $cust_bill_pkg ( @cust_bill_pkg ) {
22
23     #conceptual false laziness w/texas tax exempt_amount stuff in
24     #FS::cust_main::bill
25
26     my $freq = $cust_bill_pkg->cust_pkg->part_pkg->freq;
27     my $per_month = sprintf("%.2f", $cust_bill_pkg->recur / $freq);
28
29     my($mon, $year) = (localtime($cust_bill_pkg->sdate) )[4,5];
30     $mon+=2; $year+=1900;
31
32     foreach my $which_month ( 2 .. $freq ) {
33       until ( $mon < 13 ) { $mon -= 12; $year++; }
34       $prepaid{"$year-$mon"} += $per_month;
35       $mon++;
36     }
37
38   }
39
40   my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
41
42 %>
43
44 <%= header( 'Prepaid Income (Unearned Revenue) Report',
45             menubar( 'Main Menu'=>$p, ) )               %>
46 <%= table() %>
47 <%
48
49   my $total = 0;
50
51   my ($now_mon, $now_year) = (localtime($now))[4,5];
52   $now_mon+=2; $now_year+=1900;
53   until ( $now_mon < 13 ) { $now_mon -= 12; $now_year++; }
54
55   my $subseq = 0;
56   for my $year ( $now_year .. 2037 ) {
57     for my $mon ( ( $subseq++ ? 1 : $now_mon ) .. 12 ) {
58       if ( $prepaid{"$year-$mon"} ) {
59         $total += $prepaid{"$year-$mon"};
60         %> <TR><TD ALIGN="right"><%= $mon[$mon-1]. ' '. $year %></TD>
61                <TD ALIGN="right">
62                  <%= sprintf("%.2f", $prepaid{"$year-$mon"} ) %>
63                </TD>
64            </TR>
65         <%
66       }
67     }
68
69   }
70
71 %>
72 <TR><TH>Total</TH><TD ALIGN="right"><%= sprintf("%.2f", $total) %></TD></TR>
73 </TABLE>
74 </BODY>
75 </HTML>