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 sort { $a->weight <=> $b->weight }
445 map { qsearchs('part_export', { 'exportnum' => $_->exportnum, %search } ) }
446 qsearch('export_svc', { 'svcpart' => $self->svcpart } );
449 =item part_export_usage
451 Returns a list of any exports (see L<FS::part_export>) for this service that
452 are capable of reporting usage information.
456 sub part_export_usage {
458 grep $_->can('usage_sessions'), $self->part_export;
461 =item part_export_did
463 Returns a list of any exports (see L<FS::part_export>) for this service that
464 are capable of returing available DID (phone number) information.
468 sub part_export_did {
470 grep $_->can('get_dids'), $self->part_export;
473 =item part_export_dsl_pull
475 Returns a list of any exports (see L<FS::part_export>) for this service that
476 are capable of pulling/pushing DSL orders.
480 sub part_export_dsl_pull {
482 grep $_->can('dsl_pull'), $self->part_export;
485 =item cust_svc [ PKGPART ]
487 Returns a list of associated customer services (FS::cust_svc records).
489 If a PKGPART is specified, returns the customer services which are contained
490 within packages of that type (see L<FS::part_pkg>). If PKGPARTis specified as
491 B<0>, returns unlinked customer services.
498 my $hashref = { 'svcpart' => $self->svcpart };
500 my( $addl_from, $extra_sql ) = ( '', '' );
503 if ( $pkgpart =~ /^(\d+)$/ ) {
504 $addl_from = 'LEFT JOIN cust_pkg USING ( pkgnum )';
505 $extra_sql = "AND pkgpart = $1";
506 } elsif ( $pkgpart eq '0' ) {
507 $hashref->{'pkgnum'} = '';
512 'table' => 'cust_svc',
513 'addl_from' => $addl_from,
514 'hashref' => $hashref,
515 'extra_sql' => $extra_sql,
519 =item num_cust_svc [ PKGPART ]
521 Returns the number of associated customer services (FS::cust_svc records).
523 If a PKGPART is specified, returns the number of customer services which are
524 contained within packages of that type (see L<FS::part_pkg>). If PKGPART
525 is specified as B<0>, returns the number of unlinked customer services.
532 my @param = ( $self->svcpart );
534 my( $join, $and ) = ( '', '' );
538 $join = 'LEFT JOIN cust_pkg USING ( pkgnum )';
539 $and = 'AND pkgpart = ?';
540 push @param, $pkgpart;
541 } elsif ( $pkgpart eq '0' ) {
542 $and = 'AND pkgnum IS NULL';
546 my $sth = dbh->prepare(
547 "SELECT COUNT(*) FROM cust_svc $join WHERE svcpart = ? $and"
548 ) or die dbh->errstr;
549 $sth->execute(@param)
551 $sth->fetchrow_arrayref->[0];
556 Returns a list of associated FS::svc_* records.
562 map { $_->svc_x } $self->cust_svc;
576 return $svc_defs if $svc_defs; #cache
578 my $conf = new FS::Conf;
580 #false laziness w/part_pkg.pm::plan_info
583 foreach my $INC ( @INC ) {
584 warn "globbing $INC/FS/svc_*.pm\n" if $DEBUG;
585 foreach my $file ( glob("$INC/FS/svc_*.pm") ) {
587 warn "attempting to load service table info from $file\n" if $DEBUG;
588 $file =~ /\/(\w+)\.pm$/ or do {
589 warn "unrecognized file in $INC/FS/: $file\n";
594 if ( $mod =~ /^svc_[A-Z]/ or $mod =~ /^svc_acct_pop$/ ) {
595 warn "skipping FS::$mod" if $DEBUG;
599 eval "use FS::$mod;";
601 die "error using FS::$mod (skipping): $@\n" if $@;
604 unless ( UNIVERSAL::can("FS::$mod", 'table_info') ) {
605 warn "FS::$mod has no table_info method; skipping";
609 my $info = "FS::$mod"->table_info;
610 unless ( keys %$info ) {
611 warn "FS::$mod->table_info doesn't return info, skipping\n";
614 warn "got info from FS::$mod: $info\n" if $DEBUG;
615 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
616 warn "skipping disabled service FS::$mod" if $DEBUG;
623 tie my %svc_defs, 'Tie::IxHash',
624 map { $_ => $info{$_}->{'fields'} }
625 sort { $info{$a}->{'display_weight'} <=> $info{$b}->{'display_weight'} }
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", "checkbox", "textarea", "disabled",
662 some components specified by "select-.*.html", and a bunch more...
664 =item disable_default - This field should not allow a default value in service definitions
666 =item disable_fixed - This field should not allow a fixed value in service definitions
668 =item disable_inventory - This field should not allow inventory values in service definitions
670 =item select_list - If type is "text", this can be a listref of possible values.
672 =item select_table - An alternative to select_list, this defines a database table with the possible choices.
674 =item select_key - Used with select_table, this is the field name of keys
676 =item select_label - Used with select_table, this is the field name of labels
682 #maybe this should move and be a class method in svc_Common.pm
683 sub svc_table_fields {
684 my($class, $table) = @_;
685 my $svc_defs = $class->_svc_defs;
686 my $def = $svc_defs->{$table};
688 foreach ( grep !ref($def->{$_}), keys %$def ) {
690 #normalize the shortcut in %info hash
691 $def->{$_} = { 'label' => $def->{$_} };
693 $def->{$_}{'type'} ||= 'text';
708 Job-queue processor for web interface adds/edits
712 use Storable qw(thaw);
718 my $param = thaw(decode_base64(shift));
719 warn Dumper($param) if $DEBUG;
721 my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} })
722 if $param->{'svcpart'};
724 #unmunge cgp_accessmodes (falze laziness-ish w/edit/process/svc_acct.cgi)
725 $param->{'svc_acct__cgp_accessmodes'} ||=
727 grep { $_ !~ /^(flag|label)$/ }
728 map { /^svc_acct__cgp_accessmodes_([\w\/]+)$/ or die "no way"; $1; }
730 grep /^svc_acct__cgp_accessmodes_([\w\/]+)$/,
735 my $new = new FS::part_svc ( {
738 # } qw(svcpart svc svcdb)
739 } ( fields('part_svc'),
740 map { my $svcdb = $_;
741 my @fields = fields($svcdb);
742 push @fields, 'usergroup' if $svcdb eq 'svc_acct'
743 or $svcdb eq 'svc_broadband'; #kludge
746 my $f = $svcdb.'__'.$_;
747 my $flag = $param->{ $f.'_flag' } || ''; #silence warnings
748 if ( $flag =~ /^[MAH]$/ ) {
749 $param->{ $f } = delete( $param->{ $f.'_classnum' } );
752 or $_ eq 'usergroup' ) {
753 $param->{ $f } = ref($param->{ $f })
754 ? join(',', @{$param->{ $f }} )
757 ( $f, $f.'_flag', $f.'_label' );
761 } FS::part_svc->svc_tables()
766 map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
767 qsearch('part_export', {} );
770 if ( $param->{'svcpart'} ) {
771 $error = $new->replace( $old,
772 '1.3-COMPAT', #totally bunk, as jeff noted
778 $error = $new->insert( [ 'usergroup' ],
782 $param->{'svcpart'} = $new->getfield('svcpart');
785 die "$error\n" if $error;
788 =item process_bulk_cust_svc
790 Job-queue processor for web interface bulk customer service changes
794 use Storable qw(thaw);
797 sub process_bulk_cust_svc {
800 my $param = thaw(decode_base64(shift));
801 warn Dumper($param) if $DEBUG;
803 local($FS::svc_Common::noexport_hack) = 1
804 if $param->{'noexport'};
807 qsearchs('part_svc', { 'svcpart' => $param->{'old_svcpart'} } );
809 die "Must select a new service definition\n" unless $param->{'new_svcpart'};
811 #the rest should be abstracted out to to its own subroutine?
813 local $SIG{HUP} = 'IGNORE';
814 local $SIG{INT} = 'IGNORE';
815 local $SIG{QUIT} = 'IGNORE';
816 local $SIG{TERM} = 'IGNORE';
817 local $SIG{TSTP} = 'IGNORE';
818 local $SIG{PIPE} = 'IGNORE';
820 my $oldAutoCommit = $FS::UID::AutoCommit;
821 local $FS::UID::AutoCommit = 0;
824 local( $FS::cust_svc::ignore_quantity ) = 1;
826 my $total = $old_part_svc->num_cust_svc( $param->{'pkgpart'} );
829 foreach my $old_cust_svc ( $old_part_svc->cust_svc( $param->{'pkgpart'} ) ) {
831 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
833 $new_cust_svc->svcpart( $param->{'new_svcpart'} );
834 my $error = $new_cust_svc->replace($old_cust_svc);
836 $dbh->rollback if $oldAutoCommit;
837 die "$error\n" if $error;
840 $error = $job->update_statustext( int( 100 * ++$n / $total ) );
842 $dbh->rollback if $oldAutoCommit;
843 die $error if $error;
848 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
854 sub _upgrade_data { #class method
855 my ($class, %opts) = @_;
857 my @part_svc_column = qsearch('part_svc_column', { 'columnname' => 'usergroup' });
858 foreach my $col ( @part_svc_column ) {
859 next if $col->columnvalue =~ /^[\d,]+$/ || !$col->columnvalue;
860 my @groupnames = split(',',$col->columnvalue);
863 foreach my $groupname ( @groupnames ) {
864 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
866 $g = new FS::radius_group {
867 'groupname' => $groupname,
868 'description' => $groupname,
871 die "Error inserting new radius_group for service definition group \"$groupname\": $error"
874 push @groupnums, $g->groupnum;
876 $col->columnvalue(join(',',@groupnums));
877 $error = $col->replace;
878 die $error if $error;
881 my @badlabels = qsearch({
882 'table' => 'part_svc_column',
884 'extra_sql' => 'WHERE columnlabel IN ('.
885 "'Descriptive label for this particular device.',".
886 "'IP address. Leave blank for automatic assignment.',".
887 "'Maximum upload speed for this service in Kbps. 0 denotes unlimited.',".
888 "'Maximum download speed for this service in Kbps. 0 denotes unlimited.')"
890 foreach my $col ( @badlabels ) {
891 $col->columnlabel('');
892 my $error = $col->replace;
893 die $error if $error;
900 Delete is unimplemented.
902 The list of svc_* tables is no longer hardcoded, but svc_acct_pop is skipped
903 as a special case until it is renamed.
905 all_part_svc_column methods should be documented
909 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
910 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
911 schema.html from the base documentation.