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;
17 $me = '[' . __PACKAGE__ . ']';
21 @pw_set = ( 'a'..'k', 'm','n', 'p-z', 'A'..'N', 'P'..'Z' , '2'..'9' );
23 #ask FS::UID to run this stuff for us later
24 $FS::UID::callback{'FS::svc_acct'} = sub {
26 $phone_name_max = $conf->config('svc_phone-phone_name-max_length');
31 FS::svc_phone - Object methods for svc_phone records
37 $record = new FS::svc_phone \%hash;
38 $record = new FS::svc_phone { 'column' => 'value' };
40 $error = $record->insert;
42 $error = $new_record->replace($old_record);
44 $error = $record->delete;
46 $error = $record->check;
48 $error = $record->suspend;
50 $error = $record->unsuspend;
52 $error = $record->cancel;
56 An FS::svc_phone object represents a phone number. FS::svc_phone inherits
57 from FS::Record. The following fields are currently supported:
79 Optional svcnum from svc_pbx
89 Creates a new phone number. To add the number to the database, see L<"insert">.
91 Note that this stores the hash reference, not a distinct copy of the hash it
92 points to. You can ask the object for a copy with the I<hash> method.
96 # the new method can be inherited from FS::Record, if a table method is defined
100 'name' => 'Phone number',
101 'sorts' => 'phonenum',
102 'display_weight' => 60,
103 'cancel_weight' => 80,
105 'countrycode' => { label => 'Country code',
107 disable_inventory => 1,
110 'phonenum' => 'Phone number',
111 'pin' => { label => 'Personal Identification Number',
113 disable_inventory => 1,
116 'sip_password' => 'SIP password',
117 'phone_name' => 'Name',
118 'pbxsvc' => { label => 'PBX',
119 type => 'select-svc_pbx.html',
120 disable_inventory => 1,
121 disable_select => 1, #UI wonky, pry works otherwise
126 select_table => 'svc_domain',
127 select_key => 'svcnum',
128 select_label => 'domain',
129 disable_inventory => 1,
132 label => 'E911 location',
133 disable_inventory => 1,
140 sub table { 'svc_phone'; }
142 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
144 =item search_sql STRING
146 Class method which returns an SQL fragment to search for the given string.
151 my( $class, $string ) = @_;
153 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
159 my $conf = new FS::Conf;
160 my $ccode = ( $conf->exists('default_phone_countrycode')
161 && $conf->config('default_phone_countrycode')
163 ? $conf->config('default_phone_countrycode')
166 $string =~ s/^$ccode//;
168 $class->search_sql_field('phonenum', $string );
173 Returns the phone number.
179 my $phonenum = $self->phonenum; #XXX format it better
180 my $label = $phonenum;
181 $label .= '@'.$self->domain if $self->domsvc;
182 $label .= ' ('.$self->phone_name.')' if $self->phone_name;
188 Adds this phone number to the database. If there is an error, returns the
189 error, otherwise returns false.
198 warn "[$me] insert called on $self: ". Dumper($self).
199 "\nwith options: ". Dumper(%options);
202 local $SIG{HUP} = 'IGNORE';
203 local $SIG{INT} = 'IGNORE';
204 local $SIG{QUIT} = 'IGNORE';
205 local $SIG{TERM} = 'IGNORE';
206 local $SIG{TSTP} = 'IGNORE';
207 local $SIG{PIPE} = 'IGNORE';
209 my $oldAutoCommit = $FS::UID::AutoCommit;
210 local $FS::UID::AutoCommit = 0;
213 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
214 #make it more of a base class than a mixin... :)
215 if ( $options{'cust_location'}
216 && ( ! $self->locationnum || $self->locationnum == -1 ) ) {
217 my $error = $options{'cust_location'}->insert;
219 $dbh->rollback if $oldAutoCommit;
220 return "inserting cust_location (transaction rolled back): $error";
222 $self->locationnum( $options{'cust_location'}->locationnum );
224 #what about on-the-fly edits? if the ui supports it?
226 my $error = $self->SUPER::insert(%options);
228 $dbh->rollback if $oldAutoCommit;
232 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
239 Delete this record from the database.
246 local $SIG{HUP} = 'IGNORE';
247 local $SIG{INT} = 'IGNORE';
248 local $SIG{QUIT} = 'IGNORE';
249 local $SIG{TERM} = 'IGNORE';
250 local $SIG{TSTP} = 'IGNORE';
251 local $SIG{PIPE} = 'IGNORE';
253 my $oldAutoCommit = $FS::UID::AutoCommit;
254 local $FS::UID::AutoCommit = 0;
257 foreach my $phone_device ( $self->phone_device ) {
258 my $error = $phone_device->delete;
260 $dbh->rollback if $oldAutoCommit;
265 my $error = $self->SUPER::delete;
267 $dbh->rollback if $oldAutoCommit;
271 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
276 # the delete method can be inherited from FS::Record
278 =item replace OLD_RECORD
280 Replaces the OLD_RECORD with this one in the database. If there is an error,
281 returns the error, otherwise returns false.
288 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
295 warn "[$me] replacing $old with $new\n".
296 "\nwith options: ". Dumper(%options);
299 local $SIG{HUP} = 'IGNORE';
300 local $SIG{INT} = 'IGNORE';
301 local $SIG{QUIT} = 'IGNORE';
302 local $SIG{TERM} = 'IGNORE';
303 local $SIG{TSTP} = 'IGNORE';
304 local $SIG{PIPE} = 'IGNORE';
306 my $oldAutoCommit = $FS::UID::AutoCommit;
307 local $FS::UID::AutoCommit = 0;
310 #false laziness w/cust_pkg.pm... move this to location_Mixin? that would
311 #make it more of a base class than a mixin... :)
312 if ( $options{'cust_location'}
313 && ( ! $new->locationnum || $new->locationnum == -1 ) ) {
314 my $error = $options{'cust_location'}->insert;
316 $dbh->rollback if $oldAutoCommit;
317 return "inserting cust_location (transaction rolled back): $error";
319 $new->locationnum( $options{'cust_location'}->locationnum );
321 #what about on-the-fly edits? if the ui supports it?
323 my $error = $new->SUPER::replace($old, %options);
325 $dbh->rollback if $oldAutoCommit;
326 return $error if $error;
329 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
335 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
339 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
343 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
347 Checks all fields to make sure this is a valid phone number. If there is
348 an error, returns the error, otherwise returns false. Called by the insert
353 # the check method should currently be supplied - FS::Record contains some
354 # data checking routines
359 my $conf = new FS::Conf;
361 my $phonenum = $self->phonenum;
362 my $phonenum_check_method;
363 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
364 $phonenum =~ s/\W//g;
365 $phonenum_check_method = 'ut_alpha';
367 $phonenum =~ s/\D//g;
368 $phonenum_check_method = 'ut_number';
370 $self->phonenum($phonenum);
372 $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
375 $self->ut_numbern('svcnum')
376 || $self->ut_numbern('countrycode')
377 || $self->$phonenum_check_method('phonenum')
378 || $self->ut_anything('sip_password')
379 || $self->ut_numbern('pin')
380 || $self->ut_textn('phone_name')
381 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
382 || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
383 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
385 return $error if $error;
387 return 'Name ('. $self->phone_name.
388 ") is longer than $phone_name_max characters"
389 if $phone_name_max && length($self->phone_name) > $phone_name_max;
391 $self->countrycode(1) unless $self->countrycode;
393 unless ( length($self->sip_password) ) {
396 join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
404 =item _check duplicate
406 Internal method to check for duplicate phone numers.
410 #false laziness w/svc_acct.pm's _check_duplicate.
411 sub _check_duplicate {
414 my $global_unique = $conf->config('global_unique-phonenum') || 'none';
415 return '' if $global_unique eq 'disabled';
420 grep { !$self->svcnum || $_->svcnum != $self->svcnum }
421 qsearch( 'svc_phone', {
422 'countrycode' => $self->countrycode,
423 'phonenum' => $self->phonenum,
426 return gettext('phonenum_in_use')
427 if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
429 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
430 unless ( $part_svc ) {
431 return 'unknown svcpart '. $self->svcpart;
434 if ( @dup_ccphonenum ) {
436 my $exports = FS::part_export::export_info('svc_phone');
437 my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
439 foreach my $part_export ( $part_svc->part_export ) {
441 #this will catch to the same exact export
442 my @svcparts = map { $_->svcpart } $part_export->export_svc;
444 $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
449 foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
450 my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
451 if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
452 return "duplicate phone number ".
453 $self->countrycode. ' '. $self->phonenum.
454 ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
455 " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
467 Checks the supplied PIN against the PIN in the database. Returns true for a
468 sucessful authentication, false if no match.
473 my($self, $check_pin) = @_;
474 length($self->pin) && $check_pin eq $self->pin;
483 #XXX Session-Timeout! holy shit, need rlm_perl to ask for this in realtime
495 my $conf = new FS::Conf;
497 $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
508 Returns any FS::phone_device records associated with this service.
514 qsearch('phone_device', { 'svcnum' => $self->svcnum } );
517 #override location_Mixin version cause we want to try the cust_pkg location
518 #in between us and cust_main
519 # XXX what to do in the unlinked case??? return a pseudo-object that returns
521 sub cust_location_or_main {
523 return $self->cust_location if $self->locationnum;
524 my $cust_pkg = $self->cust_svc->cust_pkg;
525 $cust_pkg ? $cust_pkg->cust_location_or_main : '';
530 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this
531 service. By default, "associated with" means that either the "src" or the
532 "charged_party" field of the CDR matches the "phonenum" field of the service.
536 Accepts the following options:
538 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
540 =item status => "" (or "done"): Return only CDRs with that processing status.
542 =item inbound => 1: Return CDRs for inbound calls. With "status", will filter
543 on inbound processing status.
545 =item default_prefix => "XXX": Also accept the phone number of the service prepended
546 with the chosen prefix.
548 =item disable_src => 1: Only match on "charged_party", not "src".
555 my($self, %options) = @_;
560 if ( $options{'inbound'} ) {
562 if ( exists($options{'status'}) ) {
563 # must be 'done' or ''
564 my $sq = 'EXISTS ( SELECT 1 FROM cdr_termination '.
565 'WHERE cdr.acctid = cdr_termination.acctid '.
566 'AND cdr_termination.status = \'done\' '.
567 'AND cdr_termination.termpart = 1 )';
568 if ( $options{'status'} eq 'done' ) {
571 elsif ($options{'status'} eq '' ) {
572 push @where, "NOT $sq";
575 warn "invalid status: $options{'status'} (ignored)\n";
580 @fields = ( 'charged_party' );
581 push @fields, 'src' if !$options{'disable_src'};
582 $hash{'freesidestatus'} = $options{'status'}
583 if exists($options{'status'});
586 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
588 my $number = $self->phonenum;
590 my $prefix = $options{'default_prefix'};
592 my @orwhere = map " $_ = '$number' ", @fields;
593 push @orwhere, map " $_ = '$prefix$number' ", @fields
595 if ( $prefix =~ /^\+(\d+)$/ ) {
596 push @orwhere, map " $_ = '$1$number' ", @fields
599 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
601 if ( $options{'begin'} ) {
602 push @where, 'startdate >= '. $options{'begin'};
604 if ( $options{'end'} ) {
605 push @where, 'startdate < '. $options{'end'};
608 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
614 'extra_sql' => $extra_sql,
615 'order_by' => "ORDER BY startdate $for_update",
627 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
628 L<FS::cust_pkg>, schema.html from the base documentation.