This commit was generated by cvs2svn to compensate for changes in r11022,
[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   #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   my @fields = qw( radacctid username realm acctsessiontime );
102
103   my @param = ();
104   my $where = '';
105
106   my $sth = $dbh->prepare("
107     SELECT RadAcctId, UserName, AcctSessionTime,
108            $str2time AcctStartTime),  $str2time AcctStopTime), 
109            CallingStationID, CalledStationID
110       FROM radacct
111       WHERE FreesideStatus IS NULL
112         AND AcctStopTime != 0
113   ") or die $dbh->errstr;
114   $sth->execute() or die $sth->errstr;
115
116   while ( my $row = $sth->fetchrow_arrayref ) {
117     my( $RadAcctId, $UserName, $AcctSessionTime,
118         $AcctStartTime, $AcctStopTime, 
119         $CallingStationID, $CalledStationID,
120       )= @$row;
121     warn "processing record: ".
122          "$RadAcctId ($UserName for ${AcctSessionTime}s"
123       if $DEBUG;
124
125     my $oldAutoCommit = $FS::UID::AutoCommit; # can't undo side effects, but at
126     local $FS::UID::AutoCommit = 0;           # least we can avoid over counting
127
128     my $cdr = new FS::cdr {
129       'src'           => $CallingStationID,
130       'charged_party' => $UserName,
131       'dst'           => $CalledStationID,
132       'startdate'     => $AcctStartTime,
133       'enddate'       => $AcctStopTime,
134       'duration'      => $AcctStopTime - $AcctStartTime,
135       'billsec'       => $AcctSessionTime,
136     };
137
138     my $errinfo = "for RADIUS detail RadAcctID $RadAcctId ".
139                   "(UserName $UserName)";
140
141     my $error = $cdr->insert;
142     my $status = $error ? 'skipped' : 'done';
143
144     warn "setting FreesideStatus to $status $errinfo\n" if $DEBUG; 
145     my $psth = $dbh->prepare("UPDATE radacct
146                                 SET FreesideStatus = ?
147                                 WHERE RadAcctId = ?"
148     ) or die $dbh->errstr;
149     $psth->execute($status, $RadAcctId) or die $psth->errstr;
150
151     $fdbh->commit or die $fdbh->errstr if $oldAutoCommit;
152
153   }
154
155 }
156
157 1;
158