Merge branch 'patch-1' of https://github.com/gjones2/Freeside
[freeside.git] / FS / FS / ClientAPI / PrepaidPhone.pm
index b0c69f9..c7317ea 100644 (file)
@@ -1,13 +1,14 @@
 package FS::ClientAPI::PrepaidPhone;
 
 use strict;
-#use vars qw($DEBUG $me);
+use vars qw($DEBUG $me);
 use FS::Record qw(qsearchs);
+use FS::Conf;
 use FS::rate;
 use FS::svc_phone;
 
-#$DEBUG = 0;
-#$me = '[FS::ClientAPI::PrepaidPhone]';
+$DEBUG = 0;
+$me = '[FS::ClientAPI::PrepaidPhone]';
 
 #TODO:
 # - shared-secret auth? (set a conf value)
@@ -96,6 +97,9 @@ sub call_time {
   my @part_pkg = ( $part_pkg, map $_->dst_pkg, $part_pkg->bill_part_pkg_link );
   #XXX uuh, behavior indeterminate if you have more than one voip_cdr+prefix
   #add-on, i guess.
+  warn "$me ". scalar(@part_pkg). ': '.
+       join('/', map { $_->plan. $_->option('rating_method') } @part_pkg )
+    if $DEBUG;
   @part_pkg =
     grep { $_->plan eq 'voip_cdr' && $_->option('rating_method') eq 'prefix' }
          @part_pkg;
@@ -105,10 +109,26 @@ sub call_time {
     #'balance' => $cust_pkg->cust_main->balance,
   );
 
+  warn "$me: ". scalar(@part_pkg). ': '.
+       join('/', map { $_->plan. $_->option('rating_method') } @part_pkg )
+    if $DEBUG;
   return \%return unless @part_pkg;
 
+  warn "$me searching for rate ". $part_pkg[0]->option('ratenum')
+    if $DEBUG;
+
   my $rate = qsearchs('rate', { 'ratenum'=>$part_pkg[0]->option('ratenum') } );
 
+  unless ( $rate ) {
+    my $error = 'ratenum '. $part_pkg[0]->option('ratenum'). ' not found';
+    warn "$me $error"
+      if $DEBUG;
+    return { 'error'=>$error };
+  }
+
+  warn "$me found rate ". $rate->ratenum
+    if $DEBUG;
+
   #rate the call and arrive at a max # of seconds for the customer's balance
 
   my( $rate_countrycode, $rate_phonenum );
@@ -133,12 +153,22 @@ sub call_time {
   unless ( $rate_detail->min_charge > 0 ) {
     #XXX no charge??  return lots of seconds, a default, 0 or what?
     #return { 'error' => '0 rate for +$rate_countrycode $rate_phonenum; prepaid service not available" };
-    $return{'seconds'} = 1800; #half hour?!
+    #customer wants no default for now# $return{'seconds'} = 1800; #half hour?!
     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...
-  $return{'seconds'} = int(60 * $cust_main->balance / $rate_detail->min_charge);
+  if ( $balance >= 0 ) {
+    return { 'error'=>'No balance' };
+  } else {
+    $return{'seconds'} = int(60 * abs($balance) / $rate_detail->min_charge);
+  }
+
+  warn "$me returning seconds: ". $return{'seconds'};
 
   return \%return;
  
@@ -205,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,
            };
@@ -218,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,
   };
 
 }