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'
56 =item preserve - Preserve after cancellation, empty or 'Y'
58 =item selfservice_access - Access allowed to the service via self-service:
59 empty for full access, "readonly" for read-only, "hidden" to hide it entirely
61 =item restrict_edit_password - Require the "Provision customer service" access
62 right to change the password field, rather than just "Edit password". Only
63 relevant to svc_acct for now.
73 Creates a new service definition. To add the service definition to the
74 database, see L<"insert">.
78 sub table { 'part_svc'; }
80 =item insert [ EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ]
82 Adds this service definition to the database. If there is an error, returns
83 the error, otherwise returns false.
85 The following pseudo-fields may be defined, and will be maintained in
86 the part_svc_column table appropriately (see L<FS::part_svc_column>).
90 =item I<svcdb>__I<field> - Default or fixed value for I<field> in I<svcdb>.
92 =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.
96 If you want to add part_svc_column records for fields that do not exist as
97 (real or virtual) fields in the I<svcdb> table, make sure to list then in
98 EXTRA_FIELDS_ARRAYREF also.
100 If EXPORTNUMS_HASHREF is specified (keys are exportnums and values are
101 boolean), the appopriate export_svc records will be inserted.
111 @fields = @{shift(@_)} if @_;
113 my $exportnums = shift;
114 @exportnums = grep $exportnums->{$_}, keys %$exportnums;
119 local $SIG{HUP} = 'IGNORE';
120 local $SIG{INT} = 'IGNORE';
121 local $SIG{QUIT} = 'IGNORE';
122 local $SIG{TERM} = 'IGNORE';
123 local $SIG{TSTP} = 'IGNORE';
124 local $SIG{PIPE} = 'IGNORE';
126 my $oldAutoCommit = $FS::UID::AutoCommit;
127 local $FS::UID::AutoCommit = 0;
130 my $error = $self->SUPER::insert;
132 $dbh->rollback if $oldAutoCommit;
136 # add part_svc_column records
138 my $svcdb = $self->svcdb;
139 # my @rows = map { /^${svcdb}__(.*)$/; $1 }
141 # grep /^${svcdb}__/,
142 # fields('part_svc');
144 grep { $_ ne 'svcnum'
145 && ( defined( $self->getfield($svcdb.'__'.$_.'_flag') )
146 || $self->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
147 } (fields($svcdb), @fields)
149 my $part_svc_column = $self->part_svc_column($field);
150 my $previous = qsearchs('part_svc_column', {
151 'svcpart' => $self->svcpart,
152 'columnname' => $field,
155 my $flag = $self->getfield($svcdb.'__'.$field.'_flag');
156 my $label = $self->getfield($svcdb.'__'.$field.'_label');
157 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
159 if ( uc($flag) =~ /^([A-Z])$/ ) {
160 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
162 $part_svc_column->setfield('columnflag', $1);
163 $part_svc_column->setfield('columnvalue',
164 &$parser($self->getfield($svcdb.'__'.$field))
168 $part_svc_column->setfield('columnlabel', $label)
169 if $label !~ /^\s*$/;
172 $error = $part_svc_column->replace($previous);
174 $error = $part_svc_column->insert;
178 $error = $previous ? $previous->delete : '';
181 $dbh->rollback if $oldAutoCommit;
187 # add export_svc records
188 my $slice = 100/scalar(@exportnums) if @exportnums;
190 foreach my $exportnum ( @exportnums ) {
191 my $export_svc = new FS::export_svc ( {
192 'exportnum' => $exportnum,
193 'svcpart' => $self->svcpart,
195 $error = $export_svc->insert($job, $slice*$done++, $slice);
197 $dbh->rollback if $oldAutoCommit;
202 # XXX shouldn't this update fixed values?
204 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
211 Currently unimplemented. Set the "disabled" field instead.
216 return "Can't (yet?) delete service definitions.";
217 # check & make sure the svcpart isn't in cust_svc or pkg_svc (in any packages)?
220 =item replace OLD_RECORD [ '1.3-COMPAT' [ , EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ] ]
222 Replaces OLD_RECORD with this one in the database. If there is an error,
223 returns the error, otherwise returns false.
227 TODOC: EXTRA_FIELDS_ARRAYREF (same as insert method)
234 my ( $new, $old ) = ( shift, shift );
239 if ( @_ && $_[0] eq '1.3-COMPAT' ) {
242 @fields = @{shift(@_)} if @_;
243 $exportnums = @_ ? shift : '';
246 return 'non-1.3-COMPAT interface not yet written';
250 return "Can't change svcdb for an existing service definition!"
251 unless $old->svcdb eq $new->svcdb;
253 local $SIG{HUP} = 'IGNORE';
254 local $SIG{INT} = 'IGNORE';
255 local $SIG{QUIT} = 'IGNORE';
256 local $SIG{TERM} = 'IGNORE';
257 local $SIG{TSTP} = 'IGNORE';
258 local $SIG{PIPE} = 'IGNORE';
260 my $oldAutoCommit = $FS::UID::AutoCommit;
261 local $FS::UID::AutoCommit = 0;
264 my $error = $new->SUPER::replace( $old );
266 $dbh->rollback if $oldAutoCommit;
270 if ( $compat eq '1.3' ) {
272 # maintain part_svc_column records
274 my $svcdb = $new->svcdb;
276 grep { $_ ne 'svcnum'
277 && ( defined( $new->getfield($svcdb.'__'.$_.'_flag') )
278 || $new->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
279 } (fields($svcdb),@fields)
282 my $part_svc_column = $new->part_svc_column($field);
283 my $previous = qsearchs('part_svc_column', {
284 'svcpart' => $new->svcpart,
285 'columnname' => $field,
288 my $flag = $new->getfield($svcdb.'__'.$field.'_flag');
289 my $label = $new->getfield($svcdb.'__'.$field.'_label');
291 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
293 if ( uc($flag) =~ /^([A-Z])$/ ) {
294 $part_svc_column->setfield('columnflag', $1);
295 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
297 $part_svc_column->setfield('columnvalue',
298 &$parser($new->getfield($svcdb.'__'.$field))
301 $part_svc_column->setfield('columnflag', '');
302 $part_svc_column->setfield('columnvalue', '');
305 $part_svc_column->setfield('columnlabel', $label)
306 if $label !~ /^\s*$/;
309 $error = $part_svc_column->replace($previous);
311 $error = $part_svc_column->insert;
314 $error = $previous ? $previous->delete : '';
317 $dbh->rollback if $oldAutoCommit;
322 # maintain export_svc records
326 #false laziness w/ edit/process/agent_type.cgi
327 my @new_export_svc = ();
328 foreach my $part_export ( qsearch('part_export', {}) ) {
329 my $exportnum = $part_export->exportnum;
331 'exportnum' => $exportnum,
332 'svcpart' => $new->svcpart,
334 my $export_svc = qsearchs('export_svc', $hashref);
336 if ( $export_svc && ! $exportnums->{$exportnum} ) {
337 $error = $export_svc->delete;
339 $dbh->rollback if $oldAutoCommit;
342 } elsif ( ! $export_svc && $exportnums->{$exportnum} ) {
343 push @new_export_svc, new FS::export_svc ( $hashref );
348 my $slice = 100/scalar(@new_export_svc) if @new_export_svc;
350 foreach my $export_svc (@new_export_svc) {
351 $error = $export_svc->insert($job, $slice*$done++, $slice);
353 $dbh->rollback if $oldAutoCommit;
357 $error = $job->update_statustext( int( $slice * $done ) );
359 $dbh->rollback if $oldAutoCommit;
368 $dbh->rollback if $oldAutoCommit;
369 return 'non-1.3-COMPAT interface not yet written';
373 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
380 Checks all fields to make sure this is a valid service definition. If there is
381 an error, returns the error, otherwise returns false. Called by the insert
391 $self->ut_numbern('svcpart')
392 || $self->ut_text('svc')
393 || $self->ut_alpha('svcdb')
394 || $self->ut_enum('disabled', [ '', 'Y' ] )
395 || $self->ut_enum('preserve', [ '', 'Y' ] )
396 || $self->ut_enum('selfservice_access', [ '', 'hidden', 'readonly' ] )
397 || $self->ut_enum('restrict_edit_password', [ '', 'Y' ] )
399 return $error if $error;
401 my @fields = eval { fields( $self->svcdb ) }; #might die
402 return "Unknown svcdb: ". $self->svcdb. " (Error: $@)"
408 =item part_svc_column COLUMNNAME
410 Returns the part_svc_column object (see L<FS::part_svc_column>) for the given
411 COLUMNNAME, or a new part_svc_column object if none exists.
415 sub part_svc_column {
416 my( $self, $columnname) = @_;
418 qsearchs('part_svc_column', {
419 'svcpart' => $self->svcpart,
420 'columnname' => $columnname,
422 ) or new FS::part_svc_column {
423 'svcpart' => $self->svcpart,
424 'columnname' => $columnname,
428 =item all_part_svc_column
432 sub all_part_svc_column {
434 qsearch('part_svc_column', { 'svcpart' => $self->svcpart } );
437 =item part_export [ EXPORTTYPE ]
439 Returns a list of all exports (see L<FS::part_export>) for this service, or,
440 if an export type is specified, only returns exports of the given type.
447 $search{'exporttype'} = shift if @_;
448 map { $_ } #behavior of sort undefined in scalar context
449 sort { $a->weight <=> $b->weight }
450 map { qsearchs('part_export', { 'exportnum'=>$_->exportnum, %search } ) }
451 qsearch('export_svc', { 'svcpart'=>$self->svcpart } );
454 =item part_export_usage
456 Returns a list of any exports (see L<FS::part_export>) for this service that
457 are capable of reporting usage information.
461 sub part_export_usage {
463 grep $_->can('usage_sessions'), $self->part_export;
466 =item part_export_did
468 Returns a list of any exports (see L<FS::part_export>) for this service that
469 are capable of returing available DID (phone number) information.
473 sub part_export_did {
475 grep $_->can('get_dids'), $self->part_export;
478 =item part_export_dsl_pull
480 Returns a list of any exports (see L<FS::part_export>) for this service that
481 are capable of pulling/pushing DSL orders.
485 sub part_export_dsl_pull {
487 grep $_->can('dsl_pull'), $self->part_export;
490 =item cust_svc [ PKGPART ]
492 Returns a list of associated customer services (FS::cust_svc records).
494 If a PKGPART is specified, returns the customer services which are contained
495 within packages of that type (see L<FS::part_pkg>). If PKGPARTis specified as
496 B<0>, returns unlinked customer services.
503 my $hashref = { 'svcpart' => $self->svcpart };
505 my( $addl_from, $extra_sql ) = ( '', '' );
508 if ( $pkgpart =~ /^(\d+)$/ ) {
509 $addl_from = 'LEFT JOIN cust_pkg USING ( pkgnum )';
510 $extra_sql = "AND pkgpart = $1";
511 } elsif ( $pkgpart eq '0' ) {
512 $hashref->{'pkgnum'} = '';
517 'table' => 'cust_svc',
518 'addl_from' => $addl_from,
519 'hashref' => $hashref,
520 'extra_sql' => $extra_sql,
524 =item num_cust_svc [ PKGPART ]
526 Returns the number of associated customer services (FS::cust_svc records).
528 If a PKGPART is specified, returns the number of customer services which are
529 contained within packages of that type (see L<FS::part_pkg>). If PKGPART
530 is specified as B<0>, returns the number of unlinked customer services.
537 my @param = ( $self->svcpart );
539 my( $join, $and ) = ( '', '' );
543 $join = 'LEFT JOIN cust_pkg USING ( pkgnum )';
544 $and = 'AND pkgpart = ?';
545 push @param, $pkgpart;
546 } elsif ( $pkgpart eq '0' ) {
547 $and = 'AND pkgnum IS NULL';
551 my $sth = dbh->prepare(
552 "SELECT COUNT(*) FROM cust_svc $join WHERE svcpart = ? $and"
553 ) or die dbh->errstr;
554 $sth->execute(@param)
556 $sth->fetchrow_arrayref->[0];
561 Returns a list of associated FS::svc_* records.
567 map { $_->svc_x } $self->cust_svc;
581 return $svc_defs if $svc_defs; #cache
583 my $conf = new FS::Conf;
585 #false laziness w/part_pkg.pm::plan_info
588 foreach my $INC ( @INC ) {
589 warn "globbing $INC/FS/svc_*.pm\n" if $DEBUG;
590 foreach my $file ( glob("$INC/FS/svc_*.pm") ) {
592 warn "attempting to load service table info from $file\n" if $DEBUG;
593 $file =~ /\/(\w+)\.pm$/ or do {
594 warn "unrecognized file in $INC/FS/: $file\n";
599 if ( $mod =~ /^svc_[A-Z]/ or $mod =~ /^svc_acct_pop$/ ) {
600 warn "skipping FS::$mod" if $DEBUG;
604 eval "use FS::$mod;";
606 die "error using FS::$mod (skipping): $@\n" if $@;
609 unless ( UNIVERSAL::can("FS::$mod", 'table_info') ) {
610 warn "FS::$mod has no table_info method; skipping";
614 my $info = "FS::$mod"->table_info;
615 unless ( keys %$info ) {
616 warn "FS::$mod->table_info doesn't return info, skipping\n";
619 warn "got info from FS::$mod: $info\n" if $DEBUG;
620 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
621 warn "skipping disabled service FS::$mod" if $DEBUG;
628 tie my %svc_defs, 'Tie::IxHash',
629 map { $_ => $info{$_}->{'fields'} }
630 sort { $info{$a}->{'display_weight'} <=> $info{$b}->{'display_weight'} }
634 # yuck. maybe this won't be so bad when virtual fields become real fields
636 foreach my $svcdb (grep dbdef->table($_), keys %svc_defs ) {
637 eval "use FS::$svcdb;";
638 my $self = "FS::$svcdb"->new;
639 $vfields{$svcdb} = {};
640 foreach my $field ($self->virtual_fields) { # svc_Common::virtual_fields with a null svcpart returns all of them
641 my $pvf = $self->pvf($field);
642 my @list = $pvf->list;
644 $svc_defs{$svcdb}->{$field} = { desc => $pvf->label,
646 select_list => \@list };
648 $svc_defs{$svcdb}->{$field} = $pvf->label;
650 $vfields{$svcdb}->{$field} = $pvf;
651 warn "\$vfields{$svcdb}->{$field} = $pvf"
656 $svc_defs = \%svc_defs; #cache
662 Returns a list of all svc_ tables.
668 my $svc_defs = $class->_svc_defs;
669 grep { defined( dbdef->table($_) ) } keys %$svc_defs;
672 =item svc_table_fields TABLE
674 Given a table name, returns a hashref of field names. The field names
675 returned are those with additional (service-definition related) information,
676 not necessarily all database fields of the table. Pseudo-fields may also
677 be returned (i.e. svc_acct.usergroup).
679 Each value of the hashref is another hashref, which can have one or more of
684 =item label - Description of the field
686 =item def_label - Optional description of the field in the context of service definitions
688 =item type - Currently "text", "select", "checkbox", "textarea", "disabled",
689 some components specified by "select-.*.html", and a bunch more...
691 =item disable_default - This field should not allow a default value in service definitions
693 =item disable_fixed - This field should not allow a fixed value in service definitions
695 =item disable_inventory - This field should not allow inventory values in service definitions
697 =item select_list - If type is "text", this can be a listref of possible values.
699 =item select_table - An alternative to select_list, this defines a database table with the possible choices.
701 =item select_key - Used with select_table, this is the field name of keys
703 =item select_label - Used with select_table, this is the field name of labels
709 #maybe this should move and be a class method in svc_Common.pm
710 sub svc_table_fields {
711 my($class, $table) = @_;
712 my $svc_defs = $class->_svc_defs;
713 my $def = $svc_defs->{$table};
715 foreach ( grep !ref($def->{$_}), keys %$def ) {
717 #normalize the shortcut in %info hash
718 $def->{$_} = { 'label' => $def->{$_} };
720 $def->{$_}{'type'} ||= 'text';
735 Job-queue processor for web interface adds/edits
739 use Storable qw(thaw);
745 my $param = thaw(decode_base64(shift));
746 warn Dumper($param) if $DEBUG;
748 my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} })
749 if $param->{'svcpart'};
751 #unmunge cgp_accessmodes (falze laziness-ish w/edit/process/svc_acct.cgi)
752 $param->{'svc_acct__cgp_accessmodes'} ||=
754 grep { $_ !~ /^(flag|label)$/ }
755 map { /^svc_acct__cgp_accessmodes_([\w\/]+)$/ or die "no way"; $1; }
757 grep /^svc_acct__cgp_accessmodes_([\w\/]+)$/,
762 my $new = new FS::part_svc ( {
765 # } qw(svcpart svc svcdb)
766 } ( fields('part_svc'),
767 map { my $svcdb = $_;
768 my @fields = fields($svcdb);
769 push @fields, 'usergroup' if $svcdb eq 'svc_acct'
770 or $svcdb eq 'svc_broadband'; #kludge
773 my $f = $svcdb.'__'.$_;
774 my $flag = $param->{ $f.'_flag' } || ''; #silence warnings
775 if ( $flag =~ /^[MAH]$/ ) {
776 $param->{ $f } = delete( $param->{ $f.'_classnum' } );
779 or $_ eq 'usergroup' ) {
780 $param->{ $f } = ref($param->{ $f })
781 ? join(',', @{$param->{ $f }} )
784 ( $f, $f.'_flag', $f.'_label' );
788 } FS::part_svc->svc_tables()
793 map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
794 qsearch('part_export', {} );
797 if ( $param->{'svcpart'} ) {
798 $error = $new->replace( $old,
799 '1.3-COMPAT', #totally bunk, as jeff noted
805 $error = $new->insert( [ 'usergroup' ],
809 $param->{'svcpart'} = $new->getfield('svcpart');
812 die "$error\n" if $error;
815 =item process_bulk_cust_svc
817 Job-queue processor for web interface bulk customer service changes
821 use Storable qw(thaw);
824 sub process_bulk_cust_svc {
827 my $param = thaw(decode_base64(shift));
828 warn Dumper($param) if $DEBUG;
830 local($FS::svc_Common::noexport_hack) = 1
831 if $param->{'noexport'};
834 qsearchs('part_svc', { 'svcpart' => $param->{'old_svcpart'} } );
836 die "Must select a new service definition\n" unless $param->{'new_svcpart'};
838 #the rest should be abstracted out to to its own subroutine?
840 local $SIG{HUP} = 'IGNORE';
841 local $SIG{INT} = 'IGNORE';
842 local $SIG{QUIT} = 'IGNORE';
843 local $SIG{TERM} = 'IGNORE';
844 local $SIG{TSTP} = 'IGNORE';
845 local $SIG{PIPE} = 'IGNORE';
847 my $oldAutoCommit = $FS::UID::AutoCommit;
848 local $FS::UID::AutoCommit = 0;
851 local( $FS::cust_svc::ignore_quantity ) = 1;
853 my $total = $old_part_svc->num_cust_svc( $param->{'pkgpart'} );
856 foreach my $old_cust_svc ( $old_part_svc->cust_svc( $param->{'pkgpart'} ) ) {
858 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
860 $new_cust_svc->svcpart( $param->{'new_svcpart'} );
861 my $error = $new_cust_svc->replace($old_cust_svc);
863 $dbh->rollback if $oldAutoCommit;
864 die "$error\n" if $error;
867 $error = $job->update_statustext( int( 100 * ++$n / $total ) );
869 $dbh->rollback if $oldAutoCommit;
870 die $error if $error;
875 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
881 sub _upgrade_data { #class method
882 my ($class, %opts) = @_;
884 my @part_svc_column = qsearch('part_svc_column', { 'columnname' => 'usergroup' });
885 foreach my $col ( @part_svc_column ) {
886 next if $col->columnvalue =~ /^[\d,]+$/ || !$col->columnvalue;
887 my @groupnames = split(',',$col->columnvalue);
890 foreach my $groupname ( @groupnames ) {
891 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
893 $g = new FS::radius_group {
894 'groupname' => $groupname,
895 'description' => $groupname,
898 die "Error inserting new radius_group for service definition group \"$groupname\": $error"
901 push @groupnums, $g->groupnum;
903 $col->columnvalue(join(',',@groupnums));
904 $error = $col->replace;
905 die $error if $error;
908 my @badlabels = qsearch({
909 'table' => 'part_svc_column',
911 'extra_sql' => 'WHERE columnlabel IN ('.
912 "'Descriptive label for this particular device.',".
913 "'IP address. Leave blank for automatic assignment.',".
914 "'Maximum upload speed for this service in Kbps. 0 denotes unlimited.',".
915 "'Maximum download speed for this service in Kbps. 0 denotes unlimited.')"
917 foreach my $col ( @badlabels ) {
918 $col->columnlabel('');
919 my $error = $col->replace;
920 die $error if $error;
927 Delete is unimplemented.
929 The list of svc_* tables is no longer hardcoded, but svc_acct_pop is skipped
930 as a special case until it is renamed.
932 all_part_svc_column methods should be documented
936 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
937 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
938 schema.html from the base documentation.