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
88 LNP Status (can be null, native, portedin, portingin, portin-reject,
89 portingout, portout-reject)
95 =item lnp_desired_due_date
99 =item lnp_other_provider
101 If porting the number in or out, name of the losing or winning provider,
104 =item lnp_other_provider_account
106 Account number of other provider. See lnp_other_provider.
108 =item lnp_reject_reason
110 See lnp_status. If lnp_status is portin-reject or portout-reject, this is an
111 optional reject reason.
121 Creates a new phone number. To add the number to the database, see L<"insert">.
123 Note that this stores the hash reference, not a distinct copy of the hash it
124 points to. You can ask the object for a copy with the I<hash> method.
128 # the new method can be inherited from FS::Record, if a table method is defined
131 my %dis2 = ( disable_inventory=>1, disable_select=>1 );
133 'name' => 'Phone number',
134 'sorts' => 'phonenum',
135 'display_weight' => 60,
136 'cancel_weight' => 80,
138 'svcnum' => 'Service',
139 'countrycode' => { label => 'Country code',
141 disable_inventory => 1,
144 'phonenum' => 'Phone number',
145 'pin' => { label => 'Voicemail PIN', #'Personal Identification Number',
147 disable_inventory => 1,
150 'sip_password' => 'SIP password',
151 'phone_name' => 'Name',
152 'pbxsvc' => { label => 'PBX',
153 type => 'select-svc_pbx.html',
154 disable_inventory => 1,
155 disable_select => 1, #UI wonky, pry works otherwise
160 select_table => 'svc_domain',
161 select_key => 'svcnum',
162 select_label => 'domain',
163 disable_inventory => 1,
166 label => 'E911 location',
167 disable_inventory => 1,
170 'route' => { label => 'Route',
173 'lnp_status' => { label => 'LNP Status',
174 type => 'select-lnp_status.html',
177 'lnp_reject_reason' => {
178 label => 'LNP Reject Reason',
181 'portable' => { label => 'Portable?', %dis2, },
182 'lrn' => { label => 'LRN',
183 disable_inventory => 1,
185 'lnp_desired_due_date' =>
186 { label => 'LNP Desired Due Date', %dis2 },
188 { label => 'LNP Due Date', %dis2 },
189 'lnp_other_provider' =>
190 { label => 'LNP Other Provider',
191 disable_inventory => 1,
193 'lnp_other_provider_account' =>
194 { label => 'LNP Other Provider Account #',
201 sub table { 'svc_phone'; }
203 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
205 =item search_sql STRING
207 Class method which returns an SQL fragment to search for the given string.
212 my( $class, $string ) = @_;
214 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
220 my $conf = new FS::Conf;
221 my $ccode = ( $conf->exists('default_phone_countrycode')
222 && $conf->config('default_phone_countrycode')
224 ? $conf->config('default_phone_countrycode')
227 $string =~ s/^$ccode//;
229 $class->search_sql_field('phonenum', $string );
234 Returns the phone number.
240 my $phonenum = $self->phonenum; #XXX format it better
241 my $label = $phonenum;
242 $label .= '@'.$self->domain if $self->domsvc;
243 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
249 Adds this phone number to the database. If there is an error, returns the
250 error, otherwise returns false.
259 warn "[$me] insert called on $self: ". Dumper($self).
260 "\nwith options: ". Dumper(%options);
263 local $SIG{HUP} = 'IGNORE';
264 local $SIG{INT} = 'IGNORE';
265 local $SIG{QUIT} = 'IGNORE';
266 local $SIG{TERM} = 'IGNORE';
267 local $SIG{TSTP} = 'IGNORE';
268 local $SIG{PIPE} = 'IGNORE';
270 my $oldAutoCommit = $FS::UID::AutoCommit;
271 local $FS::UID::AutoCommit = 0;
274 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
275 #make it more of a base class than a mixin... :)
276 if ( $options{'cust_location'}
277 && ( ! $self->locationnum || $self->locationnum == -1 ) ) {
278 my $error = $options{'cust_location'}->insert;
280 $dbh->rollback if $oldAutoCommit;
281 return "inserting cust_location (transaction rolled back): $error";
283 $self->locationnum( $options{'cust_location'}->locationnum );
285 #what about on-the-fly edits? if the ui supports it?
287 my $error = $self->SUPER::insert(%options);
289 $dbh->rollback if $oldAutoCommit;
293 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
300 Delete this record from the database.
307 local $SIG{HUP} = 'IGNORE';
308 local $SIG{INT} = 'IGNORE';
309 local $SIG{QUIT} = 'IGNORE';
310 local $SIG{TERM} = 'IGNORE';
311 local $SIG{TSTP} = 'IGNORE';
312 local $SIG{PIPE} = 'IGNORE';
314 my $oldAutoCommit = $FS::UID::AutoCommit;
315 local $FS::UID::AutoCommit = 0;
318 foreach my $phone_device ( $self->phone_device ) {
319 my $error = $phone_device->delete;
321 $dbh->rollback if $oldAutoCommit;
326 my @phone_avail = qsearch('phone_avail', { 'svcnum' => $self->svcnum } );
327 foreach my $phone_avail ( @phone_avail ) {
328 $phone_avail->svcnum('');
329 my $error = $phone_avail->replace;
331 $dbh->rollback if $oldAutoCommit;
336 my $error = $self->SUPER::delete;
338 $dbh->rollback if $oldAutoCommit;
342 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
347 # the delete method can be inherited from FS::Record
349 =item replace OLD_RECORD
351 Replaces the OLD_RECORD with this one in the database. If there is an error,
352 returns the error, otherwise returns false.
359 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
366 warn "[$me] replacing $old with $new\n".
367 "\nwith options: ". Dumper(%options);
370 local $SIG{HUP} = 'IGNORE';
371 local $SIG{INT} = 'IGNORE';
372 local $SIG{QUIT} = 'IGNORE';
373 local $SIG{TERM} = 'IGNORE';
374 local $SIG{TSTP} = 'IGNORE';
375 local $SIG{PIPE} = 'IGNORE';
377 my $oldAutoCommit = $FS::UID::AutoCommit;
378 local $FS::UID::AutoCommit = 0;
381 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
382 #make it more of a base class than a mixin... :)
383 if ( $options{'cust_location'}
384 && ( ! $new->locationnum || $new->locationnum == -1 ) ) {
385 my $error = $options{'cust_location'}->insert;
387 $dbh->rollback if $oldAutoCommit;
388 return "inserting cust_location (transaction rolled back): $error";
390 $new->locationnum( $options{'cust_location'}->locationnum );
392 #what about on-the-fly edits? if the ui supports it?
394 # LNP data validation
395 return 'Invalid LNP status' # if someone does really stupid stuff
396 if ( ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portingin')
397 || ($old->lnp_status eq 'portout-reject' && $new->lnp_status eq 'portingin')
398 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'portingout')
399 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'native')
400 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'native')
401 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'portingout')
402 || ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portin-reject')
405 my $error = $new->SUPER::replace($old, %options);
407 $dbh->rollback if $oldAutoCommit;
408 return $error if $error;
411 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
417 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
421 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
425 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
429 Checks all fields to make sure this is a valid phone number. If there is
430 an error, returns the error, otherwise returns false. Called by the insert
435 # the check method should currently be supplied - FS::Record contains some
436 # data checking routines
441 my $conf = new FS::Conf;
443 my $phonenum = $self->phonenum;
444 my $phonenum_check_method;
445 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
446 $phonenum =~ s/\W//g;
447 $phonenum_check_method = 'ut_alpha';
449 $phonenum =~ s/\D//g;
450 $phonenum_check_method = 'ut_number';
452 $self->phonenum($phonenum);
454 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
457 $self->ut_numbern('svcnum')
458 || $self->ut_numbern('countrycode')
459 || $self->$phonenum_check_method('phonenum')
460 || $self->ut_anything('sip_password')
461 || $self->ut_numbern('pin')
462 || $self->ut_textn('phone_name')
463 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
464 || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
465 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
466 || $self->ut_textn('route')
467 || $self->ut_numbern('lrn')
468 || $self->ut_numbern('lnp_desired_due_date')
469 || $self->ut_numbern('lnp_due_date')
470 || $self->ut_textn('lnp_other_provider')
471 || $self->ut_textn('lnp_other_provider_account')
472 || $self->ut_enumn('lnp_status', ['','portingin','portingout','portedin',
473 'native', 'portin-reject', 'portout-reject'])
474 || $self->ut_enumn('portable', ['','Y'])
475 || $self->ut_textn('lnp_reject_reason')
477 return $error if $error;
479 # LNP data validation
480 return 'Cannot set LNP fields: no LNP in progress'
481 if ( ($self->lnp_desired_due_date || $self->lnp_due_date
482 || $self->lnp_other_provider || $self->lnp_other_provider_account
483 || $self->lnp_reject_reason)
484 && (!$self->lnp_status || $self->lnp_status eq 'native') );
485 return 'Cannot set LNP reject reason: no LNP in progress or status is not reject'
486 if ($self->lnp_reject_reason && (!$self->lnp_status
487 || $self->lnp_status !~ /^port(in|out)-reject$/) );
488 return 'Cannot port-out a non-portable number'
489 if (!$self->portable && $self->lnp_status eq 'portingout');
492 return 'Name ('. $self->phone_name.
493 ") is longer than $phone_name_max characters"
494 if $phone_name_max && length($self->phone_name) > $phone_name_max;
496 $self->countrycode(1) unless $self->countrycode;
498 unless ( length($self->sip_password) ) {
501 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
509 =item _check duplicate
511 Internal method to check for duplicate phone numers.
515 #false laziness w/svc_acct.pm's _check_duplicate.
516 sub _check_duplicate {
519 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
520 return '' if $global_unique eq 'disabled';
525 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
526 qsearch( 'svc_phone', {
527 'countrycode' => $self->countrycode,
528 'phonenum' => $self->phonenum,
531 return gettext('phonenum_in_use')
532 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
534 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
535 unless ( $part_svc ) {
536 return 'unknown svcpart '. $self->svcpart;
539 if ( @dup_ccphonenum ) {
541 my $exports = FS::part_export::export_info('svc_phone');
542 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
544 foreach my $part_export ( $part_svc->part_export ) {
546 #this will catch to the same exact export
547 my @svcparts = map { $_->svcpart } $part_export->export_svc;
549 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
554 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
555 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
556 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
557 return "duplicate phone number ".
558 $self->countrycode. ' '. $self->phonenum.
559 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
560 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
572 Checks the supplied PIN against the PIN in the database. Returns true for a
573 sucessful authentication, false if no match.
578 my($self, $check_pin) = @_;
579 length($self->pin) && $check_pin eq $self->pin;
588 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
600 my $conf = new FS::Conf;
602 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
613 Returns any FS::phone_device records associated with this service.
619 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
622 #override location_Mixin version cause we want to try the cust_pkg location
623 #in between us and cust_main
624 # XXX what to do in the unlinked case??? return a pseudo-object that returns
626 sub cust_location_or_main {
628 return $self->cust_location if $self->locationnum;
629 my $cust_pkg = $self->cust_svc->cust_pkg;
630 $cust_pkg ? $cust_pkg->cust_location_or_main : '';
635 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this
636 service. By default, "associated with" means that either the "src" or the
637 "charged_party" field of the CDR matches the "phonenum" field of the service.
641 Accepts the following options:
643 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
645 =item status => "" (or "done"): Return only CDRs with that processing status.
647 =item inbound => 1: Return CDRs for inbound calls. With "status", will filter
648 on inbound processing status.
650 =item default_prefix => "XXX": Also accept the phone number of the service prepended
651 with the chosen prefix.
653 =item disable_src => 1: Only match on "charged_party", not "src".
655 =item by_svcnum: not supported for svc_phone
662 my($self, %options) = @_;
667 if ( $options{'inbound'} ) {
669 if ( exists($options{'status'}) ) {
670 # must be 'done' or ''
671 my $sq = 'EXISTS ( SELECT 1 FROM cdr_termination '.
672 'WHERE cdr.acctid = cdr_termination.acctid '.
673 'AND cdr_termination.status = \'done\' '.
674 'AND cdr_termination.termpart = 1 )';
675 if ( $options{'status'} eq 'done' ) {
678 elsif ($options{'status'} eq '' ) {
679 push @where, "NOT $sq";
682 warn "invalid status: $options{'status'} (ignored)\n";
687 @fields = ( 'charged_party' );
688 push @fields, 'src' if !$options{'disable_src'};
689 $hash{'freesidestatus'} = $options{'status'}
690 if exists($options{'status'});
693 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
695 my $number = $self->phonenum;
697 my $prefix = $options{'default_prefix'};
699 my @orwhere = map " $_ = '$number' ", @fields;
700 push @orwhere, map " $_ = '$prefix$number' ", @fields
702 if ( $prefix =~ /^\+(\d+)$/ ) {
703 push @orwhere, map " $_ = '$1$number' ", @fields
706 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
708 if ( $options{'begin'} ) {
709 push @where, 'startdate >= '. $options{'begin'};
711 if ( $options{'end'} ) {
712 push @where, 'startdate < '. $options{'end'};
715 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
721 'extra_sql' => $extra_sql,
722 'order_by' => "ORDER BY startdate $for_update",
734 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
735 L<FS::cust_pkg>, schema.html from the base documentation.