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'
64 Creates a new service definition. To add the service definition to the
65 database, see L<"insert">.
69 sub table { 'part_svc'; }
71 =item insert [ EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ]
73 Adds this service definition to the database. If there is an error, returns
74 the error, otherwise returns false.
76 The following pseudo-fields may be defined, and will be maintained in
77 the part_svc_column table appropriately (see L<FS::part_svc_column>).
81 =item I<svcdb>__I<field> - Default or fixed value for I<field> in I<svcdb>.
83 =item I<svcdb>__I<field>_flag - defines I<svcdb>__I<field> action: null or empty (no default), `D' for default, `F' for fixed (unchangeable), `M' for manual selection from inventory, or `A' for automatic selection from inventory. For virtual fields, can also be 'X' for excluded.
87 If you want to add part_svc_column records for fields that do not exist as
88 (real or virtual) fields in the I<svcdb> table, make sure to list then in
89 EXTRA_FIELDS_ARRAYREF also.
91 If EXPORTNUMS_HASHREF is specified (keys are exportnums and values are
92 boolean), the appopriate export_svc records will be inserted.
102 @fields = @{shift(@_)} if @_;
104 my $exportnums = shift;
105 @exportnums = grep $exportnums->{$_}, keys %$exportnums;
110 local $SIG{HUP} = 'IGNORE';
111 local $SIG{INT} = 'IGNORE';
112 local $SIG{QUIT} = 'IGNORE';
113 local $SIG{TERM} = 'IGNORE';
114 local $SIG{TSTP} = 'IGNORE';
115 local $SIG{PIPE} = 'IGNORE';
117 my $oldAutoCommit = $FS::UID::AutoCommit;
118 local $FS::UID::AutoCommit = 0;
121 my $error = $self->SUPER::insert;
123 $dbh->rollback if $oldAutoCommit;
127 # add part_svc_column records
129 my $svcdb = $self->svcdb;
130 # my @rows = map { /^${svcdb}__(.*)$/; $1 }
132 # grep /^${svcdb}__/,
133 # fields('part_svc');
135 grep { $_ ne 'svcnum'
136 && defined( $self->getfield($svcdb.'__'.$_.'_flag') )
137 } (fields($svcdb), @fields)
139 my $part_svc_column = $self->part_svc_column($field);
140 my $previous = qsearchs('part_svc_column', {
141 'svcpart' => $self->svcpart,
142 'columnname' => $field,
145 my $flag = $self->getfield($svcdb.'__'.$field.'_flag');
146 #if ( uc($flag) =~ /^([DFMAX])$/ ) {
147 if ( uc($flag) =~ /^([A-Z])$/ ) { #part_svc_column will test it
148 $part_svc_column->setfield('columnflag', $1);
149 $part_svc_column->setfield('columnvalue',
150 $self->getfield($svcdb.'__'.$field)
153 $error = $part_svc_column->replace($previous);
155 $error = $part_svc_column->insert;
158 $error = $previous ? $previous->delete : '';
161 $dbh->rollback if $oldAutoCommit;
167 # add export_svc records
168 my $slice = 100/scalar(@exportnums) if @exportnums;
170 foreach my $exportnum ( @exportnums ) {
171 my $export_svc = new FS::export_svc ( {
172 'exportnum' => $exportnum,
173 'svcpart' => $self->svcpart,
175 $error = $export_svc->insert($job, $slice*$done++, $slice);
177 $dbh->rollback if $oldAutoCommit;
182 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
189 Currently unimplemented. Set the "disabled" field instead.
194 return "Can't (yet?) delete service definitions.";
195 # check & make sure the svcpart isn't in cust_svc or pkg_svc (in any packages)?
198 =item replace OLD_RECORD [ '1.3-COMPAT' [ , EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ] ]
200 Replaces OLD_RECORD with this one in the database. If there is an error,
201 returns the error, otherwise returns false.
205 TODOC: EXTRA_FIELDS_ARRAYREF (same as insert method)
212 my ( $new, $old ) = ( shift, shift );
217 if ( @_ && $_[0] eq '1.3-COMPAT' ) {
220 @fields = @{shift(@_)} if @_;
221 $exportnums = @_ ? shift : '';
224 return 'non-1.3-COMPAT interface not yet written';
228 return "Can't change svcdb for an existing service definition!"
229 unless $old->svcdb eq $new->svcdb;
231 local $SIG{HUP} = 'IGNORE';
232 local $SIG{INT} = 'IGNORE';
233 local $SIG{QUIT} = 'IGNORE';
234 local $SIG{TERM} = 'IGNORE';
235 local $SIG{TSTP} = 'IGNORE';
236 local $SIG{PIPE} = 'IGNORE';
238 my $oldAutoCommit = $FS::UID::AutoCommit;
239 local $FS::UID::AutoCommit = 0;
242 my $error = $new->SUPER::replace( $old );
244 $dbh->rollback if $oldAutoCommit;
248 if ( $compat eq '1.3' ) {
250 # maintain part_svc_column records
252 my $svcdb = $new->svcdb;
254 grep { $_ ne 'svcnum'
255 && defined( $new->getfield($svcdb.'__'.$_.'_flag') )
256 } (fields($svcdb),@fields)
258 my $part_svc_column = $new->part_svc_column($field);
259 my $previous = qsearchs('part_svc_column', {
260 'svcpart' => $new->svcpart,
261 'columnname' => $field,
264 my $flag = $new->getfield($svcdb.'__'.$field.'_flag');
265 #if ( uc($flag) =~ /^([DFMAX])$/ ) {
266 if ( uc($flag) =~ /^([A-Z])$/ ) { #part_svc_column will test it
267 $part_svc_column->setfield('columnflag', $1);
268 $part_svc_column->setfield('columnvalue',
269 $new->getfield($svcdb.'__'.$field)
272 $error = $part_svc_column->replace($previous);
274 $error = $part_svc_column->insert;
277 $error = $previous ? $previous->delete : '';
280 $dbh->rollback if $oldAutoCommit;
285 # maintain export_svc records
289 #false laziness w/ edit/process/agent_type.cgi
290 my @new_export_svc = ();
291 foreach my $part_export ( qsearch('part_export', {}) ) {
292 my $exportnum = $part_export->exportnum;
294 'exportnum' => $exportnum,
295 'svcpart' => $new->svcpart,
297 my $export_svc = qsearchs('export_svc', $hashref);
299 if ( $export_svc && ! $exportnums->{$exportnum} ) {
300 $error = $export_svc->delete;
302 $dbh->rollback if $oldAutoCommit;
305 } elsif ( ! $export_svc && $exportnums->{$exportnum} ) {
306 push @new_export_svc, new FS::export_svc ( $hashref );
311 my $slice = 100/scalar(@new_export_svc) if @new_export_svc;
313 foreach my $export_svc (@new_export_svc) {
314 $error = $export_svc->insert($job, $slice*$done++, $slice);
316 $dbh->rollback if $oldAutoCommit;
320 $error = $job->update_statustext( int( $slice * $done ) );
322 $dbh->rollback if $oldAutoCommit;
331 $dbh->rollback if $oldAutoCommit;
332 return 'non-1.3-COMPAT interface not yet written';
336 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
343 Checks all fields to make sure this is a valid service definition. If there is
344 an error, returns the error, otherwise returns false. Called by the insert
354 $self->ut_numbern('svcpart')
355 || $self->ut_text('svc')
356 || $self->ut_alpha('svcdb')
357 || $self->ut_enum('disabled', [ '', 'Y' ] )
359 return $error if $error;
361 my @fields = eval { fields( $self->svcdb ) }; #might die
362 return "Unknown svcdb: ". $self->svcdb. " (Error: $@)"
368 =item part_svc_column COLUMNNAME
370 Returns the part_svc_column object (see L<FS::part_svc_column>) for the given
371 COLUMNNAME, or a new part_svc_column object if none exists.
375 sub part_svc_column {
376 my( $self, $columnname) = @_;
378 qsearchs('part_svc_column', {
379 'svcpart' => $self->svcpart,
380 'columnname' => $columnname,
382 ) or new FS::part_svc_column {
383 'svcpart' => $self->svcpart,
384 'columnname' => $columnname,
388 =item all_part_svc_column
392 sub all_part_svc_column {
394 qsearch('part_svc_column', { 'svcpart' => $self->svcpart } );
397 =item part_export [ EXPORTTYPE ]
399 Returns a list of all exports (see L<FS::part_export>) for this service, or,
400 if an export type is specified, only returns exports of the given type.
407 $search{'exporttype'} = shift if @_;
408 map { qsearchs('part_export', { 'exportnum' => $_->exportnum, %search } ) }
409 qsearch('export_svc', { 'svcpart' => $self->svcpart } );
412 =item part_export_usage
414 Returns a list of any exports (see L<FS::part_export>) for this service that
415 are capable of reporting usage information.
419 sub part_export_usage {
421 grep $_->can('usage_sessions'), $self->part_export;
424 =item cust_svc [ PKGPART ]
426 Returns a list of associated customer services (FS::cust_svc records).
428 If a PKGPART is specified, returns the customer services which are contained
429 within packages of that type (see L<FS::part_pkg>). If PKGPARTis specified as
430 B<0>, returns unlinked customer services.
437 my $hashref = { 'svcpart' => $self->svcpart };
439 my( $addl_from, $extra_sql ) = ( '', '' );
442 if ( $pkgpart =~ /^(\d+)$/ ) {
443 $addl_from = 'LEFT JOIN cust_pkg USING ( pkgnum )';
444 $extra_sql = "AND pkgpart = $1";
445 } elsif ( $pkgpart eq '0' ) {
446 $hashref->{'pkgnum'} = '';
451 'table' => 'cust_svc',
452 'addl_from' => $addl_from,
453 'hashref' => $hashref,
454 'extra_sql' => $extra_sql,
458 =item num_cust_svc [ PKGPART ]
460 Returns the number of associated customer services (FS::cust_svc records).
462 If a PKGPART is specified, returns the number of customer services which are
463 contained within packages of that type (see L<FS::part_pkg>). If PKGPART
464 is specified as B<0>, returns the number of unlinked customer services.
471 my @param = ( $self->svcpart );
473 my( $join, $and ) = ( '', '' );
477 $join = 'LEFT JOIN cust_pkg USING ( pkgnum )';
478 $and = 'AND pkgpart = ?';
479 push @param, $pkgpart;
480 } elsif ( $pkgpart eq '0' ) {
481 $and = 'AND pkgnum IS NULL';
485 my $sth = dbh->prepare(
486 "SELECT COUNT(*) FROM cust_svc $join WHERE svcpart = ? $and"
487 ) or die dbh->errstr;
488 $sth->execute(@param)
490 $sth->fetchrow_arrayref->[0];
495 Returns a list of associated FS::svc_* records.
501 map { $_->svc_x } $self->cust_svc;
515 return $svc_defs if $svc_defs; #cache
517 my $conf = new FS::Conf;
519 #false laziness w/part_pkg.pm::plan_info
522 foreach my $INC ( @INC ) {
523 warn "globbing $INC/FS/svc_*.pm\n" if $DEBUG;
524 foreach my $file ( glob("$INC/FS/svc_*.pm") ) {
526 warn "attempting to load service table info from $file\n" if $DEBUG;
527 $file =~ /\/(\w+)\.pm$/ or do {
528 warn "unrecognized file in $INC/FS/: $file\n";
533 if ( $mod =~ /^svc_[A-Z]/ or $mod =~ /^svc_acct_pop$/ ) {
534 warn "skipping FS::$mod" if $DEBUG;
538 eval "use FS::$mod;";
540 die "error using FS::$mod (skipping): $@\n" if $@;
543 unless ( UNIVERSAL::can("FS::$mod", 'table_info') ) {
544 warn "FS::$mod has no table_info method; skipping";
548 my $info = "FS::$mod"->table_info;
549 unless ( keys %$info ) {
550 warn "FS::$mod->table_info doesn't return info, skipping\n";
553 warn "got info from FS::$mod: $info\n" if $DEBUG;
554 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
555 warn "skipping disabled service FS::$mod" if $DEBUG;
562 tie my %svc_defs, 'Tie::IxHash',
563 map { $_ => $info{$_}->{'fields'} }
564 sort { $info{$a}->{'display_weight'} <=> $info{$b}->{'display_weight'} }
568 # yuck. maybe this won't be so bad when virtual fields become real fields
570 foreach my $svcdb (grep dbdef->table($_), keys %svc_defs ) {
571 eval "use FS::$svcdb;";
572 my $self = "FS::$svcdb"->new;
573 $vfields{$svcdb} = {};
574 foreach my $field ($self->virtual_fields) { # svc_Common::virtual_fields with a null svcpart returns all of them
575 my $pvf = $self->pvf($field);
576 my @list = $pvf->list;
578 $svc_defs{$svcdb}->{$field} = { desc => $pvf->label,
580 select_list => \@list };
582 $svc_defs{$svcdb}->{$field} = $pvf->label;
584 $vfields{$svcdb}->{$field} = $pvf;
585 warn "\$vfields{$svcdb}->{$field} = $pvf"
590 $svc_defs = \%svc_defs; #cache
596 Returns a list of all svc_ tables.
602 my $svc_defs = $class->_svc_defs;
603 grep { defined( dbdef->table($_) ) } keys %$svc_defs;
606 =item svc_table_fields TABLE
608 Given a table name, returns a hashref of field names. The field names
609 returned are those with additional (service-definition related) information,
610 not necessarily all database fields of the table. Pseudo-fields may also
611 be returned (i.e. svc_acct.usergroup).
613 Each value of the hashref is another hashref, which can have one or more of
618 =item label - Description of the field
620 =item def_label - Optional description of the field in the context of service definitions
622 =item type - Currently "text", "select", "disabled", or "radius_usergroup_selector"
624 =item disable_default - This field should not allow a default value in service definitions
626 =item disable_fixed - This field should not allow a fixed value in service definitions
628 =item disable_inventory - This field should not allow inventory values in service definitions
630 =item select_list - If type is "text", this can be a listref of possible values.
632 =item select_table - An alternative to select_list, this defines a database table with the possible choices.
634 =item select_key - Used with select_table, this is the field name of keys
636 =item select_label - Used with select_table, this is the field name of labels
642 #maybe this should move and be a class method in svc_Common.pm
643 sub svc_table_fields {
644 my($class, $table) = @_;
645 my $svc_defs = $class->_svc_defs;
646 my $def = $svc_defs->{$table};
648 foreach ( grep !ref($def->{$_}), keys %$def ) {
650 #normalize the shortcut in %info hash
651 $def->{$_} = { 'label' => $def->{$_} };
653 $def->{$_}{'type'} ||= 'text';
668 Job-queue processor for web interface adds/edits
672 use Storable qw(thaw);
678 my $param = thaw(decode_base64(shift));
679 warn Dumper($param) if $DEBUG;
681 my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} })
682 if $param->{'svcpart'};
684 $param->{'svc_acct__usergroup'} =
685 ref($param->{'svc_acct__usergroup'})
686 ? join(',', @{$param->{'svc_acct__usergroup'}} )
687 : $param->{'svc_acct__usergroup'};
689 my $new = new FS::part_svc ( {
692 # } qw(svcpart svc svcdb)
693 } ( fields('part_svc'),
694 map { my $svcdb = $_;
695 my @fields = fields($svcdb);
696 push @fields, 'usergroup' if $svcdb eq 'svc_acct'; #kludge
699 if ( $param->{ $svcdb.'__'.$_.'_flag' } =~ /^[MA]$/ ) {
700 $param->{ $svcdb.'__'.$_ } =
701 delete( $param->{ $svcdb.'__'.$_.'_classnum' } );
703 if ( $param->{ $svcdb.'__'.$_.'_flag' } =~ /^S$/ ) {
704 $param->{ $svcdb.'__'.$_} =
705 ref($param->{ $svcdb.'__'.$_})
706 ? join(',', @{$param->{ $svcdb.'__'.$_ }} )
707 : $param->{ $svcdb.'__'.$_ };
709 ( $svcdb.'__'.$_, $svcdb.'__'.$_.'_flag' );
713 } FS::part_svc->svc_tables()
718 map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
719 qsearch('part_export', {} );
722 if ( $param->{'svcpart'} ) {
723 $error = $new->replace( $old,
730 $error = $new->insert( [ 'usergroup' ],
734 $param->{'svcpart'} = $new->getfield('svcpart');
737 die "$error\n" if $error;
740 =item process_bulk_cust_svc
742 Job-queue processor for web interface bulk customer service changes
746 use Storable qw(thaw);
749 sub process_bulk_cust_svc {
752 my $param = thaw(decode_base64(shift));
753 warn Dumper($param) if $DEBUG;
756 qsearchs('part_svc', { 'svcpart' => $param->{'old_svcpart'} } );
758 die "Must select a new service definition\n" unless $param->{'new_svcpart'};
760 #the rest should be abstracted out to to its own subroutine?
762 local $SIG{HUP} = 'IGNORE';
763 local $SIG{INT} = 'IGNORE';
764 local $SIG{QUIT} = 'IGNORE';
765 local $SIG{TERM} = 'IGNORE';
766 local $SIG{TSTP} = 'IGNORE';
767 local $SIG{PIPE} = 'IGNORE';
769 my $oldAutoCommit = $FS::UID::AutoCommit;
770 local $FS::UID::AutoCommit = 0;
773 local( $FS::cust_svc::ignore_quantity ) = 1;
775 my $total = $old_part_svc->num_cust_svc( $param->{'pkgpart'} );
778 foreach my $old_cust_svc ( $old_part_svc->cust_svc( $param->{'pkgpart'} ) ) {
780 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
782 $new_cust_svc->svcpart( $param->{'new_svcpart'} );
783 my $error = $new_cust_svc->replace($old_cust_svc);
785 $dbh->rollback if $oldAutoCommit;
786 die "$error\n" if $error;
789 $error = $job->update_statustext( int( 100 * ++$n / $total ) );
791 $dbh->rollback if $oldAutoCommit;
792 die $error if $error;
797 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
805 Delete is unimplemented.
807 The list of svc_* tables is no longer hardcoded, but svc_acct_pop is skipped
808 as a special case until it is renamed.
810 all_part_svc_column methods should be documented
814 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
815 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
816 schema.html from the base documentation.