'.$country_select.'
-
-
-
-
-
+
+
+
+
+
+
+
+
+
@@ -421,25 +457,22 @@ EOT;
foreach ($tax_rules as $tax_rule)
{
// format fields for display
+ $country_name = ($tax_rule['country_name'] == '' ? '*' : $tax_rule['country_name']);
$state_name = ($tax_rule['state_name'] == '' ? '*' : $tax_rule['state_name']);
-
$zipcodes = '*';
if (isset($tax_rule['zipcode_from']) && $tax_rule['zipcode_from'] != 0)
{
$zipcodes = $tax_rule['zipcode_from'];
if (isset($tax_rule['zipcode_to']) && $tax_rule['zipcode_to'] != 0 && $tax_rule['zipcode_to'] != $tax_rule['zipcode_from'])
- {
$zipcodes .= '-'.$tax_rule['zipcode_to'];
- }
}
$tax = ((float)$tax_rule['rate'] == 0 ? '-' : (float)$tax_rule['rate'].'%');
-
$behavior = ($tax_rule['behavior'] == 0 ? $this->l('This tax only') : $this->l('Compute with others'));
// render fields
$html .= '
- | '.Tools::htmlentitiesUTF8($tax_rule['country_name']).' |
+ '.Tools::htmlentitiesUTF8($country_name).' |
'.Tools::htmlentitiesUTF8($state_name).' |
'.Tools::htmlentitiesUTF8($zipcodes).' |
'.Tools::htmlentitiesUTF8($behavior).' |
@@ -483,4 +516,3 @@ EOT;
}
}
}
-
diff --git a/classes/Carrier.php b/classes/Carrier.php
index 229520cee..7c9217deb 100644
--- a/classes/Carrier.php
+++ b/classes/Carrier.php
@@ -781,7 +781,7 @@ class CarrierCore extends ObjectModel
{
$tax_manager = TaxManagerFactory::getManager($address, $this->id_tax_rules_group);
$tax_calculator = $tax_manager->getTaxCalculator();
- return $tax_calculator->getTaxesRate();
+ return $tax_calculator->getTotalRate();
}
/**
diff --git a/classes/Order.php b/classes/Order.php
index b1af1b1ca..2516c0423 100644
--- a/classes/Order.php
+++ b/classes/Order.php
@@ -275,14 +275,16 @@ class OrderCore extends ObjectModel
/* DOES delete the product */
protected function _deleteProduct($orderDetail, $quantity)
{
- $price = $orderDetail->product_price * (1 + $orderDetail->tax_rate * 0.01);
+ $tax_calculator = $orderDetail->getTaxCalculator();
+
+ $price = $tax_calculator->addTaxes($orderDetail->product_price);
if ($orderDetail->reduction_percent != 0.00)
$reduction_amount = $price * $orderDetail->reduction_percent / 100;
elseif ($orderDetail->reduction_amount != '0.000000')
$reduction_amount = Tools::ps_round($orderDetail->reduction_amount, 2);
if (isset($reduction_amount) AND $reduction_amount)
$price = Tools::ps_round($price - $reduction_amount, 2);
- $productPriceWithoutTax = number_format($price / (1 + $orderDetail->tax_rate * 0.01), 2, '.', '');
+ $productPriceWithoutTax = number_format($tax_calculator->removeTaxes($price), 2, '.', '');
$price += Tools::ps_round($orderDetail->ecotax * (1 + $orderDetail->ecotax_tax_rate / 100), 2);
$productPrice = number_format($quantity * $price, 2, '.', '');
/* Update cart */
@@ -413,10 +415,14 @@ class OrderCore extends ObjectModel
public function setProductPrices(&$row)
{
+ $tax_calculator = OrderDetail::getTaxCalculatorStatic((int)$row['id_order_detail']);
+ $row['tax_calculator'] = $tax_calculator;
+ $row['tax_rate'] = $tax_calculator->getTotalRate();
+
if ($this->_taxCalculationMethod == PS_TAX_EXC)
$row['product_price'] = Tools::ps_round($row['product_price'], 2);
else
- $row['product_price_wt'] = Tools::ps_round($row['product_price'] * (1 + $row['tax_rate'] / 100), 2);
+ $row['product_price_wt'] = Tools::ps_round($tax_calculator->addTaxes($row['product_price']), 2);
$group_reduction = 1;
if ($row['group_reduction'] > 0)
@@ -427,16 +433,13 @@ class OrderCore extends ObjectModel
if ($this->_taxCalculationMethod == PS_TAX_EXC)
$row['product_price'] = ($row['product_price'] - $row['product_price'] * ($row['reduction_percent'] * 0.01));
else
- {
- $reduction = Tools::ps_round($row['product_price_wt'] * ($row['reduction_percent'] * 0.01), 2);
- $row['product_price_wt'] = Tools::ps_round(($row['product_price_wt'] - $reduction), 2);
- }
+ $row['product_price_wt'] = Tools::ps_round(($row['product_price_wt'] - $row['product_price_wt'] * ($row['reduction_percent'] * 0.01)), 2);
}
if ($row['reduction_amount'] != 0)
{
if ($this->_taxCalculationMethod == PS_TAX_EXC)
- $row['product_price'] = ($row['product_price'] - ($row['reduction_amount'] / (1 + $row['tax_rate'] / 100)));
+ $row['product_price'] = ($row['product_price'] - ($tax_calculator->removeTaxes($row['reduction_amount'])));
else
$row['product_price_wt'] = Tools::ps_round(($row['product_price_wt'] - $row['reduction_amount']), 2);
}
@@ -453,7 +456,7 @@ class OrderCore extends ObjectModel
$row['product_price'] = Tools::ps_round($row['product_price'], 2);
if ($this->_taxCalculationMethod == PS_TAX_EXC)
- $row['product_price_wt'] = Tools::ps_round($row['product_price'] * (1 + ($row['tax_rate'] * 0.01)), 2) + Tools::ps_round($row['ecotax'] * (1 + $row['ecotax_tax_rate'] / 100), 2);
+ $row['product_price_wt'] = Tools::ps_round($tax_calculator->addTaxes($row['product_price']), 2) + Tools::ps_round($row['ecotax'] * (1 + $row['ecotax_tax_rate'] / 100), 2);
else
{
$row['product_price_wt_but_ecotax'] = $row['product_price_wt'];
@@ -474,6 +477,7 @@ class OrderCore extends ObjectModel
{
if (!$products)
$products = $this->getProductsDetail();
+
$resultArray = array();
foreach ($products AS $row)
{
@@ -487,6 +491,7 @@ class OrderCore extends ObjectModel
if (!$row['product_quantity'])
continue ;
}
+
$this->setProductPrices($row);
/* Add information for virtual product */
@@ -494,7 +499,7 @@ class OrderCore extends ObjectModel
$row['filename'] = ProductDownload::getFilenameFromIdProduct($row['product_id']);
/* Stock product */
- $resultArray[(int)($row['id_order_detail'])] = $row;
+ $resultArray[(int)$row['id_order_detail']] = $row;
}
return $resultArray;
}
diff --git a/classes/OrderDetail.php b/classes/OrderDetail.php
index 5b405d26c..3305f4b6b 100644
--- a/classes/OrderDetail.php
+++ b/classes/OrderDetail.php
@@ -1,6 +1,6 @@
'isFloat',
'download_nb' => 'isInt',
);
-
+
protected $table = 'order_detail';
protected $identifier = 'id_order_detail';
-
+
protected $webserviceParameters = array(
'fields' => array (
'id_order' => array('xlink_resource' => 'orders'),
@@ -157,8 +157,8 @@ class OrderDetailCore extends ObjectModel
'download_deadline' => array()
)
);
-
-
+
+
public function getFields()
{
$this->validateFields();
@@ -189,9 +189,9 @@ class OrderDetailCore extends ObjectModel
$fields['download_hash'] = pSQL($this->download_hash);
$fields['download_nb'] = (int)($this->download_nb);
$fields['download_deadline'] = pSQL($this->download_deadline);
-
+
return $fields;
- }
+ }
public static function getDownloadFromHash($hash)
{
@@ -213,6 +213,61 @@ class OrderDetailCore extends ObjectModel
return Db::getInstance()->Execute($sql);
}
+ /**
+ * Returns the tax calculator associated to this order detail.
+ * @return TaxCalculator
+ */
+ public function getTaxCalculator()
+ {
+ return OrderDetail::getTaxCalculatorStatic($this->id);
+ }
+
+ /**
+ * Return the tax calculator associated to this order_detail
+ * @param int $id_order_detail
+ * @return TaxCalculator
+ */
+ public static function getTaxCalculatorStatic($id_order_detail)
+ {
+ $sql = 'SELECT t.*, d.`tax_computation_method`
+ FROM `'._DB_PREFIX_.'order_detail_tax` t
+ LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON (d.`id_order_detail` = t.`id_order_detail`)
+ WHERE d.`id_order_detail` = '.(int)$id_order_detail;
+
+ $computation_method = 1;
+ $taxes = array();
+ if ($results = Db::getInstance()->ExecuteS($sql))
+ {
+
+ foreach ($results AS $result)
+ $taxes[] = new Tax((int)$result['id_tax']);
+
+ $computation_method = $result['tax_computation_method'];
+ }
+
+ return new TaxCalculator($taxes, $computation_method);
+ }
+
+ /**
+ * Save the tax calculator
+ * @param int $id_order_detail
+ * @param TaxCalculator $tax_calculator
+ * @return boolean
+ */
+ public static function saveTaxCalculatorStatic($id_order_detail, TaxCalculator $tax_calculator)
+ {
+ if (sizeof($tax_calculator->taxes) == 0)
+ return true;
+
+ $values = '';
+ foreach ($tax_calculator->taxes AS $tax)
+ $values .= '('.(int)$id_order_detail.','.(float)$tax->id.'),';
+
+ $values = rtrim($values, ',');
+ $sql = 'INSERT INTO `'._DB_PREFIX_.'order_detail_tax` (id_order_detail, id_tax)
+ VALUES '.$values;
+
+ return Db::getInstance()->Execute($sql);
+ }
}
-
diff --git a/classes/PaymentModule.php b/classes/PaymentModule.php
index 10fb9c1f9..233dd1812 100644
--- a/classes/PaymentModule.php
+++ b/classes/PaymentModule.php
@@ -133,10 +133,12 @@ abstract class PaymentModuleCore extends Module
$order->total_products_wt = (float)$cart->getOrderTotal(true, Cart::ONLY_PRODUCTS);
$order->total_discounts = (float)abs($cart->getOrderTotal(true, Cart::ONLY_DISCOUNTS));
$order->total_shipping = (float)$cart->getOrderShippingCost();
+
if (Validate::isLoadedObject($carrier))
$order->carrier_tax_rate = $carrier->getTaxesRate(new Address($cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')}));
$order->total_wrapping = (float)abs($cart->getOrderTotal(true, Cart::ONLY_WRAPPING));
$order->total_paid = (float)Tools::ps_round((float)($cart->getOrderTotal(true, Cart::BOTH)), 2);
+
$order->invoice_date = '0000-00-00 00:00:00';
$order->delivery_date = '0000-00-00 00:00:00';
// Amount paid by customer is not the right one -> Status = payment error
@@ -179,7 +181,7 @@ abstract class PaymentModuleCore extends Module
$productsList = '';
$db = Db::getInstance();
$query = 'INSERT INTO `'._DB_PREFIX_.'order_detail`
- (`id_order`, `product_id`, `product_attribute_id`, `product_name`, `product_quantity`, `product_quantity_in_stock`, `product_price`, `reduction_percent`, `reduction_amount`, `group_reduction`, `product_quantity_discount`, `product_ean13`, `product_upc`, `product_reference`, `product_supplier_reference`, `product_weight`, `tax_name`, `tax_rate`, `ecotax`, `ecotax_tax_rate`, `discount_quantity_applied`, `download_deadline`, `download_hash`)
+ (`id_order`, `product_id`, `product_attribute_id`, `product_name`, `product_quantity`, `product_quantity_in_stock`, `product_price`, `reduction_percent`, `reduction_amount`, `group_reduction`, `product_quantity_discount`, `product_ean13`, `product_upc`, `product_reference`, `product_supplier_reference`, `product_weight`, `tax_computation_method`, `ecotax`, `ecotax_tax_rate`, `discount_quantity_applied`, `download_deadline`, `download_hash`)
VALUES ';
$customizedDatas = Product::getAllCustomizedDatas((int)($order->id_cart));
@@ -211,14 +213,15 @@ abstract class PaymentModuleCore extends Module
}
// Exclude VAT
- if (Tax::excludeTaxeOption())
+ $tax_calculator = new TaxCalculator();
+ if (!Tax::excludeTaxeOption())
{
- $product['tax'] = 0;
- $product['rate'] = 0;
- $tax_rate = 0;
+ $address = Tax::initializeAddress($vat_address->id);
+ $id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct((int)$product['id_product']);
+
+ $tax_manager = TaxManagerFactory::getManager($vat_address, $id_tax_rules);
+ $tax_calculator = $tax_manager->getTaxCalculator();
}
- else
- $tax_rate = Tax::getProductTaxRate((int)($product['id_product']), $cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$ecotaxTaxRate = 0;
if (!empty($product['ecotax']))
@@ -227,7 +230,7 @@ abstract class PaymentModuleCore extends Module
$product_price = (float)Product::getPriceStatic((int)($product['id_product']), false, ($product['id_product_attribute'] ? (int)($product['id_product_attribute']) : NULL), (Product::getTaxCalculationMethod((int)($order->id_customer)) == PS_TAX_EXC ? 2 : 6), NULL, false, false, $product['cart_quantity'], false, (int)($order->id_customer), (int)($order->id_cart), (int)($order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}), $specificPrice, false, false);
$quantityDiscount = SpecificPrice::getQuantityDiscount((int)$product['id_product'], $this->context->shop->getID(), (int)$cart->id_currency, (int)$vat_address->id_country, (int)$customer->id_default_group, (int)$product['cart_quantity']);
$unitPrice = Product::getPriceStatic((int)$product['id_product'], true, ($product['id_product_attribute'] ? intval($product['id_product_attribute']) : NULL), 2, NULL, false, true, 1, false, (int)$order->id_customer, NULL, (int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
- $quantityDiscountValue = $quantityDiscount ? ((Product::getTaxCalculationMethod((int)$order->id_customer) == PS_TAX_EXC ? Tools::ps_round($unitPrice, 2) : $unitPrice) - $quantityDiscount['price'] * (1 + $tax_rate / 100)) : 0.00;
+ $quantityDiscountValue = $quantityDiscount ? ((Product::getTaxCalculationMethod((int)$order->id_customer) == PS_TAX_EXC ? Tools::ps_round($unitPrice, 2) : $unitPrice) - $tax_calculator->addTaxes($quantityDiscount['price'])) : 0.00;
$query .= '('.(int)($order->id).',
'.(int)($product['id_product']).',
'.(isset($product['id_product_attribute']) ? (int)($product['id_product_attribute']) : 'NULL').',
@@ -244,8 +247,7 @@ abstract class PaymentModuleCore extends Module
'.(empty($product['reference']) ? 'NULL' : '\''.pSQL($product['reference']).'\'').',
'.(empty($product['supplier_reference']) ? 'NULL' : '\''.pSQL($product['supplier_reference']).'\'').',
'.(float)($product['id_product_attribute'] ? $product['weight_attribute'] : $product['weight']).',
- \''.(empty($tax_rate) ? '' : pSQL($product['tax'])).'\',
- '.(float)($tax_rate).',
+ '.(int)$tax_calculator->computation_method.',
'.(float)Tools::convertPrice(floatval($product['ecotax']), intval($order->id_currency)).',
'.(float)$ecotaxTaxRate.',
'.(($specificPrice AND $specificPrice['from_quantity'] > 1) ? 1 : 0).',
@@ -294,6 +296,9 @@ abstract class PaymentModuleCore extends Module
$query = rtrim($query, ',');
$result = $db->Execute($query);
+ OrderDetail::saveTaxCalculatorStatic($db->Insert_ID(), $tax_calculator);
+ unset($tax_calculator);
+
// Insert discounts from cart into order_discount table
$discounts = $cart->getDiscounts();
$discountsList = '';
diff --git a/classes/tax/Tax.php b/classes/tax/Tax.php
index 530f2c70b..554f80630 100644
--- a/classes/tax/Tax.php
+++ b/classes/tax/Tax.php
@@ -25,6 +25,7 @@
* International Registered Trademark & Property of PrestaShop SA
*/
+
class TaxCore extends ObjectModel
{
/** @var string Name */
@@ -36,6 +37,9 @@ class TaxCore extends ObjectModel
/** @var bool active state */
public $active;
+ /** @var boolean true if the tax has been historized */
+ public $deleted = 0;
+
protected $fieldsRequired = array('rate');
protected $fieldsValidate = array('rate' => 'isFloat');
protected $fieldsRequiredLang = array('name');
@@ -53,6 +57,7 @@ class TaxCore extends ObjectModel
$this->validateFields();
$fields['rate'] = (float)($this->rate);
$fields['active'] = (int)($this->active);
+ $fields['deleted'] = (int)($this->deleted);
return $fields;
}
@@ -71,7 +76,22 @@ class TaxCore extends ObjectModel
{
/* Clean associations */
TaxRule::deleteTaxRuleByIdTax((int)$this->id);
- return parent::delete();
+
+ if ($this->isUsed())
+ return $this->historize();
+ else
+ return parent::delete();
+ }
+
+ /**
+ * Save the object with the field deleted to true
+ *
+ * @return bool
+ */
+ public function historize()
+ {
+ $this->deleted = true;
+ return parent::update();
}
public function toggleStatus()
@@ -84,9 +104,20 @@ class TaxCore extends ObjectModel
public function update($nullValues = false)
{
- if (parent::update($nullValues))
- return $this->_onStatusChange();
-
+ if (!$this->deleted && $this->isUsed())
+ {
+ $historized_tax = new Tax($this->id);
+ $historized_tax->historize();
+
+ // remove the id in order to create a new object
+ $this->id = 0;
+ $this->add();
+
+ // change tax id in the tax rule table
+ TaxRule::swapTaxId($historized_tax->id, $this->id);
+ } else if (parent::update($nullValues))
+ return $this->_onStatusChange();
+
return false;
}
@@ -97,20 +128,43 @@ class TaxCore extends ObjectModel
return true;
}
+
+ /**
+ * Returns true if the tax is used in an order details
+ *
+ * @return bool
+ */
+ public function isUsed()
+ {
+ return Db::getInstance()->getValue('
+ SELECT COUNT(*) FROM `'._DB_PREFIX_.'order_detail_tax`
+ WHERE `id_tax` = '.(int)$this->id
+ );
+ }
/**
* Get all available taxes
*
* @return array Taxes
*/
- public static function getTaxes($id_lang = false, $active = 1)
+ public static function getTaxes($id_lang = false, $active_only = true)
{
- return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
- SELECT t.id_tax, t.rate'.((int)($id_lang) ? ', tl.name, tl.id_lang ' : '').'
- FROM `'._DB_PREFIX_.'tax` t
- '.((int)($id_lang) ? 'LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)($id_lang).')'
- .($active == 1 ? 'WHERE t.`active` = 1' : '').'
- ORDER BY `name` ASC' : ''));
+ $query = array();
+ $query['select'] = 'SELECT t.id_tax, t.rate';
+ $query['from'] = 'FROM `'._DB_PREFIX_.'tax` t';
+ $query['where'] = 'WHERE t.`deleted` != 1';
+
+ if ($id_lang)
+ {
+ $query['select'] .= ', tl.name, tl.id_lang ';
+ $query['join'] = 'LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)($id_lang).')';
+ $query['order'] = 'ORDER BY `name` ASC';
+ }
+
+ if ($active_only)
+ $query['where'] .= ' AND t.`active` = 1';
+
+ return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(Tools::buildQuery($query));
}
public static function excludeTaxeOption()
@@ -136,26 +190,6 @@ class TaxCore extends ObjectModel
return $tax ? (int)($tax['id_tax']) : false;
}
- /**
- * Returns the product tax
- *
- * @param integer $id_product
- * @param integer $id_country
- * @return Tax
- *
- * @deprecated use $product->getTaxesRate() instead
- */
- public static function getProductTaxRate($id_product, $id_address = NULL)
- {
- $address = Tax::initializeAddress($id_address);
- $id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct($id_product);
-
- $tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
- $tax_calculator = $tax_manager->getTaxCalculator();
-
- return $tax_calculator->getTaxesRate();
- }
-
/**
* Returns the ecotax tax rate
*
@@ -169,7 +203,7 @@ class TaxCore extends ObjectModel
$tax_manager = TaxManagerFactory::getManager($address, (int)Configuration::get('PS_ECOTAX_TAX_RULES_GROUP_ID'));
$tax_calculator = $tax_manager->getTaxCalculator();
- return $tax_calculator->getTaxesRate();
+ return $tax_calculator->getTotalRate();
}
/**
@@ -186,7 +220,7 @@ class TaxCore extends ObjectModel
$tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
$tax_calculator = $tax_manager->getTaxCalculator();
- return $tax_calculator->getTaxesRate();
+ return $tax_calculator->getTotalRate();
}
/**
@@ -231,11 +265,29 @@ class TaxCore extends ObjectModel
if (!isset(self::$_product_tax_via_rules[$id_product.'-'.$id_country.'-'.$id_state.'-'.$zipcode]))
{
- $tax_rate = TaxRulesGroup::getTaxesRate((int)Product::getIdTaxRulesGroupByIdProduct((int)$id_product), (int)$id_country, (int)$id_state, $zipcode);
+ $tax_rate = TaxRulesGroup::getTotalRate((int)Product::getIdTaxRulesGroupByIdProduct((int)$id_product), (int)$id_country, (int)$id_state, $zipcode);
self::$_product_tax_via_rules[$id_product.'-'.$id_country.'-'.$zipcode] = $tax_rate;
}
return self::$_product_tax_via_rules[$id_product.'-'.$id_country.'-'.$zipcode];
}
+
+ /**
+ * Returns the product tax
+ *
+ * @param integer $id_product
+ * @param integer $id_country
+ * @return Tax
+ */
+ public static function getProductTaxRate($id_product, $id_address = NULL)
+ {
+ $address = Tax::initializeAddress($id_address);
+ $id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct($id_product);
+
+ $tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
+ $tax_calculator = $tax_manager->getTaxCalculator();
+
+ return $tax_calculator->getTotalRate();
+ }
}
diff --git a/classes/tax/TaxCalculator.php b/classes/tax/TaxCalculator.php
index 547c3446e..bb40ab49d 100644
--- a/classes/tax/TaxCalculator.php
+++ b/classes/tax/TaxCalculator.php
@@ -45,9 +45,9 @@ class TaxCalculatorCore
const ONE_AFTER_ANOTHER_METHOD = 2;
/**
- * @var array $taxes_rate
+ * @var array $taxes
*/
- public $taxes_rate;
+ public $taxes;
/**
* @var int $computation_method (COMBINE_METHOD | ONE_AFTER_ANOTHER_METHOD)
@@ -56,89 +56,89 @@ class TaxCalculatorCore
/**
- * @param array $taxes_rate
+ * @param array $taxes
* @param int $computation_method (COMBINE_METHOD | ONE_AFTER_ANOTHER_METHOD)
*/
- public function __construct(array $taxes_rate, $computation_method = TaxCalculator::COMBINE_METHOD)
+ public function __construct(array $taxes = array(), $computation_method = TaxCalculator::COMBINE_METHOD)
{
- $this->taxes_rate = $taxes_rate;
+ // sanity check
+ foreach ($taxes as $tax)
+ if (!($tax instanceof Tax))
+ throw new Exception('Invalid Tax Object');
+
+ $this->taxes = $taxes;
$this->computation_method = (int)$computation_method;
}
/**
* Compute and add the taxes to the specified price
*
- * @param price
- * @return price with taxes
+ * @param price_te price tax excluded
+ * @return float price with taxes
*/
- public function addTaxes($price)
+ public function addTaxes($price_te)
{
- $total_price = $price;
- if ($this->computation_method == TaxCalculator::ONE_AFTER_ANOTHER_METHOD)
- {
- foreach ($this->taxes_rate as $tax_rate)
- $total_price = $total_price * (1 + abs($tax_rate) / 100);
- }
- else
- {
- foreach ($this->taxes_rate as $tax_rate)
- {
- if ($tax_rate != 0)
- $total_price = $total_price + ($price * (abs($tax_rate) / 100));
- }
- }
- return $total_price;
+ return $price_te * (1 + ($this->getTotalRate() / 100));
}
/**
* Compute and remove the taxes to the specified price
*
- * @param price
+ * @param price_ti price tax inclusive
* @return price without taxes
*/
- public function removeTaxes($price)
+ public function removeTaxes($price_ti)
{
- $total_price = $price;
- if ($this->computation_method == TaxCalculator::ONE_AFTER_ANOTHER_METHOD)
- {
- foreach ($this->taxes_rate as $tax_rate)
- $total_price = $total_price / (1 + abs($tax_rate) / 100);
- }
- else
- {
- $taxes_rate = 0;
- foreach ($this->taxes_rate as $tax_rate)
- $taxes_rate += abs($tax_rate);
-
- $total_price = $total_price / (1 + (abs($taxes_rate) / 100));
- }
-
- return $total_price;
+ return $price_ti / (1 + $this->getTotalRate() / 100);
}
/**
- * @return total taxes rate
+ * @return float total taxes rate
*/
- public function getTaxesRate()
+ public function getTotalRate()
{
- $taxes_rate = 0;
+ $taxes = 0;
if ($this->computation_method == TaxCalculator::ONE_AFTER_ANOTHER_METHOD)
{
- $taxes_rate = 1;
- foreach ($this->taxes_rate as $rate)
- $taxes_rate *= (1 + (abs($rate) / 100));
+ $taxes = 1;
+ foreach ($this->taxes as $tax)
+ $taxes *= (1 + (abs($tax->rate) / 100));
- $taxes_rate = $taxes_rate - 1;
- $taxes_rate = $taxes_rate * 100;
+ $taxes = $taxes - 1;
+ $taxes = $taxes * 100;
}
else
{
- foreach ($this->taxes_rate as $rate)
- $taxes_rate += abs($rate);
+ foreach ($this->taxes as $tax)
+ $taxes += abs($tax->rate);
}
- return $taxes_rate;
+ return (float)$taxes;
+ }
+
+ /**
+ * Return the tax amount associated to each taxes of the TaxCalculator
+ *
+ * @param float $price_te
+ * @return array $taxes_amount
+ */
+ public function getTaxesAmount($price_te)
+ {
+ $taxes_amounts = array();
+
+ foreach ($this->taxes as $tax)
+ {
+ if ($this->computation_method == TaxCalculator::ONE_AFTER_ANOTHER_METHOD)
+ {
+ $taxes_amounts[$tax->rate] = $price_te * (abs($tax->rate) / 100);
+ $price_te = $price_te + $taxes_amounts[$tax->rate];
+ }
+ else
+ $taxes_amounts[$tax->rate] = ($price * (abs($tax->rate) / 100));
+ }
+
+ return $taxes_amounts;
}
}
diff --git a/classes/tax/TaxRule.php b/classes/tax/TaxRule.php
index 3b697f880..fa732a5cd 100644
--- a/classes/tax/TaxRule.php
+++ b/classes/tax/TaxRule.php
@@ -27,25 +27,25 @@
class TaxRuleCore extends ObjectModel
{
- public $id_tax_rules_group;
- public $id_country;
- public $id_state;
+ public $id_tax_rules_group;
+ public $id_country;
+ public $id_state;
public $zipcode_from;
public $zipcode_to;
- public $id_tax;
+ public $id_tax;
public $behavior;
public $description;
- protected $fieldsRequired = array('id_tax_rules_group', 'id_country', 'id_tax');
- protected $fieldsValidate = array('id_tax_rules_group' => 'isUnsignedId',
- 'id_country' => 'isUnsignedId',
- 'id_state' => 'isUnsignedId',
- 'zipcode_from' => 'isUnsignedId', // TODO: char
- 'zipcode_to' => 'isUnsignedId', // TODO: char
- 'id_tax' => 'isUnsignedId',
- 'behavior' => 'isUnsignedInt',
- 'description' => 'isUnsignedInt'); // TODO:char
-
+ protected $fieldsRequired = array('id_tax_rules_group', 'id_country', 'id_tax');
+ protected $fieldsValidate = array('id_tax_rules_group' => 'isUnsignedId',
+ 'id_country' => 'isUnsignedId',
+ 'id_state' => 'isUnsignedId',
+ 'zipcode_from' => 'isUnsignedId', // TODO: char
+ 'zipcode_to' => 'isUnsignedId', // TODO: char
+ 'id_tax' => 'isUnsignedId',
+ 'behavior' => 'isUnsignedInt',
+ 'description' => 'isUnsignedInt'); // TODO:char
+
protected $table = 'tax_rule';
protected $identifier = 'id_tax_rule';
@@ -96,8 +96,6 @@ class TaxRuleCore extends ObjectModel
public static function getTaxRulesByGroupId($id_lang, $id_group)
{
-
-
return Db::getInstance()->ExecuteS('
SELECT g.`id_tax_rule`,
c.`name` AS country_name,
@@ -110,7 +108,8 @@ class TaxRuleCore extends ObjectModel
LEFT JOIN `'._DB_PREFIX_.'country_lang` c ON (g.`id_country` = c.`id_country` AND id_lang = '.(int)$id_lang.')
LEFT JOIN `'._DB_PREFIX_.'state` s ON (g.`id_state` = s.`id_state`)
LEFT JOIN `'._DB_PREFIX_.'tax` t ON (g.`id_tax` = t.`id_tax`)
- WHERE `id_tax_rules_group` = '.(int)$id_group
+ WHERE `id_tax_rules_group` = '.(int)$id_group.'
+ ORDER BY `country_name` ASC, `state_name` ASC, `zipcode_from` ASC, `zipcode_to` ASC'
);
}
@@ -175,5 +174,14 @@ class TaxRuleCore extends ObjectModel
return array($from, $to);
}
+
+ public static function swapTaxId($old_id, $new_id)
+ {
+ return Db::getInstance()->Execute('
+ UPDATE `'._DB_PREFIX_.'tax_rule`
+ SET `id_tax` = '.(int)$new_id.'
+ WHERE `id_tax` = '.(int)$old_id
+ );
+ }
}
diff --git a/classes/tax/TaxRulesTaxManager.php b/classes/tax/TaxRulesTaxManager.php
index 9840a64d3..6d728eda0 100644
--- a/classes/tax/TaxRulesTaxManager.php
+++ b/classes/tax/TaxRulesTaxManager.php
@@ -74,13 +74,13 @@ class TaxRulesTaxManagerCore implements TaxManagerInterface
$behavior = 0;
$first_row = true;
- $taxes_rates = array();
+ $taxes = array();
foreach ($rows as $row)
{
$tax = new Tax((int)$row['id_tax']);
- $taxes_rates[] = $tax->rate;
+ $taxes[] = $tax;
// the applied behavior correspond to the most specific rules
if ($first_row)
@@ -93,9 +93,7 @@ class TaxRulesTaxManagerCore implements TaxManagerInterface
break;
}
- $this->tax_calculator = new TaxCalculator($taxes_rates, $behavior);
-
- return $this->tax_calculator;
+ return new TaxCalculator($taxes, $behavior);
}
}
diff --git a/install-dev/php/update_order_detail_taxes.php b/install-dev/php/update_order_detail_taxes.php
new file mode 100644
index 000000000..9c86a730f
--- /dev/null
+++ b/install-dev/php/update_order_detail_taxes.php
@@ -0,0 +1,45 @@
+ExecuteS('
+ SELECT `id_order_detail`, `tax_name`, `tax_rate` FROM `'._DB_PREFIX_.'order_detail`
+ ');
+
+ foreach ($order_detail_taxes as $order_detail_tax)
+ {
+ if ($order_detail_tax['tax_rate'] == '0.000')
+ continue;
+
+ $alternative_tax_name = 'Tax '.$order_detail_tax['tax_rate'];
+ $create_tax = true;
+
+ if ($id_tax = (int)Tax::getTaxIdByName($order_detail_tax['tax_name'], false) || $id_tax = (int)Tax::getTaxIdByName($alternative_tax_name, false))
+ {
+ $tax = new Tax($id_tax);
+ if (Validate::isLoadedObject($tax))
+ if ((string)$tax->rate == (string)$order_detail_tax['tax_rate'])
+ $create_tax = false;
+ else
+ echo (string)$tax->rate.'!='.(string)$order_detail_tax['tax_rate'];
+ }
+
+ if ($create_tax)
+ {
+ $tax = new Tax();
+ $tax->rate = (float)$order_detail_tax['tax_rate'];
+ $tax->name[Configuration::get('PS_LANG_DEFAULT')] = (isset($order_detail_tax['tax_name']) ? $order_detail_tax['tax_name'] : $alternative_tax_name);
+ $tax->deleted = true;
+ $tax->active = false;
+ $tax->save();
+
+ $id_tax = $tax->id;
+ }
+
+ Db::getInstance()->Execute('
+ INSERT INTO `'._DB_PREFIX_.'order_detail_tax` (`id_order_detail`, `id_tax`)
+ VALUES ('.(int)$order_detail_tax['id_order_detail'].','.$id_tax.')
+ ');
+
+ }
+}
\ No newline at end of file
diff --git a/install-dev/sql/upgrade/1.5.0.1.sql b/install-dev/sql/upgrade/1.5.0.1.sql
index 1fcf291e3..3c343bc8a 100644
--- a/install-dev/sql/upgrade/1.5.0.1.sql
+++ b/install-dev/sql/upgrade/1.5.0.1.sql
@@ -64,7 +64,7 @@ ALTER TABLE `PREFIX_employee`
ADD `id_last_order` tinyint(1) unsigned NOT NULL default '0',
ADD `id_last_message` tinyint(1) unsigned NOT NULL default '0',
ADD `id_last_customer` tinyint(1) unsigned NOT NULL default '0';
-
+
INSERT INTO `PREFIX_configuration` (`name`, `value`, `date_add`, `date_upd`) VALUES
('PS_SHOW_NEW_ORDERS', '1', NOW(), NOW()),
('PS_SHOW_NEW_CUSTOMERS', '1', NOW(), NOW()),
@@ -78,3 +78,17 @@ ALTER TABLE `PREFIX_product_attribute` ADD `available_date` DATETIME NOT NULL;
/* Index was only used by deprecated function Image::positionImage() */
ALTER TABLE `PREFIX_image` DROP INDEX `product_position`;
+
+CREATE TABLE IF NOT EXISTS `PREFIX_order_detail_tax` (
+`id_order_detail` INT NOT NULL ,
+`id_tax` INT NOT NULL
+);
+
+ALTER TABLE `PREFIX_tax` ADD `deleted` INT NOT NULL AFTER `active`;
+
+/* PHP:update_order_detail_taxes(); */;
+
+ALTER TABLE `PREFIX_order_detail`
+ DROP `tax_name`,
+ DROP `tax_rate`;
+
diff --git a/install-dev/xml/doUpgrade.php b/install-dev/xml/doUpgrade.php
index daf23e70f..0dde70875 100644
--- a/install-dev/xml/doUpgrade.php
+++ b/install-dev/xml/doUpgrade.php
@@ -135,6 +135,8 @@ require_once(_PS_INSTALLER_PHP_UPGRADE_DIR_.'add_order_state.php');
require_once(_PS_INSTALLER_PHP_UPGRADE_DIR_.'remove_tab.php');
+require_once(_PS_INSTALLER_PHP_UPGRADE_DIR_.'update_order_detail_taxes.php');
+
//old version detection
global $oldversion, $logger;
$oldversion = false;
diff --git a/modules/vatnumber/VATNumberTaxManager.php b/modules/vatnumber/VATNumberTaxManager.php
index b558b1e16..e4ded18d9 100755
--- a/modules/vatnumber/VATNumberTaxManager.php
+++ b/modules/vatnumber/VATNumberTaxManager.php
@@ -37,7 +37,10 @@ class VATNumberTaxManager implements TaxManagerInterface
public function getTaxCalculator()
{
// No tax
- return new TaxCalculator(array(0));
+ $tax = new Tax();
+ $tax->rate = 0;
+
+ return new TaxCalculator(array($tax));
}
}