4 use base qw( FS::svc_Domain_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);
15 @pw_set = ( 'a'..'k', 'm','n', 'p-z', 'A'..'N', 'P'..'Z' , '2'..'9' );
17 #ask FS::UID to run this stuff for us later
18 $FS::UID::callback{'FS::svc_acct'} = sub {
24 FS::svc_phone - Object methods for svc_phone records
30 $record = new FS::svc_phone \%hash;
31 $record = new FS::svc_phone { 'column' => 'value' };
33 $error = $record->insert;
35 $error = $new_record->replace($old_record);
37 $error = $record->delete;
39 $error = $record->check;
41 $error = $record->suspend;
43 $error = $record->unsuspend;
45 $error = $record->cancel;
49 An FS::svc_phone object represents a phone number. FS::svc_phone inherits
50 from FS::Record. The following fields are currently supported:
72 Optional svcnum from svc_pbx
82 Creates a new phone number. To add the number to the database, see L<"insert">.
84 Note that this stores the hash reference, not a distinct copy of the hash it
85 points to. You can ask the object for a copy with the I<hash> method.
89 # the new method can be inherited from FS::Record, if a table method is defined
93 'name' => 'Phone number',
94 'sorts' => 'phonenum',
95 'display_weight' => 60,
96 'cancel_weight' => 80,
98 'countrycode' => { label => 'Country code',
100 disable_inventory => 1,
103 'phonenum' => 'Phone number',
104 'pin' => { label => 'Personal Identification Number',
106 disable_inventory => 1,
109 'sip_password' => 'SIP password',
110 'phone_name' => 'Name',
111 'pbxsvc' => { label => 'PBX',
112 type => 'select-svc_pbx.html',
113 disable_inventory => 1,
114 disable_select => 1, #UI wonky, pry works otherwise
119 select_table => 'svc_domain',
120 select_key => 'svcnum',
121 select_label => 'domain',
122 disable_inventory => 1,
128 sub table { 'svc_phone'; }
130 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
132 =item search_sql STRING
134 Class method which returns an SQL fragment to search for the given string.
139 my( $class, $string ) = @_;
141 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
147 my $conf = new FS::Conf;
148 my $ccode = ( $conf->exists('default_phone_countrycode')
149 && $conf->config('default_phone_countrycode')
151 ? $conf->config('default_phone_countrycode')
154 $string =~ s/^$ccode//;
156 $class->search_sql_field('phonenum', $string );
161 Returns the phone number.
167 my $phonenum = $self->phonenum; #XXX format it better
168 my $label = $phonenum;
169 $label .= '@'.$self->domain if $self->domsvc;
170 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
176 Adds this record to the database. If there is an error, returns the error,
177 otherwise returns false.
181 # the insert method can be inherited from FS::Record
185 Delete this record from the database.
192 local $SIG{HUP} = 'IGNORE';
193 local $SIG{INT} = 'IGNORE';
194 local $SIG{QUIT} = 'IGNORE';
195 local $SIG{TERM} = 'IGNORE';
196 local $SIG{TSTP} = 'IGNORE';
197 local $SIG{PIPE} = 'IGNORE';
199 my $oldAutoCommit = $FS::UID::AutoCommit;
200 local $FS::UID::AutoCommit = 0;
203 foreach my $phone_device ( $self->phone_device ) {
204 my $error = $phone_device->delete;
206 $dbh->rollback if $oldAutoCommit;
211 my $error = $self->SUPER::delete;
213 $dbh->rollback if $oldAutoCommit;
217 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
222 # the delete method can be inherited from FS::Record
224 =item replace OLD_RECORD
226 Replaces the OLD_RECORD with this one in the database. If there is an error,
227 returns the error, otherwise returns false.
231 # the replace method can be inherited from FS::Record
235 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
239 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
243 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
247 Checks all fields to make sure this is a valid phone number. If there is
248 an error, returns the error, otherwise returns false. Called by the insert
253 # the check method should currently be supplied - FS::Record contains some
254 # data checking routines
259 my $conf = new FS::Conf;
261 my $phonenum = $self->phonenum;
262 my $phonenum_check_method;
263 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
264 $phonenum =~ s/\W//g;
265 $phonenum_check_method = 'ut_alpha';
267 $phonenum =~ s/\D//g;
268 $phonenum_check_method = 'ut_number';
270 $self->phonenum($phonenum);
273 $self->ut_numbern('svcnum')
274 || $self->ut_numbern('countrycode')
275 || $self->$phonenum_check_method('phonenum')
276 || $self->ut_anything('sip_password')
277 || $self->ut_numbern('pin')
278 || $self->ut_textn('phone_name')
279 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
280 || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
282 return $error if $error;
284 $self->countrycode(1) unless $self->countrycode;
286 unless ( length($self->sip_password) ) {
289 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
297 =item _check duplicate
299 Internal method to check for duplicate phone numers.
303 #false laziness w/svc_acct.pm's _check_duplicate.
304 sub _check_duplicate {
307 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
308 return '' if $global_unique eq 'disabled';
313 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
314 qsearch( 'svc_phone', {
315 'countrycode' => $self->countrycode,
316 'phonenum' => $self->phonenum,
319 return gettext('phonenum_in_use')
320 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
322 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
323 unless ( $part_svc ) {
324 return 'unknown svcpart '. $self->svcpart;
327 if ( @dup_ccphonenum ) {
329 my $exports = FS::part_export::export_info('svc_phone');
330 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
332 foreach my $part_export ( $part_svc->part_export ) {
334 #this will catch to the same exact export
335 my @svcparts = map { $_->svcpart } $part_export->export_svc;
337 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
342 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
343 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
344 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
345 return "duplicate phone number ".
346 $self->countrycode. ' '. $self->phonenum.
347 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
348 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
360 Checks the supplied PIN against the PIN in the database. Returns true for a
361 sucessful authentication, false if no match.
366 my($self, $check_pin) = @_;
367 length($self->pin) && $check_pin eq $self->pin;
376 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
388 my $conf = new FS::Conf;
390 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
401 Returns any FS::phone_device records associated with this service.
407 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
416 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
417 L<FS::cust_pkg>, schema.html from the base documentation.