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::PagedSearch qw( psearch );
11 use FS::Msgcat qw(gettext);
16 use FS::cust_location;
19 $me = '[' . __PACKAGE__ . ']';
23 @pw_set = ( 'a'..'k', 'm','n', 'p-z', 'A'..'N', 'P'..'Z' , '2'..'9' );
25 #ask FS::UID to run this stuff for us later
26 $FS::UID::callback{'FS::svc_acct'} = sub {
28 $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:
81 Optional svcnum from svc_pbx
85 Forwarding destination
89 Email address for virtual fax (fax-to-email) services
93 LNP Status (can be null, native, portedin, portingin, portin-reject,
94 portingout, portout-reject)
100 =item lnp_desired_due_date
104 =item lnp_other_provider
106 If porting the number in or out, name of the losing or winning provider,
109 =item lnp_other_provider_account
111 Account number of other provider. See lnp_other_provider.
113 =item lnp_reject_reason
115 See lnp_status. If lnp_status is portin-reject or portout-reject, this is an
116 optional reject reason.
126 Creates a new phone number. To add the number to the database, see L<"insert">.
128 Note that this stores the hash reference, not a distinct copy of the hash it
129 points to. You can ask the object for a copy with the I<hash> method.
133 # the new method can be inherited from FS::Record, if a table method is defined
136 my %dis2 = ( disable_inventory=>1, disable_select=>1 );
138 'name' => 'Phone number',
139 'sorts' => 'phonenum',
140 'display_weight' => 60,
141 'cancel_weight' => 80,
143 'svcnum' => 'Service',
144 'countrycode' => { label => 'Country code',
146 disable_inventory => 1,
149 'phonenum' => 'Phone number',
150 'pin' => { label => 'Voicemail PIN', #'Personal Identification Number',
152 disable_inventory => 1,
155 'sip_password' => 'SIP password',
156 'phone_name' => 'Name',
157 'pbxsvc' => { label => 'PBX',
158 type => 'select-svc_pbx.html',
159 disable_inventory => 1,
160 disable_select => 1, #UI wonky, pry works otherwise
165 select_table => 'svc_domain',
166 select_key => 'svcnum',
167 select_label => 'domain',
168 disable_inventory => 1,
171 label => 'E911 location',
172 disable_inventory => 1,
175 'forwarddst' => { label => 'Forward Destination',
178 'email' => { label => 'Email',
181 'lnp_status' => { label => 'LNP Status',
182 type => 'select-lnp_status.html',
185 'lnp_reject_reason' => {
186 label => 'LNP Reject Reason',
189 'portable' => { label => 'Portable?', %dis2, },
190 'lrn' => { label => 'LRN',
191 disable_inventory => 1,
193 'lnp_desired_due_date' =>
194 { label => 'LNP Desired Due Date', %dis2 },
196 { label => 'LNP Due Date', %dis2 },
197 'lnp_other_provider' =>
198 { label => 'LNP Other Provider',
199 disable_inventory => 1,
201 'lnp_other_provider_account' =>
202 { label => 'LNP Other Provider Account #',
209 sub table { 'svc_phone'; }
211 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
213 =item search_sql STRING
215 Class method which returns an SQL fragment to search for the given string.
220 my( $class, $string ) = @_;
222 my $conf = new FS::Conf;
224 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
230 my $ccode = ( $conf->exists('default_phone_countrycode')
231 && $conf->config('default_phone_countrycode')
233 ? $conf->config('default_phone_countrycode')
236 $string =~ s/^$ccode//;
238 $class->search_sql_field('phonenum', $string );
243 Returns the phone number.
249 my $phonenum = $self->phonenum; #XXX format it better
250 my $label = $phonenum;
251 $label .= '@'.$self->domain if $self->domsvc;
252 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
258 Adds this phone number to the database. If there is an error, returns the
259 error, otherwise returns false.
268 warn "[$me] insert called on $self: ". Dumper($self).
269 "\nwith options: ". Dumper(%options);
272 local $SIG{HUP} = 'IGNORE';
273 local $SIG{INT} = 'IGNORE';
274 local $SIG{QUIT} = 'IGNORE';
275 local $SIG{TERM} = 'IGNORE';
276 local $SIG{TSTP} = 'IGNORE';
277 local $SIG{PIPE} = 'IGNORE';
279 my $oldAutoCommit = $FS::UID::AutoCommit;
280 local $FS::UID::AutoCommit = 0;
283 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
284 #make it more of a base class than a mixin... :)
285 if ( $options{'cust_location'}
286 && ( ! $self->locationnum || $self->locationnum == -1 ) ) {
287 my $error = $options{'cust_location'}->insert;
289 $dbh->rollback if $oldAutoCommit;
290 return "inserting cust_location (transaction rolled back): $error";
292 $self->locationnum( $options{'cust_location'}->locationnum );
294 #what about on-the-fly edits? if the ui supports it?
296 my $error = $self->SUPER::insert(%options);
298 $dbh->rollback if $oldAutoCommit;
302 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
309 Delete this record from the database.
316 local $SIG{HUP} = 'IGNORE';
317 local $SIG{INT} = 'IGNORE';
318 local $SIG{QUIT} = 'IGNORE';
319 local $SIG{TERM} = 'IGNORE';
320 local $SIG{TSTP} = 'IGNORE';
321 local $SIG{PIPE} = 'IGNORE';
323 my $oldAutoCommit = $FS::UID::AutoCommit;
324 local $FS::UID::AutoCommit = 0;
327 foreach my $phone_device ( $self->phone_device ) {
328 my $error = $phone_device->delete;
330 $dbh->rollback if $oldAutoCommit;
335 my @phone_avail = qsearch('phone_avail', { 'svcnum' => $self->svcnum } );
336 foreach my $phone_avail ( @phone_avail ) {
337 $phone_avail->svcnum('');
338 my $error = $phone_avail->replace;
340 $dbh->rollback if $oldAutoCommit;
345 my $error = $self->SUPER::delete;
347 $dbh->rollback if $oldAutoCommit;
351 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
356 # the delete method can be inherited from FS::Record
358 =item replace OLD_RECORD
360 Replaces the OLD_RECORD with this one in the database. If there is an error,
361 returns the error, otherwise returns false.
368 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
375 warn "[$me] replacing $old with $new\n".
376 "\nwith options: ". Dumper(%options);
379 local $SIG{HUP} = 'IGNORE';
380 local $SIG{INT} = 'IGNORE';
381 local $SIG{QUIT} = 'IGNORE';
382 local $SIG{TERM} = 'IGNORE';
383 local $SIG{TSTP} = 'IGNORE';
384 local $SIG{PIPE} = 'IGNORE';
386 my $oldAutoCommit = $FS::UID::AutoCommit;
387 local $FS::UID::AutoCommit = 0;
390 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
391 #make it more of a base class than a mixin... :)
392 if ( $options{'cust_location'}
393 && ( ! $new->locationnum || $new->locationnum == -1 ) ) {
394 my $error = $options{'cust_location'}->insert;
396 $dbh->rollback if $oldAutoCommit;
397 return "inserting cust_location (transaction rolled back): $error";
399 $new->locationnum( $options{'cust_location'}->locationnum );
401 #what about on-the-fly edits? if the ui supports it?
403 # LNP data validation
404 return 'Invalid LNP status' # if someone does really stupid stuff
405 if ( ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portingin')
406 || ($old->lnp_status eq 'portout-reject' && $new->lnp_status eq 'portingin')
407 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'portingout')
408 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'native')
409 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'native')
410 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'portingout')
411 || ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portin-reject')
414 my $error = $new->SUPER::replace($old, %options);
416 $dbh->rollback if $oldAutoCommit;
417 return $error if $error;
420 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
426 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
430 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
434 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
438 Checks all fields to make sure this is a valid phone number. If there is
439 an error, returns the error, otherwise returns false. Called by the insert
444 # the check method should currently be supplied - FS::Record contains some
445 # data checking routines
450 my $conf = new FS::Conf;
452 my $phonenum = $self->phonenum;
453 my $phonenum_check_method;
454 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
455 $phonenum =~ s/\W//g;
456 $phonenum_check_method = 'ut_alpha';
458 $phonenum =~ s/\D//g;
459 $phonenum_check_method = 'ut_number';
461 $self->phonenum($phonenum);
463 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
466 $self->ut_numbern('svcnum')
467 || $self->ut_numbern('countrycode')
468 || $self->$phonenum_check_method('phonenum')
469 || $self->ut_anything('sip_password')
470 || $self->ut_numbern('pin')
471 || $self->ut_textn('phone_name')
472 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
473 || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
474 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
475 || $self->ut_numbern('forwarddst')
476 || $self->ut_textn('email')
477 || $self->ut_numbern('lrn')
478 || $self->ut_numbern('lnp_desired_due_date')
479 || $self->ut_numbern('lnp_due_date')
480 || $self->ut_textn('lnp_other_provider')
481 || $self->ut_textn('lnp_other_provider_account')
482 || $self->ut_enumn('lnp_status', ['','portingin','portingout','portedin',
483 'native', 'portin-reject', 'portout-reject'])
484 || $self->ut_enumn('portable', ['','Y'])
485 || $self->ut_textn('lnp_reject_reason')
487 return $error if $error;
489 # LNP data validation
490 return 'Cannot set LNP fields: no LNP in progress'
491 if ( ($self->lnp_desired_due_date || $self->lnp_due_date
492 || $self->lnp_other_provider || $self->lnp_other_provider_account
493 || $self->lnp_reject_reason)
494 && (!$self->lnp_status || $self->lnp_status eq 'native') );
495 return 'Cannot set LNP reject reason: no LNP in progress or status is not reject'
496 if ($self->lnp_reject_reason && (!$self->lnp_status
497 || $self->lnp_status !~ /^port(in|out)-reject$/) );
498 return 'Cannot port-out a non-portable number'
499 if (!$self->portable && $self->lnp_status eq 'portingout');
502 return 'Name ('. $self->phone_name.
503 ") is longer than $phone_name_max characters"
504 if $phone_name_max && length($self->phone_name) > $phone_name_max;
506 $self->countrycode(1) unless $self->countrycode;
508 unless ( length($self->pin) ) {
509 my $random_pin = $conf->config('svc_phone-random_pin');
510 if ( $random_pin =~ /^\d+$/ ) {
512 join('', map int(rand(10)), 0..($random_pin-1))
517 unless ( length($self->sip_password) ) { # option for this?
520 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
528 =item _check duplicate
530 Internal method to check for duplicate phone numers.
534 #false laziness w/svc_acct.pm's _check_duplicate.
535 sub _check_duplicate {
538 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
539 return '' if $global_unique eq 'disabled';
544 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
545 qsearch( 'svc_phone', {
546 'countrycode' => $self->countrycode,
547 'phonenum' => $self->phonenum,
550 return gettext('phonenum_in_use')
551 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
553 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
554 unless ( $part_svc ) {
555 return 'unknown svcpart '. $self->svcpart;
558 if ( @dup_ccphonenum ) {
560 my $exports = FS::part_export::export_info('svc_phone');
561 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
563 foreach my $part_export ( $part_svc->part_export ) {
565 #this will catch to the same exact export
566 my @svcparts = map { $_->svcpart } $part_export->export_svc;
568 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
573 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
574 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
575 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
576 return "duplicate phone number ".
577 $self->countrycode. ' '. $self->phonenum.
578 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
579 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
591 Checks the supplied PIN against the PIN in the database. Returns true for a
592 sucessful authentication, false if no match.
597 my($self, $check_pin) = @_;
598 length($self->pin) && $check_pin eq $self->pin;
607 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
619 my $conf = new FS::Conf;
621 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
632 Returns any FS::phone_device records associated with this service.
638 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
641 #override location_Mixin version cause we want to try the cust_pkg location
642 #in between us and cust_main
643 # XXX what to do in the unlinked case??? return a pseudo-object that returns
645 sub cust_location_or_main {
647 return $self->cust_location if $self->locationnum;
648 my $cust_pkg = $self->cust_svc->cust_pkg;
649 $cust_pkg ? $cust_pkg->cust_location_or_main : '';
652 =item psearch_cdrs OPTIONS
654 Returns a paged search (L<FS::PagedSearch>) for Call Detail Records
655 associated with this service. By default, "associated with" means that
656 either the "src" or the "charged_party" field of the CDR matches the
657 "phonenum" field of the service. To access the CDRs themselves, call
658 "->fetch" on the resulting object.
662 Accepts the following options:
664 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
666 =item status => "" (or "processing-tiered", "done"): Return only CDRs with that processing status.
668 =item inbound => 1: Return CDRs for inbound calls. With "status", will filter
669 on inbound processing status.
671 =item default_prefix => "XXX": Also accept the phone number of the service prepended
672 with the chosen prefix.
674 =item begin, end: Start and end of a date range, as unix timestamp.
676 =item cdrtypenum: Only return CDRs with this type number.
678 =item disable_src => 1: Only match on "charged_party", not "src".
680 =item by_svcnum: not supported for svc_phone
682 =item billsec_sum: Instead of returning all of the CDRs, return a single
683 record (as an L<FS::cdr> object) with the sum of the 'billsec' field over
684 the entire result set.
692 my($self, %options) = @_;
697 if ( $options{'inbound'} ) {
700 if ( exists($options{'status'}) ) {
701 my $status = $options{'status'};
703 push @where, 'EXISTS ( SELECT 1 FROM cdr_termination '.
704 'WHERE cdr.acctid = cdr_termination.acctid '.
705 "AND cdr_termination.status = '$status' ". #quoting kludge
706 'AND cdr_termination.termpart = 1 )';
708 push @where, 'NOT EXISTS ( SELECT 1 FROM cdr_termination '.
709 'WHERE cdr.acctid = cdr_termination.acctid '.
710 'AND cdr_termination.termpart = 1 )';
716 @fields = ( 'charged_party' );
717 push @fields, 'src' if !$options{'disable_src'};
718 $hash{'freesidestatus'} = $options{'status'}
719 if exists($options{'status'});
722 if ($options{'cdrtypenum'}) {
723 $hash{'cdrtypenum'} = $options{'cdrtypenum'};
726 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
728 my $number = $self->phonenum;
730 my $prefix = $options{'default_prefix'};
732 my @orwhere = map " $_ = '$number' ", @fields;
733 push @orwhere, map " $_ = '$prefix$number' ", @fields
734 if defined($prefix) && length($prefix);
735 if ( $prefix && $prefix =~ /^\+(\d+)$/ ) {
736 push @orwhere, map " $_ = '$1$number' ", @fields
739 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
741 if ( $options{'begin'} ) {
742 push @where, 'startdate >= '. $options{'begin'};
744 if ( $options{'end'} ) {
745 push @where, 'startdate < '. $options{'end'};
748 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
753 'extra_sql' => $extra_sql,
754 'order_by' => $options{'billsec_sum'} ? '' : "ORDER BY startdate $for_update",
755 'select' => $options{'billsec_sum'} ? 'sum(billsec) as billsec_sum' : '*',
759 =item get_cdrs (DEPRECATED)
761 Like psearch_cdrs, but returns all the L<FS::cdr> objects at once, in a
762 single list. Arguments are the same as for psearch_cdrs. This can take
763 an unreasonably large amount of memory and is best avoided.
769 my $psearch = $self->psearch_cdrs(@_);
770 qsearch ( $psearch->{query} )
780 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
781 L<FS::cust_pkg>, schema.html from the base documentation.