6b14bed3cb16442393eb25b9a92dac8789dd6985
[freeside.git] / FS / FS / part_export / phone_sqlradius.pm
1 package FS::part_export::phone_sqlradius;
2
3 use vars qw(@ISA $DEBUG %info );
4 use Tie::IxHash;
5 use FS::Record qw( dbh str2time_sql ); #qsearch qsearchs );
6 #use FS::part_export;
7 use FS::part_export::sqlradius qw(sqlradius_connect);
8 #use FS::svc_phone;
9 #use FS::export_svc;
10 #use Carp qw( cluck );
11
12 @ISA = qw(FS::part_export::sqlradius);
13
14 $DEBUG = 0;
15
16 tie %options, 'Tie::IxHash',
17   'datasrc'  => { label=>'DBI data source ' },
18   'username' => { label=>'Database username' },
19   'password' => { label=>'Database password' },
20   'ignore_accounting' => {
21     type  => 'checkbox',
22     label => 'Ignore accounting records from this database'
23   },
24   'hide_ip' => {
25     type  => 'checkbox',
26     label => 'Hide IP address information on session reports',
27   },
28   'hide_data' => {
29     type  => 'checkbox',
30     label => 'Hide download/upload information on session reports',
31   },
32
33   #should be default for this one, right?
34   #'show_called_station' => {
35   #  type  => 'checkbox',
36   #  label => 'Show the Called-Station-ID on session reports',
37   #},
38
39 ;
40
41 %info = (
42   'svc'      => 'svc_phone',
43   'desc'     => 'Real-time export to SQL-backed RADIUS (FreeRADIUS, ICRADIUS) for phone provisioning and rating',
44   'options'  => \%options,
45   'notes'    => <<END,
46 Real-time export of <b>radcheck</b> table
47 to any SQL database for <a href="http://www.freeradius.org/">FreeRADIUS</a>
48 or <a href="http://radius.innercite.com/">ICRADIUS</a>.
49 <br><br>
50
51 This export is for phone/VoIP provisioning and rating.  For a regular RADIUS
52 export, see sqlradius.
53 <br><br>
54
55 <!--An existing RADIUS database will be updated in realtime, but you can use
56 <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.9:Documentation:Developer/bin/freeside-phone_sqlradius-reset">freeside-phone_sqlradius-reset</a>
57 to delete the entire RADIUS database and repopulate the tables from the
58 Freeside database.
59 <br><br>
60 -->
61
62 See the
63 <a href="http://search.cpan.org/dist/DBI/DBI.pm#connect">DBI documentation</a>
64 and the
65 <a href="http://search.cpan.org/search?mode=module&query=DBD%3A%3A">documentation for your DBD</a>
66 for the exact syntax of a DBI data source.
67
68 END
69 );
70
71 sub rebless { shift; }
72
73 sub export_username {
74   my($self, $svc_phone) = (shift, shift);
75   $svc_phone->countrycode. $svc_phone->phonenum;
76 }
77
78 sub _export_suspend {}
79 sub _export_unsuspend {}
80
81 #probably harmless that we ->can('usage_sessions').... ?
82
83 #we want to feed these into CDRs, not update svc_acct records
84 sub update_svc {
85   my $self = shift;
86
87   my $fdbh = dbh;
88   my $dbh = sqlradius_connect( map $self->option($_),
89                                    qw( datasrc username password ) );
90
91   my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
92
93   my @fields = qw( radacctid username realm acctsessiontime );
94
95   my @param = ();
96   my $where = '';
97
98   my $sth = $dbh->prepare("
99     SELECT RadAcctId, UserName, AcctSessionTime,
100            $str2time AcctStartTime),  $str2time AcctStopTime), 
101            CallingStationID, CalledStationID
102       FROM radacct
103       WHERE FreesideStatus IS NULL
104         AND AcctStopTime != 0
105   ") or die $dbh->errstr;
106   $sth->execute() or die $sth->errstr;
107
108   while ( my $row = $sth->fetchrow_arrayref ) {
109     my( $RadAcctId, $UserName, $AcctSessionTime,
110         $AcctStartTime, $AcctStopTime, 
111         $CallingStationID, $CalledStationID,
112       )= @$row;
113     warn "processing record: ".
114          "$RadAcctId ($UserName for ${AcctSessionTime}s"
115       if $DEBUG;
116
117     my $oldAutoCommit = $FS::UID::AutoCommit; # can't undo side effects, but at
118     local $FS::UID::AutoCommit = 0;           # least we can avoid over counting
119
120     my $cdr = new FS::cdr {
121       'src'           => $CallingStationID,
122       'charged_party' => $UserName,
123       'dst'           => $CalledStationID,
124       'startdate'     => $AcctStartTime,
125       'enddate'       => $AcctStopTime,
126       'duration'      => $AcctStopTime - $AcctStartTime,
127       'billsec'       => $AcctSessionTime,
128     };
129
130     my $errinfo = "for RADIUS detail RadAcctID $RadAcctId ".
131                   "(UserName $UserName)";
132
133     my $error = $cdr->insert;
134     my $status = $error ? 'skipped' : 'done';
135
136     warn "setting FreesideStatus to $status $errinfo\n" if $DEBUG; 
137     my $psth = $dbh->prepare("UPDATE radacct
138                                 SET FreesideStatus = ?
139                                 WHERE RadAcctId = ?"
140     ) or die $dbh->errstr;
141     $psth->execute($status, $RadAcctId) or die $psth->errstr;
142
143     $fdbh->commit or die $fdbh->errstr if $oldAutoCommit;
144
145   }
146
147 }
148
149 1;
150