fix disappearing radius group bug, whew
[freeside.git] / FS / FS / svc_acct.pm
index 3afee7f..1411334 100644 (file)
@@ -1,7 +1,7 @@
 package FS::svc_acct;
 
 use strict;
-use vars qw( @ISA $noexport_hack $conf
+use vars qw( @ISA $DEBUG $me $noexport_hack $conf
              $dir_prefix @shells $usernamemin
              $usernamemax $passwordmin $passwordmax
              $username_ampersand $username_letter $username_letterfirst
@@ -32,6 +32,9 @@ use FS::Msgcat qw(gettext);
 
 @ISA = qw( FS::svc_Common );
 
+$DEBUG = 0;
+$me = '[FS::svc_acct]';
+
 #ask FS::UID to run this stuff for us later
 $FS::UID::callback{'FS::svc_acct'} = sub { 
   $conf = new FS::Conf;
@@ -496,6 +499,7 @@ sqlradius export only)
 sub replace {
   my ( $new, $old ) = ( shift, shift );
   my $error;
+  warn "$me replacing $old with $new\n" if $DEBUG;
 
   return "Username in use"
     if $old->username ne $new->username &&
@@ -522,7 +526,13 @@ sub replace {
   local $FS::UID::AutoCommit = 0;
   my $dbh = dbh;
 
+  # redundant, but so $new->usergroup gets set
+  my $error = $new->check;
+  return $error if $error;
+
   $old->usergroup( [ $old->radius_groups ] );
+  warn "old groups: ". join(' ',@{$old->usergroup}). "\n" if $DEBUG;
+  warn "new groups: ". join(' ',@{$new->usergroup}). "\n" if $DEBUG;
   if ( $new->usergroup ) {
     #(sorta) false laziness with FS::part_export::sqlradius::_export_replace
     my @newgroups = @{$new->usergroup};
@@ -562,18 +572,19 @@ sub replace {
     return $error if $error;
   }
 
-  #false laziness with sub insert (and cust_main)
-  my $queue = new FS::queue {
-    'svcnum' => $new->svcnum,
-    'job'    => 'FS::svc_acct::append_fuzzyfiles'
-  };
-  $error = $queue->insert($new->username);
-  if ( $error ) {
-    $dbh->rollback if $oldAutoCommit;
-    return "queueing job (transaction rolled back): $error";
+  if ( $new->username ne $old->username ) {
+    #false laziness with sub insert (and cust_main)
+    my $queue = new FS::queue {
+      'svcnum' => $new->svcnum,
+      'job'    => 'FS::svc_acct::append_fuzzyfiles'
+    };
+    $error = $queue->insert($new->username);
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "queueing job (transaction rolled back): $error";
+    }
   }
 
-
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
   ''; #no error
 }
@@ -935,16 +946,17 @@ sub seconds_since {
   $self->cust_svc->seconds_since(@_);
 }
 
-=item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END DATASRC DB_USERNAME DB_PASSWORD
+=item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
 
 Returns the numbers of seconds this account has been online between
 TIMESTAMP_START (inclusive) and TIMESTAMP_END (exclusive), according to an
-external SQL radacct table, such as those generated by ICRADIUS or FreeRADIUS.
-Sessions which started in the specified range but are still open are counted
-from session start to the end of the range.  Also, sessions which end in the
-range but started earlier are counted from the start of the range to session
-end.  Finally, sessions which start before the range but end after (or are
-still open) are counted for the entire range.
+external SQL radacct table, specified via sqlradius export.  Sessions which
+started in the specified range but are still open are counted from session
+start to the end of the range (unless they are over 1 day old, in which case
+they are presumed missing their stop record and not counted).  Also, sessions
+which end in therange but started earlier are counted from the start of the
+range to session end.  Finally, sessions which start before the range but end
+after are counted for the entire range.
 
 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
 L<perlfunc/"time">.  Also see L<Time::Local> and L<Date::Parse> for conversion
@@ -958,6 +970,24 @@ sub seconds_since_sqlradacct {
   $self->cust_svc->seconds_since_sqlradacct(@_);
 }
 
+=item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
+
+Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
+in this package for sessions ending between TIMESTAMP_START (inclusive) and
+TIMESTAMP_END (exclusive).
+
+TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
+L<perlfunc/"time">.  Also see L<Time::Local> and L<Date::Parse> for conversion
+functions.
+
+=cut
+
+#note: POD here, implementation in FS::cust_svc
+sub attribute_since_sqlradacct {
+  my $self = shift;
+  $self->cust_svc->attribute_since_sqlradacct(@_);
+}
+
 =item radius_groups
 
 Returns all RADIUS groups for this account (see L<FS::radius_usergroup>).