nibblebill integrateion, RT#19587
[freeside.git] / FS / FS / part_export / freeswitch_nibblebill.pm
1 package FS::part_export::freeswitch_nibblebill;
2 use base qw( FS::part_export );
3
4 use vars qw( %info ); # $DEBUG );
5 use Tie::IxHash;
6 use DBI;
7 #use FS::Record qw( qsearch ); #qsearchs );
8 #use FS::svc_phone;
9 #use FS::Schema qw( dbdef );
10
11 #$DEBUG = 1;
12
13 tie my %options, 'Tie::IxHash',
14   'datasrc'  => { label=>'DBI data source ' },                                  
15   'username' => { label=>'Database username' },                                 
16   'password' => { label=>'Database password' },   
17 ;
18
19 %info = (
20   'svc'     => 'svc_phone',
21   'desc'    => 'Provision prepaid credit to a FreeSWITCH mod_nibblebill database',
22   'options' => \%options,
23   'notes'   => <<'END',
24 Provision prepaid credit to a FreeSWITCH mod_nibblebill database.  Use with the <b>Prepaid credit in FreeSWITCH mod_nibblebill</b> price plan.
25 <br><br>
26  See the                                                     
27 <a href="http://search.cpan.org/dist/DBI/DBI.pm#connect">DBI documentation</a>  
28 and the                                                                         
29 <a href="http://search.cpan.org/search?mode=module&query=DBD%3A%3A">documentation for your DBD</a>
30 for the exact syntax of a DBI data source.
31 END
32 );
33
34 sub rebless { shift; }
35
36 sub _export_insert {
37   my( $self, $svc_phone ) = ( shift, shift );
38
39   #add phonenum to db (unless it is there already)
40
41   # w/the setup amount makes the most sense in this usage (rather than the
42   #  (balance/pkg-balance), since you would order the package, then provision
43   #   the phone number.
44   my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
45   my $amount = $cust_pkg ? $cust_pkg->part_pkg->option('setup_fee')
46                          : '';
47
48   my $queue = new FS::queue {
49     svcnum => $svcnum,
50     job    => 'FS::part_export::freeswitch_nibblebill::nibblebill_insert',
51   };
52   $queue->insert(
53     $self->option('datasrc'),
54     $self->option('username'),
55     $self->option('password'),
56     $svc_phone->phonenum,
57     $amount,
58   );
59
60 }
61
62 sub nibblebill_insert {
63   my($datasrc, $username, $password, $phonenum, $amount) = @_;
64   my $dbh = DBI->connect($datasrc, $username, $password) or die $DBI::errstr; 
65
66   #check for existing account
67   $dbh->{FetchHashKeyName} = 'NAME_lc';
68   my $esth = $dbh->prepare('SELECT id, name, cash FROM accounts WHERE id = ?')
69     or die $dbh->errstr;
70   $esth->execute($phonenum) or die $esth->errstr;
71   my $row = $esth->fetchrow_hashref;
72
73   #die "$phonenum already exists in nibblebill db" if $row && $row->{'id'};
74   if ( $row && $row->{'id'} ) {
75
76     nibblebill_adjust_cash($datasrc, $username, $password, $phonenum, $amount);
77
78   } else {
79
80     my $sth = $dbh->prepare(
81         'INSERT INTO accounts (id, name, cash) VALUES (?, ?, ?)'
82       ) or die $dbh->errsrr;
83     $sth->execute($phonenum, $phonenum, $amount) or die $sth->errstr;
84  
85   }
86 }
87
88 sub _export_replace {
89   my( $self, $new, $old ) = ( shift, shift, shift );
90
91   #XXX change phonenum in db?
92
93   '';
94 }
95
96 sub _export_delete {
97   my( $self, $svc_phone) = @_;
98
99   #XXX delete the phonenum in db, suck back any unused credit and make a credit?
100
101   ''
102 }
103
104 sub _adjust {
105   my( $self, $svc_phone, $amount ) = @_;
106
107   my $queue = new FS::queue {
108     svcnum => $svcnum,
109     job    => 'FS::part_export::freeswitch_nibblebill::nibblebill_adjust_cash',
110   };
111   $queue->insert(
112     $self->option('datasrc'),
113     $self->option('username'),
114     $self->option('password'),
115     $svc_phone->phonenum,
116     $amount,
117   ) or $queue;
118 }
119
120 sub nibblebill_adjust_cash {
121   my($datasrc, $username, $password, $phonenum, $amount) = @_;
122   my $dbh = DBI->connect($datasrc, $username, $password) or die $DBI::errstr; 
123
124   my $sth = $dbh->prepare('UPDATE accounts SET cash = cash + ? WHERE id = ?')
125     or die $dbh->errsrr;
126   $sth->execute($amount, $phonenum) or die $sth->errstr;
127 }
128
129 sub export_getstatus {                                                          
130   my( $self, $svc_phone, $htmlref, $hashref ) = @_;             
131
132   my $dbh = DBI->connect( map $self->option($_), qw( datasrc username password ) )
133     or return $DBI::errstr; 
134
135   my $sth = $dbh->prepare('SELECT cash FROM accounts WHERE id = ?')
136     or return $dbh->errstr;
137   $sth->execute($svc_phone->phonenum) or return $sth->errstr;
138   my $row = $sth->fetchrow_hashref or return '';
139
140   $hashref->{'Balance'} = $row->{'cash'};
141
142   '';
143
144 }
145
146 1;