option to handle credited tax date range differently, #28497
[freeside.git] / httemplate / search / cust_bill_pkg.cgi
index b1086e3..0ce141b 100644 (file)
@@ -265,8 +265,7 @@ if ( $cgi->param('distribute') == 1 ) {
   push @where, "sdate <= $ending",
                "edate >  $beginning",
   ;
-}
-else {
+} else {
   push @where, "cust_bill._date >= $beginning",
                "cust_bill._date <= $ending";
 }
@@ -561,30 +560,36 @@ if ( $cgi->param('nottax') ) {
                    ' )'
         if @classnums;
     }
-  }
 
-  # taxclass
-  if ( $cgi->param('taxclassNULL') ) {
-    push @where, 'cust_main_county.taxclass IS NULL';
-  }
+    # taxclass
+    if ( $cgi->param('taxclassNULL') ) {
+      push @where, 'cust_main_county.taxclass IS NULL';
+    }
 
-  # taxname
-  if ( $cgi->param('taxnameNULL') ) {
-    push @where, 'cust_main_county.taxname IS NULL OR '.
-                 'cust_main_county.taxname = \'Tax\'';
-  } elsif ( $cgi->param('taxname') ) {
-    push @where, 'cust_main_county.taxname = '.
-                  dbh->quote($cgi->param('taxname'));
-  }
+    # taxname
+    if ( $cgi->param('taxnameNULL') ) {
+      push @where, 'cust_main_county.taxname IS NULL OR '.
+                   'cust_main_county.taxname = \'Tax\'';
+    } elsif ( $cgi->param('taxname') ) {
+      push @where, 'cust_main_county.taxname = '.
+                    dbh->quote($cgi->param('taxname'));
+    }
 
-  # itemdesc, for breakdown from the vendor tax report
-  if ( $cgi->param('itemdesc') ) {
-    if ( $cgi->param('itemdesc') eq 'Tax' ) {
-      push @where, "($itemdesc = 'Tax' OR $itemdesc is null)";
-    } else {
-      push @where, "$itemdesc = ". dbh->quote($cgi->param('itemdesc'));
+    # itemdesc, for breakdown from the vendor tax report
+    if ( $cgi->param('itemdesc') ) {
+      if ( $cgi->param('itemdesc') eq 'Tax' ) {
+        push @where, "($itemdesc = 'Tax' OR $itemdesc is null)";
+      } else {
+        push @where, "$itemdesc = ". dbh->quote($cgi->param('itemdesc'));
+      }
     }
-  }
+
+    # specific taxnums
+    if ( $cgi->param('taxnum') =~ /^([\d,]+)$/) {
+      push @where, "cust_main_county.taxnum IN ($1)";
+    }
+
+  } #end of "normal case"
 
 } # nottax / istax
 
@@ -600,6 +605,12 @@ push @select, "($pay_sub) AS pay_amount";
 # credit
 if ( $cgi->param('credit') ) {
 
+  my $credit_where;
+
+  my($cr_begin, $cr_end) = FS::UI::Web::parse_beginning_ending($cgi, 'credit');
+  $credit_where = "WHERE cust_credit_bill._date >= $cr_begin " .
+                  "AND cust_credit_bill._date <= $cr_end";
+
   my $credit_sub;
 
   if ( $cgi->param('istax') ) {
@@ -613,6 +624,7 @@ if ( $cgi->param('credit') ) {
       JOIN cust_credit USING (crednum)
       LEFT JOIN reason USING (reasonnum)
       LEFT JOIN access_user USING (usernum)
+    $credit_where
     GROUP BY billpkgnum, billpkgtaxlocationnum, reason.reason, 
       access_user.username";
 
@@ -643,6 +655,7 @@ if ( $cgi->param('credit') ) {
       JOIN cust_credit USING (crednum)
       LEFT JOIN reason USING (reasonnum)
       LEFT JOIN access_user USING (usernum)
+    $credit_where
     GROUP BY billpkgnum, reason.reason, access_user.username";
     $join_pkg .= " LEFT JOIN ($credit_sub) AS item_credit USING (billpkgnum)";
   }