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 my $conf = new FS::Conf;
223 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
229 my $ccode = ( $conf->exists('default_phone_countrycode')
230 && $conf->config('default_phone_countrycode')
232 ? $conf->config('default_phone_countrycode')
235 $string =~ s/^$ccode//;
237 $class->search_sql_field('phonenum', $string );
242 Returns the phone number.
248 my $phonenum = $self->phonenum; #XXX format it better
249 my $label = $phonenum;
250 $label .= '@'.$self->domain if $self->domsvc;
251 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
257 Adds this phone number to the database. If there is an error, returns the
258 error, otherwise returns false.
267 warn "[$me] insert called on $self: ". Dumper($self).
268 "\nwith options: ". Dumper(%options);
271 local $SIG{HUP} = 'IGNORE';
272 local $SIG{INT} = 'IGNORE';
273 local $SIG{QUIT} = 'IGNORE';
274 local $SIG{TERM} = 'IGNORE';
275 local $SIG{TSTP} = 'IGNORE';
276 local $SIG{PIPE} = 'IGNORE';
278 my $oldAutoCommit = $FS::UID::AutoCommit;
279 local $FS::UID::AutoCommit = 0;
282 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
283 #make it more of a base class than a mixin... :)
284 if ( $options{'cust_location'}
285 && ( ! $self->locationnum || $self->locationnum == -1 ) ) {
286 my $error = $options{'cust_location'}->insert;
288 $dbh->rollback if $oldAutoCommit;
289 return "inserting cust_location (transaction rolled back): $error";
291 $self->locationnum( $options{'cust_location'}->locationnum );
293 #what about on-the-fly edits? if the ui supports it?
295 my $error = $self->SUPER::insert(%options);
297 $dbh->rollback if $oldAutoCommit;
301 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
308 Delete this record from the database.
315 local $SIG{HUP} = 'IGNORE';
316 local $SIG{INT} = 'IGNORE';
317 local $SIG{QUIT} = 'IGNORE';
318 local $SIG{TERM} = 'IGNORE';
319 local $SIG{TSTP} = 'IGNORE';
320 local $SIG{PIPE} = 'IGNORE';
322 my $oldAutoCommit = $FS::UID::AutoCommit;
323 local $FS::UID::AutoCommit = 0;
326 foreach my $phone_device ( $self->phone_device ) {
327 my $error = $phone_device->delete;
329 $dbh->rollback if $oldAutoCommit;
334 my @phone_avail = qsearch('phone_avail', { 'svcnum' => $self->svcnum } );
335 foreach my $phone_avail ( @phone_avail ) {
336 $phone_avail->svcnum('');
337 my $error = $phone_avail->replace;
339 $dbh->rollback if $oldAutoCommit;
344 my $error = $self->SUPER::delete;
346 $dbh->rollback if $oldAutoCommit;
350 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
355 # the delete method can be inherited from FS::Record
357 =item replace OLD_RECORD
359 Replaces the OLD_RECORD with this one in the database. If there is an error,
360 returns the error, otherwise returns false.
367 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
374 warn "[$me] replacing $old with $new\n".
375 "\nwith options: ". Dumper(%options);
378 local $SIG{HUP} = 'IGNORE';
379 local $SIG{INT} = 'IGNORE';
380 local $SIG{QUIT} = 'IGNORE';
381 local $SIG{TERM} = 'IGNORE';
382 local $SIG{TSTP} = 'IGNORE';
383 local $SIG{PIPE} = 'IGNORE';
385 my $oldAutoCommit = $FS::UID::AutoCommit;
386 local $FS::UID::AutoCommit = 0;
389 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
390 #make it more of a base class than a mixin... :)
391 if ( $options{'cust_location'}
392 && ( ! $new->locationnum || $new->locationnum == -1 ) ) {
393 my $error = $options{'cust_location'}->insert;
395 $dbh->rollback if $oldAutoCommit;
396 return "inserting cust_location (transaction rolled back): $error";
398 $new->locationnum( $options{'cust_location'}->locationnum );
400 #what about on-the-fly edits? if the ui supports it?
402 # LNP data validation
403 return 'Invalid LNP status' # if someone does really stupid stuff
404 if ( ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portingin')
405 || ($old->lnp_status eq 'portout-reject' && $new->lnp_status eq 'portingin')
406 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'portingout')
407 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'native')
408 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'native')
409 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'portingout')
410 || ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portin-reject')
413 my $error = $new->SUPER::replace($old, %options);
415 $dbh->rollback if $oldAutoCommit;
416 return $error if $error;
419 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
425 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
429 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
433 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
437 Checks all fields to make sure this is a valid phone number. If there is
438 an error, returns the error, otherwise returns false. Called by the insert
443 # the check method should currently be supplied - FS::Record contains some
444 # data checking routines
449 my $conf = new FS::Conf;
451 my $phonenum = $self->phonenum;
452 my $phonenum_check_method;
453 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
454 $phonenum =~ s/\W//g;
455 $phonenum_check_method = 'ut_alpha';
457 $phonenum =~ s/\D//g;
458 $phonenum_check_method = 'ut_number';
460 $self->phonenum($phonenum);
462 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
465 $self->ut_numbern('svcnum')
466 || $self->ut_numbern('countrycode')
467 || $self->$phonenum_check_method('phonenum')
468 || $self->ut_anything('sip_password')
469 || $self->ut_numbern('pin')
470 || $self->ut_textn('phone_name')
471 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
472 || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
473 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
474 || $self->ut_numbern('forwarddst')
475 || $self->ut_textn('email')
476 || $self->ut_numbern('lrn')
477 || $self->ut_numbern('lnp_desired_due_date')
478 || $self->ut_numbern('lnp_due_date')
479 || $self->ut_textn('lnp_other_provider')
480 || $self->ut_textn('lnp_other_provider_account')
481 || $self->ut_enumn('lnp_status', ['','portingin','portingout','portedin',
482 'native', 'portin-reject', 'portout-reject'])
483 || $self->ut_enumn('portable', ['','Y'])
484 || $self->ut_textn('lnp_reject_reason')
486 return $error if $error;
488 # LNP data validation
489 return 'Cannot set LNP fields: no LNP in progress'
490 if ( ($self->lnp_desired_due_date || $self->lnp_due_date
491 || $self->lnp_other_provider || $self->lnp_other_provider_account
492 || $self->lnp_reject_reason)
493 && (!$self->lnp_status || $self->lnp_status eq 'native') );
494 return 'Cannot set LNP reject reason: no LNP in progress or status is not reject'
495 if ($self->lnp_reject_reason && (!$self->lnp_status
496 || $self->lnp_status !~ /^port(in|out)-reject$/) );
497 return 'Cannot port-out a non-portable number'
498 if (!$self->portable && $self->lnp_status eq 'portingout');
501 return 'Name ('. $self->phone_name.
502 ") is longer than $phone_name_max characters"
503 if $phone_name_max && length($self->phone_name) > $phone_name_max;
505 $self->countrycode(1) unless $self->countrycode;
507 unless ( length($self->pin) ) {
508 my $random_pin = $conf->config('svc_phone-random_pin');
509 if ( $random_pin =~ /^\d+$/ ) {
511 join('', map int(rand(10)), 0..($random_pin-1))
516 unless ( length($self->sip_password) ) { # option for this?
519 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
527 =item _check duplicate
529 Internal method to check for duplicate phone numers.
533 #false laziness w/svc_acct.pm's _check_duplicate.
534 sub _check_duplicate {
537 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
538 return '' if $global_unique eq 'disabled';
543 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
544 qsearch( 'svc_phone', {
545 'countrycode' => $self->countrycode,
546 'phonenum' => $self->phonenum,
549 return gettext('phonenum_in_use')
550 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
552 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
553 unless ( $part_svc ) {
554 return 'unknown svcpart '. $self->svcpart;
557 if ( @dup_ccphonenum ) {
559 my $exports = FS::part_export::export_info('svc_phone');
560 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
562 foreach my $part_export ( $part_svc->part_export ) {
564 #this will catch to the same exact export
565 my @svcparts = map { $_->svcpart } $part_export->export_svc;
567 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
572 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
573 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
574 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
575 return "duplicate phone number ".
576 $self->countrycode. ' '. $self->phonenum.
577 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
578 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
590 Checks the supplied PIN against the PIN in the database. Returns true for a
591 sucessful authentication, false if no match.
596 my($self, $check_pin) = @_;
597 length($self->pin) && $check_pin eq $self->pin;
606 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
618 my $conf = new FS::Conf;
620 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
631 Returns any FS::phone_device records associated with this service.
637 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
640 #override location_Mixin version cause we want to try the cust_pkg location
641 #in between us and cust_main
642 # XXX what to do in the unlinked case??? return a pseudo-object that returns
644 sub cust_location_or_main {
646 return $self->cust_location if $self->locationnum;
647 my $cust_pkg = $self->cust_svc->cust_pkg;
648 $cust_pkg ? $cust_pkg->cust_location_or_main : '';
653 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this
654 service. By default, "associated with" means that either the "src" or the
655 "charged_party" field of the CDR matches the "phonenum" field of the service.
659 Accepts the following options:
661 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
663 =item status => "" (or "processing-tiered", "done"): Return only CDRs with that processing status.
665 =item inbound => 1: Return CDRs for inbound calls. With "status", will filter
666 on inbound processing status.
668 =item default_prefix => "XXX": Also accept the phone number of the service prepended
669 with the chosen prefix.
671 =item begin, end: Start and end of a date range, as unix timestamp.
673 =item cdrtypenum: Only return CDRs with this type number.
675 =item disable_src => 1: Only match on "charged_party", not "src".
677 =item by_svcnum: not supported for svc_phone
684 my($self, %options) = @_;
689 if ( $options{'inbound'} ) {
692 if ( exists($options{'status'}) ) {
693 my $status = $options{'status'};
695 push @where, 'EXISTS ( SELECT 1 FROM cdr_termination '.
696 'WHERE cdr.acctid = cdr_termination.acctid '.
697 "AND cdr_termination.status = '$status' ". #quoting kludge
698 'AND cdr_termination.termpart = 1 )';
700 push @where, 'NOT EXISTS ( SELECT 1 FROM cdr_termination '.
701 'WHERE cdr.acctid = cdr_termination.acctid '.
702 'AND cdr_termination.termpart = 1 )';
708 @fields = ( 'charged_party' );
709 push @fields, 'src' if !$options{'disable_src'};
710 $hash{'freesidestatus'} = $options{'status'}
711 if exists($options{'status'});
714 if ($options{'cdrtypenum'}) {
715 $hash{'cdrtypenum'} = $options{'cdrtypenum'};
718 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
720 my $number = $self->phonenum;
722 my $prefix = $options{'default_prefix'};
724 my @orwhere = map " $_ = '$number' ", @fields;
725 push @orwhere, map " $_ = '$prefix$number' ", @fields
726 if defined($prefix) && length($prefix);
727 if ( $prefix && $prefix =~ /^\+(\d+)$/ ) {
728 push @orwhere, map " $_ = '$1$number' ", @fields
731 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
733 if ( $options{'begin'} ) {
734 push @where, 'startdate >= '. $options{'begin'};
736 if ( $options{'end'} ) {
737 push @where, 'startdate < '. $options{'end'};
740 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
746 'extra_sql' => $extra_sql,
747 'order_by' => $options{'billsec_sum'} ? '' : "ORDER BY startdate $for_update",
748 'select' => $options{'billsec_sum'} ? 'sum(billsec) as billsec_sum' : '*',
760 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
761 L<FS::cust_pkg>, schema.html from the base documentation.