credits return taxes, but the magic calculation button does not yet work properly...
[freeside.git] / httemplate / search / cust_bill_pkg.cgi
index 52f59de..975a307 100644 (file)
@@ -404,8 +404,6 @@ if ( $cgi->param('pkg_tax') ) {
 
 }
 
-my $where = ' WHERE '. join(' AND ', @where);
-
 my $join_cust =  '      JOIN cust_bill USING ( invnum ) 
                    LEFT JOIN cust_main USING ( custnum ) ';
 
@@ -427,14 +425,28 @@ if ( $cgi->param('nottax') ) {
     $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
                    LEFT JOIN cust_location              USING ( locationnum ) ';
 
-    #quelle kludge, false laziness w/report_tax.cgi
-    $where =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g; 
-  } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
+    #quelle kludge, somewhat false laziness w/report_tax.cgi
+    s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
+  } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
+            $cgi->param('iscredit') eq 'rate') {
     $join_pkg .=
       ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ) '.
       ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
   }
 
+  if ( $cgi->param('iscredit') ) {
+    $join_pkg .= ' JOIN cust_credit_bill_pkg USING ( billpkgnum';
+    if ( $conf->exists('tax-pkg_address') ) {
+      $join_pkg .= ', billpkgtaxlocationnum )';
+      push @where, "billpkgtaxratelocationnum IS NULL";
+    } elsif ( $cgi->param('iscredit') eq 'rate' ) {
+      $join_pkg .= ', billpkgtaxratelocationnum )';
+    } else {
+      $join_pkg .= ' )';
+      push @where, "billpkgtaxratelocationnum IS NULL";
+    }
+  }
+
 } else { 
 
   #die?
@@ -445,6 +457,8 @@ if ( $cgi->param('nottax') ) {
 
 }
 
+my $where = ' WHERE '. join(' AND ', @where);
+
 if ($use_usage) {
   $count_query .=
     " FROM (SELECT cust_bill_pkg.setup, cust_bill_pkg.recur,