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 'svcnum' => 'Service',
108 'countrycode' => { label => 'Country code',
110 disable_inventory => 1,
113 'phonenum' => 'Phone number',
114 'pin' => { label => 'Voicemail PIN', #'Personal Identification Number',
116 disable_inventory => 1,
119 'sip_password' => 'SIP password',
120 'phone_name' => 'Name',
121 'pbxsvc' => { label => 'PBX',
122 type => 'select-svc_pbx.html',
123 disable_inventory => 1,
124 disable_select => 1, #UI wonky, pry works otherwise
129 select_table => 'svc_domain',
130 select_key => 'svcnum',
131 select_label => 'domain',
132 disable_inventory => 1,
135 label => 'E911 location',
136 disable_inventory => 1,
139 'lnp_status' => { label => 'LNP Status',
140 type => 'select-lnp_status.html',
143 'lnp_reject_reason' => {
144 label => 'LNP Reject Reason',
147 'portable' => { label => 'Portable?', %dis2, },
148 'lrn' => { label => 'LRN',
149 disable_inventory => 1,
151 'lnp_desired_due_date' =>
152 { label => 'LNP Desired Due Date', %dis2 },
154 { label => 'LNP Due Date', %dis2 },
155 'lnp_other_provider' =>
156 { label => 'LNP Other Provider',
157 disable_inventory => 1,
159 'lnp_other_provider_account' =>
160 { label => 'LNP Other Provider Account #',
167 sub table { 'svc_phone'; }
169 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
171 =item search_sql STRING
173 Class method which returns an SQL fragment to search for the given string.
178 my( $class, $string ) = @_;
180 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
186 my $conf = new FS::Conf;
187 my $ccode = ( $conf->exists('default_phone_countrycode')
188 && $conf->config('default_phone_countrycode')
190 ? $conf->config('default_phone_countrycode')
193 $string =~ s/^$ccode//;
195 $class->search_sql_field('phonenum', $string );
200 Returns the phone number.
206 my $phonenum = $self->phonenum; #XXX format it better
207 my $label = $phonenum;
208 $label .= '@'.$self->domain if $self->domsvc;
209 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
215 Adds this phone number to the database. If there is an error, returns the
216 error, otherwise returns false.
225 warn "[$me] insert called on $self: ". Dumper($self).
226 "\nwith options: ". Dumper(%options);
229 local $SIG{HUP} = 'IGNORE';
230 local $SIG{INT} = 'IGNORE';
231 local $SIG{QUIT} = 'IGNORE';
232 local $SIG{TERM} = 'IGNORE';
233 local $SIG{TSTP} = 'IGNORE';
234 local $SIG{PIPE} = 'IGNORE';
236 my $oldAutoCommit = $FS::UID::AutoCommit;
237 local $FS::UID::AutoCommit = 0;
240 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
241 #make it more of a base class than a mixin... :)
242 if ( $options{'cust_location'}
243 && ( ! $self->locationnum || $self->locationnum == -1 ) ) {
244 my $error = $options{'cust_location'}->insert;
246 $dbh->rollback if $oldAutoCommit;
247 return "inserting cust_location (transaction rolled back): $error";
249 $self->locationnum( $options{'cust_location'}->locationnum );
251 #what about on-the-fly edits? if the ui supports it?
253 my $error = $self->SUPER::insert(%options);
255 $dbh->rollback if $oldAutoCommit;
259 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
266 Delete this record from the database.
273 local $SIG{HUP} = 'IGNORE';
274 local $SIG{INT} = 'IGNORE';
275 local $SIG{QUIT} = 'IGNORE';
276 local $SIG{TERM} = 'IGNORE';
277 local $SIG{TSTP} = 'IGNORE';
278 local $SIG{PIPE} = 'IGNORE';
280 my $oldAutoCommit = $FS::UID::AutoCommit;
281 local $FS::UID::AutoCommit = 0;
284 foreach my $phone_device ( $self->phone_device ) {
285 my $error = $phone_device->delete;
287 $dbh->rollback if $oldAutoCommit;
292 my @phone_avail = qsearch('phone_avail', { 'svcnum' => $self->svcnum } );
293 foreach my $phone_avail ( @phone_avail ) {
294 $phone_avail->svcnum('');
295 my $error = $phone_avail->replace;
297 $dbh->rollback if $oldAutoCommit;
302 my $error = $self->SUPER::delete;
304 $dbh->rollback if $oldAutoCommit;
308 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
313 # the delete method can be inherited from FS::Record
315 =item replace OLD_RECORD
317 Replaces the OLD_RECORD with this one in the database. If there is an error,
318 returns the error, otherwise returns false.
325 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
332 warn "[$me] replacing $old with $new\n".
333 "\nwith options: ". Dumper(%options);
336 local $SIG{HUP} = 'IGNORE';
337 local $SIG{INT} = 'IGNORE';
338 local $SIG{QUIT} = 'IGNORE';
339 local $SIG{TERM} = 'IGNORE';
340 local $SIG{TSTP} = 'IGNORE';
341 local $SIG{PIPE} = 'IGNORE';
343 my $oldAutoCommit = $FS::UID::AutoCommit;
344 local $FS::UID::AutoCommit = 0;
347 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
348 #make it more of a base class than a mixin... :)
349 if ( $options{'cust_location'}
350 && ( ! $new->locationnum || $new->locationnum == -1 ) ) {
351 my $error = $options{'cust_location'}->insert;
353 $dbh->rollback if $oldAutoCommit;
354 return "inserting cust_location (transaction rolled back): $error";
356 $new->locationnum( $options{'cust_location'}->locationnum );
358 #what about on-the-fly edits? if the ui supports it?
360 # LNP data validation
361 return 'Invalid LNP status' # if someone does really stupid stuff
362 if ( ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portingin')
363 || ($old->lnp_status eq 'portout-reject' && $new->lnp_status eq 'portingin')
364 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'portingout')
365 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'native')
366 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'native')
367 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'portingout')
368 || ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portin-reject')
371 my $error = $new->SUPER::replace($old, %options);
373 $dbh->rollback if $oldAutoCommit;
374 return $error if $error;
377 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
383 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
387 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
391 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
395 Checks all fields to make sure this is a valid phone number. If there is
396 an error, returns the error, otherwise returns false. Called by the insert
401 # the check method should currently be supplied - FS::Record contains some
402 # data checking routines
407 my $conf = new FS::Conf;
409 my $phonenum = $self->phonenum;
410 my $phonenum_check_method;
411 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
412 $phonenum =~ s/\W//g;
413 $phonenum_check_method = 'ut_alpha';
415 $phonenum =~ s/\D//g;
416 $phonenum_check_method = 'ut_number';
418 $self->phonenum($phonenum);
420 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
423 $self->ut_numbern('svcnum')
424 || $self->ut_numbern('countrycode')
425 || $self->$phonenum_check_method('phonenum')
426 || $self->ut_anything('sip_password')
427 || $self->ut_numbern('pin')
428 || $self->ut_textn('phone_name')
429 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
430 || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
431 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
432 || $self->ut_numbern('lrn')
433 || $self->ut_numbern('lnp_desired_due_date')
434 || $self->ut_numbern('lnp_due_date')
435 || $self->ut_textn('lnp_other_provider')
436 || $self->ut_textn('lnp_other_provider_account')
437 || $self->ut_enumn('lnp_status', ['','portingin','portingout','portedin',
438 'native', 'portin-reject', 'portout-reject'])
439 || $self->ut_enumn('portable', ['','Y'])
440 || $self->ut_textn('lnp_reject_reason')
442 return $error if $error;
444 # LNP data validation
445 return 'Cannot set LNP fields: no LNP in progress'
446 if ( ($self->lnp_desired_due_date || $self->lnp_due_date
447 || $self->lnp_other_provider || $self->lnp_other_provider_account
448 || $self->lnp_reject_reason)
449 && (!$self->lnp_status || $self->lnp_status eq 'native') );
450 return 'Cannot set LNP reject reason: no LNP in progress or status is not reject'
451 if ($self->lnp_reject_reason && (!$self->lnp_status
452 || $self->lnp_status !~ /^port(in|out)-reject$/) );
453 return 'Cannot port-out a non-portable number'
454 if (!$self->portable && $self->lnp_status eq 'portingout');
457 return 'Name ('. $self->phone_name.
458 ") is longer than $phone_name_max characters"
459 if $phone_name_max && length($self->phone_name) > $phone_name_max;
461 $self->countrycode(1) unless $self->countrycode;
463 unless ( length($self->sip_password) ) {
466 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
474 =item _check duplicate
476 Internal method to check for duplicate phone numers.
480 #false laziness w/svc_acct.pm's _check_duplicate.
481 sub _check_duplicate {
484 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
485 return '' if $global_unique eq 'disabled';
490 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
491 qsearch( 'svc_phone', {
492 'countrycode' => $self->countrycode,
493 'phonenum' => $self->phonenum,
496 return gettext('phonenum_in_use')
497 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
499 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
500 unless ( $part_svc ) {
501 return 'unknown svcpart '. $self->svcpart;
504 if ( @dup_ccphonenum ) {
506 my $exports = FS::part_export::export_info('svc_phone');
507 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
509 foreach my $part_export ( $part_svc->part_export ) {
511 #this will catch to the same exact export
512 my @svcparts = map { $_->svcpart } $part_export->export_svc;
514 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
519 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
520 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
521 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
522 return "duplicate phone number ".
523 $self->countrycode. ' '. $self->phonenum.
524 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
525 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
537 Checks the supplied PIN against the PIN in the database. Returns true for a
538 sucessful authentication, false if no match.
543 my($self, $check_pin) = @_;
544 length($self->pin) && $check_pin eq $self->pin;
553 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
565 my $conf = new FS::Conf;
567 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
578 Returns any FS::phone_device records associated with this service.
584 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
587 #override location_Mixin version cause we want to try the cust_pkg location
588 #in between us and cust_main
589 # XXX what to do in the unlinked case??? return a pseudo-object that returns
591 sub cust_location_or_main {
593 return $self->cust_location if $self->locationnum;
594 my $cust_pkg = $self->cust_svc->cust_pkg;
595 $cust_pkg ? $cust_pkg->cust_location_or_main : '';
600 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this
601 service. By default, "associated with" means that either the "src" or the
602 "charged_party" field of the CDR matches the "phonenum" field of the service.
606 Accepts the following options:
608 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
610 =item status => "" (or "done"): Return only CDRs with that processing status.
612 =item inbound => 1: Return CDRs for inbound calls. With "status", will filter
613 on inbound processing status.
615 =item default_prefix => "XXX": Also accept the phone number of the service prepended
616 with the chosen prefix.
618 =item disable_src => 1: Only match on "charged_party", not "src".
620 =item by_svcnum: not supported for svc_phone
627 my($self, %options) = @_;
632 if ( $options{'inbound'} ) {
634 if ( exists($options{'status'}) ) {
635 # must be 'done' or ''
636 my $sq = 'EXISTS ( SELECT 1 FROM cdr_termination '.
637 'WHERE cdr.acctid = cdr_termination.acctid '.
638 'AND cdr_termination.status = \'done\' '.
639 'AND cdr_termination.termpart = 1 )';
640 if ( $options{'status'} eq 'done' ) {
643 elsif ($options{'status'} eq '' ) {
644 push @where, "NOT $sq";
647 warn "invalid status: $options{'status'} (ignored)\n";
652 @fields = ( 'charged_party' );
653 push @fields, 'src' if !$options{'disable_src'};
654 $hash{'freesidestatus'} = $options{'status'}
655 if exists($options{'status'});
658 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
660 my $number = $self->phonenum;
662 my $prefix = $options{'default_prefix'};
664 my @orwhere = map " $_ = '$number' ", @fields;
665 push @orwhere, map " $_ = '$prefix$number' ", @fields
667 if ( $prefix =~ /^\+(\d+)$/ ) {
668 push @orwhere, map " $_ = '$1$number' ", @fields
671 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
673 if ( $options{'begin'} ) {
674 push @where, 'startdate >= '. $options{'begin'};
676 if ( $options{'end'} ) {
677 push @where, 'startdate < '. $options{'end'};
680 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
686 'extra_sql' => $extra_sql,
687 'order_by' => "ORDER BY startdate $for_update",
699 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
700 L<FS::cust_pkg>, schema.html from the base documentation.