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