This commit was generated by cvs2svn to compensate for changes in r8690,
[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 dbh );
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 sub delete {
156   my $self = shift;
157
158   local $SIG{HUP} = 'IGNORE';
159   local $SIG{INT} = 'IGNORE';
160   local $SIG{QUIT} = 'IGNORE';
161   local $SIG{TERM} = 'IGNORE';
162   local $SIG{TSTP} = 'IGNORE';
163   local $SIG{PIPE} = 'IGNORE';
164
165   my $oldAutoCommit = $FS::UID::AutoCommit;
166   local $FS::UID::AutoCommit = 0;
167   my $dbh = dbh;
168
169   foreach my $phone_device ( $self->phone_device ) {
170     my $error = $phone_device->delete;
171     if ( $error ) {
172       $dbh->rollback if $oldAutoCommit;
173       return $error;
174     }
175   }
176
177   my $error = $self->SUPER::delete;
178   if ( $error ) {
179     $dbh->rollback if $oldAutoCommit;
180     return $error;
181   }
182
183   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
184   '';
185
186 }
187
188 # the delete method can be inherited from FS::Record
189
190 =item replace OLD_RECORD
191
192 Replaces the OLD_RECORD with this one in the database.  If there is an error,
193 returns the error, otherwise returns false.
194
195 =cut
196
197 # the replace method can be inherited from FS::Record
198
199 =item suspend
200
201 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
202
203 =item unsuspend
204
205 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
206
207 =item cancel
208
209 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
210
211 =item check
212
213 Checks all fields to make sure this is a valid phone number.  If there is
214 an error, returns the error, otherwise returns false.  Called by the insert
215 and replace methods.
216
217 =cut
218
219 # the check method should currently be supplied - FS::Record contains some
220 # data checking routines
221
222 sub check {
223   my $self = shift;
224
225   my $conf = new FS::Conf;
226
227   my $phonenum = $self->phonenum;
228   my $phonenum_check_method;
229   if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
230     $phonenum =~ s/\W//g;
231     $phonenum_check_method = 'ut_alpha';
232   } else {
233     $phonenum =~ s/\D//g;
234     $phonenum_check_method = 'ut_number';
235   }
236   $self->phonenum($phonenum);
237
238   my $error = 
239     $self->ut_numbern('svcnum')
240     || $self->ut_numbern('countrycode')
241     || $self->$phonenum_check_method('phonenum')
242     || $self->ut_anything('sip_password')
243     || $self->ut_numbern('pin')
244     || $self->ut_textn('phone_name')
245   ;
246   return $error if $error;
247
248   $self->countrycode(1) unless $self->countrycode;
249
250   unless ( length($self->sip_password) ) {
251
252     $self->sip_password(
253       join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
254     );
255
256   }
257
258   $self->SUPER::check;
259 }
260
261 =item _check duplicate
262
263 Internal method to check for duplicate phone numers.
264
265 =cut
266
267 #false laziness w/svc_acct.pm's _check_duplicate.
268 sub _check_duplicate {
269   my $self = shift;
270
271   my $global_unique = $conf->config('global_unique-phonenum') || 'none';
272   return '' if $global_unique eq 'disabled';
273
274   $self->lock_table;
275
276   my @dup_ccphonenum =
277     grep { !$self->svcnum || $_->svcnum != $self->svcnum }
278     qsearch( 'svc_phone', {
279       'countrycode' => $self->countrycode,
280       'phonenum'    => $self->phonenum,
281     });
282
283   return gettext('phonenum_in_use')
284     if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
285
286   my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
287   unless ( $part_svc ) {
288     return 'unknown svcpart '. $self->svcpart;
289   }
290
291   if ( @dup_ccphonenum ) {
292
293     my $exports = FS::part_export::export_info('svc_phone');
294     my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
295
296     foreach my $part_export ( $part_svc->part_export ) {
297
298       #this will catch to the same exact export
299       my @svcparts = map { $_->svcpart } $part_export->export_svc;
300
301       $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
302         foreach @svcparts;
303
304     }
305
306     foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
307       my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
308       if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
309         return "duplicate phone number ".
310                $self->countrycode. ' '. $self->phonenum.
311                ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
312                " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
313       }
314     }
315
316   }
317
318   return '';
319
320 }
321
322 =item check_pin
323
324 Checks the supplied PIN against the PIN in the database.  Returns true for a
325 sucessful authentication, false if no match.
326
327 =cut
328
329 sub check_pin {
330   my($self, $check_pin) = @_;
331   length($self->pin) && $check_pin eq $self->pin;
332 }
333
334 =item radius_reply
335
336 =cut
337
338 sub radius_reply {
339   my $self = shift;
340   #XXX Session-Timeout!  holy shit, need rlm_perl to ask for this in realtime
341   ();
342 }
343
344 =item radius_check
345
346 =cut
347
348 sub radius_check {
349   my $self = shift;
350   my %check = ();
351
352   my $conf = new FS::Conf;
353
354   $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
355
356   %check;
357 }
358
359 sub radius_groups {
360   ();
361 }
362
363 =item phone_device
364
365 Returns any FS::phone_device records associated with this service.
366
367 =cut
368
369 sub phone_device {
370   my $self = shift;
371   qsearch('phone_device', { 'svcnum' => $self->svcnum } );
372 }
373
374 =back
375
376 =head1 BUGS
377
378 =head1 SEE ALSO
379
380 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
381 L<FS::cust_pkg>, schema.html from the base documentation.
382
383 =cut
384
385 1;
386