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)
271 my $part_svc_column = $new->part_svc_column($field);
272 my $previous = qsearchs('part_svc_column', {
273 'svcpart' => $new->svcpart,
274 'columnname' => $field,
277 my $flag = $new->getfield($svcdb.'__'.$field.'_flag');
278 my $label = $new->getfield($svcdb.'__'.$field.'_label');
280 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
282 if ( uc($flag) =~ /^([A-Z])$/ ) {
283 $part_svc_column->setfield('columnflag', $1);
284 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
286 $part_svc_column->setfield('columnvalue',
287 &$parser($new->getfield($svcdb.'__'.$field))
290 $part_svc_column->setfield('columnflag', '');
291 $part_svc_column->setfield('columnvalue', '');
294 $part_svc_column->setfield('columnlabel', $label)
295 if $label !~ /^\s*$/;
298 $error = $part_svc_column->replace($previous);
300 $error = $part_svc_column->insert;
303 $error = $previous ? $previous->delete : '';
306 $dbh->rollback if $oldAutoCommit;
311 # maintain export_svc records
315 #false laziness w/ edit/process/agent_type.cgi
316 my @new_export_svc = ();
317 foreach my $part_export ( qsearch('part_export', {}) ) {
318 my $exportnum = $part_export->exportnum;
320 'exportnum' => $exportnum,
321 'svcpart' => $new->svcpart,
323 my $export_svc = qsearchs('export_svc', $hashref);
325 if ( $export_svc && ! $exportnums->{$exportnum} ) {
326 $error = $export_svc->delete;
328 $dbh->rollback if $oldAutoCommit;
331 } elsif ( ! $export_svc && $exportnums->{$exportnum} ) {
332 push @new_export_svc, new FS::export_svc ( $hashref );
337 my $slice = 100/scalar(@new_export_svc) if @new_export_svc;
339 foreach my $export_svc (@new_export_svc) {
340 $error = $export_svc->insert($job, $slice*$done++, $slice);
342 $dbh->rollback if $oldAutoCommit;
346 $error = $job->update_statustext( int( $slice * $done ) );
348 $dbh->rollback if $oldAutoCommit;
357 $dbh->rollback if $oldAutoCommit;
358 return 'non-1.3-COMPAT interface not yet written';
362 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
369 Checks all fields to make sure this is a valid service definition. If there is
370 an error, returns the error, otherwise returns false. Called by the insert
380 $self->ut_numbern('svcpart')
381 || $self->ut_text('svc')
382 || $self->ut_alpha('svcdb')
383 || $self->ut_enum('disabled', [ '', 'Y' ] )
385 return $error if $error;
387 my @fields = eval { fields( $self->svcdb ) }; #might die
388 return "Unknown svcdb: ". $self->svcdb. " (Error: $@)"
394 =item part_svc_column COLUMNNAME
396 Returns the part_svc_column object (see L<FS::part_svc_column>) for the given
397 COLUMNNAME, or a new part_svc_column object if none exists.
401 sub part_svc_column {
402 my( $self, $columnname) = @_;
404 qsearchs('part_svc_column', {
405 'svcpart' => $self->svcpart,
406 'columnname' => $columnname,
408 ) or new FS::part_svc_column {
409 'svcpart' => $self->svcpart,
410 'columnname' => $columnname,
414 =item all_part_svc_column
418 sub all_part_svc_column {
420 qsearch('part_svc_column', { 'svcpart' => $self->svcpart } );
423 =item part_export [ EXPORTTYPE ]
425 Returns a list of all exports (see L<FS::part_export>) for this service, or,
426 if an export type is specified, only returns exports of the given type.
433 $search{'exporttype'} = shift if @_;
434 map { qsearchs('part_export', { 'exportnum' => $_->exportnum, %search } ) }
435 qsearch('export_svc', { 'svcpart' => $self->svcpart } );
438 =item part_export_usage
440 Returns a list of any exports (see L<FS::part_export>) for this service that
441 are capable of reporting usage information.
445 sub part_export_usage {
447 grep $_->can('usage_sessions'), $self->part_export;
450 =item part_export_did
452 Returns a list of any exports (see L<FS::part_export>) for this service that
453 are capable of returing available DID (phone number) information.
457 sub part_export_did {
459 grep $_->can('get_dids'), $self->part_export;
463 =item cust_svc [ PKGPART ]
465 Returns a list of associated customer services (FS::cust_svc records).
467 If a PKGPART is specified, returns the customer services which are contained
468 within packages of that type (see L<FS::part_pkg>). If PKGPARTis specified as
469 B<0>, returns unlinked customer services.
476 my $hashref = { 'svcpart' => $self->svcpart };
478 my( $addl_from, $extra_sql ) = ( '', '' );
481 if ( $pkgpart =~ /^(\d+)$/ ) {
482 $addl_from = 'LEFT JOIN cust_pkg USING ( pkgnum )';
483 $extra_sql = "AND pkgpart = $1";
484 } elsif ( $pkgpart eq '0' ) {
485 $hashref->{'pkgnum'} = '';
490 'table' => 'cust_svc',
491 'addl_from' => $addl_from,
492 'hashref' => $hashref,
493 'extra_sql' => $extra_sql,
497 =item num_cust_svc [ PKGPART ]
499 Returns the number of associated customer services (FS::cust_svc records).
501 If a PKGPART is specified, returns the number of customer services which are
502 contained within packages of that type (see L<FS::part_pkg>). If PKGPART
503 is specified as B<0>, returns the number of unlinked customer services.
510 my @param = ( $self->svcpart );
512 my( $join, $and ) = ( '', '' );
516 $join = 'LEFT JOIN cust_pkg USING ( pkgnum )';
517 $and = 'AND pkgpart = ?';
518 push @param, $pkgpart;
519 } elsif ( $pkgpart eq '0' ) {
520 $and = 'AND pkgnum IS NULL';
524 my $sth = dbh->prepare(
525 "SELECT COUNT(*) FROM cust_svc $join WHERE svcpart = ? $and"
526 ) or die dbh->errstr;
527 $sth->execute(@param)
529 $sth->fetchrow_arrayref->[0];
534 Returns a list of associated FS::svc_* records.
540 map { $_->svc_x } $self->cust_svc;
554 return $svc_defs if $svc_defs; #cache
556 my $conf = new FS::Conf;
558 #false laziness w/part_pkg.pm::plan_info
561 foreach my $INC ( @INC ) {
562 warn "globbing $INC/FS/svc_*.pm\n" if $DEBUG;
563 foreach my $file ( glob("$INC/FS/svc_*.pm") ) {
565 warn "attempting to load service table info from $file\n" if $DEBUG;
566 $file =~ /\/(\w+)\.pm$/ or do {
567 warn "unrecognized file in $INC/FS/: $file\n";
572 if ( $mod =~ /^svc_[A-Z]/ or $mod =~ /^svc_acct_pop$/ ) {
573 warn "skipping FS::$mod" if $DEBUG;
577 eval "use FS::$mod;";
579 die "error using FS::$mod (skipping): $@\n" if $@;
582 unless ( UNIVERSAL::can("FS::$mod", 'table_info') ) {
583 warn "FS::$mod has no table_info method; skipping";
587 my $info = "FS::$mod"->table_info;
588 unless ( keys %$info ) {
589 warn "FS::$mod->table_info doesn't return info, skipping\n";
592 warn "got info from FS::$mod: $info\n" if $DEBUG;
593 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
594 warn "skipping disabled service FS::$mod" if $DEBUG;
601 tie my %svc_defs, 'Tie::IxHash',
602 map { $_ => $info{$_}->{'fields'} }
603 sort { $info{$a}->{'display_weight'} <=> $info{$b}->{'display_weight'} }
607 # yuck. maybe this won't be so bad when virtual fields become real fields
609 foreach my $svcdb (grep dbdef->table($_), keys %svc_defs ) {
610 eval "use FS::$svcdb;";
611 my $self = "FS::$svcdb"->new;
612 $vfields{$svcdb} = {};
613 foreach my $field ($self->virtual_fields) { # svc_Common::virtual_fields with a null svcpart returns all of them
614 my $pvf = $self->pvf($field);
615 my @list = $pvf->list;
617 $svc_defs{$svcdb}->{$field} = { desc => $pvf->label,
619 select_list => \@list };
621 $svc_defs{$svcdb}->{$field} = $pvf->label;
623 $vfields{$svcdb}->{$field} = $pvf;
624 warn "\$vfields{$svcdb}->{$field} = $pvf"
629 $svc_defs = \%svc_defs; #cache
635 Returns a list of all svc_ tables.
641 my $svc_defs = $class->_svc_defs;
642 grep { defined( dbdef->table($_) ) } keys %$svc_defs;
645 =item svc_table_fields TABLE
647 Given a table name, returns a hashref of field names. The field names
648 returned are those with additional (service-definition related) information,
649 not necessarily all database fields of the table. Pseudo-fields may also
650 be returned (i.e. svc_acct.usergroup).
652 Each value of the hashref is another hashref, which can have one or more of
657 =item label - Description of the field
659 =item def_label - Optional description of the field in the context of service definitions
661 =item type - Currently "text", "select", "disabled", or "radius_usergroup_selector"
663 =item disable_default - This field should not allow a default value in service definitions
665 =item disable_fixed - This field should not allow a fixed value in service definitions
667 =item disable_inventory - This field should not allow inventory values in service definitions
669 =item select_list - If type is "text", this can be a listref of possible values.
671 =item select_table - An alternative to select_list, this defines a database table with the possible choices.
673 =item select_key - Used with select_table, this is the field name of keys
675 =item select_label - Used with select_table, this is the field name of labels
681 #maybe this should move and be a class method in svc_Common.pm
682 sub svc_table_fields {
683 my($class, $table) = @_;
684 my $svc_defs = $class->_svc_defs;
685 my $def = $svc_defs->{$table};
687 foreach ( grep !ref($def->{$_}), keys %$def ) {
689 #normalize the shortcut in %info hash
690 $def->{$_} = { 'label' => $def->{$_} };
692 $def->{$_}{'type'} ||= 'text';
707 Job-queue processor for web interface adds/edits
711 use Storable qw(thaw);
717 my $param = thaw(decode_base64(shift));
718 warn Dumper($param) if $DEBUG;
720 my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} })
721 if $param->{'svcpart'};
723 $param->{'svc_acct__usergroup'} =
724 ref($param->{'svc_acct__usergroup'})
725 ? join(',', @{$param->{'svc_acct__usergroup'}} )
726 : $param->{'svc_acct__usergroup'};
728 #unmunge cgp_accessmodes (falze laziness-ish w/edit/process/svc_acct.cgi)
729 $param->{'svc_acct__cgp_accessmodes'} ||=
731 grep { $_ !~ /^(flag|label)$/ }
732 map { /^svc_acct__cgp_accessmodes_([\w\/]+)$/ or die "no way"; $1; }
734 grep /^svc_acct__cgp_accessmodes_([\w\/]+)$/,
739 my $new = new FS::part_svc ( {
742 # } qw(svcpart svc svcdb)
743 } ( fields('part_svc'),
744 map { my $svcdb = $_;
745 my @fields = fields($svcdb);
746 push @fields, 'usergroup' if $svcdb eq 'svc_acct'; #kludge
749 my $f = $svcdb.'__'.$_;
750 if ( $param->{ $f.'_flag' } =~ /^[MA]$/ ) {
751 $param->{ $f } = delete( $param->{ $f.'_classnum' } );
753 if ( $param->{ $f.'_flag' } =~ /^S$/ ) {
754 $param->{ $f } = ref($param->{ $f })
755 ? join(',', @{$param->{ $f }} )
758 ( $f, $f.'_flag', $f.'_label' );
762 } FS::part_svc->svc_tables()
767 map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
768 qsearch('part_export', {} );
771 if ( $param->{'svcpart'} ) {
772 $error = $new->replace( $old,
773 '1.3-COMPAT', #totally bunk, as jeff noted
779 $error = $new->insert( [ 'usergroup' ],
783 $param->{'svcpart'} = $new->getfield('svcpart');
786 die "$error\n" if $error;
789 =item process_bulk_cust_svc
791 Job-queue processor for web interface bulk customer service changes
795 use Storable qw(thaw);
798 sub process_bulk_cust_svc {
801 my $param = thaw(decode_base64(shift));
802 warn Dumper($param) if $DEBUG;
804 local($FS::svc_Common::noexport_hack) = 1
805 if $param->{'noexport'};
808 qsearchs('part_svc', { 'svcpart' => $param->{'old_svcpart'} } );
810 die "Must select a new service definition\n" unless $param->{'new_svcpart'};
812 #the rest should be abstracted out to to its own subroutine?
814 local $SIG{HUP} = 'IGNORE';
815 local $SIG{INT} = 'IGNORE';
816 local $SIG{QUIT} = 'IGNORE';
817 local $SIG{TERM} = 'IGNORE';
818 local $SIG{TSTP} = 'IGNORE';
819 local $SIG{PIPE} = 'IGNORE';
821 my $oldAutoCommit = $FS::UID::AutoCommit;
822 local $FS::UID::AutoCommit = 0;
825 local( $FS::cust_svc::ignore_quantity ) = 1;
827 my $total = $old_part_svc->num_cust_svc( $param->{'pkgpart'} );
830 foreach my $old_cust_svc ( $old_part_svc->cust_svc( $param->{'pkgpart'} ) ) {
832 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
834 $new_cust_svc->svcpart( $param->{'new_svcpart'} );
835 my $error = $new_cust_svc->replace($old_cust_svc);
837 $dbh->rollback if $oldAutoCommit;
838 die "$error\n" if $error;
841 $error = $job->update_statustext( int( 100 * ++$n / $total ) );
843 $dbh->rollback if $oldAutoCommit;
844 die $error if $error;
849 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
857 Delete is unimplemented.
859 The list of svc_* tables is no longer hardcoded, but svc_acct_pop is skipped
860 as a special case until it is renamed.
862 all_part_svc_column methods should be documented
866 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
867 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
868 schema.html from the base documentation.