As a follow up on #597992: Discount not reflected on total order (FIXED for Trigger: Calculate order discounts) I've spent the last 3 days analyzing the problem and I'm quite certain, that it is a problem in UC_VAT, not in UC_DISCOUNT.

It is going to be a long description but I'm sure it will help resolving the issue and getting closer to a resolution for everyone.

Here it goes:

  • Node View and Cart
    Display of product price including VAT is correct, also for discounted product prices.
  • Checkout
    The product prices and VAT information gets displayed twice. In both cases the discounted prices are correct, the VAT is correct in one case and wrong in the other. In detail:
    • The first pane (id=cart, callback=uc_checkout_pane_cart) is calling theme_uc_vat_cart_review_table() and that calculates the price and the VAT correctly.
    • The second pane (id=payment, callback=uc_checkout_pane_payment) is empty at the beginning and calls getTax() in JS when a payment methode got selected. getTax() is calling serializeOrder() in uc_payment.js and that uses JQuery $("[name=cart_contents]")) to retrieve the order object from a hidden html field. This order object is posted to the server using AJAX to receive the VAT value.
      The order object was produced by uc_cart_get_contents().
      On the server, the order object is received and the function uc_vat_taxes_javascript() is called to calculate the VAT. This is going wrong as it doesn't call the uc_price() as it has been doing in theme_uc_vat_cart_review_table(). So, the VAT returned to the browser from this function is wrong, if the price was discounted.
  • Review
    Prices and VAT get calculated and themed in uc_checkout_pane_payment() and it's doing that based on the passed on parameter $args1 and that includes the line items. Totals are correct (=discounted) but the tax item is wrong, it's based on the original prices not the discounted ones. I couldn't find out yet, if the UC_VAT module plays a role in this anywhere, although I'm sure it should do.

In summary this reads like this:

  1. uc_vat displays in node view, cart and checkout (cart pane) discounted prices and vat correct.
  2. uc_vat displays in checkout (payment pane) discounted prices correct and vat wrong.
  3. uc_vat displays in review discounted prices correct and vat wrong.

It also appears that making the calculation of the discounted price and the associated tax is done in various places which makes maintenance a bit difficult. May I suggest to put that all together into one place? If I can be of any help, please let me know. It sounds like a big issue but the module isn't that far from being complete on this.

CommentFileSizeAuthor
#3 677868-uc_taxes.patch1.12 KBlongwave
#3 677868-uc_vat.patch1.34 KBlongwave

Comments

DolfAndringa’s picture

I confirm this problem.
I am using product discounts, based on coupon codes, with the UC Discounts Framework. On the "calculate order discounts" trigger, I defined the "Apply a discount" action which applies 10% product discounts (not order discounts) to some products. I listed the discount price handler above the VAT price handler.
When I enter the coupon code in the coupon checkout pane during checkout and then continue the checkout process to the payment method pane, I see undiscounted prices, but with the VAT, based also on the undiscounted prices. If there would be no discount, this would be correct, but since a discount coupon was entered earlier, I would expect the discount to be applied here already, and see the VAT based on the discounted prices.
If I then continue to the order review page, I see the discounts applied correctly, and they are (correctly) based on the product price excluding VAT. So the discount is applied correctly, but the VAT listed there, is still the old VAT amount, based on the undiscounted prices. This is wrong. It should be recalculated, based on the discounted price.

The combination of the UC Discounts Framework together with the UC VAT module seems to be a promising, and also the only, solution to use Ubercart in Europe with European (in this case Dutch) VAT rules, together with discount coupons. If this problem is solved, the solution would be complete as far as I am concerned.
Cheers,

Dolf.

longwave’s picture

Many thanks for this in depth report, and sorry it has taken so long to get back to you. I think I may have traced further to uc_taxes.module and not uc_vat, but if possible I'd like someone else to confirm this - as you say the price calculations are done all over the place which makes debugging quite difficult!

As you found, uc_vat_uc_taxes_javascript() is called via AJAX to update the preview pane. However, this does not do any price handling itself, it just invokes hook_calculate_tax to do the work. The chain of functions goes something like:

uc_vat_uc_taxes_javascript()
uc_taxes_calculate_tax($order)
uc_taxes_action_apply_tax($order, $tax)
uc_taxes_apply_tax($order, $tax)
uc_taxes_apply_item_tax($item, $tax)

uc_taxes_apply_item_tax() returns $item->price * $item->qty if the item is to be taxed. However, this does not take price alterations into account - it looks like uc_price() should be called here to apply the discount. But we also need to ensure that tax is not added again at this point by the uc_vat price alterer; I guess some kind of flag will be needed to indicate that uc_taxes wants the altered price without tax included.

Adding a quick drupal_set_message() to uc_taxes_apply_item_tax() appears to confirm that $item->price is indeed not discounted at this point. However, I have no time to work on a patch or test any further at the moment; if someone else can confirm my findings then perhaps we can find a way of solving this.

longwave’s picture

StatusFileSize
new1.34 KB
new1.12 KB

I've just seen #597992: Discount not reflected on total order (FIXED for Trigger: Calculate order discounts) which appears to be a related problem.

Attached are two patches, one for uc_taxes and another for uc_vat, which appear to fix the "Subtotal excluding VAT" and "VAT" line items in the order preview pane. The order total is still incorrect, but can someone else confirm that this helps things? Perhaps these two patches in combination with some or all of the work in #597992: Discount not reflected on total order (FIXED for Trigger: Calculate order discounts) will finally solve this?

longwave’s picture

Version: 6.x-1.0 » 6.x-1.x-dev

Patches apply against the dev branch, changing version to indicate this.

DolfAndringa’s picture

Hi longwave, thanks for the patches, but I have no clue what they should do. I patched uc_taxes.module and uc_vat.module, but I see absolutely no difference in the behaviour of the site. I have caching disabled in drupal (and cleared it again anyway to be safe), and also cleared my browser (FF) cache (using webdeveloper toolbar).
Both the checkout pane where I see the prices together with payment method selection is unchanged, as is the order review page.
Just to be safe, I put a drupal_set_message in one of the functions of uc_vat.module which was affected by the patch, and it is indeed called a couple of times. So the changes are in effect, they just don't produce any effect in my setup.
I am running drupal 6.15 with ubercart 6.x-2.2 and uc_vat 6.x-1.0 (both patched with the above patches).
I guess I have to dig deeper into this to find out what is going wrong. But I gotta run now, hopefulle I have time tomorrow to check it out.

pauljb’s picture

Same problem with Ubercart Discounts (alternative)
Tried the patch #3, but with no luck on

jazzitup’s picture

Status: Active » Needs work

Same problem here, but longwave - you were very close with your patches to solve the issue completely.
I have applied both of your patches and these patches as well:
A) http://drupal.org/node/597992#comment-2306056
B) http://drupal.org/node/600478#comment-2145516 (modified*)

*In patch B) I didn't use values for 'type' => 'order_product' as you advised within your patch.

Here's the result of my test (Used Product Discount = 20%).

Subtotal: €984.00 (€800 excl. VAT) DISCOUNTED VALUE (predicate "Calculate product discounts")
Shipping: €129.15 (€105 excl. VAT)
Subtotal excluding VAT: €905.00
VAT: €208.15
Order total: €1,359.15

Everything seems ok, except the "Order total" that shows the wrong result by calculating:
(NON DISCOUNTED PRICE excl. VAT + SHIPPING excl. VAT) increased by VAT
(1000 + 105) * 1.23 = €1,359.15

... instead of:
(DISCOUNTED PRICE excl. VAT + SHIPPING excl. VAT) increased by VAT
(800 + 105) * 1.23 = €1,113.15

grey_’s picture

Subscribing

jazzitup’s picture

Is there any progress with solving this issue?

czeky’s picture

same here, someone solving this?

hanoii’s picture

subscribe..

this can also indicate that orders with mixed vat and non-vat products might have wrong applied discounts, not 100% confirming this but I got a report on this and It's probably related to this.

longwave’s picture

@hanoii: discounted mixed VAT rate orders are now correctly taxed in the latest uc_vat and uc_coupon (with a patch to uc_taxes) but other modules may need some work.

I don't use uc_discount, but it should be possible to support product discounts if someone can provide test results with the latest versions and then either provide a patch or sponsorship to fix it.

For order discounts, the real problem is that applying a discount to the order subtotal doesn't completely make sense when mixed rate products are involved - this is worked around in uc_coupon by applying the order subtotal discount proportionally across individual products, otherwise you would need to apply VAT at a calculated intermediate rate, which uc_taxes doesn't support.

hanoii’s picture

One of my client is needing discount between certain order total thresholds, uc_coupon does not provide that, does it? I thought of switching to uc_coupon but that hold me off.

grub3’s picture

Subscribe.

Shutnic’s picture

Subscribe.

manuelBS’s picture

subscribing.

nitebreed’s picture

Subscribing

Manurk’s picture

Subscribing.