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