PrestaShop/PrestaShop — pull request #695 by MyBB-Services (2013-09-04 18:40:32+00:00)
Title: [-] Project : Fix #PSCFV-9181

Reminder
========

The issue was a wrong tax break-down on orders using cart rules with free shipping (see http://forge.prestashop.com/browse/PSCFV-9181)

Analysis
=======

Basically, the total_discounts_tax_excl was substracted from the total_products, without taking into account that part of the discount was not applying to products at all, but to the shipping fee. For example, worst case scenario : a cart rule with two actions :

* -10€ tax incl on the whole order
* Free shipping

Let's assume our shipping fee was 2,39€ tax incl.
The tax breakdown was substracting 12.39€ tax incl (french tax excl : 10.35€) to the total_products instead of 12.39 - 2.39 (free shipping discount) = 10€ tax incl (french tax excl : 8.36€).
This made the entire breakdown go wrong.

The issue was actually happening at two different steps :

* The wrong total products tax excl in the breakdown was calculated in OrderInvoice, when generating the invoice
* The wrong tax detail was calculated and stored in ps_order_detail_tax at the Order valdiation step for later use

Solution
======

In order to fix the two failure points, we had to determine if the order had free shipping, and if it was the case, substract total_shipping_tax_excl from total_discounts_tax_excl. To ease the process, we added a new method on Order : hasFreeShipping. It relies on Order::getCartRules but has a fallback on Cart::getCartRules (at the time it's called in OrderDetail, ps_order_cart_rule is not populated yet, so Order::getCartRules() returns an empty array).

Regards,

PS : My editor automagically removed a lot of extra white spaces, sorry about that ! It may be cleaner this way though :)