Will things ever be the same again?
[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
63 <% include("/elements/header.html", 'Prepaid Income (Unearned Revenue) Report',
64             menubar( 'Main Menu'=>$p, ) )               %>
65 <% table() %>
66   <TR>
67     <TH>Actual Unearned Revenue</TH>
68     <TH>Legacy Unearned Revenue</TH>
69   </TR>
70   <TR>
71     <TD ALIGN="right">$<% $total %>
72     <TD ALIGN="right">
73       <% $now == $time ? "\$$total_legacy" : '<i>N/A</i>'%>
74     </TD>
75   </TR>
76
77 </TABLE>
78 <BR>
79 Actual unearned revenue is the amount of unearned revenue Freeside has  
80 actually invoiced for packages with longer-than monthly terms.
81 <BR><BR>
82 Legacy unearned revenue is the amount of unearned revenue represented by 
83 customer packages.  This number may be larger than actual unearned 
84 revenue if you have imported longer-than monthly customer packages from
85 a previous billing system.
86 </BODY>
87 </HTML>