1 package FS::svc_Common;
2 use base qw( FS::cust_main_Mixin FS::Record );
5 use vars qw( $noexport_hack $DEBUG $me
6 $overlimit_missing_cust_svc_nonfatal_kludge );
7 use Carp qw( cluck carp croak confess ); #specify cluck have to specify them all
8 use Scalar::Util qw( blessed );
9 use Lingua::EN::Inflect qw( PL_N );
11 use FS::Record qw( qsearch qsearchs fields dbh );
16 use FS::inventory_item;
17 use FS::inventory_class;
18 use FS::NetworkMonitoringSystem;
20 $me = '[FS::svc_Common]';
23 $overlimit_missing_cust_svc_nonfatal_kludge = 0;
27 FS::svc_Common - Object method for all svc_ records
31 package svc_myservice;
32 use base qw( FS::svc_Common );
36 FS::svc_Common is intended as a base class for table-specific classes to
37 inherit from, i.e. FS::svc_acct. FS::svc_Common inherits from FS::Record.
49 my $class = ref($proto) || $proto;
51 bless ($self, $class);
53 unless ( defined ( $self->table ) ) {
54 $self->{'Table'} = shift;
55 carp "warning: FS::Record::new called with table name ". $self->{'Table'};
58 #$self->{'Hash'} = shift;
60 $self->{'Hash'} = { map { $_ => $newhash->{$_} } qw(svcnum svcpart) };
62 $self->setdefault( $self->_fieldhandlers )
65 $self->{'Hash'}{$_} = $newhash->{$_}
66 foreach grep { defined($newhash->{$_}) && length($newhash->{$_}) }
69 foreach my $field ( grep !defined($self->{'Hash'}{$_}), $self->fields ) {
70 $self->{'Hash'}{$field}='';
73 $self->_rebless if $self->can('_rebless');
75 $self->{'modified'} = 0;
77 $self->_cache($self->{'Hash'}, shift) if $self->can('_cache') && @_;
83 sub _fieldhandlers { {}; }
87 # This restricts the fields based on part_svc_column and the svcpart of
88 # the service. There are four possible cases:
89 # 1. svcpart passed as part of the svc_x hash.
90 # 2. svcpart fetched via cust_svc based on svcnum.
91 # 3. No svcnum or svcpart. In this case, return ALL the fields with
92 # dbtable eq $self->table.
93 # 4. Called via "fields('svc_acct')" or something similar. In this case
94 # there is no $self object.
98 my @vfields = $self->SUPER::virtual_fields;
100 return @vfields unless (ref $self); # Case 4
102 if ($self->svcpart) { # Case 1
103 $svcpart = $self->svcpart;
104 } elsif ( $self->svcnum
105 && qsearchs('cust_svc',{'svcnum'=>$self->svcnum} )
107 $svcpart = $self->cust_svc->svcpart;
112 if ($svcpart) { #Cases 1 and 2
113 my %flags = map { $_->columnname, $_->columnflag } (
114 qsearch ('part_svc_column', { svcpart => $svcpart } )
116 return grep { not ( defined($flags{$_}) && $flags{$_} eq 'X') } @vfields;
125 svc_Common provides a fallback label subroutine that just returns the svcnum.
131 cluck "warning: ". ref($self). " not loaded or missing label method; ".
143 (($self->cust_svc || return)->cust_pkg || return)->cust_main || return
148 defined($self->cust_main);
153 Checks the validity of fields in this record.
155 At present, this does nothing but call FS::Record::check (which, in turn,
156 does nothing but run virtual field checks).
165 =item insert [ , OPTION => VALUE ... ]
167 Adds this record to the database. If there is an error, returns the error,
168 otherwise returns false.
170 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
171 defined. An FS::cust_svc record will be created and inserted.
173 Currently available options are: I<jobnums>, I<child_objects> and
176 If I<jobnum> is set to an array reference, the jobnums of any export jobs will
177 be added to the referenced array.
179 If I<child_objects> is set to an array reference of FS::tablename objects
180 (for example, FS::svc_export_machine or FS::acct_snarf objects), they
181 will have their svcnum field set and will be inserted after this record,
182 but before any exports are run. Each element of the array can also
183 optionally be a two-element array reference containing the child object
184 and the name of an alternate field to be filled in with the newly-inserted
185 svcnum, for example C<[ $svc_forward, 'srcsvc' ]>
187 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
188 jobnums), all provisioning jobs will have a dependancy on the supplied
189 jobnum(s) (they will not run until the specific job(s) complete(s)).
191 If I<export_args> is set to an array reference, the referenced list will be
192 passed to export commands.
199 warn "[$me] insert called with options ".
200 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
204 local $FS::queue::jobnums = \@jobnums;
205 warn "[$me] insert: set \$FS::queue::jobnums to $FS::queue::jobnums\n"
207 my $objects = $options{'child_objects'} || [];
208 my $depend_jobnums = $options{'depend_jobnum'} || [];
209 $depend_jobnums = [ $depend_jobnums ] unless ref($depend_jobnums);
211 local $SIG{HUP} = 'IGNORE';
212 local $SIG{INT} = 'IGNORE';
213 local $SIG{QUIT} = 'IGNORE';
214 local $SIG{TERM} = 'IGNORE';
215 local $SIG{TSTP} = 'IGNORE';
216 local $SIG{PIPE} = 'IGNORE';
218 my $oldAutoCommit = $FS::UID::AutoCommit;
219 local $FS::UID::AutoCommit = 0;
222 my $svcnum = $self->svcnum;
223 my $cust_svc = $svcnum ? qsearchs('cust_svc',{'svcnum'=>$self->svcnum}) : '';
224 my $inserted_cust_svc = 0;
225 #unless ( $svcnum ) {
226 if ( !$svcnum or !$cust_svc ) {
227 $cust_svc = new FS::cust_svc ( {
228 #hua?# 'svcnum' => $svcnum,
229 'svcnum' => $self->svcnum,
230 'pkgnum' => $self->pkgnum,
231 'svcpart' => $self->svcpart,
233 my $error = $cust_svc->insert;
235 $dbh->rollback if $oldAutoCommit;
238 $inserted_cust_svc = 1;
239 $svcnum = $self->svcnum($cust_svc->svcnum);
241 #$cust_svc = qsearchs('cust_svc',{'svcnum'=>$self->svcnum});
242 unless ( $cust_svc ) {
243 $dbh->rollback if $oldAutoCommit;
244 return "no cust_svc record found for svcnum ". $self->svcnum;
246 $self->pkgnum($cust_svc->pkgnum);
247 $self->svcpart($cust_svc->svcpart);
250 my $error = $self->preinsert_hook_first
251 || $self->set_auto_inventory
253 || $self->_check_duplicate
254 || $self->preinsert_hook
255 || $self->SUPER::insert;
257 if ( $inserted_cust_svc ) {
258 my $derror = $cust_svc->delete;
259 die $derror if $derror;
261 $dbh->rollback if $oldAutoCommit;
265 foreach my $object ( @$objects ) {
267 if ( ref($object) eq 'ARRAY' ) {
268 ($obj, $field) = @$object;
273 $obj->$field($self->svcnum);
274 $error = $obj->insert;
276 $dbh->rollback if $oldAutoCommit;
282 unless ( $noexport_hack ) {
284 warn "[$me] insert: \$FS::queue::jobnums is $FS::queue::jobnums\n"
287 my $export_args = $options{'export_args'} || [];
289 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
290 my $error = $part_export->export_insert($self, @$export_args);
292 $dbh->rollback if $oldAutoCommit;
293 return "exporting to ". $part_export->exporttype.
294 " (transaction rolled back): $error";
298 foreach my $depend_jobnum ( @$depend_jobnums ) {
299 warn "[$me] inserting dependancies on supplied job $depend_jobnum\n"
301 foreach my $jobnum ( @jobnums ) {
302 my $queue = qsearchs('queue', { 'jobnum' => $jobnum } );
303 warn "[$me] inserting dependancy for job $jobnum on $depend_jobnum\n"
305 my $error = $queue->depend_insert($depend_jobnum);
307 $dbh->rollback if $oldAutoCommit;
308 return "error queuing job dependancy: $error";
315 my $nms_ip_error = $self->nms_ip_insert;
316 if ( $nms_ip_error ) {
317 $dbh->rollback if $oldAutoCommit;
318 return "error queuing IP insert: $nms_ip_error";
321 if ( exists $options{'jobnums'} ) {
322 push @{ $options{'jobnums'} }, @jobnums;
325 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
331 sub preinsert_hook_first { ''; }
332 sub _check_duplcate { ''; }
333 sub preinsert_hook { ''; }
334 sub table_dupcheck_fields { (); }
335 sub prereplace_hook { ''; }
336 sub prereplace_hook_first { ''; }
337 sub predelete_hook { ''; }
338 sub predelete_hook_first { ''; }
340 =item delete [ , OPTION => VALUE ... ]
342 Deletes this account from the database. If there is an error, returns the
343 error, otherwise returns false.
345 The corresponding FS::cust_svc record will be deleted as well.
352 my $export_args = $options{'export_args'} || [];
354 local $SIG{HUP} = 'IGNORE';
355 local $SIG{INT} = 'IGNORE';
356 local $SIG{QUIT} = 'IGNORE';
357 local $SIG{TERM} = 'IGNORE';
358 local $SIG{TSTP} = 'IGNORE';
359 local $SIG{PIPE} = 'IGNORE';
361 my $oldAutoCommit = $FS::UID::AutoCommit;
362 local $FS::UID::AutoCommit = 0;
365 my $error = $self->predelete_hook_first
366 || $self->SUPER::delete
367 || $self->export('delete', @$export_args)
368 || $self->return_inventory
369 || $self->release_router
370 || $self->predelete_hook
371 || $self->cust_svc->delete
374 $dbh->rollback if $oldAutoCommit;
378 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
385 Currently this will only run expire exports if any are attached
390 my($self,$date) = (shift,shift);
392 return 'Expire date must be specified' unless $date;
394 local $SIG{HUP} = 'IGNORE';
395 local $SIG{INT} = 'IGNORE';
396 local $SIG{QUIT} = 'IGNORE';
397 local $SIG{TERM} = 'IGNORE';
398 local $SIG{TSTP} = 'IGNORE';
399 local $SIG{PIPE} = 'IGNORE';
401 my $oldAutoCommit = $FS::UID::AutoCommit;
402 local $FS::UID::AutoCommit = 0;
405 my $export_args = [$date];
406 my $error = $self->export('expire', @$export_args);
408 $dbh->rollback if $oldAutoCommit;
412 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
417 =item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ]
419 Replaces OLD_RECORD with this one. If there is an error, returns the error,
420 otherwise returns false.
422 Currently available options are: I<child_objects>, I<export_args> and
425 If I<child_objects> is set to an array reference of FS::tablename objects
426 (for example, FS::svc_export_machine or FS::acct_snarf objects), they
427 will have their svcnum field set and will be inserted or replaced after
428 this record, but before any exports are run. Each element of the array
429 can also optionally be a two-element array reference containing the
430 child object and the name of an alternate field to be filled in with
431 the newly-inserted svcnum, for example C<[ $svc_forward, 'srcsvc' ]>
433 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
434 jobnums), all provisioning jobs will have a dependancy on the supplied
435 jobnum(s) (they will not run until the specific job(s) complete(s)).
437 If I<export_args> is set to an array reference, the referenced list will be
438 passed to export commands.
445 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
450 ( ref($_[0]) eq 'HASH' )
454 my $objects = $options->{'child_objects'} || [];
457 local $FS::queue::jobnums = \@jobnums;
458 warn "[$me] replace: set \$FS::queue::jobnums to $FS::queue::jobnums\n"
460 my $depend_jobnums = $options->{'depend_jobnum'} || [];
461 $depend_jobnums = [ $depend_jobnums ] unless ref($depend_jobnums);
463 local $SIG{HUP} = 'IGNORE';
464 local $SIG{INT} = 'IGNORE';
465 local $SIG{QUIT} = 'IGNORE';
466 local $SIG{TERM} = 'IGNORE';
467 local $SIG{TSTP} = 'IGNORE';
468 local $SIG{PIPE} = 'IGNORE';
470 my $oldAutoCommit = $FS::UID::AutoCommit;
471 local $FS::UID::AutoCommit = 0;
474 my $error = $new->prereplace_hook_first($old)
475 || $new->set_auto_inventory($old)
476 || $new->check; #redundant, but so any duplicate fields are
477 #maniuplated as appropriate (svc_phone.phonenum)
479 $dbh->rollback if $oldAutoCommit;
483 #if ( $old->username ne $new->username || $old->domsvc != $new->domsvc ) {
484 if ( grep { $old->$_ ne $new->$_ } $new->table_dupcheck_fields ) {
486 $new->svcpart( $new->cust_svc->svcpart ) unless $new->svcpart;
487 $error = $new->_check_duplicate;
489 $dbh->rollback if $oldAutoCommit;
494 $error = $new->SUPER::replace($old);
496 $dbh->rollback if $oldAutoCommit;
500 foreach my $object ( @$objects ) {
502 if ( ref($object) eq 'ARRAY' ) {
503 ($obj, $field) = @$object;
508 $obj->$field($new->svcnum);
510 my $oldobj = qsearchs( $obj->table, {
511 $field => $new->svcnum,
512 map { $_ => $obj->$_ } $obj->_svc_child_partfields,
516 my $pkey = $oldobj->primary_key;
517 $obj->$pkey($oldobj->$pkey);
518 $obj->replace($oldobj);
520 $error = $obj->insert;
523 $dbh->rollback if $oldAutoCommit;
529 unless ( $noexport_hack ) {
531 warn "[$me] replace: \$FS::queue::jobnums is $FS::queue::jobnums\n"
534 my $export_args = $options->{'export_args'} || [];
536 #not quite false laziness, but same pattern as FS::svc_acct::replace and
537 #FS::part_export::sqlradius::_export_replace. List::Compare or something
538 #would be useful but too much of a pain in the ass to deploy
540 my @old_part_export = $old->cust_svc->part_svc->part_export;
541 my %old_exportnum = map { $_->exportnum => 1 } @old_part_export;
542 my @new_part_export =
544 ? qsearchs('part_svc', { svcpart=>$new->svcpart } )->part_export
545 : $new->cust_svc->part_svc->part_export;
546 my %new_exportnum = map { $_->exportnum => 1 } @new_part_export;
548 foreach my $delete_part_export (
549 grep { ! $new_exportnum{$_->exportnum} } @old_part_export
551 my $error = $delete_part_export->export_delete($old, @$export_args);
553 $dbh->rollback if $oldAutoCommit;
554 return "error deleting, export to ". $delete_part_export->exporttype.
555 " (transaction rolled back): $error";
559 foreach my $replace_part_export (
560 grep { $old_exportnum{$_->exportnum} } @new_part_export
563 $replace_part_export->export_replace( $new, $old, @$export_args);
565 $dbh->rollback if $oldAutoCommit;
566 return "error exporting to ". $replace_part_export->exporttype.
567 " (transaction rolled back): $error";
571 foreach my $insert_part_export (
572 grep { ! $old_exportnum{$_->exportnum} } @new_part_export
574 my $error = $insert_part_export->export_insert($new, @$export_args );
576 $dbh->rollback if $oldAutoCommit;
577 return "error inserting export to ". $insert_part_export->exporttype.
578 " (transaction rolled back): $error";
582 foreach my $depend_jobnum ( @$depend_jobnums ) {
583 warn "[$me] inserting dependancies on supplied job $depend_jobnum\n"
585 foreach my $jobnum ( @jobnums ) {
586 my $queue = qsearchs('queue', { 'jobnum' => $jobnum } );
587 warn "[$me] inserting dependancy for job $jobnum on $depend_jobnum\n"
589 my $error = $queue->depend_insert($depend_jobnum);
591 $dbh->rollback if $oldAutoCommit;
592 return "error queuing job dependancy: $error";
599 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
605 Sets any fixed fields for this service (see L<FS::part_svc>). If there is an
606 error, returns the error, otherwise returns the FS::part_svc object (use ref()
607 to test the return). Usually called by the check method.
613 $self->setx('F', @_);
618 Sets all fields to their defaults (see L<FS::part_svc>), overriding their
619 current values. If there is an error, returns the error, otherwise returns
620 the FS::part_svc object (use ref() to test the return).
626 $self->setx('D', @_ );
629 =item set_default_and_fixed
633 sub set_default_and_fixed {
635 $self->setx( [ 'D', 'F' ], @_ );
638 =item setx FLAG | FLAG_ARRAYREF , [ CALLBACK_HASHREF ]
640 Sets fields according to the passed in flag or arrayref of flags.
642 Optionally, a hashref of field names and callback coderefs can be passed.
643 If a coderef exists for a given field name, instead of setting the field,
644 the coderef is called with the column value (part_svc_column.columnvalue)
645 as the single parameter.
652 my @x = ref($x) ? @$x : ($x);
653 my $coderef = scalar(@_) ? shift : $self->_fieldhandlers;
656 $self->ut_numbern('svcnum')
658 return $error if $error;
660 my $part_svc = $self->part_svc;
661 return "Unknown svcpart" unless $part_svc;
663 #set default/fixed/whatever fields from part_svc
665 foreach my $part_svc_column (
666 grep { my $f = $_->columnflag; grep { $f eq $_ } @x } #columnflag in @x
667 $part_svc->all_part_svc_column
670 my $columnname = $part_svc_column->columnname;
671 my $columnvalue = $part_svc_column->columnvalue;
673 $columnvalue = &{ $coderef->{$columnname} }( $self, $columnvalue )
674 if exists( $coderef->{$columnname} );
675 $self->setfield( $columnname, $columnvalue );
688 if ( $self->get('svcpart') ) {
689 $svcpart = $self->get('svcpart');
690 } elsif ( $self->svcnum && qsearchs('cust_svc', {'svcnum'=>$self->svcnum}) ) {
691 my $cust_svc = $self->cust_svc;
692 return "Unknown svcnum" unless $cust_svc;
693 $svcpart = $cust_svc->svcpart;
696 qsearchs( 'part_svc', { 'svcpart' => $svcpart } );
702 Returns the FS::svc_pbx record for this service, if any (see L<FS::svc_pbx>).
704 Only makes sense if the service has a pbxsvc field (currently, svc_phone and
709 # XXX FS::h_svc_{acct,phone} could have a history-aware svc_pbx override
713 return '' unless $self->pbxsvc;
714 qsearchs( 'svc_pbx', { 'svcnum' => $self->pbxsvc } );
719 Returns the title of the FS::svc_pbx record associated with this service, if
722 Only makes sense if the service has a pbxsvc field (currently, svc_phone and
729 my $svc_pbx = $self->svc_pbx or return '';
733 =item pbx_select_hash %OPTIONS
735 Can be called as an object method or a class method.
737 Returns a hash SVCNUM => TITLE ... representing the PBXes this customer
738 that may be associated with this service.
740 Currently available options are: I<pkgnum> I<svcpart>
742 Only makes sense if the service has a pbxsvc field (currently, svc_phone and
747 #false laziness w/svc_acct::domain_select_hash
748 sub pbx_select_hash {
749 my ($self, %options) = @_;
755 $part_svc = $self->part_svc;
756 $cust_pkg = $self->cust_svc->cust_pkg
760 $part_svc = qsearchs('part_svc', { 'svcpart' => $options{svcpart} })
761 if $options{'svcpart'};
763 $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $options{pkgnum} })
764 if $options{'pkgnum'};
766 if ($part_svc && ( $part_svc->part_svc_column('pbxsvc')->columnflag eq 'S'
767 || $part_svc->part_svc_column('pbxsvc')->columnflag eq 'F')) {
768 %pbxes = map { $_->svcnum => $_->title }
769 map { qsearchs('svc_pbx', { 'svcnum' => $_ }) }
770 split(',', $part_svc->part_svc_column('pbxsvc')->columnvalue);
771 } elsif ($cust_pkg) { # && !$conf->exists('svc_acct-alldomains') ) {
772 %pbxes = map { $_->svcnum => $_->title }
773 map { qsearchs('svc_pbx', { 'svcnum' => $_->svcnum }) }
774 map { qsearch('cust_svc', { 'pkgnum' => $_->pkgnum } ) }
775 qsearch('cust_pkg', { 'custnum' => $cust_pkg->custnum });
778 %pbxes = map { $_->svcnum => $_->title } qsearch('svc_pbx', {} );
781 if ($part_svc && $part_svc->part_svc_column('pbxsvc')->columnflag eq 'D') {
782 my $svc_pbx = qsearchs('svc_pbx',
783 { 'svcnum' => $part_svc->part_svc_column('pbxsvc')->columnvalue } );
785 $pbxes{$svc_pbx->svcnum} = $svc_pbx->title;
787 warn "unknown svc_pbx.svcnum for part_svc_column pbxsvc: ".
788 $part_svc->part_svc_column('pbxsvc')->columnvalue;
797 =item set_auto_inventory
799 Sets any fields which auto-populate from inventory (see L<FS::part_svc>), and
800 also check any manually populated inventory fields.
802 If there is an error, returns the error, otherwise returns false.
806 sub set_auto_inventory {
808 my $old = @_ ? shift : '';
811 $self->ut_numbern('svcnum')
813 return $error if $error;
815 my $part_svc = $self->part_svc;
816 return "Unkonwn svcpart" unless $part_svc;
818 local $SIG{HUP} = 'IGNORE';
819 local $SIG{INT} = 'IGNORE';
820 local $SIG{QUIT} = 'IGNORE';
821 local $SIG{TERM} = 'IGNORE';
822 local $SIG{TSTP} = 'IGNORE';
823 local $SIG{PIPE} = 'IGNORE';
825 my $oldAutoCommit = $FS::UID::AutoCommit;
826 local $FS::UID::AutoCommit = 0;
829 #set default/fixed/whatever fields from part_svc
830 my $table = $self->table;
831 foreach my $field ( grep { $_ ne 'svcnum' } $self->fields ) {
833 my $part_svc_column = $part_svc->part_svc_column($field);
834 my $columnflag = $part_svc_column->columnflag;
835 next unless $columnflag =~ /^[AM]$/;
837 next if $columnflag eq 'A' && $self->$field() ne '';
839 my $classnum = $part_svc_column->columnvalue;
842 if ( $columnflag eq 'A' && $self->$field() eq '' ) {
843 $hash{'svcnum'} = '';
844 } elsif ( $columnflag eq 'M' ) {
845 return "Select inventory item for $field" unless $self->getfield($field);
846 $hash{'item'} = $self->getfield($field);
847 my $chosen_classnum = $self->getfield($field.'_classnum');
848 if ( grep {$_ == $chosen_classnum} split(',', $classnum) ) {
849 $classnum = $chosen_classnum;
851 # otherwise the chosen classnum is either (all), or somehow not on
852 # the list, so ignore it and choose the first item that's in any
856 my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql(
858 'table' => 'inventory_item',
861 my $inventory_item = qsearchs({
862 'table' => 'inventory_item',
864 'extra_sql' => "AND classnum IN ($classnum) AND $agentnums_sql",
865 'order_by' => 'ORDER BY ( agentnum IS NULL ) '. #agent inventory first
866 ' LIMIT 1 FOR UPDATE',
869 unless ( $inventory_item ) {
870 # should really only be shown if columnflag eq 'A'...
871 $dbh->rollback if $oldAutoCommit;
872 my $message = 'Out of ';
873 my @classnums = split(',', $classnum);
874 foreach ( @classnums ) {
875 my $class = FS::inventory_class->by_key($_)
876 or return "Can't find inventory_class.classnum $_";
877 $message .= PL_N($class->classname);
878 if ( scalar(@classnums) > 2 ) { # english is hard
879 if ( $_ != $classnums[-1] ) {
883 if ( scalar(@classnums) > 1 and $_ == $classnums[-2] ) {
890 next if $columnflag eq 'M' && $inventory_item->svcnum == $self->svcnum;
892 $self->setfield( $field, $inventory_item->item );
893 #if $columnflag eq 'A' && $self->$field() eq '';
895 # release the old inventory item, if there was one
896 if ( $old && $old->$field() && $old->$field() ne $self->$field() ) {
897 my $old_inv = qsearchs({
898 'table' => 'inventory_item',
900 'svcnum' => $old->svcnum,
902 'extra_sql' => "AND classnum IN ($classnum) AND ".
903 '( ( svc_field IS NOT NULL AND svc_field = '.$dbh->quote($field).' )'.
904 ' OR ( svc_field IS NULL AND item = '. dbh->quote($old->$field).' )'.
908 $old_inv->svcnum('');
909 $old_inv->svc_field('');
910 my $oerror = $old_inv->replace;
912 $dbh->rollback if $oldAutoCommit;
913 return "Error unprovisioning inventory: $oerror";
916 warn "old inventory_item not found for $field ". $self->$field;
920 $inventory_item->svcnum( $self->svcnum );
921 $inventory_item->svc_field( $field );
922 my $ierror = $inventory_item->replace();
924 $dbh->rollback if $oldAutoCommit;
925 return "Error provisioning inventory: $ierror";
930 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
936 =item return_inventory
938 Release all inventory items attached to this service's fields. Call
939 when unprovisioning the service.
943 sub return_inventory {
946 local $SIG{HUP} = 'IGNORE';
947 local $SIG{INT} = 'IGNORE';
948 local $SIG{QUIT} = 'IGNORE';
949 local $SIG{TERM} = 'IGNORE';
950 local $SIG{TSTP} = 'IGNORE';
951 local $SIG{PIPE} = 'IGNORE';
953 my $oldAutoCommit = $FS::UID::AutoCommit;
954 local $FS::UID::AutoCommit = 0;
957 foreach my $inventory_item ( $self->inventory_item ) {
958 $inventory_item->svcnum('');
959 $inventory_item->svc_field('');
960 my $error = $inventory_item->replace();
962 $dbh->rollback if $oldAutoCommit;
963 return "Error returning inventory: $error";
967 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
974 Returns the inventory items associated with this svc_ record, as
975 FS::inventory_item objects (see L<FS::inventory_item>.
982 'table' => 'inventory_item',
983 'hashref' => { 'svcnum' => $self->svcnum, },
989 Delete any routers associated with this service. This will release their
990 address blocks, also.
996 my @routers = qsearch('router', { svcnum => $self->svcnum });
998 my $error = $_->delete;
999 return "$error (removing router '".$_->routername."')" if $error;
1007 Returns the cust_svc record associated with this svc_ record, as a FS::cust_svc
1008 object (see L<FS::cust_svc>).
1012 Runs export_suspend callbacks.
1019 my $export_args = $options{'export_args'} || [];
1020 $self->export('suspend', @$export_args);
1025 Runs export_unsuspend callbacks.
1032 my $export_args = $options{'export_args'} || [];
1033 $self->export('unsuspend', @$export_args);
1038 Runs export_links callbacks and returns the links.
1045 $self->export('links', $return);
1049 =item export_getsettings
1051 Runs export_getsettings callbacks and returns the two hashrefs.
1055 sub export_getsettings {
1059 my $error = $self->export('getsettings', \%settings, \%defaults);
1061 warn "error running export_getsetings: $error";
1062 return ( { 'error' => $error }, {} );
1064 ( \%settings, \%defaults );
1067 =item export_getstatus
1069 Runs export_getstatus callbacks and returns a two item list consisting of an
1070 HTML status and a status hashref.
1074 sub export_getstatus {
1078 my $error = $self->export('getstatus', \$html, \%hash);
1080 warn "error running export_getstatus: $error";
1081 return ( '', { 'error' => $error } );
1086 =item export_setstatus
1088 Runs export_setstatus callbacks. If there is an error, returns the error,
1089 otherwise returns false.
1093 sub export_setstatus { shift->_export_setstatus_X('setstatus', @_) }
1094 sub export_setstatus_listadd { shift->_export_setstatus_X('setstatus_listadd', @_) }
1095 sub export_setstatus_listdel { shift->_export_setstatus_X('setstatus_listdel', @_) }
1096 sub export_setstatus_vacationadd { shift->_export_setstatus_X('setstatus_vacationadd', @_) }
1097 sub export_setstatus_vacationdel { shift->_export_setstatus_X('setstatus_vacationdel', @_) }
1099 sub _export_setstatus_X {
1100 my( $self, $method, @args ) = @_;
1101 my $error = $self->export($method, @args);
1103 warn "error running export_$method: $error";
1109 =item export HOOK [ EXPORT_ARGS ]
1111 Runs the provided export hook (i.e. "suspend", "unsuspend") for this service.
1116 my( $self, $method ) = ( shift, shift );
1118 $method = "export_$method" unless $method =~ /^export_/;
1120 local $SIG{HUP} = 'IGNORE';
1121 local $SIG{INT} = 'IGNORE';
1122 local $SIG{QUIT} = 'IGNORE';
1123 local $SIG{TERM} = 'IGNORE';
1124 local $SIG{TSTP} = 'IGNORE';
1125 local $SIG{PIPE} = 'IGNORE';
1127 my $oldAutoCommit = $FS::UID::AutoCommit;
1128 local $FS::UID::AutoCommit = 0;
1132 unless ( $noexport_hack ) {
1133 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
1134 next unless $part_export->can($method);
1135 my $error = $part_export->$method($self, @_);
1137 $dbh->rollback if $oldAutoCommit;
1138 return "error exporting $method event to ". $part_export->exporttype.
1139 " (transaction rolled back): $error";
1144 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1151 Sets or retrieves overlimit date.
1157 #$self->cust_svc->overlimit(@_);
1158 my $cust_svc = $self->cust_svc;
1159 unless ( $cust_svc ) { #wtf?
1160 my $error = "$me overlimit: missing cust_svc record for svc_acct svcnum ".
1162 if ( $overlimit_missing_cust_svc_nonfatal_kludge ) {
1163 cluck "$error; continuing anyway as requested";
1169 $cust_svc->overlimit(@_);
1174 Stub - returns false (no error) so derived classes don't need to define this
1175 methods. Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
1177 This method is called *before* the deletion step which actually deletes the
1178 services. This method should therefore only be used for "pre-deletion"
1179 cancellation steps, if necessary.
1185 =item clone_suspended
1187 Constructor used by FS::part_export::_export_suspend fallback. Stub returning
1188 same object for svc_ classes which don't implement a suspension fallback
1189 (everything except svc_acct at the moment). Document better.
1193 sub clone_suspended {
1197 =item clone_kludge_unsuspend
1199 Constructor used by FS::part_export::_export_unsuspend fallback. Stub returning
1200 same object for svc_ classes which don't implement a suspension fallback
1201 (everything except svc_acct at the moment). Document better.
1205 sub clone_kludge_unsuspend {
1209 =item find_duplicates MODE FIELDS...
1211 Method used by _check_duplicate routines to find services with duplicate
1212 values in specified fields. Set MODE to 'global' to search across all
1213 services, or 'export' to limit to those that share one or more exports
1214 with this service. FIELDS is a list of field names; only services
1215 matching in all fields will be returned. Empty fields will be skipped.
1219 sub find_duplicates {
1224 my %search = map { $_ => $self->getfield($_) }
1225 grep { length($self->getfield($_)) } @fields;
1226 return () if !%search;
1227 my @dup = grep { ! $self->svcnum or $_->svcnum != $self->svcnum }
1228 qsearch( $self->table, \%search );
1230 return @dup if $mode eq 'global';
1231 die "incorrect find_duplicates mode '$mode'" if $mode ne 'export';
1233 my $exports = FS::part_export::export_info($self->table);
1234 my %conflict_svcparts;
1235 my $part_svc = $self->part_svc;
1236 foreach my $part_export ( $part_svc->part_export ) {
1237 %conflict_svcparts = map { $_->svcpart => 1 } $part_export->export_svc;
1239 return grep { $conflict_svcparts{$_->cust_svc->svcpart} } @dup;
1242 =item getstatus_html
1246 sub getstatus_html {
1249 my $part_svc = $self->cust_svc->part_svc;
1253 foreach my $export ( grep $_->can('export_getstatus'), $part_svc->part_export ) {
1254 my $export_html = '';
1256 $export->export_getstatus( $self, \$export_html, \%hash );
1257 $html .= $export_html;
1270 my $conf = new FS::Conf;
1271 return '' unless grep { $self->table eq $_ }
1272 $conf->config('nms-auto_add-svc_ips');
1273 my $ip_field = $self->table_info->{'ip_field'};
1275 my $queue = FS::queue->new( {
1276 'job' => 'FS::NetworkMonitoringSystem::queued_add_router',
1277 'svcnum' => $self->svcnum,
1279 $queue->insert( 'FS::NetworkMonitoringSystem',
1281 $conf->config('nms-auto_add-community')
1290 #XXX not yet implemented
1293 =item search_sql_field FIELD STRING
1295 Class method which returns an SQL fragment to search for STRING in FIELD.
1297 It is now case-insensitive by default.
1301 sub search_sql_field {
1302 my( $class, $field, $string ) = @_;
1303 my $table = $class->table;
1304 my $q_string = dbh->quote($string);
1305 "LOWER($table.$field) = LOWER($q_string)";
1308 #fallback for services that don't provide a search...
1310 #my( $class, $string ) = @_;
1314 =item search HASHREF
1316 Class method which returns a qsearch hash expression to search for parameters
1317 specified in HASHREF.
1323 =item unlinked - set to search for all unlinked services. Overrides all other options.
1333 =item pkgpart - arrayref
1335 =item routernum - arrayref
1337 =item sectornum - arrayref
1339 =item towernum - arrayref
1347 # svc_broadband::search should eventually use this instead
1349 my ($class, $params) = @_;
1352 'LEFT JOIN cust_svc USING ( svcnum )',
1353 'LEFT JOIN part_svc USING ( svcpart )',
1354 'LEFT JOIN cust_pkg USING ( pkgnum )',
1355 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg'),
1360 $class->_search_svc($params, \@from, \@where) if $class->can('_search_svc');
1363 # if ( $params->{'domain'} ) {
1364 # my $svc_domain = qsearchs('svc_domain', { 'domain'=>$params->{'domain'} } );
1365 # #preserve previous behavior & bubble up an error if $svc_domain not found?
1366 # push @where, 'domsvc = '. $svc_domain->svcnum if $svc_domain;
1370 # if ( $params->{'domsvc'} =~ /^(\d+)$/ ) {
1371 # push @where, "domsvc = $1";
1375 push @where, 'pkgnum IS NULL' if $params->{'unlinked'};
1378 if ( $params->{'agentnum'} =~ /^(\d+)$/ && $1 ) {
1379 push @where, "cust_main.agentnum = $1";
1383 if ( $params->{'custnum'} =~ /^(\d+)$/ && $1 ) {
1384 push @where, "custnum = $1";
1388 if ( $params->{'cust_status'} =~ /^([a-z]+)$/ ) {
1389 push @where, FS::cust_main->cust_status_sql . " = '$1'";
1393 if ( $params->{'balance'} =~ /^\s*(\-?\d*(\.\d{1,2})?)\s*$/ && length($1) ) {
1397 if ( $params->{'balance_days'} =~ /^\s*(\d*(\.\d{1,3})?)\s*$/ && length($1) ) {
1398 $age = time - 86400 * $1;
1400 push @where, FS::cust_main->balance_date_sql($age) . " > $balance";
1404 if ( $params->{'payby'} && scalar(@{ $params->{'payby'} }) ) {
1405 my @payby = map "'$_'", grep /^(\w+)$/, @{ $params->{'payby'} };
1406 push @where, 'payby IN ('. join(',', @payby ). ')';
1410 ##pkgpart, now properly untainted, can be arrayref
1411 #for my $pkgpart ( $params->{'pkgpart'} ) {
1412 # if ( ref $pkgpart ) {
1413 # my $where = join(',', map { /^(\d+)$/ ? $1 : () } @$pkgpart );
1414 # push @where, "cust_pkg.pkgpart IN ($where)" if $where;
1416 # elsif ( $pkgpart =~ /^(\d+)$/ ) {
1417 # push @where, "cust_pkg.pkgpart = $1";
1420 if ( $params->{'pkgpart'} ) {
1421 my @pkgpart = ref( $params->{'pkgpart'} )
1422 ? @{ $params->{'pkgpart'} }
1423 : $params->{'pkgpart'}
1424 ? ( $params->{'pkgpart'} )
1426 @pkgpart = grep /^(\d+)$/, @pkgpart;
1427 push @where, 'cust_pkg.pkgpart IN ('. join(',', @pkgpart ). ')' if @pkgpart;
1431 if ( $params->{'svcnum'} =~ /^(\d+)$/ ) {
1432 push @where, "svcnum = $1";
1436 if ( $params->{'svcpart'} ) {
1437 my @svcpart = ref( $params->{'svcpart'} )
1438 ? @{ $params->{'svcpart'} }
1439 : $params->{'svcpart'}
1440 ? ( $params->{'svcpart'} )
1442 @svcpart = grep /^(\d+)$/, @svcpart;
1443 push @where, 'svcpart IN ('. join(',', @svcpart ). ')' if @svcpart;
1446 if ( $params->{'exportnum'} =~ /^(\d+)$/ ) {
1447 push @from, ' LEFT JOIN export_svc USING ( svcpart )';
1448 push @where, "exportnum = $1";
1451 # # sector and tower
1452 # my @where_sector = $class->tower_sector_sql($params);
1453 # if ( @where_sector ) {
1454 # push @where, @where_sector;
1455 # push @from, ' LEFT JOIN tower_sector USING ( sectornum )';
1458 # here is the agent virtualization
1459 #if ($params->{CurrentUser}) {
1461 # qsearchs('access_user', { username => $params->{CurrentUser} });
1463 # if ($access_user) {
1464 # push @where, $access_user->agentnums_sql('table'=>'cust_main');
1466 # push @where, "1=0";
1469 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql(
1470 'table' => 'cust_main',
1471 'null_right' => 'View/link unlinked services',
1475 push @where, @{ $params->{'where'} } if $params->{'where'};
1477 my $addl_from = join(' ', @from);
1478 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
1480 my $table = $class->table;
1482 my $count_query = "SELECT COUNT(*) FROM $table $addl_from $extra_sql";
1483 #if ( keys %svc_X ) {
1484 # $count_query .= ' WHERE '.
1485 # join(' AND ', map "$_ = ". dbh->quote($svc_X{$_}),
1493 'select' => join(', ',
1496 'cust_main.custnum',
1497 @{ $params->{'addl_select'} || [] },
1498 FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
1500 'addl_from' => $addl_from,
1501 'extra_sql' => $extra_sql,
1502 'order_by' => $params->{'order_by'},
1503 'count_query' => $count_query,
1512 The setfixed method return value.
1514 B<export> method isn't used by insert and replace methods yet.
1518 L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, schema.html
1519 from the base documentation.