[*] BO : Taxes are now historized
[*] BO : Taxes are no longer stored in the order details row
This commit is contained in:
+1
-1
@@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+15
-10
@@ -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;
|
||||
}
|
||||
|
||||
+82
-27
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2011 PrestaShop
|
||||
* 2007-2011 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
@@ -29,13 +29,13 @@ class OrderDetailCore extends ObjectModel
|
||||
{
|
||||
/** @var integer */
|
||||
public $id_order_detail;
|
||||
|
||||
|
||||
/** @var integer */
|
||||
public $id_order;
|
||||
|
||||
|
||||
/** @var integer */
|
||||
public $product_id;
|
||||
|
||||
|
||||
/** @var integer */
|
||||
public $product_attribute_id;
|
||||
|
||||
@@ -47,10 +47,10 @@ class OrderDetailCore extends ObjectModel
|
||||
|
||||
/** @var integer */
|
||||
public $product_quantity_in_stock;
|
||||
|
||||
|
||||
/** @var integer */
|
||||
public $product_quantity_return;
|
||||
|
||||
|
||||
/** @var integer */
|
||||
public $product_quantity_refunded;
|
||||
|
||||
@@ -59,55 +59,55 @@ class OrderDetailCore extends ObjectModel
|
||||
|
||||
/** @var float */
|
||||
public $product_price;
|
||||
|
||||
|
||||
/** @var float */
|
||||
public $reduction_percent;
|
||||
|
||||
|
||||
/** @var float */
|
||||
public $reduction_amount;
|
||||
|
||||
|
||||
/** @var float */
|
||||
public $group_reduction;
|
||||
|
||||
|
||||
/** @var float */
|
||||
public $product_quantity_discount;
|
||||
|
||||
|
||||
/** @var string */
|
||||
public $product_ean13;
|
||||
|
||||
|
||||
/** @var string */
|
||||
public $product_upc;
|
||||
|
||||
|
||||
/** @var string */
|
||||
public $product_reference;
|
||||
|
||||
|
||||
/** @var string */
|
||||
public $product_supplier_reference;
|
||||
|
||||
|
||||
/** @var float */
|
||||
public $product_weight;
|
||||
|
||||
|
||||
/** @var string */
|
||||
public $tax_name;
|
||||
|
||||
/** @var float */
|
||||
public $tax_rate;
|
||||
|
||||
|
||||
/** @var float */
|
||||
public $ecotax;
|
||||
|
||||
/** @var float */
|
||||
public $ecotax_tax_rate;
|
||||
|
||||
|
||||
/** @var integer */
|
||||
public $discount_quantity_applied;
|
||||
|
||||
/** @var string */
|
||||
public $download_hash;
|
||||
|
||||
|
||||
/** @var integer */
|
||||
public $download_nb;
|
||||
|
||||
|
||||
/** @var date */
|
||||
public $download_deadline;
|
||||
|
||||
@@ -141,10 +141,10 @@ class OrderDetailCore extends ObjectModel
|
||||
'ecotax_tax_rate' => '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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+15
-10
@@ -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 = '';
|
||||
|
||||
+86
-34
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+25
-17
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user