adding prepaid self-service hooks, RT#4100
[freeside.git] / FS / FS / ClientAPI / PrepaidPhone.pm
1 package FS::ClientAPI::PrepaidPhone;
2
3 use strict;
4 #use vars qw($DEBUG $me);
5 use FS::Record qw(qsearchs);
6 use FS::rate;
7 use FS::svc_phone;
8
9 #$DEBUG = 0;
10 #$me = '[FS::ClientAPI::PrepaidPhone]';
11
12 #TODO:
13 # - shared-secret auth? (set a conf value)
14
15 =item call_time HASHREF
16
17 HASHREF contains the following parameters:
18
19 =over 4
20
21 =item src
22
23 Source number (with countrycode)
24
25 =item dst
26
27 Destination number (with countrycode)
28
29 =back
30
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:
34
35 =over 4
36
37 =item custnum
38
39 Empty if no customer is found associated with the number, customer number
40 otherwise.
41
42 =item seconds
43
44 Number of seconds remaining for a call to destination number
45
46 =back
47
48 =cut
49
50 sub call_time {
51   my $packet = shift;
52
53   my $src = $packet->{'src'};
54   my $dst = $packet->{'dst'};
55
56   my $number;
57   #my $conf = new FS::Conf;
58   #if ( #XXX toll-free?  collect?
59   #  $phonenum = $dst;
60   #} else { #use the src to find the customer
61     $number = $src;
62   #}
63
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*)$/
68      )
69   {
70     $countrycode = $1;
71     $phonenum = $2;
72   } else { 
73     return { 'error' => "unparsable number: $number" };
74   }
75
76   my $svc_phone = qsearchs('svc_phone', { 'countrycode' => $countrycode,
77                                           'phonenum'    => $phonenum,
78                                         }
79                           );
80
81   unless ( $svc_phone ) {
82     return { 'error' => "can't find customer for +$countrycode $phonenum" };
83 #    return { 'custnum' => '',
84 #             'seconds' => 0,
85 #             #'balance' => 0,
86 #           };
87   };
88
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;
92
93   my %return = (
94     'custnum' => $cust_pkg->custnum,
95     #'balance' => $cust_pkg->cust_main->balance,
96   );
97
98   return \%return unless $part_pkg->plan eq 'voip_cdr'
99                       && $part_pkg->option('rating_method') eq 'prefix';
100
101   my $rate = qsearchs('rate', { 'ratenum' => $part_pkg->option('ratenum') } );
102
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,
106                                        });
107
108   #XXX granularity?  included minutes?  another day...
109
110   $return{'seconds'} = int(60 * $cust_main->balance / $rate_detail->min_charge);
111
112   return \%return;
113  
114 }
115
116 =item call_time_nanpa 
117
118 Like I<call_time>, except countrycode 1 is not required, and all other
119 countrycodes must be prefixed with 011.
120
121 =cut
122
123 # - everything is assumed to be countrycode 1 unless it starts with 011(ccode)
124 sub call_time_nanpa {
125   my $packet = shift;
126
127   foreach (qw( src dst )) {
128     if ( $packet->{$_} =~ /^011(\d+)/ ) {
129       $packet->{$_} = $1;
130     } elsif ( $packet->{$_} !~ /^1/ ) {
131       $packet->{$_} = '1'.$packet->{$_};
132     }
133   }
134
135   call_time($packet);
136
137 }
138
139 =item phonenum_balance HASHREF
140
141 HASHREF contains the following parameters:
142
143 =over 4
144
145 =item countrycode
146
147 Optional countrycode.  Defaults to 1.
148
149 =item phonenum
150
151 Phone number.
152
153 =back
154
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:
158
159 =over 4
160
161 =item custnum
162
163 Empty if no customer is found associated with the number, customer number
164 otherwise.
165
166 =item balance
167
168 Customer balance.
169
170 =back
171
172 =cut
173
174 sub phonenum_balance {
175   my $packet = shift;
176
177   my $svc_phone = qsearchs('svc_phone', {
178     'countrycode' => ( $packet->{'countrycode'} || 1 ),
179     'phonenum'    => $packet->{'phonenum'},
180   });
181
182   unless ( $svc_phone ) {
183     return { 'custnum' => '',
184              'balance' => 0,
185            };
186   };
187
188   my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
189
190   return {
191     'custnum' => $cust_pkg->custnum,
192     'balance' => $cust_pkg->cust_main->balance,
193   };
194
195 }
196
197 1;