merge webpay support in with autoselection of old realtime_bop and realtime_refund_bop
[freeside.git] / FS / FS / cust_pkg.pm
index 885eef1..7c8656c 100644 (file)
@@ -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,9 +439,7 @@ replace methods.
 sub check {
   my $self = shift;
 
-  $self->locationnum('')
-    if defined($self->locationnum) && length($self->locationnum)
-    && ( $self->locationnum == 0 || $self->locationnum == -1 );
+  $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
 
   my $error = 
     $self->ut_numbern('pkgnum')
@@ -933,7 +930,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'} = '';
@@ -2366,10 +2363,11 @@ sub location_sql {
   # '?' placeholders in _location_sql_where
   my @bill_param;
   if ( $ornull ) {
-    @bill_param = qw( county county county state state state country );
+    @bill_param = qw( county county state state state country );
   } else {
-    @bill_param = qw( county county state state country );
+    @bill_param = qw( county state state country );
   }
+  unshift @bill_param, 'county'; # unless $nec;
 
   my $main_where;
   my @main_param;
@@ -2380,7 +2378,7 @@ sub location_sql {
            AND ". _location_sql_where('cust_main', '', $ornull ). "
          )
       OR (       ship_last IS NOT NULL AND ship_last != ''
-           AND ". _location_sql_where('cust_main', 'ship_', $ornull). "
+           AND ". _location_sql_where('cust_main', 'ship_', $ornull ). "
          )
     )";
     #    AND payby != 'COMP'
@@ -2398,10 +2396,13 @@ sub location_sql {
   my @param;
   if ( $conf->exists('tax-pkg_address') ) {
 
+    my $loc_where = _location_sql_where( 'cust_location', '', $ornull );
+
     $where = " (
-         ( cust_pkg.locationnum IS     NULL AND $main_where                  )
-      OR ( cust_pkg.locationnum IS NOT NULL AND ". _location_sql_where('cust_location', '', $ornull). " )
-    )";
+                    ( cust_pkg.locationnum IS     NULL AND $main_where )
+                 OR ( cust_pkg.locationnum IS NOT NULL AND $loc_where  )
+               )
+             ";
     @param = ( @main_param, @bill_param );
   
   } else {
@@ -2415,15 +2416,22 @@ sub location_sql {
 
 }
 
-#subroutine, helper for able
+#subroutine, helper for location_sql
 sub _location_sql_where {
-  my $table = shift;
+  my $table  = shift;
   my $prefix = @_ ? shift : '';
   my $ornull = @_ ? shift : '';
+
+#  $ornull             = $ornull          ? " OR ( ? IS NULL AND $table.${prefix}county IS NULL ) " : '';
+
   $ornull = $ornull ? ' OR ? IS NULL ' : '';
+
+  my $or_empty_county = " OR ( ? = '' AND $table.${prefix}county IS NULL ) ";
+  my $or_empty_state =  " OR ( ? = '' AND $table.${prefix}state  IS NULL ) ";
+
   "
-        ( $table.${prefix}county  = ? OR ? = '' $ornull )
-    AND ( $table.${prefix}state   = ? OR ? = '' $ornull )
+        ( $table.${prefix}county  = ? $or_empty_county $ornull )
+    AND ( $table.${prefix}state   = ? $or_empty_state  $ornull )
     AND   $table.${prefix}country = ?
   ";
 }