multi-select for agent on RADIUS usage report, RT#73050
[freeside.git] / httemplate / search / sqlradius_usage.html
index b5551ad..494a275 100644 (file)
@@ -39,6 +39,7 @@
                      @svc_fields,
                      @svc_usage,
                    ],
+  'order_by_sql' => $order_by_sql,
   'links'       => [ #( map { $_ ne 'Cust. Status' ? $link_cust : '' }
                      #  FS::UI::Web::cust_header() ),
                      $link_cust,
@@ -63,20 +64,36 @@ my $curuser = $FS::CurrentUser::CurrentUser;
 die "access denied" unless $curuser->access_right('List services');
 
 my $title = 'Data Usage Report - '; 
-my $agentnum;
-my @include_agents;
+my $agentnum = '';
+my @include_agents = ();
 
-if ( $opt{'agentnum'} ) {
+if ( $opt{'agentnum'} =~ /^(\d+)$/ ) {
   $agentnum = $opt{'agentnum'};
-} elsif ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
-  $agentnum = $1;
+} else {
+
+  my @agentnums = grep /^(\d+)$/, $cgi->param('agentnum');
+
+  if ( ! @agentnums ) {
+    @include_agents = qsearch({ 'table'     => 'agent',
+                                'hashref'   => { 'disabled'=>'' },
+                                'extra_sql' => ' AND '. $curuser->agentnums_sql,
+                             });
+  } elsif ( scalar(@agentnums) == 1 ) {
+    $agentnum = $agentnums[0];
+  } else {
+    @include_agents = qsearch({ 'table'     => 'agent',
+                                'hashref'   => { 'disabled' => '', },
+                                'extra_sql' => 'AND agentnum IN ('.
+                                                 join(',',@agentnums). ') '.
+                                               ' AND '. $curuser->agentnums_sql,
+                             });
+  }
+
 }
 
 if ( $agentnum ) {
   my $agent = FS::agent->by_key($agentnum);
   $title = $agent->agent." $title";
-} else {
-  @include_agents = qsearch('agent', {});
 }
 
 # usage query params
@@ -161,7 +178,7 @@ if ( exists($opt{usage_by_username}) ) {
     }
   } else {
     # one query, get everyone's data
-    my $usage = $export->usage_sessions(\%usage_param);
+    $usage = $export->usage_sessions(\%usage_param);
   }
 
   # rearrange to be indexed by username.
@@ -257,7 +274,23 @@ sub bytes_to_gb {
   $_[0] ?  sprintf('%.3f', $_[0] / (1024*1024*1024.0)) : '';
 }
 
-warn Dumper \%usage_by_username;
+my $conf = new FS::Conf;
+my $order_by_sql = {
+  'name'            => "CASE WHEN cust_main.company IS NOT NULL
+                                  AND cust_main.company != ''
+                             THEN CONCAT(cust_main.company,' (',cust_main.last,', ',cust_main.first,')')
+                             ELSE CONCAT(cust_main.last,', ',cust_main.first)
+                        END",
+  'display_custnum' => $conf->exists('cust_main-default_agent_custid')
+                       ? "CASE WHEN cust_main.agent_custid IS NOT NULL
+                                    AND cust_main.agent_custid != ''
+                                    AND cust_main.agent_custid ". regexp_sql. " '^[0-9]+\$'
+                               THEN CAST(cust_main.agent_custid AS BIGINT)
+                               ELSE cust_main.custnum
+                          END"
+                       : "custnum",
+};
 
+#warn Dumper \%usage_by_username;
 
 </%init>