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 ) = @_;
124 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
130 my $conf = new FS::Conf;
131 my $ccode = ( $conf->exists('default_phone_countrycode')
132 && $conf->config('default_phone_countrycode')
134 ? $conf->config('default_phone_countrycode')
137 $string =~ s/^$ccode//;
139 $class->search_sql_field('phonenum', $string );
144 Returns the phone number.
150 my $phonenum = $self->phonenum; #XXX format it better
151 my $label = $phonenum;
152 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
158 Adds this record to the database. If there is an error, returns the error,
159 otherwise returns false.
163 # the insert method can be inherited from FS::Record
167 Delete this record from the database.
174 local $SIG{HUP} = 'IGNORE';
175 local $SIG{INT} = 'IGNORE';
176 local $SIG{QUIT} = 'IGNORE';
177 local $SIG{TERM} = 'IGNORE';
178 local $SIG{TSTP} = 'IGNORE';
179 local $SIG{PIPE} = 'IGNORE';
181 my $oldAutoCommit = $FS::UID::AutoCommit;
182 local $FS::UID::AutoCommit = 0;
185 foreach my $phone_device ( $self->phone_device ) {
186 my $error = $phone_device->delete;
188 $dbh->rollback if $oldAutoCommit;
193 my $error = $self->SUPER::delete;
195 $dbh->rollback if $oldAutoCommit;
199 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
204 # the delete method can be inherited from FS::Record
206 =item replace OLD_RECORD
208 Replaces the OLD_RECORD with this one in the database. If there is an error,
209 returns the error, otherwise returns false.
213 # the replace method can be inherited from FS::Record
217 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
221 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
225 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
229 Checks all fields to make sure this is a valid phone number. If there is
230 an error, returns the error, otherwise returns false. Called by the insert
235 # the check method should currently be supplied - FS::Record contains some
236 # data checking routines
241 my $conf = new FS::Conf;
243 my $phonenum = $self->phonenum;
244 my $phonenum_check_method;
245 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
246 $phonenum =~ s/\W//g;
247 $phonenum_check_method = 'ut_alpha';
249 $phonenum =~ s/\D//g;
250 $phonenum_check_method = 'ut_number';
252 $self->phonenum($phonenum);
255 $self->ut_numbern('svcnum')
256 || $self->ut_numbern('countrycode')
257 || $self->$phonenum_check_method('phonenum')
258 || $self->ut_anything('sip_password')
259 || $self->ut_numbern('pin')
260 || $self->ut_textn('phone_name')
262 return $error if $error;
264 $self->countrycode(1) unless $self->countrycode;
266 unless ( length($self->sip_password) ) {
269 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
277 =item _check duplicate
279 Internal method to check for duplicate phone numers.
283 #false laziness w/svc_acct.pm's _check_duplicate.
284 sub _check_duplicate {
287 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
288 return '' if $global_unique eq 'disabled';
293 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
294 qsearch( 'svc_phone', {
295 'countrycode' => $self->countrycode,
296 'phonenum' => $self->phonenum,
299 return gettext('phonenum_in_use')
300 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
302 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
303 unless ( $part_svc ) {
304 return 'unknown svcpart '. $self->svcpart;
307 if ( @dup_ccphonenum ) {
309 my $exports = FS::part_export::export_info('svc_phone');
310 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
312 foreach my $part_export ( $part_svc->part_export ) {
314 #this will catch to the same exact export
315 my @svcparts = map { $_->svcpart } $part_export->export_svc;
317 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
322 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
323 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
324 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
325 return "duplicate phone number ".
326 $self->countrycode. ' '. $self->phonenum.
327 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
328 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
340 Checks the supplied PIN against the PIN in the database. Returns true for a
341 sucessful authentication, false if no match.
346 my($self, $check_pin) = @_;
347 length($self->pin) && $check_pin eq $self->pin;
356 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
368 my $conf = new FS::Conf;
370 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
381 Returns any FS::phone_device records associated with this service.
387 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
396 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
397 L<FS::cust_pkg>, schema.html from the base documentation.