optimize customer list, RT#20173
[freeside.git] / httemplate / search / cust_main.cgi
index aabc0b9..e6ab195 100755 (executable)
@@ -4,6 +4,7 @@
 %  } else {
     <% $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum) %>
 %  }
+%  $m->abort;
 %} elsif ( scalar(@cust_main) == 0 ) {
 %  errorpage(emt("No matching customers found!"));
 % } # errorpage quits, so we don't need an 'else' below
 %      my %cust_svc_by_svcpart;
 %      my $rows = 0;
 %      local($FS::part_pkg::cache_enabled) = 1; #for $cust_pkg->part_svc
+%      local($FS::cust_svc::cache_enabled) = 1; #for $cust_svc->part_svc
+%      local($FS::pkg_svc::cache_enabled) = 1; #for $pkg_svc->part_svc
 %      foreach my $part_svc (
 %        $cust_pkg->part_svc( summarize_size=>$large_pkg_size )
 %      ) {
 %      my $part_pkg = $_->part_pkg;
 %
 %      my $pkg_comment = $part_pkg->pkg_comment( cust_pkg=>$_, nopkgpart=>1 );
-%      my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
+%      my $show = $default_customer_view =~ /^(jumbo|packages)$/
 %                   ? ''
 %                   : ';show=packages';
 %      my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
 %         }
 %         elsif ( scalar @$these ) { # do not summarize
 %           foreach my $cust_svc ( @$these ) {
+%             my $part_svc = $cust_svc->part_svc;
           <% $n2 %>
             <% $td %>
-                <% FS::UI::Web::svc_link($m, $cust_svc->part_svc, $cust_svc) %>
+                <% FS::UI::Web::svc_link($m, $part_svc, $cust_svc) %>
             </TD> 
             <% $td %>
-                <% FS::UI::Web::svc_label_link($m, $cust_svc->part_svc, $cust_svc) %>
+                <% FS::UI::Web::svc_label_link($m, $part_svc, $cust_svc) %>
             </TD>
 %             $n2="</TR><TR>";
 %           } #foreach $cust_svc
@@ -320,6 +324,8 @@ my $maxrecords = $conf->config('maxsearchrecordsperpage');
 # summarize more than this many services of the same svcpart
 my $large_pkg_size = $conf->config('cust_pkg-large_pkg_size') || 0;
 
+my $default_customer_view = $curuser->default_customer_view;
+
 my $limit = '';
 $limit .= "LIMIT $maxrecords" if $maxrecords;
 
@@ -494,35 +500,35 @@ if ( $cgi->param('browse')
   @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
 }
 
-my $pkgs_method = $conf->exists('hidecancelledpackages')
-                    ? 'ncancelled_pkgs'
-                    : 'all_pkgs';
-
-#false laziness w/httemplate/view/cust_main/packages.html
-my $select = join(',',
-               'cust_pkg.*',
-               'part_pkg.*',
-               '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;
+my %all_pkgs = ();
+if ( scalar(@cust_main) > 1 || $cgi->param('referral_custnum') ) {
+
+  my $pkgs_method = $conf->exists('hidecancelledpackages')
+                      ? 'ncancelled_pkgs'
+                      : 'all_pkgs';
+
+  #false laziness w/httemplate/view/cust_main/packages.html
+  my $select = join(',',
+                 'cust_pkg.*',
+                 'part_pkg.*',
+                 'setup_option.optionvalue AS _opt_setup_fee',
+                 'recur_option.optionvalue AS _opt_recur_fee',
+               );
+
+  my $addl_from = ' LEFT JOIN part_pkg USING ( pkgpart ) '.
+                  FS::part_pkg->join_options_sql;
+
+  local($FS::cust_pkg::cache_enabled) = 1; #for $cust_pkg->part_pkg
+  %all_pkgs = map { $_->custnum =>
+                      [ $_->$pkgs_method({ select          => $select,
+                                           addl_from       => $addl_from,
+                                           skip_label_sort => 1,
+                                        })
+                      ];
+                  }
+                @cust_main;
+
+}
 
 sub last_sort {
   lc($a->getfield('last')) cmp lc($b->getfield('last'))