eliminate some false laziness in FS::Misc::send_email vs. msg_template/email.pm send_...
[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 FS::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 = FS::DBI->connect($datasrc, $username, $password)
65     or die $FS::DBI::errstr; 
66
67   #check for existing account
68   $dbh->{FetchHashKeyName} = 'NAME_lc';
69   my $esth = $dbh->prepare('SELECT id, name, cash FROM accounts WHERE id = ?')
70     or die $dbh->errstr;
71   $esth->execute($phonenum) or die $esth->errstr;
72   my $row = $esth->fetchrow_hashref;
73
74   #die "$phonenum already exists in nibblebill db" if $row && $row->{'id'};
75   if ( $row && $row->{'id'} ) {
76
77     nibblebill_adjust_cash($datasrc, $username, $password, $phonenum, $amount);
78
79   } else {
80
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;
85  
86   }
87 }
88
89 sub _export_replace {
90   my( $self, $new, $old ) = ( shift, shift, shift );
91
92   #XXX change phonenum in db?
93
94   '';
95 }
96
97 sub _export_delete {
98   my( $self, $svc_phone) = @_;
99
100   #XXX delete the phonenum in db, suck back any unused credit and make a credit?
101
102   ''
103 }
104
105 sub _adjust {
106   my( $self, $svc_phone, $amount ) = @_;
107
108   my $queue = new FS::queue {
109     svcnum => $svcnum,
110     job    => 'FS::part_export::freeswitch_nibblebill::nibblebill_adjust_cash',
111   };
112   $queue->insert(
113     $self->option('datasrc'),
114     $self->option('username'),
115     $self->option('password'),
116     $svc_phone->phonenum,
117     $amount,
118   ) or $queue;
119 }
120
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; 
125
126   my $sth = $dbh->prepare('UPDATE accounts SET cash = cash + ? WHERE id = ?')
127     or die $dbh->errsrr;
128   $sth->execute($amount, $phonenum) or die $sth->errstr;
129 }
130
131 sub export_getstatus {                                                          
132   my( $self, $svc_phone, $htmlref, $hashref ) = @_;             
133
134   my $dbh = FS::DBI->connect( map $self->option($_), qw( datasrc username password ) )
135     or return $FS::DBI::errstr; 
136
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 '';
141
142   $hashref->{'Balance'} = $row->{'cash'};
143
144   '';
145
146 }
147
148 1;