X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Fpart_svc.pm;h=d68a4618113d0211fa01bffef76d4c2a59f70412;hp=ca26074a3da47fd7cd96289cf8a346a5c2b8b54d;hb=8d0e8149e7b19ad8543ac6c8c663be63dbc34762;hpb=c422010521a7c09673a8d4d0068d3fd482dd2dbc diff --git a/FS/FS/part_svc.pm b/FS/FS/part_svc.pm index ca26074a3..d68a46181 100644 --- a/FS/FS/part_svc.pm +++ b/FS/FS/part_svc.pm @@ -1,5 +1,5 @@ package FS::part_svc; -use base qw(FS::Record); +use base qw(FS::o2m_Common FS::Record); use strict; use vars qw( $DEBUG ); @@ -11,6 +11,12 @@ use FS::part_export; use FS::export_svc; use FS::cust_svc; use FS::part_svc_class; +use FS::part_svc_msgcat; + +FS::UID->install_callback(sub { + # preload the cache and make sure all modules load + my $svc_defs = FS::part_svc->_svc_defs; +}); $DEBUG = 0; @@ -51,7 +57,7 @@ FS::Record. The following fields are currently supported: =item svcdb - table used for this service. See L, L, and L, among others. -=item classnum - Optional service class (see L) +=item classnum - Optional service class (see L) =item disabled - Disabled flag, empty or `Y' @@ -145,9 +151,9 @@ sub insert { foreach my $field (fields($svcdb), @fields) { next if $field eq 'svcnum'; my $prefix = $svcdb.'__'; - if ( defined( $self->getfield($prefix.$_.'_flag')) - or defined($self->getfield($prefix.$_.'_required')) - or length($self->getfield($prefix.$_.'_label')) + if ( defined( $self->getfield($prefix.$field.'_flag')) + or defined($self->getfield($prefix.$field.'_required')) + or length($self->getfield($prefix.$field.'_label')) ) { my $part_svc_column = $self->part_svc_column($field); my $previous = qsearchs('part_svc_column', { @@ -284,9 +290,9 @@ sub replace { foreach my $field (fields($svcdb),@fields) { next if $field eq 'svcnum'; my $prefix = $svcdb.'__'; - if ( defined( $new->getfield($prefix.$_.'_flag')) - or defined($new->getfield($prefix.$_.'_required')) - or length($new->getfield($prefix.$_.'_label')) + if ( defined( $new->getfield($prefix.$field.'_flag')) + or defined($new->getfield($prefix.$field.'_required')) + or length($new->getfield($prefix.$field.'_label')) ) { my $part_svc_column = $new->part_svc_column($field); my $previous = qsearchs('part_svc_column', { @@ -513,6 +519,18 @@ sub part_export_dsl_pull { grep $_->can('dsl_pull'), $self->part_export; } +=item part_export_partsvc + +Returns a list of any exports (see L) for this service that +are capable of pushing a change after part svc is changed. + +=cut + +sub part_export_partsvc { + my $self = shift; + grep $_->can('export_partsvc'), $self->part_export; +} + =item cust_svc [ PKGPART ] Returns a list of associated customer services (FS::cust_svc records). @@ -560,6 +578,9 @@ is specified as B<0>, returns the number of unlinked customer services. sub num_cust_svc { my $self = shift; + return $self->{Hash}{num_cust_svc} + if !@_ && exists($self->{Hash}{num_cust_svc}); + my @param = ( $self->svcpart ); my( $join, $and ) = ( '', '' ); @@ -582,6 +603,26 @@ sub num_cust_svc { $sth->fetchrow_arrayref->[0]; } +=item num_cust_svc_cancelled + +Returns the number of associated customer services that are +attached to cancelled packages. + +=cut + +sub num_cust_svc_cancelled { + my $self = shift; + my $sth = dbh->prepare( + "SELECT COUNT(*) FROM cust_svc + LEFT JOIN cust_pkg USING ( pkgnum ) + WHERE svcpart = ? + AND cust_pkg.cancel IS NOT NULL" + ) or die dbh->errstr; + $sth->execute($self->svcpart) + or die $sth->errstr; + $sth->fetchrow_arrayref->[0]; +} + =item svc_x Returns a list of associated FS::svc_* records. @@ -593,6 +634,24 @@ sub svc_x { map { $_->svc_x } $self->cust_svc; } +=item svc_locale LOCALE + +Returns a customer-viewable service definition label in the chosen LOCALE. +If there is no entry for that locale or if LOCALE is empty, returns +part_svc.svc. + +=cut + +sub svc_locale { + my( $self, $locale ) = @_; + return $self->svc unless $locale; + my $part_svc_msgcat = qsearchs('part_svc_msgcat', { + svcpart => $self->svcpart, + locale => $locale + }) or return $self->svc; + $part_svc_msgcat->svc; +} + =back =head1 CLASS METHODS @@ -648,10 +707,23 @@ sub _svc_defs { warn "skipping disabled service FS::$mod" if $DEBUG; next; } + + foreach ("FS::$mod"->virtual_fields_hash) { + $info->{'fields'}->{$_->{'name'}} = $_->{'label'}; + } + $info{$mod} = $info; + + # all svc_* modules are required to have h_svc_* modules for invoice + # display. check for them as early as possible. + eval "use FS::h_$mod;"; + if ( $@ ) { + die "couldn't load history record module h_$mod: $@\n"; + } } } + tie my %svc_defs, 'Tie::IxHash', map { $_ => $info{$_}->{'fields'} } sort { $info{$a}->{'display_weight'} <=> $info{$b}->{'display_weight'} } @@ -806,10 +878,10 @@ sub process { map { my $f = $svcdb.'__'.$_; my $flag = $param->{ $f.'_flag' } || ''; #silence warnings - if ( $flag =~ /^[MAH]$/ ) { + if ( $flag =~ /^[MAHP]$/ ) { $param->{ $f } = delete( $param->{ $f.'_classnum' } ); } - if ( ( $flag =~ /^[MAHS]$/ or $_ eq 'usergroup' ) + if ( ( $flag =~ /^[MAHSP]$/ or $_ eq 'usergroup' ) and ref($param->{ $f }) ) { $param->{ $f } = join(',', @{ $param->{ $f } }); } @@ -831,6 +903,18 @@ sub process { $exportnums{$exportnum} = $role; } } + + ## make sure export required fields are marked required. + my @required_fields; + foreach (keys %exportnums) { + my $export = qsearchs('part_export', { 'exportnum' => $_ }) + if $exportnums{$_}; + if ($export) { + push @required_fields, $export->required_fields if $export->can('required_fields'); + } + } + foreach (@required_fields) { $new->set($_, 'Y'); } + my $error; if ( $param->{'svcpart'} ) { $error = $new->replace( $old, @@ -847,7 +931,18 @@ sub process { $param->{'svcpart'} = $new->getfield('svcpart'); } + $error ||= $new->process_o2m( + 'table' => 'part_svc_msgcat', + 'params' => $param, + 'fields' => [ 'locale', 'svc' ], + ); + die "$error\n" if $error; + + foreach my $part_svc_export ( $new->part_export_partsvc ) { + $error = $part_svc_export->export_partsvc($new); + } + return $error if $error; } =item process_bulk_cust_svc