[freeside-commits] freeside/FS/FS cust_bill.pm, 1.350.2.11, 1.350.2.12 cust_bill_pkg.pm, 1.56.2.6, 1.56.2.7 cust_bill_pkg_detail.pm, 1.16.2.1, 1.16.2.2

Ivan,,, ivan at wavetail.420.am
Tue Nov 15 11:41:52 PST 2011


Update of /home/cvs/cvsroot/freeside/FS/FS
In directory wavetail.420.am:/tmp/cvs-serv30888/FS/FS

Modified Files:
      Tag: FREESIDE_2_3_BRANCH
	cust_bill.pm cust_bill_pkg.pm cust_bill_pkg_detail.pm 
Log Message:
optimize invoice rendering with lots of CDRs, RT#15155

Index: cust_bill_pkg_detail.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_bill_pkg_detail.pm,v
retrieving revision 1.16.2.1
retrieving revision 1.16.2.2
diff -u -w -d -r1.16.2.1 -r1.16.2.2
--- cust_bill_pkg_detail.pm	26 Jul 2011 23:46:09 -0000	1.16.2.1
+++ cust_bill_pkg_detail.pm	15 Nov 2011 19:41:49 -0000	1.16.2.2
@@ -163,11 +163,13 @@
 If I<escape_function> is set then the format is processed by this
 function before being returned.
 
+DEPRECATED? (mostly unused, expensive)
 If I<format_function> is set then the detail is handed to this callback
 for processing.
 
 =cut
 
+#totally false laziness w/cust_bill_pkg->detail
 sub formatted {
   my ( $self, %opt ) = @_;
   my $format = $opt{format} || '';

Index: cust_bill_pkg.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_bill_pkg.pm,v
retrieving revision 1.56.2.6
retrieving revision 1.56.2.7
diff -u -w -d -r1.56.2.6 -r1.56.2.7
--- cust_bill_pkg.pm	14 Nov 2011 20:36:03 -0000	1.56.2.6
+++ cust_bill_pkg.pm	15 Nov 2011 19:41:49 -0000	1.56.2.7
@@ -3,6 +3,7 @@
 use strict;
 use vars qw( @ISA $DEBUG $me );
 use Carp;
+use Text::CSV_XS;
 use FS::Record qw( qsearch qsearchs dbdef dbh );
 use FS::cust_main_Mixin;
 use FS::cust_pkg;
@@ -444,30 +445,55 @@
 
 sub details {
   my ( $self, %opt ) = @_;
-  my $format = $opt{format} || '';
   my $escape_function = $opt{escape_function} || sub { shift };
-  return () unless defined dbdef->table('cust_bill_pkg_detail');
 
-  eval "use Text::CSV_XS;";
-  die $@ if $@;
   my $csv = new Text::CSV_XS;
 
-  my $format_sub = sub { my $detail = shift;
-                         $csv->parse($detail) or return "can't parse $detail";
-                         join(' - ', map { &$escape_function($_) }
-                                     $csv->fields
-                             );
-                       };
+  if ( $opt{format_function} ) {
+
+    #this still expects to be passed a cust_bill_pkg_detail object as the
+    #second argument, which is expensive
+    carp "deprecated format_function passed to cust_bill_pkg->details";
+    my $format_sub = $opt{format_function} if $opt{format_function};
+
+    map { ( $_->format eq 'C'
+              ? &{$format_sub}( $_->detail, $_ )
+              : &{$escape_function}( $_->detail )
+          )
+        }
+      qsearch ({ 'table'    => 'cust_bill_pkg_detail',
+                 'hashref'  => { 'billpkgnum' => $self->billpkgnum },
+                 'order_by' => 'ORDER BY detailnum',
+              });
+
+  } elsif ( $opt{'no_usage'} ) {
+
+    my $sql = "SELECT detail FROM cust_bill_pkg_detail ".
+              "  WHERE billpkgnum = ". $self->billpkgnum.
+              "    AND ( format IS NULL OR format != 'C' ".
+              "  ORDER BY detailnum";
+    my $sth = dbh->prepare($sql) or die dbh->errstr;
+    $sth->execute or die $sth->errstr;
+
+    map &{$escape_function}( $_->[0] ), @{ $sth->fetchall_arrayref };
+
+  } else {
+
+    my $format_sub;
+    my $format = $opt{format} || '';
+    if ( $format eq 'html' ) {
 
   $format_sub = sub { my $detail = shift;
                       $csv->parse($detail) or return "can't parse $detail";
                       join('</TD><TD>', map { &$escape_function($_) }
                                         $csv->fields
                           );
-                    }
-    if $format eq 'html';
+                        };
 
-  $format_sub = sub { my $detail = shift;
+    } elsif ( $format eq 'latex' ) {
+
+      $format_sub = sub {
+        my $detail = shift;
                       $csv->parse($detail) or return "can't parse $detail";
                       #join(' & ', map { '\small{'. &$escape_function($_). '}' }
                       #            $csv->fields );
@@ -484,21 +510,35 @@
                         $column++;
                       }
                       $result;
+      };
+
+    } else {
+
+      $format_sub = sub { my $detail = shift;
+                          $csv->parse($detail) or return "can't parse $detail";
+                          join(' - ', map { &$escape_function($_) }
+                                      $csv->fields
+                              );
+                        };
+
                     }
-    if $format eq 'latex';
 
-  $format_sub = $opt{format_function} if $opt{format_function};
+    my $sql = "SELECT format, detail FROM cust_bill_pkg_detail ".
+              "  WHERE billpkgnum = ". $self->billpkgnum.
+              "  ORDER BY detailnum";
+    my $sth = dbh->prepare($sql) or die dbh->errstr;
+    $sth->execute or die $sth->errstr;
 
-  map { ( $_->format eq 'C'
-          ? &{$format_sub}( $_->detail, $_ )
-          : &{$escape_function}( $_->detail )
-        )
+    #avoid the fetchall_arrayref and loop for less memory usage?
+
+    map { $_->[0] eq 'C'
+            ? &{$format_sub}(      $_->[1] )
+            : &{$escape_function}( $_->[1] );
       }
-    qsearch ({ 'table'    => 'cust_bill_pkg_detail',
-               'hashref'  => { 'billpkgnum' => $self->billpkgnum },
-               'order_by' => 'ORDER BY detailnum',
-            });
-    #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
+      @{ $sth->fetchall_arrayref };
+
+  }
+
 }
 
 =item details_header [ OPTION => VALUE ... ]
@@ -514,8 +554,6 @@
   my $self = shift;
   return '' unless defined dbdef->table('cust_bill_pkg_detail');
 
-  eval "use Text::CSV_XS;";
-  die $@ if $@;
   my $csv = new Text::CSV_XS;
 
   my @detail = 
@@ -825,10 +863,10 @@
 
 sub usage {
   my( $self, $classnum ) = @_;
-  my $sum = 0;
 
   if ( $self->get('details') ) {
 
+    my $sum = 0;
     foreach my $value (
       map { ref($_) eq 'HASH'
               ? $_->{'amount'}

Index: cust_bill.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_bill.pm,v
retrieving revision 1.350.2.11
retrieving revision 1.350.2.12
diff -u -w -d -r1.350.2.11 -r1.350.2.12
--- cust_bill.pm	12 Nov 2011 00:35:29 -0000	1.350.2.11
+++ cust_bill.pm	15 Nov 2011 19:41:49 -0000	1.350.2.12
@@ -3034,7 +3034,7 @@
     $options{'section'} = $section if $multisection;
     $options{'format'} = $format;
     $options{'escape_function'} = $escape_function;
-    $options{'format_function'} = sub { () } unless $unsquelched;
+    $options{'no_usage'} = 1 unless $unsquelched;
     $options{'unsquelched'} = $unsquelched;
     $options{'summary_page'} = $summarypage;
     $options{'skip_usage'} =
@@ -4762,6 +4762,7 @@
 
 escape_function: the function used to escape strings.
 
+DEPRECATED? (expensive, mostly unused?)
 format_function: the function used to format CDRs.
 
 section: a hashref containing 'description'; if this is present, 
@@ -4790,6 +4791,7 @@
   my $format = $opt{format} || '';
   my $escape_function = $opt{escape_function} || sub { shift };
   my $format_function = $opt{format_function} || '';
+  my $no_usage = $opt{no_usage} || '';
   my $unsquelched = $opt{unsquelched} || ''; #unused
   my $section = $opt{section}->{description} if $opt{section};
   my $summary_page = $opt{summary_page} || ''; #unused
@@ -4846,6 +4848,7 @@
       my %details_opt = ( 'format'          => $format,
                           'escape_function' => $escape_function,
                           'format_function' => $format_function,
+                          'no_usage'        => $opt{'no_usage'},
                         );
 
       if ( $cust_bill_pkg->pkgnum > 0 ) {
@@ -5003,7 +5006,7 @@
 
             #instead of omitting details entirely in this case (unwanted side
             # effects), just omit CDRs
-            $details_opt{'format_function'} = sub { () }
+            $details_opt{'no_usage'} = 1
               if $type && $type eq 'R';
 
             push @d, $cust_bill_pkg->details(%details_opt);



More information about the freeside-commits mailing list