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'
69 Creates a new service definition. To add the service definition to the
70 database, see L<"insert">.
74 sub table { 'part_svc'; }
76 =item insert [ EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ]
78 Adds this service definition to the database. If there is an error, returns
79 the error, otherwise returns false.
81 The following pseudo-fields may be defined, and will be maintained in
82 the part_svc_column table appropriately (see L<FS::part_svc_column>).
86 =item I<svcdb>__I<field> - Default or fixed value for I<field> in I<svcdb>.
88 =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.
92 If you want to add part_svc_column records for fields that do not exist as
93 fields in the I<svcdb> table, make sure to list then in
94 EXTRA_FIELDS_ARRAYREF also.
96 If EXPORTNUMS_HASHREF is specified (keys are exportnums and values are
97 boolean), the appopriate export_svc records will be inserted.
107 @fields = @{shift(@_)} if @_;
109 my $exportnums = shift;
110 @exportnums = grep $exportnums->{$_}, keys %$exportnums;
115 local $SIG{HUP} = 'IGNORE';
116 local $SIG{INT} = 'IGNORE';
117 local $SIG{QUIT} = 'IGNORE';
118 local $SIG{TERM} = 'IGNORE';
119 local $SIG{TSTP} = 'IGNORE';
120 local $SIG{PIPE} = 'IGNORE';
122 my $oldAutoCommit = $FS::UID::AutoCommit;
123 local $FS::UID::AutoCommit = 0;
126 my $error = $self->SUPER::insert;
128 $dbh->rollback if $oldAutoCommit;
132 # add part_svc_column records
134 my $svcdb = $self->svcdb;
135 # my @rows = map { /^${svcdb}__(.*)$/; $1 }
137 # grep /^${svcdb}__/,
138 # fields('part_svc');
140 grep { $_ ne 'svcnum'
141 && ( defined( $self->getfield($svcdb.'__'.$_.'_flag') )
142 || $self->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
143 } (fields($svcdb), @fields)
145 my $part_svc_column = $self->part_svc_column($field);
146 my $previous = qsearchs('part_svc_column', {
147 'svcpart' => $self->svcpart,
148 'columnname' => $field,
151 my $flag = $self->getfield($svcdb.'__'.$field.'_flag');
152 my $label = $self->getfield($svcdb.'__'.$field.'_label');
153 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
155 if ( uc($flag) =~ /^([A-Z])$/ ) {
156 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
158 $part_svc_column->setfield('columnflag', $1);
159 $part_svc_column->setfield('columnvalue',
160 &$parser($self->getfield($svcdb.'__'.$field))
164 $part_svc_column->setfield('columnlabel', $label)
165 if $label !~ /^\s*$/;
168 $error = $part_svc_column->replace($previous);
170 $error = $part_svc_column->insert;
174 $error = $previous ? $previous->delete : '';
177 $dbh->rollback if $oldAutoCommit;
183 # add export_svc records
184 my $slice = 100/scalar(@exportnums) if @exportnums;
186 foreach my $exportnum ( @exportnums ) {
187 my $export_svc = new FS::export_svc ( {
188 'exportnum' => $exportnum,
189 'svcpart' => $self->svcpart,
191 $error = $export_svc->insert($job, $slice*$done++, $slice);
193 $dbh->rollback if $oldAutoCommit;
198 # XXX shouldn't this update fixed values?
200 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
207 Currently unimplemented. Set the "disabled" field instead.
212 return "Can't (yet?) delete service definitions.";
213 # check & make sure the svcpart isn't in cust_svc or pkg_svc (in any packages)?
216 =item replace OLD_RECORD [ '1.3-COMPAT' [ , EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ] ]
218 Replaces OLD_RECORD with this one in the database. If there is an error,
219 returns the error, otherwise returns false.
223 TODOC: EXTRA_FIELDS_ARRAYREF (same as insert method)
230 my ( $new, $old ) = ( shift, shift );
235 if ( @_ && $_[0] eq '1.3-COMPAT' ) {
238 @fields = @{shift(@_)} if @_;
239 $exportnums = @_ ? shift : '';
242 return 'non-1.3-COMPAT interface not yet written';
246 return "Can't change svcdb for an existing service definition!"
247 unless $old->svcdb eq $new->svcdb;
249 local $SIG{HUP} = 'IGNORE';
250 local $SIG{INT} = 'IGNORE';
251 local $SIG{QUIT} = 'IGNORE';
252 local $SIG{TERM} = 'IGNORE';
253 local $SIG{TSTP} = 'IGNORE';
254 local $SIG{PIPE} = 'IGNORE';
256 my $oldAutoCommit = $FS::UID::AutoCommit;
257 local $FS::UID::AutoCommit = 0;
260 my $error = $new->SUPER::replace( $old );
262 $dbh->rollback if $oldAutoCommit;
266 if ( $compat eq '1.3' ) {
268 # maintain part_svc_column records
270 my $svcdb = $new->svcdb;
272 grep { $_ ne 'svcnum'
273 && ( defined( $new->getfield($svcdb.'__'.$_.'_flag') )
274 || $new->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
275 } (fields($svcdb),@fields)
278 my $part_svc_column = $new->part_svc_column($field);
279 my $previous = qsearchs('part_svc_column', {
280 'svcpart' => $new->svcpart,
281 'columnname' => $field,
284 my $flag = $new->getfield($svcdb.'__'.$field.'_flag');
285 my $label = $new->getfield($svcdb.'__'.$field.'_label');
287 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
289 if ( uc($flag) =~ /^([A-Z])$/ ) {
290 $part_svc_column->setfield('columnflag', $1);
291 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
293 $part_svc_column->setfield('columnvalue',
294 &$parser($new->getfield($svcdb.'__'.$field))
297 $part_svc_column->setfield('columnflag', '');
298 $part_svc_column->setfield('columnvalue', '');
301 $part_svc_column->setfield('columnlabel', $label)
302 if $label !~ /^\s*$/;
305 $error = $part_svc_column->replace($previous);
307 $error = $part_svc_column->insert;
310 $error = $previous ? $previous->delete : '';
313 $dbh->rollback if $oldAutoCommit;
318 # maintain export_svc records
322 #false laziness w/ edit/process/agent_type.cgi
323 my @new_export_svc = ();
324 foreach my $part_export ( qsearch('part_export', {}) ) {
325 my $exportnum = $part_export->exportnum;
327 'exportnum' => $exportnum,
328 'svcpart' => $new->svcpart,
330 my $export_svc = qsearchs('export_svc', $hashref);
332 if ( $export_svc && ! $exportnums->{$exportnum} ) {
333 $error = $export_svc->delete;
335 $dbh->rollback if $oldAutoCommit;
338 } elsif ( ! $export_svc && $exportnums->{$exportnum} ) {
339 push @new_export_svc, new FS::export_svc ( $hashref );
344 my $slice = 100/scalar(@new_export_svc) if @new_export_svc;
346 foreach my $export_svc (@new_export_svc) {
347 $error = $export_svc->insert($job, $slice*$done++, $slice);
349 $dbh->rollback if $oldAutoCommit;
353 $error = $job->update_statustext( int( $slice * $done ) );
355 $dbh->rollback if $oldAutoCommit;
364 $dbh->rollback if $oldAutoCommit;
365 return 'non-1.3-COMPAT interface not yet written';
369 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
376 Checks all fields to make sure this is a valid service definition. If there is
377 an error, returns the error, otherwise returns false. Called by the insert
387 $self->ut_numbern('svcpart')
388 || $self->ut_text('svc')
389 || $self->ut_alpha('svcdb')
390 || $self->ut_enum('disabled', [ '', 'Y' ] )
391 || $self->ut_enum('preserve', [ '', 'Y' ] )
392 || $self->ut_enum('selfservice_access', [ '', 'hidden', 'readonly' ] )
393 || $self->ut_foreign_keyn('classnum', 'part_svc_class', 'classnum' )
395 return $error if $error;
397 my @fields = eval { fields( $self->svcdb ) }; #might die
398 return "Unknown svcdb: ". $self->svcdb. " (Error: $@)"
404 =item part_svc_column COLUMNNAME
406 Returns the part_svc_column object (see L<FS::part_svc_column>) for the given
407 COLUMNNAME, or a new part_svc_column object if none exists.
411 sub part_svc_column {
412 my( $self, $columnname) = @_;
414 qsearchs('part_svc_column', {
415 'svcpart' => $self->svcpart,
416 'columnname' => $columnname,
418 ) or new FS::part_svc_column {
419 'svcpart' => $self->svcpart,
420 'columnname' => $columnname,
424 =item all_part_svc_column
428 sub all_part_svc_column {
430 qsearch('part_svc_column', { 'svcpart' => $self->svcpart } );
433 =item part_export [ EXPORTTYPE ]
435 Returns a list of all exports (see L<FS::part_export>) for this service, or,
436 if an export type is specified, only returns exports of the given type.
443 $search{'exporttype'} = shift if @_;
444 map { $_ } #behavior of sort undefined in scalar context
445 sort { $a->weight <=> $b->weight }
446 map { qsearchs('part_export', { 'exportnum'=>$_->exportnum, %search } ) }
447 qsearch('export_svc', { 'svcpart'=>$self->svcpart } );
450 =item part_export_usage
452 Returns a list of any exports (see L<FS::part_export>) for this service that
453 are capable of reporting usage information.
457 sub part_export_usage {
459 grep $_->can('usage_sessions'), $self->part_export;
462 =item part_export_did
464 Returns a list of any exports (see L<FS::part_export>) for this service that
465 are capable of returing available DID (phone number) information.
469 sub part_export_did {
471 grep $_->can('get_dids'), $self->part_export;
474 =item part_export_dsl_pull
476 Returns a list of any exports (see L<FS::part_export>) for this service that
477 are capable of pulling/pushing DSL orders.
481 sub part_export_dsl_pull {
483 grep $_->can('dsl_pull'), $self->part_export;
486 =item cust_svc [ PKGPART ]
488 Returns a list of associated customer services (FS::cust_svc records).
490 If a PKGPART is specified, returns the customer services which are contained
491 within packages of that type (see L<FS::part_pkg>). If PKGPARTis specified as
492 B<0>, returns unlinked customer services.
499 my $hashref = { 'svcpart' => $self->svcpart };
501 my( $addl_from, $extra_sql ) = ( '', '' );
504 if ( $pkgpart =~ /^(\d+)$/ ) {
505 $addl_from = 'LEFT JOIN cust_pkg USING ( pkgnum )';
506 $extra_sql = "AND pkgpart = $1";
507 } elsif ( $pkgpart eq '0' ) {
508 $hashref->{'pkgnum'} = '';
513 'table' => 'cust_svc',
514 'addl_from' => $addl_from,
515 'hashref' => $hashref,
516 'extra_sql' => $extra_sql,
520 =item num_cust_svc [ PKGPART ]
522 Returns the number of associated customer services (FS::cust_svc records).
524 If a PKGPART is specified, returns the number of customer services which are
525 contained within packages of that type (see L<FS::part_pkg>). If PKGPART
526 is specified as B<0>, returns the number of unlinked customer services.
533 my @param = ( $self->svcpart );
535 my( $join, $and ) = ( '', '' );
539 $join = 'LEFT JOIN cust_pkg USING ( pkgnum )';
540 $and = 'AND pkgpart = ?';
541 push @param, $pkgpart;
542 } elsif ( $pkgpart eq '0' ) {
543 $and = 'AND pkgnum IS NULL';
547 my $sth = dbh->prepare(
548 "SELECT COUNT(*) FROM cust_svc $join WHERE svcpart = ? $and"
549 ) or die dbh->errstr;
550 $sth->execute(@param)
552 $sth->fetchrow_arrayref->[0];
557 Returns a list of associated FS::svc_* records.
563 map { $_->svc_x } $self->cust_svc;
577 return $svc_defs if $svc_defs; #cache
579 my $conf = new FS::Conf;
581 #false laziness w/part_pkg.pm::plan_info
584 foreach my $INC ( @INC ) {
585 warn "globbing $INC/FS/svc_*.pm\n" if $DEBUG;
586 foreach my $file ( glob("$INC/FS/svc_*.pm") ) {
588 warn "attempting to load service table info from $file\n" if $DEBUG;
589 $file =~ /\/(\w+)\.pm$/ or do {
590 warn "unrecognized file in $INC/FS/: $file\n";
595 if ( $mod =~ /^svc_[A-Z]/ or $mod =~ /^(svc_acct_pop|svc_export_machine)$/ ) {
596 warn "skipping FS::$mod" if $DEBUG;
600 eval "use FS::$mod;";
602 die "error using FS::$mod (skipping): $@\n" if $@;
605 unless ( UNIVERSAL::can("FS::$mod", 'table_info') ) {
606 warn "FS::$mod has no table_info method; skipping";
610 my $info = "FS::$mod"->table_info;
611 unless ( keys %$info ) {
612 warn "FS::$mod->table_info doesn't return info, skipping\n";
615 warn "got info from FS::$mod: $info\n" if $DEBUG;
616 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
617 warn "skipping disabled service FS::$mod" if $DEBUG;
624 tie my %svc_defs, 'Tie::IxHash',
625 map { $_ => $info{$_}->{'fields'} }
626 sort { $info{$a}->{'display_weight'} <=> $info{$b}->{'display_weight'} }
630 $svc_defs = \%svc_defs; #cache
636 Returns a list of all svc_ tables.
642 my $svc_defs = $class->_svc_defs;
643 grep { defined( dbdef->table($_) ) } keys %$svc_defs;
646 =item svc_table_fields TABLE
648 Given a table name, returns a hashref of field names. The field names
649 returned are those with additional (service-definition related) information,
650 not necessarily all database fields of the table. Pseudo-fields may also
651 be returned (i.e. svc_acct.usergroup).
653 Each value of the hashref is another hashref, which can have one or more of
658 =item label - Description of the field
660 =item def_label - Optional description of the field in the context of service definitions
662 =item type - Currently "text", "select", "checkbox", "textarea", "disabled",
663 some components specified by "select-.*.html", and a bunch more...
665 =item disable_default - This field should not allow a default value in service definitions
667 =item disable_fixed - This field should not allow a fixed value in service definitions
669 =item disable_inventory - This field should not allow inventory values in service definitions
671 =item select_list - If type is "text", this can be a listref of possible values.
673 =item select_table - An alternative to select_list, this defines a database table with the possible choices.
675 =item select_key - Used with select_table, this is the field name of keys
677 =item select_label - Used with select_table, this is the field name of labels
683 #maybe this should move and be a class method in svc_Common.pm
684 sub svc_table_fields {
685 my($class, $table) = @_;
686 my $svc_defs = $class->_svc_defs;
687 my $def = $svc_defs->{$table};
689 foreach ( grep !ref($def->{$_}), keys %$def ) {
691 #normalize the shortcut in %info hash
692 $def->{$_} = { 'label' => $def->{$_} };
694 $def->{$_}{'type'} ||= 'text';
709 Job-queue processor for web interface adds/edits
713 use Storable qw(thaw);
719 my $param = thaw(decode_base64(shift));
720 warn Dumper($param) if $DEBUG;
722 my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} })
723 if $param->{'svcpart'};
725 #unmunge cgp_accessmodes (falze laziness-ish w/edit/process/svc_acct.cgi)
726 $param->{'svc_acct__cgp_accessmodes'} ||=
728 grep { $_ !~ /^(flag|label)$/ }
729 map { /^svc_acct__cgp_accessmodes_([\w\/]+)$/ or die "no way"; $1; }
731 grep /^svc_acct__cgp_accessmodes_([\w\/]+)$/,
736 my $new = new FS::part_svc ( {
739 # } qw(svcpart svc svcdb)
740 } ( fields('part_svc'),
741 map { my $svcdb = $_;
742 my @fields = fields($svcdb);
743 push @fields, 'usergroup' if $svcdb eq 'svc_acct'
744 or $svcdb eq 'svc_broadband'; #kludge
747 my $f = $svcdb.'__'.$_;
748 my $flag = $param->{ $f.'_flag' } || ''; #silence warnings
749 if ( $flag =~ /^[MAH]$/ ) {
750 $param->{ $f } = delete( $param->{ $f.'_classnum' } );
753 or $_ eq 'usergroup' ) {
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;
855 sub _upgrade_data { #class method
856 my ($class, %opts) = @_;
858 my @part_svc_column = qsearch('part_svc_column', { 'columnname' => 'usergroup' });
859 foreach my $col ( @part_svc_column ) {
860 next if $col->columnvalue =~ /^[\d,]+$/ || !$col->columnvalue;
861 my @groupnames = split(',',$col->columnvalue);
864 foreach my $groupname ( @groupnames ) {
865 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
867 $g = new FS::radius_group {
868 'groupname' => $groupname,
869 'description' => $groupname,
872 die "Error inserting new radius_group for service definition group \"$groupname\": $error"
875 push @groupnums, $g->groupnum;
877 $col->columnvalue(join(',',@groupnums));
878 $error = $col->replace;
879 die $error if $error;
882 my @badlabels = qsearch({
883 'table' => 'part_svc_column',
885 'extra_sql' => 'WHERE columnlabel IN ('.
886 "'Descriptive label for this particular device.',".
887 "'IP address. Leave blank for automatic assignment.',".
888 "'Maximum upload speed for this service in Kbps. 0 denotes unlimited.',".
889 "'Maximum download speed for this service in Kbps. 0 denotes unlimited.')"
891 foreach my $col ( @badlabels ) {
892 $col->columnlabel('');
893 my $error = $col->replace;
894 die $error if $error;
901 Delete is unimplemented.
903 The list of svc_* tables is no longer hardcoded, but svc_acct_pop is skipped
904 as a special case until it is renamed.
906 all_part_svc_column methods should be documented
910 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
911 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
912 schema.html from the base documentation.