X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_pkg.pm;h=ec88e1b2d89ab3ddbfd9ebaa0337081c4a6205b0;hb=8d39836a37186ad0e3aa993e2cc659e0b986cd35;hp=fe56c62e1af9576fe5fca985a127585dcb3c041e;hpb=287bf810a8ae3528525fff354610afa606468299;p=freeside.git diff --git a/FS/FS/part_pkg.pm b/FS/FS/part_pkg.pm index fe56c62e1..ec88e1b2d 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 @@ -85,6 +86,8 @@ inherits from FS::Record. The following fields are currently supported: =item disabled - Disabled flag, empty or `Y' +=item custom - Custom flag, empty or `Y' + =item setup_cost - for cost tracking =item recur_cost - for cost tracking @@ -95,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 @@ -113,9 +118,8 @@ sub table { 'part_pkg'; } =item clone An alternate constructor. Creates a new package definition by duplicating -an existing definition. A new pkgpart is assigned and `(CUSTOM) ' is prepended -to the comment field. To add the package definition to the database, see -L<"insert">. +an existing definition. A new pkgpart is assigned and the custom flag is +set to Y. To add the package definition to the database, see L<"insert">. =cut @@ -124,8 +128,7 @@ sub clone { my $class = ref($self); my %hash = $self->hash; $hash{'pkgpart'} = ''; - $hash{'comment'} = "(CUSTOM) ". $hash{'comment'} - unless $hash{'comment'} =~ /^\(CUSTOM\) /; + $hash{'custom'} = 'Y'; #new FS::part_pkg ( \%hash ); # ? new $class ( \%hash ); # ? } @@ -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' @@ -452,6 +463,7 @@ sub check { || $self->ut_enum('recurtax', [ '', 'Y' ] ) || $self->ut_textn('taxclass') || $self->ut_enum('disabled', [ '', 'Y' ] ) + || $self->ut_enum('custom', [ '', 'Y' ] ) #|| $self->ut_moneyn('setup_cost') #|| $self->ut_moneyn('recur_cost') || $self->ut_floatn('setup_cost') @@ -467,6 +479,7 @@ 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; @@ -488,20 +501,30 @@ sub check { ''; } -=item pkg_comment +=item pkg_comment [ OPTION => VALUE... ] Returns an (internal) string representing this package. Currently, "pkgpart: pkg - comment", is returned. "pkg - comment" may be returned in the -future, omitting pkgpart. +future, omitting pkgpart. The comment will have '(CUSTOM) ' prepended if +custom is Y. + +If the option nopkgpart is true then the "pkgpart: ' is omitted. =cut sub pkg_comment { my $self = shift; + my %opt = @_; #$self->pkg. ' - '. $self->comment; #$self->pkg. ' ('. $self->comment. ')'; - $self->pkgpart. ': '. $self->pkg. ' - '. $self->comment; + my $pre = $opt{nopkgpart} ? '' : $self->pkgpart. ': '; + $pre. $self->pkg. ' - '. $self->custom_comment; +} + +sub custom_comment { + my $self = shift; + ( $self->custom ? '(CUSTOM) ' : '' ). $self->comment; } =item pkg_class @@ -723,34 +746,9 @@ sub is_free { sub freqs_href { - #method, class method or sub? #my $self = shift; - - tie my %freq, 'Tie::IxHash', - '0' => '(no recurring fee)', - '1h' => 'hourly', - '1d' => 'daily', - '2d' => 'every two days', - '3d' => 'every three days', - '1w' => 'weekly', - '2w' => 'biweekly (every 2 weeks)', - '1' => 'monthly', - '45d' => 'every 45 days', - '2' => 'bimonthly (every 2 months)', - '3' => 'quarterly (every 3 months)', - '4' => 'every 4 months', - '137d' => 'every 4 1/2 months (137 days)', - '6' => 'semiannually (every 6 months)', - '12' => 'annually', - '13' => 'every 13 months (annually +1 month)', - '24' => 'biannually (every 2 years)', - '36' => 'triannually (every 3 years)', - '48' => '(every 4 years)', - '60' => '(every 5 years)', - '120' => '(every 10 years)', - ; - - \%freq; - + # moved to FS::Misc to make this accessible to other packages + # at initialization + FS::Misc::pkg_freqs(); } =item freq_pretty @@ -906,10 +904,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 { @@ -917,12 +917,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 ] @@ -1151,6 +1157,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 @@ -1214,27 +1232,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, @@ -1244,19 +1264,45 @@ 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? } + # now upgrade to the explicit custom flag + + @part_pkg = qsearch({ + 'table' => 'part_pkg', + 'hashref' => { disabled => 'Y', custom => '' }, + 'extra_sql' => "AND comment LIKE '(CUSTOM) %'", + }); + + foreach my $part_pkg (@part_pkg) { + my $new = new FS::part_pkg { $part_pkg->hash }; + $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 }; + my $primary = $part_pkg->svcpart; + my $options = { $part_pkg->options }; + + my $error = $new->replace( $part_pkg, + 'pkg_svc' => $pkg_svc, + 'primary_svc' => $primary, + 'options' => $options, + ); + die $error if $error; + } + } =item curuser_pkgs_sql @@ -1296,15 +1342,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) + ) + ) ) "; @@ -1379,6 +1426,8 @@ plandata should go part_pkg_taxrate is Pg specific +replace should be smarter about managing the related tables (options, pkg_svc) + =head1 SEE ALSO L, L, L, L, L.