phone devices (for netsapiens integration), RT#5226
[freeside.git] / FS / FS / svc_phone.pm
1 package FS::svc_phone;
2
3 use strict;
4 use vars qw( @ISA @pw_set $conf );
5 use FS::Conf;
6 use FS::Record qw( qsearch qsearchs );
7 use FS::Msgcat qw(gettext);
8 use FS::svc_Common;
9 use FS::part_svc;
10 use FS::phone_device;
11
12 @ISA = qw( FS::svc_Common );
13
14 #avoid l 1 and o O 0
15 @pw_set = ( 'a'..'k', 'm','n', 'p-z', 'A'..'N', 'P'..'Z' , '2'..'9' );
16
17 #ask FS::UID to run this stuff for us later
18 $FS::UID::callback{'FS::svc_acct'} = sub { 
19   $conf = new FS::Conf;
20 };
21
22 =head1 NAME
23
24 FS::svc_phone - Object methods for svc_phone records
25
26 =head1 SYNOPSIS
27
28   use FS::svc_phone;
29
30   $record = new FS::svc_phone \%hash;
31   $record = new FS::svc_phone { 'column' => 'value' };
32
33   $error = $record->insert;
34
35   $error = $new_record->replace($old_record);
36
37   $error = $record->delete;
38
39   $error = $record->check;
40
41   $error = $record->suspend;
42
43   $error = $record->unsuspend;
44
45   $error = $record->cancel;
46
47 =head1 DESCRIPTION
48
49 An FS::svc_phone object represents a phone number.  FS::svc_phone inherits
50 from FS::Record.  The following fields are currently supported:
51
52 =over 4
53
54 =item svcnum
55
56 primary key
57
58 =item countrycode
59
60 =item phonenum
61
62 =item sip_password
63
64 =item pin
65
66 Voicemail PIN
67
68 =item phone_name
69
70 =back
71
72 =head1 METHODS
73
74 =over 4
75
76 =item new HASHREF
77
78 Creates a new phone number.  To add the number to the database, see L<"insert">.
79
80 Note that this stores the hash reference, not a distinct copy of the hash it
81 points to.  You can ask the object for a copy with the I<hash> method.
82
83 =cut
84
85 # the new method can be inherited from FS::Record, if a table method is defined
86 #
87 sub table_info {
88   {
89     'name' => 'Phone number',
90     'sorts' => 'phonenum',
91     'display_weight' => 60,
92     'cancel_weight'  => 80,
93     'fields' => {
94         'countrycode'  => { label => 'Country code',
95                             type  => 'text',
96                             disable_inventory => 1,
97                             disable_select => 1,
98                           },
99         'phonenum'     => 'Phone number',
100         'pin'          => { label => 'Personal Identification Number',
101                             type  => 'text',
102                             disable_inventory => 1,
103                             disable_select => 1,
104                           },
105         'sip_password' => 'SIP password',
106         'phone_name'   => 'Name',
107     },
108   };
109 }
110
111 sub table { 'svc_phone'; }
112
113 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
114
115 =item search_sql STRING
116
117 Class method which returns an SQL fragment to search for the given string.
118
119 =cut
120
121 sub search_sql {
122   my( $class, $string ) = @_;
123   $class->search_sql_field('phonenum', $string );
124 }
125
126 =item label
127
128 Returns the phone number.
129
130 =cut
131
132 sub label {
133   my $self = shift;
134   my $phonenum = $self->phonenum; #XXX format it better
135   my $label = $phonenum;
136   $label .= ' ('.$self->phone_name.')' if $self->phone_name;
137   $label;
138 }
139
140 =item insert
141
142 Adds this record to the database.  If there is an error, returns the error,
143 otherwise returns false.
144
145 =cut
146
147 # the insert method can be inherited from FS::Record
148
149 =item delete
150
151 Delete this record from the database.
152
153 =cut
154
155 # the delete method can be inherited from FS::Record
156
157 =item replace OLD_RECORD
158
159 Replaces the OLD_RECORD with this one in the database.  If there is an error,
160 returns the error, otherwise returns false.
161
162 =cut
163
164 # the replace method can be inherited from FS::Record
165
166 =item suspend
167
168 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
169
170 =item unsuspend
171
172 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
173
174 =item cancel
175
176 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
177
178 =item check
179
180 Checks all fields to make sure this is a valid phone number.  If there is
181 an error, returns the error, otherwise returns false.  Called by the insert
182 and replace methods.
183
184 =cut
185
186 # the check method should currently be supplied - FS::Record contains some
187 # data checking routines
188
189 sub check {
190   my $self = shift;
191
192   my $conf = new FS::Conf;
193
194   my $phonenum = $self->phonenum;
195   my $phonenum_check_method;
196   if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
197     $phonenum =~ s/\W//g;
198     $phonenum_check_method = 'ut_alpha';
199   } else {
200     $phonenum =~ s/\D//g;
201     $phonenum_check_method = 'ut_number';
202   }
203   $self->phonenum($phonenum);
204
205   my $error = 
206     $self->ut_numbern('svcnum')
207     || $self->ut_numbern('countrycode')
208     || $self->$phonenum_check_method('phonenum')
209     || $self->ut_anything('sip_password')
210     || $self->ut_numbern('pin')
211     || $self->ut_textn('phone_name')
212   ;
213   return $error if $error;
214
215   $self->countrycode(1) unless $self->countrycode;
216
217   unless ( length($self->sip_password) ) {
218
219     $self->sip_password(
220       join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
221     );
222
223   }
224
225   $self->SUPER::check;
226 }
227
228 =item _check duplicate
229
230 Internal method to check for duplicate phone numers.
231
232 =cut
233
234 #false laziness w/svc_acct.pm's _check_duplicate.
235 sub _check_duplicate {
236   my $self = shift;
237
238   my $global_unique = $conf->config('global_unique-phonenum') || 'none';
239   return '' if $global_unique eq 'disabled';
240
241   $self->lock_table;
242
243   my @dup_ccphonenum =
244     grep { !$self->svcnum || $_->svcnum != $self->svcnum }
245     qsearch( 'svc_phone', {
246       'countrycode' => $self->countrycode,
247       'phonenum'    => $self->phonenum,
248     });
249
250   return gettext('phonenum_in_use')
251     if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
252
253   my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
254   unless ( $part_svc ) {
255     return 'unknown svcpart '. $self->svcpart;
256   }
257
258   if ( @dup_ccphonenum ) {
259
260     my $exports = FS::part_export::export_info('svc_phone');
261     my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
262
263     foreach my $part_export ( $part_svc->part_export ) {
264
265       #this will catch to the same exact export
266       my @svcparts = map { $_->svcpart } $part_export->export_svc;
267
268       $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
269         foreach @svcparts;
270
271     }
272
273     foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
274       my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
275       if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
276         return "duplicate phone number ".
277                $self->countrycode. ' '. $self->phonenum.
278                ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
279                " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
280       }
281     }
282
283   }
284
285   return '';
286
287 }
288
289 =item check_pin
290
291 Checks the supplied PIN against the PIN in the database.  Returns true for a
292 sucessful authentication, false if no match.
293
294 =cut
295
296 sub check_pin {
297   my($self, $check_pin) = @_;
298   length($self->pin) && $check_pin eq $self->pin;
299 }
300
301 =item radius_reply
302
303 =cut
304
305 sub radius_reply {
306   my $self = shift;
307   #XXX Session-Timeout!  holy shit, need rlm_perl to ask for this in realtime
308   ();
309 }
310
311 =item radius_check
312
313 =cut
314
315 sub radius_check {
316   my $self = shift;
317   my %check = ();
318
319   my $conf = new FS::Conf;
320
321   $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
322
323   %check;
324 }
325
326 sub radius_groups {
327   ();
328 }
329
330 =item phone_device
331
332 Returns any FS::phone_device records associated with this service.
333
334 =cut
335
336 sub phone_device {
337   my $self = shift;
338   qsearch('phone_device', { 'svcnum' => $self->svcnum } );
339 }
340
341 =back
342
343 =head1 BUGS
344
345 =head1 SEE ALSO
346
347 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
348 L<FS::cust_pkg>, schema.html from the base documentation.
349
350 =cut
351
352 1;
353