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->install_callback( sub {
27 $phone_name_max = $conf->config('svc_phone-phone_name-max_length');
33 FS::svc_phone - Object methods for svc_phone records
39 $record = new FS::svc_phone \%hash;
40 $record = new FS::svc_phone { 'column' => 'value' };
42 $error = $record->insert;
44 $error = $new_record->replace($old_record);
46 $error = $record->delete;
48 $error = $record->check;
50 $error = $record->suspend;
52 $error = $record->unsuspend;
54 $error = $record->cancel;
58 An FS::svc_phone object represents a phone number. FS::svc_phone inherits
59 from FS::Record. The following fields are currently supported:
73 SIM IMSI (http://en.wikipedia.org/wiki/International_mobile_subscriber_identity)
85 Optional svcnum from svc_pbx
89 Forwarding destination
93 Email address for virtual fax (fax-to-email) services
97 LNP Status (can be null, native, portedin, portingin, portin-reject,
98 portingout, portout-reject)
104 =item lnp_desired_due_date
108 =item lnp_other_provider
110 If porting the number in or out, name of the losing or winning provider,
113 =item lnp_other_provider_account
115 Account number of other provider. See lnp_other_provider.
117 =item lnp_reject_reason
119 See lnp_status. If lnp_status is portin-reject or portout-reject, this is an
120 optional reject reason.
130 Creates a new phone number. To add the number to the database, see L<"insert">.
132 Note that this stores the hash reference, not a distinct copy of the hash it
133 points to. You can ask the object for a copy with the I<hash> method.
137 # the new method can be inherited from FS::Record, if a table method is defined
140 my %dis2 = ( disable_inventory=>1, disable_select=>1 );
142 'name' => 'Phone number',
143 'sorts' => 'phonenum',
144 'display_weight' => 60,
145 'cancel_weight' => 80,
147 'svcnum' => 'Service',
148 'countrycode' => { label => 'Country code',
150 disable_inventory => 1,
153 'phonenum' => 'Phone number',
154 'sim_imsi' => 'IMSI', #http://en.wikipedia.org/wiki/International_mobile_subscriber_identity
155 'pin' => { label => 'Voicemail PIN', #'Personal Identification Number',
157 disable_inventory => 1,
160 'sip_password' => 'SIP password',
161 'phone_name' => 'Name',
162 'pbxsvc' => { label => 'PBX',
163 type => 'select-svc_pbx.html',
164 disable_inventory => 1,
165 disable_select => 1, #UI wonky, pry works otherwise
170 select_table => 'svc_domain',
171 select_key => 'svcnum',
172 select_label => 'domain',
173 disable_inventory => 1,
176 label => 'E911 location',
177 disable_inventory => 1,
180 'forwarddst' => { label => 'Forward Destination',
183 'email' => { label => 'Email',
186 'lnp_status' => { label => 'LNP Status',
187 type => 'select-lnp_status.html',
190 'lnp_reject_reason' => {
191 label => 'LNP Reject Reason',
194 'portable' => { label => 'Portable?', %dis2, },
195 'lrn' => { label => 'LRN',
196 disable_inventory => 1,
198 'lnp_desired_due_date' =>
199 { label => 'LNP Desired Due Date', %dis2 },
201 { label => 'LNP Due Date', %dis2 },
202 'lnp_other_provider' =>
203 { label => 'LNP Other Provider',
204 disable_inventory => 1,
206 'lnp_other_provider_account' =>
207 { label => 'LNP Other Provider Account #',
214 sub table { 'svc_phone'; }
216 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
218 =item search_sql STRING
220 Class method which returns an SQL fragment to search for the given string.
225 my( $class, $string ) = @_;
227 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
233 my $conf = new FS::Conf;
234 my $ccode = ( $conf->exists('default_phone_countrycode')
235 && $conf->config('default_phone_countrycode')
237 ? $conf->config('default_phone_countrycode')
240 $string =~ s/^$ccode//;
242 $class->search_sql_field('phonenum', $string );
247 Returns the phone number.
253 my $phonenum = $self->phonenum; #XXX format it better
254 my $label = $phonenum;
255 $label .= '@'.$self->domain if $self->domsvc;
256 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
262 Adds this phone number to the database. If there is an error, returns the
263 error, otherwise returns false.
272 warn "[$me] insert called on $self: ". Dumper($self).
273 "\nwith options: ". Dumper(%options);
276 local $SIG{HUP} = 'IGNORE';
277 local $SIG{INT} = 'IGNORE';
278 local $SIG{QUIT} = 'IGNORE';
279 local $SIG{TERM} = 'IGNORE';
280 local $SIG{TSTP} = 'IGNORE';
281 local $SIG{PIPE} = 'IGNORE';
283 my $oldAutoCommit = $FS::UID::AutoCommit;
284 local $FS::UID::AutoCommit = 0;
287 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
288 #make it more of a base class than a mixin... :)
289 if ( $options{'cust_location'}
290 && ( ! $self->locationnum || $self->locationnum == -1 ) ) {
291 my $error = $options{'cust_location'}->insert;
293 $dbh->rollback if $oldAutoCommit;
294 return "inserting cust_location (transaction rolled back): $error";
296 $self->locationnum( $options{'cust_location'}->locationnum );
298 #what about on-the-fly edits? if the ui supports it?
300 my $error = $self->SUPER::insert(%options);
302 $dbh->rollback if $oldAutoCommit;
306 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
313 Delete this record from the database.
320 local $SIG{HUP} = 'IGNORE';
321 local $SIG{INT} = 'IGNORE';
322 local $SIG{QUIT} = 'IGNORE';
323 local $SIG{TERM} = 'IGNORE';
324 local $SIG{TSTP} = 'IGNORE';
325 local $SIG{PIPE} = 'IGNORE';
327 my $oldAutoCommit = $FS::UID::AutoCommit;
328 local $FS::UID::AutoCommit = 0;
331 foreach my $phone_device ( $self->phone_device ) {
332 my $error = $phone_device->delete;
334 $dbh->rollback if $oldAutoCommit;
339 my @phone_avail = qsearch('phone_avail', { 'svcnum' => $self->svcnum } );
340 foreach my $phone_avail ( @phone_avail ) {
341 $phone_avail->svcnum('');
342 my $error = $phone_avail->replace;
344 $dbh->rollback if $oldAutoCommit;
349 my $error = $self->SUPER::delete;
351 $dbh->rollback if $oldAutoCommit;
355 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
360 # the delete method can be inherited from FS::Record
362 =item replace OLD_RECORD
364 Replaces the OLD_RECORD with this one in the database. If there is an error,
365 returns the error, otherwise returns false.
372 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
379 warn "[$me] replacing $old with $new\n".
380 "\nwith options: ". Dumper(%options);
383 local $SIG{HUP} = 'IGNORE';
384 local $SIG{INT} = 'IGNORE';
385 local $SIG{QUIT} = 'IGNORE';
386 local $SIG{TERM} = 'IGNORE';
387 local $SIG{TSTP} = 'IGNORE';
388 local $SIG{PIPE} = 'IGNORE';
390 my $oldAutoCommit = $FS::UID::AutoCommit;
391 local $FS::UID::AutoCommit = 0;
394 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
395 #make it more of a base class than a mixin... :)
396 if ( $options{'cust_location'}
397 && ( ! $new->locationnum || $new->locationnum == -1 ) ) {
398 my $error = $options{'cust_location'}->insert;
400 $dbh->rollback if $oldAutoCommit;
401 return "inserting cust_location (transaction rolled back): $error";
403 $new->locationnum( $options{'cust_location'}->locationnum );
405 #what about on-the-fly edits? if the ui supports it?
407 # LNP data validation
408 return 'Invalid LNP status' # if someone does really stupid stuff
409 if ( ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portingin')
410 || ($old->lnp_status eq 'portout-reject' && $new->lnp_status eq 'portingin')
411 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'portingout')
412 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'native')
413 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'native')
414 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'portingout')
415 || ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portin-reject')
418 my $error = $new->SUPER::replace($old, %options);
420 $dbh->rollback if $oldAutoCommit;
421 return $error if $error;
424 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
430 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
434 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
438 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
442 Checks all fields to make sure this is a valid phone number. If there is
443 an error, returns the error, otherwise returns false. Called by the insert
448 # the check method should currently be supplied - FS::Record contains some
449 # data checking routines
454 my $conf = new FS::Conf;
456 my $phonenum = $self->phonenum;
457 my $phonenum_check_method;
458 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
459 $phonenum =~ s/\W//g;
460 $phonenum_check_method = 'ut_alpha';
462 $phonenum =~ s/\D//g;
463 $phonenum_check_method = 'ut_number';
465 $self->phonenum($phonenum);
467 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
470 $self->ut_numbern('svcnum')
471 || $self->ut_numbern('countrycode')
472 || $self->$phonenum_check_method('phonenum')
473 || $self->ut_numbern('sim_imsi')
474 || $self->ut_anything('sip_password')
475 || $self->ut_numbern('pin')
476 || $self->ut_textn('phone_name')
477 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
478 || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
479 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
480 || $self->ut_numbern('forwarddst')
481 || $self->ut_textn('email')
482 || $self->ut_numbern('lrn')
483 || $self->ut_numbern('lnp_desired_due_date')
484 || $self->ut_numbern('lnp_due_date')
485 || $self->ut_textn('lnp_other_provider')
486 || $self->ut_textn('lnp_other_provider_account')
487 || $self->ut_enumn('lnp_status', ['','portingin','portingout','portedin',
488 'native', 'portin-reject', 'portout-reject'])
489 || $self->ut_enumn('portable', ['','Y'])
490 || $self->ut_textn('lnp_reject_reason')
492 return $error if $error;
494 return 'Illegal IMSI (not 14-15 digits)' #shorter?
495 if length($self->sim_imsi)
496 && ( length($self->sim_imsi) < 14 || length($self->sim_imsi) > 15 );
498 # LNP data validation
499 return 'Cannot set LNP fields: no LNP in progress'
500 if ( ($self->lnp_desired_due_date || $self->lnp_due_date
501 || $self->lnp_other_provider || $self->lnp_other_provider_account
502 || $self->lnp_reject_reason)
503 && (!$self->lnp_status || $self->lnp_status eq 'native') );
504 return 'Cannot set LNP reject reason: no LNP in progress or status is not reject'
505 if ($self->lnp_reject_reason && (!$self->lnp_status
506 || $self->lnp_status !~ /^port(in|out)-reject$/) );
507 return 'Cannot port-out a non-portable number'
508 if (!$self->portable && $self->lnp_status eq 'portingout');
511 return 'Name ('. $self->phone_name.
512 ") is longer than $phone_name_max characters"
513 if $phone_name_max && length($self->phone_name) > $phone_name_max;
515 $self->countrycode(1) unless $self->countrycode;
517 unless ( length($self->pin) ) {
518 my $random_pin = $conf->config('svc_phone-random_pin');
519 if ( $random_pin =~ /^\d+$/ ) {
521 join('', map int(rand(10)), 0..($random_pin-1))
526 unless ( length($self->sip_password) ) { # option for this?
529 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
537 =item _check duplicate
539 Internal method to check for duplicate phone numers.
543 #false laziness w/svc_acct.pm's _check_duplicate.
544 sub _check_duplicate {
547 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
548 return '' if $global_unique eq 'disabled';
553 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
554 qsearch( 'svc_phone', {
555 'countrycode' => $self->countrycode,
556 'phonenum' => $self->phonenum,
559 return gettext('phonenum_in_use')
560 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
562 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
563 unless ( $part_svc ) {
564 return 'unknown svcpart '. $self->svcpart;
567 if ( @dup_ccphonenum ) {
569 my $exports = FS::part_export::export_info('svc_phone');
570 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
572 foreach my $part_export ( $part_svc->part_export ) {
574 #this will catch to the same exact export
575 my @svcparts = map { $_->svcpart } $part_export->export_svc;
577 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
582 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
583 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
584 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
585 return "duplicate phone number ".
586 $self->countrycode. ' '. $self->phonenum.
587 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
588 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
600 Checks the supplied PIN against the PIN in the database. Returns true for a
601 sucessful authentication, false if no match.
606 my($self, $check_pin) = @_;
607 length($self->pin) && $check_pin eq $self->pin;
616 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
628 my $conf = new FS::Conf;
630 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
641 Returns any FS::phone_device records associated with this service.
647 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
650 #override location_Mixin version cause we want to try the cust_pkg location
651 #in between us and cust_main
652 # XXX what to do in the unlinked case??? return a pseudo-object that returns
654 sub cust_location_or_main {
656 return $self->cust_location if $self->locationnum;
657 my $cust_pkg = $self->cust_svc->cust_pkg;
658 $cust_pkg ? $cust_pkg->cust_location_or_main : '';
663 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this
664 service. By default, "associated with" means that either the "src" or the
665 "charged_party" field of the CDR matches the "phonenum" field of the service.
669 Accepts the following options:
671 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
673 =item status => "" (or "processing-tiered", "done"): Return only CDRs with that processing status.
675 =item inbound => 1: Return CDRs for inbound calls. With "status", will filter
676 on inbound processing status.
678 =item default_prefix => "XXX": Also accept the phone number of the service prepended
679 with the chosen prefix.
681 =item begin, end: Start and end of a date range, as unix timestamp.
683 =item cdrtypenum: Only return CDRs with this type number.
685 =item disable_src => 1: Only match on "charged_party", not "src".
687 =item by_svcnum: not supported for svc_phone
694 my($self, %options) = @_;
699 if ( $options{'inbound'} ) {
702 if ( exists($options{'status'}) ) {
703 my $status = $options{'status'};
705 push @where, 'EXISTS ( SELECT 1 FROM cdr_termination '.
706 'WHERE cdr.acctid = cdr_termination.acctid '.
707 "AND cdr_termination.status = '$status' ". #quoting kludge
708 'AND cdr_termination.termpart = 1 )';
710 push @where, 'NOT EXISTS ( SELECT 1 FROM cdr_termination '.
711 'WHERE cdr.acctid = cdr_termination.acctid '.
712 'AND cdr_termination.termpart = 1 )';
718 @fields = ( 'charged_party' );
719 push @fields, 'src' if !$options{'disable_src'};
720 $hash{'freesidestatus'} = $options{'status'}
721 if exists($options{'status'});
724 if ($options{'cdrtypenum'}) {
725 $hash{'cdrtypenum'} = $options{'cdrtypenum'};
728 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
730 my $number = $self->phonenum;
732 my $prefix = $options{'default_prefix'};
734 my @orwhere = map " $_ = '$number' ", @fields;
735 push @orwhere, map " $_ = '$prefix$number' ", @fields
736 if defined($prefix) && length($prefix);
737 if ( $prefix && $prefix =~ /^\+(\d+)$/ ) {
738 push @orwhere, map " $_ = '$1$number' ", @fields
741 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
743 if ( $options{'begin'} ) {
744 push @where, 'startdate >= '. $options{'begin'};
746 if ( $options{'end'} ) {
747 push @where, 'startdate < '. $options{'end'};
750 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
756 'extra_sql' => $extra_sql,
757 'order_by' => $options{'billsec_sum'} ? '' : "ORDER BY startdate $for_update",
758 'select' => $options{'billsec_sum'} ? 'sum(billsec) as billsec_sum' : '*',
770 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
771 L<FS::cust_pkg>, schema.html from the base documentation.