As we talked about, commerce_shipping should support tax, both tax-inclusive shipping rates and applying tax separately.

Here's a very rough, probably not working patch. Not sure about base_price VS quote for the first component.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bojanz’s picture

FileSize
8.94 KB

This should be slightly less ugly. Still seems broken.

guy_schneerson’s picture

Subscribing

googletorp’s picture

Status: Needs work » Fixed

From the initial work by bojanz I've have added this to Commerce shipping.

• A rule that will be called after the creation of each shipping line item that the quote plugin creates, which can be used to add tax on shipping line items: 861e106
• Allow quote plugins to return price instead of amount which will be a fully populated commerce price with components 75a1377
• Added the option to write prices much like products as an amount with a specific tax rate included, by utilizing the above feature: 03d525c.

bojanz’s picture

Yay! I've just tested the final version, and it works.
Thank you for your time and effort, googletorp, it is very appreciated.

bojanz’s picture

Status: Fixed » Needs work

Reopening to mention the point from IRC:

bojanz: just one thing, I've noticed that we're still adding the "include tax" setting per currency, but it's not used later on?
bojanz: calculate_quote() only uses $settings['include_tax']
bojanz: no mention of $settings[$currency_code . '_include_tax']
bojanz: so either kill the per-currency setting, or bring back my function for getting the correct setting

I wonder if as a followup we should add code to configure default shipping tax rules for all defined tax types (perhaps even some code to do it automatically when a new tax type is created, though that might be taking it too far). That's all convenience stuff so it's really minor.

googletorp’s picture

Status: Needs work » Fixed

And the fix: b710ace

Status: Fixed » Closed (fixed)

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

dwkitchen’s picture

How did you use the event "Calculate the shipping quote for a line item."

For now I have tried adding it to the Calculate Taxes: VAT Rule and it looks like this is going to work

jazzdrive3’s picture

Ok, I'm a little confused. I've bounced back and forth between several issues regarding taxing the shipping rate. What steps do we have to take to get this to work. Is there a setting we need to check? A new rule we need to create?

Thanks.

bojanz’s picture

Shipping 2.x has a "Calculation rules" tab in Store -> Configuration -> Shipping. Just add a tax rule there and you're done.

jazzdrive3’s picture

FileSize
168.04 KB

I did the following, and taxes are still not applied on shipping. See attached.

What exactly should the "tax rule" look like?

Thanks.

jazzdrive3’s picture

FileSize
168.04 KB

Well that attachment didn't work.

Anonymous’s picture

jazzdrive3 - Still can't see your attachment.

I also searched for ages to work out how to do this. I'm using Shipping 1.0 as that is what is in MartPlug.

#8 finally got me on the right track.

Configuration > Workflow > Rules > Calculate taxes: VAT > Edit

In Events:
Add event: of type > "Calculate the shipping quote for a line item" (Should already have "Calculating the sell price of a product", leave this in).

That's it, assuming your VAT is already set up to calculate on the product items.

Still doesn't include the VAT in the actual Shipping price, but at least it gets put into the VAT total.

Cheers
Peter

jazzdrive3’s picture

Here is what I finally did to get it to work.

I created a new shipping calculation rule, and in that rule, as an action, I added the rule component "rule: Calculate Indiana Sales Tax".

This did the trick.

And then I did a hook_commerce_price_component_type_info_alter to change the order of the line items so shipping was above the tax amount.

jazzdrive3’s picture

Category: feature » support

Here's the thing now:

Everything works on the first time through. After putting in a shipping address in a taxable state and going to the next page, taxes are calculated fine.

But if I go back, change the shipping address to a state that should not have tax, and then continue, taxes are still applied to the shipping, even though it is still subject to the same tax rule component and conditions as product line items. And the taxes on product line items are removed correctly as they should be.

I guess the shipping calculation rule isn't getting called again? Is there a way to trigger that, or an additional event I should be tying into?

Thanks!

dwkitchen’s picture

I guess its not triggering a shipping recalculation - is that dependant on country not state in your setup?

jazzdrive3’s picture

Dependent on state. And yeah, that's probably all it is, the shipping calculation rules are not triggered the second time it goes through.

Update: It only invokes the rules on the event when creating a new shipping line item. Being that it is not creating a new line item the next time through, the calculation rules do not get invoked. This is found on line 560 in function commerce_shipping_service_rate_calculate of the main .module file.

rtdean93’s picture

jazzdrive3 - Can you provide an example of your form alter?

"And then I did a hook_commerce_price_component_type_info_alter to change the order of the line items so shipping was above the tax amount."

jazzdrive3’s picture

rtdean93,

Here's my hook:

function module_commerce_price_component_type_info_alter(&$component_types) {
  //we change the weight of the tax line item, so it is shown last in cart and checkout displays.
  $component_types['tax|indiana_sales_tax']['weight'] = 30;
}
jazzdrive3’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Category: support » bug
Status: Closed (fixed) » Active

Here's the issue now:

I have a shipping calculation rule that adds tax, and that works fine. And adding the line item refresh hook below, it even works when they don't put in a taxable state address the first time, go back, change their state address to one that IS taxable. That part works great.

function prosco_extra_commerce_cart_line_item_refresh($line_item, $order_wrapper) {
  if ($line_item->type == "shipping") {
    rules_invoke_all('commerce_shipping_calculate_rate', $line_item);
  }
}

But it doesn't work the other way. Taxes are not REMOVED properly. Here's the sequence on our two step checkout.

1. They enter their shipping address in Indiana, which means the order is taxable. They choose their shipping rate, and then click next to go to the next step of checkout.
2. The shipping rate and correct taxes are added to the order just fine. But, now they need to change their shipping address, because they made a mistake, so they click "go back."
3. They change their shipping address to a state that we do not tax. So taxes should be removed from the order. They click continue.
4. On the next page, taxes have been removed from the products, but there is still a tax showing because it still exists on the shipping line item. So tax is not getting removed properly.

And here is where it gets really interesting.

If I add a step 5, and click "go back" again, and then just click Continue again without changing anything, the taxes are then removed correctly from the shipping line item. So its like it needs an extra request to accomplish this. Not sure why.

Thanks.

googletorp’s picture

Status: Active » Fixed

Sounds like you have misconfigured your tax rules. If you apply the taxes to the shipping line item, which will be removed when you re-submit the shipping pane, you should be fine.

If you have a custom flow, where it's possible to change something that will influence taxes on shipping line items after they are created, you will need to add a rule or some code that can handle those changes, since taxes for shipping line items are only added once and not recalculated later on by the shipping module.

Either way, this is not a bug in the shipping module, but something you need to configure to match how your store works.

jazzdrive3’s picture

Status: Fixed » Active

googletorp,

The tax rules are not misconfigured, unless I'm missing something obvious.

Taxes are applied and added correctly during the 'commerce_shipping_calculate_rate' event. The first time you go through.

I change the address so taxes should not be applied, and submit again, which includes submitting the shipping pane again. Taxes are removed from all other items correctly, but not shipping.

You have to go back and submit the previous page, which has the shipping pane, again to get it to finally take the taxes off.

The shipping pane is being submitted. But for some reason, you have to submit it twice to get the taxes removed. See my previous comment.

This is my checkout flow.

On checkout page one:

-cart contents
-billing address
-shipping address
-shipping rates pane

On checkout page two:

-review order
-payment

googletorp’s picture

Category: bug » support
googletorp’s picture

Status: Active » Fixed

Most likely the bug is that you check the address on the order which is not yet updated when the form is submitted. For 1.x it's possible to get the correct address through a custom shipping method, for 2.x you need to have the shipping selection on a separate step after address selection.

So to keep it short, the problem you are seeing is due to the way you have configured things and how the module is currently designed.

You can fix this by changing the configuration or making some custom code to make up for your shipping logic. This is one of the reasons why the 2.x branch is on a beta release.

dwkitchen’s picture

jazzdrive3,

I created a custom module - but now you can use this: Commerce Checkout Pages http://drupal.org/project/dc_co_pages

I had the shipping address page first before the cart as tax doesn't apply to products when they are being exported outside the EU so needed the country set before the product list part.

So:

Page 1
-Shipping Address

Page 2
-Cart Contents
-Shipping Method

Page 3
-Review
-Payment Details

jazzdrive3’s picture

Ok, thanks. We can't move the pane for various requirements so it will need to stay where it is.

Is there a hook or series of hooks I should focus on, or should I just do something to the module itself? And perhaps see about submitting a patch for this use case?

Status: Fixed » Closed (fixed)

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

idflood’s picture

Status: Closed (fixed) » Active

Sorry to reopen this issue but it's not really easy to add tax to shipping.

I've followed the video by Randy Fay to have shipping by price, but how would you add tax calculation to it?

I've used some crazy workaround to have it working how I expected but I'm sure there is a much better approach.
The workaround I've used it to directly calculate the VAT in the shipping rule "Cost by item". I've used some actions "Calculate a value" and hardcoded the tax rate in them to have the "unit_price - tax" and "tax" values. There is also one extra action "Add an amount to the unit price" on the tax component.

edit: The screencast I've followed http://vimeo.com/33838479

rszrama’s picture

Status: Active » Closed (fixed)
FileSize
51.54 KB

Moving this back closed; it's essentially the same as adding taxes to line items, but you do it as a shipping rate calculation rule. Find the interface at Administration » Store » Configuration » Shipping. It's the far right tab called "Calculation rules." Here you can add a rule that applies a tax directly to your shipping line items.

See this for example:

There should be no reason for you to manually calculate taxes using generic Rules features, because the Tax module itself defines components for applying tax rates to shipping. Notice the name of the action I used; it's at the very bottom of the actions list.

bensti’s picture

#29 work very well ! thx you

ShaneOnABike’s picture

BRILLIANT! Would be great to add this to the online documentation it saved my ars

dpearcefl’s picture

Perhaps I'm not following this proposed solution, but this would seem to just increase the shipping costs by the calculated tax amount. So if you want to run a report that prints out how much sales tax you collected, it wouldn't include this amount since it is "shipping costs".

rszrama’s picture

The tax report will include it because it's based on Order Total price field components. Even if the tax is on the shipping line item, its components will be preserved in the order total.

dpearcefl’s picture

Sorry to keep bothering folks. I'm glad some people have been able to make this work, but I guess I need a little more information.

1) When you add a new action, what do you select for "Select the Action to add"? I've tried "Rule: Calculate" and "Rule: Rate" with no change.

dpearcefl’s picture

Status: Closed (fixed) » Active

Sorry to open this task, but I really need to understand how to set this up. And I suspect other folks later might have the same question. I'm trying to figure out how #29 is set up.

1) When you add a new action, what do you select for "Select the Action to add"?

rszrama’s picture

Status: Active » Closed (fixed)

See the "rule: Calculate SC Sales Tax" at the bottom? That's me invoking the rules component that was created when I added the tax rate. Look for it at the bottom of your action list.

dpearcefl’s picture

I have it working now. I did try that action, but it didn't work for me initially.

What I am seeing is that if you add a shipping calculation, it does not affect any orders already in the shopping cart. You have to clear out the shopping cart and then add some items and the new calculation will appear.

rszrama’s picture

Right, shipping does not tie into the shopping cart refresh, but if you were to go to checkout it would recalculate as well.

PQ’s picture

Hi, I'm struggling to get this to work in a situation where shipping prices are changed from the base price via a calculation rule, It seems that where a tax item is added, it initially adds a tax component to commerce-line-item:commerce-unit-price:data and then applies any price change as a separate component and I can't seem to get the tax component to update to reflect the different shipping price.

I've tried using "Apply a tax rate to a line item", "Calculate taxes for a line item" and "Rule: Calculate VAT" after setting the shipping price, but none of them has made any difference. I've also tried modifying the base price at the same time as modifying the shipping service price, and that didn't work either.

Below is the current state of the rule in question and I've attached a screenshot of a dpm of the resulting commerce-unit-price object.

{ "rules_priority_delivery_northern_ireland_rate" : {
    "LABEL" : "Priority delivery - Northern Ireland rate",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "geographical", "ni", "priority", "shipping" ],
    "REQUIRES" : [ "rules", "commerce_line_item", "devel", "commerce_shipping" ],
    "ON" : [ "commerce_shipping_calculate_rate" ],
    "IF" : [
      { "data_is" : {
          "data" : [ "commerce-line-item:line-item-label" ],
          "value" : "Priority Delivery"
        }
      },
      { "entity_has_field" : {
          "entity" : [ "commerce-line-item:order" ],
          "field" : "field_delivery_region"
        }
      },
      { "data_is" : {
          "data" : [ "commerce-line-item:order:field-delivery-region" ],
          "value" : "ni"
        }
      }
    ],
    "DO" : [
      { "commerce_line_item_unit_price_amount" : {
          "commerce_line_item" : [ "commerce_line_item" ],
          "amount" : "1499",
          "component_name" : "flat_rate_priority",
          "round_mode" : "1"
        }
      },
      { "component_commerce_tax_rate_vat" : { "commerce_line_item" : [ "commerce_line_item" ] } },
      { "devel_debug" : { "value" : [ "commerce-line-item:commerce-unit-price" ] } }
    ]
  }
}
PQ’s picture

I've created a new issue for #39 at #2065543: tax on shipping prices where shipping price is altered by a calculation rule, apologies for hijacking this issue.

Summit’s picture

Status: Closed (fixed) » Active

Hi,
Sorry to set this to active. I do not need Sales tax, but flat VAT rate. Crossposting with this flat rate post: https://drupal.org/node/1762006
but I tried the rule in #29 with latest 2.dev, could there be a bug somewhere?

1) I added a shipping rate of 15 Baseprice (18,.15 inclusive VAT). I checked my Tax rate in
admin/commerce/config/shipping/services/flat-rate-within_holland/edit
I saw this underneath: Saving a rate tax inclusive will bypass later calculations for the specified tax.

2) My rule is the following:

{ "rules_taxes_shipping" : {
    "LABEL" : "Taxes Shipping",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "Shipping", "taxes" ],
    "REQUIRES" : [ "commerce_tax", "commerce_shipping" ],
    "ON" : [ "commerce_shipping_calculate_rate" ],
    "DO" : [
      { "commerce_tax_calculate_by_type" : {
          "commerce_line_item" : [ "commerce_line_item" ],
          "tax_type_name" : "VAT"
        }
      }
    ]
  }
}

It just won't work.
I do not see Shipping (excl VAT 15): 18.15
Only If I set the Base Amount to 18.15 and do not select a VAT within the shipping service, the Checkout View Shows 18.15

This my rule output. It seems the Taxes Shipping rule is not fired...


" Reacting on event Calculating the sell price of a product.
0 ms Reacting on event Calculating the sell price of a product.
0.206 ms Evaluating conditions of rule Unset the price of disabled products in the cart. [edit]
0.363 ms The condition data_is_empty evaluated to TRUE [edit]
0.375 ms AND evaluated to FALSE.
0.406 ms Evaluating conditions of rule Calculate taxes: nl_btw_hoog. [edit]
0.417 ms AND evaluated to TRUE.
" Rule Calculate taxes: nl_btw_hoog fires. [edit]
0.754 ms Finished reacting on event Calculating the sell price of a product.

Shouldn't it not be that I set the Base Amount to 15, select a VAT rate and than with the above rule 18.15 should be shown on the checkout View?

Thanks still for replying and sorry if it is wrong to set this to active, but trying for days now to get VAT flat taxes to work with shipping items.
Greetings, Martijn

Summit’s picture

Status: Active » Closed (fixed)

Hi Finally I got this working!
I had the wrong rule.. This is my working rule

{ "rules_taxes_shipping" : {
    "LABEL" : "Taxes Shipping",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "Shipping", "taxes" ],
    "REQUIRES" : [ "rules", "commerce_shipping" ],
    "ON" : [ "commerce_shipping_calculate_rate" ],
    "DO" : [
      { "component_commerce_tax_rate_btw_hoog" : { "commerce_line_item" : [ "commerce_line_item" ] } }
    ]
  }
}

I needed the action "rule: CALCULATE "
So setting this to fixed again.
Greetings, Martijn

akosipax’s picture

Issue summary: View changes

I'm finding myself in a similar dilemma as jazzdrive3.

Referring to this comment, https://www.drupal.org/node/1243218#comment-5940592 ... I'm wondering whether this is still indeed a problem with the module now in a stable release and out of beta. Not sure if I should proceed in implementing my own shipping logic since my shipping service pane is in the same page as the shipping address pane.

aprilr’s picture

I have really struggled to get this to work. And since it took me several days, I'm sharing!

I needed to apply NC Sales tax to a Flat Rate Shipping Cost. I did the following:

1) Create NC Sales Tax Rate
Condition: Order address component comparison (Shipping state is NC)
and
Condition: Not line item contains specific product type: Giftcard (no tax on gift cards)

Actions: Apply a tax rate to a line item (commerce-line-item)

2) Create New Shipping Tax Type called "Shipping Tax".
Event: Calculating the sell price of a product

Elements: NONE

3) Create NC Shipping Sales Tax Rate called "Shipping Sales Tax"
Condition: Order address comparison (Shipping state is NC)

Actions: Apply a tax rate to a line item (commerce-line-item)

4) Create New Shipping Calculation Rule called "Calculate NC Sales Tax"
Event: Calculating a shipping rate

Actions: rule: Calculate Shipping Sales Tax (commerce-line-item)

Subtotal $36.00
NC Sales Tax $1.71
Shipping Sales Tax $0.48
Flat Rate Shipping $10.00
Order total $48.19

Now to just get the Shipping Sales Tax to fall below the Flat Rate Shipping line. :-)

Thanks!