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.
76 Creates a new service definition. To add the service definition to the
77 database, see L<"insert">.
81 sub table { 'part_svc'; }
83 =item insert [ EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ]
85 Adds this service definition to the database. If there is an error, returns
86 the error, otherwise returns false.
88 The following pseudo-fields may be defined, and will be maintained in
89 the part_svc_column table appropriately (see L<FS::part_svc_column>).
93 =item I<svcdb>__I<field> - Default or fixed value for I<field> in I<svcdb>.
95 =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.
99 If you want to add part_svc_column records for fields that do not exist as
100 fields in the I<svcdb> table, make sure to list then in
101 EXTRA_FIELDS_ARRAYREF also.
103 If EXPORTNUMS_HASHREF is specified (keys are exportnums and values are
104 boolean), the appopriate export_svc records will be inserted.
114 @fields = @{shift(@_)} if @_;
116 my $exportnums = shift;
117 @exportnums = grep $exportnums->{$_}, keys %$exportnums;
122 local $SIG{HUP} = 'IGNORE';
123 local $SIG{INT} = 'IGNORE';
124 local $SIG{QUIT} = 'IGNORE';
125 local $SIG{TERM} = 'IGNORE';
126 local $SIG{TSTP} = 'IGNORE';
127 local $SIG{PIPE} = 'IGNORE';
129 my $oldAutoCommit = $FS::UID::AutoCommit;
130 local $FS::UID::AutoCommit = 0;
133 my $error = $self->SUPER::insert;
135 $dbh->rollback if $oldAutoCommit;
139 # add part_svc_column records
141 my $svcdb = $self->svcdb;
142 # my @rows = map { /^${svcdb}__(.*)$/; $1 }
144 # grep /^${svcdb}__/,
145 # fields('part_svc');
147 grep { $_ ne 'svcnum'
148 && ( defined( $self->getfield($svcdb.'__'.$_.'_flag') )
149 || $self->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
150 } (fields($svcdb), @fields)
152 my $part_svc_column = $self->part_svc_column($field);
153 my $previous = qsearchs('part_svc_column', {
154 'svcpart' => $self->svcpart,
155 'columnname' => $field,
158 my $flag = $self->getfield($svcdb.'__'.$field.'_flag');
159 my $label = $self->getfield($svcdb.'__'.$field.'_label');
160 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
162 if ( uc($flag) =~ /^([A-Z])$/ ) {
163 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
165 $part_svc_column->setfield('columnflag', $1);
166 $part_svc_column->setfield('columnvalue',
167 &$parser($self->getfield($svcdb.'__'.$field))
171 $part_svc_column->setfield('columnlabel', $label)
172 if $label !~ /^\s*$/;
175 $error = $part_svc_column->replace($previous);
177 $error = $part_svc_column->insert;
181 $error = $previous ? $previous->delete : '';
184 $dbh->rollback if $oldAutoCommit;
190 # add export_svc records
191 my $slice = 100/scalar(@exportnums) if @exportnums;
193 foreach my $exportnum ( @exportnums ) {
194 my $export_svc = new FS::export_svc ( {
195 'exportnum' => $exportnum,
196 'svcpart' => $self->svcpart,
198 $error = $export_svc->insert($job, $slice*$done++, $slice);
200 $dbh->rollback if $oldAutoCommit;
205 # XXX shouldn't this update fixed values?
207 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
214 Currently unimplemented. Set the "disabled" field instead.
219 return "Can't (yet?) delete service definitions.";
220 # check & make sure the svcpart isn't in cust_svc or pkg_svc (in any packages)?
223 =item replace OLD_RECORD [ '1.3-COMPAT' [ , EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ] ]
225 Replaces OLD_RECORD with this one in the database. If there is an error,
226 returns the error, otherwise returns false.
230 TODOC: EXTRA_FIELDS_ARRAYREF (same as insert method)
237 my ( $new, $old ) = ( shift, shift );
242 if ( @_ && $_[0] eq '1.3-COMPAT' ) {
245 @fields = @{shift(@_)} if @_;
246 $exportnums = @_ ? shift : '';
249 return 'non-1.3-COMPAT interface not yet written';
253 return "Can't change svcdb for an existing service definition!"
254 unless $old->svcdb eq $new->svcdb;
256 local $SIG{HUP} = 'IGNORE';
257 local $SIG{INT} = 'IGNORE';
258 local $SIG{QUIT} = 'IGNORE';
259 local $SIG{TERM} = 'IGNORE';
260 local $SIG{TSTP} = 'IGNORE';
261 local $SIG{PIPE} = 'IGNORE';
263 my $oldAutoCommit = $FS::UID::AutoCommit;
264 local $FS::UID::AutoCommit = 0;
267 my $error = $new->SUPER::replace( $old );
269 $dbh->rollback if $oldAutoCommit;
273 if ( $compat eq '1.3' ) {
275 # maintain part_svc_column records
277 my $svcdb = $new->svcdb;
279 grep { $_ ne 'svcnum'
280 && ( defined( $new->getfield($svcdb.'__'.$_.'_flag') )
281 || $new->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
282 } (fields($svcdb),@fields)
285 my $part_svc_column = $new->part_svc_column($field);
286 my $previous = qsearchs('part_svc_column', {
287 'svcpart' => $new->svcpart,
288 'columnname' => $field,
291 my $flag = $new->getfield($svcdb.'__'.$field.'_flag');
292 my $label = $new->getfield($svcdb.'__'.$field.'_label');
294 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
296 if ( uc($flag) =~ /^([A-Z])$/ ) {
297 $part_svc_column->setfield('columnflag', $1);
298 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
300 $part_svc_column->setfield('columnvalue',
301 &$parser($new->getfield($svcdb.'__'.$field))
304 $part_svc_column->setfield('columnflag', '');
305 $part_svc_column->setfield('columnvalue', '');
308 $part_svc_column->setfield('columnlabel', $label)
309 if $label !~ /^\s*$/;
312 $error = $part_svc_column->replace($previous);
314 $error = $part_svc_column->insert;
317 $error = $previous ? $previous->delete : '';
320 $dbh->rollback if $oldAutoCommit;
325 # maintain export_svc records
329 #false laziness w/ edit/process/agent_type.cgi
330 my @new_export_svc = ();
331 foreach my $part_export ( qsearch('part_export', {}) ) {
332 my $exportnum = $part_export->exportnum;
334 'exportnum' => $exportnum,
335 'svcpart' => $new->svcpart,
337 my $export_svc = qsearchs('export_svc', $hashref);
339 if ( $export_svc && ! $exportnums->{$exportnum} ) {
340 $error = $export_svc->delete;
342 $dbh->rollback if $oldAutoCommit;
345 } elsif ( ! $export_svc && $exportnums->{$exportnum} ) {
346 push @new_export_svc, new FS::export_svc ( $hashref );
351 my $slice = 100/scalar(@new_export_svc) if @new_export_svc;
353 foreach my $export_svc (@new_export_svc) {
354 $error = $export_svc->insert($job, $slice*$done++, $slice);
356 $dbh->rollback if $oldAutoCommit;
360 $error = $job->update_statustext( int( $slice * $done ) );
362 $dbh->rollback if $oldAutoCommit;
371 $dbh->rollback if $oldAutoCommit;
372 return 'non-1.3-COMPAT interface not yet written';
376 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
383 Checks all fields to make sure this is a valid service definition. If there is
384 an error, returns the error, otherwise returns false. Called by the insert
394 $self->ut_numbern('svcpart')
395 || $self->ut_text('svc')
396 || $self->ut_alpha('svcdb')
397 || $self->ut_enum('disabled', [ '', 'Y' ] )
398 || $self->ut_enum('preserve', [ '', 'Y' ] )
399 || $self->ut_enum('selfservice_access', [ '', 'hidden', 'readonly' ] )
400 || $self->ut_foreign_keyn('classnum', 'part_svc_class', 'classnum' )
401 || $self->ut_enum('restrict_edit_password', [ '', 'Y' ] )
403 return $error if $error;
405 my @fields = eval { fields( $self->svcdb ) }; #might die
406 return "Unknown svcdb: ". $self->svcdb. " (Error: $@)"
412 =item part_svc_column COLUMNNAME
414 Returns the part_svc_column object (see L<FS::part_svc_column>) for the given
415 COLUMNNAME, or a new part_svc_column object if none exists.
419 sub part_svc_column {
420 my( $self, $columnname) = @_;
422 qsearchs('part_svc_column', {
423 'svcpart' => $self->svcpart,
424 'columnname' => $columnname,
426 ) or new FS::part_svc_column {
427 'svcpart' => $self->svcpart,
428 'columnname' => $columnname,
432 =item all_part_svc_column
436 sub all_part_svc_column {
438 qsearch('part_svc_column', { 'svcpart' => $self->svcpart } );
441 =item part_export [ EXPORTTYPE ]
443 Returns a list of all exports (see L<FS::part_export>) for this service, or,
444 if an export type is specified, only returns exports of the given type.
451 $search{'exporttype'} = shift if @_;
452 map { $_ } #behavior of sort undefined in scalar context
453 sort { $a->weight <=> $b->weight }
454 map { qsearchs('part_export', { 'exportnum'=>$_->exportnum, %search } ) }
455 qsearch('export_svc', { 'svcpart'=>$self->svcpart } );
458 =item part_export_usage
460 Returns a list of any exports (see L<FS::part_export>) for this service that
461 are capable of reporting usage information.
465 sub part_export_usage {
467 grep $_->can('usage_sessions'), $self->part_export;
470 =item part_export_did
472 Returns a list of any exports (see L<FS::part_export>) for this service that
473 are capable of returing available DID (phone number) information.
477 sub part_export_did {
479 grep $_->can('get_dids'), $self->part_export;
482 =item part_export_dsl_pull
484 Returns a list of any exports (see L<FS::part_export>) for this service that
485 are capable of pulling/pushing DSL orders.
489 sub part_export_dsl_pull {
491 grep $_->can('dsl_pull'), $self->part_export;
494 =item cust_svc [ PKGPART ]
496 Returns a list of associated customer services (FS::cust_svc records).
498 If a PKGPART is specified, returns the customer services which are contained
499 within packages of that type (see L<FS::part_pkg>). If PKGPARTis specified as
500 B<0>, returns unlinked customer services.
507 my $hashref = { 'svcpart' => $self->svcpart };
509 my( $addl_from, $extra_sql ) = ( '', '' );
512 if ( $pkgpart =~ /^(\d+)$/ ) {
513 $addl_from = 'LEFT JOIN cust_pkg USING ( pkgnum )';
514 $extra_sql = "AND pkgpart = $1";
515 } elsif ( $pkgpart eq '0' ) {
516 $hashref->{'pkgnum'} = '';
521 'table' => 'cust_svc',
522 'addl_from' => $addl_from,
523 'hashref' => $hashref,
524 'extra_sql' => $extra_sql,
528 =item num_cust_svc [ PKGPART ]
530 Returns the number of associated customer services (FS::cust_svc records).
532 If a PKGPART is specified, returns the number of customer services which are
533 contained within packages of that type (see L<FS::part_pkg>). If PKGPART
534 is specified as B<0>, returns the number of unlinked customer services.
541 my @param = ( $self->svcpart );
543 my( $join, $and ) = ( '', '' );
547 $join = 'LEFT JOIN cust_pkg USING ( pkgnum )';
548 $and = 'AND pkgpart = ?';
549 push @param, $pkgpart;
550 } elsif ( $pkgpart eq '0' ) {
551 $and = 'AND pkgnum IS NULL';
555 my $sth = dbh->prepare(
556 "SELECT COUNT(*) FROM cust_svc $join WHERE svcpart = ? $and"
557 ) or die dbh->errstr;
558 $sth->execute(@param)
560 $sth->fetchrow_arrayref->[0];
565 Returns a list of associated FS::svc_* records.
571 map { $_->svc_x } $self->cust_svc;
585 return $svc_defs if $svc_defs; #cache
587 my $conf = new FS::Conf;
589 #false laziness w/part_pkg.pm::plan_info
592 foreach my $INC ( @INC ) {
593 warn "globbing $INC/FS/svc_*.pm\n" if $DEBUG;
594 foreach my $file ( glob("$INC/FS/svc_*.pm") ) {
596 warn "attempting to load service table info from $file\n" if $DEBUG;
597 $file =~ /\/(\w+)\.pm$/ or do {
598 warn "unrecognized file in $INC/FS/: $file\n";
603 if ( $mod =~ /^svc_[A-Z]/ or $mod =~ /^(svc_acct_pop|svc_export_machine)$/ ) {
604 warn "skipping FS::$mod" if $DEBUG;
608 eval "use FS::$mod;";
610 die "error using FS::$mod (skipping): $@\n" if $@;
613 unless ( UNIVERSAL::can("FS::$mod", 'table_info') ) {
614 warn "FS::$mod has no table_info method; skipping";
618 my $info = "FS::$mod"->table_info;
619 unless ( keys %$info ) {
620 warn "FS::$mod->table_info doesn't return info, skipping\n";
623 warn "got info from FS::$mod: $info\n" if $DEBUG;
624 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
625 warn "skipping disabled service FS::$mod" if $DEBUG;
632 tie my %svc_defs, 'Tie::IxHash',
633 map { $_ => $info{$_}->{'fields'} }
634 sort { $info{$a}->{'display_weight'} <=> $info{$b}->{'display_weight'} }
638 $svc_defs = \%svc_defs; #cache
644 Returns a list of all svc_ tables.
650 my $svc_defs = $class->_svc_defs;
651 grep { defined( dbdef->table($_) ) } keys %$svc_defs;
654 =item svc_table_fields TABLE
656 Given a table name, returns a hashref of field names. The field names
657 returned are those with additional (service-definition related) information,
658 not necessarily all database fields of the table. Pseudo-fields may also
659 be returned (i.e. svc_acct.usergroup).
661 Each value of the hashref is another hashref, which can have one or more of
666 =item label - Description of the field
668 =item def_label - Optional description of the field in the context of service definitions
670 =item type - Currently "text", "select", "checkbox", "textarea", "disabled",
671 some components specified by "select-.*.html", and a bunch more...
673 =item disable_default - This field should not allow a default value in service definitions
675 =item disable_fixed - This field should not allow a fixed value in service definitions
677 =item disable_inventory - This field should not allow inventory values in service definitions
679 =item select_list - If type is "text", this can be a listref of possible values.
681 =item select_table - An alternative to select_list, this defines a database table with the possible choices.
683 =item select_key - Used with select_table, this is the field name of keys
685 =item select_label - Used with select_table, this is the field name of labels
691 #maybe this should move and be a class method in svc_Common.pm
692 sub svc_table_fields {
693 my($class, $table) = @_;
694 my $svc_defs = $class->_svc_defs;
695 my $def = $svc_defs->{$table};
697 foreach ( grep !ref($def->{$_}), keys %$def ) {
699 #normalize the shortcut in %info hash
700 $def->{$_} = { 'label' => $def->{$_} };
702 $def->{$_}{'type'} ||= 'text';
717 Job-queue processor for web interface adds/edits
721 use Storable qw(thaw);
727 my $param = thaw(decode_base64(shift));
728 warn Dumper($param) if $DEBUG;
730 my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} })
731 if $param->{'svcpart'};
733 #unmunge cgp_accessmodes (falze laziness-ish w/edit/process/svc_acct.cgi)
734 $param->{'svc_acct__cgp_accessmodes'} ||=
736 grep { $_ !~ /^(flag|label)$/ }
737 map { /^svc_acct__cgp_accessmodes_([\w\/]+)$/ or die "no way"; $1; }
739 grep /^svc_acct__cgp_accessmodes_([\w\/]+)$/,
744 my $new = new FS::part_svc ( {
747 # } qw(svcpart svc svcdb)
748 } ( fields('part_svc'),
749 map { my $svcdb = $_;
750 my @fields = fields($svcdb);
751 push @fields, 'usergroup' if $svcdb eq 'svc_acct'
752 or $svcdb eq 'svc_broadband'; #kludge
755 my $f = $svcdb.'__'.$_;
756 my $flag = $param->{ $f.'_flag' } || ''; #silence warnings
757 if ( $flag =~ /^[MAH]$/ ) {
758 $param->{ $f } = delete( $param->{ $f.'_classnum' } );
760 if ( ( $flag =~ /^[MAHS]$/ or $_ eq 'usergroup' )
761 and ref($param->{ $f }) ) {
762 $param->{ $f } = join(',', @{ $param->{ $f } });
764 ( $f, $f.'_flag', $f.'_label' );
768 } FS::part_svc->svc_tables()
773 map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
774 qsearch('part_export', {} );
777 if ( $param->{'svcpart'} ) {
778 $error = $new->replace( $old,
779 '1.3-COMPAT', #totally bunk, as jeff noted
785 $error = $new->insert( [ 'usergroup' ],
789 $param->{'svcpart'} = $new->getfield('svcpart');
792 die "$error\n" if $error;
795 =item process_bulk_cust_svc
797 Job-queue processor for web interface bulk customer service changes
801 use Storable qw(thaw);
804 sub process_bulk_cust_svc {
807 my $param = thaw(decode_base64(shift));
808 warn Dumper($param) if $DEBUG;
810 local($FS::svc_Common::noexport_hack) = 1
811 if $param->{'noexport'};
814 qsearchs('part_svc', { 'svcpart' => $param->{'old_svcpart'} } );
816 die "Must select a new service definition\n" unless $param->{'new_svcpart'};
818 #the rest should be abstracted out to to its own subroutine?
820 local $SIG{HUP} = 'IGNORE';
821 local $SIG{INT} = 'IGNORE';
822 local $SIG{QUIT} = 'IGNORE';
823 local $SIG{TERM} = 'IGNORE';
824 local $SIG{TSTP} = 'IGNORE';
825 local $SIG{PIPE} = 'IGNORE';
827 my $oldAutoCommit = $FS::UID::AutoCommit;
828 local $FS::UID::AutoCommit = 0;
831 local( $FS::cust_svc::ignore_quantity ) = 1;
833 my $total = $old_part_svc->num_cust_svc( $param->{'pkgpart'} );
836 foreach my $old_cust_svc ( $old_part_svc->cust_svc( $param->{'pkgpart'} ) ) {
838 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
840 $new_cust_svc->svcpart( $param->{'new_svcpart'} );
841 my $error = $new_cust_svc->replace($old_cust_svc);
843 $dbh->rollback if $oldAutoCommit;
844 die "$error\n" if $error;
847 $error = $job->update_statustext( int( 100 * ++$n / $total ) );
849 $dbh->rollback if $oldAutoCommit;
850 die $error if $error;
855 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
861 sub _upgrade_data { #class method
862 my ($class, %opts) = @_;
864 my @part_svc_column = qsearch('part_svc_column', { 'columnname' => 'usergroup' });
865 foreach my $col ( @part_svc_column ) {
866 next if $col->columnvalue =~ /^[\d,]+$/ || !$col->columnvalue;
867 my @groupnames = split(',',$col->columnvalue);
870 foreach my $groupname ( @groupnames ) {
871 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
873 $g = new FS::radius_group {
874 'groupname' => $groupname,
875 'description' => $groupname,
878 die "Error inserting new radius_group for service definition group \"$groupname\": $error"
881 push @groupnums, $g->groupnum;
883 $col->columnvalue(join(',',@groupnums));
884 $error = $col->replace;
885 die $error if $error;
888 my @badlabels = qsearch({
889 'table' => 'part_svc_column',
891 'extra_sql' => 'WHERE columnlabel IN ('.
892 "'Descriptive label for this particular device.',".
893 "'IP address. Leave blank for automatic assignment.',".
894 "'Maximum upload speed for this service in Kbps. 0 denotes unlimited.',".
895 "'Maximum download speed for this service in Kbps. 0 denotes unlimited.')"
897 foreach my $col ( @badlabels ) {
898 $col->columnlabel('');
899 my $error = $col->replace;
900 die $error if $error;
907 Delete is unimplemented.
909 The list of svc_* tables is no longer hardcoded, but svc_acct_pop is skipped
910 as a special case until it is renamed.
912 all_part_svc_column methods should be documented
916 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
917 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
918 schema.html from the base documentation.