fix multiple pkgpart search, RT#5924
[freeside.git] / httemplate / search / cust_bill_pkg.cgi
index 9d48432..4a31611 100644 (file)
@@ -83,6 +83,10 @@ if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
   push @where, "cust_main.agentnum = $1";
 }
 
+#classnum
+# not specified: all classes
+# 0: empty class
+# N: classnum
 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
   if ( $1 == 0 ) {
     push @where, "classnum IS NULL";
@@ -91,6 +95,16 @@ if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
   }
 }
 
+push @where, ' ( '. join(' OR ',
+                      map ' taxclass = '.dbh->quote($_), $cgi->param('taxclass')
+                    ).
+             ' ) '
+  if $cgi->param('taxclass')
+  && ! $cgi->param('istax'); #no part_pkg.taxclass in this case
+                             #(should we save a taxclass or a link to taxnum
+                             # in cust_bill_pkg or something like
+                             # cust_bill_pkg_tax_location?)
+
 #sub _where {
 # my $table = shift;
 # my $prefix = @_ ? shift : '';
@@ -143,17 +157,49 @@ if ( $cgi->param('out') ) {
 
   }
 
-} elsif ( $cgi->param('country' ) ) {
+} elsif ( $cgi->param('country') ) {
 
-  my %ph = map { $_ => dbh->quote( $cgi->param($_) ) }
-               qw( county state country );
+  my @counties = $cgi->param('county');
+   
+  if ( scalar(@counties) > 1 ) {
 
-  my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
-  while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
-    $loc_sql =~ s/\?/$ph{shift(@param)}/e;
-  }
+    #hacky, could be more efficient.  care if it is ever used for more than the
+    # tax-report_groups filtering kludge
+
+    my $locs_sql =
+      ' ( '. join(' OR ', map {
+
+          my %ph = ( 'county' => dbh->quote($_),
+                     map { $_ => dbh->quote( $cgi->param($_) ) }
+                       qw( state country )
+                   );
+
+          my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
+          while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
+            $loc_sql =~ s/\?/$ph{shift(@param)}/e;
+          }
+
+          $loc_sql;
+
+        } @counties
 
-  push @where, $loc_sql;
+      ). ' ) ';
+
+    push @where, $locs_sql;
+
+  } else {
+
+    my %ph = map { $_ => dbh->quote( $cgi->param($_) ) }
+                 qw( county state country );
+
+    my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
+    while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
+      $loc_sql =~ s/\?/$ph{shift(@param)}/e;
+    }
+
+    push @where, $loc_sql;
+
+  }
    
   if ( $cgi->param('istax') ) {
     if ( $cgi->param('taxname') ) {
@@ -168,13 +214,6 @@ if ( $cgi->param('out') ) {
     #warn "neither nottax nor istax parameters specified";
   }
 
-  push @where, ' taxclass = '. dbh->quote( $cgi->param('taxclass') )
-    if $cgi->param('taxclass')
-    && ! $cgi->param('istax'); #no part_pkg.taxclass in this case
-                               #(should we save a taxclass or a link to taxnum
-                               # in cust_bill_pkg or something like
-                               # cust_bill_pkg_tax_location?)
-
   if ( $cgi->param('taxclassNULL') ) {
 
     my %hash = ( 'country' => scalar($cgi->param('country')) );
@@ -189,7 +228,7 @@ if ( $cgi->param('out') ) {
 
   }
 
- } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
+} elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
 
   # this should really be shoved out to FS::cust_pkg->location_sql or something
   # along with the code in report_newtax.cgi
@@ -211,17 +250,48 @@ if ( $cgi->param('out') ) {
 
 }
 
-if ($cgi->param('itemdesc')) {
-  if ($cgi->param('itemdesc') eq 'Tax') {
+if ( $cgi->param('itemdesc') ) {
+  if ( $cgi->param('itemdesc') eq 'Tax' ) {
     push @where, "(itemdesc='Tax' OR itemdesc is null)";
-  }else{
+  } else {
     push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
   }
 }
+
+if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
+  my ( $group_op, $group_value ) = ( $1, $2 );
+  if ( $group_op eq '=' ) {
+    #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
+    push @where, 'itemdesc = '. dbh->quote($group_value);
+  } elsif ( $group_op eq '!=' ) {
+    push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
+  } else {
+    die "guru meditation #00de: group_op $group_op\n";
+  }
+  
+}
+
 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
 push @where, 'cust_bill_pkg.pkgnum  = 0' if $cgi->param('istax');
 
-push @where, " tax = 'Y' " if $cgi->param('cust_tax');
+if ( $cgi->param('cust_tax') ) {
+  #false laziness -ish w/report_tax.cgi
+  my $cust_exempt;
+  if ( $cgi->param('taxname') ) {
+    my $q_taxname = dbh->quote($cgi->param('taxname'));
+    $cust_exempt =
+      "( tax = 'Y'
+         OR EXISTS ( SELECT 1 FROM cust_main_exemption
+                       WHERE cust_main_exemption.custnum = cust_main.custnum
+                         AND cust_main_exemption.taxname = $q_taxname )
+       )
+      ";
+  } else {
+    $cust_exempt = " tax = 'Y' ";
+  }
+
+  push @where, $cust_exempt;
+}
 
 my $count_query;
 if ( $cgi->param('pkg_tax') ) {