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
6 $passwordmin $passwordmax
9 use Scalar::Util qw( blessed );
10 use List::Util qw( min );
12 use FS::Record qw( qsearch qsearchs dbh );
13 use FS::Msgcat qw(gettext);
18 use FS::cust_location;
21 $me = '[' . __PACKAGE__ . ']';
25 @pw_set = ( 'a'..'k', 'm','n', 'p-z', 'A'..'N', 'P'..'Z' , '2'..'9' );
27 #ask FS::UID to run this stuff for us later
28 FS::UID->install_callback( sub {
30 $phone_name_max = $conf->config('svc_phone-phone_name-max_length');
31 $passwordmin = $conf->config('sip_passwordmin') || 0;
32 $passwordmax = $conf->config('sip_passwordmax') || 80;
38 FS::svc_phone - Object methods for svc_phone records
44 $record = new FS::svc_phone \%hash;
45 $record = new FS::svc_phone { 'column' => 'value' };
47 $error = $record->insert;
49 $error = $new_record->replace($old_record);
51 $error = $record->delete;
53 $error = $record->check;
55 $error = $record->suspend;
57 $error = $record->unsuspend;
59 $error = $record->cancel;
63 An FS::svc_phone object represents a phone number. FS::svc_phone inherits
64 from FS::Record. The following fields are currently supported:
86 Optional svcnum from svc_pbx
90 Forwarding destination
94 Email address for virtual fax (fax-to-email) services
98 LNP Status (can be null, native, portedin, portingin, portin-reject,
99 portingout, portout-reject)
105 =item lnp_desired_due_date
109 =item lnp_other_provider
111 If porting the number in or out, name of the losing or winning provider,
114 =item lnp_other_provider_account
116 Account number of other provider. See lnp_other_provider.
118 =item lnp_reject_reason
120 See lnp_status. If lnp_status is portin-reject or portout-reject, this is an
121 optional reject reason.
131 Creates a new phone number. To add the number to the database, see L<"insert">.
133 Note that this stores the hash reference, not a distinct copy of the hash it
134 points to. You can ask the object for a copy with the I<hash> method.
138 # the new method can be inherited from FS::Record, if a table method is defined
141 my %dis2 = ( disable_inventory=>1, disable_select=>1 );
143 'name' => 'Phone number',
144 'sorts' => 'phonenum',
145 'display_weight' => 60,
146 'cancel_weight' => 80,
148 'svcnum' => 'Service',
149 'countrycode' => { label => 'Country code',
151 disable_inventory => 1,
154 'phonenum' => 'Phone number',
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 $x = $self->setfixed;
457 return $x unless ref($x);
460 my $phonenum = $self->phonenum;
461 my $phonenum_check_method;
462 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
463 $phonenum =~ s/\W//g;
464 $phonenum_check_method = 'ut_alpha';
466 $phonenum =~ s/\D//g;
467 $phonenum_check_method = 'ut_number';
469 $self->phonenum($phonenum);
471 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
474 $self->ut_numbern('svcnum')
475 || $self->ut_numbern('countrycode')
476 || $self->$phonenum_check_method('phonenum')
477 || $self->ut_anything('sip_password')
478 || $self->ut_numbern('pin')
479 || $self->ut_textn('phone_name')
480 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
481 || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
482 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
483 || $self->ut_numbern('forwarddst')
484 || $self->ut_textn('email')
485 || $self->ut_numbern('lrn')
486 || $self->ut_numbern('lnp_desired_due_date')
487 || $self->ut_numbern('lnp_due_date')
488 || $self->ut_textn('lnp_other_provider')
489 || $self->ut_textn('lnp_other_provider_account')
490 || $self->ut_enumn('lnp_status', ['','portingin','portingout','portedin',
491 'native', 'portin-reject', 'portout-reject'])
492 || $self->ut_enumn('portable', ['','Y'])
493 || $self->ut_textn('lnp_reject_reason')
495 return $error if $error;
497 # LNP data validation
498 return 'Cannot set LNP fields: no LNP in progress'
499 if ( ($self->lnp_desired_due_date || $self->lnp_due_date
500 || $self->lnp_other_provider || $self->lnp_other_provider_account
501 || $self->lnp_reject_reason)
502 && (!$self->lnp_status || $self->lnp_status eq 'native') );
503 return 'Cannot set LNP reject reason: no LNP in progress or status is not reject'
504 if ($self->lnp_reject_reason && (!$self->lnp_status
505 || $self->lnp_status !~ /^port(in|out)-reject$/) );
506 return 'Cannot port-out a non-portable number'
507 if (!$self->portable && $self->lnp_status eq 'portingout');
510 return 'Name ('. $self->phone_name.
511 ") is longer than $phone_name_max characters"
512 if $phone_name_max && length($self->phone_name) > $phone_name_max;
514 $self->countrycode(1) unless $self->countrycode;
516 unless ( length($self->pin) ) {
517 my $random_pin = $conf->config('svc_phone-random_pin');
518 if ( $random_pin =~ /^\d+$/ ) {
520 join('', map int(rand(10)), 0..($random_pin-1))
525 if ( length($self->sip_password) ) {
527 return "SIP password must be longer than $passwordmin characters"
528 if length($self->sip_password) < $passwordmin;
529 return "SIP password must be shorter than $passwordmax characters"
530 if length($self->sip_password) > $passwordmax;
532 } elsif ( $part_svc->part_svc_column('sip_password')->columnflag ne 'F' ) {
536 join('', map $pw_set[ int(rand $#pw_set) ], (1..min($passwordmax,16)) )
544 =item _check duplicate
546 Internal method to check for duplicate phone numers.
550 #false laziness w/svc_acct.pm's _check_duplicate.
551 sub _check_duplicate {
554 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
555 return '' if $global_unique eq 'disabled';
560 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
561 qsearch( 'svc_phone', {
562 'countrycode' => $self->countrycode,
563 'phonenum' => $self->phonenum,
566 return gettext('phonenum_in_use')
567 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
569 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
570 unless ( $part_svc ) {
571 return 'unknown svcpart '. $self->svcpart;
574 if ( @dup_ccphonenum ) {
576 my $exports = FS::part_export::export_info('svc_phone');
577 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
579 foreach my $part_export ( $part_svc->part_export ) {
581 #this will catch to the same exact export
582 my @svcparts = map { $_->svcpart } $part_export->export_svc;
584 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
589 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
590 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
591 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
592 return "duplicate phone number ".
593 $self->countrycode. ' '. $self->phonenum.
594 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
595 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
607 Checks the supplied PIN against the PIN in the database. Returns true for a
608 sucessful authentication, false if no match.
613 my($self, $check_pin) = @_;
614 length($self->pin) && $check_pin eq $self->pin;
623 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
635 #my $conf = new FS::Conf;
637 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
648 Returns any FS::phone_device records associated with this service.
654 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
657 #override location_Mixin version cause we want to try the cust_pkg location
658 #in between us and cust_main
659 # XXX what to do in the unlinked case??? return a pseudo-object that returns
661 sub cust_location_or_main {
663 return $self->cust_location if $self->locationnum;
664 my $cust_pkg = $self->cust_svc->cust_pkg;
665 $cust_pkg ? $cust_pkg->cust_location_or_main : '';
670 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this
671 service. By default, "associated with" means that either the "src" or the
672 "charged_party" field of the CDR matches the "phonenum" field of the service.
676 Accepts the following options:
678 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
680 =item status => "" (or "processing-tiered", "done"): Return only CDRs with that processing status.
682 =item inbound => 1: Return CDRs for inbound calls. With "status", will filter
683 on inbound processing status.
685 =item default_prefix => "XXX": Also accept the phone number of the service prepended
686 with the chosen prefix.
688 =item begin, end: Start and end of a date range, as unix timestamp.
690 =item cdrtypenum: Only return CDRs with this type number.
692 =item disable_src => 1: Only match on "charged_party", not "src".
694 =item nonzero: Only return CDRs where duration > 0.
696 =item by_svcnum: not supported for svc_phone
702 sub _search_cdrs { # transitional; replaced with psearch_cdrs in 3.0
703 my($self, %options) = @_;
708 if ( $options{'inbound'} ) {
711 if ( exists($options{'status'}) ) {
712 my $status = $options{'status'};
714 push @where, 'EXISTS ( SELECT 1 FROM cdr_termination '.
715 'WHERE cdr.acctid = cdr_termination.acctid '.
716 "AND cdr_termination.status = '$status' ". #quoting kludge
717 'AND cdr_termination.termpart = 1 )';
719 push @where, 'NOT EXISTS ( SELECT 1 FROM cdr_termination '.
720 'WHERE cdr.acctid = cdr_termination.acctid '.
721 'AND cdr_termination.termpart = 1 )';
727 @fields = ( 'charged_party' );
728 push @fields, 'src' if !$options{'disable_src'};
729 $hash{'freesidestatus'} = $options{'status'}
730 if exists($options{'status'});
733 if ($options{'cdrtypenum'}) {
734 $hash{'cdrtypenum'} = $options{'cdrtypenum'};
737 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
739 my $number = $self->phonenum;
741 my $prefix = $options{'default_prefix'};
743 my @orwhere = map " $_ = '$number' ", @fields;
744 push @orwhere, map " $_ = '$prefix$number' ", @fields
745 if defined($prefix) && length($prefix);
746 if ( $prefix && $prefix =~ /^\+(\d+)$/ ) {
747 push @orwhere, map " $_ = '$1$number' ", @fields
750 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
752 if ( $options{'begin'} ) {
753 push @where, 'startdate >= '. $options{'begin'};
755 if ( $options{'end'} ) {
756 push @where, 'startdate < '. $options{'end'};
758 if ( $options{'nonzero'} ) {
759 push @where, 'duration > 0';
762 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
768 'extra_sql' => $extra_sql,
769 'order_by' => $options{'billsec_sum'} ? '' : "ORDER BY startdate $for_update",
770 'select' => $options{'billsec_sum'} ? 'sum(billsec) as billsec_sum' : '*',
775 qsearch(_search_cdrs(@_));
780 Takes the same options as psearch_cdrs, but returns a single row containing
781 "count" (the number of CDRs) and the sums of the following fields: duration,
782 billsec, rated_price, rated_seconds, rated_minutes.
784 Note that if any calls are not rated, their rated_* fields will be null.
785 If you want to use those fields, pass the 'status' option to limit to
786 calls that have been rated. This is intentional; please don't "fix" it.
792 my $search = $self->_search_cdrs(@_);
793 $search->{'select'} = join(',',
795 map { "SUM($_) AS $_" }
796 qw(duration billsec rated_price rated_seconds rated_minutes)
798 $search->{'order_by'} = '';
799 qsearchs ( $search );
808 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
809 L<FS::cust_pkg>, schema.html from the base documentation.