1 package FS::ClientAPI::PrepaidPhone;
4 #use vars qw($DEBUG $me);
5 use FS::Record qw(qsearchs);
10 #$me = '[FS::ClientAPI::PrepaidPhone]';
13 # - shared-secret auth? (set a conf value)
15 =item call_time HASHREF
17 HASHREF contains the following parameters:
23 Source number (with countrycode)
27 Destination number (with countrycode)
31 Always returns a hashref. If there is an error, the hashref contains a single
32 "error" key with the error message as a value. Otherwise, returns a hashref
33 with the following keys:
39 Empty if no customer is found associated with the number, customer number
44 Number of seconds remaining for a call to destination number
53 my $src = $packet->{'src'};
54 my $dst = $packet->{'dst'};
57 #my $conf = new FS::Conf;
58 #if ( #XXX toll-free? collect?
60 #} else { #use the src to find the customer
64 my( $countrycode, $phonenum );
65 if ( $number #this is an interesting regex to parse out 1&2 digit countrycodes
66 =~ /^(2[078]|3[0-469]|4[013-9]|5[1-8]|6[0-6]|7|8[1-469]|9[0-58])(\d*)$/
67 || $number =~ /^(\d{3})(\d*)$/
73 return { 'error' => "unparsable number: $number" };
76 my $svc_phone = qsearchs('svc_phone', { 'countrycode' => $countrycode,
77 'phonenum' => $phonenum,
81 unless ( $svc_phone ) {
82 return { 'error' => "can't find customer for +$countrycode $phonenum" };
83 # return { 'custnum' => '',
89 my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
90 my $part_pkg = $cust_pkg->part_pkg;
91 my $cust_main = $cust_pkg->cust_main;
94 'custnum' => $cust_pkg->custnum,
95 #'balance' => $cust_pkg->cust_main->balance,
98 return \%return unless $part_pkg->plan eq 'voip_cdr'
99 && $part_pkg->option('rating_method') eq 'prefix';
101 my $rate = qsearchs('rate', { 'ratenum' => $part_pkg->option('ratenum') } );
103 #rate the call and arrive at a max # of seconds for the customer's balance
104 my $rate_detail = $rate->dest_detail({ 'countrycode' => $countrycode,
105 'phonenum' => $phonenum,
108 #XXX granularity? included minutes? another day...
110 $return{'seconds'} = int(60 * $cust_main->balance / $rate_detail->min_charge);
116 =item call_time_nanpa
118 Like I<call_time>, except countrycode 1 is not required, and all other
119 countrycodes must be prefixed with 011.
123 # - everything is assumed to be countrycode 1 unless it starts with 011(ccode)
124 sub call_time_nanpa {
127 foreach (qw( src dst )) {
128 if ( $packet->{$_} =~ /^011(\d+)/ ) {
130 } elsif ( $packet->{$_} !~ /^1/ ) {
131 $packet->{$_} = '1'.$packet->{$_};
139 =item phonenum_balance HASHREF
141 HASHREF contains the following parameters:
147 Optional countrycode. Defaults to 1.
155 Always returns a hashref. If there is an error, the hashref contains a single
156 "error" key with the error message as a value. Otherwise, returns a hashref
157 with the following keys:
163 Empty if no customer is found associated with the number, customer number
174 sub phonenum_balance {
177 my $svc_phone = qsearchs('svc_phone', {
178 'countrycode' => ( $packet->{'countrycode'} || 1 ),
179 'phonenum' => $packet->{'phonenum'},
182 unless ( $svc_phone ) {
183 return { 'custnum' => '',
188 my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
191 'custnum' => $cust_pkg->custnum,
192 'balance' => $cust_pkg->cust_main->balance,