2 use base qw(FS::Record);
7 use FS::Record qw( qsearch qsearchs fields dbh );
8 use FS::Schema qw( dbdef );
9 use FS::part_svc_column;
13 use FS::part_svc_class;
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 classnum - Optional service class (see L<FS::svc_class>)
56 =item disabled - Disabled flag, empty or `Y'
58 =item preserve - Preserve after cancellation, empty or 'Y'
60 =item selfservice_access - Access allowed to the service via self-service:
61 empty for full access, "readonly" for read-only, "hidden" to hide it entirely
63 =item restrict_edit_password - Require the "Provision customer service" access
64 right to change the password field, rather than just "Edit password". Only
65 relevant to svc_acct for now.
67 =item has_router - Allow the service to have an L<FS::router> connected
68 through it. Probably only relevant to svc_broadband, svc_acct, and svc_dsl
79 Creates a new service definition. To add the service definition to the
80 database, see L<"insert">.
84 sub table { 'part_svc'; }
86 =item insert [ EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ]
88 Adds this service definition to the database. If there is an error, returns
89 the error, otherwise returns false.
91 The following pseudo-fields may be defined, and will be maintained in
92 the part_svc_column table appropriately (see L<FS::part_svc_column>).
96 =item I<svcdb>__I<field> - Default or fixed value for I<field> in I<svcdb>.
98 =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.
102 If you want to add part_svc_column records for fields that do not exist as
103 fields in the I<svcdb> table, make sure to list then in
104 EXTRA_FIELDS_ARRAYREF also.
106 If EXPORTNUMS_HASHREF is specified (keys are exportnums and values are
107 boolean), the appopriate export_svc records will be inserted.
117 @fields = @{shift(@_)} if @_;
119 my $exportnums = shift;
120 @exportnums = grep $exportnums->{$_}, keys %$exportnums;
125 local $SIG{HUP} = 'IGNORE';
126 local $SIG{INT} = 'IGNORE';
127 local $SIG{QUIT} = 'IGNORE';
128 local $SIG{TERM} = 'IGNORE';
129 local $SIG{TSTP} = 'IGNORE';
130 local $SIG{PIPE} = 'IGNORE';
132 my $oldAutoCommit = $FS::UID::AutoCommit;
133 local $FS::UID::AutoCommit = 0;
136 my $error = $self->SUPER::insert;
138 $dbh->rollback if $oldAutoCommit;
142 # add part_svc_column records
144 my $svcdb = $self->svcdb;
145 # my @rows = map { /^${svcdb}__(.*)$/; $1 }
147 # grep /^${svcdb}__/,
148 # fields('part_svc');
150 grep { $_ ne 'svcnum'
151 && ( defined( $self->getfield($svcdb.'__'.$_.'_flag') )
152 || $self->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
153 } (fields($svcdb), @fields)
155 my $part_svc_column = $self->part_svc_column($field);
156 my $previous = qsearchs('part_svc_column', {
157 'svcpart' => $self->svcpart,
158 'columnname' => $field,
161 my $flag = $self->getfield($svcdb.'__'.$field.'_flag');
162 my $label = $self->getfield($svcdb.'__'.$field.'_label');
163 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
165 if ( uc($flag) =~ /^([A-Z])$/ ) {
166 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
168 $part_svc_column->setfield('columnflag', $1);
169 $part_svc_column->setfield('columnvalue',
170 &$parser($self->getfield($svcdb.'__'.$field))
174 $part_svc_column->setfield('columnlabel', $label)
175 if $label !~ /^\s*$/;
178 $error = $part_svc_column->replace($previous);
180 $error = $part_svc_column->insert;
184 $error = $previous ? $previous->delete : '';
187 $dbh->rollback if $oldAutoCommit;
193 # add export_svc records
194 my $slice = 100/scalar(@exportnums) if @exportnums;
196 foreach my $exportnum ( @exportnums ) {
197 my $export_svc = new FS::export_svc ( {
198 'exportnum' => $exportnum,
199 'svcpart' => $self->svcpart,
201 $error = $export_svc->insert($job, $slice*$done++, $slice);
203 $dbh->rollback if $oldAutoCommit;
208 # XXX shouldn't this update fixed values?
210 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
217 Currently unimplemented. Set the "disabled" field instead.
222 return "Can't (yet?) delete service definitions.";
223 # check & make sure the svcpart isn't in cust_svc or pkg_svc (in any packages)?
226 =item replace OLD_RECORD [ '1.3-COMPAT' [ , EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ] ]
228 Replaces OLD_RECORD with this one in the database. If there is an error,
229 returns the error, otherwise returns false.
233 TODOC: EXTRA_FIELDS_ARRAYREF (same as insert method)
240 my ( $new, $old ) = ( shift, shift );
245 if ( @_ && $_[0] eq '1.3-COMPAT' ) {
248 @fields = @{shift(@_)} if @_;
249 $exportnums = @_ ? shift : '';
252 return 'non-1.3-COMPAT interface not yet written';
256 return "Can't change svcdb for an existing service definition!"
257 unless $old->svcdb eq $new->svcdb;
259 local $SIG{HUP} = 'IGNORE';
260 local $SIG{INT} = 'IGNORE';
261 local $SIG{QUIT} = 'IGNORE';
262 local $SIG{TERM} = 'IGNORE';
263 local $SIG{TSTP} = 'IGNORE';
264 local $SIG{PIPE} = 'IGNORE';
266 my $oldAutoCommit = $FS::UID::AutoCommit;
267 local $FS::UID::AutoCommit = 0;
270 my $error = $new->SUPER::replace( $old );
272 $dbh->rollback if $oldAutoCommit;
276 if ( $compat eq '1.3' ) {
278 # maintain part_svc_column records
280 my $svcdb = $new->svcdb;
282 grep { $_ ne 'svcnum'
283 && ( defined( $new->getfield($svcdb.'__'.$_.'_flag') )
284 || $new->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
285 } (fields($svcdb),@fields)
288 my $part_svc_column = $new->part_svc_column($field);
289 my $previous = qsearchs('part_svc_column', {
290 'svcpart' => $new->svcpart,
291 'columnname' => $field,
294 my $flag = $new->getfield($svcdb.'__'.$field.'_flag');
295 my $label = $new->getfield($svcdb.'__'.$field.'_label');
297 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
299 if ( uc($flag) =~ /^([A-Z])$/ ) {
300 $part_svc_column->setfield('columnflag', $1);
301 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
303 $part_svc_column->setfield('columnvalue',
304 &$parser($new->getfield($svcdb.'__'.$field))
307 $part_svc_column->setfield('columnflag', '');
308 $part_svc_column->setfield('columnvalue', '');
311 $part_svc_column->setfield('columnlabel', $label)
312 if $label !~ /^\s*$/;
315 $error = $part_svc_column->replace($previous);
317 $error = $part_svc_column->insert;
320 $error = $previous ? $previous->delete : '';
323 $dbh->rollback if $oldAutoCommit;
328 # maintain export_svc records
332 #false laziness w/ edit/process/agent_type.cgi
333 my @new_export_svc = ();
334 foreach my $part_export ( qsearch('part_export', {}) ) {
335 my $exportnum = $part_export->exportnum;
337 'exportnum' => $exportnum,
338 'svcpart' => $new->svcpart,
340 my $export_svc = qsearchs('export_svc', $hashref);
342 if ( $export_svc && ! $exportnums->{$exportnum} ) {
343 $error = $export_svc->delete;
345 $dbh->rollback if $oldAutoCommit;
348 } elsif ( ! $export_svc && $exportnums->{$exportnum} ) {
349 push @new_export_svc, new FS::export_svc ( $hashref );
354 my $slice = 100/scalar(@new_export_svc) if @new_export_svc;
356 foreach my $export_svc (@new_export_svc) {
357 $error = $export_svc->insert($job, $slice*$done++, $slice);
359 $dbh->rollback if $oldAutoCommit;
363 $error = $job->update_statustext( int( $slice * $done ) );
365 $dbh->rollback if $oldAutoCommit;
374 $dbh->rollback if $oldAutoCommit;
375 return 'non-1.3-COMPAT interface not yet written';
379 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
386 Checks all fields to make sure this is a valid service definition. If there is
387 an error, returns the error, otherwise returns false. Called by the insert
397 $self->ut_numbern('svcpart')
398 || $self->ut_text('svc')
399 || $self->ut_alpha('svcdb')
400 || $self->ut_flag('disabled')
401 || $self->ut_flag('preserve')
402 || $self->ut_enum('selfservice_access', [ '', 'hidden', 'readonly' ] )
403 || $self->ut_foreign_keyn('classnum', 'part_svc_class', 'classnum' )
404 || $self->ut_flag('restrict_edit_password')
405 || $self->ut_flag('has_router')
407 return $error if $error;
409 my @fields = eval { fields( $self->svcdb ) }; #might die
410 return "Unknown svcdb: ". $self->svcdb. " (Error: $@)"
416 =item part_svc_column COLUMNNAME
418 Returns the part_svc_column object (see L<FS::part_svc_column>) for the given
419 COLUMNNAME, or a new part_svc_column object if none exists.
423 sub part_svc_column {
424 my( $self, $columnname) = @_;
426 qsearchs('part_svc_column', {
427 'svcpart' => $self->svcpart,
428 'columnname' => $columnname,
430 ) or new FS::part_svc_column {
431 'svcpart' => $self->svcpart,
432 'columnname' => $columnname,
436 =item all_part_svc_column
440 sub all_part_svc_column {
442 qsearch('part_svc_column', { 'svcpart' => $self->svcpart } );
445 =item part_export [ EXPORTTYPE ]
447 Returns a list of all exports (see L<FS::part_export>) for this service, or,
448 if an export type is specified, only returns exports of the given type.
455 $search{'exporttype'} = shift if @_;
456 map { $_ } #behavior of sort undefined in scalar context
457 sort { $a->weight <=> $b->weight }
458 map { qsearchs('part_export', { 'exportnum'=>$_->exportnum, %search } ) }
459 qsearch('export_svc', { 'svcpart'=>$self->svcpart } );
462 =item part_export_usage
464 Returns a list of any exports (see L<FS::part_export>) for this service that
465 are capable of reporting usage information.
469 sub part_export_usage {
471 grep $_->can('usage_sessions'), $self->part_export;
474 =item part_export_did
476 Returns a list of any exports (see L<FS::part_export>) for this service that
477 are capable of returing available DID (phone number) information.
481 sub part_export_did {
483 grep $_->can('get_dids'), $self->part_export;
486 =item part_export_dsl_pull
488 Returns a list of any exports (see L<FS::part_export>) for this service that
489 are capable of pulling/pushing DSL orders.
493 sub part_export_dsl_pull {
495 grep $_->can('dsl_pull'), $self->part_export;
498 =item cust_svc [ PKGPART ]
500 Returns a list of associated customer services (FS::cust_svc records).
502 If a PKGPART is specified, returns the customer services which are contained
503 within packages of that type (see L<FS::part_pkg>). If PKGPARTis specified as
504 B<0>, returns unlinked customer services.
511 my $hashref = { 'svcpart' => $self->svcpart };
513 my( $addl_from, $extra_sql ) = ( '', '' );
516 if ( $pkgpart =~ /^(\d+)$/ ) {
517 $addl_from = 'LEFT JOIN cust_pkg USING ( pkgnum )';
518 $extra_sql = "AND pkgpart = $1";
519 } elsif ( $pkgpart eq '0' ) {
520 $hashref->{'pkgnum'} = '';
525 'table' => 'cust_svc',
526 'addl_from' => $addl_from,
527 'hashref' => $hashref,
528 'extra_sql' => $extra_sql,
532 =item num_cust_svc [ PKGPART ]
534 Returns the number of associated customer services (FS::cust_svc records).
536 If a PKGPART is specified, returns the number of customer services which are
537 contained within packages of that type (see L<FS::part_pkg>). If PKGPART
538 is specified as B<0>, returns the number of unlinked customer services.
545 my @param = ( $self->svcpart );
547 my( $join, $and ) = ( '', '' );
551 $join = 'LEFT JOIN cust_pkg USING ( pkgnum )';
552 $and = 'AND pkgpart = ?';
553 push @param, $pkgpart;
554 } elsif ( $pkgpart eq '0' ) {
555 $and = 'AND pkgnum IS NULL';
559 my $sth = dbh->prepare(
560 "SELECT COUNT(*) FROM cust_svc $join WHERE svcpart = ? $and"
561 ) or die dbh->errstr;
562 $sth->execute(@param)
564 $sth->fetchrow_arrayref->[0];
569 Returns a list of associated FS::svc_* records.
575 map { $_->svc_x } $self->cust_svc;
589 return $svc_defs if $svc_defs; #cache
591 my $conf = new FS::Conf;
593 #false laziness w/part_pkg.pm::plan_info
596 foreach my $INC ( @INC ) {
597 warn "globbing $INC/FS/svc_*.pm\n" if $DEBUG;
598 foreach my $file ( glob("$INC/FS/svc_*.pm") ) {
600 warn "attempting to load service table info from $file\n" if $DEBUG;
601 $file =~ /\/(\w+)\.pm$/ or do {
602 warn "unrecognized file in $INC/FS/: $file\n";
607 if ( $mod =~ /^svc_[A-Z]/ or $mod =~ /^(svc_acct_pop|svc_export_machine)$/ ) {
608 warn "skipping FS::$mod" if $DEBUG;
612 eval "use FS::$mod;";
614 die "error using FS::$mod (skipping): $@\n" if $@;
617 unless ( UNIVERSAL::can("FS::$mod", 'table_info') ) {
618 warn "FS::$mod has no table_info method; skipping";
622 my $info = "FS::$mod"->table_info;
623 unless ( keys %$info ) {
624 warn "FS::$mod->table_info doesn't return info, skipping\n";
627 warn "got info from FS::$mod: $info\n" if $DEBUG;
628 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
629 warn "skipping disabled service FS::$mod" if $DEBUG;
636 tie my %svc_defs, 'Tie::IxHash',
637 map { $_ => $info{$_}->{'fields'} }
638 sort { $info{$a}->{'display_weight'} <=> $info{$b}->{'display_weight'} }
642 $svc_defs = \%svc_defs; #cache
648 Returns a list of all svc_ tables.
654 my $svc_defs = $class->_svc_defs;
655 grep { defined( dbdef->table($_) ) } keys %$svc_defs;
658 =item svc_table_fields TABLE
660 Given a table name, returns a hashref of field names. The field names
661 returned are those with additional (service-definition related) information,
662 not necessarily all database fields of the table. Pseudo-fields may also
663 be returned (i.e. svc_acct.usergroup).
665 Each value of the hashref is another hashref, which can have one or more of
670 =item label - Description of the field
672 =item def_label - Optional description of the field in the context of service definitions
674 =item type - Currently "text", "select", "checkbox", "textarea", "disabled",
675 some components specified by "select-.*.html", and a bunch more...
677 =item disable_default - This field should not allow a default value in service definitions
679 =item disable_fixed - This field should not allow a fixed value in service definitions
681 =item disable_inventory - This field should not allow inventory values in service definitions
683 =item select_list - If type is "text", this can be a listref of possible values.
685 =item select_table - An alternative to select_list, this defines a database table with the possible choices.
687 =item select_key - Used with select_table, this is the field name of keys
689 =item select_label - Used with select_table, this is the field name of labels
695 #maybe this should move and be a class method in svc_Common.pm
696 sub svc_table_fields {
697 my($class, $table) = @_;
698 my $svc_defs = $class->_svc_defs;
699 my $def = $svc_defs->{$table};
701 foreach ( grep !ref($def->{$_}), keys %$def ) {
703 #normalize the shortcut in %info hash
704 $def->{$_} = { 'label' => $def->{$_} };
706 $def->{$_}{'type'} ||= 'text';
721 Job-queue processor for web interface adds/edits
725 use Storable qw(thaw);
731 my $param = thaw(decode_base64(shift));
732 warn Dumper($param) if $DEBUG;
734 my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} })
735 if $param->{'svcpart'};
737 #unmunge cgp_accessmodes (falze laziness-ish w/edit/process/svc_acct.cgi)
738 $param->{'svc_acct__cgp_accessmodes'} ||=
740 grep { $_ !~ /^(flag|label)$/ }
741 map { /^svc_acct__cgp_accessmodes_([\w\/]+)$/ or die "no way"; $1; }
743 grep /^svc_acct__cgp_accessmodes_([\w\/]+)$/,
748 my $new = new FS::part_svc ( {
751 # } qw(svcpart svc svcdb)
752 } ( fields('part_svc'),
753 map { my $svcdb = $_;
754 my @fields = fields($svcdb);
755 push @fields, 'usergroup' if $svcdb eq 'svc_acct'
756 or $svcdb eq 'svc_broadband'; #kludge
759 my $f = $svcdb.'__'.$_;
760 my $flag = $param->{ $f.'_flag' } || ''; #silence warnings
761 if ( $flag =~ /^[MAH]$/ ) {
762 $param->{ $f } = delete( $param->{ $f.'_classnum' } );
764 if ( ( $flag =~ /^[MAHS]$/ or $_ eq 'usergroup' )
765 and ref($param->{ $f }) ) {
766 $param->{ $f } = join(',', @{ $param->{ $f } });
768 ( $f, $f.'_flag', $f.'_label' );
772 } FS::part_svc->svc_tables()
777 map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
778 qsearch('part_export', {} );
781 if ( $param->{'svcpart'} ) {
782 $error = $new->replace( $old,
783 '1.3-COMPAT', #totally bunk, as jeff noted
789 $error = $new->insert( [ 'usergroup' ],
793 $param->{'svcpart'} = $new->getfield('svcpart');
796 die "$error\n" if $error;
799 =item process_bulk_cust_svc
801 Job-queue processor for web interface bulk customer service changes
805 use Storable qw(thaw);
808 sub process_bulk_cust_svc {
811 my $param = thaw(decode_base64(shift));
812 warn Dumper($param) if $DEBUG;
814 local($FS::svc_Common::noexport_hack) = 1
815 if $param->{'noexport'};
818 qsearchs('part_svc', { 'svcpart' => $param->{'old_svcpart'} } );
820 die "Must select a new service definition\n" unless $param->{'new_svcpart'};
822 #the rest should be abstracted out to to its own subroutine?
824 local $SIG{HUP} = 'IGNORE';
825 local $SIG{INT} = 'IGNORE';
826 local $SIG{QUIT} = 'IGNORE';
827 local $SIG{TERM} = 'IGNORE';
828 local $SIG{TSTP} = 'IGNORE';
829 local $SIG{PIPE} = 'IGNORE';
831 my $oldAutoCommit = $FS::UID::AutoCommit;
832 local $FS::UID::AutoCommit = 0;
835 local( $FS::cust_svc::ignore_quantity ) = 1;
837 my $total = $old_part_svc->num_cust_svc( $param->{'pkgpart'} );
840 foreach my $old_cust_svc ( $old_part_svc->cust_svc( $param->{'pkgpart'} ) ) {
842 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
844 $new_cust_svc->svcpart( $param->{'new_svcpart'} );
845 my $error = $new_cust_svc->replace($old_cust_svc);
847 $dbh->rollback if $oldAutoCommit;
848 die "$error\n" if $error;
851 $error = $job->update_statustext( int( 100 * ++$n / $total ) );
853 $dbh->rollback if $oldAutoCommit;
854 die $error if $error;
859 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
865 sub _upgrade_data { #class method
866 my ($class, %opts) = @_;
868 my @part_svc_column = qsearch('part_svc_column', { 'columnname' => 'usergroup' });
869 foreach my $col ( @part_svc_column ) {
870 next if $col->columnvalue =~ /^[\d,]+$/ || !$col->columnvalue;
871 my @groupnames = split(',',$col->columnvalue);
874 foreach my $groupname ( @groupnames ) {
875 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
877 $g = new FS::radius_group {
878 'groupname' => $groupname,
879 'description' => $groupname,
882 die "Error inserting new radius_group for service definition group \"$groupname\": $error"
885 push @groupnums, $g->groupnum;
887 $col->columnvalue(join(',',@groupnums));
888 $error = $col->replace;
889 die $error if $error;
892 my @badlabels = qsearch({
893 'table' => 'part_svc_column',
895 'extra_sql' => 'WHERE columnlabel IN ('.
896 "'Descriptive label for this particular device.',".
897 "'IP address. Leave blank for automatic assignment.',".
898 "'Maximum upload speed for this service in Kbps. 0 denotes unlimited.',".
899 "'Maximum download speed for this service in Kbps. 0 denotes unlimited.')"
901 foreach my $col ( @badlabels ) {
902 $col->columnlabel('');
903 my $error = $col->replace;
904 die $error if $error;
911 Delete is unimplemented.
913 The list of svc_* tables is no longer hardcoded, but svc_acct_pop is skipped
914 as a special case until it is renamed.
916 all_part_svc_column methods should be documented
920 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
921 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
922 schema.html from the base documentation.