4 use vars qw( @ISA @pw_set $conf );
6 use FS::Record qw( qsearch qsearchs dbh );
7 use FS::Msgcat qw(gettext);
13 @ISA = qw( FS::svc_Common );
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
121 sub table { 'svc_phone'; }
123 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
125 =item search_sql STRING
127 Class method which returns an SQL fragment to search for the given string.
132 my( $class, $string ) = @_;
134 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
140 my $conf = new FS::Conf;
141 my $ccode = ( $conf->exists('default_phone_countrycode')
142 && $conf->config('default_phone_countrycode')
144 ? $conf->config('default_phone_countrycode')
147 $string =~ s/^$ccode//;
149 $class->search_sql_field('phonenum', $string );
154 Returns the phone number.
160 my $phonenum = $self->phonenum; #XXX format it better
161 my $label = $phonenum;
162 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
168 Adds this record to the database. If there is an error, returns the error,
169 otherwise returns false.
173 # the insert method can be inherited from FS::Record
177 Delete this record from the database.
184 local $SIG{HUP} = 'IGNORE';
185 local $SIG{INT} = 'IGNORE';
186 local $SIG{QUIT} = 'IGNORE';
187 local $SIG{TERM} = 'IGNORE';
188 local $SIG{TSTP} = 'IGNORE';
189 local $SIG{PIPE} = 'IGNORE';
191 my $oldAutoCommit = $FS::UID::AutoCommit;
192 local $FS::UID::AutoCommit = 0;
195 foreach my $phone_device ( $self->phone_device ) {
196 my $error = $phone_device->delete;
198 $dbh->rollback if $oldAutoCommit;
203 my $error = $self->SUPER::delete;
205 $dbh->rollback if $oldAutoCommit;
209 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
214 # the delete method can be inherited from FS::Record
216 =item replace OLD_RECORD
218 Replaces the OLD_RECORD with this one in the database. If there is an error,
219 returns the error, otherwise returns false.
223 # the replace method can be inherited from FS::Record
227 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
231 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
235 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
239 Checks all fields to make sure this is a valid phone number. If there is
240 an error, returns the error, otherwise returns false. Called by the insert
245 # the check method should currently be supplied - FS::Record contains some
246 # data checking routines
251 my $conf = new FS::Conf;
253 my $phonenum = $self->phonenum;
254 my $phonenum_check_method;
255 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
256 $phonenum =~ s/\W//g;
257 $phonenum_check_method = 'ut_alpha';
259 $phonenum =~ s/\D//g;
260 $phonenum_check_method = 'ut_number';
262 $self->phonenum($phonenum);
265 $self->ut_numbern('svcnum')
266 || $self->ut_numbern('countrycode')
267 || $self->$phonenum_check_method('phonenum')
268 || $self->ut_anything('sip_password')
269 || $self->ut_numbern('pin')
270 || $self->ut_textn('phone_name')
271 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
273 return $error if $error;
275 $self->countrycode(1) unless $self->countrycode;
277 unless ( length($self->sip_password) ) {
280 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
288 =item _check duplicate
290 Internal method to check for duplicate phone numers.
294 #false laziness w/svc_acct.pm's _check_duplicate.
295 sub _check_duplicate {
298 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
299 return '' if $global_unique eq 'disabled';
304 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
305 qsearch( 'svc_phone', {
306 'countrycode' => $self->countrycode,
307 'phonenum' => $self->phonenum,
310 return gettext('phonenum_in_use')
311 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
313 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
314 unless ( $part_svc ) {
315 return 'unknown svcpart '. $self->svcpart;
318 if ( @dup_ccphonenum ) {
320 my $exports = FS::part_export::export_info('svc_phone');
321 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
323 foreach my $part_export ( $part_svc->part_export ) {
325 #this will catch to the same exact export
326 my @svcparts = map { $_->svcpart } $part_export->export_svc;
328 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
333 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
334 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
335 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
336 return "duplicate phone number ".
337 $self->countrycode. ' '. $self->phonenum.
338 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
339 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
351 Checks the supplied PIN against the PIN in the database. Returns true for a
352 sucessful authentication, false if no match.
357 my($self, $check_pin) = @_;
358 length($self->pin) && $check_pin eq $self->pin;
367 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
379 my $conf = new FS::Conf;
381 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
392 Returns any FS::phone_device records associated with this service.
398 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
407 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
408 L<FS::cust_pkg>, schema.html from the base documentation.