4 use vars qw( @ISA $DEBUG );
5 use FS::Record qw( qsearch qsearchs fields dbh );
6 use FS::part_svc_column;
11 @ISA = qw(FS::Record);
17 FS::part_svc - Object methods for part_svc objects
23 $record = new FS::part_svc \%hash
24 $record = new FS::part_svc { 'column' => 'value' };
26 $error = $record->insert;
27 $error = $record->insert( [ 'pseudofield' ] );
28 $error = $record->insert( [ 'pseudofield' ], \%exportnums );
30 $error = $new_record->replace($old_record);
31 $error = $new_record->replace($old_record, '1.3-COMPAT', [ 'pseudofield' ] );
32 $error = $new_record->replace($old_record, '1.3-COMPAT', [ 'pseudofield' ], \%exportnums );
34 $error = $record->delete;
36 $error = $record->check;
40 An FS::part_svc represents a service definition. FS::part_svc inherits from
41 FS::Record. The following fields are currently supported:
45 =item svcpart - primary key (assigned automatically for new service definitions)
47 =item svc - text name of this service definition
49 =item svcdb - table used for this service. See L<FS::svc_acct>,
50 L<FS::svc_domain>, and L<FS::svc_forward>, among others.
52 =item disabled - Disabled flag, empty or `Y'
62 Creates a new service definition. To add the service definition to the
63 database, see L<"insert">.
67 sub table { 'part_svc'; }
69 =item insert [ EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ]
71 Adds this service definition to the database. If there is an error, returns
72 the error, otherwise returns false.
74 The following pseudo-fields may be defined, and will be maintained in
75 the part_svc_column table appropriately (see L<FS::part_svc_column>).
79 =item I<svcdb>__I<field> - Default or fixed value for I<field> in I<svcdb>.
81 =item I<svcdb>__I<field>_flag - defines I<svcdb>__I<field> action: null, `D' for default, or `F' for fixed. For virtual fields, can also be 'X' for excluded.
85 If you want to add part_svc_column records for fields that do not exist as
86 (real or virtual) fields in the I<svcdb> table, make sure to list then in
87 EXTRA_FIELDS_ARRAYREF also.
89 If EXPORTNUMS_HASHREF is specified (keys are exportnums and values are
90 boolean), the appopriate export_svc records will be inserted.
100 @fields = @{shift(@_)} if @_;
102 my $exportnums = shift;
103 @exportnums = grep $exportnums->{$_}, keys %$exportnums;
108 local $SIG{HUP} = 'IGNORE';
109 local $SIG{INT} = 'IGNORE';
110 local $SIG{QUIT} = 'IGNORE';
111 local $SIG{TERM} = 'IGNORE';
112 local $SIG{TSTP} = 'IGNORE';
113 local $SIG{PIPE} = 'IGNORE';
115 my $oldAutoCommit = $FS::UID::AutoCommit;
116 local $FS::UID::AutoCommit = 0;
119 my $error = $self->SUPER::insert;
121 $dbh->rollback if $oldAutoCommit;
125 # add part_svc_column records
127 my $svcdb = $self->svcdb;
128 # my @rows = map { /^${svcdb}__(.*)$/; $1 }
130 # grep /^${svcdb}__/,
131 # fields('part_svc');
133 grep { $_ ne 'svcnum'
134 && defined( $self->getfield($svcdb.'__'.$_.'_flag') )
135 } (fields($svcdb), @fields)
137 my $part_svc_column = $self->part_svc_column($field);
138 my $previous = qsearchs('part_svc_column', {
139 'svcpart' => $self->svcpart,
140 'columnname' => $field,
143 my $flag = $self->getfield($svcdb.'__'.$field.'_flag');
144 if ( uc($flag) =~ /^([DFX])$/ ) {
145 $part_svc_column->setfield('columnflag', $1);
146 $part_svc_column->setfield('columnvalue',
147 $self->getfield($svcdb.'__'.$field)
150 $error = $part_svc_column->replace($previous);
152 $error = $part_svc_column->insert;
155 $error = $previous ? $previous->delete : '';
158 $dbh->rollback if $oldAutoCommit;
164 # add export_svc records
165 my $slice = 100/scalar(@exportnums) if @exportnums;
167 foreach my $exportnum ( @exportnums ) {
168 my $export_svc = new FS::export_svc ( {
169 'exportnum' => $exportnum,
170 'svcpart' => $self->svcpart,
172 $error = $export_svc->insert($job, $slice*$done++, $slice);
174 $dbh->rollback if $oldAutoCommit;
179 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
186 Currently unimplemented. Set the "disabled" field instead.
191 return "Can't (yet?) delete service definitions.";
192 # check & make sure the svcpart isn't in cust_svc or pkg_svc (in any packages)?
195 =item replace OLD_RECORD [ '1.3-COMPAT' [ , EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ] ]
197 Replaces OLD_RECORD with this one in the database. If there is an error,
198 returns the error, otherwise returns false.
202 TODOC: EXTRA_FIELDS_ARRAYREF (same as insert method)
209 my ( $new, $old ) = ( shift, shift );
214 if ( @_ && $_[0] eq '1.3-COMPAT' ) {
217 @fields = @{shift(@_)} if @_;
218 $exportnums = @_ ? shift : '';
221 return 'non-1.3-COMPAT interface not yet written';
225 return "Can't change svcdb for an existing service definition!"
226 unless $old->svcdb eq $new->svcdb;
228 local $SIG{HUP} = 'IGNORE';
229 local $SIG{INT} = 'IGNORE';
230 local $SIG{QUIT} = 'IGNORE';
231 local $SIG{TERM} = 'IGNORE';
232 local $SIG{TSTP} = 'IGNORE';
233 local $SIG{PIPE} = 'IGNORE';
235 my $oldAutoCommit = $FS::UID::AutoCommit;
236 local $FS::UID::AutoCommit = 0;
239 my $error = $new->SUPER::replace( $old );
241 $dbh->rollback if $oldAutoCommit;
245 if ( $compat eq '1.3' ) {
247 # maintain part_svc_column records
249 my $svcdb = $new->svcdb;
251 grep { $_ ne 'svcnum'
252 && defined( $new->getfield($svcdb.'__'.$_.'_flag') )
253 } (fields($svcdb),@fields)
255 my $part_svc_column = $new->part_svc_column($field);
256 my $previous = qsearchs('part_svc_column', {
257 'svcpart' => $new->svcpart,
258 'columnname' => $field,
261 my $flag = $new->getfield($svcdb.'__'.$field.'_flag');
262 if ( uc($flag) =~ /^([DFX])$/ ) {
263 $part_svc_column->setfield('columnflag', $1);
264 $part_svc_column->setfield('columnvalue',
265 $new->getfield($svcdb.'__'.$field)
268 $error = $part_svc_column->replace($previous);
270 $error = $part_svc_column->insert;
273 $error = $previous ? $previous->delete : '';
276 $dbh->rollback if $oldAutoCommit;
281 # maintain export_svc records
285 #false laziness w/ edit/process/agent_type.cgi
286 my @new_export_svc = ();
287 foreach my $part_export ( qsearch('part_export', {}) ) {
288 my $exportnum = $part_export->exportnum;
290 'exportnum' => $exportnum,
291 'svcpart' => $new->svcpart,
293 my $export_svc = qsearchs('export_svc', $hashref);
295 if ( $export_svc && ! $exportnums->{$exportnum} ) {
296 $error = $export_svc->delete;
298 $dbh->rollback if $oldAutoCommit;
301 } elsif ( ! $export_svc && $exportnums->{$exportnum} ) {
302 push @new_export_svc, new FS::export_svc ( $hashref );
307 my $slice = 100/scalar(@new_export_svc) if @new_export_svc;
309 foreach my $export_svc (@new_export_svc) {
310 $error = $export_svc->insert($job, $slice*$done++, $slice);
312 $dbh->rollback if $oldAutoCommit;
316 $error = $job->update_statustext( int( $slice * $done ) );
318 $dbh->rollback if $oldAutoCommit;
327 $dbh->rollback if $oldAutoCommit;
328 return 'non-1.3-COMPAT interface not yet written';
332 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
339 Checks all fields to make sure this is a valid service definition. If there is
340 an error, returns the error, otherwise returns false. Called by the insert
347 my $recref = $self->hashref;
351 $self->ut_numbern('svcpart')
352 || $self->ut_text('svc')
353 || $self->ut_alpha('svcdb')
354 || $self->ut_enum('disabled', [ '', 'Y' ] )
356 return $error if $error;
358 my @fields = eval { fields( $recref->{svcdb} ) }; #might die
359 return "Unknown svcdb!" unless @fields;
364 =item part_svc_column COLUMNNAME
366 Returns the part_svc_column object (see L<FS::part_svc_column>) for the given
367 COLUMNNAME, or a new part_svc_column object if none exists.
371 sub part_svc_column {
372 my( $self, $columnname) = @_;
374 qsearchs('part_svc_column', {
375 'svcpart' => $self->svcpart,
376 'columnname' => $columnname,
378 ) or new FS::part_svc_column {
379 'svcpart' => $self->svcpart,
380 'columnname' => $columnname,
384 =item all_part_svc_column
388 sub all_part_svc_column {
390 qsearch('part_svc_column', { 'svcpart' => $self->svcpart } );
393 =item part_export [ EXPORTTYPE ]
395 Returns all exports (see L<FS::part_export>) for this service, or, if an
396 export type is specified, only returns exports of the given type.
403 $search{'exporttype'} = shift if @_;
404 map { qsearchs('part_export', { 'exportnum' => $_->exportnum, %search } ) }
405 qsearch('export_svc', { 'svcpart' => $self->svcpart } );
410 Returns a list of associated FS::cust_svc records.
416 qsearch('cust_svc', { 'svcpart' => $self->svcpart } );
421 Returns a list of associated FS::svc_* records.
427 map { $_->svc_x } $self->cust_svc;
438 Experimental job-queue processor for web interface adds/edits
442 use Storable qw(thaw);
448 my $param = thaw(decode_base64(shift));
449 warn Dumper($param) if $DEBUG;
451 my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} })
452 if $param->{'svcpart'};
454 $param->{'svc_acct__usergroup'} =
455 ref($param->{'svc_acct__usergroup'})
456 ? join(',', @{$param->{'svc_acct__usergroup'}} )
459 my $new = new FS::part_svc ( {
462 # } qw(svcpart svc svcdb)
463 } ( fields('part_svc'),
464 map { my $svcdb = $_;
465 my @fields = fields($svcdb);
466 push @fields, 'usergroup' if $svcdb eq 'svc_acct'; #kludge
467 map { ( $svcdb.'__'.$_, $svcdb.'__'.$_.'_flag' ) } @fields;
468 } grep defined( $FS::Record::dbdef->table($_) ),
469 qw( svc_acct svc_domain svc_forward svc_www svc_broadband )
474 map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
475 qsearch('part_export', {} );
478 if ( $param->{'svcpart'} ) {
479 $error = $new->replace( $old,
486 $error = $new->insert( [ 'usergroup' ],
490 $param->{'svcpart'} = $new->getfield('svcpart');
493 die $error if $error;
498 Delete is unimplemented.
500 The list of svc_* tables is hardcoded. When svc_acct_pop is renamed, this
503 all_part_svc_column method should be documented
507 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
508 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
509 schema.html from the base documentation.