link customers back to tickets!
[freeside.git] / httemplate / search / cust_main.cgi
index 27f23de..48fbf41 100755 (executable)
@@ -48,6 +48,9 @@ $limit .= " OFFSET $offset" if $offset;
 my $total = 0;
 
 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')
@@ -65,6 +68,12 @@ if ( $cgi->param('browse')
     } elsif ( $query eq 'company' ) {
       $sortby=\*company_sort;
       $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
+    } elsif ( $query eq 'tickets' ) {
+      $sortby = \*tickets_sort;
+      $orderby = "ORDER BY tickets DESC";
+      push @select, FS::TicketSystem->sql_customer_tickets. " as tickets";
+      push @addl_headers, 'Tickets';
+      push @addl_cols, 'tickets';
     } else {
       die "unknown browse field $query";
     }
@@ -136,7 +145,14 @@ if ( $cgi->param('browse')
     }
   }
 
-  @cust_main = qsearch('cust_main', \%search, '',   
+  my $select;
+  if ( @select ) {
+    $select = 'cust_main.*, '. join (', ', @select);
+  } else {
+    $select = '*';
+  }
+
+  @cust_main = qsearch('cust_main', \%search, $select,   
                          "$addl_qual $orderby $limit" );
 
 #  foreach my $cust_main ( @just_cust_main ) {
@@ -312,6 +328,10 @@ if ( defined dbdef->table('cust_main')->column('ship_last') ) {
 END
 }
 
+foreach my $addl_header ( @addl_headers ) {
+  print "<TH>$addl_header</TH>";
+}
+
 print <<END;
         <TH>Packages</TH>
         <TH COLSPAN=2>Services</TH>
@@ -319,7 +339,6 @@ print <<END;
 END
 
   my(%saw,$cust_main);
-  my $p = popurl(2);
   foreach $cust_main (
     sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
   ) {
@@ -370,6 +389,15 @@ END
 END
     }
 
+    foreach my $addl_col ( @addl_cols ) {
+      print "<TD ROWSPAN=$rowspan>". 
+            qq!<A HREF="${p}rt/Search/Results.html?Order=ASC&Query=%20MemberOf%20%3D%20%27freeside%3A%2F%2Ffreeside%2Fcust_main%2F!.
+            $cust_main->custnum. 
+            qq!%27%20%20AND%20%28%20Status%20%3D%20%27open%27%20%20OR%20Status%20%3D%20%27new%27%20%20OR%20Status%20%3D%20%27stalled%27%20%29%20&Rows=50&OrderBy=id&Page=1&Format=%27%20%20%20%3Cb%3E%3Ca%20href%3D%22%2Ffreeside%2Frt%2FTicket%2FDisplay.html%3Fid%3D__id__%22%3E__id__%3C%2Fa%3E%3C%2Fb%3E%2FTITLE%3A%23%27%2C%20%0A%27%3Cb%3E%3Ca%20href%3D%22%2Ffreeside%2Frt%2FTicket%2FDisplay.html%3Fid%3D__id__%22%3E__Subject__%3C%2Fa%3E%3C%2Fb%3E%2FTITLE%3ASubject%27%2C%20%0A%27__Status__%27%2C%20%0A%27__QueueName__%27%2C%20%0A%27__OwnerName__%27%2C%20%0A%27__Priority__%27%2C%20%0A%27__NEWLINE__%27%2C%20%0A%27%27%2C%20%0A%27%3Csmall%3E__Requestors__%3C%2Fsmall%3E%27%2C%20%0A%27%3Csmall%3E__CreatedRelative__%3C%2Fsmall%3E%27%2C%20%0A%27%3Csmall%3E__ToldRelative__%3C%2Fsmall%3E%27%2C%20%0A%27%3Csmall%3E__LastUpdatedRelative__%3C%2Fsmall%3E%27%2C%20%0A%27%3Csmall%3E__TimeLeft__%3C%2Fsmall%3E%27">!.
+              $cust_main->get($addl_col).
+            "</A></TD>";
+    }
+
     my($n1)='';
     foreach ( @{$all_pkgs{$custnum}} ) {
       my $pkgnum = $_->pkgnum;
@@ -424,6 +452,10 @@ sub custnum_sort {
   $a->getfield('custnum') <=> $b->getfield('custnum');
 }
 
+sub tickets_sort {
+  $b->getfield('tickets') <=> $a->getfield('tickets');
+}
+
 sub custnumsearch {
 
   my $custnum = $cgi->param('custnum_text');