This commit was generated by cvs2svn to compensate for changes in r6252,
[freeside.git] / FS / FS / part_svc.pm
1 package FS::part_svc;
2
3 use strict;
4 use vars qw( @ISA $DEBUG );
5 use Tie::IxHash;
6 use FS::Record qw( qsearch qsearchs fields dbh );
7 use FS::Schema qw( dbdef );
8 use FS::part_svc_column;
9 use FS::part_export;
10 use FS::export_svc;
11 use FS::cust_svc;
12
13 @ISA = qw(FS::Record);
14
15 $DEBUG = 0;
16
17 =head1 NAME
18
19 FS::part_svc - Object methods for part_svc objects
20
21 =head1 SYNOPSIS
22
23   use FS::part_svc;
24
25   $record = new FS::part_svc \%hash
26   $record = new FS::part_svc { 'column' => 'value' };
27
28   $error = $record->insert;
29   $error = $record->insert( [ 'pseudofield' ] );
30   $error = $record->insert( [ 'pseudofield' ], \%exportnums );
31
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 );
35
36   $error = $record->delete;
37
38   $error = $record->check;
39
40 =head1 DESCRIPTION
41
42 An FS::part_svc represents a service definition.  FS::part_svc inherits from
43 FS::Record.  The following fields are currently supported:
44
45 =over 4
46
47 =item svcpart - primary key (assigned automatically for new service definitions)
48
49 =item svc - text name of this service definition
50
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.
53
54 =item disabled - Disabled flag, empty or `Y'
55
56 =back
57
58 =head1 METHODS
59
60 =over 4
61
62 =item new HASHREF
63
64 Creates a new service definition.  To add the service definition to the
65 database, see L<"insert">.
66
67 =cut
68
69 sub table { 'part_svc'; }
70
71 =item insert [ EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ] 
72
73 Adds this service definition to the database.  If there is an error, returns
74 the error, otherwise returns false.
75
76 The following pseudo-fields may be defined, and will be maintained in
77 the part_svc_column table appropriately (see L<FS::part_svc_column>).
78
79 =over 4
80
81 =item I<svcdb>__I<field> - Default or fixed value for I<field> in I<svcdb>.
82
83 =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.
84
85 =back
86
87 If you want to add part_svc_column records for fields that do not exist as
88 (real or virtual) fields in the I<svcdb> table, make sure to list then in 
89 EXTRA_FIELDS_ARRAYREF also.
90
91 If EXPORTNUMS_HASHREF is specified (keys are exportnums and values are
92 boolean), the appopriate export_svc records will be inserted.
93
94 TODOC: JOB
95
96 =cut
97
98 sub insert {
99   my $self = shift;
100   my @fields = ();
101   my @exportnums = ();
102   @fields = @{shift(@_)} if @_;
103   if ( @_ ) {
104     my $exportnums = shift;
105     @exportnums = grep $exportnums->{$_}, keys %$exportnums;
106   }
107   my $job = '';
108   $job = shift if @_;
109
110   local $SIG{HUP} = 'IGNORE';
111   local $SIG{INT} = 'IGNORE';
112   local $SIG{QUIT} = 'IGNORE';
113   local $SIG{TERM} = 'IGNORE';
114   local $SIG{TSTP} = 'IGNORE';
115   local $SIG{PIPE} = 'IGNORE';
116
117   my $oldAutoCommit = $FS::UID::AutoCommit;
118   local $FS::UID::AutoCommit = 0;
119   my $dbh = dbh;
120
121   my $error = $self->SUPER::insert;
122   if ( $error ) {
123     $dbh->rollback if $oldAutoCommit;
124     return $error;
125   }
126
127   # add part_svc_column records
128
129   my $svcdb = $self->svcdb;
130 #  my @rows = map { /^${svcdb}__(.*)$/; $1 }
131 #    grep ! /_flag$/,
132 #      grep /^${svcdb}__/,
133 #        fields('part_svc');
134   foreach my $field (
135     grep { $_ ne 'svcnum'
136            && defined( $self->getfield($svcdb.'__'.$_.'_flag') )
137          } (fields($svcdb), @fields)
138   ) {
139     my $part_svc_column = $self->part_svc_column($field);
140     my $previous = qsearchs('part_svc_column', {
141       'svcpart'    => $self->svcpart,
142       'columnname' => $field,
143     } );
144
145     my $flag = $self->getfield($svcdb.'__'.$field.'_flag');
146     #if ( uc($flag) =~ /^([DFMAX])$/ ) {
147     if ( uc($flag) =~ /^([A-Z])$/ ) { #part_svc_column will test it
148       my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
149                    || sub { shift };
150       $part_svc_column->setfield('columnflag', $1);
151       $part_svc_column->setfield('columnvalue',
152         &$parser($self->getfield($svcdb.'__'.$field))
153       );
154       if ( $previous ) {
155         $error = $part_svc_column->replace($previous);
156       } else {
157         $error = $part_svc_column->insert;
158       }
159     } else {
160       $error = $previous ? $previous->delete : '';
161     }
162     if ( $error ) {
163       $dbh->rollback if $oldAutoCommit;
164       return $error;
165     }
166
167   }
168
169   # add export_svc records
170   my $slice = 100/scalar(@exportnums) if @exportnums;
171   my $done = 0;
172   foreach my $exportnum ( @exportnums ) {
173     my $export_svc = new FS::export_svc ( {
174       'exportnum' => $exportnum,
175       'svcpart'   => $self->svcpart,
176     } );
177     $error = $export_svc->insert($job, $slice*$done++, $slice);
178     if ( $error ) {
179       $dbh->rollback if $oldAutoCommit;
180       return $error;
181     }
182   }
183
184   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
185
186   '';
187 }
188
189 =item delete
190
191 Currently unimplemented.  Set the "disabled" field instead.
192
193 =cut
194
195 sub delete {
196   return "Can't (yet?) delete service definitions.";
197 # check & make sure the svcpart isn't in cust_svc or pkg_svc (in any packages)?
198 }
199
200 =item replace OLD_RECORD [ '1.3-COMPAT' [ , EXTRA_FIELDS_ARRAYREF [ , EXPORTNUMS_HASHREF [ , JOB ] ] ] ]
201
202 Replaces OLD_RECORD with this one in the database.  If there is an error,
203 returns the error, otherwise returns false.
204
205 TODOC: 1.3-COMPAT
206
207 TODOC: EXTRA_FIELDS_ARRAYREF (same as insert method)
208
209 TODOC: JOB
210
211 =cut
212
213 sub replace {
214   my ( $new, $old ) = ( shift, shift );
215   my $compat = '';
216   my @fields = ();
217   my $exportnums;
218   my $job = '';
219   if ( @_ && $_[0] eq '1.3-COMPAT' ) {
220     shift;
221     $compat = '1.3';
222     @fields = @{shift(@_)} if @_;
223     $exportnums = @_ ? shift : '';
224     $job = shift if @_;
225   } else {
226     return 'non-1.3-COMPAT interface not yet written';
227     #not yet implemented
228   }
229
230   return "Can't change svcdb for an existing service definition!"
231     unless $old->svcdb eq $new->svcdb;
232
233   local $SIG{HUP} = 'IGNORE';
234   local $SIG{INT} = 'IGNORE';
235   local $SIG{QUIT} = 'IGNORE';
236   local $SIG{TERM} = 'IGNORE';
237   local $SIG{TSTP} = 'IGNORE';
238   local $SIG{PIPE} = 'IGNORE';
239
240   my $oldAutoCommit = $FS::UID::AutoCommit;
241   local $FS::UID::AutoCommit = 0;
242   my $dbh = dbh;
243
244   my $error = $new->SUPER::replace( $old );
245   if ( $error ) {
246     $dbh->rollback if $oldAutoCommit;
247     return $error;
248   }
249
250   if ( $compat eq '1.3' ) {
251
252    # maintain part_svc_column records
253
254     my $svcdb = $new->svcdb;
255     foreach my $field (
256       grep { $_ ne 'svcnum'
257              && defined( $new->getfield($svcdb.'__'.$_.'_flag') )
258            } (fields($svcdb),@fields)
259     ) {
260       my $part_svc_column = $new->part_svc_column($field);
261       my $previous = qsearchs('part_svc_column', {
262         'svcpart'    => $new->svcpart,
263         'columnname' => $field,
264       } );
265
266       my $flag = $new->getfield($svcdb.'__'.$field.'_flag');
267       #if ( uc($flag) =~ /^([DFMAX])$/ ) {
268       if ( uc($flag) =~ /^([A-Z])$/ ) { #part_svc_column will test it
269         my $parser = FS::part_svc->svc_table_fields($svcdb)->{$field}->{parse}
270                      || sub { shift };
271         $part_svc_column->setfield('columnflag', $1);
272         $part_svc_column->setfield('columnvalue',
273           &$parser($new->getfield($svcdb.'__'.$field))
274         );
275         if ( $previous ) {
276           $error = $part_svc_column->replace($previous);
277         } else {
278           $error = $part_svc_column->insert;
279         }
280       } else {
281         $error = $previous ? $previous->delete : '';
282       }
283       if ( $error ) {
284         $dbh->rollback if $oldAutoCommit;
285         return $error;
286       }
287     }
288
289     # maintain export_svc records
290
291     if ( $exportnums ) {
292
293       #false laziness w/ edit/process/agent_type.cgi
294       my @new_export_svc = ();
295       foreach my $part_export ( qsearch('part_export', {}) ) {
296         my $exportnum = $part_export->exportnum;
297         my $hashref = {
298           'exportnum' => $exportnum,
299           'svcpart'   => $new->svcpart,
300         };
301         my $export_svc = qsearchs('export_svc', $hashref);
302
303         if ( $export_svc && ! $exportnums->{$exportnum} ) {
304           $error = $export_svc->delete;
305           if ( $error ) {
306             $dbh->rollback if $oldAutoCommit;
307             return $error;
308           }
309         } elsif ( ! $export_svc && $exportnums->{$exportnum} ) {
310           push @new_export_svc, new FS::export_svc ( $hashref );
311         }
312
313       }
314
315       my $slice = 100/scalar(@new_export_svc) if @new_export_svc;
316       my $done = 0;
317       foreach my $export_svc (@new_export_svc) {
318         $error = $export_svc->insert($job, $slice*$done++, $slice);
319         if ( $error ) {
320           $dbh->rollback if $oldAutoCommit;
321           return $error;
322         }
323         if ( $job ) {
324           $error = $job->update_statustext( int( $slice * $done ) );
325           if ( $error ) {
326             $dbh->rollback if $oldAutoCommit;
327             return $error;
328           }
329         }
330       }
331
332     }
333
334   } else {
335     $dbh->rollback if $oldAutoCommit;
336     return 'non-1.3-COMPAT interface not yet written';
337     #not yet implemented
338   }
339
340   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
341
342   '';
343 }
344
345 =item check
346
347 Checks all fields to make sure this is a valid service definition.  If there is
348 an error, returns the error, otherwise returns false.  Called by the insert
349 and replace methods.
350
351 =cut
352
353 sub check {
354   my $self = shift;
355
356   my $error;
357   $error=
358     $self->ut_numbern('svcpart')
359     || $self->ut_text('svc')
360     || $self->ut_alpha('svcdb')
361     || $self->ut_enum('disabled', [ '', 'Y' ] )
362   ;
363   return $error if $error;
364
365   my @fields = eval { fields( $self->svcdb ) }; #might die
366   return "Unknown svcdb: ". $self->svcdb. " (Error: $@)"
367     unless @fields;
368
369   $self->SUPER::check;
370 }
371
372 =item part_svc_column COLUMNNAME
373
374 Returns the part_svc_column object (see L<FS::part_svc_column>) for the given
375 COLUMNNAME, or a new part_svc_column object if none exists.
376
377 =cut
378
379 sub part_svc_column {
380   my( $self, $columnname) = @_;
381   $self->svcpart &&
382     qsearchs('part_svc_column',  {
383                                    'svcpart'    => $self->svcpart,
384                                    'columnname' => $columnname,
385                                  }
386   ) or new FS::part_svc_column {
387                                  'svcpart'    => $self->svcpart,
388                                  'columnname' => $columnname,
389                                };
390 }
391
392 =item all_part_svc_column
393
394 =cut
395
396 sub all_part_svc_column {
397   my $self = shift;
398   qsearch('part_svc_column', { 'svcpart' => $self->svcpart } );
399 }
400
401 =item part_export [ EXPORTTYPE ]
402
403 Returns a list of all exports (see L<FS::part_export>) for this service, or,
404 if an export type is specified, only returns exports of the given type.
405
406 =cut
407
408 sub part_export {
409   my $self = shift;
410   my %search;
411   $search{'exporttype'} = shift if @_;
412   map { qsearchs('part_export', { 'exportnum' => $_->exportnum, %search } ) }
413     qsearch('export_svc', { 'svcpart' => $self->svcpart } );
414 }
415
416 =item part_export_usage
417
418 Returns a list of any exports (see L<FS::part_export>) for this service that
419 are capable of reporting usage information.
420
421 =cut
422
423 sub part_export_usage {
424   my $self = shift;
425   grep $_->can('usage_sessions'), $self->part_export;
426 }
427
428 =item cust_svc [ PKGPART ] 
429
430 Returns a list of associated customer services (FS::cust_svc records).
431
432 If a PKGPART is specified, returns the customer services which are contained
433 within packages of that type (see L<FS::part_pkg>).  If PKGPARTis specified as
434 B<0>, returns unlinked customer services.
435
436 =cut
437
438 sub cust_svc {
439   my $self = shift;
440
441   my $hashref = { 'svcpart' => $self->svcpart };
442
443   my( $addl_from, $extra_sql ) = ( '', '' );
444   if ( @_ ) {
445     my $pkgpart = shift;
446     if ( $pkgpart =~ /^(\d+)$/ ) {
447       $addl_from = 'LEFT JOIN cust_pkg USING ( pkgnum )';
448       $extra_sql = "AND pkgpart = $1";
449     } elsif ( $pkgpart eq '0' ) {
450       $hashref->{'pkgnum'} = '';
451     }
452   }
453
454   qsearch({
455     'table'     => 'cust_svc',
456     'addl_from' => $addl_from,
457     'hashref'   => $hashref,
458     'extra_sql' => $extra_sql,
459   });
460 }
461
462 =item num_cust_svc [ PKGPART ] 
463
464 Returns the number of associated customer services (FS::cust_svc records).
465
466 If a PKGPART is specified, returns the number of customer services which are
467 contained within packages of that type (see L<FS::part_pkg>).  If PKGPART
468 is specified as B<0>, returns the number of unlinked customer services.
469
470 =cut
471
472 sub num_cust_svc {
473   my $self = shift;
474
475   my @param = ( $self->svcpart );
476
477   my( $join, $and ) = ( '', '' );
478   if ( @_ ) {
479     my $pkgpart = shift;
480     if ( $pkgpart ) {
481       $join = 'LEFT JOIN cust_pkg USING ( pkgnum )';
482       $and = 'AND pkgpart = ?';
483       push @param, $pkgpart;
484     } elsif ( $pkgpart eq '0' ) {
485       $and = 'AND pkgnum IS NULL';
486     }
487   }
488
489   my $sth = dbh->prepare(
490     "SELECT COUNT(*) FROM cust_svc $join WHERE svcpart = ? $and"
491   ) or die dbh->errstr;
492   $sth->execute(@param)
493     or die $sth->errstr;
494   $sth->fetchrow_arrayref->[0];
495 }
496
497 =item svc_x
498
499 Returns a list of associated FS::svc_* records.
500
501 =cut
502
503 sub svc_x {
504   my $self = shift;
505   map { $_->svc_x } $self->cust_svc;
506 }
507
508 =back
509
510 =head1 CLASS METHODS
511
512 =over 4
513
514 =cut
515
516 my $svc_defs;
517 sub _svc_defs {
518
519   return $svc_defs if $svc_defs; #cache
520
521   my $conf = new FS::Conf;
522
523   #false laziness w/part_pkg.pm::plan_info
524
525   my %info;
526   foreach my $INC ( @INC ) {
527     warn "globbing $INC/FS/svc_*.pm\n" if $DEBUG;
528     foreach my $file ( glob("$INC/FS/svc_*.pm") ) {
529
530       warn "attempting to load service table info from $file\n" if $DEBUG;
531       $file =~ /\/(\w+)\.pm$/ or do {
532         warn "unrecognized file in $INC/FS/: $file\n";
533         next;
534       };
535       my $mod = $1;
536
537       if ( $mod =~ /^svc_[A-Z]/ or $mod =~ /^svc_acct_pop$/ ) {
538         warn "skipping FS::$mod" if $DEBUG;
539         next;
540       }
541
542       eval "use FS::$mod;";
543       if ( $@ ) {
544         die "error using FS::$mod (skipping): $@\n" if $@;
545         next;
546       }
547       unless ( UNIVERSAL::can("FS::$mod", 'table_info') ) {
548         warn "FS::$mod has no table_info method; skipping";
549         next;
550       }
551
552       my $info = "FS::$mod"->table_info;
553       unless ( keys %$info ) {
554         warn "FS::$mod->table_info doesn't return info, skipping\n";
555         next;
556       }
557       warn "got info from FS::$mod: $info\n" if $DEBUG;
558       if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
559         warn "skipping disabled service FS::$mod" if $DEBUG;
560         next;
561       }
562       $info{$mod} = $info;
563     }
564   }
565
566   tie my %svc_defs, 'Tie::IxHash', 
567     map  { $_ => $info{$_}->{'fields'} }
568     sort { $info{$a}->{'display_weight'} <=> $info{$b}->{'display_weight'} }
569     keys %info,
570   ;
571   
572   # yuck.  maybe this won't be so bad when virtual fields become real fields
573   my %vfields;
574   foreach my $svcdb (grep dbdef->table($_), keys %svc_defs ) {
575     eval "use FS::$svcdb;";
576     my $self = "FS::$svcdb"->new;
577     $vfields{$svcdb} = {};
578     foreach my $field ($self->virtual_fields) { # svc_Common::virtual_fields with a null svcpart returns all of them
579       my $pvf = $self->pvf($field);
580       my @list = $pvf->list;
581       if (scalar @list) {
582         $svc_defs{$svcdb}->{$field} = { desc        => $pvf->label,
583                                         type        => 'select',
584                                         select_list => \@list };
585       } else {
586         $svc_defs{$svcdb}->{$field} = $pvf->label;
587       } #endif
588       $vfields{$svcdb}->{$field} = $pvf;
589       warn "\$vfields{$svcdb}->{$field} = $pvf"
590         if $DEBUG;
591     } #next $field
592   } #next $svcdb
593   
594   $svc_defs = \%svc_defs; #cache
595   
596 }
597
598 =item svc_tables
599
600 Returns a list of all svc_ tables.
601
602 =cut
603
604 sub svc_tables {
605   my $class = shift;
606   my $svc_defs = $class->_svc_defs;
607   grep { defined( dbdef->table($_) ) } keys %$svc_defs;
608 }
609
610 =item svc_table_fields TABLE
611
612 Given a table name, returns a hashref of field names.  The field names
613 returned are those with additional (service-definition related) information,
614 not necessarily all database fields of the table.  Pseudo-fields may also
615 be returned (i.e. svc_acct.usergroup).
616
617 Each value of the hashref is another hashref, which can have one or more of
618 the following keys:
619
620 =over 4
621
622 =item label - Description of the field
623
624 =item def_label - Optional description of the field in the context of service definitions
625
626 =item type - Currently "text", "select", "disabled", or "radius_usergroup_selector"
627
628 =item disable_default - This field should not allow a default value in service definitions
629
630 =item disable_fixed - This field should not allow a fixed value in service definitions
631
632 =item disable_inventory - This field should not allow inventory values in service definitions
633
634 =item select_list - If type is "text", this can be a listref of possible values.
635
636 =item select_table - An alternative to select_list, this defines a database table with the possible choices.
637
638 =item select_key - Used with select_table, this is the field name of keys
639
640 =item select_label - Used with select_table, this is the field name of labels
641
642 =back
643
644 =cut
645
646 #maybe this should move and be a class method in svc_Common.pm
647 sub svc_table_fields {
648   my($class, $table) = @_;
649   my $svc_defs = $class->_svc_defs;
650   my $def = $svc_defs->{$table};
651
652   foreach ( grep !ref($def->{$_}), keys %$def ) {
653
654     #normalize the shortcut in %info hash
655     $def->{$_} = { 'label' => $def->{$_} };
656
657     $def->{$_}{'type'} ||= 'text';
658
659   }
660
661   $def;
662 }
663
664 =back
665
666 =head1 SUBROUTINES
667
668 =over 4
669
670 =item process
671
672 Job-queue processor for web interface adds/edits
673
674 =cut
675
676 use Storable qw(thaw);
677 use Data::Dumper;
678 use MIME::Base64;
679 sub process {
680   my $job = shift;
681
682   my $param = thaw(decode_base64(shift));
683   warn Dumper($param) if $DEBUG;
684
685   my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} }) 
686     if $param->{'svcpart'};
687
688   $param->{'svc_acct__usergroup'} =
689     ref($param->{'svc_acct__usergroup'})
690       ? join(',', @{$param->{'svc_acct__usergroup'}} )
691       : $param->{'svc_acct__usergroup'};
692   
693   my $new = new FS::part_svc ( {
694     map {
695       $_ => $param->{$_};
696   #  } qw(svcpart svc svcdb)
697     } ( fields('part_svc'),
698         map { my $svcdb = $_;
699               my @fields = fields($svcdb);
700               push @fields, 'usergroup' if $svcdb eq 'svc_acct'; #kludge
701
702               map {
703                     if ( $param->{ $svcdb.'__'.$_.'_flag' } =~ /^[MA]$/ ) {
704                       $param->{ $svcdb.'__'.$_ } =
705                         delete( $param->{ $svcdb.'__'.$_.'_classnum' } );
706                     }
707                     if ( $param->{ $svcdb.'__'.$_.'_flag' } =~ /^S$/ ) {
708                       $param->{ $svcdb.'__'.$_} =
709                         ref($param->{ $svcdb.'__'.$_})
710                           ? join(',', @{$param->{ $svcdb.'__'.$_ }} )
711                           : $param->{ $svcdb.'__'.$_ };
712                     }
713                     ( $svcdb.'__'.$_, $svcdb.'__'.$_.'_flag' );
714                   }
715                   @fields;
716
717             } FS::part_svc->svc_tables()
718       )
719   } );
720   
721   my %exportnums =
722     map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
723         qsearch('part_export', {} );
724
725   my $error;
726   if ( $param->{'svcpart'} ) {
727     $error = $new->replace( $old,
728                             '1.3-COMPAT',
729                             [ 'usergroup' ],
730                             \%exportnums,
731                             $job
732                           );
733   } else {
734     $error = $new->insert( [ 'usergroup' ],
735                            \%exportnums,
736                            $job,
737                          );
738     $param->{'svcpart'} = $new->getfield('svcpart');
739   }
740
741   die "$error\n" if $error;
742 }
743
744 =item process_bulk_cust_svc
745
746 Job-queue processor for web interface bulk customer service changes
747
748 =cut
749
750 use Storable qw(thaw);
751 use Data::Dumper;
752 use MIME::Base64;
753 sub process_bulk_cust_svc {
754   my $job = shift;
755
756   my $param = thaw(decode_base64(shift));
757   warn Dumper($param) if $DEBUG;
758
759   my $old_part_svc =
760     qsearchs('part_svc', { 'svcpart' => $param->{'old_svcpart'} } );
761
762   die "Must select a new service definition\n" unless $param->{'new_svcpart'};
763
764   #the rest should be abstracted out to to its own subroutine?
765
766   local $SIG{HUP} = 'IGNORE';
767   local $SIG{INT} = 'IGNORE';
768   local $SIG{QUIT} = 'IGNORE';
769   local $SIG{TERM} = 'IGNORE';
770   local $SIG{TSTP} = 'IGNORE';
771   local $SIG{PIPE} = 'IGNORE';
772
773   my $oldAutoCommit = $FS::UID::AutoCommit;
774   local $FS::UID::AutoCommit = 0;
775   my $dbh = dbh;
776
777   local( $FS::cust_svc::ignore_quantity ) = 1;
778
779   my $total = $old_part_svc->num_cust_svc( $param->{'pkgpart'} );
780
781   my $n = 0;
782   foreach my $old_cust_svc ( $old_part_svc->cust_svc( $param->{'pkgpart'} ) ) {
783
784     my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
785
786     $new_cust_svc->svcpart( $param->{'new_svcpart'} );
787     my $error = $new_cust_svc->replace($old_cust_svc);
788     if ( $error ) {
789       $dbh->rollback if $oldAutoCommit;
790       die "$error\n" if $error;
791     }
792
793     $error = $job->update_statustext( int( 100 * ++$n / $total ) );
794     if ( $error ) {
795       $dbh->rollback if $oldAutoCommit;
796       die $error if $error;
797     }
798
799   }
800
801   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
802
803   '';
804
805 }
806
807 =head1 BUGS
808
809 Delete is unimplemented.
810
811 The list of svc_* tables is no longer hardcoded, but svc_acct_pop is skipped
812 as a special case until it is renamed.
813
814 all_part_svc_column methods should be documented
815
816 =head1 SEE ALSO
817
818 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
819 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
820 schema.html from the base documentation.
821
822 =cut
823
824 1;
825