4 use vars qw( @ISA @pw_set $conf );
6 use FS::Record qw( qsearch qsearchs dbh );
7 use FS::Msgcat qw(gettext);
12 @ISA = qw( FS::svc_Common );
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:
78 Creates a new phone number. To add the number to the database, see L<"insert">.
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.
85 # the new method can be inherited from FS::Record, if a table method is defined
89 'name' => 'Phone number',
90 'sorts' => 'phonenum',
91 'display_weight' => 60,
92 'cancel_weight' => 80,
94 'countrycode' => { label => 'Country code',
96 disable_inventory => 1,
99 'phonenum' => 'Phone number',
100 'pin' => { label => 'Personal Identification Number',
102 disable_inventory => 1,
105 'sip_password' => 'SIP password',
106 'phone_name' => 'Name',
111 sub table { 'svc_phone'; }
113 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
115 =item search_sql STRING
117 Class method which returns an SQL fragment to search for the given string.
122 my( $class, $string ) = @_;
123 $class->search_sql_field('phonenum', $string );
128 Returns the phone number.
134 my $phonenum = $self->phonenum; #XXX format it better
135 my $label = $phonenum;
136 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
142 Adds this record to the database. If there is an error, returns the error,
143 otherwise returns false.
147 # the insert method can be inherited from FS::Record
151 Delete this record from the database.
158 local $SIG{HUP} = 'IGNORE';
159 local $SIG{INT} = 'IGNORE';
160 local $SIG{QUIT} = 'IGNORE';
161 local $SIG{TERM} = 'IGNORE';
162 local $SIG{TSTP} = 'IGNORE';
163 local $SIG{PIPE} = 'IGNORE';
165 my $oldAutoCommit = $FS::UID::AutoCommit;
166 local $FS::UID::AutoCommit = 0;
169 foreach my $phone_device ( $self->phone_device ) {
170 my $error = $phone_device->delete;
172 $dbh->rollback if $oldAutoCommit;
177 my $error = $self->SUPER::delete;
179 $dbh->rollback if $oldAutoCommit;
183 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
188 # the delete method can be inherited from FS::Record
190 =item replace OLD_RECORD
192 Replaces the OLD_RECORD with this one in the database. If there is an error,
193 returns the error, otherwise returns false.
197 # the replace method can be inherited from FS::Record
201 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
205 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
209 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
213 Checks all fields to make sure this is a valid phone number. If there is
214 an error, returns the error, otherwise returns false. Called by the insert
219 # the check method should currently be supplied - FS::Record contains some
220 # data checking routines
225 my $conf = new FS::Conf;
227 my $phonenum = $self->phonenum;
228 my $phonenum_check_method;
229 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
230 $phonenum =~ s/\W//g;
231 $phonenum_check_method = 'ut_alpha';
233 $phonenum =~ s/\D//g;
234 $phonenum_check_method = 'ut_number';
236 $self->phonenum($phonenum);
239 $self->ut_numbern('svcnum')
240 || $self->ut_numbern('countrycode')
241 || $self->$phonenum_check_method('phonenum')
242 || $self->ut_anything('sip_password')
243 || $self->ut_numbern('pin')
244 || $self->ut_textn('phone_name')
246 return $error if $error;
248 $self->countrycode(1) unless $self->countrycode;
250 unless ( length($self->sip_password) ) {
253 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
261 =item _check duplicate
263 Internal method to check for duplicate phone numers.
267 #false laziness w/svc_acct.pm's _check_duplicate.
268 sub _check_duplicate {
271 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
272 return '' if $global_unique eq 'disabled';
277 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
278 qsearch( 'svc_phone', {
279 'countrycode' => $self->countrycode,
280 'phonenum' => $self->phonenum,
283 return gettext('phonenum_in_use')
284 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
286 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
287 unless ( $part_svc ) {
288 return 'unknown svcpart '. $self->svcpart;
291 if ( @dup_ccphonenum ) {
293 my $exports = FS::part_export::export_info('svc_phone');
294 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
296 foreach my $part_export ( $part_svc->part_export ) {
298 #this will catch to the same exact export
299 my @svcparts = map { $_->svcpart } $part_export->export_svc;
301 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
306 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
307 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
308 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
309 return "duplicate phone number ".
310 $self->countrycode. ' '. $self->phonenum.
311 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
312 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
324 Checks the supplied PIN against the PIN in the database. Returns true for a
325 sucessful authentication, false if no match.
330 my($self, $check_pin) = @_;
331 length($self->pin) && $check_pin eq $self->pin;
340 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
352 my $conf = new FS::Conf;
354 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
365 Returns any FS::phone_device records associated with this service.
371 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
380 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
381 L<FS::cust_pkg>, schema.html from the base documentation.