4 use base qw( FS::svc_Domain_Mixin FS::location_Mixin FS::svc_Common );
5 use vars qw( @pw_set $conf );
7 use FS::Record qw( qsearch qsearchs dbh );
8 use FS::Msgcat qw(gettext);
13 use FS::cust_location;
16 @pw_set = ( 'a'..'k', 'm','n', 'p-z', 'A'..'N', 'P'..'Z' , '2'..'9' );
18 #ask FS::UID to run this stuff for us later
19 $FS::UID::callback{'FS::svc_acct'} = sub {
25 FS::svc_phone - Object methods for svc_phone records
31 $record = new FS::svc_phone \%hash;
32 $record = new FS::svc_phone { 'column' => 'value' };
34 $error = $record->insert;
36 $error = $new_record->replace($old_record);
38 $error = $record->delete;
40 $error = $record->check;
42 $error = $record->suspend;
44 $error = $record->unsuspend;
46 $error = $record->cancel;
50 An FS::svc_phone object represents a phone number. FS::svc_phone inherits
51 from FS::Record. The following fields are currently supported:
73 Optional svcnum from svc_pbx
83 Creates a new phone number. To add the number to the database, see L<"insert">.
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.
90 # the new method can be inherited from FS::Record, if a table method is defined
94 'name' => 'Phone number',
95 'sorts' => 'phonenum',
96 'display_weight' => 60,
97 'cancel_weight' => 80,
99 'countrycode' => { label => 'Country code',
101 disable_inventory => 1,
104 'phonenum' => 'Phone number',
105 'pin' => { label => 'Personal Identification Number',
107 disable_inventory => 1,
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
120 select_table => 'svc_domain',
121 select_key => 'svcnum',
122 select_label => 'domain',
123 disable_inventory => 1,
126 label => 'E911 location',
127 disable_inventory => 1,
134 sub table { 'svc_phone'; }
136 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
138 =item search_sql STRING
140 Class method which returns an SQL fragment to search for the given string.
145 my( $class, $string ) = @_;
147 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
153 my $conf = new FS::Conf;
154 my $ccode = ( $conf->exists('default_phone_countrycode')
155 && $conf->config('default_phone_countrycode')
157 ? $conf->config('default_phone_countrycode')
160 $string =~ s/^$ccode//;
162 $class->search_sql_field('phonenum', $string );
167 Returns the phone number.
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;
182 Adds this record to the database. If there is an error, returns the error,
183 otherwise returns false.
187 # the insert method can be inherited from FS::Record
191 Delete this record from the database.
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';
205 my $oldAutoCommit = $FS::UID::AutoCommit;
206 local $FS::UID::AutoCommit = 0;
209 foreach my $phone_device ( $self->phone_device ) {
210 my $error = $phone_device->delete;
212 $dbh->rollback if $oldAutoCommit;
217 my $error = $self->SUPER::delete;
219 $dbh->rollback if $oldAutoCommit;
223 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
228 # the delete method can be inherited from FS::Record
230 =item replace OLD_RECORD
232 Replaces the OLD_RECORD with this one in the database. If there is an error,
233 returns the error, otherwise returns false.
237 # the replace method can be inherited from FS::Record
241 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
245 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
249 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
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
259 # the check method should currently be supplied - FS::Record contains some
260 # data checking routines
265 my $conf = new FS::Conf;
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';
273 $phonenum =~ s/\D//g;
274 $phonenum_check_method = 'ut_number';
276 $self->phonenum($phonenum);
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')
289 return $error if $error;
291 $self->countrycode(1) unless $self->countrycode;
293 unless ( length($self->sip_password) ) {
296 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
304 =item _check duplicate
306 Internal method to check for duplicate phone numers.
310 #false laziness w/svc_acct.pm's _check_duplicate.
311 sub _check_duplicate {
314 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
315 return '' if $global_unique eq 'disabled';
320 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
321 qsearch( 'svc_phone', {
322 'countrycode' => $self->countrycode,
323 'phonenum' => $self->phonenum,
326 return gettext('phonenum_in_use')
327 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
329 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
330 unless ( $part_svc ) {
331 return 'unknown svcpart '. $self->svcpart;
334 if ( @dup_ccphonenum ) {
336 my $exports = FS::part_export::export_info('svc_phone');
337 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
339 foreach my $part_export ( $part_svc->part_export ) {
341 #this will catch to the same exact export
342 my @svcparts = map { $_->svcpart } $part_export->export_svc;
344 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
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};
367 Checks the supplied PIN against the PIN in the database. Returns true for a
368 sucessful authentication, false if no match.
373 my($self, $check_pin) = @_;
374 length($self->pin) && $check_pin eq $self->pin;
383 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
395 my $conf = new FS::Conf;
397 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
408 Returns any FS::phone_device records associated with this service.
414 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
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.