X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_pkg.pm;h=203a26901b23168ac0f2bd3220324391ae3287d7;hb=a04f7d97cd3e57ee9061fba5f737d6d77c782c13;hp=e2ca871d052ca53a7b3526a956ee150b8b33d751;hpb=705f7d564546e7211844773f3566a89f0ae87a2c;p=freeside.git diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index e2ca871d0..203a26901 100644 --- a/FS/FS/cust_pkg.pm +++ b/FS/FS/cust_pkg.pm @@ -14,7 +14,6 @@ use FS::cust_svc; use FS::part_pkg; use FS::cust_main; use FS::cust_location; -use FS::type_pkgs; use FS::pkg_svc; use FS::cust_bill_pkg; use FS::cust_pkg_detail; @@ -440,7 +439,9 @@ replace methods. sub check { my $self = shift; - $self->locationnum('') if $self->locationnum == 0 || $self->locationnum == -1; + $self->locationnum('') + if defined($self->locationnum) && length($self->locationnum) + && ( $self->locationnum == 0 || $self->locationnum == -1 ); my $error = $self->ut_numbern('pkgnum') @@ -931,7 +932,7 @@ sub unsuspend { $hash{'bill'} = ( $hash{'bill'} || $hash{'setup'} ) + $inactive if ( $opt{'adjust_next_bill'} - || $conf->config('unsuspend-always_adjust_next_bill_date') ) + || $conf->exists('unsuspend-always_adjust_next_bill_date') ) && $inactive > 0 && ( $hash{'bill'} || $hash{'setup'} ); $hash{'susp'} = ''; @@ -2346,6 +2347,99 @@ sub search_sql { } +=item location_sql + +Returns a list: the first item is an SQL fragment identifying matching +packages/customers via location (taking into account shipping and package +address taxation, if enabled), and subsequent items are the parameters to +substitute for the placeholders in that fragment. + +=cut + +sub location_sql { + my($class, %opt) = @_; + my $ornull = $opt{'ornull'}; + my $nec = $opt{'noempty_county'}; + + my $conf = new FS::Conf; + + # '?' placeholders in _location_sql_where + my @bill_param; + if ( $ornull ) { + @bill_param = qw( county county state state state country ); + } else { + @bill_param = qw( county state state country ); + } + unshift @bill_param, 'county' unless $nec; + + my $main_where; + my @main_param; + if ( $conf->exists('tax-ship_address') ) { + + $main_where = "( + ( ( ship_last IS NULL OR ship_last = '' ) + AND ". _location_sql_where('cust_main', '', $ornull, $nec ). " + ) + OR ( ship_last IS NOT NULL AND ship_last != '' + AND ". _location_sql_where('cust_main', 'ship_', $ornull, $nec ). " + ) + )"; + # AND payby != 'COMP' + + @main_param = ( @bill_param, @bill_param ); + + } else { + + $main_where = _location_sql_where('cust_main'); # AND payby != 'COMP' + @main_param = @bill_param; + + } + + my $where; + my @param; + if ( $conf->exists('tax-pkg_address') ) { + + my $loc_where = _location_sql_where( 'cust_location', + '', #prefix + $ornull, + $nec, + ); + + $where = " ( + ( cust_pkg.locationnum IS NULL AND $main_where ) + OR ( cust_pkg.locationnum IS NOT NULL AND $loc_where ) + ) + "; + @param = ( @main_param, @bill_param ); + + } else { + + $where = $main_where; + @param = @main_param; + + } + + ( $where, @param ); + +} + +#subroutine, helper for location_sql +sub _location_sql_where { + my $table = shift; + my $prefix = @_ ? shift : ''; + my $ornull = @_ ? shift : ''; + my $no_empty_county = @_ ? shift : ''; + + $ornull = $ornull ? ' OR ? IS NULL ' : ''; + my $or_empty_county = $no_empty_county ? '' : " OR ? = '' "; + + " + ( $table.${prefix}county = ? $or_empty_county $ornull ) + AND ( $table.${prefix}state = ? OR ? = '' $ornull ) + AND $table.${prefix}country = ? + "; +} + =head1 SUBROUTINES =over 4