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