summaryrefslogtreecommitdiff
path: root/httemplate/search/report_prepaid_income.cgi
blob: eb8bbb55eb903d2df14624311dea752c46fcf1f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!-- mason kludge -->
<%

  #doesn't yet deal with daily/weekly packages

  #needs to be re-written in sql for efficiency

  my $now = $cgi->param('date') && str2time($cgi->param('date')) || time;
  $now =~ /^(\d+)$/ or die "unparsable date?";
  $now = $1;

  my %prepaid;

  my @cust_bill_pkg =
    grep { $_->cust_pkg && $_->cust_pkg->part_pkg->freq !~ /^([01]|\d+[dw])$/ }
      qsearch( 'cust_bill_pkg', {
                                  'recur' => { op=>'!=', value=>0 },
                                  'edate' => { op=>'>', value=>$now },
                                }, );

  foreach my $cust_bill_pkg ( @cust_bill_pkg ) {

    #conceptual false laziness w/texas tax exempt_amount stuff in
    #FS::cust_main::bill

    my $freq = $cust_bill_pkg->cust_pkg->part_pkg->freq;
    my $per_month = sprintf("%.2f", $cust_bill_pkg->recur / $freq);

    my($mon, $year) = (localtime($cust_bill_pkg->sdate) )[4,5];
    $mon+=2; $year+=1900;

    foreach my $which_month ( 2 .. $freq ) {
      until ( $mon < 13 ) { $mon -= 12; $year++; }
      $prepaid{"$year-$mon"} += $per_month;
      $mon++;
    }

  }

  my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);

%>

<%= header( 'Prepaid Income (Unearned Revenue) Report',
            menubar( 'Main Menu'=>$p, ) )               %>
<%= table() %>
<%

  my $total = 0;

  my ($now_mon, $now_year) = (localtime($now))[4,5];
  $now_mon+=2; $now_year+=1900;
  until ( $now_mon < 13 ) { $now_mon -= 12; $now_year++; }

  my $subseq = 0;
  for my $year ( $now_year .. 2037 ) {
    for my $mon ( ( $subseq++ ? 1 : $now_mon ) .. 12 ) {
      if ( $prepaid{"$year-$mon"} ) {
        $total += $prepaid{"$year-$mon"};
        %> <TR><TD ALIGN="right"><%= $mon[$mon-1]. ' '. $year %></TD>
               <TD ALIGN="right">
                 <%= sprintf("%.2f", $prepaid{"$year-$mon"} ) %>
               </TD>
           </TR>
        <%
      }
    }

  }

%>
<TR><TH>Total</TH><TD ALIGN="right"><%= sprintf("%.2f", $total) %></TD></TR>
</TABLE>
</BODY>
</HTML>