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 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
150 $part_svc_column->setfield('columnflag', $1);
151 $part_svc_column->setfield('columnvalue',
152 &$parser($self->getfield($svcdb.'__'.$field))
155 $error = $part_svc_column->replace($previous);
157 $error = $part_svc_column->insert;
160 $error = $previous ? $previous->delete : '';
163 $dbh->rollback if $oldAutoCommit;
169 # add export_svc records
170 my $slice = 100/scalar(@exportnums) if @exportnums;
172 foreach my $exportnum ( @exportnums ) {
173 my $export_svc = new FS::export_svc ( {
174 'exportnum' => $exportnum,
175 'svcpart' => $self->svcpart,
177 $error = $export_svc->insert($job, $slice*$done++, $slice);
179 $dbh->rollback if $oldAutoCommit;
184 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
191 Currently unimplemented. Set the "disabled" field instead.
196 return "Can't (yet?) delete service definitions.";
197 # check & make sure the svcpart isn't in cust_svc or pkg_svc (in any packages)?
200 =item replace OLD_RECORD [ '1.3-COMPAT' [ , EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ] ]
202 Replaces OLD_RECORD with this one in the database. If there is an error,
203 returns the error, otherwise returns false.
207 TODOC: EXTRA_FIELDS_ARRAYREF (same as insert method)
214 my ( $new, $old ) = ( shift, shift );
219 if ( @_ && $_[0] eq '1.3-COMPAT' ) {
222 @fields = @{shift(@_)} if @_;
223 $exportnums = @_ ? shift : '';
226 return 'non-1.3-COMPAT interface not yet written';
230 return "Can't change svcdb for an existing service definition!"
231 unless $old->svcdb eq $new->svcdb;
233 local $SIG{HUP} = 'IGNORE';
234 local $SIG{INT} = 'IGNORE';
235 local $SIG{QUIT} = 'IGNORE';
236 local $SIG{TERM} = 'IGNORE';
237 local $SIG{TSTP} = 'IGNORE';
238 local $SIG{PIPE} = 'IGNORE';
240 my $oldAutoCommit = $FS::UID::AutoCommit;
241 local $FS::UID::AutoCommit = 0;
244 my $error = $new->SUPER::replace( $old );
246 $dbh->rollback if $oldAutoCommit;
250 if ( $compat eq '1.3' ) {
252 # maintain part_svc_column records
254 my $svcdb = $new->svcdb;
256 grep { $_ ne 'svcnum'
257 && defined( $new->getfield($svcdb.'__'.$_.'_flag') )
258 } (fields($svcdb),@fields)
260 my $part_svc_column = $new->part_svc_column($field);
261 my $previous = qsearchs('part_svc_column', {
262 'svcpart' => $new->svcpart,
263 'columnname' => $field,
266 my $flag = $new->getfield($svcdb.'__'.$field.'_flag');
267 #if ( uc($flag) =~ /^([DFMAX])$/ ) {
268 if ( uc($flag) =~ /^([A-Z])$/ ) { #part_svc_column will test it
269 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
271 $part_svc_column->setfield('columnflag', $1);
272 $part_svc_column->setfield('columnvalue',
273 &$parser($new->getfield($svcdb.'__'.$field))
276 $error = $part_svc_column->replace($previous);
278 $error = $part_svc_column->insert;
281 $error = $previous ? $previous->delete : '';
284 $dbh->rollback if $oldAutoCommit;
289 # maintain export_svc records
293 #false laziness w/ edit/process/agent_type.cgi
294 my @new_export_svc = ();
295 foreach my $part_export ( qsearch('part_export', {}) ) {
296 my $exportnum = $part_export->exportnum;
298 'exportnum' => $exportnum,
299 'svcpart' => $new->svcpart,
301 my $export_svc = qsearchs('export_svc', $hashref);
303 if ( $export_svc && ! $exportnums->{$exportnum} ) {
304 $error = $export_svc->delete;
306 $dbh->rollback if $oldAutoCommit;
309 } elsif ( ! $export_svc && $exportnums->{$exportnum} ) {
310 push @new_export_svc, new FS::export_svc ( $hashref );
315 my $slice = 100/scalar(@new_export_svc) if @new_export_svc;
317 foreach my $export_svc (@new_export_svc) {
318 $error = $export_svc->insert($job, $slice*$done++, $slice);
320 $dbh->rollback if $oldAutoCommit;
324 $error = $job->update_statustext( int( $slice * $done ) );
326 $dbh->rollback if $oldAutoCommit;
335 $dbh->rollback if $oldAutoCommit;
336 return 'non-1.3-COMPAT interface not yet written';
340 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
347 Checks all fields to make sure this is a valid service definition. If there is
348 an error, returns the error, otherwise returns false. Called by the insert
358 $self->ut_numbern('svcpart')
359 || $self->ut_text('svc')
360 || $self->ut_alpha('svcdb')
361 || $self->ut_enum('disabled', [ '', 'Y' ] )
363 return $error if $error;
365 my @fields = eval { fields( $self->svcdb ) }; #might die
366 return "Unknown svcdb: ". $self->svcdb. " (Error: $@)"
372 =item part_svc_column COLUMNNAME
374 Returns the part_svc_column object (see L<FS::part_svc_column>) for the given
375 COLUMNNAME, or a new part_svc_column object if none exists.
379 sub part_svc_column {
380 my( $self, $columnname) = @_;
382 qsearchs('part_svc_column', {
383 'svcpart' => $self->svcpart,
384 'columnname' => $columnname,
386 ) or new FS::part_svc_column {
387 'svcpart' => $self->svcpart,
388 'columnname' => $columnname,
392 =item all_part_svc_column
396 sub all_part_svc_column {
398 qsearch('part_svc_column', { 'svcpart' => $self->svcpart } );
401 =item part_export [ EXPORTTYPE ]
403 Returns a list of all exports (see L<FS::part_export>) for this service, or,
404 if an export type is specified, only returns exports of the given type.
411 $search{'exporttype'} = shift if @_;
412 map { qsearchs('part_export', { 'exportnum' => $_->exportnum, %search } ) }
413 qsearch('export_svc', { 'svcpart' => $self->svcpart } );
416 =item part_export_usage
418 Returns a list of any exports (see L<FS::part_export>) for this service that
419 are capable of reporting usage information.
423 sub part_export_usage {
425 grep $_->can('usage_sessions'), $self->part_export;
428 =item cust_svc [ PKGPART ]
430 Returns a list of associated customer services (FS::cust_svc records).
432 If a PKGPART is specified, returns the customer services which are contained
433 within packages of that type (see L<FS::part_pkg>). If PKGPARTis specified as
434 B<0>, returns unlinked customer services.
441 my $hashref = { 'svcpart' => $self->svcpart };
443 my( $addl_from, $extra_sql ) = ( '', '' );
446 if ( $pkgpart =~ /^(\d+)$/ ) {
447 $addl_from = 'LEFT JOIN cust_pkg USING ( pkgnum )';
448 $extra_sql = "AND pkgpart = $1";
449 } elsif ( $pkgpart eq '0' ) {
450 $hashref->{'pkgnum'} = '';
455 'table' => 'cust_svc',
456 'addl_from' => $addl_from,
457 'hashref' => $hashref,
458 'extra_sql' => $extra_sql,
462 =item num_cust_svc [ PKGPART ]
464 Returns the number of associated customer services (FS::cust_svc records).
466 If a PKGPART is specified, returns the number of customer services which are
467 contained within packages of that type (see L<FS::part_pkg>). If PKGPART
468 is specified as B<0>, returns the number of unlinked customer services.
475 my @param = ( $self->svcpart );
477 my( $join, $and ) = ( '', '' );
481 $join = 'LEFT JOIN cust_pkg USING ( pkgnum )';
482 $and = 'AND pkgpart = ?';
483 push @param, $pkgpart;
484 } elsif ( $pkgpart eq '0' ) {
485 $and = 'AND pkgnum IS NULL';
489 my $sth = dbh->prepare(
490 "SELECT COUNT(*) FROM cust_svc $join WHERE svcpart = ? $and"
491 ) or die dbh->errstr;
492 $sth->execute(@param)
494 $sth->fetchrow_arrayref->[0];
499 Returns a list of associated FS::svc_* records.
505 map { $_->svc_x } $self->cust_svc;
519 return $svc_defs if $svc_defs; #cache
521 my $conf = new FS::Conf;
523 #false laziness w/part_pkg.pm::plan_info
526 foreach my $INC ( @INC ) {
527 warn "globbing $INC/FS/svc_*.pm\n" if $DEBUG;
528 foreach my $file ( glob("$INC/FS/svc_*.pm") ) {
530 warn "attempting to load service table info from $file\n" if $DEBUG;
531 $file =~ /\/(\w+)\.pm$/ or do {
532 warn "unrecognized file in $INC/FS/: $file\n";
537 if ( $mod =~ /^svc_[A-Z]/ or $mod =~ /^svc_acct_pop$/ ) {
538 warn "skipping FS::$mod" if $DEBUG;
542 eval "use FS::$mod;";
544 die "error using FS::$mod (skipping): $@\n" if $@;
547 unless ( UNIVERSAL::can("FS::$mod", 'table_info') ) {
548 warn "FS::$mod has no table_info method; skipping";
552 my $info = "FS::$mod"->table_info;
553 unless ( keys %$info ) {
554 warn "FS::$mod->table_info doesn't return info, skipping\n";
557 warn "got info from FS::$mod: $info\n" if $DEBUG;
558 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
559 warn "skipping disabled service FS::$mod" if $DEBUG;
566 tie my %svc_defs, 'Tie::IxHash',
567 map { $_ => $info{$_}->{'fields'} }
568 sort { $info{$a}->{'display_weight'} <=> $info{$b}->{'display_weight'} }
572 # yuck. maybe this won't be so bad when virtual fields become real fields
574 foreach my $svcdb (grep dbdef->table($_), keys %svc_defs ) {
575 eval "use FS::$svcdb;";
576 my $self = "FS::$svcdb"->new;
577 $vfields{$svcdb} = {};
578 foreach my $field ($self->virtual_fields) { # svc_Common::virtual_fields with a null svcpart returns all of them
579 my $pvf = $self->pvf($field);
580 my @list = $pvf->list;
582 $svc_defs{$svcdb}->{$field} = { desc => $pvf->label,
584 select_list => \@list };
586 $svc_defs{$svcdb}->{$field} = $pvf->label;
588 $vfields{$svcdb}->{$field} = $pvf;
589 warn "\$vfields{$svcdb}->{$field} = $pvf"
594 $svc_defs = \%svc_defs; #cache
600 Returns a list of all svc_ tables.
606 my $svc_defs = $class->_svc_defs;
607 grep { defined( dbdef->table($_) ) } keys %$svc_defs;
610 =item svc_table_fields TABLE
612 Given a table name, returns a hashref of field names. The field names
613 returned are those with additional (service-definition related) information,
614 not necessarily all database fields of the table. Pseudo-fields may also
615 be returned (i.e. svc_acct.usergroup).
617 Each value of the hashref is another hashref, which can have one or more of
622 =item label - Description of the field
624 =item def_label - Optional description of the field in the context of service definitions
626 =item type - Currently "text", "select", "disabled", or "radius_usergroup_selector"
628 =item disable_default - This field should not allow a default value in service definitions
630 =item disable_fixed - This field should not allow a fixed value in service definitions
632 =item disable_inventory - This field should not allow inventory values in service definitions
634 =item select_list - If type is "text", this can be a listref of possible values.
636 =item select_table - An alternative to select_list, this defines a database table with the possible choices.
638 =item select_key - Used with select_table, this is the field name of keys
640 =item select_label - Used with select_table, this is the field name of labels
646 #maybe this should move and be a class method in svc_Common.pm
647 sub svc_table_fields {
648 my($class, $table) = @_;
649 my $svc_defs = $class->_svc_defs;
650 my $def = $svc_defs->{$table};
652 foreach ( grep !ref($def->{$_}), keys %$def ) {
654 #normalize the shortcut in %info hash
655 $def->{$_} = { 'label' => $def->{$_} };
657 $def->{$_}{'type'} ||= 'text';
672 Job-queue processor for web interface adds/edits
676 use Storable qw(thaw);
682 my $param = thaw(decode_base64(shift));
683 warn Dumper($param) if $DEBUG;
685 my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} })
686 if $param->{'svcpart'};
688 $param->{'svc_acct__usergroup'} =
689 ref($param->{'svc_acct__usergroup'})
690 ? join(',', @{$param->{'svc_acct__usergroup'}} )
691 : $param->{'svc_acct__usergroup'};
693 my $new = new FS::part_svc ( {
696 # } qw(svcpart svc svcdb)
697 } ( fields('part_svc'),
698 map { my $svcdb = $_;
699 my @fields = fields($svcdb);
700 push @fields, 'usergroup' if $svcdb eq 'svc_acct'; #kludge
703 if ( $param->{ $svcdb.'__'.$_.'_flag' } =~ /^[MA]$/ ) {
704 $param->{ $svcdb.'__'.$_ } =
705 delete( $param->{ $svcdb.'__'.$_.'_classnum' } );
707 if ( $param->{ $svcdb.'__'.$_.'_flag' } =~ /^S$/ ) {
708 $param->{ $svcdb.'__'.$_} =
709 ref($param->{ $svcdb.'__'.$_})
710 ? join(',', @{$param->{ $svcdb.'__'.$_ }} )
711 : $param->{ $svcdb.'__'.$_ };
713 ( $svcdb.'__'.$_, $svcdb.'__'.$_.'_flag' );
717 } FS::part_svc->svc_tables()
722 map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
723 qsearch('part_export', {} );
726 if ( $param->{'svcpart'} ) {
727 $error = $new->replace( $old,
734 $error = $new->insert( [ 'usergroup' ],
738 $param->{'svcpart'} = $new->getfield('svcpart');
741 die "$error\n" if $error;
744 =item process_bulk_cust_svc
746 Job-queue processor for web interface bulk customer service changes
750 use Storable qw(thaw);
753 sub process_bulk_cust_svc {
756 my $param = thaw(decode_base64(shift));
757 warn Dumper($param) if $DEBUG;
760 qsearchs('part_svc', { 'svcpart' => $param->{'old_svcpart'} } );
762 die "Must select a new service definition\n" unless $param->{'new_svcpart'};
764 #the rest should be abstracted out to to its own subroutine?
766 local $SIG{HUP} = 'IGNORE';
767 local $SIG{INT} = 'IGNORE';
768 local $SIG{QUIT} = 'IGNORE';
769 local $SIG{TERM} = 'IGNORE';
770 local $SIG{TSTP} = 'IGNORE';
771 local $SIG{PIPE} = 'IGNORE';
773 my $oldAutoCommit = $FS::UID::AutoCommit;
774 local $FS::UID::AutoCommit = 0;
777 local( $FS::cust_svc::ignore_quantity ) = 1;
779 my $total = $old_part_svc->num_cust_svc( $param->{'pkgpart'} );
782 foreach my $old_cust_svc ( $old_part_svc->cust_svc( $param->{'pkgpart'} ) ) {
784 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
786 $new_cust_svc->svcpart( $param->{'new_svcpart'} );
787 my $error = $new_cust_svc->replace($old_cust_svc);
789 $dbh->rollback if $oldAutoCommit;
790 die "$error\n" if $error;
793 $error = $job->update_statustext( int( 100 * ++$n / $total ) );
795 $dbh->rollback if $oldAutoCommit;
796 die $error if $error;
801 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
809 Delete is unimplemented.
811 The list of svc_* tables is no longer hardcoded, but svc_acct_pop is skipped
812 as a special case until it is renamed.
814 all_part_svc_column methods should be documented
818 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
819 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
820 schema.html from the base documentation.