Vote up!
Vote down!

How to perform a complex calculation of order's final cost, depending on different VATs & discounts

Here's is what I want to accomplish:

- There are 2 VAT taxes in my country: VAT1:6.5% & VAT2: 23%. Every product has to be matched with 1 of these 2 VATs (taxonomy term reference field in product type?)
- A discount will always be available, depending on total order's amount (e.g. €0,01-€50,00: -5%, €50,01-€80,00: -8%, €80,01-€110,00: -10% etc)

What I want to accomplish:
- Customer adds products to cart and heads to checkout. According to order's total (addition of products' base prices), the appropriate discount will be applied.
- Final Price = (Products' base prices + their total VATs)*Discount + Shipping Cost

Shopping cart includes:
- Product 1: €15 base price - 23% VAT
- Product 2: €20 base price - 6.5% VAT
Checkout Process:
- Total base price = €15 + €20 = €35. As a result, the customer will be awarded with a -5% discount (see available discount rates above)
- Order's total including products' VATs = (€15 + €15*0.23) + (€20 + €20*0.065) = €18.45 + €21.3 = €39.75
- The -5% sale scale is applied: €39.75 - €39.75*0.08 = €36.57 and that's what the customer has to pay for the products in his/her cart
- Order's weight is calculated and a shipping flat rate is applied (I have this functionality already working). As a result, the shipping cost is added to €36.57 and we finally get the sum that the customer will have to pay using one of available payment methods.

I would appreciate your suggestions on how to make this happen. Tricky parts are that different products can have different VAT (23% or 6.5%). Also, the discount has first to be calculated depending on base prices and then applied to full (inclusing VAT) prices. It would also be nice if VATs & discount were displayed to customer in order's review.

Asked by: sagan_gr
on November 5, 2012

2 Answers

Vote up!
Vote down!

The problem you have here is that you need to calculate the base price + tax

This is an interesting case study and a common problem.

As you probably know all of this needs to be done in rules and you will need to use components.

The main step is to sum the the vat inclusive price of all the product line items on the order as you do not want to include shipping or the discount (especially the discount as this could end up in a sort of recursive loop where adding the discount takes the total down to less than the discountable price).

First you will need an action set takes line items calculates the VAT on them and sums them.
A rule component that takes a line item, checks it is a product and adds it to list of line items.
An action set that takes line items, first with a loop that calls the rule above and then after the loop calls the action set above. This action set will return the total price of the products on the order.

Add this to the actions of your discount rule. Then using Conditional Rules (http://drupal.org/project/rules_conditional) you can use an if statement on the products total and pace the apply discount action inside it.

You may also want to look at this issue on Drupal Commerce, Add rules actions for applying discounts to prices with VAT included.

David Kitchen
Answer by: David Kitchen
Posted: Nov 5, 2012
Vote up!
Vote down!

Thank you for your responce,
Are you sure that the procedure you're describing above applies to my issue?

1. Define the discount scale by adding the base prices of the products in the cart, excluding tax.
2. Add correct tax to each product and
3. Finally, apply the discount to order's total (ie to all product prices added, including their taxes).

Answer by: sagan_gr
Posted: Nov 5, 2012


I was going down a different route, as it is a % discount was working on this:

  1. Define the discount scale by adding the base prices of the products in the cart, including tax
  2. Apply the discount to al products.
  3. Calculate the tax on the discounted product.

As I thought you needed the sum of the non-discounted prices including tax, as the tax rate varies on each product.

- David Kitchen on November 6, 2012

I need to calculate the discount by adding all base prices, excluding tax. Then apply each product's tax and finally apply the pre-calculated discount.

- sagan_gr on November 6, 2012