use strict;
use vars qw($DEBUG $me);
use FS::Record qw(qsearchs);
+use FS::Conf;
use FS::rate;
use FS::svc_phone;
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'};
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,
};
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;