stray closing /TABLE in the no-ticket case
[freeside.git] / FS / FS / ClientAPI / PrepaidPhone.pm
index c918dde..2cea3c2 100644 (file)
@@ -3,10 +3,11 @@ 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;
 
-$DEBUG = 1;
+$DEBUG = 0;
 $me = '[FS::ClientAPI::PrepaidPhone]';
 
 #TODO:
@@ -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'};
@@ -248,15 +251,72 @@ sub phonenum_balance {
 
   my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
 
-  warn "$me returning ". $cust_pkg->cust_main->balance.
-       " balance for custnum ". $cust_pkg->custnum
+  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;