Hi,

I'm using Drupal 7.12, Ubercart 7.x-3.0 and Discount Coupons 7.x-2.1-alpha7

My issue is that when multiple dicount coupons are applied to an order, the tax is not being calculated correctly. The tax rate I'm applying is 20%. The discount coupons are being applied automatically.

This is the order total summary from the payment method pane with 2 x £365 items in cart:

1. With multiple discounts - Coupon Discount NOT ticked as taxable line item

Subtotal: £730.00
Discount: TICKET Seminar 2012 - Member Discount: -£260.00
Discount: TICKET Seminar 2012 - Multiple Discount: -£40.00
Subtotal excluding taxes: £430.00
VAT: £146.00
Order total: £576.00

This is the expected result as per settings

2. With multiple discounts - Coupon Discount IS ticked as taxable line item

Subtotal: £730.00
Discount: TICKET Seminar 2012 - Member Discount: -£260.00
Discount: TICKET Seminar 2012 - Multiple Discount: -£40.00
Subtotal excluding taxes: £430.00
VAT: £26.00
Order total: £456.00

This is incorrect - the tax value appears to be 20% of 1 of the first discounts, ie 0.2 * 130 = 26

3. With 1 x £365 item in cart and one discount, Coupon Discount IS ticked as taxable line item

Discount: TICKET Seminar 2012 - Member Discount: -£130.00
Subtotal: £365.00
Subtotal excluding taxes: £235.00
VAT: £47.00
Order total: £282.00

This is correct, although notice how the discount line is appearing above the subtotal line.

I would speculate that the difference in display order (line weight) of the items in each case is related to this problem. Can anyone please advise on how to fix this, or confirm that this is a bug.

Thank you
Hans

Comments

hanskuhn’s picture

Just a quick update, I've played around with the line item weight in Store -> Configuration -> Coupon module settings and I can change the order of the discount coupon in the summary, but it does not affect the tax calculation. I will therefore withdraw my earlier speculation, leaving me clueless for now...

hanskuhn’s picture

Hi,

I've managed to track this problem down, not sure if it should be classified as a uc_coupon bug or a uc_taxes bug, but this is the cause and my hack solution.

The problem is that uc_taxes_apply_tax in uc_taxes calls the uc_coupon_tax_adjustment function in uc_coupon for EACH coupon line item in the order, but uc_coupon_tax_adjustment processes ALL coupons each time it is called, so if you have 2 coupons applied it will take the discounts off the taxable amount twice.

To fix it I just modified the uc_taxes_apply_tax function so that it only calls the adjustment once, so this:

  if (is_array($order->line_items) && is_array($taxed_line_items)) {
    foreach ($order->line_items as $key => $line_item) {
      if ($line_item['type'] == 'tax') {
        // Don't tax old taxes.
        continue;
      }
      if (in_array($line_item['type'], $taxed_line_items)) {
        $callback = _uc_line_item_data($line_item['type'], 'tax_adjustment');
        if (isset($callback) && function_exists($callback)) {
          $taxable_amount += $callback($line_item['amount'], $order, $tax);
        }
        else {
          $taxable_amount += $line_item['amount'];
        }
      }
    }
  }

became:

  $coupons_processed = false;
  if (is_array($order->line_items) && is_array($taxed_line_items)) {
    foreach ($order->line_items as $key => $line_item) {
      if ($line_item['type'] == 'tax') {
        // Don't tax old taxes.
        continue;
      }
      if (in_array($line_item['type'], $taxed_line_items)) {
        if ($line_item['type']=='coupon' && $coupons_processed) {
          // don't process coupons again
        } else {
          $callback = _uc_line_item_data($line_item['type'], 'tax_adjustment');
          if (isset($callback) && function_exists($callback)) {
            $taxable_amount += $callback($line_item['amount'], $order, $tax);
          }
          else {
            $taxable_amount += $line_item['amount'];
          }
          if ($line_item['type']=='coupon') $coupons_processed = true;
        }
      }
    }
  }

That's in uc_taxes (modules/ubercart/uc_taxes/uc_taxes.module) at about line 600.

Regards
Hans

alou’s picture

Thanks hanskuhn, I was having a similar issue and that helped me solve it much faster than I thought!

wodenx’s picture

Thanks for tracking this down. Generalized patch submtted to the uc issue queue at #1856596: Call each line item "tax_adjustment" callback only once..

wodenx’s picture

Status: Active » Postponed

Posponed until the patch above is reviewed.

neilsky’s picture

I installed Ubercart 3, four years after this posting, and the cart still contains exactly the same error. I spent five hours running endless cart options to try and discover why sometimes the GST was wrong, concluding (like hanskuhn) that more than one coupon meant ALL coupons were processed N times; where N is the number of coupons.

Disappointing that Ubercart is still shipping latest version with serious monetary bug in it.

A *big thankyou* to hanskuhn for providing an elegant solution, works a treat!

jberg1’s picture

Thank You! hanskuhn
Worked like a charm still (4 years later).

neilsky’s picture

Sadly, hanskuhn's excellent fix for this multi-coupon tax bug didn't get built into the latest Ubercart release. I had to edit the fix in again after installing the update.

Is there a way to ensure that the fix gets built into the next Ubercart release?