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 = FS::DBI->connect($datasrc, $username, $password)
65 or die $FS::DBI::errstr;
67 #check for existing account
68 $dbh->{FetchHashKeyName} = 'NAME_lc';
69 my $esth = $dbh->prepare('SELECT id, name, cash FROM accounts WHERE id = ?')
71 $esth->execute($phonenum) or die $esth->errstr;
72 my $row = $esth->fetchrow_hashref;
74 #die "$phonenum already exists in nibblebill db" if $row && $row->{'id'};
75 if ( $row && $row->{'id'} ) {
77 nibblebill_adjust_cash($datasrc, $username, $password, $phonenum, $amount);
81 my $sth = $dbh->prepare(
82 'INSERT INTO accounts (id, name, cash) VALUES (?, ?, ?)'
83 ) or die $dbh->errsrr;
84 $sth->execute($phonenum, $phonenum, $amount) or die $sth->errstr;
90 my( $self, $new, $old ) = ( shift, shift, shift );
92 #XXX change phonenum in db?
98 my( $self, $svc_phone) = @_;
100 #XXX delete the phonenum in db, suck back any unused credit and make a credit?
106 my( $self, $svc_phone, $amount ) = @_;
108 my $queue = new FS::queue {
110 job => 'FS::part_export::freeswitch_nibblebill::nibblebill_adjust_cash',
113 $self->option('datasrc'),
114 $self->option('username'),
115 $self->option('password'),
116 $svc_phone->phonenum,
121 sub nibblebill_adjust_cash {
122 my($datasrc, $username, $password, $phonenum, $amount) = @_;
123 my $dbh = FS::DBI->connect($datasrc, $username, $password)
124 or die $FS::DBI::errstr;
126 my $sth = $dbh->prepare('UPDATE accounts SET cash = cash + ? WHERE id = ?')
128 $sth->execute($amount, $phonenum) or die $sth->errstr;
131 sub export_getstatus {
132 my( $self, $svc_phone, $htmlref, $hashref ) = @_;
134 my $dbh = FS::DBI->connect( map $self->option($_), qw( datasrc username password ) )
135 or return $FS::DBI::errstr;
137 my $sth = $dbh->prepare('SELECT cash FROM accounts WHERE id = ?')
138 or return $dbh->errstr;
139 $sth->execute($svc_phone->phonenum) or return $sth->errstr;
140 my $row = $sth->fetchrow_hashref or return '';
142 $hashref->{'Balance'} = $row->{'cash'};