Skip to content

Commit

Permalink
[-] BO : FixBug #PSCSX-3570 - Fix tax calculation with free shipping
Browse files Browse the repository at this point in the history
  • Loading branch information
jnadaud committed Oct 21, 2014
1 parent 9d324ca commit f5d905d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
16 changes: 12 additions & 4 deletions classes/PaymentModule.php
Expand Up @@ -30,7 +30,7 @@ abstract class PaymentModuleCore extends Module
public $currentOrder;
public $currencies = true;
public $currencies_mode = 'checkbox';

const DEBUG_MODE = false;

public function install()
Expand Down Expand Up @@ -155,13 +155,13 @@ public function validateOrder($id_cart, $id_order_state, $amount_paid, $payment_
{
if (self::DEBUG_MODE)
PrestaShopLogger::addLog('PaymentModule::validateOrder - Function called', 1, null, 'Cart', (int)$id_cart, true);

if (!isset($this->context))
$this->context = Context::getContext();
$this->context->cart = new Cart($id_cart);
$this->context->customer = new Customer($this->context->cart->id_customer);
$this->context->language = new Language($this->context->cart->id_lang);
$this->context->shop = ($shop ? $shop : new Shop($this->context->cart->id_shop));
$this->context->shop = ($shop ? $shop : new Shop($this->context->cart->id_shop));
ShopUrl::resetMainDomainCache();
$id_currency = $currency_special ? (int)$currency_special : (int)$this->context->cart->id_currency;
$this->context->currency = new Currency($id_currency, null, $this->context->shop->id);
Expand Down Expand Up @@ -326,7 +326,7 @@ public function validateOrder($id_cart, $id_order_state, $amount_paid, $payment_

if (self::DEBUG_MODE)
PrestaShopLogger::addLog('PaymentModule::validateOrder - Order is about to be added', 1, null, 'Cart', (int)$id_cart, true);

// Creating order
$result = $order->add();

Expand Down Expand Up @@ -791,6 +791,14 @@ public function validateOrder($id_cart, $id_order_state, $amount_paid, $payment_
die($error);
}
} // End foreach $order_detail_list

// Update Order Details Tax in case cart rules have free shipping
foreach ($order->getOrderDetailList() as $detail)
{
$order_detail = new OrderDetail($detail['id_order_detail']);
$order_detail->updateTaxAmount($order);
}

// Use the last order as currentOrder
if (isset($order) && $order->id)
$this->currentOrder = (int)$order->id;
Expand Down
21 changes: 15 additions & 6 deletions classes/order/OrderDetail.php
Expand Up @@ -346,8 +346,17 @@ public function saveTaxCalculator(Order $order, $replace = false)
if ($order->total_products <= 0)
return true;

$shipping_tax_amount = 0;

foreach ($order->getCartRules() as $cart_rule)
if ($cart_rule['free_shipping'])
{
$shipping_tax_amount = $order->total_shipping_tax_excl;
break;
}

$ratio = $this->unit_price_tax_excl / $order->total_products;
$order_reduction_amount = $order->total_discounts_tax_excl * $ratio;
$order_reduction_amount = ($order->total_discounts_tax_excl - $shipping_tax_amount) * $ratio;
$discounted_price_tax_excl = $this->unit_price_tax_excl - $order_reduction_amount;

$values = '';
Expand All @@ -374,14 +383,14 @@ public function saveTaxCalculator(Order $order, $replace = false)

if ($replace)
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'order_detail_tax` WHERE id_order_detail='.(int)$this->id);

$values = rtrim($values, ',');
$sql = 'INSERT INTO `'._DB_PREFIX_.'order_detail_tax` (id_order_detail, id_tax, unit_amount, total_amount)
VALUES '.$values;

return Db::getInstance()->execute($sql);
}

public function updateTaxAmount($order)
{
$this->setContext((int)$this->id_shop);
Expand Down Expand Up @@ -599,10 +608,10 @@ protected function create(Order $order, Cart $cart, $product, $id_order_state, $

// Set order invoice id
$this->id_order_invoice = (int)$id_order_invoice;

// Set shop id
$this->id_shop = (int)$product['id_shop'];

// Add new entry to the table
$this->save();

Expand Down
28 changes: 9 additions & 19 deletions classes/order/OrderInvoice.php
Expand Up @@ -312,6 +312,14 @@ public function getProductTaxesBreakdown($order = null)

// sum by taxes
$tmp_tax_infos = array();
$shipping_tax_amount = 0;
foreach ($order->getCartRules() as $cart_rule)
if ($cart_rule['free_shipping'])
{
$shipping_tax_amount = $this->total_shipping_tax_excl;
break;
}

foreach ($taxes_infos as $tax_infos)
{
if (!isset($tmp_tax_infos[$tax_infos['rate']]))
Expand All @@ -320,11 +328,9 @@ public function getProductTaxesBreakdown($order = null)
'name' => 0,
'total_price_tax_excl' => 0
);

$ratio = $tax_infos['total_price_tax_excl'] / $this->total_products;
$order_reduction_amount = $this->total_discount_tax_excl * $ratio;
$order_reduction_amount = ($this->total_discount_tax_excl - $shipping_tax_amount) * $ratio;
$tmp_tax_infos[$tax_infos['rate']]['total_amount'] += ($tax_infos['total_amount'] - Tools::ps_round($tax_infos['ecotax'] * $tax_infos['product_quantity'] * $tax_infos['ecotax_tax_rate'] / 100, _PS_PRICE_DISPLAY_PRECISION_));

$tmp_tax_infos[$tax_infos['rate']]['name'] = $tax_infos['name'];
$tmp_tax_infos[$tax_infos['rate']]['total_price_tax_excl'] += $tax_infos['total_price_tax_excl'] - $order_reduction_amount - Tools::ps_round($tax_infos['ecotax'] * $tax_infos['product_quantity'], _PS_PRICE_DISPLAY_PRECISION_);
}
Expand All @@ -333,22 +339,6 @@ public function getProductTaxesBreakdown($order = null)
foreach ($tmp_tax_infos as &$tax)
$tax['total_amount'] = Tools::ps_round($tax['total_amount'], _PS_PRICE_DISPLAY_PRECISION_);

// Add shipping tax amount to tax infos if we have a free shipping cart rule
foreach ($order->getCartRules() as $cart_rule)
if ($cart_rule['free_shipping'])
{
$shipping_tax_amount = $this->total_shipping_tax_incl - $this->total_shipping_tax_excl;

if ($shipping_tax_amount > 0)
{
$tmp_tax_infos[$tax_infos['rate']]['total_amount'] += $shipping_tax_amount;
$tmp_tax_infos[$tax_infos['rate']]['name'] = $tax_infos['name'];
$tmp_tax_infos[$tax_infos['rate']]['total_price_tax_excl'] += $this->total_shipping_tax_excl;
}

break;
}

return $tmp_tax_infos;
}

Expand Down

0 comments on commit f5d905d

Please sign in to comment.