4 use vars qw( @ISA $DEBUG );
5 use FS::Record qw( qsearch qsearchs fields dbh );
6 use FS::Schema qw( dbdef );
7 use FS::part_svc_column;
12 @ISA = qw(FS::Record);
18 FS::part_svc - Object methods for part_svc objects
24 $record = new FS::part_svc \%hash
25 $record = new FS::part_svc { 'column' => 'value' };
27 $error = $record->insert;
28 $error = $record->insert( [ 'pseudofield' ] );
29 $error = $record->insert( [ 'pseudofield' ], \%exportnums );
31 $error = $new_record->replace($old_record);
32 $error = $new_record->replace($old_record, '1.3-COMPAT', [ 'pseudofield' ] );
33 $error = $new_record->replace($old_record, '1.3-COMPAT', [ 'pseudofield' ], \%exportnums );
35 $error = $record->delete;
37 $error = $record->check;
41 An FS::part_svc represents a service definition. FS::part_svc inherits from
42 FS::Record. The following fields are currently supported:
46 =item svcpart - primary key (assigned automatically for new service definitions)
48 =item svc - text name of this service definition
50 =item svcdb - table used for this service. See L<FS::svc_acct>,
51 L<FS::svc_domain>, and L<FS::svc_forward>, among others.
53 =item disabled - Disabled flag, empty or `Y'
63 Creates a new service definition. To add the service definition to the
64 database, see L<"insert">.
68 sub table { 'part_svc'; }
70 =item insert [ EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ]
72 Adds this service definition to the database. If there is an error, returns
73 the error, otherwise returns false.
75 The following pseudo-fields may be defined, and will be maintained in
76 the part_svc_column table appropriately (see L<FS::part_svc_column>).
80 =item I<svcdb>__I<field> - Default or fixed value for I<field> in I<svcdb>.
82 =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.
86 If you want to add part_svc_column records for fields that do not exist as
87 (real or virtual) fields in the I<svcdb> table, make sure to list then in
88 EXTRA_FIELDS_ARRAYREF also.
90 If EXPORTNUMS_HASHREF is specified (keys are exportnums and values are
91 boolean), the appopriate export_svc records will be inserted.
101 @fields = @{shift(@_)} if @_;
103 my $exportnums = shift;
104 @exportnums = grep $exportnums->{$_}, keys %$exportnums;
109 local $SIG{HUP} = 'IGNORE';
110 local $SIG{INT} = 'IGNORE';
111 local $SIG{QUIT} = 'IGNORE';
112 local $SIG{TERM} = 'IGNORE';
113 local $SIG{TSTP} = 'IGNORE';
114 local $SIG{PIPE} = 'IGNORE';
116 my $oldAutoCommit = $FS::UID::AutoCommit;
117 local $FS::UID::AutoCommit = 0;
120 my $error = $self->SUPER::insert;
122 $dbh->rollback if $oldAutoCommit;
126 # add part_svc_column records
128 my $svcdb = $self->svcdb;
129 # my @rows = map { /^${svcdb}__(.*)$/; $1 }
131 # grep /^${svcdb}__/,
132 # fields('part_svc');
134 grep { $_ ne 'svcnum'
135 && defined( $self->getfield($svcdb.'__'.$_.'_flag') )
136 } (fields($svcdb), @fields)
138 my $part_svc_column = $self->part_svc_column($field);
139 my $previous = qsearchs('part_svc_column', {
140 'svcpart' => $self->svcpart,
141 'columnname' => $field,
144 my $flag = $self->getfield($svcdb.'__'.$field.'_flag');
145 if ( uc($flag) =~ /^([DFX])$/ ) {
146 $part_svc_column->setfield('columnflag', $1);
147 $part_svc_column->setfield('columnvalue',
148 $self->getfield($svcdb.'__'.$field)
151 $error = $part_svc_column->replace($previous);
153 $error = $part_svc_column->insert;
156 $error = $previous ? $previous->delete : '';
159 $dbh->rollback if $oldAutoCommit;
165 # add export_svc records
166 my $slice = 100/scalar(@exportnums) if @exportnums;
168 foreach my $exportnum ( @exportnums ) {
169 my $export_svc = new FS::export_svc ( {
170 'exportnum' => $exportnum,
171 'svcpart' => $self->svcpart,
173 $error = $export_svc->insert($job, $slice*$done++, $slice);
175 $dbh->rollback if $oldAutoCommit;
180 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
187 Currently unimplemented. Set the "disabled" field instead.
192 return "Can't (yet?) delete service definitions.";
193 # check & make sure the svcpart isn't in cust_svc or pkg_svc (in any packages)?
196 =item replace OLD_RECORD [ '1.3-COMPAT' [ , EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ] ]
198 Replaces OLD_RECORD with this one in the database. If there is an error,
199 returns the error, otherwise returns false.
203 TODOC: EXTRA_FIELDS_ARRAYREF (same as insert method)
210 my ( $new, $old ) = ( shift, shift );
215 if ( @_ && $_[0] eq '1.3-COMPAT' ) {
218 @fields = @{shift(@_)} if @_;
219 $exportnums = @_ ? shift : '';
222 return 'non-1.3-COMPAT interface not yet written';
226 return "Can't change svcdb for an existing service definition!"
227 unless $old->svcdb eq $new->svcdb;
229 local $SIG{HUP} = 'IGNORE';
230 local $SIG{INT} = 'IGNORE';
231 local $SIG{QUIT} = 'IGNORE';
232 local $SIG{TERM} = 'IGNORE';
233 local $SIG{TSTP} = 'IGNORE';
234 local $SIG{PIPE} = 'IGNORE';
236 my $oldAutoCommit = $FS::UID::AutoCommit;
237 local $FS::UID::AutoCommit = 0;
240 my $error = $new->SUPER::replace( $old );
242 $dbh->rollback if $oldAutoCommit;
246 if ( $compat eq '1.3' ) {
248 # maintain part_svc_column records
250 my $svcdb = $new->svcdb;
252 grep { $_ ne 'svcnum'
253 && defined( $new->getfield($svcdb.'__'.$_.'_flag') )
254 } (fields($svcdb),@fields)
256 my $part_svc_column = $new->part_svc_column($field);
257 my $previous = qsearchs('part_svc_column', {
258 'svcpart' => $new->svcpart,
259 'columnname' => $field,
262 my $flag = $new->getfield($svcdb.'__'.$field.'_flag');
263 if ( uc($flag) =~ /^([DFX])$/ ) {
264 $part_svc_column->setfield('columnflag', $1);
265 $part_svc_column->setfield('columnvalue',
266 $new->getfield($svcdb.'__'.$field)
269 $error = $part_svc_column->replace($previous);
271 $error = $part_svc_column->insert;
274 $error = $previous ? $previous->delete : '';
277 $dbh->rollback if $oldAutoCommit;
282 # maintain export_svc records
286 #false laziness w/ edit/process/agent_type.cgi
287 my @new_export_svc = ();
288 foreach my $part_export ( qsearch('part_export', {}) ) {
289 my $exportnum = $part_export->exportnum;
291 'exportnum' => $exportnum,
292 'svcpart' => $new->svcpart,
294 my $export_svc = qsearchs('export_svc', $hashref);
296 if ( $export_svc && ! $exportnums->{$exportnum} ) {
297 $error = $export_svc->delete;
299 $dbh->rollback if $oldAutoCommit;
302 } elsif ( ! $export_svc && $exportnums->{$exportnum} ) {
303 push @new_export_svc, new FS::export_svc ( $hashref );
308 my $slice = 100/scalar(@new_export_svc) if @new_export_svc;
310 foreach my $export_svc (@new_export_svc) {
311 $error = $export_svc->insert($job, $slice*$done++, $slice);
313 $dbh->rollback if $oldAutoCommit;
317 $error = $job->update_statustext( int( $slice * $done ) );
319 $dbh->rollback if $oldAutoCommit;
328 $dbh->rollback if $oldAutoCommit;
329 return 'non-1.3-COMPAT interface not yet written';
333 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
340 Checks all fields to make sure this is a valid service definition. If there is
341 an error, returns the error, otherwise returns false. Called by the insert
348 my $recref = $self->hashref;
352 $self->ut_numbern('svcpart')
353 || $self->ut_text('svc')
354 || $self->ut_alpha('svcdb')
355 || $self->ut_enum('disabled', [ '', 'Y' ] )
357 return $error if $error;
359 my @fields = eval { fields( $recref->{svcdb} ) }; #might die
360 return "Unknown svcdb!" unless @fields;
365 =item part_svc_column COLUMNNAME
367 Returns the part_svc_column object (see L<FS::part_svc_column>) for the given
368 COLUMNNAME, or a new part_svc_column object if none exists.
372 sub part_svc_column {
373 my( $self, $columnname) = @_;
375 qsearchs('part_svc_column', {
376 'svcpart' => $self->svcpart,
377 'columnname' => $columnname,
379 ) or new FS::part_svc_column {
380 'svcpart' => $self->svcpart,
381 'columnname' => $columnname,
385 =item all_part_svc_column
389 sub all_part_svc_column {
391 qsearch('part_svc_column', { 'svcpart' => $self->svcpart } );
394 =item part_export [ EXPORTTYPE ]
396 Returns all exports (see L<FS::part_export>) for this service, or, if an
397 export type is specified, only returns exports of the given type.
404 $search{'exporttype'} = shift if @_;
405 map { qsearchs('part_export', { 'exportnum' => $_->exportnum, %search } ) }
406 qsearch('export_svc', { 'svcpart' => $self->svcpart } );
411 Returns a list of associated FS::cust_svc records.
417 qsearch('cust_svc', { 'svcpart' => $self->svcpart } );
422 Returns a list of associated FS::svc_* records.
428 map { $_->svc_x } $self->cust_svc;
439 Experimental job-queue processor for web interface adds/edits
443 use Storable qw(thaw);
449 my $param = thaw(decode_base64(shift));
450 warn Dumper($param) if $DEBUG;
452 my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} })
453 if $param->{'svcpart'};
455 $param->{'svc_acct__usergroup'} =
456 ref($param->{'svc_acct__usergroup'})
457 ? join(',', @{$param->{'svc_acct__usergroup'}} )
460 my $new = new FS::part_svc ( {
463 # } qw(svcpart svc svcdb)
464 } ( fields('part_svc'),
465 map { my $svcdb = $_;
466 my @fields = fields($svcdb);
467 push @fields, 'usergroup' if $svcdb eq 'svc_acct'; #kludge
468 map { ( $svcdb.'__'.$_, $svcdb.'__'.$_.'_flag' ) } @fields;
469 } grep defined( dbdef->table($_) ),
470 qw( svc_acct svc_domain svc_forward svc_www svc_broadband )
475 map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
476 qsearch('part_export', {} );
479 if ( $param->{'svcpart'} ) {
480 $error = $new->replace( $old,
487 $error = $new->insert( [ 'usergroup' ],
491 $param->{'svcpart'} = $new->getfield('svcpart');
494 die $error if $error;
499 Delete is unimplemented.
501 The list of svc_* tables is hardcoded. When svc_acct_pop is renamed, this
504 all_part_svc_column method should be documented
508 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
509 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
510 schema.html from the base documentation.