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->pin) ) {
507 my $random_pin = $conf->config('svc_phone-random_pin');
508 if ( $random_pin =~ /^\d+$/ ) {
510 join('', map int(rand(10)), 0..($random_pin-1))
515 unless ( length($self->sip_password) ) { # option for this?
518 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
526 =item _check duplicate
528 Internal method to check for duplicate phone numers.
532 #false laziness w/svc_acct.pm's _check_duplicate.
533 sub _check_duplicate {
536 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
537 return '' if $global_unique eq 'disabled';
542 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
543 qsearch( 'svc_phone', {
544 'countrycode' => $self->countrycode,
545 'phonenum' => $self->phonenum,
548 return gettext('phonenum_in_use')
549 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
551 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
552 unless ( $part_svc ) {
553 return 'unknown svcpart '. $self->svcpart;
556 if ( @dup_ccphonenum ) {
558 my $exports = FS::part_export::export_info('svc_phone');
559 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
561 foreach my $part_export ( $part_svc->part_export ) {
563 #this will catch to the same exact export
564 my @svcparts = map { $_->svcpart } $part_export->export_svc;
566 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
571 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
572 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
573 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
574 return "duplicate phone number ".
575 $self->countrycode. ' '. $self->phonenum.
576 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
577 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
589 Checks the supplied PIN against the PIN in the database. Returns true for a
590 sucessful authentication, false if no match.
595 my($self, $check_pin) = @_;
596 length($self->pin) && $check_pin eq $self->pin;
605 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
617 my $conf = new FS::Conf;
619 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
630 Returns any FS::phone_device records associated with this service.
636 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
639 #override location_Mixin version cause we want to try the cust_pkg location
640 #in between us and cust_main
641 # XXX what to do in the unlinked case??? return a pseudo-object that returns
643 sub cust_location_or_main {
645 return $self->cust_location if $self->locationnum;
646 my $cust_pkg = $self->cust_svc->cust_pkg;
647 $cust_pkg ? $cust_pkg->cust_location_or_main : '';
652 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this
653 service. By default, "associated with" means that either the "src" or the
654 "charged_party" field of the CDR matches the "phonenum" field of the service.
658 Accepts the following options:
660 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
662 =item status => "" (or "processing-tiered", "done"): Return only CDRs with that processing status.
664 =item inbound => 1: Return CDRs for inbound calls. With "status", will filter
665 on inbound processing status.
667 =item default_prefix => "XXX": Also accept the phone number of the service prepended
668 with the chosen prefix.
670 =item begin, end: Start and end of a date range, as unix timestamp.
672 =item cdrtypenum: Only return CDRs with this type number.
674 =item disable_src => 1: Only match on "charged_party", not "src".
676 =item nonzero: Only return CDRs where duration > 0.
678 =item by_svcnum: not supported for svc_phone
684 sub _search_cdrs { # transitional; replaced with psearch_cdrs in 3.0
685 my($self, %options) = @_;
690 if ( $options{'inbound'} ) {
693 if ( exists($options{'status'}) ) {
694 my $status = $options{'status'};
696 push @where, 'EXISTS ( SELECT 1 FROM cdr_termination '.
697 'WHERE cdr.acctid = cdr_termination.acctid '.
698 "AND cdr_termination.status = '$status' ". #quoting kludge
699 'AND cdr_termination.termpart = 1 )';
701 push @where, 'NOT EXISTS ( SELECT 1 FROM cdr_termination '.
702 'WHERE cdr.acctid = cdr_termination.acctid '.
703 'AND cdr_termination.termpart = 1 )';
709 @fields = ( 'charged_party' );
710 push @fields, 'src' if !$options{'disable_src'};
711 $hash{'freesidestatus'} = $options{'status'}
712 if exists($options{'status'});
715 if ($options{'cdrtypenum'}) {
716 $hash{'cdrtypenum'} = $options{'cdrtypenum'};
719 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
721 my $number = $self->phonenum;
723 my $prefix = $options{'default_prefix'};
725 my @orwhere = map " $_ = '$number' ", @fields;
726 push @orwhere, map " $_ = '$prefix$number' ", @fields
727 if defined($prefix) && length($prefix);
728 if ( $prefix && $prefix =~ /^\+(\d+)$/ ) {
729 push @orwhere, map " $_ = '$1$number' ", @fields
732 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
734 if ( $options{'begin'} ) {
735 push @where, 'startdate >= '. $options{'begin'};
737 if ( $options{'end'} ) {
738 push @where, 'startdate < '. $options{'end'};
740 if ( $options{'nonzero'} ) {
741 push @where, 'duration > 0';
744 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
750 'extra_sql' => $extra_sql,
751 'order_by' => $options{'billsec_sum'} ? '' : "ORDER BY startdate $for_update",
752 'select' => $options{'billsec_sum'} ? 'sum(billsec) as billsec_sum' : '*',
757 qsearch(_search_cdrs(@_));
762 Takes the same options as psearch_cdrs, but returns a single row containing
763 "count" (the number of CDRs) and the sums of the following fields: duration,
764 billsec, rated_price, rated_seconds, rated_minutes.
766 Note that if any calls are not rated, their rated_* fields will be null.
767 If you want to use those fields, pass the 'status' option to limit to
768 calls that have been rated. This is intentional; please don't "fix" it.
774 my $search = $self->_search_cdrs(@_);
775 $search->{'select'} = join(',',
777 map { "SUM($_) AS $_" }
778 qw(duration billsec rated_price rated_seconds rated_minutes)
780 $search->{'order_by'} = '';
781 qsearchs ( $search );
790 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
791 L<FS::cust_pkg>, schema.html from the base documentation.