summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/freeswitch_nibblebill.pm
blob: 921444596514fa9079fa2c5763fa43c47c11ae6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package FS::part_export::freeswitch_nibblebill;
use base qw( FS::part_export );

use vars qw( %info ); # $DEBUG );
use Tie::IxHash;
use FS::DBI;
#use FS::Record qw( qsearch ); #qsearchs );
#use FS::svc_phone;
#use FS::Schema qw( dbdef );

#$DEBUG = 1;

tie my %options, 'Tie::IxHash',
  'datasrc'  => { label=>'DBI data source ' },                                  
  'username' => { label=>'Database username' },                                 
  'password' => { label=>'Database password' },   
;

%info = (
  'svc'     => 'svc_phone',
  'desc'    => 'Provision prepaid credit to a FreeSWITCH mod_nibblebill database',
  'options' => \%options,
  'notes'   => <<'END',
Provision prepaid credit to a FreeSWITCH mod_nibblebill database.  Use with the <b>Prepaid credit in FreeSWITCH mod_nibblebill</b> price plan.
<br><br>
 See the                                                     
<a href="http://search.cpan.org/dist/DBI/DBI.pm#connect">DBI documentation</a>  
and the                                                                         
<a href="http://search.cpan.org/search?mode=module&query=DBD%3A%3A">documentation for your DBD</a>
for the exact syntax of a DBI data source.
END
);

sub rebless { shift; }

sub _export_insert {
  my( $self, $svc_phone ) = ( shift, shift );

  #add phonenum to db (unless it is there already)

  # w/the setup amount makes the most sense in this usage (rather than the
  #  (balance/pkg-balance), since you would order the package, then provision
  #   the phone number.
  my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
  my $amount = $cust_pkg ? $cust_pkg->part_pkg->option('setup_fee')
                         : '';

  my $queue = new FS::queue {
    svcnum => $svcnum,
    job    => 'FS::part_export::freeswitch_nibblebill::nibblebill_insert',
  };
  $queue->insert(
    $self->option('datasrc'),
    $self->option('username'),
    $self->option('password'),
    $svc_phone->phonenum,
    $amount,
  );

}

sub nibblebill_insert {
  my($datasrc, $username, $password, $phonenum, $amount) = @_;
  my $dbh = FS::DBI->connect($datasrc, $username, $password)
    or die $FS::DBI::errstr; 

  #check for existing account
  $dbh->{FetchHashKeyName} = 'NAME_lc';
  my $esth = $dbh->prepare('SELECT id, name, cash FROM accounts WHERE id = ?')
    or die $dbh->errstr;
  $esth->execute($phonenum) or die $esth->errstr;
  my $row = $esth->fetchrow_hashref;

  #die "$phonenum already exists in nibblebill db" if $row && $row->{'id'};
  if ( $row && $row->{'id'} ) {

    nibblebill_adjust_cash($datasrc, $username, $password, $phonenum, $amount);

  } else {

    my $sth = $dbh->prepare(
        'INSERT INTO accounts (id, name, cash) VALUES (?, ?, ?)'
      ) or die $dbh->errsrr;
    $sth->execute($phonenum, $phonenum, $amount) or die $sth->errstr;
 
  }
}

sub _export_replace {
  my( $self, $new, $old ) = ( shift, shift, shift );

  #XXX change phonenum in db?

  '';
}

sub _export_delete {
  my( $self, $svc_phone) = @_;

  #XXX delete the phonenum in db, suck back any unused credit and make a credit?

  ''
}

sub _adjust {
  my( $self, $svc_phone, $amount ) = @_;

  my $queue = new FS::queue {
    svcnum => $svcnum,
    job    => 'FS::part_export::freeswitch_nibblebill::nibblebill_adjust_cash',
  };
  $queue->insert(
    $self->option('datasrc'),
    $self->option('username'),
    $self->option('password'),
    $svc_phone->phonenum,
    $amount,
  ) or $queue;
}

sub nibblebill_adjust_cash {
  my($datasrc, $username, $password, $phonenum, $amount) = @_;
  my $dbh = FS::DBI->connect($datasrc, $username, $password)
    or die $FS::DBI::errstr; 

  my $sth = $dbh->prepare('UPDATE accounts SET cash = cash + ? WHERE id = ?')
    or die $dbh->errsrr;
  $sth->execute($amount, $phonenum) or die $sth->errstr;
}

sub export_getstatus {                                                          
  my( $self, $svc_phone, $htmlref, $hashref ) = @_;             

  my $dbh = FS::DBI->connect( map $self->option($_), qw( datasrc username password ) )
    or return $FS::DBI::errstr; 

  my $sth = $dbh->prepare('SELECT cash FROM accounts WHERE id = ?')
    or return $dbh->errstr;
  $sth->execute($svc_phone->phonenum) or return $sth->errstr;
  my $row = $sth->fetchrow_hashref or return '';

  $hashref->{'Balance'} = $row->{'cash'};

  '';

}

1;