After using the coupon module, latest dev version, drupal 5 latest, php5, there is a bug related to tax calculations. The first time, after inputting a correct coupon code, the sales tax does not reflect the total minus the coupon (review order screen). If I hit back button, then submit to go back to review order, the tax is correct -- it reflects the coupon discounted amount. I have "Coupon Code" selected in my tax settings as well.

Comments

chrisschaub’s picture

More info. A stale order object is being processed by the sales tax workflow.

In uc_taxes_action_apply_tax, I just added a line to re-fetch the order object to make sure we have all of the latest line items.

function uc_taxes_action_apply_tax($order, $tax) {
  $order = uc_order_load($order->order_id);
...

This seems to fix the tax calculations for uc_coupon. So, is this something that should be fixed in uc_taxes, to get the latest order object before trying to calc taxes. Or is there a way within uc_coupon that I could update the order object after adding a line item? I've tried all of the system weight tricks to get uc_coupon to fire before workflow or taxes -- none work. So, any way to update the order after doing a line item insert?

Within the uc_coupon_order hook, should I just add a ...

$arg1 = uc_order_load($arg1->order_id);

after I do my insert of order line items? This works, but is there a better way?

You also need to change the function argument $arg1 to &arg1 in the hook uc_coupon_order. Otherwise, the order object won't be updated. This is the correct use of the hook anyway according to the ubercart api docs.

This ...

function uc_coupon_order($op, $arg1, $arg2) {

Becomes this ...

function uc_coupon_order($op, &$arg1, $arg2) {

Maybe uc_coupon should just update the order object just to make sure the insert or update of coupons gets counted in the subtotal and tax total the first time?

longwave’s picture

Status: Active » Postponed (maintainer needs more info)

I think this was due to the way the line items were added to the order, please re-test with 1.6-beta1.

chrisschaub’s picture

No, this does not fix the bug. The approach is better from a coding perspective, but the bug remains. The problem is that taxes are calculated via a workflow event. The calculation does not reflect the coupon on the first try -- from the "review order" screen you have to select "back" and then go to review order again. Then the taxes are correct, reflecting the coupon. The fix should be to either update the order object in the uc_coupon module to get the discount line item or do the same in the uc_taxes workflow function. Either will work, not sure of the best approach -- see comment #1 for both approaches.

I've also tested changing the module weight so uc_coupon gets fired before either workflow or order -- doesn't help since uc_taxes is getting passed a stale order object.

longwave’s picture

I realised that using the uc_taxes approach for applying coupons was more complicated than necessary. It's much simpler to add the coupon as soon as we know it is valid, so this work is now done directly in the checkout pane process callback; this appears to be the same method that uc_quotes uses to apply shipping costs.

I have tested this with uc_taxes enabled and the order review page now seems to be correct the first time it is loaded.

CVS diff: http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/uc_coupon/u...

chrisschaub’s picture

Nice, this fixes the problem. It would be really cool if the coupon could update the preview totals on the checkout screen after it is entered. Maybe a "Get Coupon" button. Thanks for the fix.

longwave’s picture

Status: Postponed (maintainer needs more info) » Fixed

Thanks for the report. Please open a separate feature request for the order preview functionality :)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.