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