optimize Reports->Customers->List Customers, RT#20173
authorIvan Kohler <ivan@freeside.biz>
Fri, 29 Jan 2016 00:23:42 +0000 (16:23 -0800)
committerIvan Kohler <ivan@freeside.biz>
Fri, 29 Jan 2016 00:23:42 +0000 (16:23 -0800)
FS/FS/Record.pm
FS/FS/cust_pkg.pm
FS/FS/part_pkg/flat.pm
FS/FS/part_svc.pm
httemplate/search/cust_main.cgi

index 671d16c..18624e6 100644 (file)
@@ -204,6 +204,7 @@ sub new {
 
   $self->{'modified'} = 0;
 
+  $self->_simplecache($self->{'Hash'})  if $self->can('_simplecache');
   $self->_cache($self->{'Hash'}, shift) if $self->can('_cache') && @_;
 
   $self;
index abbbc8d..6a5580a 100644 (file)
@@ -56,18 +56,25 @@ $disable_agentcheck = 0;
 
 $upgrade = 0; #go away after setup+start dates cleaned up for old customers
 
-sub _cache {
-  my $self = shift;
-  my ( $hashref, $cache ) = @_;
-  #if ( $hashref->{'pkgpart'} ) {
+sub _simplecache {
+  my( $self, $hashref ) = @_;
   if ( $hashref->{'pkg'} ) {
-    # #@{ $self->{'_pkgnum'} } = ();
-    # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
-    # $self->{'_pkgpart'} = $subcache;
-    # #push @{ $self->{'_pkgnum'} },
-    #   FS::part_pkg->new_or_cached($hashref, $subcache);
     $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
   }
+}
+
+sub _cache {
+  my $self = shift;
+  my ( $hashref, $cache ) = @_;
+#  #if ( $hashref->{'pkgpart'} ) {
+#  if ( $hashref->{'pkg'} ) {
+#    # #@{ $self->{'_pkgnum'} } = ();
+#    # my $subcache = $cache->subcache('pkgpart', 'part_pkg');
+#    # $self->{'_pkgpart'} = $subcache;
+#    # #push @{ $self->{'_pkgnum'} },
+#    #   FS::part_pkg->new_or_cached($hashref, $subcache);
+#    $self->{'_pkgpart'} = FS::part_pkg->new($hashref);
+#  }
   if ( exists $hashref->{'svcnum'} ) {
     #@{ $self->{'_pkgnum'} } = ();
     my $subcache = $cache->subcache('svcnum', 'cust_svc', $hashref->{pkgnum});
index 3192f2f..da1f0ad 100644 (file)
@@ -134,8 +134,11 @@ sub calc_setup {
 
 sub unit_setup {
   my($self, $cust_pkg, $sdate, $details ) = @_;
-
-  $self->option('setup_fee') || 0;
+  ( exists( $self->{'Hash'}{'_opt_setup_fee'} )
+      ? $self->{'Hash'}{'_opt_setup_fee'}
+      : $self->option('setup_fee', 1) 
+  )
+    || 0;
 }
 
 sub calc_recur {
@@ -185,7 +188,11 @@ sub cutoff_day {
 
 sub base_recur {
   my($self, $cust_pkg, $sdate) = @_;
-  $self->option('recur_fee', 1) || 0;
+  ( exists( $self->{'Hash'}{'_opt_recur_fee'} )
+      ? $self->{'Hash'}{'_opt_recur_fee'}
+      : $self->option('recur_fee', 1) 
+  )
+    || 0;
 }
 
 sub base_recur_permonth {
index 8a5fc35..aba3ead 100644 (file)
@@ -561,6 +561,9 @@ is specified as B<0>, returns the number of unlinked customer services.
 sub num_cust_svc {
   my $self = shift;
 
+  return $self->{Hash}{num_cust_svc}
+    if !@_ && exists($self->{Hash}{num_cust_svc});
+
   my @param = ( $self->svcpart );
 
   my( $join, $and ) = ( '', '' );
index fb59882..ba80f02 100755 (executable)
 %    foreach my $cust_pkg ( @{$all_pkgs{$custnum}} ) {
 %      my %cust_svc_by_svcpart;
 %      my $rows = 0;
-%      foreach my $part_svc ( $cust_pkg->part_svc ) {
+%      foreach my $part_svc (
+%        $cust_pkg->part_svc( summarize_size=>$large_pkg_size )
+%      ) {
 %        my $svcpart = $part_svc->svcpart;
-%        my $num_cust_svc = $cust_pkg->num_cust_svc($svcpart);
+%        my $num_cust_svc = $part_svc->num_cust_svc;
 %        if ( $large_pkg_size > 0 and $num_cust_svc >= $large_pkg_size ) {
 %          # don't retrieve the cust_svc records, just stash the 
 %          # part_svc and num_cust_svc for later
 %          $rows += 2;
 %        }
 %        elsif ( $num_cust_svc ) {
-%          $cust_svc_by_svcpart{$svcpart} = [ $cust_pkg->cust_svc($svcpart) ];
+%          $cust_svc_by_svcpart{$svcpart} = $part_svc->cust_pkg_svc;
 %          $rows += $num_cust_svc;
 %        } #if summarize
 %      } #foreach $part_svc
@@ -482,7 +484,7 @@ if ( $cgi->param('browse')
     );
   }
 
-  @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
+  @cust_main = grep { $_->num_ncancelled_pkgs || ! $_->num_pkgs } @cust_main
     if ! $cgi->param('cancelled')
        && (
          $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
@@ -494,12 +496,30 @@ if ( $cgi->param('browse')
   @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
 }
 
-my %all_pkgs;
-if ( $conf->exists('hidecancelledpackages' ) ) {
-  %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
-} else {
-  %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
-}
+my $pkgs_method = $conf->exists('hidecancelledpackages')
+                    ? 'ncancelled_pkgs'
+                    : 'all_pkgs';
+
+#false laziness w/httemplate/view/cust_main/packages.html
+my $select = '*, setup_option.optionvalue AS _opt_setup_fee, '.
+                'recur_option.optionvalue AS _opt_recur_fee',
+my $addl_from = qq{
+    LEFT JOIN part_pkg USING ( pkgpart )
+    LEFT JOIN part_pkg_option AS setup_option
+      ON (     cust_pkg.pkgpart = setup_option.pkgpart
+           AND setup_option.optionname = 'setup_fee' )
+    LEFT JOIN part_pkg_option AS recur_option
+      ON (     cust_pkg.pkgpart = recur_option.pkgpart
+           AND recur_option.optionname = 'recur_fee' )
+};
+
+my %all_pkgs = map { $_->custnum =>
+                       [ $_->$pkgs_method({ select    => $select,
+                                            addl_from => $addl_from,
+                                         })
+                       ];
+                   }
+                 @cust_main;
 
 sub last_sort {
   lc($a->getfield('last')) cmp lc($b->getfield('last'))