ce928b81cb72e1ef0aa869772d1e09933cdc8eff
[freeside.git] / httemplate / search / report_prepaid_income.cgi
1 <% include("/elements/header.html", 'Prepaid Income (Unearned Revenue) Report') %>
2
3 <% table() %>
4   <TR>
5     <TH>Actual Unearned Revenue</TH>
6     <TH>Legacy Unearned Revenue</TH>
7   </TR>
8   <TR>
9     <TD ALIGN="right">$<% $total %>
10     <TD ALIGN="right">
11       <% $now == $time ? "\$$total_legacy" : '<i>N/A</i>'%>
12     </TD>
13   </TR>
14
15 </TABLE>
16 <BR>
17 Actual unearned revenue is the amount of unearned revenue Freeside has  
18 actually invoiced for packages with longer-than monthly terms.
19 <BR><BR>
20 Legacy unearned revenue is the amount of unearned revenue represented by 
21 customer packages.  This number may be larger than actual unearned 
22 revenue if you have imported longer-than monthly customer packages from
23 a previous billing system.
24 </BODY>
25 </HTML>
26 <%init>
27
28 die "access denied"
29   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
30
31 #doesn't yet deal with daily/weekly packages
32
33 #needs to be re-written in sql for efficiency
34
35 my $time = time;
36
37 my $now = $cgi->param('date') && str2time($cgi->param('date')) || $time;
38 $now =~ /^(\d+)$/ or die "unparsable date?";
39 $now = $1;
40
41 my @where = ();
42
43 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
44   my $agentnum = $1;
45   push @where, "agentnum = $agentnum";
46 }
47
48 #here is the agent virtualization
49 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
50
51 my $where = join(' AND ', @where);
52 $where = "AND $where" if $where;
53
54 my( $total, $total_legacy ) = ( 0, 0 );
55
56 my @cust_bill_pkg =
57   grep { $_->cust_pkg && $_->cust_pkg->part_pkg->freq !~ /^([01]|\d+[dw])$/ }
58     qsearch({
59       'select'    => 'cust_bill_pkg.*',
60       'table'     => 'cust_bill_pkg',
61       'addl_from' => ' LEFT JOIN cust_bill USING ( invnum  ) '.
62                      ' LEFT JOIN cust_main USING ( custnum ) ',
63       'hashref'   => {
64                        'recur' => { op=>'!=', value=>0    },
65                        'edate' => { op=>'>',  value=>$now },
66                      },
67       'extra_sql' => $where,
68     });
69
70 my @cust_pkg = 
71   grep { $_->part_pkg->recur != 0
72          && $_->part_pkg->freq !~ /^([01]|\d+[dw])$/
73        }
74     qsearch({
75       'select'    => 'cust_pkg.*',
76       'table'     => 'cust_pkg',
77       'addl_from' => ' LEFT JOIN cust_main USING ( custnum ) ',
78       'hashref'   => { 'bill' => { op=>'>', value=>$now } },
79       'extra_sql' => $where,
80     });
81
82 foreach my $cust_bill_pkg ( @cust_bill_pkg) { 
83   my $period = $cust_bill_pkg->edate - $cust_bill_pkg->sdate;
84
85   my $elapsed = $now - $cust_bill_pkg->sdate;
86   $elapsed = 0 if $elapsed < 0;
87
88   my $remaining = 1 - $elapsed/$period;
89
90   my $unearned = $remaining * $cust_bill_pkg->recur;
91   $total += $unearned;
92
93 }
94
95 foreach my $cust_pkg ( @cust_pkg ) {
96   my $period = $cust_pkg->bill - $cust_pkg->last_bill;
97
98   my $elapsed = $now - $cust_pkg->last_bill;
99   $elapsed = 0 if $elapsed < 0;
100
101   my $remaining = 1 - $elapsed/$period;
102
103   my $unearned = $remaining * $cust_pkg->part_pkg->recur; #!! only works for flat/legacy
104   $total_legacy += $unearned;
105
106 }
107
108 $total = sprintf('%.2f', $total);
109 $total_legacy = sprintf('%.2f', $total_legacy);
110
111 </%init>