[freeside-commits] freeside/httemplate/search report_receivables.cgi, 1.41, 1.42

Ivan,,, ivan at wavetail.420.am
Wed Jun 25 13:14:29 PDT 2008


Update of /home/cvs/cvsroot/freeside/httemplate/search
In directory wavetail.420.am:/tmp/cvs-serv7558

Modified Files:
	report_receivables.cgi 
Log Message:
optimize total query in receivables report.  very significant speedup for large DBs

Index: report_receivables.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/report_receivables.cgi,v
retrieving revision 1.41
retrieving revision 1.42
diff -u -d -r1.41 -r1.42
--- report_receivables.cgi	8 Jan 2008 11:23:03 -0000	1.41
+++ report_receivables.cgi	25 Jun 2008 20:14:26 -0000	1.42
@@ -107,7 +107,7 @@
     $days = $1;
   }
 
-  push @where, balance($days, 0, 'no_as'=>1). " > 0";
+  push @where, balance($days, 0, 'no_as'=>1). ' > 0'; # != 0';
 
 }
 
@@ -132,10 +132,8 @@
   'order_by'  => "order by coalesce(lower(company), ''), lower(last)",
 };
 
-my $join = 'LEFT JOIN cust_main USING ( custnum )';
-
-my $total_sql = "SELECT ".
-  join(',', map balance( @$_, total=>1, join=>$join, where=>\@where ), @ranges);
+my $total_sql = "SELECT ". join(',', map balance( @$_, 'sum'=>1 ), @ranges).
+                " FROM cust_main $where";
 
 my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
 $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
@@ -157,7 +155,9 @@
 #   '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")
-#   #options for totals
+#   '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)
@@ -170,12 +170,16 @@
 
   #handle start and end ranges (86400 = 24h * 60m * 60s)
   my $str2time = str2time_sql;
-  $start = $start ? "( $str2time now() ) - ".($start * 86400). ' )' : '';
-  $end   = $end   ? "( $str2time now() ) - ".($end   * 86400). ' )' : '';
+  my $closing = str2time_sql_closing;
+  $start = $start ? "( $str2time now() $closing - ".($start * 86400). ' )' : '';
+  $end   = $end   ? "( $str2time now() $closing - ".($end   * 86400). ' )' : '';
 
-  $opt{'unapplied_date'} = 1;
+  #$opt{'unapplied_date'} = 1;
 
-  FS::cust_main->balance_date_sql( $start, $end, %opt ). $as;
+  ( $opt{sum} ? 'SUM( ' : '' ). 
+  FS::cust_main->balance_date_sql( $start, $end, %opt ).
+  ( $opt{sum} ? ' )' : '' ). 
+  $as;
 
 }
 



More information about the freeside-commits mailing list