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.
116 @fields = @{shift(@_)} if @_;
117 my $exportnums = shift || {};
121 local $SIG{HUP} = 'IGNORE';
122 local $SIG{INT} = 'IGNORE';
123 local $SIG{QUIT} = 'IGNORE';
124 local $SIG{TERM} = 'IGNORE';
125 local $SIG{TSTP} = 'IGNORE';
126 local $SIG{PIPE} = 'IGNORE';
128 my $oldAutoCommit = $FS::UID::AutoCommit;
129 local $FS::UID::AutoCommit = 0;
132 my $error = $self->SUPER::insert;
134 $dbh->rollback if $oldAutoCommit;
138 # add part_svc_column records
140 my $svcdb = $self->svcdb;
141 # my @rows = map { /^${svcdb}__(.*)$/; $1 }
143 # grep /^${svcdb}__/,
144 # fields('part_svc');
146 grep { $_ ne 'svcnum'
147 && ( defined( $self->getfield($svcdb.'__'.$_.'_flag') )
148 || $self->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
149 } (fields($svcdb), @fields)
151 my $part_svc_column = $self->part_svc_column($field);
152 my $previous = qsearchs('part_svc_column', {
153 'svcpart' => $self->svcpart,
154 'columnname' => $field,
157 my $flag = $self->getfield($svcdb.'__'.$field.'_flag');
158 my $label = $self->getfield($svcdb.'__'.$field.'_label');
159 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
161 if ( uc($flag) =~ /^([A-Z])$/ ) {
162 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
164 $part_svc_column->setfield('columnflag', $1);
165 $part_svc_column->setfield('columnvalue',
166 &$parser($self->getfield($svcdb.'__'.$field))
170 $part_svc_column->setfield('columnlabel', $label)
171 if $label !~ /^\s*$/;
174 $error = $part_svc_column->replace($previous);
176 $error = $part_svc_column->insert;
180 $error = $previous ? $previous->delete : '';
183 $dbh->rollback if $oldAutoCommit;
189 # add export_svc records
190 my @exportnums = grep $exportnums->{$_}, keys %$exportnums;
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,
197 'role' => $exportnums->{$exportnum},
199 $error = $export_svc->insert($job, $slice*$done++, $slice);
201 $dbh->rollback if $oldAutoCommit;
206 # XXX shouldn't this update fixed values?
208 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
215 Currently unimplemented. Set the "disabled" field instead.
220 return "Can't (yet?) delete service definitions.";
221 # check & make sure the svcpart isn't in cust_svc or pkg_svc (in any packages)?
224 =item replace OLD_RECORD [ '1.3-COMPAT' [ , EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ] ]
226 Replaces OLD_RECORD with this one in the database. If there is an error,
227 returns the error, otherwise returns false.
231 TODOC: EXTRA_FIELDS_ARRAYREF (same as insert method)
238 my ( $new, $old ) = ( shift, shift );
243 if ( @_ && $_[0] eq '1.3-COMPAT' ) {
246 @fields = @{shift(@_)} if @_;
247 $exportnums = @_ ? shift : '';
250 return 'non-1.3-COMPAT interface not yet written';
254 return "Can't change svcdb for an existing service definition!"
255 unless $old->svcdb eq $new->svcdb;
257 local $SIG{HUP} = 'IGNORE';
258 local $SIG{INT} = 'IGNORE';
259 local $SIG{QUIT} = 'IGNORE';
260 local $SIG{TERM} = 'IGNORE';
261 local $SIG{TSTP} = 'IGNORE';
262 local $SIG{PIPE} = 'IGNORE';
264 my $oldAutoCommit = $FS::UID::AutoCommit;
265 local $FS::UID::AutoCommit = 0;
268 my $error = $new->SUPER::replace( $old );
270 $dbh->rollback if $oldAutoCommit;
274 if ( $compat eq '1.3' ) {
276 # maintain part_svc_column records
278 my $svcdb = $new->svcdb;
280 grep { $_ ne 'svcnum'
281 && ( defined( $new->getfield($svcdb.'__'.$_.'_flag') )
282 || $new->getfield($svcdb.'__'.$_.'_label') !~ /^\s*$/ )
283 } (fields($svcdb),@fields)
286 my $part_svc_column = $new->part_svc_column($field);
287 my $previous = qsearchs('part_svc_column', {
288 'svcpart' => $new->svcpart,
289 'columnname' => $field,
292 my $flag = $new->getfield($svcdb.'__'.$field.'_flag');
293 my $label = $new->getfield($svcdb.'__'.$field.'_label');
295 if ( uc($flag) =~ /^([A-Z])$/ || $label !~ /^\s*$/ ) {
297 if ( uc($flag) =~ /^([A-Z])$/ ) {
298 $part_svc_column->setfield('columnflag', $1);
299 my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
301 $part_svc_column->setfield('columnvalue',
302 &$parser($new->getfield($svcdb.'__'.$field))
305 $part_svc_column->setfield('columnflag', '');
306 $part_svc_column->setfield('columnvalue', '');
309 $part_svc_column->setfield('columnlabel', $label)
310 if $label !~ /^\s*$/;
313 $error = $part_svc_column->replace($previous);
315 $error = $part_svc_column->insert;
318 $error = $previous ? $previous->delete : '';
321 $dbh->rollback if $oldAutoCommit;
326 # maintain export_svc records
328 if ( $exportnums ) { # hash of exportnum => role
330 #false laziness w/ edit/process/agent_type.cgi
331 #and, more importantly, with m2m_Common
332 my @new_export_svc = ();
333 foreach my $part_export ( qsearch('part_export', {}) ) {
334 my $exportnum = $part_export->exportnum;
336 'exportnum' => $exportnum,
337 'svcpart' => $new->svcpart,
339 my $export_svc = qsearchs('export_svc', $hashref);
342 my $old_role = $export_svc->role || 1; # 1 = null in the db
343 if ( ! $exportnums->{$exportnum}
344 or $old_role ne $exportnums->{$exportnum} ) {
346 $error = $export_svc->delete;
348 $dbh->rollback if $oldAutoCommit;
351 undef $export_svc; # on a role change, force it to be reinserted
355 if ( ! $export_svc && $exportnums->{$exportnum} ) {
356 # also applies if it's been undef'd because of role change
357 $hashref->{role} = $exportnums->{$exportnum};
358 push @new_export_svc, new FS::export_svc ( $hashref );
363 my $slice = 100/scalar(@new_export_svc) if @new_export_svc;
365 foreach my $export_svc (@new_export_svc) {
366 $error = $export_svc->insert($job, $slice*$done++, $slice);
368 $dbh->rollback if $oldAutoCommit;
372 $error = $job->update_statustext( int( $slice * $done ) );
374 $dbh->rollback if $oldAutoCommit;
383 $dbh->rollback if $oldAutoCommit;
384 return 'non-1.3-COMPAT interface not yet written';
388 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
395 Checks all fields to make sure this is a valid service definition. If there is
396 an error, returns the error, otherwise returns false. Called by the insert
406 $self->ut_numbern('svcpart')
407 || $self->ut_text('svc')
408 || $self->ut_alpha('svcdb')
409 || $self->ut_flag('disabled')
410 || $self->ut_flag('preserve')
411 || $self->ut_enum('selfservice_access', [ '', 'hidden', 'readonly' ] )
412 || $self->ut_foreign_keyn('classnum', 'part_svc_class', 'classnum' )
413 || $self->ut_flag('restrict_edit_password')
414 || $self->ut_flag('has_router')
416 return $error if $error;
418 my @fields = eval { fields( $self->svcdb ) }; #might die
419 return "Unknown svcdb: ". $self->svcdb. " (Error: $@)"
425 =item part_svc_column COLUMNNAME
427 Returns the part_svc_column object (see L<FS::part_svc_column>) for the given
428 COLUMNNAME, or a new part_svc_column object if none exists.
432 sub part_svc_column {
433 my( $self, $columnname) = @_;
435 qsearchs('part_svc_column', {
436 'svcpart' => $self->svcpart,
437 'columnname' => $columnname,
439 ) or new FS::part_svc_column {
440 'svcpart' => $self->svcpart,
441 'columnname' => $columnname,
445 =item all_part_svc_column
449 sub all_part_svc_column {
451 qsearch('part_svc_column', { 'svcpart' => $self->svcpart } );
454 =item part_export [ EXPORTTYPE ]
456 Returns a list of all exports (see L<FS::part_export>) for this service, or,
457 if an export type is specified, only returns exports of the given type.
464 $search{'exporttype'} = shift if @_;
465 map { $_ } #behavior of sort undefined in scalar context
466 sort { $a->weight <=> $b->weight }
467 map { qsearchs('part_export', { 'exportnum'=>$_->exportnum, %search } ) }
468 qsearch('export_svc', { 'svcpart'=>$self->svcpart } );
471 =item part_export_usage
473 Returns a list of any exports (see L<FS::part_export>) for this service that
474 are capable of reporting usage information.
478 sub part_export_usage {
480 grep $_->can('usage_sessions'), $self->part_export;
483 =item part_export_did
485 Returns a list of any exports (see L<FS::part_export>) for this service that
486 are capable of returing available DID (phone number) information.
490 sub part_export_did {
492 grep $_->can_get_dids, $self->part_export;
495 =item part_export_dsl_pull
497 Returns a list of any exports (see L<FS::part_export>) for this service that
498 are capable of pulling/pushing DSL orders.
502 sub part_export_dsl_pull {
504 grep $_->can('dsl_pull'), $self->part_export;
507 =item cust_svc [ PKGPART ]
509 Returns a list of associated customer services (FS::cust_svc records).
511 If a PKGPART is specified, returns the customer services which are contained
512 within packages of that type (see L<FS::part_pkg>). If PKGPARTis specified as
513 B<0>, returns unlinked customer services.
520 my $hashref = { 'svcpart' => $self->svcpart };
522 my( $addl_from, $extra_sql ) = ( '', '' );
525 if ( $pkgpart =~ /^(\d+)$/ ) {
526 $addl_from = 'LEFT JOIN cust_pkg USING ( pkgnum )';
527 $extra_sql = "AND pkgpart = $1";
528 } elsif ( $pkgpart eq '0' ) {
529 $hashref->{'pkgnum'} = '';
534 'table' => 'cust_svc',
535 'addl_from' => $addl_from,
536 'hashref' => $hashref,
537 'extra_sql' => $extra_sql,
541 =item num_cust_svc [ PKGPART ]
543 Returns the number of associated customer services (FS::cust_svc records).
545 If a PKGPART is specified, returns the number of customer services which are
546 contained within packages of that type (see L<FS::part_pkg>). If PKGPART
547 is specified as B<0>, returns the number of unlinked customer services.
554 my @param = ( $self->svcpart );
556 my( $join, $and ) = ( '', '' );
560 $join = 'LEFT JOIN cust_pkg USING ( pkgnum )';
561 $and = 'AND pkgpart = ?';
562 push @param, $pkgpart;
563 } elsif ( $pkgpart eq '0' ) {
564 $and = 'AND pkgnum IS NULL';
568 my $sth = dbh->prepare(
569 "SELECT COUNT(*) FROM cust_svc $join WHERE svcpart = ? $and"
570 ) or die dbh->errstr;
571 $sth->execute(@param)
573 $sth->fetchrow_arrayref->[0];
578 Returns a list of associated FS::svc_* records.
584 map { $_->svc_x } $self->cust_svc;
598 return $svc_defs if $svc_defs; #cache
600 my $conf = new FS::Conf;
602 #false laziness w/part_pkg.pm::plan_info
605 foreach my $INC ( @INC ) {
606 warn "globbing $INC/FS/svc_*.pm\n" if $DEBUG;
607 foreach my $file ( glob("$INC/FS/svc_*.pm") ) {
609 warn "attempting to load service table info from $file\n" if $DEBUG;
610 $file =~ /\/(\w+)\.pm$/ or do {
611 warn "unrecognized file in $INC/FS/: $file\n";
616 if ( $mod =~ /^svc_[A-Z]/ or $mod =~ /^(svc_acct_pop|svc_export_machine)$/ ) {
617 warn "skipping FS::$mod" if $DEBUG;
621 eval "use FS::$mod;";
623 die "error using FS::$mod (skipping): $@\n" if $@;
626 unless ( UNIVERSAL::can("FS::$mod", 'table_info') ) {
627 warn "FS::$mod has no table_info method; skipping";
631 my $info = "FS::$mod"->table_info;
632 unless ( keys %$info ) {
633 warn "FS::$mod->table_info doesn't return info, skipping\n";
636 warn "got info from FS::$mod: $info\n" if $DEBUG;
637 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
638 warn "skipping disabled service FS::$mod" if $DEBUG;
645 tie my %svc_defs, 'Tie::IxHash',
646 map { $_ => $info{$_}->{'fields'} }
647 sort { $info{$a}->{'display_weight'} <=> $info{$b}->{'display_weight'} }
651 $svc_defs = \%svc_defs; #cache
657 Returns a list of all svc_ tables.
663 my $svc_defs = $class->_svc_defs;
664 grep { defined( dbdef->table($_) ) } keys %$svc_defs;
667 =item svc_table_fields TABLE
669 Given a table name, returns a hashref of field names. The field names
670 returned are those with additional (service-definition related) information,
671 not necessarily all database fields of the table. Pseudo-fields may also
672 be returned (i.e. svc_acct.usergroup).
674 Each value of the hashref is another hashref, which can have one or more of
679 =item label - Description of the field
681 =item def_label - Optional description of the field in the context of service definitions
683 =item type - Currently "text", "select", "checkbox", "textarea", "disabled",
684 some components specified by "select-.*.html", and a bunch more...
686 =item disable_default - This field should not allow a default value in service definitions
688 =item disable_fixed - This field should not allow a fixed value in service definitions
690 =item disable_inventory - This field should not allow inventory values in service definitions
692 =item select_list - If type is "text", this can be a listref of possible values.
694 =item select_table - An alternative to select_list, this defines a database table with the possible choices.
696 =item select_key - Used with select_table, this is the field name of keys
698 =item select_label - Used with select_table, this is the field name of labels
700 =item select_allow_empty - Used with select_table, adds an empty option
706 #maybe this should move and be a class method in svc_Common.pm
707 sub svc_table_fields {
708 my($class, $table) = @_;
709 my $svc_defs = $class->_svc_defs;
710 my $def = $svc_defs->{$table};
712 foreach ( grep !ref($def->{$_}), keys %$def ) {
714 #normalize the shortcut in %info hash
715 $def->{$_} = { 'label' => $def->{$_} };
717 $def->{$_}{'type'} ||= 'text';
732 Job-queue processor for web interface adds/edits
740 warn Dumper($param) if $DEBUG;
742 my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} })
743 if $param->{'svcpart'};
745 #unmunge cgp_accessmodes (falze laziness-ish w/edit/process/svc_acct.cgi)
746 $param->{'svc_acct__cgp_accessmodes'} ||=
748 grep { $_ !~ /^(flag|label)$/ }
749 map { /^svc_acct__cgp_accessmodes_([\w\/]+)$/ or die "no way"; $1; }
751 grep /^svc_acct__cgp_accessmodes_([\w\/]+)$/,
756 my $new = new FS::part_svc ( {
759 # } qw(svcpart svc svcdb)
760 } ( fields('part_svc'),
761 map { my $svcdb = $_;
762 my @fields = fields($svcdb);
763 push @fields, 'usergroup' if $svcdb eq 'svc_acct'
764 or $svcdb eq 'svc_broadband'; #kludge
767 my $f = $svcdb.'__'.$_;
768 my $flag = $param->{ $f.'_flag' } || ''; #silence warnings
769 if ( $flag =~ /^[MAH]$/ ) {
770 $param->{ $f } = delete( $param->{ $f.'_classnum' } );
772 if ( ( $flag =~ /^[MAHS]$/ or $_ eq 'usergroup' )
773 and ref($param->{ $f }) ) {
774 $param->{ $f } = join(',', @{ $param->{ $f } });
776 ( $f, $f.'_flag', $f.'_label' );
780 } FS::part_svc->svc_tables()
785 map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
786 qsearch('part_export', {} );
787 foreach my $exportnum (%exportnums) {
788 my $role = $param->{'exportnum'.$exportnum.'_role'};
789 # role is undef if the export has no role selector
790 if ( $exportnums{$exportnum} && $role ) {
791 $exportnums{$exportnum} = $role;
795 if ( $param->{'svcpart'} ) {
796 $error = $new->replace( $old,
797 '1.3-COMPAT', #totally bunk, as jeff noted
803 $error = $new->insert( [ 'usergroup' ],
807 $param->{'svcpart'} = $new->getfield('svcpart');
810 die "$error\n" if $error;
813 =item process_bulk_cust_svc
815 Job-queue processor for web interface bulk customer service changes
820 sub process_bulk_cust_svc {
823 warn Dumper($param) if $DEBUG;
825 local($FS::svc_Common::noexport_hack) = 1
826 if $param->{'noexport'};
829 qsearchs('part_svc', { 'svcpart' => $param->{'old_svcpart'} } );
831 die "Must select a new service definition\n" unless $param->{'new_svcpart'};
833 #the rest should be abstracted out to to its own subroutine?
835 local $SIG{HUP} = 'IGNORE';
836 local $SIG{INT} = 'IGNORE';
837 local $SIG{QUIT} = 'IGNORE';
838 local $SIG{TERM} = 'IGNORE';
839 local $SIG{TSTP} = 'IGNORE';
840 local $SIG{PIPE} = 'IGNORE';
842 my $oldAutoCommit = $FS::UID::AutoCommit;
843 local $FS::UID::AutoCommit = 0;
846 local( $FS::cust_svc::ignore_quantity ) = 1;
848 my $total = $old_part_svc->num_cust_svc( $param->{'pkgpart'} );
851 foreach my $old_cust_svc ( $old_part_svc->cust_svc( $param->{'pkgpart'} ) ) {
853 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
855 $new_cust_svc->svcpart( $param->{'new_svcpart'} );
856 my $error = $new_cust_svc->replace($old_cust_svc);
858 $dbh->rollback if $oldAutoCommit;
859 die "$error\n" if $error;
862 $error = $job->update_statustext( int( 100 * ++$n / $total ) );
864 $dbh->rollback if $oldAutoCommit;
865 die $error if $error;
870 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
876 sub _upgrade_data { #class method
877 my ($class, %opts) = @_;
879 my @part_svc_column = qsearch('part_svc_column', { 'columnname' => 'usergroup' });
880 foreach my $col ( @part_svc_column ) {
881 next if $col->columnvalue =~ /^[\d,]+$/ || !$col->columnvalue;
882 my @groupnames = split(',',$col->columnvalue);
885 foreach my $groupname ( @groupnames ) {
886 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
888 $g = new FS::radius_group {
889 'groupname' => $groupname,
890 'description' => $groupname,
893 die "Error inserting new radius_group for service definition group \"$groupname\": $error"
896 push @groupnums, $g->groupnum;
898 $col->columnvalue(join(',',@groupnums));
899 $error = $col->replace;
900 die $error if $error;
903 my @badlabels = qsearch({
904 'table' => 'part_svc_column',
906 'extra_sql' => 'WHERE columnlabel IN ('.
907 "'Descriptive label for this particular device.',".
908 "'IP address. Leave blank for automatic assignment.',".
909 "'Maximum upload speed for this service in Kbps. 0 denotes unlimited.',".
910 "'Maximum download speed for this service in Kbps. 0 denotes unlimited.')"
912 foreach my $col ( @badlabels ) {
913 $col->columnlabel('');
914 my $error = $col->replace;
915 die $error if $error;
922 Delete is unimplemented.
924 The list of svc_* tables is no longer hardcoded, but svc_acct_pop is skipped
925 as a special case until it is renamed.
927 all_part_svc_column methods should be documented
931 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
932 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
933 schema.html from the base documentation.