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 || $self->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
138 } (fields($svcdb), @fields)
140 my $part_svc_column = $self->part_svc_column($field);
141 my $previous = qsearchs('part_svc_column', {
142 'svcpart' => $self->svcpart,
143 'columnname' => $field,
146 my $flag = $self->getfield($svcdb.'__'.$field.'_flag');
147 my $label = $self->getfield($svcdb.'__'.$field.'_label');
148 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
150 if ( uc($flag) =~ /^([A-Z])$/ ) {
151 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
153 $part_svc_column->setfield('columnflag', $1);
154 $part_svc_column->setfield('columnvalue',
155 &$parser($self->getfield($svcdb.'__'.$field))
159 $part_svc_column->setfield('columnlabel', $label)
160 if $label !~ /^\s*$/;
163 $error = $part_svc_column->replace($previous);
165 $error = $part_svc_column->insert;
169 $error = $previous ? $previous->delete : '';
172 $dbh->rollback if $oldAutoCommit;
178 # add export_svc records
179 my $slice = 100/scalar(@exportnums) if @exportnums;
181 foreach my $exportnum ( @exportnums ) {
182 my $export_svc = new FS::export_svc ( {
183 'exportnum' => $exportnum,
184 'svcpart' => $self->svcpart,
186 $error = $export_svc->insert($job, $slice*$done++, $slice);
188 $dbh->rollback if $oldAutoCommit;
193 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
200 Currently unimplemented. Set the "disabled" field instead.
205 return "Can't (yet?) delete service definitions.";
206 # check & make sure the svcpart isn't in cust_svc or pkg_svc (in any packages)?
209 =item replace OLD_RECORD [ '1.3-COMPAT' [ , EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ] ]
211 Replaces OLD_RECORD with this one in the database. If there is an error,
212 returns the error, otherwise returns false.
216 TODOC: EXTRA_FIELDS_ARRAYREF (same as insert method)
223 my ( $new, $old ) = ( shift, shift );
228 if ( @_ && $_[0] eq '1.3-COMPAT' ) {
231 @fields = @{shift(@_)} if @_;
232 $exportnums = @_ ? shift : '';
235 return 'non-1.3-COMPAT interface not yet written';
239 return "Can't change svcdb for an existing service definition!"
240 unless $old->svcdb eq $new->svcdb;
242 local $SIG{HUP} = 'IGNORE';
243 local $SIG{INT} = 'IGNORE';
244 local $SIG{QUIT} = 'IGNORE';
245 local $SIG{TERM} = 'IGNORE';
246 local $SIG{TSTP} = 'IGNORE';
247 local $SIG{PIPE} = 'IGNORE';
249 my $oldAutoCommit = $FS::UID::AutoCommit;
250 local $FS::UID::AutoCommit = 0;
253 my $error = $new->SUPER::replace( $old );
255 $dbh->rollback if $oldAutoCommit;
259 if ( $compat eq '1.3' ) {
261 # maintain part_svc_column records
263 my $svcdb = $new->svcdb;
265 grep { $_ ne 'svcnum'
266 && ( defined( $new->getfield($svcdb.'__'.$_.'_flag') )
267 || $new->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
268 } (fields($svcdb),@fields)
270 my $part_svc_column = $new->part_svc_column($field);
271 my $previous = qsearchs('part_svc_column', {
272 'svcpart' => $new->svcpart,
273 'columnname' => $field,
276 my $flag = $new->getfield($svcdb.'__'.$field.'_flag');
277 my $label = $new->getfield($svcdb.'__'.$field.'_label');
279 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
281 if ( uc($flag) =~ /^([A-Z])$/ ) {
282 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
284 $part_svc_column->setfield('columnflag', $1);
285 $part_svc_column->setfield('columnvalue',
286 &$parser($new->getfield($svcdb.'__'.$field))
290 $part_svc_column->setfield('columnlabel', $label)
291 if $label !~ /^\s*$/;
294 $error = $part_svc_column->replace($previous);
296 $error = $part_svc_column->insert;
299 $error = $previous ? $previous->delete : '';
302 $dbh->rollback if $oldAutoCommit;
307 # maintain export_svc records
311 #false laziness w/ edit/process/agent_type.cgi
312 my @new_export_svc = ();
313 foreach my $part_export ( qsearch('part_export', {}) ) {
314 my $exportnum = $part_export->exportnum;
316 'exportnum' => $exportnum,
317 'svcpart' => $new->svcpart,
319 my $export_svc = qsearchs('export_svc', $hashref);
321 if ( $export_svc && ! $exportnums->{$exportnum} ) {
322 $error = $export_svc->delete;
324 $dbh->rollback if $oldAutoCommit;
327 } elsif ( ! $export_svc && $exportnums->{$exportnum} ) {
328 push @new_export_svc, new FS::export_svc ( $hashref );
333 my $slice = 100/scalar(@new_export_svc) if @new_export_svc;
335 foreach my $export_svc (@new_export_svc) {
336 $error = $export_svc->insert($job, $slice*$done++, $slice);
338 $dbh->rollback if $oldAutoCommit;
342 $error = $job->update_statustext( int( $slice * $done ) );
344 $dbh->rollback if $oldAutoCommit;
353 $dbh->rollback if $oldAutoCommit;
354 return 'non-1.3-COMPAT interface not yet written';
358 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
365 Checks all fields to make sure this is a valid service definition. If there is
366 an error, returns the error, otherwise returns false. Called by the insert
376 $self->ut_numbern('svcpart')
377 || $self->ut_text('svc')
378 || $self->ut_alpha('svcdb')
379 || $self->ut_enum('disabled', [ '', 'Y' ] )
381 return $error if $error;
383 my @fields = eval { fields( $self->svcdb ) }; #might die
384 return "Unknown svcdb: ". $self->svcdb. " (Error: $@)"
390 =item part_svc_column COLUMNNAME
392 Returns the part_svc_column object (see L<FS::part_svc_column>) for the given
393 COLUMNNAME, or a new part_svc_column object if none exists.
397 sub part_svc_column {
398 my( $self, $columnname) = @_;
400 qsearchs('part_svc_column', {
401 'svcpart' => $self->svcpart,
402 'columnname' => $columnname,
404 ) or new FS::part_svc_column {
405 'svcpart' => $self->svcpart,
406 'columnname' => $columnname,
410 =item all_part_svc_column
414 sub all_part_svc_column {
416 qsearch('part_svc_column', { 'svcpart' => $self->svcpart } );
419 =item part_export [ EXPORTTYPE ]
421 Returns a list of all exports (see L<FS::part_export>) for this service, or,
422 if an export type is specified, only returns exports of the given type.
429 $search{'exporttype'} = shift if @_;
430 map { qsearchs('part_export', { 'exportnum' => $_->exportnum, %search } ) }
431 qsearch('export_svc', { 'svcpart' => $self->svcpart } );
434 =item part_export_usage
436 Returns a list of any exports (see L<FS::part_export>) for this service that
437 are capable of reporting usage information.
441 sub part_export_usage {
443 grep $_->can('usage_sessions'), $self->part_export;
446 =item part_export_did
448 Returns a list of any exports (see L<FS::part_export>) for this service that
449 are capable of returing available DID (phone number) information.
453 sub part_export_did {
455 grep $_->can('get_dids'), $self->part_export;
459 =item cust_svc [ PKGPART ]
461 Returns a list of associated customer services (FS::cust_svc records).
463 If a PKGPART is specified, returns the customer services which are contained
464 within packages of that type (see L<FS::part_pkg>). If PKGPARTis specified as
465 B<0>, returns unlinked customer services.
472 my $hashref = { 'svcpart' => $self->svcpart };
474 my( $addl_from, $extra_sql ) = ( '', '' );
477 if ( $pkgpart =~ /^(\d+)$/ ) {
478 $addl_from = 'LEFT JOIN cust_pkg USING ( pkgnum )';
479 $extra_sql = "AND pkgpart = $1";
480 } elsif ( $pkgpart eq '0' ) {
481 $hashref->{'pkgnum'} = '';
486 'table' => 'cust_svc',
487 'addl_from' => $addl_from,
488 'hashref' => $hashref,
489 'extra_sql' => $extra_sql,
493 =item num_cust_svc [ PKGPART ]
495 Returns the number of associated customer services (FS::cust_svc records).
497 If a PKGPART is specified, returns the number of customer services which are
498 contained within packages of that type (see L<FS::part_pkg>). If PKGPART
499 is specified as B<0>, returns the number of unlinked customer services.
506 my @param = ( $self->svcpart );
508 my( $join, $and ) = ( '', '' );
512 $join = 'LEFT JOIN cust_pkg USING ( pkgnum )';
513 $and = 'AND pkgpart = ?';
514 push @param, $pkgpart;
515 } elsif ( $pkgpart eq '0' ) {
516 $and = 'AND pkgnum IS NULL';
520 my $sth = dbh->prepare(
521 "SELECT COUNT(*) FROM cust_svc $join WHERE svcpart = ? $and"
522 ) or die dbh->errstr;
523 $sth->execute(@param)
525 $sth->fetchrow_arrayref->[0];
530 Returns a list of associated FS::svc_* records.
536 map { $_->svc_x } $self->cust_svc;
550 return $svc_defs if $svc_defs; #cache
552 my $conf = new FS::Conf;
554 #false laziness w/part_pkg.pm::plan_info
557 foreach my $INC ( @INC ) {
558 warn "globbing $INC/FS/svc_*.pm\n" if $DEBUG;
559 foreach my $file ( glob("$INC/FS/svc_*.pm") ) {
561 warn "attempting to load service table info from $file\n" if $DEBUG;
562 $file =~ /\/(\w+)\.pm$/ or do {
563 warn "unrecognized file in $INC/FS/: $file\n";
568 if ( $mod =~ /^svc_[A-Z]/ or $mod =~ /^svc_acct_pop$/ ) {
569 warn "skipping FS::$mod" if $DEBUG;
573 eval "use FS::$mod;";
575 die "error using FS::$mod (skipping): $@\n" if $@;
578 unless ( UNIVERSAL::can("FS::$mod", 'table_info') ) {
579 warn "FS::$mod has no table_info method; skipping";
583 my $info = "FS::$mod"->table_info;
584 unless ( keys %$info ) {
585 warn "FS::$mod->table_info doesn't return info, skipping\n";
588 warn "got info from FS::$mod: $info\n" if $DEBUG;
589 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
590 warn "skipping disabled service FS::$mod" if $DEBUG;
597 tie my %svc_defs, 'Tie::IxHash',
598 map { $_ => $info{$_}->{'fields'} }
599 sort { $info{$a}->{'display_weight'} <=> $info{$b}->{'display_weight'} }
603 # yuck. maybe this won't be so bad when virtual fields become real fields
605 foreach my $svcdb (grep dbdef->table($_), keys %svc_defs ) {
606 eval "use FS::$svcdb;";
607 my $self = "FS::$svcdb"->new;
608 $vfields{$svcdb} = {};
609 foreach my $field ($self->virtual_fields) { # svc_Common::virtual_fields with a null svcpart returns all of them
610 my $pvf = $self->pvf($field);
611 my @list = $pvf->list;
613 $svc_defs{$svcdb}->{$field} = { desc => $pvf->label,
615 select_list => \@list };
617 $svc_defs{$svcdb}->{$field} = $pvf->label;
619 $vfields{$svcdb}->{$field} = $pvf;
620 warn "\$vfields{$svcdb}->{$field} = $pvf"
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", "disabled", or "radius_usergroup_selector"
659 =item disable_default - This field should not allow a default value in service definitions
661 =item disable_fixed - This field should not allow a fixed value in service definitions
663 =item disable_inventory - This field should not allow inventory values in service definitions
665 =item select_list - If type is "text", this can be a listref of possible values.
667 =item select_table - An alternative to select_list, this defines a database table with the possible choices.
669 =item select_key - Used with select_table, this is the field name of keys
671 =item select_label - Used with select_table, this is the field name of labels
677 #maybe this should move and be a class method in svc_Common.pm
678 sub svc_table_fields {
679 my($class, $table) = @_;
680 my $svc_defs = $class->_svc_defs;
681 my $def = $svc_defs->{$table};
683 foreach ( grep !ref($def->{$_}), keys %$def ) {
685 #normalize the shortcut in %info hash
686 $def->{$_} = { 'label' => $def->{$_} };
688 $def->{$_}{'type'} ||= 'text';
703 Job-queue processor for web interface adds/edits
707 use Storable qw(thaw);
713 my $param = thaw(decode_base64(shift));
714 warn Dumper($param) if $DEBUG;
716 my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} })
717 if $param->{'svcpart'};
719 $param->{'svc_acct__usergroup'} =
720 ref($param->{'svc_acct__usergroup'})
721 ? join(',', @{$param->{'svc_acct__usergroup'}} )
722 : $param->{'svc_acct__usergroup'};
724 my $new = new FS::part_svc ( {
727 # } qw(svcpart svc svcdb)
728 } ( fields('part_svc'),
729 map { my $svcdb = $_;
730 my @fields = fields($svcdb);
731 push @fields, 'usergroup' if $svcdb eq 'svc_acct'; #kludge
734 my $f = $svcdb.'__'.$_;
735 if ( $param->{ $f.'_flag' } =~ /^[MA]$/ ) {
736 $param->{ $f } = delete( $param->{ $f.'_classnum' } );
738 if ( $param->{ $f.'_flag' } =~ /^S$/ ) {
739 $param->{ $f } = ref($param->{ $f })
740 ? join(',', @{$param->{ $f }} )
743 ( $f, $f.'_flag', $f.'_label' );
747 } FS::part_svc->svc_tables()
752 map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
753 qsearch('part_export', {} );
756 if ( $param->{'svcpart'} ) {
757 $error = $new->replace( $old,
758 '1.3-COMPAT', #totally bunk, as jeff noted
764 $error = $new->insert( [ 'usergroup' ],
768 $param->{'svcpart'} = $new->getfield('svcpart');
771 die "$error\n" if $error;
774 =item process_bulk_cust_svc
776 Job-queue processor for web interface bulk customer service changes
780 use Storable qw(thaw);
783 sub process_bulk_cust_svc {
786 my $param = thaw(decode_base64(shift));
787 warn Dumper($param) if $DEBUG;
790 qsearchs('part_svc', { 'svcpart' => $param->{'old_svcpart'} } );
792 die "Must select a new service definition\n" unless $param->{'new_svcpart'};
794 #the rest should be abstracted out to to its own subroutine?
796 local $SIG{HUP} = 'IGNORE';
797 local $SIG{INT} = 'IGNORE';
798 local $SIG{QUIT} = 'IGNORE';
799 local $SIG{TERM} = 'IGNORE';
800 local $SIG{TSTP} = 'IGNORE';
801 local $SIG{PIPE} = 'IGNORE';
803 my $oldAutoCommit = $FS::UID::AutoCommit;
804 local $FS::UID::AutoCommit = 0;
807 local( $FS::cust_svc::ignore_quantity ) = 1;
809 my $total = $old_part_svc->num_cust_svc( $param->{'pkgpart'} );
812 foreach my $old_cust_svc ( $old_part_svc->cust_svc( $param->{'pkgpart'} ) ) {
814 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
816 $new_cust_svc->svcpart( $param->{'new_svcpart'} );
817 my $error = $new_cust_svc->replace($old_cust_svc);
819 $dbh->rollback if $oldAutoCommit;
820 die "$error\n" if $error;
823 $error = $job->update_statustext( int( 100 * ++$n / $total ) );
825 $dbh->rollback if $oldAutoCommit;
826 die $error if $error;
831 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
839 Delete is unimplemented.
841 The list of svc_* tables is no longer hardcoded, but svc_acct_pop is skipped
842 as a special case until it is renamed.
844 all_part_svc_column methods should be documented
848 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
849 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
850 schema.html from the base documentation.