RT#7266: aging reports as of a past date
authormark <mark>
Wed, 31 Mar 2010 07:41:42 +0000 (07:41 +0000)
committermark <mark>
Wed, 31 Mar 2010 07:41:42 +0000 (07:41 +0000)
httemplate/search/elements/cust_main_dayranges.html
httemplate/search/report_receivables.cgi
httemplate/search/report_receivables.html
httemplate/search/report_unapplied_cust_pay.html
httemplate/search/unapplied_cust_pay.html

index cc01492..f759278 100644 (file)
@@ -98,8 +98,14 @@ my $ranges = $opt{'ranges'} ? delete($opt{'ranges'}) : [
 
 my $range_sub = delete($opt{'range_sub'}); #or die
 
+my $offset = 0;
+if($cgi->param('as_of')) {
+  $offset = int((time - str2time($cgi->param('as_of'))) / 86400);
+  $opt{'title'} .= ' ('.$cgi->param('as_of').')' if $offset > 0;
+}
+
 #my $range_cols = join(',', map &{$range_sub}( @$_ ), @ranges );
-my $range_cols = join(',', map call_range_sub($range_sub, @$_ ), @$ranges );
+my $range_cols = join(',', map call_range_sub($range_sub, @$_, 'offset' => $offset ), @$ranges );
 
 my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
 
@@ -119,14 +125,14 @@ END
 my @where = ();
 
 unless ( $cgi->param('all_customers') ) {
-
+# Exclude entire cust_main records where the balance is >0
   my $days = 0;
   if ( $cgi->param('days') =~ /^\s*(\d+)\s*$/ ) {
     $days = $1;
   }
 
   push @where,
-    call_range_sub($range_sub, $days, 0, 'no_as'=>1). ' > 0'; # != 0';
+    call_range_sub($range_sub, $days + $offset, 0, 'no_as'=>1). ' > 0'; # != 0';
 }
 
 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
@@ -167,7 +173,7 @@ my $sql_query = {
 
 my $total_sql =
   "SELECT ".
-      join(',', map call_range_sub( $range_sub, @$_, 'sum'=>1 ), @$ranges).
+      join(',', map call_range_sub( $range_sub, @$_, 'offset' => $offset, 'sum'=>1 ), @$ranges).
     " FROM cust_main $where";
 
 my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
@@ -203,7 +209,7 @@ sub call_range_sub {
 
   my $as = $opt{'no_as'} ? '' : " AS rangecol_${start}_$end";
 
-  my $sql = &{$range_sub}( $start, $end ); #%opt?
+  my $sql = &{$range_sub}( $start, $end, $opt{'offset'} ); #%opt?
 
   $sql = "SUM($sql)" if $opt{'sum'};
 
index 6df0161..5326184 100755 (executable)
@@ -28,13 +28,18 @@ die "access denied"
 # )
 
 sub balance {
-  my($start, $end) = @_; #, %opt ?
-
+  my($start, $end, $offset) = @_; #, %opt ?
   #handle start and end ranges (86400 = 24h * 60m * 60s)
   my $str2time = str2time_sql;
   my $closing = str2time_sql_closing;
-  $start = $start ? "( $str2time now() $closing - ".($start * 86400). ' )' : '';
-  $end   = $end   ? "( $str2time now() $closing - ".($end   * 86400). ' )' : '';
+
+  # $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;
 
index bfb0169..b7c84f0 100755 (executable)
       <INPUT TYPE="radio" NAME="all_customers" VALUE="0" CHECKED onClick="if ( ! this.checked ) { document.OneTrueForm.days.disabled=true; document.OneTrueForm.days.style.backgroundColor = '#dddddd'; } else { document.OneTrueForm.days.disabled=false; document.OneTrueForm.days.style.backgroundColor = '#ffffff'; }">Customers with a balance over <INPUT NAME="days" TYPE="text" SIZE=4 MAXLENGTH=3 VALUE="0"> days old
     </TD>
   </TR>
+  <% include( '/elements/tr-input-date-field.html', {
+                'name'      => 'as_of',
+                'value'     => time,
+                'label'     => 'As of date ',
+                'format'    => FS::Conf->new->config('date_format') || '%m/%d/%Y',
+  } ) %>
 
 </TABLE>
 
index 10093e5..65e064c 100755 (executable)
       <INPUT TYPE="radio" NAME="all_customers" VALUE="0" CHECKED onClick="if ( ! this.checked ) { document.OneTrueForm.days.disabled=true; document.OneTrueForm.days.style.backgroundColor = '#dddddd'; } else { document.OneTrueForm.days.disabled=false; document.OneTrueForm.days.style.backgroundColor = '#ffffff'; }">Customers with unapplied payments over <INPUT NAME="days" TYPE="text" SIZE=4 MAXLENGTH=3 VALUE="0"> days old
     </TD>
   </TR>
+  <% include( '/elements/tr-input-date-field.html', {
+                'name'      => 'as_of',
+                'value'     => time,
+                'label'     => 'As of date ',
+                'format'    => FS::Conf->new->config('date_format') || '%m/%d/%Y',
+  } ) %>
 
 </TABLE>
 
index 8d064d1..73361c0 100755 (executable)
@@ -13,13 +13,15 @@ die "access denied"
 <%once>
 
 sub unapplied_payments {
-  my($start, $end, %opt) = @_;
+  my($start, $end, $offset) = @_;
 
   #handle start and end ranges (86400 = 24h * 60m * 60s)
   my $str2time = str2time_sql;
   my $closing = str2time_sql_closing;
-  $start = $start ? "( $str2time now() $closing - ".($start * 86400). ' )' : '';
-  $end   = $end   ? "( $str2time now() $closing - ".($end   * 86400). ' )' : '';
+  $start = "( $str2time now() $closing - ".($start + $offset) * 86400 . ' )';
+  $end = $end ? 
+           "( $str2time now() $closing - ".($end   + $offset) * 86400 . ' )'
+           : '';
 
   FS::cust_main->unapplied_payments_date_sql( $start, $end );