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 Forwarding destination
88 Email address for virtual fax (fax-to-email) services
92 LNP Status (can be null, native, portedin, portingin, portin-reject,
93 portingout, portout-reject)
99 =item lnp_desired_due_date
103 =item lnp_other_provider
105 If porting the number in or out, name of the losing or winning provider,
108 =item lnp_other_provider_account
110 Account number of other provider. See lnp_other_provider.
112 =item lnp_reject_reason
114 See lnp_status. If lnp_status is portin-reject or portout-reject, this is an
115 optional reject reason.
125 Creates a new phone number. To add the number to the database, see L<"insert">.
127 Note that this stores the hash reference, not a distinct copy of the hash it
128 points to. You can ask the object for a copy with the I<hash> method.
132 # the new method can be inherited from FS::Record, if a table method is defined
135 my %dis2 = ( disable_inventory=>1, disable_select=>1 );
137 'name' => 'Phone number',
138 'sorts' => 'phonenum',
139 'display_weight' => 60,
140 'cancel_weight' => 80,
142 'svcnum' => 'Service',
143 'countrycode' => { label => 'Country code',
145 disable_inventory => 1,
148 'phonenum' => 'Phone number',
149 'pin' => { label => 'Voicemail PIN', #'Personal Identification Number',
151 disable_inventory => 1,
154 'sip_password' => 'SIP password',
155 'phone_name' => 'Name',
156 'pbxsvc' => { label => 'PBX',
157 type => 'select-svc_pbx.html',
158 disable_inventory => 1,
159 disable_select => 1, #UI wonky, pry works otherwise
164 select_table => 'svc_domain',
165 select_key => 'svcnum',
166 select_label => 'domain',
167 disable_inventory => 1,
170 label => 'E911 location',
171 disable_inventory => 1,
174 'forwarddst' => { label => 'Forward Destination',
177 'email' => { label => 'Email',
180 'lnp_status' => { label => 'LNP Status',
181 type => 'select-lnp_status.html',
184 'lnp_reject_reason' => {
185 label => 'LNP Reject Reason',
188 'portable' => { label => 'Portable?', %dis2, },
189 'lrn' => { label => 'LRN',
190 disable_inventory => 1,
192 'lnp_desired_due_date' =>
193 { label => 'LNP Desired Due Date', %dis2 },
195 { label => 'LNP Due Date', %dis2 },
196 'lnp_other_provider' =>
197 { label => 'LNP Other Provider',
198 disable_inventory => 1,
200 'lnp_other_provider_account' =>
201 { label => 'LNP Other Provider Account #',
208 sub table { 'svc_phone'; }
210 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
212 =item search_sql STRING
214 Class method which returns an SQL fragment to search for the given string.
219 my( $class, $string ) = @_;
221 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
227 my $conf = new FS::Conf;
228 my $ccode = ( $conf->exists('default_phone_countrycode')
229 && $conf->config('default_phone_countrycode')
231 ? $conf->config('default_phone_countrycode')
234 $string =~ s/^$ccode//;
236 $class->search_sql_field('phonenum', $string );
241 Returns the phone number.
247 my $phonenum = $self->phonenum; #XXX format it better
248 my $label = $phonenum;
249 $label .= '@'.$self->domain if $self->domsvc;
250 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
256 Adds this phone number to the database. If there is an error, returns the
257 error, otherwise returns false.
266 warn "[$me] insert called on $self: ". Dumper($self).
267 "\nwith options: ". Dumper(%options);
270 local $SIG{HUP} = 'IGNORE';
271 local $SIG{INT} = 'IGNORE';
272 local $SIG{QUIT} = 'IGNORE';
273 local $SIG{TERM} = 'IGNORE';
274 local $SIG{TSTP} = 'IGNORE';
275 local $SIG{PIPE} = 'IGNORE';
277 my $oldAutoCommit = $FS::UID::AutoCommit;
278 local $FS::UID::AutoCommit = 0;
281 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
282 #make it more of a base class than a mixin... :)
283 if ( $options{'cust_location'}
284 && ( ! $self->locationnum || $self->locationnum == -1 ) ) {
285 my $error = $options{'cust_location'}->insert;
287 $dbh->rollback if $oldAutoCommit;
288 return "inserting cust_location (transaction rolled back): $error";
290 $self->locationnum( $options{'cust_location'}->locationnum );
292 #what about on-the-fly edits? if the ui supports it?
294 my $error = $self->SUPER::insert(%options);
296 $dbh->rollback if $oldAutoCommit;
300 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
307 Delete this record from the database.
314 local $SIG{HUP} = 'IGNORE';
315 local $SIG{INT} = 'IGNORE';
316 local $SIG{QUIT} = 'IGNORE';
317 local $SIG{TERM} = 'IGNORE';
318 local $SIG{TSTP} = 'IGNORE';
319 local $SIG{PIPE} = 'IGNORE';
321 my $oldAutoCommit = $FS::UID::AutoCommit;
322 local $FS::UID::AutoCommit = 0;
325 foreach my $phone_device ( $self->phone_device ) {
326 my $error = $phone_device->delete;
328 $dbh->rollback if $oldAutoCommit;
333 my @phone_avail = qsearch('phone_avail', { 'svcnum' => $self->svcnum } );
334 foreach my $phone_avail ( @phone_avail ) {
335 $phone_avail->svcnum('');
336 my $error = $phone_avail->replace;
338 $dbh->rollback if $oldAutoCommit;
343 my $error = $self->SUPER::delete;
345 $dbh->rollback if $oldAutoCommit;
349 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
354 # the delete method can be inherited from FS::Record
356 =item replace OLD_RECORD
358 Replaces the OLD_RECORD with this one in the database. If there is an error,
359 returns the error, otherwise returns false.
366 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
373 warn "[$me] replacing $old with $new\n".
374 "\nwith options: ". Dumper(%options);
377 local $SIG{HUP} = 'IGNORE';
378 local $SIG{INT} = 'IGNORE';
379 local $SIG{QUIT} = 'IGNORE';
380 local $SIG{TERM} = 'IGNORE';
381 local $SIG{TSTP} = 'IGNORE';
382 local $SIG{PIPE} = 'IGNORE';
384 my $oldAutoCommit = $FS::UID::AutoCommit;
385 local $FS::UID::AutoCommit = 0;
388 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
389 #make it more of a base class than a mixin... :)
390 if ( $options{'cust_location'}
391 && ( ! $new->locationnum || $new->locationnum == -1 ) ) {
392 my $error = $options{'cust_location'}->insert;
394 $dbh->rollback if $oldAutoCommit;
395 return "inserting cust_location (transaction rolled back): $error";
397 $new->locationnum( $options{'cust_location'}->locationnum );
399 #what about on-the-fly edits? if the ui supports it?
401 # LNP data validation
402 return 'Invalid LNP status' # if someone does really stupid stuff
403 if ( ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portingin')
404 || ($old->lnp_status eq 'portout-reject' && $new->lnp_status eq 'portingin')
405 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'portingout')
406 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'native')
407 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'native')
408 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'portingout')
409 || ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portin-reject')
412 my $error = $new->SUPER::replace($old, %options);
414 $dbh->rollback if $oldAutoCommit;
415 return $error if $error;
418 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
424 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
428 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
432 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
436 Checks all fields to make sure this is a valid phone number. If there is
437 an error, returns the error, otherwise returns false. Called by the insert
442 # the check method should currently be supplied - FS::Record contains some
443 # data checking routines
448 my $conf = new FS::Conf;
450 my $phonenum = $self->phonenum;
451 my $phonenum_check_method;
452 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
453 $phonenum =~ s/\W//g;
454 $phonenum_check_method = 'ut_alpha';
456 $phonenum =~ s/\D//g;
457 $phonenum_check_method = 'ut_number';
459 $self->phonenum($phonenum);
461 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
464 $self->ut_numbern('svcnum')
465 || $self->ut_numbern('countrycode')
466 || $self->$phonenum_check_method('phonenum')
467 || $self->ut_anything('sip_password')
468 || $self->ut_numbern('pin')
469 || $self->ut_textn('phone_name')
470 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
471 || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
472 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
473 || $self->ut_numbern('forwarddst')
474 || $self->ut_textn('email')
475 || $self->ut_numbern('lrn')
476 || $self->ut_numbern('lnp_desired_due_date')
477 || $self->ut_numbern('lnp_due_date')
478 || $self->ut_textn('lnp_other_provider')
479 || $self->ut_textn('lnp_other_provider_account')
480 || $self->ut_enumn('lnp_status', ['','portingin','portingout','portedin',
481 'native', 'portin-reject', 'portout-reject'])
482 || $self->ut_enumn('portable', ['','Y'])
483 || $self->ut_textn('lnp_reject_reason')
485 return $error if $error;
487 # LNP data validation
488 return 'Cannot set LNP fields: no LNP in progress'
489 if ( ($self->lnp_desired_due_date || $self->lnp_due_date
490 || $self->lnp_other_provider || $self->lnp_other_provider_account
491 || $self->lnp_reject_reason)
492 && (!$self->lnp_status || $self->lnp_status eq 'native') );
493 return 'Cannot set LNP reject reason: no LNP in progress or status is not reject'
494 if ($self->lnp_reject_reason && (!$self->lnp_status
495 || $self->lnp_status !~ /^port(in|out)-reject$/) );
496 return 'Cannot port-out a non-portable number'
497 if (!$self->portable && $self->lnp_status eq 'portingout');
500 return 'Name ('. $self->phone_name.
501 ") is longer than $phone_name_max characters"
502 if $phone_name_max && length($self->phone_name) > $phone_name_max;
504 $self->countrycode(1) unless $self->countrycode;
506 unless ( length($self->sip_password) ) {
509 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
517 =item _check duplicate
519 Internal method to check for duplicate phone numers.
523 #false laziness w/svc_acct.pm's _check_duplicate.
524 sub _check_duplicate {
527 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
528 return '' if $global_unique eq 'disabled';
533 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
534 qsearch( 'svc_phone', {
535 'countrycode' => $self->countrycode,
536 'phonenum' => $self->phonenum,
539 return gettext('phonenum_in_use')
540 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
542 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
543 unless ( $part_svc ) {
544 return 'unknown svcpart '. $self->svcpart;
547 if ( @dup_ccphonenum ) {
549 my $exports = FS::part_export::export_info('svc_phone');
550 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
552 foreach my $part_export ( $part_svc->part_export ) {
554 #this will catch to the same exact export
555 my @svcparts = map { $_->svcpart } $part_export->export_svc;
557 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
562 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
563 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
564 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
565 return "duplicate phone number ".
566 $self->countrycode. ' '. $self->phonenum.
567 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
568 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
580 Checks the supplied PIN against the PIN in the database. Returns true for a
581 sucessful authentication, false if no match.
586 my($self, $check_pin) = @_;
587 length($self->pin) && $check_pin eq $self->pin;
596 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
608 my $conf = new FS::Conf;
610 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
621 Returns any FS::phone_device records associated with this service.
627 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
630 #override location_Mixin version cause we want to try the cust_pkg location
631 #in between us and cust_main
632 # XXX what to do in the unlinked case??? return a pseudo-object that returns
634 sub cust_location_or_main {
636 return $self->cust_location if $self->locationnum;
637 my $cust_pkg = $self->cust_svc->cust_pkg;
638 $cust_pkg ? $cust_pkg->cust_location_or_main : '';
643 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this
644 service. By default, "associated with" means that either the "src" or the
645 "charged_party" field of the CDR matches the "phonenum" field of the service.
649 Accepts the following options:
651 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
653 =item status => "" (or "done"): Return only CDRs with that processing status.
655 =item inbound => 1: Return CDRs for inbound calls. With "status", will filter
656 on inbound processing status.
658 =item default_prefix => "XXX": Also accept the phone number of the service prepended
659 with the chosen prefix.
661 =item disable_src => 1: Only match on "charged_party", not "src".
663 =item by_svcnum: not supported for svc_phone
670 my($self, %options) = @_;
675 if ( $options{'inbound'} ) {
677 if ( exists($options{'status'}) ) {
678 # must be 'done' or ''
679 my $sq = 'EXISTS ( SELECT 1 FROM cdr_termination '.
680 'WHERE cdr.acctid = cdr_termination.acctid '.
681 'AND cdr_termination.status = \'done\' '.
682 'AND cdr_termination.termpart = 1 )';
683 if ( $options{'status'} eq 'done' ) {
686 elsif ($options{'status'} eq '' ) {
687 push @where, "NOT $sq";
690 warn "invalid status: $options{'status'} (ignored)\n";
695 @fields = ( 'charged_party' );
696 push @fields, 'src' if !$options{'disable_src'};
697 $hash{'freesidestatus'} = $options{'status'}
698 if exists($options{'status'});
701 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
703 my $number = $self->phonenum;
705 my $prefix = $options{'default_prefix'};
707 my @orwhere = map " $_ = '$number' ", @fields;
708 push @orwhere, map " $_ = '$prefix$number' ", @fields
710 if ( $prefix =~ /^\+(\d+)$/ ) {
711 push @orwhere, map " $_ = '$1$number' ", @fields
714 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
716 if ( $options{'begin'} ) {
717 push @where, 'startdate >= '. $options{'begin'};
719 if ( $options{'end'} ) {
720 push @where, 'startdate < '. $options{'end'};
723 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
729 'extra_sql' => $extra_sql,
730 'order_by' => "ORDER BY startdate $for_update",
742 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
743 L<FS::cust_pkg>, schema.html from the base documentation.