export host selection per service, RT#17914
[freeside.git] / FS / FS / part_export / phone_sqlradius.pm
index b93fe8c..46c372c 100644 (file)
@@ -2,9 +2,9 @@ package FS::part_export::phone_sqlradius;
 
 use vars qw(@ISA $DEBUG %info );
 use Tie::IxHash;
-use FS::Record; #qw( dbh qsearch qsearchs str2time_sql );
+use FS::Record qw( dbh str2time_sql ); #qsearch qsearchs );
 #use FS::part_export;
-use FS::part_export::sqlradius;
+use FS::part_export::sqlradius qw(sqlradius_connect);
 #use FS::svc_phone;
 #use FS::export_svc;
 #use Carp qw( cluck );
@@ -36,22 +36,15 @@ tie %options, 'Tie::IxHash',
   #  label => 'Show the Called-Station-ID on session reports',
   #},
 
-  #N/A
-  #'overlimit_groups' => { label => 'Radius groups to assign to svc_acct which has exceeded its bandwidth or time limit', } ,
-  #'groups_susp_reason' => { label =>
-  #                           'Radius group mapping to reason (via template user) (svcnum|username|username@domain  reasonnum|reason)',
-  #                          type  => 'textarea',
-  #                        },
-
 ;
 
 %info = (
-  'svc'      => 'svc_phone',
-  'desc'     => 'Real-time export to SQL-backed RADIUS (FreeRADIUS, ICRADIUS) for phone provisioning and rating',
-  'options'  => \%options,
-  'notes'    => <<END,
+  'svc'        => 'svc_phone',
+  'desc'       => 'Real-time export to SQL-backed RADIUS (FreeRADIUS, ICRADIUS) for phone provisioning and rating',
+  'options'    => \%options,
+  'no_machine' => 1,
+  'notes'      => <<END,
 Real-time export of <b>radcheck</b> table
-<!--, <b>radreply</b> and <b>usergroup</b>-- tables>
 to any SQL database for <a href="http://www.freeradius.org/">FreeRADIUS</a>
 or <a href="http://radius.innercite.com/">ICRADIUS</a>.
 <br><br>
@@ -88,5 +81,71 @@ sub _export_unsuspend {}
 
 #probably harmless that we ->can('usage_sessions').... ?
 
+#we want to feed these into CDRs, not update svc_acct records
+sub update_svc {
+  my $self = shift;
+
+  my $fdbh = dbh;
+  my $dbh = sqlradius_connect( map $self->option($_),
+                                   qw( datasrc username password ) );
+
+  my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
+
+  my @fields = qw( radacctid username realm acctsessiontime );
+
+  my @param = ();
+  my $where = '';
+
+  my $sth = $dbh->prepare("
+    SELECT RadAcctId, UserName, AcctSessionTime,
+           $str2time AcctStartTime),  $str2time AcctStopTime), 
+           CallingStationID, CalledStationID
+      FROM radacct
+      WHERE FreesideStatus IS NULL
+        AND AcctStopTime != 0
+  ") or die $dbh->errstr;
+  $sth->execute() or die $sth->errstr;
+
+  while ( my $row = $sth->fetchrow_arrayref ) {
+    my( $RadAcctId, $UserName, $AcctSessionTime,
+        $AcctStartTime, $AcctStopTime, 
+        $CallingStationID, $CalledStationID,
+      )= @$row;
+    warn "processing record: ".
+         "$RadAcctId ($UserName for ${AcctSessionTime}s"
+      if $DEBUG;
+
+    my $oldAutoCommit = $FS::UID::AutoCommit; # can't undo side effects, but at
+    local $FS::UID::AutoCommit = 0;           # least we can avoid over counting
+
+    my $cdr = new FS::cdr {
+      'src'           => $CallingStationID,
+      'charged_party' => $UserName,
+      'dst'           => $CalledStationID,
+      'startdate'     => $AcctStartTime,
+      'enddate'       => $AcctStopTime,
+      'duration'      => $AcctStopTime - $AcctStartTime,
+      'billsec'       => $AcctSessionTime,
+    };
+
+    my $errinfo = "for RADIUS detail RadAcctID $RadAcctId ".
+                  "(UserName $UserName)";
+
+    my $error = $cdr->insert;
+    my $status = $error ? 'skipped' : 'done';
+
+    warn "setting FreesideStatus to $status $errinfo\n" if $DEBUG; 
+    my $psth = $dbh->prepare("UPDATE radacct
+                                SET FreesideStatus = ?
+                                WHERE RadAcctId = ?"
+    ) or die $dbh->errstr;
+    $psth->execute($status, $RadAcctId) or die $psth->errstr;
+
+    $fdbh->commit or die $fdbh->errstr if $oldAutoCommit;
+
+  }
+
+}
+
 1;