untested code to suck in CDRs in from VoIP RADIUS exports, RT#4100
[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;
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   #N/A
40   #'overlimit_groups' => { label => 'Radius groups to assign to svc_acct which has exceeded its bandwidth or time limit', } ,
41   #'groups_susp_reason' => { label =>
42   #                           'Radius group mapping to reason (via template user) (svcnum|username|username@domain  reasonnum|reason)',
43   #                          type  => 'textarea',
44   #                        },
45
46 ;
47
48 %info = (
49   'svc'      => 'svc_phone',
50   'desc'     => 'Real-time export to SQL-backed RADIUS (FreeRADIUS, ICRADIUS) for phone provisioning and rating',
51   'options'  => \%options,
52   'notes'    => <<END,
53 Real-time export of <b>radcheck</b> table
54 <!--, <b>radreply</b> and <b>usergroup</b>-- tables>
55 to any SQL database for <a href="http://www.freeradius.org/">FreeRADIUS</a>
56 or <a href="http://radius.innercite.com/">ICRADIUS</a>.
57 <br><br>
58
59 This export is for phone/VoIP provisioning and rating.  For a regular RADIUS
60 export, see sqlradius.
61 <br><br>
62
63 <!--An existing RADIUS database will be updated in realtime, but you can use
64 <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.9:Documentation:Developer/bin/freeside-phone_sqlradius-reset">freeside-phone_sqlradius-reset</a>
65 to delete the entire RADIUS database and repopulate the tables from the
66 Freeside database.
67 <br><br>
68 -->
69
70 See the
71 <a href="http://search.cpan.org/dist/DBI/DBI.pm#connect">DBI documentation</a>
72 and the
73 <a href="http://search.cpan.org/search?mode=module&query=DBD%3A%3A">documentation for your DBD</a>
74 for the exact syntax of a DBI data source.
75
76 END
77 );
78
79 sub rebless { shift; }
80
81 sub export_username {
82   my($self, $svc_phone) = (shift, shift);
83   $svc_phone->countrycode. $svc_phone->phonenum;
84 }
85
86 sub _export_suspend {}
87 sub _export_unsuspend {}
88
89 #probably harmless that we ->can('usage_sessions').... ?
90
91 #we want to feed these into CDRs, not update svc_acct records
92 sub update_svc {
93   my $self = shift;
94
95   my $fdbh = dbh;
96   my $dbh = sqlradius_connect( map $self->option($_),
97                                    qw( datasrc username password ) );
98
99   my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
100
101
102
103   my @fields = qw( radacctid username realm acctsessiontime );
104
105   my @param = ();
106   my $where = '';
107
108   my $sth = $dbh->prepare("
109     SELECT RadAcctId, UserName, AcctSessionTime,
110            $str2time AcctStartTime),  $str2time AcctStopTime), 
111            CallingStationID, CalledStationID
112       FROM radacct
113       WHERE FreesideStatus IS NULL
114         AND AcctStopTime != 0
115   ") or die $dbh->errstr;
116   $sth->execute() or die $sth->errstr;
117
118   while ( my $row = $sth->fetchrow_arrayref ) {
119     my( $RadAcctId, $UserName, $AcctSessionTime,
120         $AcctStartTime, $AcctStopTime, 
121         $CallingStationID, $CalledStationID,
122       )= @$row;
123     warn "processing record: ".
124          "$RadAcctId ($UserName for ${AcctSessionTime}s"
125       if $DEBUG;
126
127     my $oldAutoCommit = $FS::UID::AutoCommit; # can't undo side effects, but at
128     local $FS::UID::AutoCommit = 0;           # least we can avoid over counting
129
130     my $cdr = new FS::cdr {
131       'src'           => $CallingStationID,
132       'charged_party' => $UserName,
133       'dst'           => $CalledStationID,
134       'startdate'     => $AcctStartTime,
135       'enddate'       => $AcctStopTime,
136       'duration'      => $AcctStopTime - $AcctStartTime,
137       'billsec'       => $AcctSessionTime,
138     };
139
140     my $errinfo = "for RADIUS detail RadAcctID $RadAcctId ".
141                   "(UserName $UserName)";
142
143     my $error = $cdr->insert;
144     my $status = $error ? 'skipped' : 'done';
145
146     warn "setting FreesideStatus to $status $errinfo\n" if $DEBUG; 
147     my $psth = $dbh->prepare("UPDATE radacct
148                                 SET FreesideStatus = ?
149                                 WHERE RadAcctId = ?"
150     ) or die $dbh->errstr;
151     $psth->execute($status, $RadAcctId) or die $psth->errstr;
152
153     $fdbh->commit or die $fdbh->errstr if $oldAutoCommit;
154
155   }
156
157 }
158
159 1;
160