4 use vars qw( @ISA $DEBUG );
6 use FS::Record qw( qsearch qsearchs fields dbh );
7 use FS::Schema qw( dbdef );
8 use FS::part_svc_column;
13 @ISA = qw(FS::Record);
19 FS::part_svc - Object methods for part_svc objects
25 $record = new FS::part_svc \%hash
26 $record = new FS::part_svc { 'column' => 'value' };
28 $error = $record->insert;
29 $error = $record->insert( [ 'pseudofield' ] );
30 $error = $record->insert( [ 'pseudofield' ], \%exportnums );
32 $error = $new_record->replace($old_record);
33 $error = $new_record->replace($old_record, '1.3-COMPAT', [ 'pseudofield' ] );
34 $error = $new_record->replace($old_record, '1.3-COMPAT', [ 'pseudofield' ], \%exportnums );
36 $error = $record->delete;
38 $error = $record->check;
42 An FS::part_svc represents a service definition. FS::part_svc inherits from
43 FS::Record. The following fields are currently supported:
47 =item svcpart - primary key (assigned automatically for new service definitions)
49 =item svc - text name of this service definition
51 =item svcdb - table used for this service. See L<FS::svc_acct>,
52 L<FS::svc_domain>, and L<FS::svc_forward>, among others.
54 =item disabled - Disabled flag, empty or `Y'
56 =item preserve - Preserve after cancellation, empty or 'Y'
66 Creates a new service definition. To add the service definition to the
67 database, see L<"insert">.
71 sub table { 'part_svc'; }
73 =item insert [ EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ]
75 Adds this service definition to the database. If there is an error, returns
76 the error, otherwise returns false.
78 The following pseudo-fields may be defined, and will be maintained in
79 the part_svc_column table appropriately (see L<FS::part_svc_column>).
83 =item I<svcdb>__I<field> - Default or fixed value for I<field> in I<svcdb>.
85 =item I<svcdb>__I<field>_flag - defines I<svcdb>__I<field> action: null or empty (no default), `D' for default, `F' for fixed (unchangeable), , `S' for selectable choice, `M' for manual selection from inventory, or `A' for automatic selection from inventory. For virtual fields, can also be 'X' for excluded.
89 If you want to add part_svc_column records for fields that do not exist as
90 fields in the I<svcdb> table, make sure to list then in
91 EXTRA_FIELDS_ARRAYREF also.
93 If EXPORTNUMS_HASHREF is specified (keys are exportnums and values are
94 boolean), the appopriate export_svc records will be inserted.
104 @fields = @{shift(@_)} if @_;
106 my $exportnums = shift;
107 @exportnums = grep $exportnums->{$_}, keys %$exportnums;
112 local $SIG{HUP} = 'IGNORE';
113 local $SIG{INT} = 'IGNORE';
114 local $SIG{QUIT} = 'IGNORE';
115 local $SIG{TERM} = 'IGNORE';
116 local $SIG{TSTP} = 'IGNORE';
117 local $SIG{PIPE} = 'IGNORE';
119 my $oldAutoCommit = $FS::UID::AutoCommit;
120 local $FS::UID::AutoCommit = 0;
123 my $error = $self->SUPER::insert;
125 $dbh->rollback if $oldAutoCommit;
129 # add part_svc_column records
131 my $svcdb = $self->svcdb;
132 # my @rows = map { /^${svcdb}__(.*)$/; $1 }
134 # grep /^${svcdb}__/,
135 # fields('part_svc');
137 grep { $_ ne 'svcnum'
138 && ( defined( $self->getfield($svcdb.'__'.$_.'_flag') )
139 || $self->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
140 } (fields($svcdb), @fields)
142 my $part_svc_column = $self->part_svc_column($field);
143 my $previous = qsearchs('part_svc_column', {
144 'svcpart' => $self->svcpart,
145 'columnname' => $field,
148 my $flag = $self->getfield($svcdb.'__'.$field.'_flag');
149 my $label = $self->getfield($svcdb.'__'.$field.'_label');
150 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
152 if ( uc($flag) =~ /^([A-Z])$/ ) {
153 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
155 $part_svc_column->setfield('columnflag', $1);
156 $part_svc_column->setfield('columnvalue',
157 &$parser($self->getfield($svcdb.'__'.$field))
161 $part_svc_column->setfield('columnlabel', $label)
162 if $label !~ /^\s*$/;
165 $error = $part_svc_column->replace($previous);
167 $error = $part_svc_column->insert;
171 $error = $previous ? $previous->delete : '';
174 $dbh->rollback if $oldAutoCommit;
180 # add export_svc records
181 my $slice = 100/scalar(@exportnums) if @exportnums;
183 foreach my $exportnum ( @exportnums ) {
184 my $export_svc = new FS::export_svc ( {
185 'exportnum' => $exportnum,
186 'svcpart' => $self->svcpart,
188 $error = $export_svc->insert($job, $slice*$done++, $slice);
190 $dbh->rollback if $oldAutoCommit;
195 # XXX shouldn't this update fixed values?
197 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
204 Currently unimplemented. Set the "disabled" field instead.
209 return "Can't (yet?) delete service definitions.";
210 # check & make sure the svcpart isn't in cust_svc or pkg_svc (in any packages)?
213 =item replace OLD_RECORD [ '1.3-COMPAT' [ , EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ] ]
215 Replaces OLD_RECORD with this one in the database. If there is an error,
216 returns the error, otherwise returns false.
220 TODOC: EXTRA_FIELDS_ARRAYREF (same as insert method)
227 my ( $new, $old ) = ( shift, shift );
232 if ( @_ && $_[0] eq '1.3-COMPAT' ) {
235 @fields = @{shift(@_)} if @_;
236 $exportnums = @_ ? shift : '';
239 return 'non-1.3-COMPAT interface not yet written';
243 return "Can't change svcdb for an existing service definition!"
244 unless $old->svcdb eq $new->svcdb;
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 my $error = $new->SUPER::replace( $old );
259 $dbh->rollback if $oldAutoCommit;
263 if ( $compat eq '1.3' ) {
265 # maintain part_svc_column records
267 my $svcdb = $new->svcdb;
269 grep { $_ ne 'svcnum'
270 && ( defined( $new->getfield($svcdb.'__'.$_.'_flag') )
271 || $new->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
272 } (fields($svcdb),@fields)
275 my $part_svc_column = $new->part_svc_column($field);
276 my $previous = qsearchs('part_svc_column', {
277 'svcpart' => $new->svcpart,
278 'columnname' => $field,
281 my $flag = $new->getfield($svcdb.'__'.$field.'_flag');
282 my $label = $new->getfield($svcdb.'__'.$field.'_label');
284 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
286 if ( uc($flag) =~ /^([A-Z])$/ ) {
287 $part_svc_column->setfield('columnflag', $1);
288 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
290 $part_svc_column->setfield('columnvalue',
291 &$parser($new->getfield($svcdb.'__'.$field))
294 $part_svc_column->setfield('columnflag', '');
295 $part_svc_column->setfield('columnvalue', '');
298 $part_svc_column->setfield('columnlabel', $label)
299 if $label !~ /^\s*$/;
302 $error = $part_svc_column->replace($previous);
304 $error = $part_svc_column->insert;
307 $error = $previous ? $previous->delete : '';
310 $dbh->rollback if $oldAutoCommit;
315 # maintain export_svc records
319 #false laziness w/ edit/process/agent_type.cgi
320 my @new_export_svc = ();
321 foreach my $part_export ( qsearch('part_export', {}) ) {
322 my $exportnum = $part_export->exportnum;
324 'exportnum' => $exportnum,
325 'svcpart' => $new->svcpart,
327 my $export_svc = qsearchs('export_svc', $hashref);
329 if ( $export_svc && ! $exportnums->{$exportnum} ) {
330 $error = $export_svc->delete;
332 $dbh->rollback if $oldAutoCommit;
335 } elsif ( ! $export_svc && $exportnums->{$exportnum} ) {
336 push @new_export_svc, new FS::export_svc ( $hashref );
341 my $slice = 100/scalar(@new_export_svc) if @new_export_svc;
343 foreach my $export_svc (@new_export_svc) {
344 $error = $export_svc->insert($job, $slice*$done++, $slice);
346 $dbh->rollback if $oldAutoCommit;
350 $error = $job->update_statustext( int( $slice * $done ) );
352 $dbh->rollback if $oldAutoCommit;
361 $dbh->rollback if $oldAutoCommit;
362 return 'non-1.3-COMPAT interface not yet written';
366 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
373 Checks all fields to make sure this is a valid service definition. If there is
374 an error, returns the error, otherwise returns false. Called by the insert
384 $self->ut_numbern('svcpart')
385 || $self->ut_text('svc')
386 || $self->ut_alpha('svcdb')
387 || $self->ut_enum('disabled', [ '', 'Y' ] )
388 || $self->ut_enum('preserve', [ '', 'Y' ] )
389 || $self->ut_enum('selfservice_access', [ '', 'hidden', 'readonly' ] )
391 return $error if $error;
393 my @fields = eval { fields( $self->svcdb ) }; #might die
394 return "Unknown svcdb: ". $self->svcdb. " (Error: $@)"
400 =item part_svc_column COLUMNNAME
402 Returns the part_svc_column object (see L<FS::part_svc_column>) for the given
403 COLUMNNAME, or a new part_svc_column object if none exists.
407 sub part_svc_column {
408 my( $self, $columnname) = @_;
410 qsearchs('part_svc_column', {
411 'svcpart' => $self->svcpart,
412 'columnname' => $columnname,
414 ) or new FS::part_svc_column {
415 'svcpart' => $self->svcpart,
416 'columnname' => $columnname,
420 =item all_part_svc_column
424 sub all_part_svc_column {
426 qsearch('part_svc_column', { 'svcpart' => $self->svcpart } );
429 =item part_export [ EXPORTTYPE ]
431 Returns a list of all exports (see L<FS::part_export>) for this service, or,
432 if an export type is specified, only returns exports of the given type.
439 $search{'exporttype'} = shift if @_;
440 sort { $a->weight <=> $b->weight }
441 map { qsearchs('part_export', { 'exportnum' => $_->exportnum, %search } ) }
442 qsearch('export_svc', { 'svcpart' => $self->svcpart } );
445 =item part_export_usage
447 Returns a list of any exports (see L<FS::part_export>) for this service that
448 are capable of reporting usage information.
452 sub part_export_usage {
454 grep $_->can('usage_sessions'), $self->part_export;
457 =item part_export_did
459 Returns a list of any exports (see L<FS::part_export>) for this service that
460 are capable of returing available DID (phone number) information.
464 sub part_export_did {
466 grep $_->can('get_dids'), $self->part_export;
469 =item part_export_dsl_pull
471 Returns a list of any exports (see L<FS::part_export>) for this service that
472 are capable of pulling/pushing DSL orders.
476 sub part_export_dsl_pull {
478 grep $_->can('dsl_pull'), $self->part_export;
481 =item cust_svc [ PKGPART ]
483 Returns a list of associated customer services (FS::cust_svc records).
485 If a PKGPART is specified, returns the customer services which are contained
486 within packages of that type (see L<FS::part_pkg>). If PKGPARTis specified as
487 B<0>, returns unlinked customer services.
494 my $hashref = { 'svcpart' => $self->svcpart };
496 my( $addl_from, $extra_sql ) = ( '', '' );
499 if ( $pkgpart =~ /^(\d+)$/ ) {
500 $addl_from = 'LEFT JOIN cust_pkg USING ( pkgnum )';
501 $extra_sql = "AND pkgpart = $1";
502 } elsif ( $pkgpart eq '0' ) {
503 $hashref->{'pkgnum'} = '';
508 'table' => 'cust_svc',
509 'addl_from' => $addl_from,
510 'hashref' => $hashref,
511 'extra_sql' => $extra_sql,
515 =item num_cust_svc [ PKGPART ]
517 Returns the number of associated customer services (FS::cust_svc records).
519 If a PKGPART is specified, returns the number of customer services which are
520 contained within packages of that type (see L<FS::part_pkg>). If PKGPART
521 is specified as B<0>, returns the number of unlinked customer services.
528 my @param = ( $self->svcpart );
530 my( $join, $and ) = ( '', '' );
534 $join = 'LEFT JOIN cust_pkg USING ( pkgnum )';
535 $and = 'AND pkgpart = ?';
536 push @param, $pkgpart;
537 } elsif ( $pkgpart eq '0' ) {
538 $and = 'AND pkgnum IS NULL';
542 my $sth = dbh->prepare(
543 "SELECT COUNT(*) FROM cust_svc $join WHERE svcpart = ? $and"
544 ) or die dbh->errstr;
545 $sth->execute(@param)
547 $sth->fetchrow_arrayref->[0];
552 Returns a list of associated FS::svc_* records.
558 map { $_->svc_x } $self->cust_svc;
572 return $svc_defs if $svc_defs; #cache
574 my $conf = new FS::Conf;
576 #false laziness w/part_pkg.pm::plan_info
579 foreach my $INC ( @INC ) {
580 warn "globbing $INC/FS/svc_*.pm\n" if $DEBUG;
581 foreach my $file ( glob("$INC/FS/svc_*.pm") ) {
583 warn "attempting to load service table info from $file\n" if $DEBUG;
584 $file =~ /\/(\w+)\.pm$/ or do {
585 warn "unrecognized file in $INC/FS/: $file\n";
590 if ( $mod =~ /^svc_[A-Z]/ or $mod =~ /^svc_acct_pop$/ ) {
591 warn "skipping FS::$mod" if $DEBUG;
595 eval "use FS::$mod;";
597 die "error using FS::$mod (skipping): $@\n" if $@;
600 unless ( UNIVERSAL::can("FS::$mod", 'table_info') ) {
601 warn "FS::$mod has no table_info method; skipping";
605 my $info = "FS::$mod"->table_info;
606 unless ( keys %$info ) {
607 warn "FS::$mod->table_info doesn't return info, skipping\n";
610 warn "got info from FS::$mod: $info\n" if $DEBUG;
611 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
612 warn "skipping disabled service FS::$mod" if $DEBUG;
619 tie my %svc_defs, 'Tie::IxHash',
620 map { $_ => $info{$_}->{'fields'} }
621 sort { $info{$a}->{'display_weight'} <=> $info{$b}->{'display_weight'} }
625 $svc_defs = \%svc_defs; #cache
631 Returns a list of all svc_ tables.
637 my $svc_defs = $class->_svc_defs;
638 grep { defined( dbdef->table($_) ) } keys %$svc_defs;
641 =item svc_table_fields TABLE
643 Given a table name, returns a hashref of field names. The field names
644 returned are those with additional (service-definition related) information,
645 not necessarily all database fields of the table. Pseudo-fields may also
646 be returned (i.e. svc_acct.usergroup).
648 Each value of the hashref is another hashref, which can have one or more of
653 =item label - Description of the field
655 =item def_label - Optional description of the field in the context of service definitions
657 =item type - Currently "text", "select", "checkbox", "textarea", "disabled",
658 some components specified by "select-.*.html", and a bunch more...
660 =item disable_default - This field should not allow a default value in service definitions
662 =item disable_fixed - This field should not allow a fixed value in service definitions
664 =item disable_inventory - This field should not allow inventory values in service definitions
666 =item select_list - If type is "text", this can be a listref of possible values.
668 =item select_table - An alternative to select_list, this defines a database table with the possible choices.
670 =item select_key - Used with select_table, this is the field name of keys
672 =item select_label - Used with select_table, this is the field name of labels
678 #maybe this should move and be a class method in svc_Common.pm
679 sub svc_table_fields {
680 my($class, $table) = @_;
681 my $svc_defs = $class->_svc_defs;
682 my $def = $svc_defs->{$table};
684 foreach ( grep !ref($def->{$_}), keys %$def ) {
686 #normalize the shortcut in %info hash
687 $def->{$_} = { 'label' => $def->{$_} };
689 $def->{$_}{'type'} ||= 'text';
704 Job-queue processor for web interface adds/edits
708 use Storable qw(thaw);
714 my $param = thaw(decode_base64(shift));
715 warn Dumper($param) if $DEBUG;
717 my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} })
718 if $param->{'svcpart'};
720 #unmunge cgp_accessmodes (falze laziness-ish w/edit/process/svc_acct.cgi)
721 $param->{'svc_acct__cgp_accessmodes'} ||=
723 grep { $_ !~ /^(flag|label)$/ }
724 map { /^svc_acct__cgp_accessmodes_([\w\/]+)$/ or die "no way"; $1; }
726 grep /^svc_acct__cgp_accessmodes_([\w\/]+)$/,
731 my $new = new FS::part_svc ( {
734 # } qw(svcpart svc svcdb)
735 } ( fields('part_svc'),
736 map { my $svcdb = $_;
737 my @fields = fields($svcdb);
738 push @fields, 'usergroup' if $svcdb eq 'svc_acct'
739 or $svcdb eq 'svc_broadband'; #kludge
742 my $f = $svcdb.'__'.$_;
743 my $flag = $param->{ $f.'_flag' } || ''; #silence warnings
744 if ( $flag =~ /^[MAH]$/ ) {
745 $param->{ $f } = delete( $param->{ $f.'_classnum' } );
748 or $_ eq 'usergroup' ) {
749 $param->{ $f } = ref($param->{ $f })
750 ? join(',', @{$param->{ $f }} )
753 ( $f, $f.'_flag', $f.'_label' );
757 } FS::part_svc->svc_tables()
762 map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
763 qsearch('part_export', {} );
766 if ( $param->{'svcpart'} ) {
767 $error = $new->replace( $old,
768 '1.3-COMPAT', #totally bunk, as jeff noted
774 $error = $new->insert( [ 'usergroup' ],
778 $param->{'svcpart'} = $new->getfield('svcpart');
781 die "$error\n" if $error;
784 =item process_bulk_cust_svc
786 Job-queue processor for web interface bulk customer service changes
790 use Storable qw(thaw);
793 sub process_bulk_cust_svc {
796 my $param = thaw(decode_base64(shift));
797 warn Dumper($param) if $DEBUG;
799 local($FS::svc_Common::noexport_hack) = 1
800 if $param->{'noexport'};
803 qsearchs('part_svc', { 'svcpart' => $param->{'old_svcpart'} } );
805 die "Must select a new service definition\n" unless $param->{'new_svcpart'};
807 #the rest should be abstracted out to to its own subroutine?
809 local $SIG{HUP} = 'IGNORE';
810 local $SIG{INT} = 'IGNORE';
811 local $SIG{QUIT} = 'IGNORE';
812 local $SIG{TERM} = 'IGNORE';
813 local $SIG{TSTP} = 'IGNORE';
814 local $SIG{PIPE} = 'IGNORE';
816 my $oldAutoCommit = $FS::UID::AutoCommit;
817 local $FS::UID::AutoCommit = 0;
820 local( $FS::cust_svc::ignore_quantity ) = 1;
822 my $total = $old_part_svc->num_cust_svc( $param->{'pkgpart'} );
825 foreach my $old_cust_svc ( $old_part_svc->cust_svc( $param->{'pkgpart'} ) ) {
827 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
829 $new_cust_svc->svcpart( $param->{'new_svcpart'} );
830 my $error = $new_cust_svc->replace($old_cust_svc);
832 $dbh->rollback if $oldAutoCommit;
833 die "$error\n" if $error;
836 $error = $job->update_statustext( int( 100 * ++$n / $total ) );
838 $dbh->rollback if $oldAutoCommit;
839 die $error if $error;
844 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
850 sub _upgrade_data { #class method
851 my ($class, %opts) = @_;
853 my @part_svc_column = qsearch('part_svc_column', { 'columnname' => 'usergroup' });
854 foreach my $col ( @part_svc_column ) {
855 next if $col->columnvalue =~ /^[\d,]+$/ || !$col->columnvalue;
856 my @groupnames = split(',',$col->columnvalue);
859 foreach my $groupname ( @groupnames ) {
860 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
862 $g = new FS::radius_group {
863 'groupname' => $groupname,
864 'description' => $groupname,
867 die $error if $error;
869 push @groupnums, $g->groupnum;
871 $col->columnvalue(join(',',@groupnums));
872 $error = $col->replace;
873 die $error if $error;
876 my @badlabels = qsearch({
877 'table' => 'part_svc_column',
879 'extra_sql' => 'WHERE columnlabel IN ('.
880 "'Descriptive label for this particular device.',".
881 "'IP address. Leave blank for automatic assignment.',".
882 "'Maximum upload speed for this service in Kbps. 0 denotes unlimited.',".
883 "'Maximum download speed for this service in Kbps. 0 denotes unlimited.')"
885 foreach my $col ( @badlabels ) {
886 $col->columnlabel('');
887 my $error = $col->replace;
888 die $error if $error;
895 Delete is unimplemented.
897 The list of svc_* tables is no longer hardcoded, but svc_acct_pop is skipped
898 as a special case until it is renamed.
900 all_part_svc_column methods should be documented
904 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
905 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
906 schema.html from the base documentation.