X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_pkg.pm;h=ef7f557807abe5f6e31c8617cdeaee2664dbd84c;hb=7b125e587a4d1ee0aca692e23ea7897f671855ae;hp=0562a6d442e470eebf6d6667390b493cb7c95e06;hpb=04a69f9d197efee6fa396bd35d04ae553e669978;p=freeside.git diff --git a/FS/FS/part_pkg.pm b/FS/FS/part_pkg.pm index 0562a6d44..ef7f55780 100644 --- a/FS/FS/part_pkg.pm +++ b/FS/FS/part_pkg.pm @@ -1,7 +1,7 @@ package FS::part_pkg; use strict; -use vars qw( @ISA %plans $DEBUG $setup_hack ); +use vars qw( @ISA %plans $DEBUG $setup_hack $skip_pkg_svc_hack ); use Carp qw(carp cluck confess); use Scalar::Util qw( blessed ); use Time::Local qw( timelocal_nocheck ); @@ -23,6 +23,7 @@ use FS::part_pkg_link; @ISA = qw( FS::m2m_Common FS::option_Common ); $DEBUG = 0; $setup_hack = 0; +$skip_pkg_svc_hack = 0; =head1 NAME @@ -97,6 +98,8 @@ inherits from FS::Record. The following fields are currently supported: =item agentnum - Optional agentnum (see L) +=item fcc_ds0s - Optional DS0 equivalency number for FCC form 477 + =back =head1 METHODS @@ -204,7 +207,11 @@ sub insert { warn " inserting part_pkg_taxoverride records" if $DEBUG; my %overrides = %{ $options{'tax_overrides'} || {} }; foreach my $usage_class ( keys %overrides ) { - my @overrides = (grep "$_", split (',', $overrides{$usage_class}) ); + my $override = + ( exists($overrides{$usage_class}) && defined($overrides{$usage_class}) ) + ? $overrides{$usage_class} + : ''; + my @overrides = (grep "$_", split(',', $override) ); my $error = $self->process_m2m ( 'link_table' => 'part_pkg_taxoverride', 'target_table' => 'tax_class', @@ -217,26 +224,30 @@ sub insert { } } - warn " inserting pkg_svc records" if $DEBUG; - my $pkg_svc = $options{'pkg_svc'} || {}; - foreach my $part_svc ( qsearch('part_svc', {} ) ) { - my $quantity = $pkg_svc->{$part_svc->svcpart} || 0; - my $primary_svc = - ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart ) - ? 'Y' - : ''; - - my $pkg_svc = new FS::pkg_svc( { - 'pkgpart' => $self->pkgpart, - 'svcpart' => $part_svc->svcpart, - 'quantity' => $quantity, - 'primary_svc' => $primary_svc, - } ); - my $error = $pkg_svc->insert; - if ( $error ) { - $dbh->rollback if $oldAutoCommit; - return $error; + unless ( $skip_pkg_svc_hack ) { + + warn " inserting pkg_svc records" if $DEBUG; + my $pkg_svc = $options{'pkg_svc'} || {}; + foreach my $part_svc ( qsearch('part_svc', {} ) ) { + my $quantity = $pkg_svc->{$part_svc->svcpart} || 0; + my $primary_svc = + ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart ) + ? 'Y' + : ''; + + my $pkg_svc = new FS::pkg_svc( { + 'pkgpart' => $self->pkgpart, + 'svcpart' => $part_svc->svcpart, + 'quantity' => $quantity, + 'primary_svc' => $primary_svc, + } ); + my $error = $pkg_svc->insert; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } } + } if ( $options{'cust_pkg'} ) { @@ -369,7 +380,7 @@ sub replace { foreach my $part_svc ( qsearch('part_svc', {} ) ) { my $quantity = $pkg_svc->{$part_svc->svcpart} || 0; my $primary_svc = - ( defined($options->{'primary_svc'}) + ( defined($options->{'primary_svc'}) && $options->{'primary_svc'} && $options->{'primary_svc'} == $part_svc->svcpart ) ? 'Y' @@ -453,6 +464,7 @@ sub check { || $self->ut_textn('taxclass') || $self->ut_enum('disabled', [ '', 'Y' ] ) || $self->ut_enum('custom', [ '', 'Y' ] ) + || $self->ut_enum('no_auto', [ '', 'Y' ]) #|| $self->ut_moneyn('setup_cost') #|| $self->ut_moneyn('recur_cost') || $self->ut_floatn('setup_cost') @@ -460,6 +472,8 @@ sub check { || $self->ut_floatn('pay_weight') || $self->ut_floatn('credit_weight') || $self->ut_numbern('taxproductnum') + || $self->ut_foreign_keyn('classnum', 'pkg_class', 'classnum') + || $self->ut_foreign_keyn('addon_classnum', 'pkg_class', 'classnum') || $self->ut_foreign_keyn('taxproductnum', 'part_pkg_taxproduct', 'taxproductnum' @@ -468,17 +482,11 @@ sub check { ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' ) : $self->ut_agentnum_acl('agentnum', \@null_agentnum_right) ) + || $self->ut_numbern('fcc_ds0s') || $self->SUPER::check ; return $error if $error; - if ( $self->classnum !~ /^$/ ) { - my $error = $self->ut_foreign_key('classnum', 'pkg_class', 'classnum'); - return $error if $error; - } else { - $self->classnum(''); - } - return 'Unknown plan '. $self->plan unless exists($plans{$self->plan}); @@ -531,6 +539,22 @@ sub pkg_class { } } +=item addon_pkg_class + +Returns the add-on package class, as an FS::pkg_class object, or the empty +string if there is no add-on package class. + +=cut + +sub addon_pkg_class { + my $self = shift; + if ( $self->addon_classnum ) { + qsearchs('pkg_class', { 'classnum' => $self->addon_classnum } ); + } else { + return ''; + } +} + =item categoryname Returns the package category name, or the empty string if there is no package @@ -561,6 +585,21 @@ sub classname { : ''; } +=item addon_classname + +Returns the add-on package class name, or the empty string if there is no +add-on package class. + +=cut + +sub addon_classname { + my $self = shift; + my $pkg_class = $self->addon_pkg_class; + $pkg_class + ? $pkg_class->classname + : ''; +} + =item agent Returns the associated agent for this event, if any, as an FS::agent object. @@ -732,6 +771,7 @@ sub is_free { } } +sub can_discount { 0; } sub freqs_href { #method, class method or sub? #my $self = shift; @@ -872,9 +912,11 @@ sub options { map { $_->optionname => $_->optionvalue } $self->part_pkg_option; } -=item option OPTIONNAME +=item option OPTIONNAME [ QUIET ] -Returns the option value for the given name, or the empty string. +Returns the option value for the given name, or the empty string. If a true +value is passed as the second argument, warnings about missing the option +will be suppressed. =cut @@ -917,10 +959,12 @@ sub svc_part_pkg_link { sub _part_pkg_link { my( $self, $type ) = @_; - qsearch('part_pkg_link', { 'src_pkgpart' => $self->pkgpart, - 'link_type' => $type, - } - ); + qsearch({ table => 'part_pkg_link', + hashref => { 'src_pkgpart' => $self->pkgpart, + 'link_type' => $type, + }, + order_by => "ORDER BY hidden", + }); } sub self_and_bill_linked { @@ -928,12 +972,18 @@ sub self_and_bill_linked { } sub _self_and_linked { - my( $self, $type ) = @_; + my( $self, $type, $hidden ) = @_; + $hidden ||= ''; + + my @result = (); + foreach ( ( $self, map { $_->dst_pkg->_self_and_linked($type, $_->hidden) } + $self->_part_pkg_link($type) ) ) + { + $_->hidden($hidden) if $hidden; + push @result, $_; + } - ( $self, - map { $_->dst_pkg->_self_and_linked($type) } - $self->_part_pkg_link($type) - ); + (@result); } =item part_pkg_taxoverride [ CLASS ] @@ -1162,6 +1212,18 @@ sub calc_units { 0; } #fallback for everything except bulk.pm sub hide_svc_detail { 0; } +=item recur_cost_permonth CUST_PKG + +recur_cost divided by freq (only supported for monthly and longer frequencies) + +=cut + +sub recur_cost_permonth { + my($self, $cust_pkg) = @_; + return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0; + sprintf('%.2f', $self->recur_cost / $self->freq ); +} + =item format OPTION DATA Returns data formatted according to the function 'format' described @@ -1225,27 +1287,29 @@ sub _upgrade_data { # class method foreach my $part_pkg (@part_pkg) { unless ( $part_pkg->plan ) { - $part_pkg->plan('flat'); + } - if ( $part_pkg->setup =~ /^\s*([\d\.]+)\s*$/ ) { + if ( length($part_pkg->option('setup_fee')) == 0 + && $part_pkg->setup =~ /^\s*([\d\.]+)\s*$/ ) { - my $opt = new FS::part_pkg_option { - 'pkgpart' => $part_pkg->pkgpart, - 'optionname' => 'setup_fee', - 'optionvalue' => $1, - }; - my $error = $opt->insert; - die $error if $error; + my $opt = new FS::part_pkg_option { + 'pkgpart' => $part_pkg->pkgpart, + 'optionname' => 'setup_fee', + 'optionvalue' => $1, + }; + my $error = $opt->insert; + die $error if $error; - $part_pkg->setup(''); - } else { - die "Can't parse part_pkg.setup for fee; convert pkgnum ". - $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n"; - } + #} else { + # die "Can't parse part_pkg.setup for fee; convert pkgnum ". + # $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n"; + } + $part_pkg->setup(''); - if ( $part_pkg->recur =~ /^\s*([\d\.]+)\s*$/ ) { + if ( length($part_pkg->option('recur_fee')) == 0 + && $part_pkg->recur =~ /^\s*([\d\.]+)\s*$/ ) { my $opt = new FS::part_pkg_option { 'pkgpart' => $part_pkg->pkgpart, @@ -1255,14 +1319,12 @@ sub _upgrade_data { # class method my $error = $opt->insert; die $error if $error; - $part_pkg->recur(''); - - } else { - die "Can't parse part_pkg.setup for fee; convert pkgnum ". - $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n"; - } + #} else { + # die "Can't parse part_pkg.setup for fee; convert pkgnum ". + # $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n"; } + $part_pkg->recur(''); $part_pkg->replace; #this should take care of plandata, right? @@ -1281,6 +1343,7 @@ sub _upgrade_data { # class method $new->custom('Y'); my $comment = $part_pkg->comment; $comment =~ s/^\(CUSTOM\) //; + $comment = '(none)' unless $comment =~ /\S/; $new->comment($comment); my $pkg_svc = { map { $_->svcpart => $_->quantity } $part_pkg->pkg_svc }; @@ -1334,15 +1397,16 @@ sub _pkgs_sql { " ( - agentnum IS NOT NULL - OR - 0 < ( SELECT COUNT(*) - FROM type_pkgs - LEFT JOIN agent_type USING ( typenum ) - LEFT JOIN agent AS typeagent USING ( typenum ) - WHERE type_pkgs.pkgpart = part_pkg.pkgpart - AND typeagent.agentnum IN ($agentnums) - ) + ( agentnum IS NOT NULL AND agentnum IN ($agentnums) ) + OR ( agentnum IS NULL + AND EXISTS ( SELECT 1 + FROM type_pkgs + LEFT JOIN agent_type USING ( typenum ) + LEFT JOIN agent AS typeagent USING ( typenum ) + WHERE type_pkgs.pkgpart = part_pkg.pkgpart + AND typeagent.agentnum IN ($agentnums) + ) + ) ) ";