From 4d81e21107622f8731a6301c3c811108840582fd Mon Sep 17 00:00:00 2001 From: Ivan Kohler Date: Mon, 22 Jul 2013 11:52:22 -0700 Subject: [PATCH] nibblebill integrateion, RT#19587 --- FS/FS/part_export/freeswitch.pm | 5 + FS/FS/part_export/freeswitch_multifile.pm | 7 +- FS/FS/part_export/freeswitch_nibblebill.pm | 146 +++++++++++++++++++++++++++++ FS/FS/part_pkg/base_delayed.pm | 12 +++ FS/FS/part_pkg/base_rate.pm | 12 +++ FS/FS/part_pkg/prepaid_nibblebill.pm | 26 +++++ FS/FS/part_pkg/sqlradacct_daily.pm | 2 +- FS/FS/part_pkg/voip_cdr.pm | 2 +- httemplate/elements/tr-cust_svc.html | 11 ++- httemplate/view/svc_phone.cgi | 3 + 10 files changed, 222 insertions(+), 4 deletions(-) create mode 100644 FS/FS/part_export/freeswitch_nibblebill.pm create mode 100644 FS/FS/part_pkg/base_delayed.pm create mode 100644 FS/FS/part_pkg/base_rate.pm create mode 100644 FS/FS/part_pkg/prepaid_nibblebill.pm diff --git a/FS/FS/part_export/freeswitch.pm b/FS/FS/part_export/freeswitch.pm index ff0d243bb..291130fde 100644 --- a/FS/FS/part_export/freeswitch.pm +++ b/FS/FS/part_export/freeswitch.pm @@ -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 ); diff --git a/FS/FS/part_export/freeswitch_multifile.pm b/FS/FS/part_export/freeswitch_multifile.pm index 7f79a0e68..972ea24e1 100644 --- a/FS/FS/part_export/freeswitch_multifile.pm +++ b/FS/FS/part_export/freeswitch_multifile.pm @@ -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 index 000000000..6611f7827 --- /dev/null +++ b/FS/FS/part_export/freeswitch_nibblebill.pm @@ -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 Prepaid credit in FreeSWITCH mod_nibblebill price plan. +

+ See the +DBI documentation +and the +documentation for your DBD +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 index 000000000..550681115 --- /dev/null +++ b/FS/FS/part_pkg/base_delayed.pm @@ -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 index 000000000..7453f98d3 --- /dev/null +++ b/FS/FS/part_pkg/base_rate.pm @@ -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 index 000000000..338bcf468 --- /dev/null +++ b/FS/FS/part_pkg/prepaid_nibblebill.pm @@ -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; + diff --git a/FS/FS/part_pkg/sqlradacct_daily.pm b/FS/FS/part_pkg/sqlradacct_daily.pm index 27fc1df3e..1a0bafe15 100644 --- a/FS/FS/part_pkg/sqlradacct_daily.pm +++ b/FS/FS/part_pkg/sqlradacct_daily.pm @@ -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 { diff --git a/FS/FS/part_pkg/voip_cdr.pm b/FS/FS/part_pkg/voip_cdr.pm index 1a9718641..4200f33bc 100644 --- a/FS/FS/part_pkg/voip_cdr.pm +++ b/FS/FS/part_pkg/voip_cdr.pm @@ -345,7 +345,7 @@ tie my %detail_formats, 'Tie::IxHash', count_available_phones suspend_bill ) ], - 'weight' => 40, + 'weight' => 41, ); sub price_info { diff --git a/httemplate/elements/tr-cust_svc.html b/httemplate/elements/tr-cust_svc.html index 3710b27ff..cc5ec0f50 100644 --- a/httemplate/elements/tr-cust_svc.html +++ b/httemplate/elements/tr-cust_svc.html @@ -60,8 +60,17 @@ $cust_svc->overlimit ) % } -% # first column: recharge link +% # first column: (optional external balance and) recharge link +% if ( $part_svc->svcdb eq 'svc_phone' +% && ! $opt{no_links} +% ) +% { +% my( $html, $hashref ) = $svc_x->export_getstatus; +% if ( length($hashref->{'Balance'}) ) { #quelle hack + Balance: <% $hashref->{'Balance'} %>  +% } +% } % if ( $curuser->access_right('Recharge customer service') % && $part_svc->svcdb eq 'svc_acct' % && ! $opt{no_links} diff --git a/httemplate/view/svc_phone.cgi b/httemplate/view/svc_phone.cgi index ccd9ae7c7..2a2ef243b 100644 --- a/httemplate/view/svc_phone.cgi +++ b/httemplate/view/svc_phone.cgi @@ -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 ). '
'. join(' | ', @ilinks). '
'; -- 2.11.0