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::callback{'FS::svc_acct'} = 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;
37 FS::svc_phone - Object methods for svc_phone records
43 $record = new FS::svc_phone \%hash;
44 $record = new FS::svc_phone { 'column' => 'value' };
46 $error = $record->insert;
48 $error = $new_record->replace($old_record);
50 $error = $record->delete;
52 $error = $record->check;
54 $error = $record->suspend;
56 $error = $record->unsuspend;
58 $error = $record->cancel;
62 An FS::svc_phone object represents a phone number. FS::svc_phone inherits
63 from FS::Record. The following fields are currently supported:
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 'pin' => { label => 'Voicemail PIN', #'Personal Identification Number',
156 disable_inventory => 1,
159 'sip_password' => 'SIP password',
160 'phone_name' => 'Name',
161 'pbxsvc' => { label => 'PBX',
162 type => 'select-svc_pbx.html',
163 disable_inventory => 1,
164 disable_select => 1, #UI wonky, pry works otherwise
169 select_table => 'svc_domain',
170 select_key => 'svcnum',
171 select_label => 'domain',
172 disable_inventory => 1,
175 label => 'E911 location',
176 disable_inventory => 1,
179 'forwarddst' => { label => 'Forward Destination',
182 'email' => { label => 'Email',
185 'lnp_status' => { label => 'LNP Status',
186 type => 'select-lnp_status.html',
189 'lnp_reject_reason' => {
190 label => 'LNP Reject Reason',
193 'portable' => { label => 'Portable?', %dis2, },
194 'lrn' => { label => 'LRN',
195 disable_inventory => 1,
197 'lnp_desired_due_date' =>
198 { label => 'LNP Desired Due Date', %dis2 },
200 { label => 'LNP Due Date', %dis2 },
201 'lnp_other_provider' =>
202 { label => 'LNP Other Provider',
203 disable_inventory => 1,
205 'lnp_other_provider_account' =>
206 { label => 'LNP Other Provider Account #',
213 sub table { 'svc_phone'; }
215 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
217 =item search_sql STRING
219 Class method which returns an SQL fragment to search for the given string.
224 my( $class, $string ) = @_;
226 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
232 my $conf = new FS::Conf;
233 my $ccode = ( $conf->exists('default_phone_countrycode')
234 && $conf->config('default_phone_countrycode')
236 ? $conf->config('default_phone_countrycode')
239 $string =~ s/^$ccode//;
241 $class->search_sql_field('phonenum', $string );
246 Returns the phone number.
252 my $phonenum = $self->phonenum; #XXX format it better
253 my $label = $phonenum;
254 $label .= '@'.$self->domain if $self->domsvc;
255 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
261 Adds this phone number to the database. If there is an error, returns the
262 error, otherwise returns false.
271 warn "[$me] insert called on $self: ". Dumper($self).
272 "\nwith options: ". Dumper(%options);
275 local $SIG{HUP} = 'IGNORE';
276 local $SIG{INT} = 'IGNORE';
277 local $SIG{QUIT} = 'IGNORE';
278 local $SIG{TERM} = 'IGNORE';
279 local $SIG{TSTP} = 'IGNORE';
280 local $SIG{PIPE} = 'IGNORE';
282 my $oldAutoCommit = $FS::UID::AutoCommit;
283 local $FS::UID::AutoCommit = 0;
286 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
287 #make it more of a base class than a mixin... :)
288 if ( $options{'cust_location'}
289 && ( ! $self->locationnum || $self->locationnum == -1 ) ) {
290 my $error = $options{'cust_location'}->insert;
292 $dbh->rollback if $oldAutoCommit;
293 return "inserting cust_location (transaction rolled back): $error";
295 $self->locationnum( $options{'cust_location'}->locationnum );
297 #what about on-the-fly edits? if the ui supports it?
299 my $error = $self->SUPER::insert(%options);
301 $dbh->rollback if $oldAutoCommit;
305 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
312 Delete this record from the database.
319 local $SIG{HUP} = 'IGNORE';
320 local $SIG{INT} = 'IGNORE';
321 local $SIG{QUIT} = 'IGNORE';
322 local $SIG{TERM} = 'IGNORE';
323 local $SIG{TSTP} = 'IGNORE';
324 local $SIG{PIPE} = 'IGNORE';
326 my $oldAutoCommit = $FS::UID::AutoCommit;
327 local $FS::UID::AutoCommit = 0;
330 foreach my $phone_device ( $self->phone_device ) {
331 my $error = $phone_device->delete;
333 $dbh->rollback if $oldAutoCommit;
338 my @phone_avail = qsearch('phone_avail', { 'svcnum' => $self->svcnum } );
339 foreach my $phone_avail ( @phone_avail ) {
340 $phone_avail->svcnum('');
341 my $error = $phone_avail->replace;
343 $dbh->rollback if $oldAutoCommit;
348 my $error = $self->SUPER::delete;
350 $dbh->rollback if $oldAutoCommit;
354 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
359 # the delete method can be inherited from FS::Record
361 =item replace OLD_RECORD
363 Replaces the OLD_RECORD with this one in the database. If there is an error,
364 returns the error, otherwise returns false.
371 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
378 warn "[$me] replacing $old with $new\n".
379 "\nwith options: ". Dumper(%options);
382 local $SIG{HUP} = 'IGNORE';
383 local $SIG{INT} = 'IGNORE';
384 local $SIG{QUIT} = 'IGNORE';
385 local $SIG{TERM} = 'IGNORE';
386 local $SIG{TSTP} = 'IGNORE';
387 local $SIG{PIPE} = 'IGNORE';
389 my $oldAutoCommit = $FS::UID::AutoCommit;
390 local $FS::UID::AutoCommit = 0;
393 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
394 #make it more of a base class than a mixin... :)
395 if ( $options{'cust_location'}
396 && ( ! $new->locationnum || $new->locationnum == -1 ) ) {
397 my $error = $options{'cust_location'}->insert;
399 $dbh->rollback if $oldAutoCommit;
400 return "inserting cust_location (transaction rolled back): $error";
402 $new->locationnum( $options{'cust_location'}->locationnum );
404 #what about on-the-fly edits? if the ui supports it?
406 # LNP data validation
407 return 'Invalid LNP status' # if someone does really stupid stuff
408 if ( ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portingin')
409 || ($old->lnp_status eq 'portout-reject' && $new->lnp_status eq 'portingin')
410 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'portingout')
411 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'native')
412 || ($old->lnp_status eq 'portin-reject' && $new->lnp_status eq 'native')
413 || ($old->lnp_status eq 'portingin' && $new->lnp_status eq 'portingout')
414 || ($old->lnp_status eq 'portingout' && $new->lnp_status eq 'portin-reject')
417 my $error = $new->SUPER::replace($old, %options);
419 $dbh->rollback if $oldAutoCommit;
420 return $error if $error;
423 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
429 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
433 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
437 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
441 Checks all fields to make sure this is a valid phone number. If there is
442 an error, returns the error, otherwise returns false. Called by the insert
447 # the check method should currently be supplied - FS::Record contains some
448 # data checking routines
453 my $conf = new FS::Conf;
455 my $phonenum = $self->phonenum;
456 my $phonenum_check_method;
457 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
458 $phonenum =~ s/\W//g;
459 $phonenum_check_method = 'ut_alpha';
461 $phonenum =~ s/\D//g;
462 $phonenum_check_method = 'ut_number';
464 $self->phonenum($phonenum);
466 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
469 $self->ut_numbern('svcnum')
470 || $self->ut_numbern('countrycode')
471 || $self->$phonenum_check_method('phonenum')
472 || $self->ut_anything('sip_password')
473 || $self->ut_numbern('pin')
474 || $self->ut_textn('phone_name')
475 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
476 || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
477 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
478 || $self->ut_numbern('forwarddst')
479 || $self->ut_textn('email')
480 || $self->ut_numbern('lrn')
481 || $self->ut_numbern('lnp_desired_due_date')
482 || $self->ut_numbern('lnp_due_date')
483 || $self->ut_textn('lnp_other_provider')
484 || $self->ut_textn('lnp_other_provider_account')
485 || $self->ut_enumn('lnp_status', ['','portingin','portingout','portedin',
486 'native', 'portin-reject', 'portout-reject'])
487 || $self->ut_enumn('portable', ['','Y'])
488 || $self->ut_textn('lnp_reject_reason')
490 return $error if $error;
492 # LNP data validation
493 return 'Cannot set LNP fields: no LNP in progress'
494 if ( ($self->lnp_desired_due_date || $self->lnp_due_date
495 || $self->lnp_other_provider || $self->lnp_other_provider_account
496 || $self->lnp_reject_reason)
497 && (!$self->lnp_status || $self->lnp_status eq 'native') );
498 return 'Cannot set LNP reject reason: no LNP in progress or status is not reject'
499 if ($self->lnp_reject_reason && (!$self->lnp_status
500 || $self->lnp_status !~ /^port(in|out)-reject$/) );
501 return 'Cannot port-out a non-portable number'
502 if (!$self->portable && $self->lnp_status eq 'portingout');
505 return 'Name ('. $self->phone_name.
506 ") is longer than $phone_name_max characters"
507 if $phone_name_max && length($self->phone_name) > $phone_name_max;
509 $self->countrycode(1) unless $self->countrycode;
511 unless ( length($self->pin) ) {
512 my $random_pin = $conf->config('svc_phone-random_pin');
513 if ( $random_pin =~ /^\d+$/ ) {
515 join('', map int(rand(10)), 0..($random_pin-1))
520 if ( length($self->sip_password) ) {
522 return "SIP password must be longer than $passwordmin characters"
523 if length($self->sip_password) < $passwordmin;
524 return "SIP password must be shorter than $passwordmax characters"
525 if length($self->sip_password) > $passwordmax;
527 } else { # option for this?
530 join('', map $pw_set[ int(rand $#pw_set) ], (1..min($passwordmax,16)) )
538 =item _check duplicate
540 Internal method to check for duplicate phone numers.
544 #false laziness w/svc_acct.pm's _check_duplicate.
545 sub _check_duplicate {
548 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
549 return '' if $global_unique eq 'disabled';
554 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
555 qsearch( 'svc_phone', {
556 'countrycode' => $self->countrycode,
557 'phonenum' => $self->phonenum,
560 return gettext('phonenum_in_use')
561 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
563 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
564 unless ( $part_svc ) {
565 return 'unknown svcpart '. $self->svcpart;
568 if ( @dup_ccphonenum ) {
570 my $exports = FS::part_export::export_info('svc_phone');
571 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
573 foreach my $part_export ( $part_svc->part_export ) {
575 #this will catch to the same exact export
576 my @svcparts = map { $_->svcpart } $part_export->export_svc;
578 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
583 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
584 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
585 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
586 return "duplicate phone number ".
587 $self->countrycode. ' '. $self->phonenum.
588 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
589 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
601 Checks the supplied PIN against the PIN in the database. Returns true for a
602 sucessful authentication, false if no match.
607 my($self, $check_pin) = @_;
608 length($self->pin) && $check_pin eq $self->pin;
617 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
629 my $conf = new FS::Conf;
631 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
642 Returns any FS::phone_device records associated with this service.
648 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
651 #override location_Mixin version cause we want to try the cust_pkg location
652 #in between us and cust_main
653 # XXX what to do in the unlinked case??? return a pseudo-object that returns
655 sub cust_location_or_main {
657 return $self->cust_location if $self->locationnum;
658 my $cust_pkg = $self->cust_svc->cust_pkg;
659 $cust_pkg ? $cust_pkg->cust_location_or_main : '';
664 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this
665 service. By default, "associated with" means that either the "src" or the
666 "charged_party" field of the CDR matches the "phonenum" field of the service.
670 Accepts the following options:
672 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
674 =item status => "" (or "processing-tiered", "done"): Return only CDRs with that processing status.
676 =item inbound => 1: Return CDRs for inbound calls. With "status", will filter
677 on inbound processing status.
679 =item default_prefix => "XXX": Also accept the phone number of the service prepended
680 with the chosen prefix.
682 =item begin, end: Start and end of a date range, as unix timestamp.
684 =item cdrtypenum: Only return CDRs with this type number.
686 =item disable_src => 1: Only match on "charged_party", not "src".
688 =item nonzero: Only return CDRs where duration > 0.
690 =item by_svcnum: not supported for svc_phone
696 sub _search_cdrs { # transitional; replaced with psearch_cdrs in 3.0
697 my($self, %options) = @_;
702 if ( $options{'inbound'} ) {
705 if ( exists($options{'status'}) ) {
706 my $status = $options{'status'};
708 push @where, 'EXISTS ( SELECT 1 FROM cdr_termination '.
709 'WHERE cdr.acctid = cdr_termination.acctid '.
710 "AND cdr_termination.status = '$status' ". #quoting kludge
711 'AND cdr_termination.termpart = 1 )';
713 push @where, 'NOT EXISTS ( SELECT 1 FROM cdr_termination '.
714 'WHERE cdr.acctid = cdr_termination.acctid '.
715 'AND cdr_termination.termpart = 1 )';
721 @fields = ( 'charged_party' );
722 push @fields, 'src' if !$options{'disable_src'};
723 $hash{'freesidestatus'} = $options{'status'}
724 if exists($options{'status'});
727 if ($options{'cdrtypenum'}) {
728 $hash{'cdrtypenum'} = $options{'cdrtypenum'};
731 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
733 my $number = $self->phonenum;
735 my $prefix = $options{'default_prefix'};
737 my @orwhere = map " $_ = '$number' ", @fields;
738 push @orwhere, map " $_ = '$prefix$number' ", @fields
739 if defined($prefix) && length($prefix);
740 if ( $prefix && $prefix =~ /^\+(\d+)$/ ) {
741 push @orwhere, map " $_ = '$1$number' ", @fields
744 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
746 if ( $options{'begin'} ) {
747 push @where, 'startdate >= '. $options{'begin'};
749 if ( $options{'end'} ) {
750 push @where, 'startdate < '. $options{'end'};
752 if ( $options{'nonzero'} ) {
753 push @where, 'duration > 0';
756 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
762 'extra_sql' => $extra_sql,
763 'order_by' => $options{'billsec_sum'} ? '' : "ORDER BY startdate $for_update",
764 'select' => $options{'billsec_sum'} ? 'sum(billsec) as billsec_sum' : '*',
769 qsearch(_search_cdrs(@_));
774 Takes the same options as psearch_cdrs, but returns a single row containing
775 "count" (the number of CDRs) and the sums of the following fields: duration,
776 billsec, rated_price, rated_seconds, rated_minutes.
778 Note that if any calls are not rated, their rated_* fields will be null.
779 If you want to use those fields, pass the 'status' option to limit to
780 calls that have been rated. This is intentional; please don't "fix" it.
786 my $search = $self->_search_cdrs(@_);
787 $search->{'select'} = join(',',
789 map { "SUM($_) AS $_" }
790 qw(duration billsec rated_price rated_seconds rated_minutes)
792 $search->{'order_by'} = '';
793 qsearchs ( $search );
802 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
803 L<FS::cust_pkg>, schema.html from the base documentation.