RT#7266: aging report "as of" date now limits applied payments
[freeside.git] / httemplate / search / report_receivables.cgi
index 6b96ba7..3696ed4 100755 (executable)
@@ -1,28 +1,52 @@
-<%
-
-use strict;
-use vars qw( $cgi $user );
-use CGI;
-use CGI::Carp qw( fatalsToBrowser );
-use FS::UID qw( cgisuidsetup getotaker );
-
-$cgi = new CGI;
-&cgisuidsetup($cgi);
-
-$user = getotaker;
-
-print $cgi->header( '-expires' => '-2m' ),
-  header('Current Receivables Report Results');
-
-open (REPORT, "/usr/bin/freeside-receivables-report -v $user |");
+<% include( 'elements/cust_main_dayranges.html',
+                 'title'       => 'Accounts Receivable Aging Summary',
+                 'range_sub'   => \&balance,
+                 'payment_links' => 1,
+          )
+%>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Receivables report')
+      or $FS::CurrentUser::CurrentUser->access_right('Financial reports');
+
+</%init>
+<%once>
+
+#Example:
+#
+# my $balance = balance(
+#   $start, $end, $offset,
+#   'no_as'  => 1, #set to true when using in a WHERE clause (supress AS clause)
+#                 #or 0 / omit when using in a SELECT clause as a column
+#                 #  ("AS balance_$start_$end")
+#   'sum'    => 1, #set to true to get a SUM() of the values, for totals
+#
+#   #obsolete? options for totals (passed to cust_main::balance_date_sql)
+#   'total'  => 1, #set to true to remove all customer comparison clauses
+#   'join'   => $join,   #JOIN clause
+#   'where'  => \@where, #WHERE clause hashref (elements "AND"ed together)
+# )
+
+sub balance {
+  my($start, $end, $offset) = @_; #, %opt ?
+  #handle start and end ranges (86400 = 24h * 60m * 60s)
+  my $str2time = str2time_sql;
+  my $closing = str2time_sql_closing;
+
+  # $end == 0 means "+infinity", while $start == 0 really means 0
+  # so we should always include a start condition
+  $start = "( $str2time now() $closing - ". ($start + $offset) * 86400 . ' )';
+  # but only include an end condition if $end != 0
+  $end = $end ? 
+           "( $str2time now() $closing - ". ($end + $offset) * 86400 . ' )' 
+           : '';
+
+  #$opt{'unapplied_date'} = 1;
+
+  FS::cust_main->balance_date_sql( $start, $end, 'unapplied_date'=>1,
+           'cutoff' => "( $str2time now() $closing - ".$offset * 86400 . ')' );
 
-print '<PRE>';
-while(<REPORT>) {
-  print $_;
 }
-print '</PRE>';
-
-print '</BODY></HTML>';
-
-%>
 
+</%once>