nibblebill integrateion, RT#19587
authorIvan Kohler <ivan@freeside.biz>
Mon, 22 Jul 2013 18:52:22 +0000 (11:52 -0700)
committerIvan Kohler <ivan@freeside.biz>
Mon, 22 Jul 2013 18:52:22 +0000 (11:52 -0700)
FS/FS/part_export/freeswitch.pm
FS/FS/part_export/freeswitch_multifile.pm
FS/FS/part_export/freeswitch_nibblebill.pm [new file with mode: 0644]
FS/FS/part_pkg/base_delayed.pm [new file with mode: 0644]
FS/FS/part_pkg/base_rate.pm [new file with mode: 0644]
FS/FS/part_pkg/prepaid_nibblebill.pm [new file with mode: 0644]
FS/FS/part_pkg/sqlradacct_daily.pm
FS/FS/part_pkg/voip_cdr.pm
httemplate/elements/tr-cust_svc.html
httemplate/view/svc_phone.cgi

index ff0d243..291130f 100644 (file)
@@ -136,8 +136,13 @@ sub freeswitch_template_fillin {
     DELIMITERS => [ '<%', '%>' ],
   );
 
+  my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
+  my $nibble_rate = $cust_pkg ? $cust_pkg->part_pkg->option('nibble_rate')
+                              : '';
+
   #false lazinessish w/phone_shellcommands::_export_command
   my %hash = (
+    'nibble_rate' => $nibble_rate,
     map { $_ => $svc_phone->getfield($_) } $svc_phone->fields
   );
 
index 7f79a0e..972ea24 100644 (file)
@@ -123,9 +123,14 @@ sub freeswitch_template_fillin {
              || $svc_phone->domain
              || '$${sip_profile}';
 
+  my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
+  my $nibble_rate = $cust_pkg ? $cust_pkg->part_pkg->option('nibble_rate')
+                              : '';
+
   #false lazinessish w/phone_shellcommands::_export_command
   my %hash = (
-    'domain' => $domain,
+    'domain'      => $domain,
+    'nibble_rate' => $nibble_rate,
     map { $_ => $svc_phone->getfield($_) } $svc_phone->fields
   );
 
diff --git a/FS/FS/part_export/freeswitch_nibblebill.pm b/FS/FS/part_export/freeswitch_nibblebill.pm
new file mode 100644 (file)
index 0000000..6611f78
--- /dev/null
@@ -0,0 +1,146 @@
+package FS::part_export::freeswitch_nibblebill;
+use base qw( FS::part_export );
+
+use vars qw( %info ); # $DEBUG );
+use Tie::IxHash;
+use 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 = DBI->connect($datasrc, $username, $password) or die $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 = DBI->connect($datasrc, $username, $password) or die $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 = DBI->connect( map $self->option($_), qw( datasrc username password ) )
+    or return $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;
diff --git a/FS/FS/part_pkg/base_delayed.pm b/FS/FS/part_pkg/base_delayed.pm
new file mode 100644 (file)
index 0000000..5506811
--- /dev/null
@@ -0,0 +1,12 @@
+package FS::part_pkg::base_delayed;
+
+use strict;
+use vars qw( %info );
+
+#disabled stub, otherwise, the old files stick around and show up as price plans
+
+%info = ( 
+  'disabled'  => 1,
+);
+
+1;
diff --git a/FS/FS/part_pkg/base_rate.pm b/FS/FS/part_pkg/base_rate.pm
new file mode 100644 (file)
index 0000000..7453f98
--- /dev/null
@@ -0,0 +1,12 @@
+package FS::part_pkg::base_rate;
+
+use strict;
+use vars qw( %info );
+
+#disabled stub, otherwise, the old files stick around and show up as price plans
+
+%info = ( 
+  'disabled'  => 1,
+);
+
+1;
diff --git a/FS/FS/part_pkg/prepaid_nibblebill.pm b/FS/FS/part_pkg/prepaid_nibblebill.pm
new file mode 100644 (file)
index 0000000..338bcf4
--- /dev/null
@@ -0,0 +1,26 @@
+package FS::part_pkg::prepaid_nibblebill;
+use base qw( FS::part_pkg::flat );
+
+use strict;
+use vars qw(%info);
+
+%info = (
+  'name' => 'Prepaid credit in FreeSWITCH mod_nibblebill',
+  #'name' => 'Prepaid (no automatic recurring)', #maybe use it here too
+  'shortname' => 'Prepaid FreeSWITCH mod_nibblebill',
+  #'inherit_fields' => [ 'global_Mixin' ],
+  'fields' => {
+    'setup_fee'   => { 'default' => 0, },
+    'recur_fee'   => { 'default' => 0, },
+    'nibble_rate' => { 'name' => 'Nibble rate' },
+  },
+  'fieldorder' => [ qw( setup_fee recur_fee nibble_rate ) ],
+  'weight' => 49,
+);
+
+sub is_prepaid {
+  1;
+}
+
+1;
+
index 27fc1df..1a0bafe 100644 (file)
@@ -73,7 +73,7 @@ use Date::Format;
 
   },
   'fieldorder' => [qw( recur_included_hours recur_hourly_charge recur_hourly_cap recur_included_input recur_input_charge recur_input_cap recur_included_output recur_output_charge recur_output_cap recur_included_total recur_total_charge recur_total_cap global_cap monthly_cap )],
-  'weight' => 41,
+  'weight' => 40.1,
 );
 
 sub price_info {
index 1a97186..4200f33 100644 (file)
@@ -345,7 +345,7 @@ tie my %detail_formats, 'Tie::IxHash',
                        count_available_phones suspend_bill 
                      )
                   ],
-  'weight' => 40,
+  'weight' => 41,
 );
 
 sub price_info {
index 3710b27..cc5ec0f 100644 (file)
@@ -60,8 +60,17 @@ $cust_svc->overlimit )
 </TR>
 % }
 <TR>
-% # first column: recharge link
+% # first column: (optional external balance and) recharge link
   <TD ALIGN="right" VALIGN="top" STYLE="padding-bottom:5px; padding-top:0px">
+% if ( $part_svc->svcdb eq 'svc_phone'
+%        && ! $opt{no_links}
+%    )
+% {
+%   my( $html, $hashref ) = $svc_x->export_getstatus;
+%   if ( length($hashref->{'Balance'}) ) { #quelle hack
+      <FONT SIZE="-1">Balance:&nbsp;<b><% $hashref->{'Balance'} %></b>&nbsp;</FONT>
+%   }
+% }
 % if ( $curuser->access_right('Recharge customer service') 
 %     && $part_svc->svcdb eq 'svc_acct'
 %     && ! $opt{no_links}
index ccd9ae7..2a2ef24 100644 (file)
@@ -84,6 +84,8 @@ my $html_foot = sub {
                           'table' => 'phone_device',
                        );
 
+  my $status = include('/view/elements/svc_export_status.html', $svc_phone );
+
   ##
   # CDR links
   ##
@@ -137,6 +139,7 @@ my $html_foot = sub {
 
   $e911.
   $devices.
+  $status.
   join(' | ', @links ). '<BR>'.
   join(' | ', @ilinks). '<BR>';