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 or empty (no default), `D' for default, `F' for fixed (unchangeable), `M' for manual selection from inventory, or `A' for automatic selection from inventory. 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) =~ /^([DFMAX])$/ ) {
146 if ( uc($flag) =~ /^([A-Z])$/ ) { #part_svc_column will test it
147 $part_svc_column->setfield('columnflag', $1);
148 $part_svc_column->setfield('columnvalue',
149 $self->getfield($svcdb.'__'.$field)
152 $error = $part_svc_column->replace($previous);
154 $error = $part_svc_column->insert;
157 $error = $previous ? $previous->delete : '';
160 $dbh->rollback if $oldAutoCommit;
166 # add export_svc records
167 my $slice = 100/scalar(@exportnums) if @exportnums;
169 foreach my $exportnum ( @exportnums ) {
170 my $export_svc = new FS::export_svc ( {
171 'exportnum' => $exportnum,
172 'svcpart' => $self->svcpart,
174 $error = $export_svc->insert($job, $slice*$done++, $slice);
176 $dbh->rollback if $oldAutoCommit;
181 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
188 Currently unimplemented. Set the "disabled" field instead.
193 return "Can't (yet?) delete service definitions.";
194 # check & make sure the svcpart isn't in cust_svc or pkg_svc (in any packages)?
197 =item replace OLD_RECORD [ '1.3-COMPAT' [ , EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ] ]
199 Replaces OLD_RECORD with this one in the database. If there is an error,
200 returns the error, otherwise returns false.
204 TODOC: EXTRA_FIELDS_ARRAYREF (same as insert method)
211 my ( $new, $old ) = ( shift, shift );
216 if ( @_ && $_[0] eq '1.3-COMPAT' ) {
219 @fields = @{shift(@_)} if @_;
220 $exportnums = @_ ? shift : '';
223 return 'non-1.3-COMPAT interface not yet written';
227 return "Can't change svcdb for an existing service definition!"
228 unless $old->svcdb eq $new->svcdb;
230 local $SIG{HUP} = 'IGNORE';
231 local $SIG{INT} = 'IGNORE';
232 local $SIG{QUIT} = 'IGNORE';
233 local $SIG{TERM} = 'IGNORE';
234 local $SIG{TSTP} = 'IGNORE';
235 local $SIG{PIPE} = 'IGNORE';
237 my $oldAutoCommit = $FS::UID::AutoCommit;
238 local $FS::UID::AutoCommit = 0;
241 my $error = $new->SUPER::replace( $old );
243 $dbh->rollback if $oldAutoCommit;
247 if ( $compat eq '1.3' ) {
249 # maintain part_svc_column records
251 my $svcdb = $new->svcdb;
253 grep { $_ ne 'svcnum'
254 && defined( $new->getfield($svcdb.'__'.$_.'_flag') )
255 } (fields($svcdb),@fields)
257 my $part_svc_column = $new->part_svc_column($field);
258 my $previous = qsearchs('part_svc_column', {
259 'svcpart' => $new->svcpart,
260 'columnname' => $field,
263 my $flag = $new->getfield($svcdb.'__'.$field.'_flag');
264 #if ( uc($flag) =~ /^([DFMAX])$/ ) {
265 if ( uc($flag) =~ /^([A-Z])$/ ) { #part_svc_column will test it
266 $part_svc_column->setfield('columnflag', $1);
267 $part_svc_column->setfield('columnvalue',
268 $new->getfield($svcdb.'__'.$field)
271 $error = $part_svc_column->replace($previous);
273 $error = $part_svc_column->insert;
276 $error = $previous ? $previous->delete : '';
279 $dbh->rollback if $oldAutoCommit;
284 # maintain export_svc records
288 #false laziness w/ edit/process/agent_type.cgi
289 my @new_export_svc = ();
290 foreach my $part_export ( qsearch('part_export', {}) ) {
291 my $exportnum = $part_export->exportnum;
293 'exportnum' => $exportnum,
294 'svcpart' => $new->svcpart,
296 my $export_svc = qsearchs('export_svc', $hashref);
298 if ( $export_svc && ! $exportnums->{$exportnum} ) {
299 $error = $export_svc->delete;
301 $dbh->rollback if $oldAutoCommit;
304 } elsif ( ! $export_svc && $exportnums->{$exportnum} ) {
305 push @new_export_svc, new FS::export_svc ( $hashref );
310 my $slice = 100/scalar(@new_export_svc) if @new_export_svc;
312 foreach my $export_svc (@new_export_svc) {
313 $error = $export_svc->insert($job, $slice*$done++, $slice);
315 $dbh->rollback if $oldAutoCommit;
319 $error = $job->update_statustext( int( $slice * $done ) );
321 $dbh->rollback if $oldAutoCommit;
330 $dbh->rollback if $oldAutoCommit;
331 return 'non-1.3-COMPAT interface not yet written';
335 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
342 Checks all fields to make sure this is a valid service definition. If there is
343 an error, returns the error, otherwise returns false. Called by the insert
353 $self->ut_numbern('svcpart')
354 || $self->ut_text('svc')
355 || $self->ut_alpha('svcdb')
356 || $self->ut_enum('disabled', [ '', 'Y' ] )
358 return $error if $error;
360 my @fields = eval { fields( $self->svcdb ) }; #might die
361 return "Unknown svcdb: ". $self->svcdb. " (Error: $@)"
367 =item part_svc_column COLUMNNAME
369 Returns the part_svc_column object (see L<FS::part_svc_column>) for the given
370 COLUMNNAME, or a new part_svc_column object if none exists.
374 sub part_svc_column {
375 my( $self, $columnname) = @_;
377 qsearchs('part_svc_column', {
378 'svcpart' => $self->svcpart,
379 'columnname' => $columnname,
381 ) or new FS::part_svc_column {
382 'svcpart' => $self->svcpart,
383 'columnname' => $columnname,
387 =item all_part_svc_column
391 sub all_part_svc_column {
393 qsearch('part_svc_column', { 'svcpart' => $self->svcpart } );
396 =item part_export [ EXPORTTYPE ]
398 Returns a list of all exports (see L<FS::part_export>) for this service, or,
399 if an export type is specified, only returns exports of the given type.
406 $search{'exporttype'} = shift if @_;
407 map { qsearchs('part_export', { 'exportnum' => $_->exportnum, %search } ) }
408 qsearch('export_svc', { 'svcpart' => $self->svcpart } );
411 =item part_export_usage
413 Returns a list of any exports (see L<FS::part_export>) for this service that
414 are capable of reporting usage information.
418 sub part_export_usage {
420 grep $_->can('usage_sessions'), $self->part_export;
423 =item cust_svc [ PKGPART ]
425 Returns a list of associated customer services (FS::cust_svc records).
427 If a PKGPART is specified, returns the customer services which are contained
428 within packages of that type (see L<FS::part_pkg>). If PKGPARTis specified as
429 B<0>, returns unlinked customer services.
436 my $hashref = { 'svcpart' => $self->svcpart };
438 my( $addl_from, $extra_sql ) = ( '', '' );
441 if ( $pkgpart =~ /^(\d+)$/ ) {
442 $addl_from = 'LEFT JOIN cust_pkg USING ( pkgnum )';
443 $extra_sql = "AND pkgpart = $1";
444 } elsif ( $pkgpart eq '0' ) {
445 $hashref->{'pkgnum'} = '';
450 'table' => 'cust_svc',
451 'addl_from' => $addl_from,
452 'hashref' => $hashref,
453 'extra_sql' => $extra_sql,
457 =item num_cust_svc [ PKGPART ]
459 Returns the number of associated customer services (FS::cust_svc records).
461 If a PKGPART is specified, returns the number of customer services which are
462 contained within packages of that type (see L<FS::part_pkg>). If PKGPART
463 is specified as B<0>, returns the number of unlinked customer services.
470 my @param = ( $self->svcpart );
472 my( $join, $and ) = ( '', '' );
476 $join = 'LEFT JOIN cust_pkg USING ( pkgnum )';
477 $and = 'AND pkgpart = ?';
478 push @param, $pkgpart;
479 } elsif ( $pkgpart eq '0' ) {
480 $and = 'AND pkgnum IS NULL';
484 my $sth = dbh->prepare(
485 "SELECT COUNT(*) FROM cust_svc $join WHERE svcpart = ? $and"
486 ) or die dbh->errstr;
487 $sth->execute(@param)
489 $sth->fetchrow_arrayref->[0];
494 Returns a list of associated FS::svc_* records.
500 map { $_->svc_x } $self->cust_svc;
512 Job-queue processor for web interface adds/edits
516 use Storable qw(thaw);
522 my $param = thaw(decode_base64(shift));
523 warn Dumper($param) if $DEBUG;
525 my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} })
526 if $param->{'svcpart'};
528 $param->{'svc_acct__usergroup'} =
529 ref($param->{'svc_acct__usergroup'})
530 ? join(',', @{$param->{'svc_acct__usergroup'}} )
531 : $param->{'svc_acct__usergroup'};
533 my $new = new FS::part_svc ( {
536 # } qw(svcpart svc svcdb)
537 } ( fields('part_svc'),
538 map { my $svcdb = $_;
539 my @fields = fields($svcdb);
540 push @fields, 'usergroup' if $svcdb eq 'svc_acct'; #kludge
543 if ( $param->{ $svcdb.'__'.$_.'_flag' } =~ /^[MA]$/ ) {
544 $param->{ $svcdb.'__'.$_ } =
545 delete( $param->{ $svcdb.'__'.$_.'_classnum' } );
547 ( $svcdb.'__'.$_, $svcdb.'__'.$_.'_flag' );
551 } grep defined( dbdef->table($_) ),
552 qw( svc_acct svc_domain svc_forward svc_www svc_broadband
553 svc_phone svc_external
559 map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
560 qsearch('part_export', {} );
563 if ( $param->{'svcpart'} ) {
564 $error = $new->replace( $old,
571 $error = $new->insert( [ 'usergroup' ],
575 $param->{'svcpart'} = $new->getfield('svcpart');
578 die "$error\n" if $error;
581 =item process_bulk_cust_svc
583 Job-queue processor for web interface bulk customer service changes
587 use Storable qw(thaw);
590 sub process_bulk_cust_svc {
593 my $param = thaw(decode_base64(shift));
594 warn Dumper($param) if $DEBUG;
597 qsearchs('part_svc', { 'svcpart' => $param->{'old_svcpart'} } );
599 die "Must select a new service definition\n" unless $param->{'new_svcpart'};
601 #the rest should be abstracted out to to its own subroutine?
603 local $SIG{HUP} = 'IGNORE';
604 local $SIG{INT} = 'IGNORE';
605 local $SIG{QUIT} = 'IGNORE';
606 local $SIG{TERM} = 'IGNORE';
607 local $SIG{TSTP} = 'IGNORE';
608 local $SIG{PIPE} = 'IGNORE';
610 my $oldAutoCommit = $FS::UID::AutoCommit;
611 local $FS::UID::AutoCommit = 0;
614 local( $FS::cust_svc::ignore_quantity ) = 1;
616 my $total = $old_part_svc->num_cust_svc( $param->{'pkgpart'} );
619 foreach my $old_cust_svc ( $old_part_svc->cust_svc( $param->{'pkgpart'} ) ) {
621 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
623 $new_cust_svc->svcpart( $param->{'new_svcpart'} );
624 my $error = $new_cust_svc->replace($old_cust_svc);
626 $dbh->rollback if $oldAutoCommit;
627 die "$error\n" if $error;
630 $error = $job->update_statustext( int( 100 * ++$n / $total ) );
632 $dbh->rollback if $oldAutoCommit;
633 die $error if $error;
638 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
646 Delete is unimplemented.
648 The list of svc_* tables is hardcoded. When svc_acct_pop is renamed, this
651 all_part_svc_column methods should be documented
655 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
656 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
657 schema.html from the base documentation.