+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;
+
+}
+