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;
12 use FS::part_svc_class;
14 @ISA = qw(FS::Record);
20 FS::part_svc - Object methods for part_svc objects
26 $record = new FS::part_svc \%hash
27 $record = new FS::part_svc { 'column' => 'value' };
29 $error = $record->insert;
30 $error = $record->insert( [ 'pseudofield' ] );
31 $error = $record->insert( [ 'pseudofield' ], \%exportnums );
33 $error = $new_record->replace($old_record);
34 $error = $new_record->replace($old_record, '1.3-COMPAT', [ 'pseudofield' ] );
35 $error = $new_record->replace($old_record, '1.3-COMPAT', [ 'pseudofield' ], \%exportnums );
37 $error = $record->delete;
39 $error = $record->check;
43 An FS::part_svc represents a service definition. FS::part_svc inherits from
44 FS::Record. The following fields are currently supported:
48 =item svcpart - primary key (assigned automatically for new service definitions)
50 =item svc - text name of this service definition
52 =item svcdb - table used for this service. See L<FS::svc_acct>,
53 L<FS::svc_domain>, and L<FS::svc_forward>, among others.
55 =item classnum - Optional service class (see L<FS::svc_class>)
57 =item disabled - Disabled flag, empty or `Y'
59 =item preserve - Preserve after cancellation, empty or 'Y'
61 =item selfservice_access - Access allowed to the service via self-service:
62 empty for full access, "readonly" for read-only, "hidden" to hide it entirely
64 =item restrict_edit_password - Require the "Provision customer service" access
65 right to change the password field, rather than just "Edit password". Only
66 relevant to svc_acct for now.
68 =item has_router - Allow the service to have an L<FS::router> connected
69 through it. Probably only relevant to svc_broadband, svc_acct, and svc_dsl
80 Creates a new service definition. To add the service definition to the
81 database, see L<"insert">.
85 sub table { 'part_svc'; }
87 =item insert [ EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ]
89 Adds this service definition to the database. If there is an error, returns
90 the error, otherwise returns false.
92 The following pseudo-fields may be defined, and will be maintained in
93 the part_svc_column table appropriately (see L<FS::part_svc_column>).
97 =item I<svcdb>__I<field> - Default or fixed value for I<field> in I<svcdb>.
99 =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.
103 If you want to add part_svc_column records for fields that do not exist as
104 fields in the I<svcdb> table, make sure to list then in
105 EXTRA_FIELDS_ARRAYREF also.
107 If EXPORTNUMS_HASHREF is specified (keys are exportnums and values are
108 boolean), the appopriate export_svc records will be inserted.
118 @fields = @{shift(@_)} if @_;
120 my $exportnums = shift;
121 @exportnums = grep $exportnums->{$_}, keys %$exportnums;
126 local $SIG{HUP} = 'IGNORE';
127 local $SIG{INT} = 'IGNORE';
128 local $SIG{QUIT} = 'IGNORE';
129 local $SIG{TERM} = 'IGNORE';
130 local $SIG{TSTP} = 'IGNORE';
131 local $SIG{PIPE} = 'IGNORE';
133 my $oldAutoCommit = $FS::UID::AutoCommit;
134 local $FS::UID::AutoCommit = 0;
137 my $error = $self->SUPER::insert;
139 $dbh->rollback if $oldAutoCommit;
143 # add part_svc_column records
145 my $svcdb = $self->svcdb;
146 # my @rows = map { /^${svcdb}__(.*)$/; $1 }
148 # grep /^${svcdb}__/,
149 # fields('part_svc');
151 grep { $_ ne 'svcnum'
152 && ( defined( $self->getfield($svcdb.'__'.$_.'_flag') )
153 || $self->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
154 } (fields($svcdb), @fields)
156 my $part_svc_column = $self->part_svc_column($field);
157 my $previous = qsearchs('part_svc_column', {
158 'svcpart' => $self->svcpart,
159 'columnname' => $field,
162 my $flag = $self->getfield($svcdb.'__'.$field.'_flag');
163 my $label = $self->getfield($svcdb.'__'.$field.'_label');
164 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
166 if ( uc($flag) =~ /^([A-Z])$/ ) {
167 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
169 $part_svc_column->setfield('columnflag', $1);
170 $part_svc_column->setfield('columnvalue',
171 &$parser($self->getfield($svcdb.'__'.$field))
175 $part_svc_column->setfield('columnlabel', $label)
176 if $label !~ /^\s*$/;
179 $error = $part_svc_column->replace($previous);
181 $error = $part_svc_column->insert;
185 $error = $previous ? $previous->delete : '';
188 $dbh->rollback if $oldAutoCommit;
194 # add export_svc records
195 my $slice = 100/scalar(@exportnums) if @exportnums;
197 foreach my $exportnum ( @exportnums ) {
198 my $export_svc = new FS::export_svc ( {
199 'exportnum' => $exportnum,
200 'svcpart' => $self->svcpart,
202 $error = $export_svc->insert($job, $slice*$done++, $slice);
204 $dbh->rollback if $oldAutoCommit;
209 # XXX shouldn't this update fixed values?
211 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
218 Currently unimplemented. Set the "disabled" field instead.
223 return "Can't (yet?) delete service definitions.";
224 # check & make sure the svcpart isn't in cust_svc or pkg_svc (in any packages)?
227 =item replace OLD_RECORD [ '1.3-COMPAT' [ , EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ] ]
229 Replaces OLD_RECORD with this one in the database. If there is an error,
230 returns the error, otherwise returns false.
234 TODOC: EXTRA_FIELDS_ARRAYREF (same as insert method)
241 my ( $new, $old ) = ( shift, shift );
246 if ( @_ && $_[0] eq '1.3-COMPAT' ) {
249 @fields = @{shift(@_)} if @_;
250 $exportnums = @_ ? shift : '';
253 return 'non-1.3-COMPAT interface not yet written';
257 return "Can't change svcdb for an existing service definition!"
258 unless $old->svcdb eq $new->svcdb;
260 local $SIG{HUP} = 'IGNORE';
261 local $SIG{INT} = 'IGNORE';
262 local $SIG{QUIT} = 'IGNORE';
263 local $SIG{TERM} = 'IGNORE';
264 local $SIG{TSTP} = 'IGNORE';
265 local $SIG{PIPE} = 'IGNORE';
267 my $oldAutoCommit = $FS::UID::AutoCommit;
268 local $FS::UID::AutoCommit = 0;
271 my $error = $new->SUPER::replace( $old );
273 $dbh->rollback if $oldAutoCommit;
277 if ( $compat eq '1.3' ) {
279 # maintain part_svc_column records
281 my $svcdb = $new->svcdb;
283 grep { $_ ne 'svcnum'
284 && ( defined( $new->getfield($svcdb.'__'.$_.'_flag') )
285 || $new->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
286 } (fields($svcdb),@fields)
289 my $part_svc_column = $new->part_svc_column($field);
290 my $previous = qsearchs('part_svc_column', {
291 'svcpart' => $new->svcpart,
292 'columnname' => $field,
295 my $flag = $new->getfield($svcdb.'__'.$field.'_flag');
296 my $label = $new->getfield($svcdb.'__'.$field.'_label');
298 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
300 if ( uc($flag) =~ /^([A-Z])$/ ) {
301 $part_svc_column->setfield('columnflag', $1);
302 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
304 $part_svc_column->setfield('columnvalue',
305 &$parser($new->getfield($svcdb.'__'.$field))
308 $part_svc_column->setfield('columnflag', '');
309 $part_svc_column->setfield('columnvalue', '');
312 $part_svc_column->setfield('columnlabel', $label)
313 if $label !~ /^\s*$/;
316 $error = $part_svc_column->replace($previous);
318 $error = $part_svc_column->insert;
321 $error = $previous ? $previous->delete : '';
324 $dbh->rollback if $oldAutoCommit;
329 # maintain export_svc records
333 #false laziness w/ edit/process/agent_type.cgi
334 my @new_export_svc = ();
335 foreach my $part_export ( qsearch('part_export', {}) ) {
336 my $exportnum = $part_export->exportnum;
338 'exportnum' => $exportnum,
339 'svcpart' => $new->svcpart,
341 my $export_svc = qsearchs('export_svc', $hashref);
343 if ( $export_svc && ! $exportnums->{$exportnum} ) {
344 $error = $export_svc->delete;
346 $dbh->rollback if $oldAutoCommit;
349 } elsif ( ! $export_svc && $exportnums->{$exportnum} ) {
350 push @new_export_svc, new FS::export_svc ( $hashref );
355 my $slice = 100/scalar(@new_export_svc) if @new_export_svc;
357 foreach my $export_svc (@new_export_svc) {
358 $error = $export_svc->insert($job, $slice*$done++, $slice);
360 $dbh->rollback if $oldAutoCommit;
364 $error = $job->update_statustext( int( $slice * $done ) );
366 $dbh->rollback if $oldAutoCommit;
375 $dbh->rollback if $oldAutoCommit;
376 return 'non-1.3-COMPAT interface not yet written';
380 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
387 Checks all fields to make sure this is a valid service definition. If there is
388 an error, returns the error, otherwise returns false. Called by the insert
398 $self->ut_numbern('svcpart')
399 || $self->ut_text('svc')
400 || $self->ut_alpha('svcdb')
401 || $self->ut_flag('disabled')
402 || $self->ut_flag('preserve')
403 || $self->ut_enum('selfservice_access', [ '', 'hidden', 'readonly' ] )
404 || $self->ut_foreign_keyn('classnum', 'part_svc_class', 'classnum' )
405 || $self->ut_flag('restrict_edit_password')
406 || $self->ut_flag('has_router')
408 return $error if $error;
410 my @fields = eval { fields( $self->svcdb ) }; #might die
411 return "Unknown svcdb: ". $self->svcdb. " (Error: $@)"
417 =item part_svc_column COLUMNNAME
419 Returns the part_svc_column object (see L<FS::part_svc_column>) for the given
420 COLUMNNAME, or a new part_svc_column object if none exists.
424 sub part_svc_column {
425 my( $self, $columnname) = @_;
427 qsearchs('part_svc_column', {
428 'svcpart' => $self->svcpart,
429 'columnname' => $columnname,
431 ) or new FS::part_svc_column {
432 'svcpart' => $self->svcpart,
433 'columnname' => $columnname,
437 =item all_part_svc_column
441 sub all_part_svc_column {
443 qsearch('part_svc_column', { 'svcpart' => $self->svcpart } );
446 =item part_export [ EXPORTTYPE ]
448 Returns a list of all exports (see L<FS::part_export>) for this service, or,
449 if an export type is specified, only returns exports of the given type.
456 $search{'exporttype'} = shift if @_;
457 map { $_ } #behavior of sort undefined in scalar context
458 sort { $a->weight <=> $b->weight }
459 map { qsearchs('part_export', { 'exportnum'=>$_->exportnum, %search } ) }
460 qsearch('export_svc', { 'svcpart'=>$self->svcpart } );
463 =item part_export_usage
465 Returns a list of any exports (see L<FS::part_export>) for this service that
466 are capable of reporting usage information.
470 sub part_export_usage {
472 grep $_->can('usage_sessions'), $self->part_export;
475 =item part_export_did
477 Returns a list of any exports (see L<FS::part_export>) for this service that
478 are capable of returing available DID (phone number) information.
482 sub part_export_did {
484 grep $_->can('get_dids'), $self->part_export;
487 =item part_export_dsl_pull
489 Returns a list of any exports (see L<FS::part_export>) for this service that
490 are capable of pulling/pushing DSL orders.
494 sub part_export_dsl_pull {
496 grep $_->can('dsl_pull'), $self->part_export;
499 =item cust_svc [ PKGPART ]
501 Returns a list of associated customer services (FS::cust_svc records).
503 If a PKGPART is specified, returns the customer services which are contained
504 within packages of that type (see L<FS::part_pkg>). If PKGPARTis specified as
505 B<0>, returns unlinked customer services.
512 my $hashref = { 'svcpart' => $self->svcpart };
514 my( $addl_from, $extra_sql ) = ( '', '' );
517 if ( $pkgpart =~ /^(\d+)$/ ) {
518 $addl_from = 'LEFT JOIN cust_pkg USING ( pkgnum )';
519 $extra_sql = "AND pkgpart = $1";
520 } elsif ( $pkgpart eq '0' ) {
521 $hashref->{'pkgnum'} = '';
526 'table' => 'cust_svc',
527 'addl_from' => $addl_from,
528 'hashref' => $hashref,
529 'extra_sql' => $extra_sql,
533 =item num_cust_svc [ PKGPART ]
535 Returns the number of associated customer services (FS::cust_svc records).
537 If a PKGPART is specified, returns the number of customer services which are
538 contained within packages of that type (see L<FS::part_pkg>). If PKGPART
539 is specified as B<0>, returns the number of unlinked customer services.
546 my @param = ( $self->svcpart );
548 my( $join, $and ) = ( '', '' );
552 $join = 'LEFT JOIN cust_pkg USING ( pkgnum )';
553 $and = 'AND pkgpart = ?';
554 push @param, $pkgpart;
555 } elsif ( $pkgpart eq '0' ) {
556 $and = 'AND pkgnum IS NULL';
560 my $sth = dbh->prepare(
561 "SELECT COUNT(*) FROM cust_svc $join WHERE svcpart = ? $and"
562 ) or die dbh->errstr;
563 $sth->execute(@param)
565 $sth->fetchrow_arrayref->[0];
570 Returns a list of associated FS::svc_* records.
576 map { $_->svc_x } $self->cust_svc;
590 return $svc_defs if $svc_defs; #cache
592 my $conf = new FS::Conf;
594 #false laziness w/part_pkg.pm::plan_info
597 foreach my $INC ( @INC ) {
598 warn "globbing $INC/FS/svc_*.pm\n" if $DEBUG;
599 foreach my $file ( glob("$INC/FS/svc_*.pm") ) {
601 warn "attempting to load service table info from $file\n" if $DEBUG;
602 $file =~ /\/(\w+)\.pm$/ or do {
603 warn "unrecognized file in $INC/FS/: $file\n";
608 if ( $mod =~ /^svc_[A-Z]/ or $mod =~ /^(svc_acct_pop|svc_export_machine)$/ ) {
609 warn "skipping FS::$mod" if $DEBUG;
613 eval "use FS::$mod;";
615 die "error using FS::$mod (skipping): $@\n" if $@;
618 unless ( UNIVERSAL::can("FS::$mod", 'table_info') ) {
619 warn "FS::$mod has no table_info method; skipping";
623 my $info = "FS::$mod"->table_info;
624 unless ( keys %$info ) {
625 warn "FS::$mod->table_info doesn't return info, skipping\n";
628 warn "got info from FS::$mod: $info\n" if $DEBUG;
629 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
630 warn "skipping disabled service FS::$mod" if $DEBUG;
637 tie my %svc_defs, 'Tie::IxHash',
638 map { $_ => $info{$_}->{'fields'} }
639 sort { $info{$a}->{'display_weight'} <=> $info{$b}->{'display_weight'} }
643 $svc_defs = \%svc_defs; #cache
649 Returns a list of all svc_ tables.
655 my $svc_defs = $class->_svc_defs;
656 grep { defined( dbdef->table($_) ) } keys %$svc_defs;
659 =item svc_table_fields TABLE
661 Given a table name, returns a hashref of field names. The field names
662 returned are those with additional (service-definition related) information,
663 not necessarily all database fields of the table. Pseudo-fields may also
664 be returned (i.e. svc_acct.usergroup).
666 Each value of the hashref is another hashref, which can have one or more of
671 =item label - Description of the field
673 =item def_label - Optional description of the field in the context of service definitions
675 =item type - Currently "text", "select", "checkbox", "textarea", "disabled",
676 some components specified by "select-.*.html", and a bunch more...
678 =item disable_default - This field should not allow a default value in service definitions
680 =item disable_fixed - This field should not allow a fixed value in service definitions
682 =item disable_inventory - This field should not allow inventory values in service definitions
684 =item select_list - If type is "text", this can be a listref of possible values.
686 =item select_table - An alternative to select_list, this defines a database table with the possible choices.
688 =item select_key - Used with select_table, this is the field name of keys
690 =item select_label - Used with select_table, this is the field name of labels
696 #maybe this should move and be a class method in svc_Common.pm
697 sub svc_table_fields {
698 my($class, $table) = @_;
699 my $svc_defs = $class->_svc_defs;
700 my $def = $svc_defs->{$table};
702 foreach ( grep !ref($def->{$_}), keys %$def ) {
704 #normalize the shortcut in %info hash
705 $def->{$_} = { 'label' => $def->{$_} };
707 $def->{$_}{'type'} ||= 'text';
722 Job-queue processor for web interface adds/edits
726 use Storable qw(thaw);
732 my $param = thaw(decode_base64(shift));
733 warn Dumper($param) if $DEBUG;
735 my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} })
736 if $param->{'svcpart'};
738 #unmunge cgp_accessmodes (falze laziness-ish w/edit/process/svc_acct.cgi)
739 $param->{'svc_acct__cgp_accessmodes'} ||=
741 grep { $_ !~ /^(flag|label)$/ }
742 map { /^svc_acct__cgp_accessmodes_([\w\/]+)$/ or die "no way"; $1; }
744 grep /^svc_acct__cgp_accessmodes_([\w\/]+)$/,
749 my $new = new FS::part_svc ( {
752 # } qw(svcpart svc svcdb)
753 } ( fields('part_svc'),
754 map { my $svcdb = $_;
755 my @fields = fields($svcdb);
756 push @fields, 'usergroup' if $svcdb eq 'svc_acct'
757 or $svcdb eq 'svc_broadband'; #kludge
760 my $f = $svcdb.'__'.$_;
761 my $flag = $param->{ $f.'_flag' } || ''; #silence warnings
762 if ( $flag =~ /^[MAH]$/ ) {
763 $param->{ $f } = delete( $param->{ $f.'_classnum' } );
765 if ( ( $flag =~ /^[MAHS]$/ or $_ eq 'usergroup' )
766 and ref($param->{ $f }) ) {
767 $param->{ $f } = join(',', @{ $param->{ $f } });
769 ( $f, $f.'_flag', $f.'_label' );
773 } FS::part_svc->svc_tables()
778 map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
779 qsearch('part_export', {} );
782 if ( $param->{'svcpart'} ) {
783 $error = $new->replace( $old,
784 '1.3-COMPAT', #totally bunk, as jeff noted
790 $error = $new->insert( [ 'usergroup' ],
794 $param->{'svcpart'} = $new->getfield('svcpart');
797 die "$error\n" if $error;
800 =item process_bulk_cust_svc
802 Job-queue processor for web interface bulk customer service changes
806 use Storable qw(thaw);
809 sub process_bulk_cust_svc {
812 my $param = thaw(decode_base64(shift));
813 warn Dumper($param) if $DEBUG;
815 local($FS::svc_Common::noexport_hack) = 1
816 if $param->{'noexport'};
819 qsearchs('part_svc', { 'svcpart' => $param->{'old_svcpart'} } );
821 die "Must select a new service definition\n" unless $param->{'new_svcpart'};
823 #the rest should be abstracted out to to its own subroutine?
825 local $SIG{HUP} = 'IGNORE';
826 local $SIG{INT} = 'IGNORE';
827 local $SIG{QUIT} = 'IGNORE';
828 local $SIG{TERM} = 'IGNORE';
829 local $SIG{TSTP} = 'IGNORE';
830 local $SIG{PIPE} = 'IGNORE';
832 my $oldAutoCommit = $FS::UID::AutoCommit;
833 local $FS::UID::AutoCommit = 0;
836 local( $FS::cust_svc::ignore_quantity ) = 1;
838 my $total = $old_part_svc->num_cust_svc( $param->{'pkgpart'} );
841 foreach my $old_cust_svc ( $old_part_svc->cust_svc( $param->{'pkgpart'} ) ) {
843 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
845 $new_cust_svc->svcpart( $param->{'new_svcpart'} );
846 my $error = $new_cust_svc->replace($old_cust_svc);
848 $dbh->rollback if $oldAutoCommit;
849 die "$error\n" if $error;
852 $error = $job->update_statustext( int( 100 * ++$n / $total ) );
854 $dbh->rollback if $oldAutoCommit;
855 die $error if $error;
860 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
866 sub _upgrade_data { #class method
867 my ($class, %opts) = @_;
869 my @part_svc_column = qsearch('part_svc_column', { 'columnname' => 'usergroup' });
870 foreach my $col ( @part_svc_column ) {
871 next if $col->columnvalue =~ /^[\d,]+$/ || !$col->columnvalue;
872 my @groupnames = split(',',$col->columnvalue);
875 foreach my $groupname ( @groupnames ) {
876 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
878 $g = new FS::radius_group {
879 'groupname' => $groupname,
880 'description' => $groupname,
883 die "Error inserting new radius_group for service definition group \"$groupname\": $error"
886 push @groupnums, $g->groupnum;
888 $col->columnvalue(join(',',@groupnums));
889 $error = $col->replace;
890 die $error if $error;
893 my @badlabels = qsearch({
894 'table' => 'part_svc_column',
896 'extra_sql' => 'WHERE columnlabel IN ('.
897 "'Descriptive label for this particular device.',".
898 "'IP address. Leave blank for automatic assignment.',".
899 "'Maximum upload speed for this service in Kbps. 0 denotes unlimited.',".
900 "'Maximum download speed for this service in Kbps. 0 denotes unlimited.')"
902 foreach my $col ( @badlabels ) {
903 $col->columnlabel('');
904 my $error = $col->replace;
905 die $error if $error;
912 Delete is unimplemented.
914 The list of svc_* tables is no longer hardcoded, but svc_acct_pop is skipped
915 as a special case until it is renamed.
917 all_part_svc_column methods should be documented
921 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
922 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
923 schema.html from the base documentation.