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
84 LNP Status (can be null, native, portedin, portingin, portin-reject,
85 portingout, portout-reject)
91 =item lnp_desired_due_date
95 =item lnp_other_provider
97 If porting the number in or out, name of the losing or winning provider,
100 =item lnp_other_provider_account
102 Account number of other provider. See lnp_other_provider.
104 =item lnp_reject_reason
106 See lnp_status. If lnp_status is portin-reject or portout-reject, this is an
107 optional reject reason.
117 Creates a new phone number. To add the number to the database, see L<"insert">.
119 Note that this stores the hash reference, not a distinct copy of the hash it
120 points to. You can ask the object for a copy with the I<hash> method.
124 # the new method can be inherited from FS::Record, if a table method is defined
127 my %dis2 = ( disable_inventory=>1, disable_select=>1 );
129 'name' => 'Phone number',
130 'sorts' => 'phonenum',
131 'display_weight' => 60,
132 'cancel_weight' => 80,
134 'svcnum' => 'Service',
135 'countrycode' => { label => 'Country code',
137 disable_inventory => 1,
140 'phonenum' => 'Phone number',
141 'pin' => { label => 'Voicemail PIN', #'Personal Identification Number',
143 disable_inventory => 1,
146 'sip_password' => 'SIP password',
147 'phone_name' => 'Name',
148 'pbxsvc' => { label => 'PBX',
149 type => 'select-svc_pbx.html',
150 disable_inventory => 1,
151 disable_select => 1, #UI wonky, pry works otherwise
156 select_table => 'svc_domain',
157 select_key => 'svcnum',
158 select_label => 'domain',
159 disable_inventory => 1,
162 label => 'E911 location',
163 disable_inventory => 1,
166 'lnp_status' => { label => 'LNP Status',
167 type => 'select-lnp_status.html',
170 'lnp_reject_reason' => {
171 label => 'LNP Reject Reason',
174 'portable' => { label => 'Portable?', %dis2, },
175 'lrn' => { label => 'LRN',
176 disable_inventory => 1,
178 'lnp_desired_due_date' =>
179 { label => 'LNP Desired Due Date', %dis2 },
181 { label => 'LNP Due Date', %dis2 },
182 'lnp_other_provider' =>
183 { label => 'LNP Other Provider',
184 disable_inventory => 1,
186 'lnp_other_provider_account' =>
187 { label => 'LNP Other Provider Account #',
194 sub table { 'svc_phone'; }
196 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
198 =item search_sql STRING
200 Class method which returns an SQL fragment to search for the given string.
205 my( $class, $string ) = @_;
207 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
213 my $conf = new FS::Conf;
214 my $ccode = ( $conf->exists('default_phone_countrycode')
215 && $conf->config('default_phone_countrycode')
217 ? $conf->config('default_phone_countrycode')
220 $string =~ s/^$ccode//;
222 $class->search_sql_field('phonenum', $string );
227 Returns the phone number.
233 my $phonenum = $self->phonenum; #XXX format it better
234 my $label = $phonenum;
235 $label .= '@'.$self->domain if $self->domsvc;
236 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
242 Adds this phone number to the database. If there is an error, returns the
243 error, otherwise returns false.
252 warn "[$me] insert called on $self: ". Dumper($self).
253 "\nwith options: ". Dumper(%options);
256 local $SIG{HUP} = 'IGNORE';
257 local $SIG{INT} = 'IGNORE';
258 local $SIG{QUIT} = 'IGNORE';
259 local $SIG{TERM} = 'IGNORE';
260 local $SIG{TSTP} = 'IGNORE';
261 local $SIG{PIPE} = 'IGNORE';
263 my $oldAutoCommit = $FS::UID::AutoCommit;
264 local $FS::UID::AutoCommit = 0;
267 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
268 #make it more of a base class than a mixin... :)
269 if ( $options{'cust_location'}
270 && ( ! $self->locationnum || $self->locationnum == -1 ) ) {
271 my $error = $options{'cust_location'}->insert;
273 $dbh->rollback if $oldAutoCommit;
274 return "inserting cust_location (transaction rolled back): $error";
276 $self->locationnum( $options{'cust_location'}->locationnum );
278 #what about on-the-fly edits? if the ui supports it?
280 my $error = $self->SUPER::insert(%options);
282 $dbh->rollback if $oldAutoCommit;
286 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
293 Delete this record from the database.
300 local $SIG{HUP} = 'IGNORE';
301 local $SIG{INT} = 'IGNORE';
302 local $SIG{QUIT} = 'IGNORE';
303 local $SIG{TERM} = 'IGNORE';
304 local $SIG{TSTP} = 'IGNORE';
305 local $SIG{PIPE} = 'IGNORE';
307 my $oldAutoCommit = $FS::UID::AutoCommit;
308 local $FS::UID::AutoCommit = 0;
311 foreach my $phone_device ( $self->phone_device ) {
312 my $error = $phone_device->delete;
314 $dbh->rollback if $oldAutoCommit;
319 my @phone_avail = qsearch('phone_avail', { 'svcnum' => $self->svcnum } );
320 foreach my $phone_avail ( @phone_avail ) {
321 $phone_avail->svcnum('');
322 my $error = $phone_avail->replace;
324 $dbh->rollback if $oldAutoCommit;
329 my $error = $self->SUPER::delete;
331 $dbh->rollback if $oldAutoCommit;
335 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
340 # the delete method can be inherited from FS::Record
342 =item replace OLD_RECORD
344 Replaces the OLD_RECORD with this one in the database. If there is an error,
345 returns the error, otherwise returns false.
352 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
359 warn "[$me] replacing $old with $new\n".
360 "\nwith options: ". Dumper(%options);
363 local $SIG{HUP} = 'IGNORE';
364 local $SIG{INT} = 'IGNORE';
365 local $SIG{QUIT} = 'IGNORE';
366 local $SIG{TERM} = 'IGNORE';
367 local $SIG{TSTP} = 'IGNORE';
368 local $SIG{PIPE} = 'IGNORE';
370 my $oldAutoCommit = $FS::UID::AutoCommit;
371 local $FS::UID::AutoCommit = 0;
374 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
375 #make it more of a base class than a mixin... :)
376 if ( $options{'cust_location'}
377 && ( ! $new->locationnum || $new->locationnum == -1 ) ) {
378 my $error = $options{'cust_location'}->insert;
380 $dbh->rollback if $oldAutoCommit;
381 return "inserting cust_location (transaction rolled back): $error";
383 $new->locationnum( $options{'cust_location'}->locationnum );
385 #what about on-the-fly edits? if the ui supports it?
387 # LNP data validation
388 return 'Invalid LNP status' # if someone does really stupid stuff
389 if ( ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portingin')
390 || ($old->lnp_status eq 'portout-reject' && $new->lnp_status eq 'portingin')
391 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'portingout')
392 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'native')
393 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'native')
394 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'portingout')
395 || ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portin-reject')
398 my $error = $new->SUPER::replace($old, %options);
400 $dbh->rollback if $oldAutoCommit;
401 return $error if $error;
404 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
410 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
414 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
418 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
422 Checks all fields to make sure this is a valid phone number. If there is
423 an error, returns the error, otherwise returns false. Called by the insert
428 # the check method should currently be supplied - FS::Record contains some
429 # data checking routines
434 my $conf = new FS::Conf;
436 my $phonenum = $self->phonenum;
437 my $phonenum_check_method;
438 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
439 $phonenum =~ s/\W//g;
440 $phonenum_check_method = 'ut_alpha';
442 $phonenum =~ s/\D//g;
443 $phonenum_check_method = 'ut_number';
445 $self->phonenum($phonenum);
447 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
450 $self->ut_numbern('svcnum')
451 || $self->ut_numbern('countrycode')
452 || $self->$phonenum_check_method('phonenum')
453 || $self->ut_anything('sip_password')
454 || $self->ut_numbern('pin')
455 || $self->ut_textn('phone_name')
456 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
457 || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
458 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
459 || $self->ut_numbern('lrn')
460 || $self->ut_numbern('lnp_desired_due_date')
461 || $self->ut_numbern('lnp_due_date')
462 || $self->ut_textn('lnp_other_provider')
463 || $self->ut_textn('lnp_other_provider_account')
464 || $self->ut_enumn('lnp_status', ['','portingin','portingout','portedin',
465 'native', 'portin-reject', 'portout-reject'])
466 || $self->ut_enumn('portable', ['','Y'])
467 || $self->ut_textn('lnp_reject_reason')
469 return $error if $error;
471 # LNP data validation
472 return 'Cannot set LNP fields: no LNP in progress'
473 if ( ($self->lnp_desired_due_date || $self->lnp_due_date
474 || $self->lnp_other_provider || $self->lnp_other_provider_account
475 || $self->lnp_reject_reason)
476 && (!$self->lnp_status || $self->lnp_status eq 'native') );
477 return 'Cannot set LNP reject reason: no LNP in progress or status is not reject'
478 if ($self->lnp_reject_reason && (!$self->lnp_status
479 || $self->lnp_status !~ /^port(in|out)-reject$/) );
480 return 'Cannot port-out a non-portable number'
481 if (!$self->portable && $self->lnp_status eq 'portingout');
484 return 'Name ('. $self->phone_name.
485 ") is longer than $phone_name_max characters"
486 if $phone_name_max && length($self->phone_name) > $phone_name_max;
488 $self->countrycode(1) unless $self->countrycode;
490 unless ( length($self->sip_password) ) {
493 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
501 =item _check duplicate
503 Internal method to check for duplicate phone numers.
507 #false laziness w/svc_acct.pm's _check_duplicate.
508 sub _check_duplicate {
511 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
512 return '' if $global_unique eq 'disabled';
517 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
518 qsearch( 'svc_phone', {
519 'countrycode' => $self->countrycode,
520 'phonenum' => $self->phonenum,
523 return gettext('phonenum_in_use')
524 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
526 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
527 unless ( $part_svc ) {
528 return 'unknown svcpart '. $self->svcpart;
531 if ( @dup_ccphonenum ) {
533 my $exports = FS::part_export::export_info('svc_phone');
534 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
536 foreach my $part_export ( $part_svc->part_export ) {
538 #this will catch to the same exact export
539 my @svcparts = map { $_->svcpart } $part_export->export_svc;
541 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
546 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
547 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
548 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
549 return "duplicate phone number ".
550 $self->countrycode. ' '. $self->phonenum.
551 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
552 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
564 Checks the supplied PIN against the PIN in the database. Returns true for a
565 sucessful authentication, false if no match.
570 my($self, $check_pin) = @_;
571 length($self->pin) && $check_pin eq $self->pin;
580 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
592 my $conf = new FS::Conf;
594 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
605 Returns any FS::phone_device records associated with this service.
611 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
614 #override location_Mixin version cause we want to try the cust_pkg location
615 #in between us and cust_main
616 # XXX what to do in the unlinked case??? return a pseudo-object that returns
618 sub cust_location_or_main {
620 return $self->cust_location if $self->locationnum;
621 my $cust_pkg = $self->cust_svc->cust_pkg;
622 $cust_pkg ? $cust_pkg->cust_location_or_main : '';
627 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this
628 service. By default, "associated with" means that either the "src" or the
629 "charged_party" field of the CDR matches the "phonenum" field of the service.
633 Accepts the following options:
635 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
637 =item status => "" (or "done"): Return only CDRs with that processing status.
639 =item inbound => 1: Return CDRs for inbound calls. With "status", will filter
640 on inbound processing status.
642 =item default_prefix => "XXX": Also accept the phone number of the service prepended
643 with the chosen prefix.
645 =item disable_src => 1: Only match on "charged_party", not "src".
647 =item by_svcnum: not supported for svc_phone
654 my($self, %options) = @_;
659 if ( $options{'inbound'} ) {
661 if ( exists($options{'status'}) ) {
662 # must be 'done' or ''
663 my $sq = 'EXISTS ( SELECT 1 FROM cdr_termination '.
664 'WHERE cdr.acctid = cdr_termination.acctid '.
665 'AND cdr_termination.status = \'done\' '.
666 'AND cdr_termination.termpart = 1 )';
667 if ( $options{'status'} eq 'done' ) {
670 elsif ($options{'status'} eq '' ) {
671 push @where, "NOT $sq";
674 warn "invalid status: $options{'status'} (ignored)\n";
679 @fields = ( 'charged_party' );
680 push @fields, 'src' if !$options{'disable_src'};
681 $hash{'freesidestatus'} = $options{'status'}
682 if exists($options{'status'});
685 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
687 my $number = $self->phonenum;
689 my $prefix = $options{'default_prefix'};
691 my @orwhere = map " $_ = '$number' ", @fields;
692 push @orwhere, map " $_ = '$prefix$number' ", @fields
694 if ( $prefix =~ /^\+(\d+)$/ ) {
695 push @orwhere, map " $_ = '$1$number' ", @fields
698 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
700 if ( $options{'begin'} ) {
701 push @where, 'startdate >= '. $options{'begin'};
703 if ( $options{'end'} ) {
704 push @where, 'startdate < '. $options{'end'};
707 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
713 'extra_sql' => $extra_sql,
714 'order_by' => "ORDER BY startdate $for_update",
726 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
727 L<FS::cust_pkg>, schema.html from the base documentation.