X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_pkg.pm;h=ef24b53433a1c245a3465b2da74af33a9978baf5;hb=32db3ad86bcf04e4f34705a396b718061d333f20;hp=f521d65e2a93912f511e44afe4ee6c31c650771a;hpb=6e44387ee72331ece3a9ac00ec9b1d9b818c4943;p=freeside.git diff --git a/FS/FS/part_pkg.pm b/FS/FS/part_pkg.pm index f521d65e2..ef24b5343 100644 --- a/FS/FS/part_pkg.pm +++ b/FS/FS/part_pkg.pm @@ -1,9 +1,10 @@ package FS::part_pkg; use strict; -use vars qw( @ISA %plans $DEBUG ); +use vars qw( @ISA %plans $DEBUG $setup_hack ); use Carp qw(carp cluck confess); use Scalar::Util qw( blessed ); +use Time::Local qw( timelocal_nocheck ); use Tie::IxHash; use FS::Conf; use FS::Record qw( qsearch qsearchs dbh dbdef ); @@ -21,6 +22,7 @@ use FS::part_pkg_link; @ISA = qw( FS::m2m_Common FS::option_Common ); $DEBUG = 0; +$setup_hack = 0; =head1 NAME @@ -431,6 +433,12 @@ sub check { $self->freq($1); } + my @null_agentnum_right = ( 'Edit global package definitions' ); + push @null_agentnum_right, 'One-time charge' + if $self->freq =~ /^0/; + push @null_agentnum_right, 'Customize customer package' + if $self->disabled eq 'Y'; #good enough + my $error = $self->ut_numbern('pkgpart') || $self->ut_text('pkg') || $self->ut_text('comment') @@ -447,7 +455,10 @@ sub check { 'part_pkg_taxproduct', 'taxproductnum' ) - || $self->ut_agentnum_acl('agentnum', 'Edit global package definitions') + || ( $setup_hack + ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' ) + : $self->ut_agentnum_acl('agentnum', \@null_agentnum_right) + ) || $self->SUPER::check ; return $error if $error; @@ -553,6 +564,18 @@ packages. =cut +=item type_pkgs + +Returns all FS::type_pkgs objects (see L) for this package +definition. + +=cut + +sub type_pkgs { + my $self = shift; + qsearch('type_pkgs', { 'pkgpart' => $self->pkgpart } ); +} + sub pkg_svc { my $self = shift; @@ -728,6 +751,41 @@ sub freq_pretty { } } +=item add_freq TIMESTAMP + +Adds the frequency of this package to the provided timestamp and returns +the resulting timestamp, or -1 if the frequency of this package could not be +parsed (shouldn't happen). + +=cut + +sub add_freq { + my( $self, $date ) = @_; + my $freq = $self->freq; + + #change this bit to use Date::Manip? CAREFUL with timezones (see + # mailing list archive) + my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($date) )[0,1,2,3,4,5]; + + if ( $self->freq =~ /^\d+$/ ) { + $mon += $self->freq; + until ( $mon < 12 ) { $mon -= 12; $year++; } + } elsif ( $self->freq =~ /^(\d+)w$/ ) { + my $weeks = $1; + $mday += $weeks * 7; + } elsif ( $self->freq =~ /^(\d+)d$/ ) { + my $days = $1; + $mday += $days; + } elsif ( $self->freq =~ /^(\d+)h$/ ) { + my $hours = $1; + $hour += $hours; + } else { + return -1; + } + + timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year); +} + =item plandata For backwards compatibility, returns the plandata field as well as all options @@ -973,10 +1031,13 @@ sub part_pkg_taxrate { ')'; # much more CCH oddness in m2m -- this is kludgy my @tpnums = $self->_expand_cch_taxproductnum($class); - $extra_sql .= ' AND ('. - join(' OR ', map{ "taxproductnum = $_" } @tpnums ). - ')' - if @tpnums; + if (scalar(@tpnums)) { + $extra_sql .= ' AND ('. + join(' OR ', map{ "taxproductnum = $_" } @tpnums ). + ')'; + } else { + $extra_sql .= ' AND ( 0 = 1 )'; + } my $addl_from = 'LEFT JOIN part_pkg_taxproduct USING ( taxproductnum )'; my $order_by = 'ORDER BY taxclassnum, length(geocode) desc, length(taxproduct) desc'; @@ -1055,10 +1116,48 @@ sub calc_remain { 0; } sub calc_cancel { 0; } sub calc_units { 0; } +=item format OPTION DATA + +Returns data formatted according to the function 'format' described +in the plan info. Returns DATA if no such function exists. + +=cut + +sub format { + my ($self, $option, $data) = (shift, shift, shift); + if (exists($plans{$self->plan}->{fields}->{$option}{format})) { + &{$plans{$self->plan}->{fields}->{$option}{format}}($data); + }else{ + $data; + } +} + +=item parse OPTION DATA + +Returns data parsed according to the function 'parse' described +in the plan info. Returns DATA if no such function exists. + +=cut + +sub parse { + my ($self, $option, $data) = (shift, shift, shift); + if (exists($plans{$self->plan}->{fields}->{$option}{parse})) { + &{$plans{$self->plan}->{fields}->{$option}{parse}}($data); + }else{ + $data; + } +} + =back =cut +=head1 CLASS METHODS + +=over 4 + +=cut + # _upgrade_data # # Used by FS::Upgrade to migrate to a new database. @@ -1125,6 +1224,37 @@ sub _upgrade_data { # class method } +=item curuser_pkgs_sql + +Returns an SQL fragment for searching for packages the current user can +use, either via part_pkg.agentnum directly, or via agent type (see +L). + +=cut + +sub curuser_pkgs_sql { + #my($class) = shift; + + my $agentnums = join(',', $FS::CurrentUser::CurrentUser->agentnums); + + " + ( + 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) + ) + ) + "; + +} + +=back + =head1 SUBROUTINES =over 4 @@ -1172,38 +1302,6 @@ sub plan_info { \%plans; } -=item format OPTION DATA - -Returns data formatted according to the function 'format' described -in the plan info. Returns DATA if no such function exists. - -=cut - -sub format { - my ($self, $option, $data) = (shift, shift, shift); - if (exists($plans{$self->plan}->{fields}->{$option}{format})) { - &{$plans{$self->plan}->{fields}->{$option}{format}}($data); - }else{ - $data; - } -} - -=item parse OPTION DATA - -Returns data parsed according to the function 'parse' described -in the plan info. Returns DATA if no such function exists. - -=cut - -sub parse { - my ($self, $option, $data) = (shift, shift, shift); - if (exists($plans{$self->plan}->{fields}->{$option}{parse})) { - &{$plans{$self->plan}->{fields}->{$option}{parse}}($data); - }else{ - $data; - } -} - =back