From aea97dbd5c5ac96c4610b112e55f64da66028ef6 Mon Sep 17 00:00:00 2001 From: Ivan Kohler Date: Thu, 28 Jan 2016 16:23:42 -0800 Subject: [PATCH] optimize Reports->Customers->List Customers, RT#20173 --- FS/FS/Record.pm | 1 + FS/FS/cust_pkg.pm | 25 ++++++++++++++++--------- FS/FS/part_pkg/flat.pm | 13 ++++++++++--- FS/FS/part_svc.pm | 3 +++ httemplate/search/cust_main.cgi | 40 ++++++++++++++++++++++++++++++---------- 5 files changed, 60 insertions(+), 22 deletions(-) diff --git a/FS/FS/Record.pm b/FS/FS/Record.pm index 671d16c92..18624e6c7 100644 --- a/FS/FS/Record.pm +++ b/FS/FS/Record.pm @@ -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; diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index abbbc8d13..6a5580a1d 100644 --- a/FS/FS/cust_pkg.pm +++ b/FS/FS/cust_pkg.pm @@ -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}); diff --git a/FS/FS/part_pkg/flat.pm b/FS/FS/part_pkg/flat.pm index 3192f2f16..da1f0ad33 100644 --- a/FS/FS/part_pkg/flat.pm +++ b/FS/FS/part_pkg/flat.pm @@ -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 { diff --git a/FS/FS/part_svc.pm b/FS/FS/part_svc.pm index 8a5fc35c9..aba3ead40 100644 --- a/FS/FS/part_svc.pm +++ b/FS/FS/part_svc.pm @@ -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 ) = ( '', '' ); diff --git a/httemplate/search/cust_main.cgi b/httemplate/search/cust_main.cgi index fb5988259..ba80f0275 100755 --- a/httemplate/search/cust_main.cgi +++ b/httemplate/search/cust_main.cgi @@ -119,9 +119,11 @@ % 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 @@ -130,7 +132,7 @@ % $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')) -- 2.11.0