show credit balance on invoices, #11564
[freeside.git] / FS / FS / tax_rate.pm
index b375a66..334c31e 100644 (file)
@@ -18,6 +18,7 @@ use DBIx::DBSchema;
 use DBIx::DBSchema::Table;
 use DBIx::DBSchema::Column;
 use FS::Record qw( qsearch qsearchs dbh dbdef );
+use FS::Conf;
 use FS::tax_class;
 use FS::cust_bill_pkg;
 use FS::cust_tax_location;
@@ -443,20 +444,35 @@ sub taxline {
 
   my $taxable_units = 0;
   unless ($self->recurtax =~ /^Y$/i) {
-    if (( $self->unittype || 0 ) == 0) {
+
+    if (( $self->unittype || 0 ) == 0) { #access line
       my %seen = ();
       foreach (@cust_bill_pkg) {
         $taxable_units += $_->units
-          unless $seen{$_->pkgnum};
-        $seen{$_->pkgnum}++;
+          unless $seen{$_->pkgnum}++;
       }
-    }elsif ($self->unittype == 1) {
+
+    } elsif ($self->unittype == 1) { #minute
       return $self->_fatal_or_null( 'fee with minute unit type' );
-    }elsif ($self->unittype == 2) {
-      $taxable_units = 1;
-    }else {
+
+    } elsif ($self->unittype == 2) { #account
+
+      my $conf = new FS::Conf;
+      if ( $conf->exists('tax-pkg_address') ) {
+        #number of distinct locations
+        my %seen = ();
+        foreach (@cust_bill_pkg) {
+          $taxable_units++
+            unless $seen{$_->cust_pkg->locationnum}++;
+        }
+      } else {
+        $taxable_units = 1;
+      }
+
+    } else {
       return $self->_fatal_or_null( 'unknown unit type in tax'. $self->taxnum );
     }
+
   }
 
   #
@@ -1316,7 +1332,7 @@ sub _restore_remembered_tax_products {
 
       unless ( $part_pkg_taxproduct ) {
         return "failed to find part_pkg_taxproduct (".
-          $taxproduct->{pkgpart}->{$class}. ") for pkgpart $pkgpart\n";
+          $taxproduct->{$pkgpart}->{$class}. ") for pkgpart $pkgpart\n";
       }
 
       if ( $class eq '' ) {
@@ -1773,7 +1789,7 @@ sub queue_liability_report {
   $cgi->param('ending', $param->{ending});
   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
   my $agentnum = $param->{agentnum};
-  $agentnum =~ /^(\d+)$/ ? $agentnum = $1 : $agentnum = '';
+  if ($agentnum =~ /^(\d+)$/) { $agentnum = $1; } else { $agentnum = ''; };
   generate_liability_report(
     'beginning' => $beginning,
     'ending'    => $ending,
@@ -1790,6 +1806,7 @@ agentnum, beginning, and ending
 
 =cut
 
+#shit, all sorts of false laxiness w/report_newtax.cgi
 sub generate_liability_report {
   my %args = @_;
 
@@ -1881,8 +1898,7 @@ sub generate_liability_report {
       my $taxwhere = "FROM cust_bill_pkg $addl_from $where AND payby != 'COMP' ".
         "AND ". join( ' AND ', map { "( $_ = ? OR ? = '' AND $_ IS NULL)" } @taxparam );
 
-      my $sql = "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) ".
-                " $taxwhere AND cust_bill_pkg.pkgnum = 0";
+      my $sql = "SELECT SUM(amount) $taxwhere AND cust_bill_pkg.pkgnum = 0";
 
       my $x = &{$scalar_sql}($t, [ map { $_, $_ } @params ], $sql );
       $tax += $x;