4 use base qw( FS::svc_Domain_Mixin FS::location_Mixin FS::svc_Common );
5 use vars qw( $DEBUG $me @pw_set $conf $phone_name_max );
7 use Scalar::Util qw( blessed );
9 use FS::Record qw( qsearch qsearchs dbh );
10 use FS::Msgcat qw(gettext);
15 use FS::cust_location;
18 $me = '[' . __PACKAGE__ . ']';
22 @pw_set = ( 'a'..'k', 'm','n', 'p-z', 'A'..'N', 'P'..'Z' , '2'..'9' );
24 #ask FS::UID to run this stuff for us later
25 $FS::UID::callback{'FS::svc_acct'} = sub {
27 $phone_name_max = $conf->config('svc_phone-phone_name-max_length');
32 FS::svc_phone - Object methods for svc_phone records
38 $record = new FS::svc_phone \%hash;
39 $record = new FS::svc_phone { 'column' => 'value' };
41 $error = $record->insert;
43 $error = $new_record->replace($old_record);
45 $error = $record->delete;
47 $error = $record->check;
49 $error = $record->suspend;
51 $error = $record->unsuspend;
53 $error = $record->cancel;
57 An FS::svc_phone object represents a phone number. FS::svc_phone inherits
58 from FS::Record. The following fields are currently supported:
80 Optional svcnum from svc_pbx
90 Creates a new phone number. To add the number to the database, see L<"insert">.
92 Note that this stores the hash reference, not a distinct copy of the hash it
93 points to. You can ask the object for a copy with the I<hash> method.
97 # the new method can be inherited from FS::Record, if a table method is defined
101 'name' => 'Phone number',
102 'sorts' => 'phonenum',
103 'display_weight' => 60,
104 'cancel_weight' => 80,
106 'countrycode' => { label => 'Country code',
108 disable_inventory => 1,
111 'phonenum' => 'Phone number',
112 'pin' => { label => 'Personal Identification Number',
114 disable_inventory => 1,
117 'sip_password' => 'SIP password',
118 'phone_name' => 'Name',
119 'pbxsvc' => { label => 'PBX',
120 type => 'select-svc_pbx.html',
121 disable_inventory => 1,
122 disable_select => 1, #UI wonky, pry works otherwise
127 select_table => 'svc_domain',
128 select_key => 'svcnum',
129 select_label => 'domain',
130 disable_inventory => 1,
133 label => 'E911 location',
134 disable_inventory => 1,
141 sub table { 'svc_phone'; }
143 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
145 =item search_sql STRING
147 Class method which returns an SQL fragment to search for the given string.
152 my( $class, $string ) = @_;
154 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
160 my $conf = new FS::Conf;
161 my $ccode = ( $conf->exists('default_phone_countrycode')
162 && $conf->config('default_phone_countrycode')
164 ? $conf->config('default_phone_countrycode')
167 $string =~ s/^$ccode//;
169 $class->search_sql_field('phonenum', $string );
174 Returns the phone number.
180 my $phonenum = $self->phonenum; #XXX format it better
181 my $label = $phonenum;
182 $label .= '@'.$self->domain if $self->domsvc;
183 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
189 Adds this phone number to the database. If there is an error, returns the
190 error, otherwise returns false.
199 warn "[$me] insert called on $self: ". Dumper($self).
200 "\nwith options: ". Dumper(%options);
203 local $SIG{HUP} = 'IGNORE';
204 local $SIG{INT} = 'IGNORE';
205 local $SIG{QUIT} = 'IGNORE';
206 local $SIG{TERM} = 'IGNORE';
207 local $SIG{TSTP} = 'IGNORE';
208 local $SIG{PIPE} = 'IGNORE';
210 my $oldAutoCommit = $FS::UID::AutoCommit;
211 local $FS::UID::AutoCommit = 0;
214 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
215 #make it more of a base class than a mixin... :)
216 if ( $options{'cust_location'}
217 && ( ! $self->locationnum || $self->locationnum == -1 ) ) {
218 my $error = $options{'cust_location'}->insert;
220 $dbh->rollback if $oldAutoCommit;
221 return "inserting cust_location (transaction rolled back): $error";
223 $self->locationnum( $options{'cust_location'}->locationnum );
225 #what about on-the-fly edits? if the ui supports it?
227 my $error = $self->SUPER::insert(%options);
229 $dbh->rollback if $oldAutoCommit;
233 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
240 Delete this record from the database.
247 local $SIG{HUP} = 'IGNORE';
248 local $SIG{INT} = 'IGNORE';
249 local $SIG{QUIT} = 'IGNORE';
250 local $SIG{TERM} = 'IGNORE';
251 local $SIG{TSTP} = 'IGNORE';
252 local $SIG{PIPE} = 'IGNORE';
254 my $oldAutoCommit = $FS::UID::AutoCommit;
255 local $FS::UID::AutoCommit = 0;
258 foreach my $phone_device ( $self->phone_device ) {
259 my $error = $phone_device->delete;
261 $dbh->rollback if $oldAutoCommit;
266 my @phone_avail = qsearch('phone_avail', { 'svcnum' => $self->svcnum } );
267 foreach my $phone_avail ( @phone_avail ) {
268 $phone_avail->svcnum('');
269 my $error = $phone_avail->replace;
271 $dbh->rollback if $oldAutoCommit;
276 my $error = $self->SUPER::delete;
278 $dbh->rollback if $oldAutoCommit;
282 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
287 # the delete method can be inherited from FS::Record
289 =item replace OLD_RECORD
291 Replaces the OLD_RECORD with this one in the database. If there is an error,
292 returns the error, otherwise returns false.
299 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
306 warn "[$me] replacing $old with $new\n".
307 "\nwith options: ". Dumper(%options);
310 local $SIG{HUP} = 'IGNORE';
311 local $SIG{INT} = 'IGNORE';
312 local $SIG{QUIT} = 'IGNORE';
313 local $SIG{TERM} = 'IGNORE';
314 local $SIG{TSTP} = 'IGNORE';
315 local $SIG{PIPE} = 'IGNORE';
317 my $oldAutoCommit = $FS::UID::AutoCommit;
318 local $FS::UID::AutoCommit = 0;
321 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
322 #make it more of a base class than a mixin... :)
323 if ( $options{'cust_location'}
324 && ( ! $new->locationnum || $new->locationnum == -1 ) ) {
325 my $error = $options{'cust_location'}->insert;
327 $dbh->rollback if $oldAutoCommit;
328 return "inserting cust_location (transaction rolled back): $error";
330 $new->locationnum( $options{'cust_location'}->locationnum );
332 #what about on-the-fly edits? if the ui supports it?
334 my $error = $new->SUPER::replace($old, %options);
336 $dbh->rollback if $oldAutoCommit;
337 return $error if $error;
340 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
346 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
350 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
354 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
358 Checks all fields to make sure this is a valid phone number. If there is
359 an error, returns the error, otherwise returns false. Called by the insert
364 # the check method should currently be supplied - FS::Record contains some
365 # data checking routines
370 my $conf = new FS::Conf;
372 my $phonenum = $self->phonenum;
373 my $phonenum_check_method;
374 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
375 $phonenum =~ s/\W//g;
376 $phonenum_check_method = 'ut_alpha';
378 $phonenum =~ s/\D//g;
379 $phonenum_check_method = 'ut_number';
381 $self->phonenum($phonenum);
383 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
386 $self->ut_numbern('svcnum')
387 || $self->ut_numbern('countrycode')
388 || $self->$phonenum_check_method('phonenum')
389 || $self->ut_anything('sip_password')
390 || $self->ut_numbern('pin')
391 || $self->ut_textn('phone_name')
392 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
393 || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
394 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
396 return $error if $error;
398 return 'Name ('. $self->phone_name.
399 ") is longer than $phone_name_max characters"
400 if $phone_name_max && length($self->phone_name) > $phone_name_max;
402 $self->countrycode(1) unless $self->countrycode;
404 unless ( length($self->sip_password) ) {
407 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
415 =item _check duplicate
417 Internal method to check for duplicate phone numers.
421 #false laziness w/svc_acct.pm's _check_duplicate.
422 sub _check_duplicate {
425 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
426 return '' if $global_unique eq 'disabled';
431 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
432 qsearch( 'svc_phone', {
433 'countrycode' => $self->countrycode,
434 'phonenum' => $self->phonenum,
437 return gettext('phonenum_in_use')
438 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
440 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
441 unless ( $part_svc ) {
442 return 'unknown svcpart '. $self->svcpart;
445 if ( @dup_ccphonenum ) {
447 my $exports = FS::part_export::export_info('svc_phone');
448 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
450 foreach my $part_export ( $part_svc->part_export ) {
452 #this will catch to the same exact export
453 my @svcparts = map { $_->svcpart } $part_export->export_svc;
455 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
460 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
461 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
462 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
463 return "duplicate phone number ".
464 $self->countrycode. ' '. $self->phonenum.
465 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
466 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
478 Checks the supplied PIN against the PIN in the database. Returns true for a
479 sucessful authentication, false if no match.
484 my($self, $check_pin) = @_;
485 length($self->pin) && $check_pin eq $self->pin;
494 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
506 my $conf = new FS::Conf;
508 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
519 Returns any FS::phone_device records associated with this service.
525 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
528 #override location_Mixin version cause we want to try the cust_pkg location
529 #in between us and cust_main
530 # XXX what to do in the unlinked case??? return a pseudo-object that returns
532 sub cust_location_or_main {
534 return $self->cust_location if $self->locationnum;
535 my $cust_pkg = $self->cust_svc->cust_pkg;
536 $cust_pkg ? $cust_pkg->cust_location_or_main : '';
541 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this
542 service. By default, "associated with" means that either the "src" or the
543 "charged_party" field of the CDR matches the "phonenum" field of the service.
547 Accepts the following options:
549 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
551 =item status => "" (or "done"): Return only CDRs with that processing status.
553 =item inbound => 1: Return CDRs for inbound calls. With "status", will filter
554 on inbound processing status.
556 =item default_prefix => "XXX": Also accept the phone number of the service prepended
557 with the chosen prefix.
559 =item disable_src => 1: Only match on "charged_party", not "src".
561 =item by_svcnum: not supported for svc_phone
568 my($self, %options) = @_;
573 if ( $options{'inbound'} ) {
575 if ( exists($options{'status'}) ) {
576 # must be 'done' or ''
577 my $sq = 'EXISTS ( SELECT 1 FROM cdr_termination '.
578 'WHERE cdr.acctid = cdr_termination.acctid '.
579 'AND cdr_termination.status = \'done\' '.
580 'AND cdr_termination.termpart = 1 )';
581 if ( $options{'status'} eq 'done' ) {
584 elsif ($options{'status'} eq '' ) {
585 push @where, "NOT $sq";
588 warn "invalid status: $options{'status'} (ignored)\n";
593 @fields = ( 'charged_party' );
594 push @fields, 'src' if !$options{'disable_src'};
595 $hash{'freesidestatus'} = $options{'status'}
596 if exists($options{'status'});
599 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
601 my $number = $self->phonenum;
603 my $prefix = $options{'default_prefix'};
605 my @orwhere = map " $_ = '$number' ", @fields;
606 push @orwhere, map " $_ = '$prefix$number' ", @fields
608 if ( $prefix =~ /^\+(\d+)$/ ) {
609 push @orwhere, map " $_ = '$1$number' ", @fields
612 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
614 if ( $options{'begin'} ) {
615 push @where, 'startdate >= '. $options{'begin'};
617 if ( $options{'end'} ) {
618 push @where, 'startdate < '. $options{'end'};
621 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
627 'extra_sql' => $extra_sql,
628 'order_by' => "ORDER BY startdate $for_update",
640 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
641 L<FS::cust_pkg>, schema.html from the base documentation.