update jscalendar
[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 $time = time;
9
10   my $now = $cgi->param('date') && str2time($cgi->param('date')) || $time;
11   $now =~ /^(\d+)$/ or die "unparsable date?";
12   $now = $1;
13
14   my( $total, $total_legacy ) = ( 0, 0 );
15
16   my @cust_bill_pkg =
17     grep { $_->cust_pkg && $_->cust_pkg->part_pkg->freq !~ /^([01]|\d+[dw])$/ }
18       qsearch( 'cust_bill_pkg', {
19                                   'recur' => { op=>'!=', value=>0 },
20                                   'edate' => { op=>'>', value=>$now },
21                                 }, );
22
23   my @cust_pkg = 
24     grep { $_->part_pkg->recur != 0
25            && $_->part_pkg->freq !~ /^([01]|\d+[dw])$/
26          }
27       qsearch ( 'cust_pkg', {
28                               'bill' => { op=>'>', value=>$now }
29                             } );
30
31   foreach my $cust_bill_pkg ( @cust_bill_pkg) { 
32     my $period = $cust_bill_pkg->edate - $cust_bill_pkg->sdate;
33
34     my $elapsed = $now - $cust_bill_pkg->sdate;
35     $elapsed = 0 if $elapsed < 0;
36
37     my $remaining = 1 - $elapsed/$period;
38
39     my $unearned = $remaining * $cust_bill_pkg->recur;
40     $total += $unearned;
41
42   }
43
44   foreach my $cust_pkg ( @cust_pkg ) {
45     my $period = $cust_pkg->bill - $cust_pkg->last_bill;
46
47     my $elapsed = $now - $cust_pkg->last_bill;
48     $elapsed = 0 if $elapsed < 0;
49
50     my $remaining = 1 - $elapsed/$period;
51
52     my $unearned = $remaining * $cust_pkg->part_pkg->recur; #!! only works for flat/legacy
53     $total_legacy += $unearned;
54
55   }
56
57   $total = sprintf('%.2f', $total);
58   $total_legacy = sprintf('%.2f', $total_legacy);
59
60 %>
61
62 <%= header( 'Prepaid Income (Unearned Revenue) Report',
63             menubar( 'Main Menu'=>$p, ) )               %>
64 <%= table() %>
65   <TR>
66     <TH>Actual Unearned Revenue</TH>
67     <TH>Legacy Unearned Revenue</TH>
68   </TR>
69   <TR>
70     <TD ALIGN="right">$<%= $total %>
71     <TD ALIGN="right">
72       <%= $now == $time ? "\$$total_legacy" : '<i>N/A</i>'%>
73     </TD>
74   </TR>
75
76 </TABLE>
77 <BR>
78 Actual unearned revenue is the amount of unearned revenue Freeside has  
79 actually invoiced for packages with longer-than monthly terms.
80 <BR><BR>
81 Legacy unearned revenue is the amount of unearned revenue represented by 
82 customer packages.  This number may be larger than actual unearned 
83 revenue if you have imported longer-than monthly customer packages from
84 a previous billing system.
85 </BODY>
86 </HTML>