Merge branch 'patch-1' of https://github.com/gjones2/Freeside
[freeside.git] / FS / FS / ClientAPI / PrepaidPhone.pm
index 00bc0ff..c7317ea 100644 (file)
@@ -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,15 @@ sub call_time {
     return \%return;
   }
 
+  my $conf = new FS::Conf;
+  my $balance = $conf->config_bool('pkg-balances') ? $cust_pkg->balance
+                                                   : $cust_main->balance;
+
   #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 +235,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,9 +253,18 @@ sub phonenum_balance {
 
   my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
 
+  my $conf = new FS::Conf;
+  my $balance = $conf->config_bool('pkg-balances')
+    ? $cust_pkg->balance
+    : $cust_pkg->cust_main->balance;
+
+  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,
   };
 
 }