get DIDs from globalpops
[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 part_export_did
429
430 Returns a list of any exports (see L<FS::part_export>) for this service that
431 are capable of returing available DID (phone number) information.
432
433 =cut
434
435 sub part_export_did {
436   my $self = shift;
437   grep $_->can('get_dids'), $self->part_export;
438 }
439
440
441 =item cust_svc [ PKGPART ] 
442
443 Returns a list of associated customer services (FS::cust_svc records).
444
445 If a PKGPART is specified, returns the customer services which are contained
446 within packages of that type (see L<FS::part_pkg>).  If PKGPARTis specified as
447 B<0>, returns unlinked customer services.
448
449 =cut
450
451 sub cust_svc {
452   my $self = shift;
453
454   my $hashref = { 'svcpart' => $self->svcpart };
455
456   my( $addl_from, $extra_sql ) = ( '', '' );
457   if ( @_ ) {
458     my $pkgpart = shift;
459     if ( $pkgpart =~ /^(\d+)$/ ) {
460       $addl_from = 'LEFT JOIN cust_pkg USING ( pkgnum )';
461       $extra_sql = "AND pkgpart = $1";
462     } elsif ( $pkgpart eq '0' ) {
463       $hashref->{'pkgnum'} = '';
464     }
465   }
466
467   qsearch({
468     'table'     => 'cust_svc',
469     'addl_from' => $addl_from,
470     'hashref'   => $hashref,
471     'extra_sql' => $extra_sql,
472   });
473 }
474
475 =item num_cust_svc [ PKGPART ] 
476
477 Returns the number of associated customer services (FS::cust_svc records).
478
479 If a PKGPART is specified, returns the number of customer services which are
480 contained within packages of that type (see L<FS::part_pkg>).  If PKGPART
481 is specified as B<0>, returns the number of unlinked customer services.
482
483 =cut
484
485 sub num_cust_svc {
486   my $self = shift;
487
488   my @param = ( $self->svcpart );
489
490   my( $join, $and ) = ( '', '' );
491   if ( @_ ) {
492     my $pkgpart = shift;
493     if ( $pkgpart ) {
494       $join = 'LEFT JOIN cust_pkg USING ( pkgnum )';
495       $and = 'AND pkgpart = ?';
496       push @param, $pkgpart;
497     } elsif ( $pkgpart eq '0' ) {
498       $and = 'AND pkgnum IS NULL';
499     }
500   }
501
502   my $sth = dbh->prepare(
503     "SELECT COUNT(*) FROM cust_svc $join WHERE svcpart = ? $and"
504   ) or die dbh->errstr;
505   $sth->execute(@param)
506     or die $sth->errstr;
507   $sth->fetchrow_arrayref->[0];
508 }
509
510 =item svc_x
511
512 Returns a list of associated FS::svc_* records.
513
514 =cut
515
516 sub svc_x {
517   my $self = shift;
518   map { $_->svc_x } $self->cust_svc;
519 }
520
521 =back
522
523 =head1 CLASS METHODS
524
525 =over 4
526
527 =cut
528
529 my $svc_defs;
530 sub _svc_defs {
531
532   return $svc_defs if $svc_defs; #cache
533
534   my $conf = new FS::Conf;
535
536   #false laziness w/part_pkg.pm::plan_info
537
538   my %info;
539   foreach my $INC ( @INC ) {
540     warn "globbing $INC/FS/svc_*.pm\n" if $DEBUG;
541     foreach my $file ( glob("$INC/FS/svc_*.pm") ) {
542
543       warn "attempting to load service table info from $file\n" if $DEBUG;
544       $file =~ /\/(\w+)\.pm$/ or do {
545         warn "unrecognized file in $INC/FS/: $file\n";
546         next;
547       };
548       my $mod = $1;
549
550       if ( $mod =~ /^svc_[A-Z]/ or $mod =~ /^svc_acct_pop$/ ) {
551         warn "skipping FS::$mod" if $DEBUG;
552         next;
553       }
554
555       eval "use FS::$mod;";
556       if ( $@ ) {
557         die "error using FS::$mod (skipping): $@\n" if $@;
558         next;
559       }
560       unless ( UNIVERSAL::can("FS::$mod", 'table_info') ) {
561         warn "FS::$mod has no table_info method; skipping";
562         next;
563       }
564
565       my $info = "FS::$mod"->table_info;
566       unless ( keys %$info ) {
567         warn "FS::$mod->table_info doesn't return info, skipping\n";
568         next;
569       }
570       warn "got info from FS::$mod: $info\n" if $DEBUG;
571       if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
572         warn "skipping disabled service FS::$mod" if $DEBUG;
573         next;
574       }
575       $info{$mod} = $info;
576     }
577   }
578
579   tie my %svc_defs, 'Tie::IxHash', 
580     map  { $_ => $info{$_}->{'fields'} }
581     sort { $info{$a}->{'display_weight'} <=> $info{$b}->{'display_weight'} }
582     keys %info,
583   ;
584   
585   # yuck.  maybe this won't be so bad when virtual fields become real fields
586   my %vfields;
587   foreach my $svcdb (grep dbdef->table($_), keys %svc_defs ) {
588     eval "use FS::$svcdb;";
589     my $self = "FS::$svcdb"->new;
590     $vfields{$svcdb} = {};
591     foreach my $field ($self->virtual_fields) { # svc_Common::virtual_fields with a null svcpart returns all of them
592       my $pvf = $self->pvf($field);
593       my @list = $pvf->list;
594       if (scalar @list) {
595         $svc_defs{$svcdb}->{$field} = { desc        => $pvf->label,
596                                         type        => 'select',
597                                         select_list => \@list };
598       } else {
599         $svc_defs{$svcdb}->{$field} = $pvf->label;
600       } #endif
601       $vfields{$svcdb}->{$field} = $pvf;
602       warn "\$vfields{$svcdb}->{$field} = $pvf"
603         if $DEBUG;
604     } #next $field
605   } #next $svcdb
606   
607   $svc_defs = \%svc_defs; #cache
608   
609 }
610
611 =item svc_tables
612
613 Returns a list of all svc_ tables.
614
615 =cut
616
617 sub svc_tables {
618   my $class = shift;
619   my $svc_defs = $class->_svc_defs;
620   grep { defined( dbdef->table($_) ) } keys %$svc_defs;
621 }
622
623 =item svc_table_fields TABLE
624
625 Given a table name, returns a hashref of field names.  The field names
626 returned are those with additional (service-definition related) information,
627 not necessarily all database fields of the table.  Pseudo-fields may also
628 be returned (i.e. svc_acct.usergroup).
629
630 Each value of the hashref is another hashref, which can have one or more of
631 the following keys:
632
633 =over 4
634
635 =item label - Description of the field
636
637 =item def_label - Optional description of the field in the context of service definitions
638
639 =item type - Currently "text", "select", "disabled", or "radius_usergroup_selector"
640
641 =item disable_default - This field should not allow a default value in service definitions
642
643 =item disable_fixed - This field should not allow a fixed value in service definitions
644
645 =item disable_inventory - This field should not allow inventory values in service definitions
646
647 =item select_list - If type is "text", this can be a listref of possible values.
648
649 =item select_table - An alternative to select_list, this defines a database table with the possible choices.
650
651 =item select_key - Used with select_table, this is the field name of keys
652
653 =item select_label - Used with select_table, this is the field name of labels
654
655 =back
656
657 =cut
658
659 #maybe this should move and be a class method in svc_Common.pm
660 sub svc_table_fields {
661   my($class, $table) = @_;
662   my $svc_defs = $class->_svc_defs;
663   my $def = $svc_defs->{$table};
664
665   foreach ( grep !ref($def->{$_}), keys %$def ) {
666
667     #normalize the shortcut in %info hash
668     $def->{$_} = { 'label' => $def->{$_} };
669
670     $def->{$_}{'type'} ||= 'text';
671
672   }
673
674   $def;
675 }
676
677 =back
678
679 =head1 SUBROUTINES
680
681 =over 4
682
683 =item process
684
685 Job-queue processor for web interface adds/edits
686
687 =cut
688
689 use Storable qw(thaw);
690 use Data::Dumper;
691 use MIME::Base64;
692 sub process {
693   my $job = shift;
694
695   my $param = thaw(decode_base64(shift));
696   warn Dumper($param) if $DEBUG;
697
698   my $old = qsearchs('part_svc', { 'svcpart' => $param->{'svcpart'} }) 
699     if $param->{'svcpart'};
700
701   $param->{'svc_acct__usergroup'} =
702     ref($param->{'svc_acct__usergroup'})
703       ? join(',', @{$param->{'svc_acct__usergroup'}} )
704       : $param->{'svc_acct__usergroup'};
705   
706   my $new = new FS::part_svc ( {
707     map {
708       $_ => $param->{$_};
709   #  } qw(svcpart svc svcdb)
710     } ( fields('part_svc'),
711         map { my $svcdb = $_;
712               my @fields = fields($svcdb);
713               push @fields, 'usergroup' if $svcdb eq 'svc_acct'; #kludge
714
715               map {
716                     if ( $param->{ $svcdb.'__'.$_.'_flag' } =~ /^[MA]$/ ) {
717                       $param->{ $svcdb.'__'.$_ } =
718                         delete( $param->{ $svcdb.'__'.$_.'_classnum' } );
719                     }
720                     if ( $param->{ $svcdb.'__'.$_.'_flag' } =~ /^S$/ ) {
721                       $param->{ $svcdb.'__'.$_} =
722                         ref($param->{ $svcdb.'__'.$_})
723                           ? join(',', @{$param->{ $svcdb.'__'.$_ }} )
724                           : $param->{ $svcdb.'__'.$_ };
725                     }
726                     ( $svcdb.'__'.$_, $svcdb.'__'.$_.'_flag' );
727                   }
728                   @fields;
729
730             } FS::part_svc->svc_tables()
731       )
732   } );
733   
734   my %exportnums =
735     map { $_->exportnum => ( $param->{'exportnum'.$_->exportnum} || '') }
736         qsearch('part_export', {} );
737
738   my $error;
739   if ( $param->{'svcpart'} ) {
740     $error = $new->replace( $old,
741                             '1.3-COMPAT',
742                             [ 'usergroup' ],
743                             \%exportnums,
744                             $job
745                           );
746   } else {
747     $error = $new->insert( [ 'usergroup' ],
748                            \%exportnums,
749                            $job,
750                          );
751     $param->{'svcpart'} = $new->getfield('svcpart');
752   }
753
754   die "$error\n" if $error;
755 }
756
757 =item process_bulk_cust_svc
758
759 Job-queue processor for web interface bulk customer service changes
760
761 =cut
762
763 use Storable qw(thaw);
764 use Data::Dumper;
765 use MIME::Base64;
766 sub process_bulk_cust_svc {
767   my $job = shift;
768
769   my $param = thaw(decode_base64(shift));
770   warn Dumper($param) if $DEBUG;
771
772   my $old_part_svc =
773     qsearchs('part_svc', { 'svcpart' => $param->{'old_svcpart'} } );
774
775   die "Must select a new service definition\n" unless $param->{'new_svcpart'};
776
777   #the rest should be abstracted out to to its own subroutine?
778
779   local $SIG{HUP} = 'IGNORE';
780   local $SIG{INT} = 'IGNORE';
781   local $SIG{QUIT} = 'IGNORE';
782   local $SIG{TERM} = 'IGNORE';
783   local $SIG{TSTP} = 'IGNORE';
784   local $SIG{PIPE} = 'IGNORE';
785
786   my $oldAutoCommit = $FS::UID::AutoCommit;
787   local $FS::UID::AutoCommit = 0;
788   my $dbh = dbh;
789
790   local( $FS::cust_svc::ignore_quantity ) = 1;
791
792   my $total = $old_part_svc->num_cust_svc( $param->{'pkgpart'} );
793
794   my $n = 0;
795   foreach my $old_cust_svc ( $old_part_svc->cust_svc( $param->{'pkgpart'} ) ) {
796
797     my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
798
799     $new_cust_svc->svcpart( $param->{'new_svcpart'} );
800     my $error = $new_cust_svc->replace($old_cust_svc);
801     if ( $error ) {
802       $dbh->rollback if $oldAutoCommit;
803       die "$error\n" if $error;
804     }
805
806     $error = $job->update_statustext( int( 100 * ++$n / $total ) );
807     if ( $error ) {
808       $dbh->rollback if $oldAutoCommit;
809       die $error if $error;
810     }
811
812   }
813
814   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
815
816   '';
817
818 }
819
820 =head1 BUGS
821
822 Delete is unimplemented.
823
824 The list of svc_* tables is no longer hardcoded, but svc_acct_pop is skipped
825 as a special case until it is renamed.
826
827 all_part_svc_column methods should be documented
828
829 =head1 SEE ALSO
830
831 L<FS::Record>, L<FS::part_svc_column>, L<FS::part_pkg>, L<FS::pkg_svc>,
832 L<FS::cust_svc>, L<FS::svc_acct>, L<FS::svc_forward>, L<FS::svc_domain>,
833 schema.html from the base documentation.
834
835 =cut
836
837 1;
838