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