quiet warnings about CGI::param in list context
[freeside.git] / httemplate / search / cust_main.cgi
index 7c3ad33..24d49c5 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
         ( <a href="<% $cgi->self_url %>"><% mt("$linklabel canceled customers") |h %></a> )
 %  }
 
+%  unless ($cgi->param('browse')) {
+    <& /elements/agent_transfer_link.html, {'list' => \@cust_main} &>
+%  }
+
 %  if ( $cgi->param('referral_custnum') ) {
 %    $cgi->param('referral_custnum') =~ /^(\d+)$/
 %      or errorpage(emt("Illegal referral_custnum"));
@@ -54,7 +59,7 @@
 %   my $refcustlabel = "$referral_custnum: " .
 %         ( $cust_main->company || $cust_main->last. ', '. $cust_main->first );
         referrals of
-        <A HREF="<% popurl(2)."view/cust_main.cgi?$referral_custnum" %>"><% $refcustlabel %></A>
+        <A HREF="<% popurl(2)."view/cust_main.cgi?$referral_custnum" %>"><% $refcustlabel |h %></A>
         <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
 
 %    my $max = 8;
 %    foreach my $cust_pkg ( @{$all_pkgs{$custnum}} ) {
 %      my %cust_svc_by_svcpart;
 %      my $rows = 0;
-%      foreach my $part_svc ( $cust_pkg->part_svc ) {
+%      #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 )
+%        qsearch({
+%          'select'      => 'part_svc.*, COUNT(*) AS num_cust_svc',
+%          'table'       => 'part_svc', 
+%          'addl_from'   => 'LEFT JOIN cust_svc USING ( svcpart )',
+%          'extra_sql'   => 'WHERE pkgnum = ? '.
+%                           ' GROUP BY '. join(', ',
+%                             map "part_svc.$_", fields('part_svc')
+%                           ),
+%          'extra_param' => [ [$cust_pkg->pkgnum,'int'] ],
+%        })
+%      ) {
 %        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;
+%          #further optimization opportunities: don't need to re-pull in another $part_svc object, sorting this is expensive, etc.
+%          $cust_svc_by_svcpart{$svcpart} = [ $cust_pkg->cust_svc($part_svc->svcpart) ];
 %          $rows += $num_cust_svc;
 %        } #if summarize
 %      } #foreach $part_svc
 %      $view = $p. 'view/cust_main.cgi?'. $custnum;
 %    }
 %    my $pcompany = $company
-%      ? qq!<A HREF="$view"><FONT SIZE=-1>$company</FONT></A>!
+%      ? qq!<A HREF="$view"><FONT SIZE=-1>!. encode_entities($company). '</FONT></A>'
 %      : '<FONT SIZE=-1>&nbsp;</FONT>';
 %    
-%    my $status = $cust_main->status;
-%    my $statuscol = $cust_main->statuscolor;
+%    my $status_label = $cust_main->status_label;
+%    my $statuscolor = $cust_main->statuscolor;
 
     <TR>
       <TD CLASS="grid" ALIGN="right" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
         <A HREF="<% $view %>"><FONT SIZE=-1><% $cust_main->display_custnum %></FONT></A>
       </TD>
       <TD CLASS="grid" ALIGN="center" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
-        <FONT SIZE="-1" COLOR="#<% $statuscol %>"><B><% ucfirst($status) %></B></FONT>
+        <FONT SIZE="-1" COLOR="#<% $statuscolor %>"><B><% $status_label %></B></FONT>
       </TD>
       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
-        <A HREF="<% $view %>"><FONT SIZE=-1><% "$last, $first" %></FONT></A>
+        <A HREF="<% $view %>"><FONT SIZE=-1><% "$last, $first" |h %></FONT></A>
       </TD>
       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>>
         <% $pcompany %>
 
 %    my $n1 = '';
 %    foreach ( @{$all_pkgs{$custnum}} ) {
+%      local($FS::cust_svc::cache_enabled) = 1; #for $cust_svc->part_svc
 %      my $pkgnum = $_->pkgnum;
 %      my $part_pkg = $_->part_pkg;
 %
-%      my $pkg_comment = $part_pkg->pkg_comment(nopkgpart => 1);
-%      my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
+%      my $pkg_comment = $part_pkg->pkg_comment( cust_pkg=>$_, nopkgpart=>1 );
+%      my $show = $default_customer_view =~ /^(jumbo|packages)$/
 %                   ? ''
 %                   : ';show=packages';
 %      my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
 %      my $pkg_rowspan = shift @pkg_rowspans;
 
         <% $n1 %><TD CLASS="grid" BGCOLOR="<% $bgcolor %>"  ROWSPAN="<% $pkg_rowspan%>">
-            <A HREF="<% $pkgview %>"><FONT SIZE=-1><% $pkg_comment %></FONT></A>
+            <A HREF="<% $pkgview %>"><FONT SIZE=-1><% $pkg_comment |h %></FONT></A>
         </TD>
 
 %       my $n2 = '';
 %         }
 %         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
 my $curuser = $FS::CurrentUser::CurrentUser;
 
 die "access denied"
-  unless $curuser->access_right('List all customers');
+  unless $curuser->access_right('List all customers')
+      || $curuser->access_right('List customers');
 
 my $conf = new FS::Conf;
-my $maxrecords = $conf->config('maxsearchrecordsperpage');
+my $maxrecords = $conf->config('maxsearchrecordsperpage') || 100;
 # 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;
 
@@ -329,9 +356,11 @@ my(@cust_main, $sortby, $orderby);
 my @select = ();
 my @addl_headers = ();
 my @addl_cols = ();
-if ( $cgi->param('browse')
-     || $cgi->param('otaker_on')
-     || $cgi->param('agentnum_on')
+if ( (    $cgi->param('browse')
+       || $cgi->param('otaker_on')
+       || $cgi->param('agentnum_on')
+     )
+     and $curuser->access_right('List all customers')
 ) {
 
   my %search = ();
@@ -341,7 +370,12 @@ if ( $cgi->param('browse')
     if ( $query eq 'custnum' ) {
       if ( $conf->exists('cust_main-default_agent_custid') ) {
         $sortby=\*display_custnum_sort;
-        $orderby = "ORDER BY CASE WHEN agent_custid IS NOT NULL AND agent_custid != '' THEN CAST(agent_custid AS BIGINT) ELSE custnum END";
+        $orderby = "ORDER BY CASE WHEN agent_custid IS NOT NULL
+                                   AND agent_custid != ''
+                                   AND agent_custid ". regexp_sql. " '^[0-9]+\$'
+                             THEN CAST(agent_custid AS BIGINT)
+                             ELSE custnum
+                             END";
       } else {
         $sortby=\*custnum_sort;
         $orderby = "ORDER BY custnum";
@@ -393,6 +427,7 @@ if ( $cgi->param('browse')
 
   push @qual, FS::cust_main->cancel_sql   if $cgi->param('cancelled');
   push @qual, FS::cust_main->prospect_sql if $cgi->param('prospect');
+  push @qual, FS::cust_main->ordered_sql  if $cgi->param('ordered');
   push @qual, FS::cust_main->active_sql   if $cgi->param('active');
   push @qual, FS::cust_main->inactive_sql if $cgi->param('inactive');
   push @qual, FS::cust_main->susp_sql     if $cgi->param('suspended');
@@ -470,11 +505,10 @@ if ( $cgi->param('browse')
     $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
     push @cust_main, smart_search(
       'search'            => scalar($cgi->param('search_cust')),
-      'no_fuzzy_on_exact' => ! $curuser->option('enable_fuzzy_on_exact'),
     );
   }
 
-  @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
+  @cust_main = grep { $_->status ne 'cancelled' } @cust_main
     if ! $cgi->param('cancelled')
        && (
          $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
@@ -486,11 +520,34 @@ 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 %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 {
@@ -532,7 +589,7 @@ sub cardsearch {
 
   my($card)=$cgi->param('card');
   $card =~ s/\D//g;
-  $card =~ /^(\d{13,16}|\d{8,9})$/ or errorpage(emt("Illegal card number"));
+  $card =~ /^(\d{13,19}|\d{8,9})$/ or errorpage(emt("Illegal card number"));
   my($payinfo)=$1;
 
   [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}),
@@ -653,12 +710,20 @@ sub address2search {
       or errorpage(emt("Illegal address2"));
   my $address2 = $1;
 
-  push @cust_main, qsearch( 'cust_main',
-                            { 'address2' => { 'op'    => 'ILIKE',
-                                              'value' => $address2 } } );
-  push @cust_main, qsearch( 'cust_main',
-                            { 'ship_address2' => { 'op'    => 'ILIKE',
-                                                   'value' => $address2 } } );
+  # matching at the start or end of an address, but not in the middle
+  my @where;
+  foreach my $toggle (0,1) {
+    push @where, 'LOWER(cust_location.address2) LIKE LOWER('
+                 . dbh->quote($toggle ? $address2 . '%' : '%' . $address2)
+                 . ')';
+  }
+
+  push @cust_main, qsearch({
+    'debug'     => 1,
+    'table'     => 'cust_main',
+    'addl_from' => 'JOIN cust_location ON (cust_location.locationnum IN (cust_main.bill_locationnum, cust_main.ship_locationnum))',
+    'extra_sql' => 'WHERE ' . join(' OR ',@where),
+  });
 
   \@cust_main;
 }