[*] BO : Taxes are now historized

[*] BO : Taxes are no longer stored in the order details row
This commit is contained in:
fBrignoli
2011-09-14 09:06:20 +00:00
parent 0dd3f002e3
commit efbf3c5d45
14 changed files with 439 additions and 217 deletions
+5 -2
View File
@@ -395,7 +395,10 @@ if (isset($_GET['ajaxStates']) AND isset($_GET['id_country']))
{
$list = '';
if (Tools::getValue('no_empty') != true)
$list = '<option value="0">-----------</option>'."\n";
{
$empty_value = (Tools::isSubmit('empty_value')) ? Tools::getValue('empty_value') : '----------';
$list = '<option value="0">'.Tools::htmlentitiesUTF8($empty_value).'</option>'."\n";
}
foreach ($states AS $state)
$list .= '<option value="'.(int)($state['id_state']).'"'.((isset($_GET['id_state']) AND $_GET['id_state'] == $state['id_state']) ? ' selected="selected"' : '').'>'.$state['name'].'</option>'."\n";
@@ -652,7 +655,7 @@ if (Tools::isSubmit('getAdminHomeElement'))
{
$content = explode('|', $content);
if ($content[0] == 'OK' && Validate::isCleanHtml($content[2]) && Validate::isCleanHtml($content[1]))
{
{
$result['partner_preactivation'] = $content[2];
$content[1] = explode('#%#', $content[1]);
foreach ($content[1] as $partnerPopUp)
+89 -57
View File
@@ -25,10 +25,10 @@
* International Registered Trademark & Property of PrestaShop SA
*/
class
AdminTaxRulesGroup extends AdminTab
class AdminTaxRulesGroup extends AdminTab
{
public $tax_rule;
public $selected_countries = array();
public $_errors_tax_rule;
public function __construct()
@@ -84,31 +84,45 @@ class
{
$zipcode = Tools::getValue('zipcode');
$id_rule = (int)Tools::getValue('id_tax_rule');
$this->selected_countries = Tools::getValue('country');
$this->selected_states = Tools::getValue('states');
if (empty($this->selected_states) || sizeof($this->selected_states) == 0)
$this->selected_states = array(0);
$tr = new TaxRule();
// update or creation?
if (isset($id_rule))
$tr->id = $id_rule;
$tr->id_tax = (int)Tools::getValue('tax');
$tr->id_tax_rules_group = (int)Tools::getValue('id_tax_rules_group');
$tr->id_country = (int)Tools::getValue('country');
$tr->id_state = (int)Tools::getValue('states');
list($tr->zipcode_from, $tr->zipcode_to) = $tr->breakDownZipCode($zipcode);
$tr->behavior = (int)Tools::getValue('behavior');
$tr->description = Tools::getValue('description');
$this->_errors_tax_rule = $this->validateTaxRule($tr);
if (sizeof($this->_errors_tax_rule) == 0)
foreach ($this->selected_countries as $id_country)
{
if (!$tr->save())
die('error');
foreach ($this->selected_states as $id_state)
{
$tr = new TaxRule();
// update or creation?
if (isset($id_rule))
$tr->id = $id_rule;
$tr->id_tax = (int)Tools::getValue('tax');
$tr->id_tax_rules_group = (int)Tools::getValue('id_tax_rules_group');
$tr->id_country = (int)$id_country;
$tr->id_state = (int)$id_state;
list($tr->zipcode_from, $tr->zipcode_to) = $tr->breakDownZipCode($zipcode);
$tr->behavior = (int)Tools::getValue('behavior');
$tr->description = Tools::getValue('description');
$this->tax_rule = $tr;
$this->_errors_tax_rule = $this->validateTaxRule($tr);
if (sizeof($this->_errors_tax_rule) == 0)
{
if (!$tr->save())
die(Tools::displayError('An error has occured: Can\'t save the current tax rule'));
} else
Tools::redirectAdmin(self::$currentIndex.'&'.$this->identifier.'='.$tr->id_tax_rules_group.'&conf=4&update'.$this->table.'&token='.$this->token);
}
}
$this->tax_rule = $tr;
Tools::redirectAdmin(self::$currentIndex.'&'.$this->identifier.'='.$tr->id_tax_rules_group.'&conf=4&update'.$this->table.'&token='.$this->token);
if (sizeof($this->_errors_tax_rule) == 0)
Tools::redirectAdmin(self::$currentIndex.'&'.$this->identifier.'='.$tr->id_tax_rules_group.'&conf=4&update'.$this->table.'&token='.$this->token);
} else
parent::postProcess();
}
@@ -127,28 +141,42 @@ class
{
global $cookie;
$all = $this->l('All');
$javascript = <<<EOT
<script type="text/javascript">
function populateStates(id_country, id_state)
{
$.ajax({
url: "ajax.php",
cache: false,
data: "ajaxStates=1&id_country="+id_country+"&id_state="+id_state,
success: function(html){
if (html == "false")
{
$("#state-label").hide();
$("#state-select").hide();
}
else
{
$("#state-label").show();
$("#state-select").show();
$("#states").html(html);
}
}
});
if ($("#country option:selected").size() > 1)
{
$("#zipcode-label").hide();
$("#zipcode").hide();
$("#state-select").hide();
$("#state-label").hide();
} else {
$.ajax({
url: "ajax.php",
cache: false,
data: "ajaxStates=1&id_country="+id_country+"&id_state="+id_state+"&empty_value=$all",
success: function(html){
if (html == "false")
{
$("#state-label").hide();
$("#state-select").hide();
}
else
{
$("#state-label").show();
$("#state-select").show();
$("#states").html(html);
}
}
});
$("#zipcode-label").show();
$("#zipcode").show();
}
}
function loadTaxRule(id_tax_rule)
@@ -302,12 +330,15 @@ EOT;
{
global $cookie, $currentIndex;
if (Validate::isLoadedObject($this->tax_rule))
if (Validate::isLoadedObject($this->tax_rule))
die(Tools::displayError('Unable to load the tax rule!'));
$country_select = Helper::selectInput(Country::getCountries((int)$cookie->id_lang),
array('id' => 'country', 'name' => 'country', 'onclick' => 'populateStates($(this).val(), \'\')'),
array('key' => 'id_country', 'value' => 'name'));
array('id' => 'country', 'name' => 'country[]', 'onclick' => 'populateStates($(this).val(), \'\')', 'multiple' => 'multiple'),
array('key' => 'id_country',
'value' => 'name',
'selected' => $this->selected_countries,
'empty' => $this->l('All')));
$tax_select = Helper::selectInput(Tax::getTaxes((int)$cookie->id_lang),
@@ -325,18 +356,23 @@ EOT;
echo '<a href="#" onclick="initForm();$(\'#rule_form\').slideToggle();return false;"><img src="../img/admin/add.gif" alt="" /> '.$this->l('Add a new tax rule').'</a>
<form action="'.$currentIndex.'&submitAdd'.$this->table.'=1&token='.$this->token.'" method="post">
<div id="rule_form" style="display: none">
<div style="float: left">
<label>'.$this->l('Country').'</label>
<div class="margin-form">
'.$country_select.'
</div>
<label id="state-label">'.$this->l('State').'</label>
<div id="state-select" class="margin-form">
<select id="states" name="states">
</select>
</div>
<label>'.$this->l('ZipCode range').'</label>
<div style="float: left">
<label id="state-label">'.$this->l('State').'</label>
<div id="state-select" class="margin-form">
<select id="states" name="states[]" multiple="multiple">
</select>
</div>
</div>
<div style="clear: both"></div>
<label id="zipcode-label">'.$this->l('ZipCode range').'</label>
<div class="margin-form">
<input type="hidden" name="action" value="create_rule" />
<input type="hidden" id="id_tax_rules_group" name="id_tax_rules_group" value="'.(int)$id_rule_group.'" />
@@ -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 .= '<tr>
<td>'.Tools::htmlentitiesUTF8($tax_rule['country_name']).'</td>
<td>'.Tools::htmlentitiesUTF8($country_name).'</td>
<td>'.Tools::htmlentitiesUTF8($state_name).'</td>
<td>'.Tools::htmlentitiesUTF8($zipcodes).'</td>
<td>'.Tools::htmlentitiesUTF8($behavior).'</td>
@@ -483,4 +516,3 @@ EOT;
}
}
}
+1 -1
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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();
}
}
+52 -52
View File
@@ -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
View File
@@ -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
);
}
}
+3 -5
View File
@@ -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);
}
}
@@ -0,0 +1,45 @@
<?php
function update_order_detail_taxes()
{
$order_detail_taxes = Db::getInstance()->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.')
');
}
}
+15 -1
View File
@@ -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`;
+2
View File
@@ -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;
+4 -1
View File
@@ -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));
}
}