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
90 Creates a new phone number. To add the number to the database, see L<"insert">.
92 Note that this stores the hash reference, not a distinct copy of the hash it
93 points to. You can ask the object for a copy with the I<hash> method.
97 # the new method can be inherited from FS::Record, if a table method is defined
100 my %dis2 = ( disable_inventory=>1, disable_select=>1 );
102 'name' => 'Phone number',
103 'sorts' => 'phonenum',
104 'display_weight' => 60,
105 'cancel_weight' => 80,
107 'svcnum' => 'Service',
108 'countrycode' => { label => 'Country code',
110 disable_inventory => 1,
113 'phonenum' => 'Phone number',
114 'pin' => { label => 'Voicemail PIN', #'Personal Identification Number',
116 disable_inventory => 1,
119 'sip_password' => 'SIP password',
120 'phone_name' => 'Name',
121 'pbxsvc' => { label => 'PBX',
122 type => 'select-svc_pbx.html',
123 disable_inventory => 1,
124 disable_select => 1, #UI wonky, pry works otherwise
129 select_table => 'svc_domain',
130 select_key => 'svcnum',
131 select_label => 'domain',
132 disable_inventory => 1,
135 label => 'E911 location',
136 disable_inventory => 1,
139 'lnp_status' => { label => 'LNP Status',
140 type => 'select-lnp_status.html',
143 'portable' => { label => 'Portable?', %dis2, },
144 'lrn' => { label => 'LRN',
145 disable_inventory => 1,
147 'lnp_desired_due_date' =>
148 { label => 'LNP Desired Due Date', %dis2 },
150 { label => 'LNP Due Date', %dis2 },
151 'lnp_other_provider' =>
152 { label => 'LNP Other Provider',
153 disable_inventory => 1,
155 'lnp_other_provider_account' =>
156 { label => 'LNP Other Provider Account #',
163 sub table { 'svc_phone'; }
165 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
167 =item search_sql STRING
169 Class method which returns an SQL fragment to search for the given string.
174 my( $class, $string ) = @_;
176 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
182 my $conf = new FS::Conf;
183 my $ccode = ( $conf->exists('default_phone_countrycode')
184 && $conf->config('default_phone_countrycode')
186 ? $conf->config('default_phone_countrycode')
189 $string =~ s/^$ccode//;
191 $class->search_sql_field('phonenum', $string );
196 Returns the phone number.
202 my $phonenum = $self->phonenum; #XXX format it better
203 my $label = $phonenum;
204 $label .= '@'.$self->domain if $self->domsvc;
205 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
211 Adds this phone number to the database. If there is an error, returns the
212 error, otherwise returns false.
221 warn "[$me] insert called on $self: ". Dumper($self).
222 "\nwith options: ". Dumper(%options);
225 local $SIG{HUP} = 'IGNORE';
226 local $SIG{INT} = 'IGNORE';
227 local $SIG{QUIT} = 'IGNORE';
228 local $SIG{TERM} = 'IGNORE';
229 local $SIG{TSTP} = 'IGNORE';
230 local $SIG{PIPE} = 'IGNORE';
232 my $oldAutoCommit = $FS::UID::AutoCommit;
233 local $FS::UID::AutoCommit = 0;
236 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
237 #make it more of a base class than a mixin... :)
238 if ( $options{'cust_location'}
239 && ( ! $self->locationnum || $self->locationnum == -1 ) ) {
240 my $error = $options{'cust_location'}->insert;
242 $dbh->rollback if $oldAutoCommit;
243 return "inserting cust_location (transaction rolled back): $error";
245 $self->locationnum( $options{'cust_location'}->locationnum );
247 #what about on-the-fly edits? if the ui supports it?
249 my $error = $self->SUPER::insert(%options);
251 $dbh->rollback if $oldAutoCommit;
255 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
262 Delete this record from the database.
269 local $SIG{HUP} = 'IGNORE';
270 local $SIG{INT} = 'IGNORE';
271 local $SIG{QUIT} = 'IGNORE';
272 local $SIG{TERM} = 'IGNORE';
273 local $SIG{TSTP} = 'IGNORE';
274 local $SIG{PIPE} = 'IGNORE';
276 my $oldAutoCommit = $FS::UID::AutoCommit;
277 local $FS::UID::AutoCommit = 0;
280 foreach my $phone_device ( $self->phone_device ) {
281 my $error = $phone_device->delete;
283 $dbh->rollback if $oldAutoCommit;
288 my @phone_avail = qsearch('phone_avail', { 'svcnum' => $self->svcnum } );
289 foreach my $phone_avail ( @phone_avail ) {
290 $phone_avail->svcnum('');
291 my $error = $phone_avail->replace;
293 $dbh->rollback if $oldAutoCommit;
298 my $error = $self->SUPER::delete;
300 $dbh->rollback if $oldAutoCommit;
304 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
309 # the delete method can be inherited from FS::Record
311 =item replace OLD_RECORD
313 Replaces the OLD_RECORD with this one in the database. If there is an error,
314 returns the error, otherwise returns false.
321 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
328 warn "[$me] replacing $old with $new\n".
329 "\nwith options: ". Dumper(%options);
332 local $SIG{HUP} = 'IGNORE';
333 local $SIG{INT} = 'IGNORE';
334 local $SIG{QUIT} = 'IGNORE';
335 local $SIG{TERM} = 'IGNORE';
336 local $SIG{TSTP} = 'IGNORE';
337 local $SIG{PIPE} = 'IGNORE';
339 my $oldAutoCommit = $FS::UID::AutoCommit;
340 local $FS::UID::AutoCommit = 0;
343 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
344 #make it more of a base class than a mixin... :)
345 if ( $options{'cust_location'}
346 && ( ! $new->locationnum || $new->locationnum == -1 ) ) {
347 my $error = $options{'cust_location'}->insert;
349 $dbh->rollback if $oldAutoCommit;
350 return "inserting cust_location (transaction rolled back): $error";
352 $new->locationnum( $options{'cust_location'}->locationnum );
354 #what about on-the-fly edits? if the ui supports it?
356 my $error = $new->SUPER::replace($old, %options);
358 $dbh->rollback if $oldAutoCommit;
359 return $error if $error;
362 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
368 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
372 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
376 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
380 Checks all fields to make sure this is a valid phone number. If there is
381 an error, returns the error, otherwise returns false. Called by the insert
386 # the check method should currently be supplied - FS::Record contains some
387 # data checking routines
392 my $conf = new FS::Conf;
394 my $phonenum = $self->phonenum;
395 my $phonenum_check_method;
396 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
397 $phonenum =~ s/\W//g;
398 $phonenum_check_method = 'ut_alpha';
400 $phonenum =~ s/\D//g;
401 $phonenum_check_method = 'ut_number';
403 $self->phonenum($phonenum);
405 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
408 $self->ut_numbern('svcnum')
409 || $self->ut_numbern('countrycode')
410 || $self->$phonenum_check_method('phonenum')
411 || $self->ut_anything('sip_password')
412 || $self->ut_numbern('pin')
413 || $self->ut_textn('phone_name')
414 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
415 || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
416 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
417 || $self->ut_numbern('lrn')
418 || $self->ut_numbern('lnp_desired_due_date')
419 || $self->ut_numbern('lnp_due_date')
420 || $self->ut_textn('lnp_other_provider')
421 || $self->ut_textn('lnp_other_provider_account')
422 || $self->ut_enumn('lnp_status', ['','portingin','portingout','portedin','native'])
423 || $self->ut_enumn('portable', ['','Y'])
425 return $error if $error;
427 return 'Name ('. $self->phone_name.
428 ") is longer than $phone_name_max characters"
429 if $phone_name_max && length($self->phone_name) > $phone_name_max;
431 $self->countrycode(1) unless $self->countrycode;
433 unless ( length($self->sip_password) ) {
436 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
444 =item _check duplicate
446 Internal method to check for duplicate phone numers.
450 #false laziness w/svc_acct.pm's _check_duplicate.
451 sub _check_duplicate {
454 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
455 return '' if $global_unique eq 'disabled';
460 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
461 qsearch( 'svc_phone', {
462 'countrycode' => $self->countrycode,
463 'phonenum' => $self->phonenum,
466 return gettext('phonenum_in_use')
467 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
469 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
470 unless ( $part_svc ) {
471 return 'unknown svcpart '. $self->svcpart;
474 if ( @dup_ccphonenum ) {
476 my $exports = FS::part_export::export_info('svc_phone');
477 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
479 foreach my $part_export ( $part_svc->part_export ) {
481 #this will catch to the same exact export
482 my @svcparts = map { $_->svcpart } $part_export->export_svc;
484 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
489 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
490 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
491 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
492 return "duplicate phone number ".
493 $self->countrycode. ' '. $self->phonenum.
494 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
495 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
507 Checks the supplied PIN against the PIN in the database. Returns true for a
508 sucessful authentication, false if no match.
513 my($self, $check_pin) = @_;
514 length($self->pin) && $check_pin eq $self->pin;
523 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
535 my $conf = new FS::Conf;
537 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
548 Returns any FS::phone_device records associated with this service.
554 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
557 #override location_Mixin version cause we want to try the cust_pkg location
558 #in between us and cust_main
559 # XXX what to do in the unlinked case??? return a pseudo-object that returns
561 sub cust_location_or_main {
563 return $self->cust_location if $self->locationnum;
564 my $cust_pkg = $self->cust_svc->cust_pkg;
565 $cust_pkg ? $cust_pkg->cust_location_or_main : '';
570 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this
571 service. By default, "associated with" means that either the "src" or the
572 "charged_party" field of the CDR matches the "phonenum" field of the service.
576 Accepts the following options:
578 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
580 =item status => "" (or "done"): Return only CDRs with that processing status.
582 =item inbound => 1: Return CDRs for inbound calls. With "status", will filter
583 on inbound processing status.
585 =item default_prefix => "XXX": Also accept the phone number of the service prepended
586 with the chosen prefix.
588 =item disable_src => 1: Only match on "charged_party", not "src".
590 =item by_svcnum: not supported for svc_phone
597 my($self, %options) = @_;
602 if ( $options{'inbound'} ) {
604 if ( exists($options{'status'}) ) {
605 # must be 'done' or ''
606 my $sq = 'EXISTS ( SELECT 1 FROM cdr_termination '.
607 'WHERE cdr.acctid = cdr_termination.acctid '.
608 'AND cdr_termination.status = \'done\' '.
609 'AND cdr_termination.termpart = 1 )';
610 if ( $options{'status'} eq 'done' ) {
613 elsif ($options{'status'} eq '' ) {
614 push @where, "NOT $sq";
617 warn "invalid status: $options{'status'} (ignored)\n";
622 @fields = ( 'charged_party' );
623 push @fields, 'src' if !$options{'disable_src'};
624 $hash{'freesidestatus'} = $options{'status'}
625 if exists($options{'status'});
628 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
630 my $number = $self->phonenum;
632 my $prefix = $options{'default_prefix'};
634 my @orwhere = map " $_ = '$number' ", @fields;
635 push @orwhere, map " $_ = '$prefix$number' ", @fields
637 if ( $prefix =~ /^\+(\d+)$/ ) {
638 push @orwhere, map " $_ = '$1$number' ", @fields
641 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
643 if ( $options{'begin'} ) {
644 push @where, 'startdate >= '. $options{'begin'};
646 if ( $options{'end'} ) {
647 push @where, 'startdate < '. $options{'end'};
650 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
656 'extra_sql' => $extra_sql,
657 'order_by' => "ORDER BY startdate $for_update",
669 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
670 L<FS::cust_pkg>, schema.html from the base documentation.