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
100 my %dis2 = ( disable_inventory=>1, disable_select=>1 );
102 'name' => 'Phone number',
103 'sorts' => 'phonenum',
104 'display_weight' => 60,
105 'cancel_weight' => 80,
107 'countrycode' => { label => 'Country code',
109 disable_inventory => 1,
112 'phonenum' => 'Phone number',
113 'pin' => { label => 'Personal Identification Number',
115 disable_inventory => 1,
118 'sip_password' => 'SIP password',
119 'phone_name' => 'Name',
120 'pbxsvc' => { label => 'PBX',
121 type => 'select-svc_pbx.html',
122 disable_inventory => 1,
123 disable_select => 1, #UI wonky, pry works otherwise
128 select_table => 'svc_domain',
129 select_key => 'svcnum',
130 select_label => 'domain',
131 disable_inventory => 1,
134 label => 'E911 location',
135 disable_inventory => 1,
138 'lnp_status' => { label => 'LNP Status',
139 type => 'select-lnp_status.html',
142 'portable' => { label => 'Portable?', %dis2, },
143 'lrn' => { label => 'LRN',
144 disable_inventory => 1,
146 'lnp_desired_due_date' =>
147 { label => 'LNP Desired Due Date', %dis2 },
149 { label => 'LNP Due Date', %dis2 },
150 'lnp_other_provider' =>
151 { label => 'LNP Other Provider',
152 disable_inventory => 1,
154 'lnp_other_provider_account' =>
155 { label => 'LNP Other Provider Account #',
162 sub table { 'svc_phone'; }
164 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
166 =item search_sql STRING
168 Class method which returns an SQL fragment to search for the given string.
173 my( $class, $string ) = @_;
175 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
181 my $conf = new FS::Conf;
182 my $ccode = ( $conf->exists('default_phone_countrycode')
183 && $conf->config('default_phone_countrycode')
185 ? $conf->config('default_phone_countrycode')
188 $string =~ s/^$ccode//;
190 $class->search_sql_field('phonenum', $string );
195 Returns the phone number.
201 my $phonenum = $self->phonenum; #XXX format it better
202 my $label = $phonenum;
203 $label .= '@'.$self->domain if $self->domsvc;
204 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
210 Adds this phone number to the database. If there is an error, returns the
211 error, otherwise returns false.
220 warn "[$me] insert called on $self: ". Dumper($self).
221 "\nwith options: ". Dumper(%options);
224 local $SIG{HUP} = 'IGNORE';
225 local $SIG{INT} = 'IGNORE';
226 local $SIG{QUIT} = 'IGNORE';
227 local $SIG{TERM} = 'IGNORE';
228 local $SIG{TSTP} = 'IGNORE';
229 local $SIG{PIPE} = 'IGNORE';
231 my $oldAutoCommit = $FS::UID::AutoCommit;
232 local $FS::UID::AutoCommit = 0;
235 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
236 #make it more of a base class than a mixin... :)
237 if ( $options{'cust_location'}
238 && ( ! $self->locationnum || $self->locationnum == -1 ) ) {
239 my $error = $options{'cust_location'}->insert;
241 $dbh->rollback if $oldAutoCommit;
242 return "inserting cust_location (transaction rolled back): $error";
244 $self->locationnum( $options{'cust_location'}->locationnum );
246 #what about on-the-fly edits? if the ui supports it?
248 my $error = $self->SUPER::insert(%options);
250 $dbh->rollback if $oldAutoCommit;
254 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
261 Delete this record from the database.
268 local $SIG{HUP} = 'IGNORE';
269 local $SIG{INT} = 'IGNORE';
270 local $SIG{QUIT} = 'IGNORE';
271 local $SIG{TERM} = 'IGNORE';
272 local $SIG{TSTP} = 'IGNORE';
273 local $SIG{PIPE} = 'IGNORE';
275 my $oldAutoCommit = $FS::UID::AutoCommit;
276 local $FS::UID::AutoCommit = 0;
279 foreach my $phone_device ( $self->phone_device ) {
280 my $error = $phone_device->delete;
282 $dbh->rollback if $oldAutoCommit;
287 my @phone_avail = qsearch('phone_avail', { 'svcnum' => $self->svcnum } );
288 foreach my $phone_avail ( @phone_avail ) {
289 $phone_avail->svcnum('');
290 my $error = $phone_avail->replace;
292 $dbh->rollback if $oldAutoCommit;
297 my $error = $self->SUPER::delete;
299 $dbh->rollback if $oldAutoCommit;
303 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
308 # the delete method can be inherited from FS::Record
310 =item replace OLD_RECORD
312 Replaces the OLD_RECORD with this one in the database. If there is an error,
313 returns the error, otherwise returns false.
320 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
327 warn "[$me] replacing $old with $new\n".
328 "\nwith options: ". Dumper(%options);
331 local $SIG{HUP} = 'IGNORE';
332 local $SIG{INT} = 'IGNORE';
333 local $SIG{QUIT} = 'IGNORE';
334 local $SIG{TERM} = 'IGNORE';
335 local $SIG{TSTP} = 'IGNORE';
336 local $SIG{PIPE} = 'IGNORE';
338 my $oldAutoCommit = $FS::UID::AutoCommit;
339 local $FS::UID::AutoCommit = 0;
342 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
343 #make it more of a base class than a mixin... :)
344 if ( $options{'cust_location'}
345 && ( ! $new->locationnum || $new->locationnum == -1 ) ) {
346 my $error = $options{'cust_location'}->insert;
348 $dbh->rollback if $oldAutoCommit;
349 return "inserting cust_location (transaction rolled back): $error";
351 $new->locationnum( $options{'cust_location'}->locationnum );
353 #what about on-the-fly edits? if the ui supports it?
355 my $error = $new->SUPER::replace($old, %options);
357 $dbh->rollback if $oldAutoCommit;
358 return $error if $error;
361 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
367 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
371 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
375 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
379 Checks all fields to make sure this is a valid phone number. If there is
380 an error, returns the error, otherwise returns false. Called by the insert
385 # the check method should currently be supplied - FS::Record contains some
386 # data checking routines
391 my $conf = new FS::Conf;
393 my $phonenum = $self->phonenum;
394 my $phonenum_check_method;
395 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
396 $phonenum =~ s/\W//g;
397 $phonenum_check_method = 'ut_alpha';
399 $phonenum =~ s/\D//g;
400 $phonenum_check_method = 'ut_number';
402 $self->phonenum($phonenum);
404 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
407 $self->ut_numbern('svcnum')
408 || $self->ut_numbern('countrycode')
409 || $self->$phonenum_check_method('phonenum')
410 || $self->ut_anything('sip_password')
411 || $self->ut_numbern('pin')
412 || $self->ut_textn('phone_name')
413 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
414 || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
415 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
416 || $self->ut_numbern('lrn')
417 || $self->ut_numbern('lnp_desired_due_date')
418 || $self->ut_numbern('lnp_due_date')
419 || $self->ut_textn('lnp_other_provider')
420 || $self->ut_textn('lnp_other_provider_account')
421 || $self->ut_enumn('lnp_status', ['','portingin','portingout','portedin','native'])
422 || $self->ut_enumn('portable', ['','Y'])
424 return $error if $error;
426 return 'Name ('. $self->phone_name.
427 ") is longer than $phone_name_max characters"
428 if $phone_name_max && length($self->phone_name) > $phone_name_max;
430 $self->countrycode(1) unless $self->countrycode;
432 unless ( length($self->sip_password) ) {
435 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
443 =item _check duplicate
445 Internal method to check for duplicate phone numers.
449 #false laziness w/svc_acct.pm's _check_duplicate.
450 sub _check_duplicate {
453 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
454 return '' if $global_unique eq 'disabled';
459 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
460 qsearch( 'svc_phone', {
461 'countrycode' => $self->countrycode,
462 'phonenum' => $self->phonenum,
465 return gettext('phonenum_in_use')
466 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
468 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
469 unless ( $part_svc ) {
470 return 'unknown svcpart '. $self->svcpart;
473 if ( @dup_ccphonenum ) {
475 my $exports = FS::part_export::export_info('svc_phone');
476 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
478 foreach my $part_export ( $part_svc->part_export ) {
480 #this will catch to the same exact export
481 my @svcparts = map { $_->svcpart } $part_export->export_svc;
483 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
488 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
489 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
490 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
491 return "duplicate phone number ".
492 $self->countrycode. ' '. $self->phonenum.
493 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
494 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
506 Checks the supplied PIN against the PIN in the database. Returns true for a
507 sucessful authentication, false if no match.
512 my($self, $check_pin) = @_;
513 length($self->pin) && $check_pin eq $self->pin;
522 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
534 my $conf = new FS::Conf;
536 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
547 Returns any FS::phone_device records associated with this service.
553 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
556 #override location_Mixin version cause we want to try the cust_pkg location
557 #in between us and cust_main
558 # XXX what to do in the unlinked case??? return a pseudo-object that returns
560 sub cust_location_or_main {
562 return $self->cust_location if $self->locationnum;
563 my $cust_pkg = $self->cust_svc->cust_pkg;
564 $cust_pkg ? $cust_pkg->cust_location_or_main : '';
569 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this
570 service. By default, "associated with" means that either the "src" or the
571 "charged_party" field of the CDR matches the "phonenum" field of the service.
575 Accepts the following options:
577 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
579 =item status => "" (or "done"): Return only CDRs with that processing status.
581 =item inbound => 1: Return CDRs for inbound calls. With "status", will filter
582 on inbound processing status.
584 =item default_prefix => "XXX": Also accept the phone number of the service prepended
585 with the chosen prefix.
587 =item disable_src => 1: Only match on "charged_party", not "src".
589 =item by_svcnum: not supported for svc_phone
596 my($self, %options) = @_;
601 if ( $options{'inbound'} ) {
603 if ( exists($options{'status'}) ) {
604 # must be 'done' or ''
605 my $sq = 'EXISTS ( SELECT 1 FROM cdr_termination '.
606 'WHERE cdr.acctid = cdr_termination.acctid '.
607 'AND cdr_termination.status = \'done\' '.
608 'AND cdr_termination.termpart = 1 )';
609 if ( $options{'status'} eq 'done' ) {
612 elsif ($options{'status'} eq '' ) {
613 push @where, "NOT $sq";
616 warn "invalid status: $options{'status'} (ignored)\n";
621 @fields = ( 'charged_party' );
622 push @fields, 'src' if !$options{'disable_src'};
623 $hash{'freesidestatus'} = $options{'status'}
624 if exists($options{'status'});
627 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
629 my $number = $self->phonenum;
631 my $prefix = $options{'default_prefix'};
633 my @orwhere = map " $_ = '$number' ", @fields;
634 push @orwhere, map " $_ = '$prefix$number' ", @fields
636 if ( $prefix =~ /^\+(\d+)$/ ) {
637 push @orwhere, map " $_ = '$1$number' ", @fields
640 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
642 if ( $options{'begin'} ) {
643 push @where, 'startdate >= '. $options{'begin'};
645 if ( $options{'end'} ) {
646 push @where, 'startdate < '. $options{'end'};
649 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
655 'extra_sql' => $extra_sql,
656 'order_by' => "ORDER BY startdate $for_update",
668 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
669 L<FS::cust_pkg>, schema.html from the base documentation.