+ my $tax_cents = 0;
+
+ while (@$taxables) {
+ my $cust_bill_pkg = shift @$taxables;
+ my $class = 'all';
+ if ( defined($taxables->[0]) and !ref($taxables->[0]) ) {
+ $class = shift @$taxables;
+ }
+
+ my %usage_map = map { $_ => $cust_bill_pkg->usage($_) }
+ $cust_bill_pkg->usage_classes;
+ my $usage_total = sum( values(%usage_map), 0 );
+
+ # determine if the item has exemptions that apply to this tax def
+ my @exemptions = grep { $_->taxnum == $self->taxnum }
+ @{ $cust_bill_pkg->cust_tax_exempt_pkg };
+
+ if ( $self->tax > 0 ) {
+
+ my $taxable_charged = 0;
+ if ($class eq 'all') {
+ $taxable_charged = $cust_bill_pkg->setup + $cust_bill_pkg->recur;
+ } elsif ($class eq 'setup') {
+ $taxable_charged = $cust_bill_pkg->setup;
+ } elsif ($class eq 'recur') {
+ $taxable_charged = $cust_bill_pkg->recur - $usage_total;
+ } else {
+ $taxable_charged = $usage_map{$class} || 0;
+ }
+
+ foreach my $ex (@exemptions) {
+ # the only cases where the exemption doesn't apply:
+ # if it's a setup exemption and $class is not 'setup' or 'all'
+ # if it's a recur exemption and $class is 'setup'
+ if ( ( $ex->exempt_recur and $class eq 'setup' )
+ or ( $ex->exempt_setup and $class ne 'setup' and $class ne 'all' )
+ ) {
+ next;
+ }
+
+ $taxable_charged -= $ex->amount;
+ }
+ # cust_main_county handles monthly capped exemptions; this doesn't.
+ #
+ # $taxable_charged can also be less than zero at this point
+ # (recur exemption + usage class breakdown); treat that as zero.
+ next if $taxable_charged <= 0;
+
+ # yeah, some false laziness with cust_main_county
+ my $this_tax_cents = int(100 * $taxable_charged * $self->tax);
+ my $tax_location = FS::cust_bill_pkg_tax_rate_location->new({
+ 'taxnum' => $self->taxnum,
+ 'taxtype' => ref($self),
+ 'cents' => $this_tax_cents, # not a real field
+ 'locationtaxid' => $self->location, # fundamentally silly
+ 'taxable_cust_bill_pkg' => $cust_bill_pkg,
+ 'taxratelocationnum' => $taxratelocationnum,
+ 'taxclass' => $class,
+ });
+ push @tax_locations, $tax_location;
+
+ $taxable_cents += 100 * $taxable_charged;
+ $tax_cents += $this_tax_cents;
+
+ } elsif ( $self->fee > 0 ) {
+ # most CCH taxes are this type, because nearly every county has a 911
+ # fee
+ my $units = 0;
+
+ # since we don't support partial exemptions (except setup/recur),
+ # if there's an exemption that applies to this package and taxrate,
+ # don't charge ANY per-unit fees
+ next if @exemptions;
+
+ # don't apply fees to usage classes (maybe if we ever get per-minute
+ # fees?)
+ next unless $class eq 'setup'
+ or $class eq 'recur'
+ or $class eq 'all';
+
+ if ( $self->unittype == 0 ) {
+ if ( !$seen{$cust_bill_pkg->pkgnum} ) {
+ # per access line
+ $units = $cust_bill_pkg->units;
+ $seen{$cust_bill_pkg->pkgnum} = 1;
+ } # else it's been seen, leave it at zero units
+
+ } elsif ($self->unittype == 1) { # per minute
+ # STILL not supported...fortunately these only exist if you happen
+ # to be in Idaho or Little Rock, Arkansas
+ #
+ # though a voip_cdr package could easily report minutes of usage...
+ return $self->_fatal_or_null( 'fee with minute unit type' );
+
+ } elsif ( $self->unittype == 2 ) {
+
+ my $locationnum = $cust_bill_pkg->tax_locationnum
+ || $cust_main->ship_locationnum;
+ # per account
+ $units = 1 unless $seen{$locationnum};
+ $seen{$locationnum} = 1;
+
+ } else {
+ # Unittype 19 is used for prepaid wireless E911 charges in many states.
+ # Apparently "per retail purchase", which for us would mean per invoice.
+ # Unittype 20 is used for some 911 surcharges and I have no idea what
+ # it means.
+ return $self->_fatal_or_null( 'unknown unit type in tax'. $self->taxnum );