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 { 'custnum' => '',
88 my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
89 my $part_pkg = $cust_pkg->part_pkg;
90 my $cust_main = $cust_pkg->cust_main;
93 'custnum' => $cust_pkg->custnum,
94 #'balance' => $cust_pkg->cust_main->balance,
97 return \%return unless $part_pkg->plan eq 'voip_cdr'
98 && $part_pkg->option('rating_method') eq 'prefix';
100 my $rate = qsearchs('rate', { 'ratenum' => $part_pkg->option('ratenum') } );
102 #rate the call and arrive at a max # of seconds for the customer's balance
103 my $rate_detail = $rate->dest_detail({ 'countrycode' => $countrycode,
104 'phonenum' => $phonenum,
107 #XXX granularity? included minutes? another day...
109 $return{'seconds'} = int(60 * $cust_main->balance / $rate_detail->min_charge);
115 =item call_time_nanpa
117 Like I<call_time>, except countrycode 1 is not required, and all other
118 countrycodes must be prefixed with 011.
122 # - everything is assumed to be countrycode 1 unless it starts with 011(ccode)
123 sub call_time_nanpa {
126 foreach (qw( src dst )) {
127 if ( $packet->{$_} =~ /^011(\d+)/ ) {
129 } elsif ( $packet->{$_} !~ /^1/ ) {
130 $packet->{$_} = '1'.$packet->{$_};
138 =item phonenum_balance HASHREF
140 HASHREF contains the following parameters:
146 Optional countrycode. Defaults to 1.
154 Always returns a hashref. If there is an error, the hashref contains a single
155 "error" key with the error message as a value. Otherwise, returns a hashref
156 with the following keys:
162 Empty if no customer is found associated with the number, customer number
173 sub phonenum_balance {
176 my $svc_phone = qsearchs('svc_phone', {
177 'countrycode' => ( $packet->{'countrycode'} || 1 ),
178 'phonenum' => $packet->{'phonenum'},
181 unless ( $svc_phone ) {
182 return { 'custnum' => '',
187 my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
190 'custnum' => $cust_pkg->custnum,
191 'balance' => $cust_pkg->cust_main->balance,