+ # but in the new era, we no longer have the problem of uniquely
+ # identifying the tax_Xlocation record. The billpkgnums of the
+ # tax and the taxed item are known.
+ foreach my $new_loc
+ ( @{ $taxline->get('cust_bill_pkg_tax_location') },
+ @{ $taxline->get('cust_bill_pkg_tax_rate_location') } )
+ {
+ # the existing tax_Xlocation object
+ my $old_loc =
+ $tax_links{$tax_item->billpkgnum}{$new_loc->taxable_billpkgnum};
+
+ next if !$old_loc; # apply the leftover amount nonspecifically
+
+ #support partial credits: use $amount if smaller
+ # (so just distribute to the first location? perhaps should
+ # do so evenly...)
+ my $loc_amount = min( $amount, $new_loc->amount);
+
+ $amount -= $loc_amount;
+
+ $cust_credit_bill{$invnum} += $loc_amount;
+ push @{ $cust_credit_bill_pkg{$invnum} },
+ new FS::cust_credit_bill_pkg {
+ 'billpkgnum' => $tax_item->billpkgnum,
+ 'amount' => $loc_amount,
+ 'setuprecur' => 'setup',
+ 'billpkgtaxlocationnum' => $old_loc->billpkgtaxlocationnum,
+ 'billpkgtaxratelocationnum' => $old_loc->billpkgtaxratelocationnum,
+ };
+
+ } #foreach my $new_loc
+
+ # we still have to deal with the possibility that the tax links don't
+ # cover the whole amount of tax because of an incomplete upgrade...
+ if ($amount > 0) {
+ $cust_credit_bill{$invnum} += $amount;
+ push @{ $cust_credit_bill_pkg{$invnum} },
+ new FS::cust_credit_bill_pkg {
+ 'billpkgnum' => $tax_item->billpkgnum,
+ 'amount' => $amount,
+ 'setuprecur' => 'setup',
+ };
+
+ } # if $amount > 0
+
+ #unapply any payments applied to the tax
+ foreach my $cust_bill_pay_pkg
+ ( $tax_item->cust_bill_pay_pkg('setup') )
+ {
+ $error = $cust_bill_pay_pkg->delete;
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return "Error unapplying payment: $error";