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