agent-virtualize credit card surcharge percentage, RT#72961
[freeside.git] / FS / FS / part_export / phone_sqlopensips.pm
1 package FS::part_export::phone_sqlopensips;
2
3 use vars qw(@ISA @EXPORT_OK %info %options);
4 use Exporter;
5 use Tie::IxHash;
6 use FS::Record qw( dbh qsearch qsearchs );
7 use FS::part_export;
8 use FS::svc_phone;
9 use FS::export_svc;
10 use LWP::UserAgent;
11
12 @ISA = qw(FS::part_export);
13
14 tie %options, 'Tie::IxHash',
15   'datasrc'  => { label=>'DBI data source ' },
16   'username' => { label=>'Database username' },
17   'password' => { label=>'Database password' },
18   'xmlrpc_url' => { label=>'XMLRPC URL' },
19   # XXX: in future, add non-agent-virtualized config, i.e. per-export setting of gwlist, routeid, description, etc.
20   # and/or setting description from the phone_name column
21 ;
22
23 %info = (
24   'svc'        => 'svc_phone',
25   'desc'       => 'Export DIDs to OpenSIPs dr_rules table',
26   'options'    => \%options,
27   'no_machine' => 1,
28   'notes'      => 'Export DIDs to OpenSIPs dr_rules table',
29 );
30
31 sub rebless { shift; }
32
33 sub _export_insert {
34   my($self, $svc_x) = (shift, shift);
35
36   my $conf = new FS::Conf;
37   my $agentnum = $svc_x->cust_svc->cust_pkg->cust_main->agentnum || 0;
38   my $gwlist = $conf->config('opensips_gwlist',$agentnum) || '';
39   my $description = $conf->config('opensips_description',$agentnum) || '';
40   my $route = $conf->config('opensips_route',$agentnum) || '';
41
42   my $dbh = $self->opensips_connect;
43   my $sth = $dbh->prepare("insert into dr_rules ".
44             "( groupid, prefix, timerec, routeid, gwlist, description ) ".
45             " values ( ?, ?, ?, ?, ?, ? )") or die $dbh->errstr;
46   $sth->execute('0',$svc_x->phonenum,'',$route,$gwlist,$description)
47             or die $sth->errstr;
48   $dbh->disconnect;
49   $self->dr_reload; # XXX: if this fails, do we delete what we just inserted?
50 }
51
52 sub opensips_connect {
53     my $self = shift;
54     DBI->connect($self->option('datasrc'),$self->option('username'),
55                         $self->option('password')) or die $DBI::errstr;
56 }
57
58 sub _export_replace {
59    '';
60 }
61
62 sub _export_suspend {
63   my( $self, $svc_phone ) = (shift, shift);
64   '';
65 }
66
67 sub _export_unsuspend {
68   my( $self, $svc_phone ) = (shift, shift);
69   '';
70 }
71
72 sub _export_delete {
73   my( $self, $svc_x ) = (shift, shift);
74   my $dbh = $self->opensips_connect;
75   my $sth = $dbh->prepare("delete from dr_rules where prefix = ?")
76     or die $dbh->errstr;
77   $sth->execute($svc_x->phonenum) or die $sth->errstr;
78   $dbh->disconnect;
79   $self->dr_reload; # XXX: if this fails, do we re-insert what we just deleted?
80 }
81
82 sub dr_reload {
83     my $self = shift;
84     my $reqxml = "<?xml version=\"1.0\"?>
85 <methodCall>
86   <methodName>dr_reload</methodName>
87 </methodCall>";
88     my $ua = LWP::UserAgent->new;
89     my $resp = $ua->post(   $self->option('xmlrpc_url'),
90                             Content_Type => 'text/xml', 
91                             Content => $reqxml );
92     return "invalid HTTP response from OpenSIPS: " . $resp->status_line
93         unless $resp->is_success;
94     '';
95 }
96
97 1;