RT#7266: aging reports as of a past date
[freeside.git] / httemplate / search / report_receivables.cgi
1 <% include( 'elements/cust_main_dayranges.html',
2                  'title'       => 'Accounts Receivable Aging Summary',
3                  'range_sub'   => \&balance,
4           )
5 %>
6 <%init>
7
8 die "access denied"
9   unless $FS::CurrentUser::CurrentUser->access_right('Receivables report')
10       or $FS::CurrentUser::CurrentUser->access_right('Financial reports');
11
12 </%init>
13 <%once>
14
15 #Example:
16 #
17 # my $balance = balance(
18 #   $start, $end, 
19 #   'no_as'  => 1, #set to true when using in a WHERE clause (supress AS clause)
20 #                 #or 0 / omit when using in a SELECT clause as a column
21 #                 #  ("AS balance_$start_$end")
22 #   'sum'    => 1, #set to true to get a SUM() of the values, for totals
23 #
24 #   #obsolete? options for totals (passed to cust_main::balance_date_sql)
25 #   'total'  => 1, #set to true to remove all customer comparison clauses
26 #   'join'   => $join,   #JOIN clause
27 #   'where'  => \@where, #WHERE clause hashref (elements "AND"ed together)
28 # )
29
30 sub balance {
31   my($start, $end, $offset) = @_; #, %opt ?
32   #handle start and end ranges (86400 = 24h * 60m * 60s)
33   my $str2time = str2time_sql;
34   my $closing = str2time_sql_closing;
35
36   # $end == 0 means "+infinity", while $start == 0 really means 0
37   # so we should always include a start condition
38   $start = "( $str2time now() $closing - ". ($start + $offset) * 86400 . ' )';
39   # but only include an end condition if $end != 0
40   $end = $end ? 
41            "( $str2time now() $closing - ". ($end + $offset) * 86400 . ' )' 
42            : '';
43
44   #$opt{'unapplied_date'} = 1;
45
46   FS::cust_main->balance_date_sql( $start, $end, 'unapplied_date'=>1,);
47
48 }
49
50 </%once>