[*] Project : US + CA TaxSystem
This commit is contained in:
@@ -0,0 +1,241 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2011 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2011 PrestaShop SA
|
||||
* @version Release: $Revision: 6844 $
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
class TaxCore extends ObjectModel
|
||||
{
|
||||
/** @var string Name */
|
||||
public $name;
|
||||
|
||||
/** @var float Rate (%) */
|
||||
public $rate;
|
||||
|
||||
/** @var bool active state */
|
||||
public $active;
|
||||
|
||||
protected $fieldsRequired = array('rate');
|
||||
protected $fieldsValidate = array('rate' => 'isFloat');
|
||||
protected $fieldsRequiredLang = array('name');
|
||||
protected $fieldsSizeLang = array('name' => 32);
|
||||
protected $fieldsValidateLang = array('name' => 'isGenericName');
|
||||
|
||||
protected $table = 'tax';
|
||||
protected $identifier = 'id_tax';
|
||||
|
||||
protected static $_product_country_tax = array();
|
||||
protected static $_product_tax_via_rules = array();
|
||||
|
||||
public function getFields()
|
||||
{
|
||||
$this->validateFields();
|
||||
$fields['rate'] = (float)($this->rate);
|
||||
$fields['active'] = (int)($this->active);
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check then return multilingual fields for database interaction
|
||||
*
|
||||
* @return array Multilingual fields
|
||||
*/
|
||||
public function getTranslationsFieldsChild()
|
||||
{
|
||||
$this->validateFieldsLang();
|
||||
return $this->getTranslationsFields(array('name'));
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
/* Clean associations */
|
||||
TaxRule::deleteTaxRuleByIdTax((int)$this->id);
|
||||
return parent::delete();
|
||||
}
|
||||
|
||||
public function toggleStatus()
|
||||
{
|
||||
if (parent::toggleStatus())
|
||||
return $this->_onStatusChange();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function update($nullValues = false)
|
||||
{
|
||||
if (parent::update($nullValues))
|
||||
return $this->_onStatusChange();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function _onStatusChange()
|
||||
{
|
||||
if (!$this->active)
|
||||
return TaxRule::deleteTaxRuleByIdTax($this->id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all available taxes
|
||||
*
|
||||
* @return array Taxes
|
||||
*/
|
||||
public static function getTaxes($id_lang = false, $active = 1)
|
||||
{
|
||||
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' : ''));
|
||||
}
|
||||
|
||||
public static function excludeTaxeOption()
|
||||
{
|
||||
return !Configuration::get('PS_TAX');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the tax id associated to the specified name
|
||||
*
|
||||
* @param string $tax_name
|
||||
* @param boolean $active (true by default)
|
||||
*/
|
||||
public static function getTaxIdByName($tax_name, $active = 1)
|
||||
{
|
||||
$tax = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
|
||||
SELECT t.`id_tax`
|
||||
FROM `'._DB_PREFIX_.'tax` t
|
||||
LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (tl.id_tax = t.id_tax)
|
||||
WHERE tl.`name` = \''.pSQL($tax_name).'\' '.
|
||||
($active == 1 ? ' AND t.`active` = 1' : ''));
|
||||
|
||||
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
|
||||
*
|
||||
* @param id_address
|
||||
* @return float $tax_rate
|
||||
*/
|
||||
public static function getProductEcotaxRate($id_address = NULL)
|
||||
{
|
||||
$address = Tax::initializeAddress($id_address);
|
||||
|
||||
$tax_manager = TaxManagerFactory::getManager($address, (int)Configuration::get('PS_ECOTAX_TAX_RULES_GROUP_ID'));
|
||||
$tax_calculator = $tax_manager->getTaxCalculator();
|
||||
|
||||
return $tax_calculator->getTaxesRate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the carrier tax rate
|
||||
*
|
||||
* @param id_address
|
||||
* @return float $tax_rate
|
||||
*/
|
||||
public static function getCarrierTaxRate($id_carrier, $id_address = NULL)
|
||||
{
|
||||
$address = Tax::initializeAddress($id_address);
|
||||
$id_tax_rules = (int)Carrier::getIdTaxRulesGroupByIdCarrier((int)$id_carrier);
|
||||
|
||||
$tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
|
||||
$tax_calculator = $tax_manager->getTaxCalculator();
|
||||
|
||||
return $tax_calculator->getTaxesRate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiliaze an address corresponding to the id address if any or to the
|
||||
* default shop configuration
|
||||
*
|
||||
* @param int $id_address
|
||||
* @return Address address
|
||||
*/
|
||||
public static function initializeAddress($id_address = NULL)
|
||||
{
|
||||
// set the default address
|
||||
$address = new Address();
|
||||
$address->id_country = (int)Context::getContext()->country->id;
|
||||
$address->id_state = 0;
|
||||
$address->postcode = 0;
|
||||
|
||||
// if an id_address has been specified retrieve the address
|
||||
if ($id_address)
|
||||
{
|
||||
$address = new Address((int)$id_address);
|
||||
|
||||
if (!Validate::isLoadedObject())
|
||||
throw new Exception('Invalid address');
|
||||
}
|
||||
|
||||
return $address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the product tax rate using the tax rules system
|
||||
*
|
||||
* @param integer $id_product
|
||||
* @param integer $id_country
|
||||
* @return Tax
|
||||
*
|
||||
* @deprecated since 1.5
|
||||
*/
|
||||
public static function getProductTaxRateViaRules($id_product, $id_country, $id_state, $zipcode)
|
||||
{
|
||||
Tools::displayAsDeprecated();
|
||||
|
||||
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);
|
||||
self::$_product_tax_via_rules[$id_product.'-'.$id_country.'-'.$zipcode] = $tax_rate;
|
||||
}
|
||||
|
||||
return self::$_product_tax_via_rules[$id_product.'-'.$id_country.'-'.$zipcode];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2011 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2011 PrestaShop SA
|
||||
* @version Release: $Revision$
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
/**
|
||||
* @since 1.5.0
|
||||
*
|
||||
* TaxCaculator is responsible of the tax computation
|
||||
*/
|
||||
class TaxCalculatorCore
|
||||
{
|
||||
/**
|
||||
* COMBINE_METHOD sum taxes
|
||||
* eg: 100€ * (10% + 15%)
|
||||
*/
|
||||
const COMBINE_METHOD = 1;
|
||||
|
||||
/**
|
||||
* ONE_AFTER_ANOTHER_METHOD apply taxes one after another
|
||||
* eg: (100€ * 10%) * 15%
|
||||
*/
|
||||
const ONE_AFTER_ANOTHER_METHOD = 2;
|
||||
|
||||
/**
|
||||
* @var array $taxes_rate
|
||||
*/
|
||||
public $taxes_rate;
|
||||
|
||||
/**
|
||||
* @var int $computation_method (COMBINE_METHOD | ONE_AFTER_ANOTHER_METHOD)
|
||||
*/
|
||||
public $computation_method;
|
||||
|
||||
|
||||
/**
|
||||
* @param array $taxes_rate
|
||||
* @param int $computation_method (COMBINE_METHOD | ONE_AFTER_ANOTHER_METHOD)
|
||||
*/
|
||||
public function __construct(array $taxes_rate, $computation_method = TaxCalculator::COMBINE_METHOD)
|
||||
{
|
||||
$this->taxes_rate = $taxes_rate;
|
||||
$this->computation_method = (int)$computation_method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute and add the taxes to the specified price
|
||||
*
|
||||
* @param price
|
||||
* @return price with taxes
|
||||
*/
|
||||
public function addTaxes($price)
|
||||
{
|
||||
$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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compute and remove the taxes to the specified price
|
||||
*
|
||||
* @param price
|
||||
* @return price without taxes
|
||||
*/
|
||||
public function removeTaxes($price)
|
||||
{
|
||||
$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 total taxes rate
|
||||
*/
|
||||
public function getTaxesRate()
|
||||
{
|
||||
$taxes_rate = 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_rate = $taxes_rate - 1;
|
||||
$taxes_rate = $taxes_rate * 100;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach ($this->taxes_rate as $rate)
|
||||
$taxes_rate += abs($rate);
|
||||
}
|
||||
|
||||
return $taxes_rate;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2011 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2011 PrestaShop SA
|
||||
* @version Release: $Revision$
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
/**
|
||||
* @since 1.5
|
||||
*/
|
||||
class TaxManagerFactoryCore
|
||||
{
|
||||
protected static $cache_tax_manager;
|
||||
|
||||
/**
|
||||
* Returns a tax manager able to handle this address
|
||||
*
|
||||
* @param Address $address
|
||||
* @param string $type
|
||||
*
|
||||
* @return TaxManager
|
||||
*/
|
||||
public static function getManager(Address $address, $type)
|
||||
{
|
||||
$cache_id = TaxManagerFactory::getCacheKey($address).'-'.$type;
|
||||
if (!isset(TaxManagerFactory::$cache_tax_manager[$cache_id]))
|
||||
{
|
||||
$tax_manager = TaxManagerFactory::execHookTaxManagerFactory($address, $type);
|
||||
if (!($tax_manager instanceof TaxManagerInterface))
|
||||
$tax_manager = new TaxRulesTaxManager($address, $type);
|
||||
|
||||
TaxManagerFactory::$cache_tax_manager[$cache_id] = $tax_manager;
|
||||
}
|
||||
|
||||
return TaxManagerFactory::$cache_tax_manager[$cache_id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for a tax manager able to handle this type of address in the module list
|
||||
*
|
||||
* @param Address $address
|
||||
* @param string $type
|
||||
*
|
||||
* @return TaxManager
|
||||
*/
|
||||
public static function execHookTaxManagerFactory(Address $address, $type)
|
||||
{
|
||||
$modules_infos = Hook::getModulesFromHook(Hook::getIdByName('taxManager'));
|
||||
$tax_manager = false;
|
||||
|
||||
foreach ($modules_infos as $module_infos)
|
||||
{
|
||||
$module_instance = Module::getInstanceByName($module_infos['name']);
|
||||
if (is_callable(array($module_instance, 'hookTaxManager')))
|
||||
{
|
||||
$tax_manager = $module_instance->hookTaxManager(array(
|
||||
'address' => $address,
|
||||
'params' => $type
|
||||
));
|
||||
}
|
||||
|
||||
if ($tax_manager)
|
||||
break;
|
||||
}
|
||||
|
||||
return $tax_manager;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a unique identifier for the address
|
||||
* @param Address
|
||||
*/
|
||||
protected static function getCacheKey(Address $address)
|
||||
{
|
||||
return $address->id_country.'-'
|
||||
.(int)$address->id_state.'-'
|
||||
.$address->postcode.'-'
|
||||
.$address->vat_number.'-'
|
||||
.$address->dni;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2011 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2011 PrestaShop SA
|
||||
* @version Release: $Revision$
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* A TaxManager define a way to retrieve tax.
|
||||
*/
|
||||
interface TaxManagerInterface
|
||||
{
|
||||
/**
|
||||
* This method determine if the tax manager is available for the specified address.
|
||||
*
|
||||
* @param Address $address
|
||||
* @param string $type
|
||||
*
|
||||
* @return TaxManager
|
||||
*/
|
||||
public static function isAvailableForThisAddress(Address $address);
|
||||
|
||||
/**
|
||||
* Return the tax calculator associated to this address
|
||||
*
|
||||
* @return TaxCalculator
|
||||
*/
|
||||
public function getTaxCalculator();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2011 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2011 PrestaShop SA
|
||||
* @version Release: $Revision$
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
abstract class TaxManagerModuleCore extends Module
|
||||
{
|
||||
public $tax_manager_class;
|
||||
|
||||
public function install()
|
||||
{
|
||||
return (parent::install() && $this->registerHook('taxManager') );
|
||||
}
|
||||
|
||||
public function hookTaxManager($args)
|
||||
{
|
||||
$class_file =_PS_MODULE_DIR_.'/'.$this->name.'/'.$this->tax_manager_class.'.php';
|
||||
|
||||
if (!isset($this->tax_manager_class) || !file_exists($class_file))
|
||||
die(Tools::displayError('Incorrect Tax Manager class ['.$this->tax_manager_class.']'));
|
||||
|
||||
require_once($class_file);
|
||||
|
||||
if (!class_exists($this->tax_manager_class))
|
||||
die(Tools::displayError('Tax Manager class not found ['.$this->tax_manager_class.']'));
|
||||
|
||||
$class = $this->tax_manager_class;
|
||||
|
||||
if ($class::isAvailableForThisAddress($args['address']))
|
||||
return new $class();
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2011 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2011 PrestaShop SA
|
||||
* @version Release: $Revision: 6844 $
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
class TaxRuleCore extends ObjectModel
|
||||
{
|
||||
public $id_tax_rules_group;
|
||||
public $id_country;
|
||||
public $id_state;
|
||||
public $zipcode_from;
|
||||
public $zipcode_to;
|
||||
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 $table = 'tax_rule';
|
||||
protected $identifier = 'id_tax_rule';
|
||||
|
||||
public function getFields()
|
||||
{
|
||||
$this->validateFields();
|
||||
$fields['id_tax_rules_group'] = (int)($this->id_tax_rules_group);
|
||||
$fields['id_country'] = (int)$this->id_country;
|
||||
$fields['id_state'] = (int)$this->id_state;
|
||||
$fields['zipcode_from'] = (int)$this->zipcode_from;
|
||||
$fields['zipcode_to'] = (int)$this->zipcode_to;
|
||||
$fields['behavior'] = (int)$this->behavior;
|
||||
$fields['id_tax'] = (int)($this->id_tax);
|
||||
$fields['description'] = $this->description;
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
public static function deleteByGroupId($id_group)
|
||||
{
|
||||
if (empty($id_group))
|
||||
die(Tools::displayError());
|
||||
|
||||
return Db::getInstance()->Execute('
|
||||
DELETE FROM `'._DB_PREFIX_.'tax_rule`
|
||||
WHERE `id_tax_rules_group` = '.(int)$id_group
|
||||
);
|
||||
}
|
||||
|
||||
public static function retrieveById($id_tax_rule)
|
||||
{
|
||||
return Db::getInstance()->getRow('
|
||||
SELECT * FROM `'._DB_PREFIX_.'tax_rule`
|
||||
WHERE `id_tax_rule` = '.(int)$id_tax_rule);
|
||||
}
|
||||
/*
|
||||
public static function getTaxRulesByGroupId($id_group)
|
||||
{
|
||||
if (empty($id_group))
|
||||
die(Tools::displayError());
|
||||
|
||||
return Db::getInstance()->ExecuteS('
|
||||
SELECT *
|
||||
FROM `'._DB_PREFIX_.'tax_rule`
|
||||
WHERE `id_tax_rules_group` = '.(int)$id_group
|
||||
);
|
||||
}*/
|
||||
|
||||
public static function getTaxRulesByGroupId($id_lang, $id_group)
|
||||
{
|
||||
|
||||
|
||||
return Db::getInstance()->ExecuteS('
|
||||
SELECT g.`id_tax_rule`,
|
||||
c.`name` AS country_name,
|
||||
s.`name` AS state_name,
|
||||
t.rate,
|
||||
g.`zipcode_from`, g.`zipcode_to`,
|
||||
g.`description`,
|
||||
g.`behavior`
|
||||
FROM `'._DB_PREFIX_.'tax_rule` g
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
public static function deleteTaxRuleByIdTax($id_tax)
|
||||
{
|
||||
return Db::getInstance()->Execute('
|
||||
DELETE FROM `'._DB_PREFIX_.'tax_rule`
|
||||
WHERE `id_tax` = '.(int)$id_tax
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @deprecated since 1.5
|
||||
*/
|
||||
public static function deleteTaxRuleByIdCounty($id_county)
|
||||
{
|
||||
Tools::displayAsDeprecated();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id_tax
|
||||
* @return boolean
|
||||
*/
|
||||
public static function isTaxInUse($id_tax)
|
||||
{
|
||||
return Db::getInstance()->getValue('
|
||||
SELECT COUNT(*) FROM `'._DB_PREFIX_.'tax_rule` WHERE `id_tax` = '.(int)$id_tax
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $zipcode a range of zipcode (eg: 75000 / 75000-75015)
|
||||
* @return array an array containing two zipcode ordered by zipcode
|
||||
*/
|
||||
public function breakDownZipCode($zip_codes)
|
||||
{
|
||||
$zip_codes = preg_split('/-/', $zip_codes);
|
||||
|
||||
if (sizeof($zip_codes) == 2)
|
||||
{
|
||||
$from = $zip_codes[0];
|
||||
$to = $zip_codes[1];
|
||||
if ($zip_codes[0] > $zip_codes[1])
|
||||
{
|
||||
$from = $zip_codes[1];
|
||||
$to = $zip_codes[0];
|
||||
}
|
||||
elseif ($zip_codes[0] == $zip_codes[1])
|
||||
{
|
||||
$from = $zip_codes[0];
|
||||
$to = 0;
|
||||
}
|
||||
}
|
||||
elseif (sizeof($zip_codes) == 1)
|
||||
{
|
||||
$from = $zip_codes[0];
|
||||
$to = 0;
|
||||
}
|
||||
|
||||
return array($from, $to);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2011 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2011 PrestaShop SA
|
||||
* @version Release: $Revision: 6844 $
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
|
||||
class TaxRulesGroupCore extends ObjectModel
|
||||
{
|
||||
public $name;
|
||||
|
||||
/** @var bool active state */
|
||||
public $active;
|
||||
|
||||
protected $fieldsRequired = array('name');
|
||||
protected $fieldsSize = array('name' => 64);
|
||||
protected $fieldsValidate = array('name' => 'isGenericName');
|
||||
|
||||
protected $table = 'tax_rules_group';
|
||||
protected $identifier = 'id_tax_rules_group';
|
||||
|
||||
protected static $_taxes = array();
|
||||
|
||||
public function getFields()
|
||||
{
|
||||
$this->validateFields();
|
||||
$fields['name'] = ($this->name);
|
||||
$fields['active'] = (int)($this->active);
|
||||
return $fields;
|
||||
}
|
||||
|
||||
public static function getTaxRulesGroups($only_active = true)
|
||||
{
|
||||
return Db::getInstance()->ExecuteS('
|
||||
SELECT *
|
||||
FROM `'._DB_PREFIX_.'tax_rules_group` g'
|
||||
.($only_active ? ' WHERE g.`active` = 1' : '')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array an array of tax rules group formatted as $id => $name
|
||||
*/
|
||||
public static function getTaxRulesGroupsForOptions()
|
||||
{
|
||||
$tax_rules[] = array('id_tax_rules_group' => 0, 'name' => Tools::displayError('No tax'));
|
||||
return array_merge($tax_rules, TaxRulesGroup::getTaxRulesGroups());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function getAssociatedTaxRatesByIdCountry($id_country)
|
||||
{
|
||||
$rows = Db::getInstance()->ExecuteS('
|
||||
SELECT rg.`id_tax_rules_group`, t.`rate`
|
||||
FROM `'._DB_PREFIX_.'tax_rules_group` rg
|
||||
LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (tr.`id_tax_rules_group` = rg.`id_tax_rules_group`)
|
||||
LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`)
|
||||
WHERE tr.`id_country` = '.(int)$id_country.'
|
||||
AND tr.`id_state` = 0
|
||||
AND 0 between `zipcode_from` AND `zipcode_to`'
|
||||
);
|
||||
|
||||
$res = array();
|
||||
foreach ($rows AS $row)
|
||||
$res[$row['id_tax_rules_group']] = $row['rate'];
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tax rules group id corresponding to the name
|
||||
*
|
||||
* @param string name
|
||||
* @return int id of the tax rules
|
||||
*/
|
||||
public static function getIdByName($name)
|
||||
{
|
||||
return Db::getInstance()->getValue(
|
||||
'SELECT `id_tax_rules_group`
|
||||
FROM `'._DB_PREFIX_.'tax_rules_group` rg
|
||||
WHERE `name` = \''.pSQL($name).'\''
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since 1.5
|
||||
*/
|
||||
public static function getTaxesRate($id_tax_rules_group, $id_country, $id_state, $zipcode)
|
||||
{
|
||||
Tools::displayAsDeprecated();
|
||||
$rate = 0;
|
||||
foreach (TaxRulesGroup::getTaxes($id_tax_rules_group, $id_country, $id_state, $zipcode) AS $tax)
|
||||
$rate += (float)$tax->rate;
|
||||
|
||||
return $rate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return taxes associated to this para
|
||||
* @deprecated since 1.5
|
||||
*/
|
||||
public static function getTaxes($id_tax_rules_group, $id_country, $id_state, $id_county)
|
||||
{
|
||||
Tools::displayAsDeprecated();
|
||||
return array();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* 2007-2011 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2011 PrestaShop SA
|
||||
* @version Release: $Revision$
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
class TaxRulesTaxManagerCore implements TaxManagerInterface
|
||||
{
|
||||
public $address;
|
||||
public $type;
|
||||
public $tax_calculator;
|
||||
|
||||
|
||||
public function __construct(Address $address, $type)
|
||||
{
|
||||
$this->address = $address;
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this tax manager is available for this address
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function isAvailableForThisAddress(Address $address)
|
||||
{
|
||||
return true; // default manager, available for all addresses
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the tax calculator associated to this address
|
||||
*
|
||||
* @return TaxCalculator
|
||||
*/
|
||||
public function getTaxCalculator()
|
||||
{
|
||||
if (isset($this->tax_calculator))
|
||||
return $this->tax_calculator;
|
||||
|
||||
$postcode = 0;
|
||||
if (!empty($this->address->postcode))
|
||||
$postcode = $this->address->postcode;
|
||||
|
||||
$rows = Db::getInstance()->ExecuteS('
|
||||
SELECT *
|
||||
FROM `'._DB_PREFIX_.'tax_rule`
|
||||
WHERE `id_country` = '.(int)$this->address->id_country.'
|
||||
AND `id_tax_rules_group` = '.(int)$this->type.'
|
||||
AND `id_state` IN (0, '.(int)$this->address->id_state.')
|
||||
AND ('.pSQL($postcode).' BETWEEN `zipcode_from` AND `zipcode_to` OR `zipcode_from` = 0 OR `zipcode_from` = '.pSQL($postcode).')
|
||||
ORDER BY `zipcode_from` DESC, `zipcode_to` DESC, `id_state` DESC, `id_country` DESC');
|
||||
|
||||
$behavior = 0;
|
||||
$first_row = true;
|
||||
$taxes_rates = array();
|
||||
|
||||
foreach ($rows as $row)
|
||||
{
|
||||
$tax = new Tax((int)$row['id_tax']);
|
||||
|
||||
$taxes_rates[] = $tax->rate;
|
||||
|
||||
// the applied behavior correspond to the most specific rules
|
||||
if ($first_row)
|
||||
{
|
||||
$behavior = $row['behavior'];
|
||||
$first_row = false;
|
||||
}
|
||||
|
||||
if ($row['behavior'] == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
$this->tax_calculator = new TaxCalculator($taxes_rates, $behavior);
|
||||
|
||||
return $this->tax_calculator;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user