X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FClientAPI%2FPrepaidPhone.pm;h=2cea3c23184be09747ea0d3e9e8556d318c2570a;hb=8b8d621a1bb4e9abaa1f0edab5190d302e6a6e99;hp=00bc0ffd07135c0e63c7590f4d933be897a67863;hpb=624b2d44625f69d71175c3348cae635d580c890b;p=freeside.git diff --git a/FS/FS/ClientAPI/PrepaidPhone.pm b/FS/FS/ClientAPI/PrepaidPhone.pm index 00bc0ffd0..2cea3c231 100644 --- a/FS/FS/ClientAPI/PrepaidPhone.pm +++ b/FS/FS/ClientAPI/PrepaidPhone.pm @@ -3,6 +3,7 @@ package FS::ClientAPI::PrepaidPhone; use strict; use vars qw($DEBUG $me); use FS::Record qw(qsearchs); +use FS::Conf; use FS::rate; use FS::svc_phone; @@ -156,11 +157,13 @@ sub call_time { return \%return; } + my $balance = FS::ClientAPI::PrepaidPhone->prepaid_phone_balance( $cust_pkg ); + #XXX granularity? included minutes? another day... - if ( $cust_main->balance >= 0 ) { + if ( $balance >= 0 ) { return { 'error'=>'No balance' }; } else { - $return{'seconds'} = int(60 * abs($cust_main->balance) / $rate_detail->min_charge); + $return{'seconds'} = int(60 * abs($balance) / $rate_detail->min_charge); } warn "$me returning seconds: ". $return{'seconds'}; @@ -230,12 +233,17 @@ Customer balance. sub phonenum_balance { my $packet = shift; + warn "$me phonenum_balance called with countrycode ".$packet->{'countrycode'}. + " and phonenum ". $packet->{'phonenum'}. "\n" + if $DEBUG; + my $svc_phone = qsearchs('svc_phone', { 'countrycode' => ( $packet->{'countrycode'} || 1 ), 'phonenum' => $packet->{'phonenum'}, }); unless ( $svc_phone ) { + warn "$me no phone number found\n" if $DEBUG; return { 'custnum' => '', 'balance' => 0, }; @@ -243,11 +251,72 @@ sub phonenum_balance { my $cust_pkg = $svc_phone->cust_svc->cust_pkg; + my $balance = FS::ClientAPI::PrepaidPhone->prepaid_phone_balance( $cust_pkg ); + + warn "$me returning $balance balance for pkgnum ". $cust_pkg->pkgnum. + ", custnum ". $cust_pkg->custnum + if $DEBUG; + return { 'custnum' => $cust_pkg->custnum, - 'balance' => $cust_pkg->cust_main->balance, + 'balance' => $balance, }; } +sub prepaid_phone_balance { + my $class = shift; # i guess + my ($cust_pkg) = @_; + + my $conf = new FS::Conf; + + my $pkg_balances = $conf->config_bool('pkg-balances'); + + my $balance = $pkg_balances ? $cust_pkg->balance + : $cust_pkg->cust_main->balance; + + if ( $conf->config_bool('cdr-prerate') ) { + my @cust_pkg = $pkg_balances ? ( $cust_pkg ) + : ( $cust_pkg->cust_main->ncancelled_pkgs ); + foreach my $cust_pkg ( @cust_pkg ) { + + #we only support prerated CDRs with "VOIP/telco CDR rating (standard)" + # and "Phone numbers (svc_phone.phonenum)" CDR service matching for now + my $part_pkg = $cust_pkg->part_pkg; + next unless $part_pkg->plan eq 'voip_cdr' + && ($part_pkg->option('cdr_svc_method') || 'svc_phone.phonenum') + eq 'svc_phone.phonenum' + && ! $part_pkg->option('bill_inactive_svcs'); + #XXX skip when there's included minutes + + #select prerated cdrs & subtract them from balance + + # false laziness w/ part_pkg/voip_cdr.pm sorta + + my %options = ( + 'disable_src' => $part_pkg->option('disable_src'), + 'default_prefix' => $part_pkg->option('default_prefix'), + 'cdrtypenum' => $part_pkg->option('use_cdrtypenum'), + 'calltypenum' => $part_pkg->option('use_calltypenum'), + 'status' => 'rated', + 'by_svcnum' => 1, + ); # $last_bill, $$sdate ) + + my @cust_svc = grep { $_->part_svc->svcdb eq 'svc_phone' } + $cust_pkg->cust_svc; + foreach my $cust_svc ( @cust_svc ) { + + my $svc_x = $cust_svc->svc_x; + my $sum_cdr = $svc_x->sum_cdrs(%options); + $balance += $sum_cdr->rated_price; + + } + + } + } + + $balance; + +} + 1;