option to show both open and closed RADIUS sessions, #21483
[freeside.git] / FS / FS / part_export / sqlradius.pm
index 58cc5be..db66c39 100644 (file)
@@ -597,7 +597,8 @@ New-style: pass a hashref with the following keys:
 
 =item stoptime_end - Upper bound for AcctStopTime, as a UNIX timestamp
 
-=item open_sessions - Only show records with no AcctStopTime (typically used without stoptime_* options and with starttime_* options instead)
+=item session_status - 'closed' to only show records with AcctStopTime,
+'open' to only show records I<without> AcctStopTime, empty to show both.
 
 =item starttime_start - Lower bound for AcctStartTime, as a UNIX timestamp
 
@@ -727,17 +728,24 @@ sub usage_sessions {
     push @where, " CalledStationID LIKE 'sip:$prefix\%'";
   }
 
-  if ( $start ) {
-    push @where, "$str2time AcctStopTime ) >= ?";
-    push @param, $start;
-  }
-  if ( $end ) {
-    push @where, "$str2time AcctStopTime ) <= ?";
-    push @param, $end;
+  my $acctstoptime = '';
+  if ( $opt->{session_status} ne 'open' ) {
+    if ( $start ) {
+      $acctstoptime .= "$str2time AcctStopTime ) >= ?";
+      push @param, $start;
+      $acctstoptime .= ' AND ' if $end;
+    }
+    if ( $end ) {
+      $acctstoptime .= "$str2time AcctStopTime ) <= ?";
+      push @param, $end;
+    }
   }
-  if ( $opt->{open_sessions} ) {
-    push @where, 'AcctStopTime IS NULL';
+  if ( $opt->{session_status} ne 'closed' ) {
+    $acctstoptime = "( $acctstoptime ) OR " if $acctstoptime;
+    $acctstoptime .= 'AcctStopTime IS NULL';
   }
+  push @where, $acctstoptime;
+
   if ( $opt->{starttime_start} ) {
     push @where, "$str2time AcctStartTime ) >= ?";
     push @param, $opt->{starttime_start};
@@ -756,10 +764,14 @@ sub usage_sessions {
   my $orderby = 'ORDER BY AcctStartTime DESC';
   $orderby = '' if $summarize;
 
-  my $sth = $dbh->prepare('SELECT '. join(', ', @fields).
-                          "  FROM radacct $where $groupby $orderby
-                        ") or die $dbh->errstr;                                 
-  $sth->execute(@param) or die $sth->errstr;
+  my $sql = 'SELECT '. join(', ', @fields).
+            "  FROM radacct $where $groupby $orderby";
+  if ( $DEBUG ) {
+    warn $sql;
+    warn join(',', @param);
+  }
+  my $sth = $dbh->prepare($sql) or die $dbh->errstr;
+  $sth->execute(@param)         or die $sth->errstr;
 
   [ map { { %$_ } } @{ $sth->fetchall_arrayref({}) } ];