1 package FS::part_export::freeswitch_nibblebill;
2 use base qw( FS::part_export );
4 use vars qw( %info ); # $DEBUG );
7 #use FS::Record qw( qsearch ); #qsearchs );
9 #use FS::Schema qw( dbdef );
13 tie my %options, 'Tie::IxHash',
14 'datasrc' => { label=>'DBI data source ' },
15 'username' => { label=>'Database username' },
16 'password' => { label=>'Database password' },
21 'desc' => 'Provision prepaid credit to a FreeSWITCH mod_nibblebill database',
22 'options' => \%options,
24 Provision prepaid credit to a FreeSWITCH mod_nibblebill database. Use with the <b>Prepaid credit in FreeSWITCH mod_nibblebill</b> price plan.
27 <a href="http://search.cpan.org/dist/DBI/DBI.pm#connect">DBI documentation</a>
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.
34 sub rebless { shift; }
37 my( $self, $svc_phone ) = ( shift, shift );
39 #add phonenum to db (unless it is there already)
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
44 my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
45 my $amount = $cust_pkg ? $cust_pkg->part_pkg->option('setup_fee')
48 my $queue = new FS::queue {
50 job => 'FS::part_export::freeswitch_nibblebill::nibblebill_insert',
53 $self->option('datasrc'),
54 $self->option('username'),
55 $self->option('password'),
62 sub nibblebill_insert {
63 my($datasrc, $username, $password, $phonenum, $amount) = @_;
64 my $dbh = DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
66 #check for existing account
67 $dbh->{FetchHashKeyName} = 'NAME_lc';
68 my $esth = $dbh->prepare('SELECT id, name, cash FROM accounts WHERE id = ?')
70 $esth->execute($phonenum) or die $esth->errstr;
71 my $row = $esth->fetchrow_hashref;
73 #die "$phonenum already exists in nibblebill db" if $row && $row->{'id'};
74 if ( $row && $row->{'id'} ) {
76 nibblebill_adjust_cash($datasrc, $username, $password, $phonenum, $amount);
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;
89 my( $self, $new, $old ) = ( shift, shift, shift );
91 #XXX change phonenum in db?
97 my( $self, $svc_phone) = @_;
99 #XXX delete the phonenum in db, suck back any unused credit and make a credit?
105 my( $self, $svc_phone, $amount ) = @_;
107 my $queue = new FS::queue {
109 job => 'FS::part_export::freeswitch_nibblebill::nibblebill_adjust_cash',
112 $self->option('datasrc'),
113 $self->option('username'),
114 $self->option('password'),
115 $svc_phone->phonenum,
120 sub nibblebill_adjust_cash {
121 my($datasrc, $username, $password, $phonenum, $amount) = @_;
122 my $dbh = DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
124 my $sth = $dbh->prepare('UPDATE accounts SET cash = cash + ? WHERE id = ?')
126 $sth->execute($amount, $phonenum) or die $sth->errstr;
129 sub export_getstatus {
130 my( $self, $svc_phone, $htmlref, $hashref ) = @_;
132 my $dbh = DBI->connect( map $self->option($_), qw( datasrc username password ) )
133 or return $DBI::errstr;
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 '';
140 $hashref->{'Balance'} = $row->{'cash'};