Fix weird behavior of aging report, RT#9234
authormark <mark>
Thu, 22 Jul 2010 07:25:48 +0000 (07:25 +0000)
committermark <mark>
Thu, 22 Jul 2010 07:25:48 +0000 (07:25 +0000)
httemplate/search/elements/cust_main_dayranges.html
httemplate/search/report_receivables.cgi
httemplate/search/unapplied_cust_pay.html

index aa13388..d5ef625 100644 (file)
@@ -11,6 +11,7 @@ Example:
     my( $start, $end ) = @_;
 
     "SQL EXPRESSION BASED ON $start AND $end";
+    # where $start and $end are unix timestamps
   };
 
 </%doc>
@@ -252,7 +253,16 @@ sub call_range_sub {
 
   my $as = $opt{'no_as'} ? '' : " AS rangecol_${start}_$end";
 
-  my $sql = &{$range_sub}( $start, $end, $opt{'offset'} ); #%opt?
+  my $offset = $opt{'offset'} || 0;
+  # Time::ParseDate for excruciating correctness
+  # Always use $offset - 1day + 1sec = the last second of that day
+  $start =  parsedate('00:00 '.($start + $offset - 1).' days ago') - 1;
+  $end = $end ?
+            parsedate('00:00 '.($end + $offset - 1).' days ago') - 1 : 
+            '';
+  my $cutoff = parsedate('00:00 '.($offset - 1).' days ago') - 1;
+
+  my $sql = &{$range_sub}( $start, $end, $cutoff ); #%opt?
 
   $sql = "SUM($sql)" if $opt{'sum'};
 
index 3696ed4..57228a5 100755 (executable)
@@ -29,24 +29,12 @@ die "access denied"
 # )
 
 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 . ')' );
+  my($start, $end, $cutoff) = @_; #, %opt ?
 
+  FS::cust_main->balance_date_sql( $start, $end, 
+        'cutoff' => $cutoff,
+        'unapplied_date'=>1,
+  );
 }
 
 </%once>
index 161c650..e232291 100755 (executable)
@@ -13,19 +13,11 @@ die "access denied"
 <%once>
 
 sub unapplied_payments {
-  my($start, $end, $offset) = @_;
-
-  #handle start and end ranges (86400 = 24h * 60m * 60s)
-  my $str2time = str2time_sql;
-  my $closing = str2time_sql_closing;
-  $start = "( $str2time now() $closing - ".($start + $offset) * 86400 . ' )';
-  $end = $end ? 
-           "( $str2time now() $closing - ".($end   + $offset) * 86400 . ' )'
-           : '';
+  my($start, $end, $cutoff) = @_;
 
   FS::cust_main->unapplied_payments_date_sql( $start, $end, 
-          'cutoff' => "( $str2time now() $closing - ".$offset * 86400 . ')'  );
-
+          'cutoff' => $cutoff,
+  );
 }
 
 </%once>