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