9f045738a33da51feae64a76aa5b9cf20d4ea3cb
[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 = time;
9
10   my %prepaid;
11
12   my @cust_bill_pkg =
13     grep { $_->cust_pkg && $_->cust_pkg->part_pkg->freq !~ /^([01]|\d+[dw])$/ }
14       qsearch( 'cust_bill_pkg', {
15                                   'recur' => { op=>'!=', value=>0 },
16                                   'edate' => { op=>'>', value=>$now },
17                                 }, );
18
19   foreach my $cust_bill_pkg ( @cust_bill_pkg ) {
20
21     #conceptual false laziness w/texas tax exempt_amount stuff in
22     #FS::cust_main::bill
23
24     my $freq = $cust_bill_pkg->cust_pkg->part_pkg->freq;
25     my $per_month = sprintf("%.2f", $cust_bill_pkg->recur / $freq);
26
27     my($mon, $year) = (localtime($cust_bill_pkg->sdate) )[4,5];
28     $mon+=2; $year+=1900;
29
30     foreach my $which_month ( 2 .. $freq ) {
31       until ( $mon < 13 ) { $mon -= 12; $year++; }
32       $prepaid{"$year-$mon"} += $per_month;
33       $mon++;
34     }
35
36   }
37
38   my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
39
40 %>
41
42 <%= header('Prepaid Income Report', menubar( 'Main Menu'=>$p, ) ) %>
43 <%= table() %>
44 <%
45
46   my $total = 0;
47
48   my ($now_mon, $now_year) = (localtime($now))[4,5];
49   $now_mon+=2; $now_year+=1900;
50   until ( $now_mon < 13 ) { $now_mon -= 12; $now_year++; }
51
52   my $subseq = 0;
53   for my $year ( $now_year .. 2037 ) {
54     for my $mon ( ( $subseq++ ? 1 : $now_mon ) .. 12 ) {
55       if ( $prepaid{"$year-$mon"} ) {
56         $total += $prepaid{"$year-$mon"};
57         %> <TR><TD><%= $mon[$mon-1]. ' '. $year %></TD>
58                <TD ALIGN="right">
59                  <%= sprintf("%.2f", $prepaid{"$year-$mon"} ) %>
60                </TD>
61            </TR>
62         <%
63       }
64     }
65
66   }
67
68 %>
69 <TR><TH>Total</TH><TD ALIGN="right"><%= sprintf("%.2f", $total) %></TD></TR>
70 </TABLE>
71 </BODY>
72 </HTML>