X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_export%2Fphone_sqlradius.pm;h=24f7845347d4e768179f901e230a10ea25a45c71;hb=5f0e4d1d57c18d5bb8a52de4f7d4f519db5327f0;hp=b93fe8c1a564ade959ba589c2c82e3b45f1c62e7;hpb=2641816698538bbe52d56365266a66e292ce08f1;p=freeside.git diff --git a/FS/FS/part_export/phone_sqlradius.pm b/FS/FS/part_export/phone_sqlradius.pm index b93fe8c1a..24f784534 100644 --- a/FS/FS/part_export/phone_sqlradius.pm +++ b/FS/FS/part_export/phone_sqlradius.pm @@ -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 ); @@ -88,5 +88,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;