first commit

This commit is contained in:
2024-11-05 12:22:50 +01:00
commit e5682a3912
19641 changed files with 2948548 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* 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.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* 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 https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
class AverageTaxOfProductsTaxCalculator
{
private $id_order;
private $configuration;
private $db;
public $computation_method = 'average_tax_of_products';
public function __construct(PrestaShop\PrestaShop\Core\Foundation\Database\DatabaseInterface $db, PrestaShop\PrestaShop\Core\ConfigurationInterface $configuration)
{
$this->db = $db;
$this->configuration = $configuration;
}
private function getProductTaxes()
{
$prefix = $this->configuration->get('_DB_PREFIX_');
$sql = 'SELECT t.id_tax, t.rate, od.total_price_tax_excl FROM ' . $prefix . 'orders o
INNER JOIN ' . $prefix . 'order_detail od ON od.id_order = o.id_order
INNER JOIN ' . $prefix . 'order_detail_tax odt ON odt.id_order_detail = od.id_order_detail
INNER JOIN ' . $prefix . 'tax t ON t.id_tax = odt.id_tax
WHERE o.id_order = ' . (int) $this->id_order;
return $this->db->select($sql);
}
public function setIdOrder($id_order)
{
$this->id_order = $id_order;
return $this;
}
public function getTaxesAmount($price_before_tax, $price_after_tax = null, $round_precision = 2, $round_mode = null)
{
$amounts_arr = [];
$total_base = 0;
foreach ($this->getProductTaxes() as $row) {
if (!array_key_exists($row['id_tax'], $amounts_arr)) {
$amounts_arr[$row['id_tax']] = [
'rate' => $row['rate'],
'base' => 0,
];
}
$amounts_arr[$row['id_tax']]['base'] += $row['total_price_tax_excl'];
$total_base += $row['total_price_tax_excl'];
}
$amounts = [];
foreach ($amounts_arr as $k => $amount) {
$amounts[$k] = Tools::ps_round(
$price_before_tax * ($amount['base'] / $total_base) * $amount['rate'] / 100,
$round_precision,
$round_mode
);
}
if ($price_after_tax) {
$actual_tax = array_sum($amounts);
Tools::spreadAmount(
$price_after_tax - $price_before_tax - $actual_tax,
$round_precision,
$amounts,
'id_tax'
);
}
return $amounts;
}
}

277
classes/tax/Tax.php Normal file
View File

@@ -0,0 +1,277 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* 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.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* 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 https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
class TaxCore extends ObjectModel
{
const TAX_DEFAULT_PRECISION = 3;
/** @var array<int,string> Name */
public $name;
/** @var float Rate (%) */
public $rate;
/** @var bool active state */
public $active;
/** @var bool true if the tax has been historized */
public $deleted = 0;
/**
* @see ObjectModel::$definition
*/
public static $definition = [
'table' => 'tax',
'primary' => 'id_tax',
'multilang' => true,
'fields' => [
'rate' => ['type' => self::TYPE_FLOAT, 'validate' => 'isFloat', 'required' => true],
'active' => ['type' => self::TYPE_BOOL],
'deleted' => ['type' => self::TYPE_BOOL],
/* Lang fields */
'name' => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 32],
],
];
protected static $_product_country_tax = [];
protected static $_product_tax_via_rules = [];
protected $webserviceParameters = [
'objectsNodeName' => 'taxes',
];
public function delete()
{
/* Clean associations */
TaxRule::deleteTaxRuleByIdTax((int) $this->id);
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()
{
if (parent::toggleStatus()) {
return $this->_onStatusChange();
}
return false;
}
public function update($null_values = false)
{
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;
$res = $this->add();
// change tax id in the tax rule table
$res &= TaxRule::swapTaxId($historized_tax->id, $this->id);
return $res;
} elseif (parent::update($null_values)) {
return $this->_onStatusChange();
}
return false;
}
protected function _onStatusChange()
{
if (!$this->active) {
return TaxRule::deleteTaxRuleByIdTax($this->id);
}
return true;
}
/**
* Returns true if the tax is used in an order details.
*
* @return bool
*/
public function isUsed()
{
return Db::getInstance()->getValue(
'
SELECT `id_tax`
FROM `' . _DB_PREFIX_ . 'order_detail_tax`
WHERE `id_tax` = ' . (int) $this->id
);
}
/**
* Get all available taxes.
*
* @param int $id_lang
* @param bool $active_only (true by default)
*
* @return array Taxes
*/
public static function getTaxes($id_lang = false, $active_only = true)
{
$sql = new DbQuery();
$sql->select('t.id_tax, t.rate');
$sql->from('tax', 't');
$sql->where('t.`deleted` != 1');
if ($id_lang) {
$sql->select('tl.name, tl.id_lang');
$sql->leftJoin('tax_lang', 'tl', 't.`id_tax` = tl.`id_tax` AND tl.`id_lang` = ' . (int) $id_lang);
$sql->orderBy('`name` ASC');
}
if ($active_only) {
$sql->where('t.`active` = 1');
}
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
}
public static function excludeTaxeOption()
{
return !Configuration::get('PS_TAX');
}
/**
* Return the tax id associated to the specified name.
*
* @param string $tax_name
* @param bool $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 ecotax tax rate.
*
* @param int $id_address
*
* @return float $tax_rate
*/
public static function getProductEcotaxRate($id_address = null)
{
$address = Address::initialize($id_address);
$tax_manager = TaxManagerFactory::getManager($address, (int) Configuration::get('PS_ECOTAX_TAX_RULES_GROUP_ID'));
$tax_calculator = $tax_manager->getTaxCalculator();
return $tax_calculator->getTotalRate();
}
/**
* Returns the carrier tax rate.
*
* @param int $id_carrier
* @param int $id_address
*
* @return float $tax_rate
*/
public static function getCarrierTaxRate($id_carrier, $id_address = null)
{
$address = Address::initialize($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->getTotalRate();
}
/**
* Return the product tax rate using the tax rules system.
*
* @param int $id_product
* @param int $id_country
* @param int $id_state
* @param string $zipcode
*
* @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];
}
/**
* Returns the product tax rate.
*
* @param int $id_product
* @param int $id_address
* @param Context $context
*
* @return float
*/
public static function getProductTaxRate($id_product, $id_address = null, Context $context = null)
{
if ($context == null) {
$context = Context::getContext();
}
$address = Address::initialize($id_address);
$id_tax_rules = (int) Product::getIdTaxRulesGroupByIdProduct($id_product, $context);
$tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
$tax_calculator = $tax_manager->getTaxCalculator();
return $tax_calculator->getTotalRate();
}
}

View File

@@ -0,0 +1,175 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* 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.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* 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 https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
/**
* @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
*/
public $taxes;
/**
* @var int (COMBINE_METHOD | ONE_AFTER_ANOTHER_METHOD)
*/
public $computation_method;
/**
* @param array $taxes
* @param int $computation_method (COMBINE_METHOD | ONE_AFTER_ANOTHER_METHOD)
*/
public function __construct(array $taxes = [], $computation_method = TaxCalculator::COMBINE_METHOD)
{
// 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 float $price_te price tax excluded
*
* @return float price with taxes
*/
public function addTaxes($price_te)
{
return $price_te * (1 + ($this->getTotalRate() / 100));
}
/**
* Compute and remove the taxes to the specified price.
*
* @param float $price_ti price tax inclusive
*
* @return float price without taxes
*/
public function removeTaxes($price_ti)
{
return $price_ti / (1 + $this->getTotalRate() / 100);
}
/**
* @return float total taxes rate
*/
public function getTotalRate()
{
$taxes = 0;
if ($this->computation_method == TaxCalculator::ONE_AFTER_ANOTHER_METHOD) {
$taxes = 1;
foreach ($this->taxes as $tax) {
$taxes *= (1 + (abs($tax->rate) / 100));
}
$taxes = $taxes - 1;
$taxes = $taxes * 100;
} else {
foreach ($this->taxes as $tax) {
$taxes += abs($tax->rate);
}
}
return (float) $taxes;
}
public function getTaxesName()
{
$name = '';
$languageId = (int) Context::getContext()->language->id;
foreach ($this->taxes as $tax) {
$name .= ($tax->name[$languageId] ?? '') . ' - ';
}
$name = rtrim($name, ' - ');
return $name;
}
/**
* 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 = [];
foreach ($this->taxes as $tax) {
if ($this->computation_method == TaxCalculator::ONE_AFTER_ANOTHER_METHOD) {
$taxes_amounts[$tax->id] = $price_te * (abs($tax->rate) / 100);
$price_te = $price_te + $taxes_amounts[$tax->id];
} else {
$taxes_amounts[$tax->id] = ($price_te * (abs($tax->rate) / 100));
}
}
return $taxes_amounts;
}
/**
* Return the total taxes amount.
*
* @param float $price_te
*
* @return float $amount
*/
public function getTaxesTotalAmount($price_te)
{
$amount = 0;
$taxes = $this->getTaxesAmount($price_te);
foreach ($taxes as $tax) {
$amount += $tax;
}
return $amount;
}
}

View File

@@ -0,0 +1,47 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* 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.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* 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 https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
class TaxConfigurationCore
{
private $taxCalculationMethod = [];
/**
* @return bool
*/
public function includeTaxes()
{
if (!Configuration::get('PS_TAX')) {
return false;
}
$idCustomer = (int) Context::getContext()->cookie->id_customer;
if (!array_key_exists($idCustomer, $this->taxCalculationMethod)) {
$this->taxCalculationMethod[$idCustomer] = !Product::getTaxCalculationMethod($idCustomer);
}
return $this->taxCalculationMethod[$idCustomer];
}
}

View File

@@ -0,0 +1,108 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* 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.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* 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 https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
/**
* @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 TaxManagerInterface
*/
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 TaxManagerInterface|false
*/
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([$module_instance, 'hookTaxManager'])) {
$tax_manager = $module_instance->hookTaxManager([
'address' => $address,
'params' => $type,
]);
}
if ($tax_manager) {
break;
}
}
return $tax_manager;
}
/**
* Reset static cache (mainly for test environment)
*/
public static function resetStaticCache()
{
TaxManagerFactory::$cache_tax_manager = null;
}
/**
* 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;
}
}

View File

@@ -0,0 +1,47 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* 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.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* 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 https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
/**
* 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
*
* @return bool
*/
public static function isAvailableForThisAddress(Address $address);
/**
* Return the tax calculator associated to this address.
*
* @return TaxCalculator
*/
public function getTaxCalculator();
}

View File

@@ -0,0 +1,56 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* 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.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* 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 https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
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($this->trans('Incorrect Tax Manager class [%s]', [$this->tax_manager_class], 'Admin.International.Notification'));
}
require_once $class_file;
if (!class_exists($this->tax_manager_class)) {
die($this->trans('Tax Manager class not found [%s]', [$this->tax_manager_class], 'Admin.International.Notification'));
}
$class = $this->tax_manager_class;
if (call_user_func([$class, 'isAvailableForThisAddress'], $args['address'])) {
return new $class();
}
return false;
}
}

186
classes/tax/TaxRule.php Normal file
View File

@@ -0,0 +1,186 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* 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.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* 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 https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
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;
/**
* @see ObjectModel::$definition
*/
public static $definition = [
'table' => 'tax_rule',
'primary' => 'id_tax_rule',
'fields' => [
'id_tax_rules_group' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true],
'id_country' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true],
'id_state' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId'],
'zipcode_from' => ['type' => self::TYPE_STRING, 'validate' => 'isPostCode'],
'zipcode_to' => ['type' => self::TYPE_STRING, 'validate' => 'isPostCode'],
'id_tax' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true],
'behavior' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'],
'description' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
],
];
protected $webserviceParameters = [
'fields' => [
'id_tax_rules_group' => ['xlink_resource' => 'tax_rule_groups'],
'id_state' => ['xlink_resource' => 'states'],
'id_country' => ['xlink_resource' => 'countries'],
],
];
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_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`,
g.`id_country`,
g.`id_state`
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 . '
ORDER BY `country_name` ASC, `state_name` ASC, `zipcode_from` ASC, `zipcode_to` ASC'
);
}
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 bool
*/
public static function isTaxInUse($id_tax)
{
$cache_id = 'TaxRule::isTaxInUse_' . (int) $id_tax;
if (!Cache::isStored($cache_id)) {
$result = (int) Db::getInstance()->getValue('SELECT COUNT(*) FROM `' . _DB_PREFIX_ . 'tax_rule` WHERE `id_tax` = ' . (int) $id_tax);
Cache::store($cache_id, $result);
return $result;
}
return Cache::retrieve($cache_id);
}
/**
* @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);
$from = $zip_codes[0];
$to = isset($zip_codes[1]) ? $zip_codes[1] : 0;
if (count($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 (count($zip_codes) == 1) {
$from = $zip_codes[0];
$to = 0;
}
return [$from, $to];
}
/**
* Replace a tax_rule id by an other one in the tax_rule table.
*
* @param int $old_id
* @param int $new_id
*/
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
);
}
}

View File

@@ -0,0 +1,293 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* 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.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* 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 https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
class TaxRulesGroupCore extends ObjectModel
{
public $name;
/** @var bool active state */
public $active;
public $deleted = 0;
/** @var string Object creation date */
public $date_add;
/** @var string Object last modification date */
public $date_upd;
/**
* @see ObjectModel::$definition
*/
public static $definition = [
'table' => 'tax_rules_group',
'primary' => 'id_tax_rules_group',
'fields' => [
'name' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 64],
'active' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'deleted' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'date_add' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
'date_upd' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
],
];
protected $webserviceParameters = [
'objectsNodeName' => 'tax_rule_groups',
'objectNodeName' => 'tax_rule_group',
'fields' => [
],
];
protected static $_taxes = [];
public function update($null_values = false)
{
if (!$this->deleted && $this->isUsed()) {
$current_tax_rules_group = new TaxRulesGroup((int) $this->id);
if ((!$new_tax_rules_group = $current_tax_rules_group->duplicateObject()) || !$current_tax_rules_group->historize($new_tax_rules_group)) {
return false;
}
$this->id = (int) $new_tax_rules_group->id;
}
return parent::update($null_values);
}
/**
* Save the object with the field deleted to true.
*
* @return bool
*/
public function historize(TaxRulesGroup $tax_rules_group)
{
$this->deleted = true;
return parent::update() &&
Db::getInstance()->execute('
INSERT INTO ' . _DB_PREFIX_ . 'tax_rule
(id_tax_rules_group, id_country, id_state, zipcode_from, zipcode_to, id_tax, behavior, description)
(
SELECT ' . (int) $tax_rules_group->id . ', id_country, id_state, zipcode_from, zipcode_to, id_tax, behavior, description
FROM ' . _DB_PREFIX_ . 'tax_rule
WHERE id_tax_rules_group=' . (int) $this->id . '
)') &&
Db::getInstance()->execute('
UPDATE ' . _DB_PREFIX_ . 'product
SET id_tax_rules_group=' . (int) $tax_rules_group->id . '
WHERE id_tax_rules_group=' . (int) $this->id) &&
Db::getInstance()->execute('
UPDATE ' . _DB_PREFIX_ . 'product_shop
SET id_tax_rules_group=' . (int) $tax_rules_group->id . '
WHERE id_tax_rules_group=' . (int) $this->id) &&
Db::getInstance()->execute('
UPDATE ' . _DB_PREFIX_ . 'carrier
SET id_tax_rules_group=' . (int) $tax_rules_group->id . '
WHERE id_tax_rules_group=' . (int) $this->id) &&
Db::getInstance()->execute('
UPDATE ' . _DB_PREFIX_ . 'carrier_tax_rules_group_shop
SET id_tax_rules_group=' . (int) $tax_rules_group->id . '
WHERE id_tax_rules_group=' . (int) $this->id);
}
public function getIdTaxRuleGroupFromHistorizedId($id_tax_rule)
{
$params = Db::getInstance()->getRow(
'
SELECT id_country, id_state, zipcode_from, zipcode_to, id_tax, behavior
FROM ' . _DB_PREFIX_ . 'tax_rule
WHERE id_tax_rule=' . (int) $id_tax_rule
);
return Db::getInstance()->getValue(
'
SELECT id_tax_rule
FROM ' . _DB_PREFIX_ . 'tax_rule
WHERE
id_tax_rules_group = ' . (int) $this->id . ' AND
id_country=' . (int) $params['id_country'] . ' AND id_state=' . (int) $params['id_state'] . ' AND id_tax=' . (int) $params['id_tax'] . ' AND
zipcode_from=\'' . pSQL($params['zipcode_from']) . '\' AND zipcode_to=\'' . pSQL($params['zipcode_to']) . '\' AND behavior=' . (int) $params['behavior']
);
}
public static function getTaxRulesGroups($only_active = true)
{
return static::getTaxRulesGroupsData($only_active);
}
/**
* This method returns the list of TaxRulesGroup as array with default placeholder
* it is used to populate a select box. The returned array is formatted like this:
* [
* [
* 'id_tax_rules_group' => ...,
* 'name' => ...,
* 'active' => ...,
* 'rate' => ...,
* ],
* ...
* ]
*
* @return array an array of tax rules group formatted as:
*/
public static function getTaxRulesGroupsForOptions()
{
$tax_rules[] = [
'id_tax_rules_group' => 0,
'name' => Context::getContext()->getTranslator()->trans('No tax', [], 'Admin.International.Notification'),
];
return array_merge($tax_rules, TaxRulesGroup::getTaxRulesGroupsData(true, true));
}
/**
* @param bool $onlyActive Filter active tax rules group only
* @param bool $includeRates Include tax rate amount in returned data
*
* @return array|false
*
* @throws PrestaShopDatabaseException
*/
private static function getTaxRulesGroupsData($onlyActive = true, bool $includeRates = false)
{
$sql = 'SELECT DISTINCT g.id_tax_rules_group, g.name, g.active';
if ($includeRates) {
$sql .= ', t.rate';
}
$sql .= ' FROM `' . _DB_PREFIX_ . 'tax_rules_group` g';
if ($includeRates) {
$sql .= '
INNER JOIN ' . _DB_PREFIX_ . 'tax_rule tr
ON g.id_tax_rules_group = tr.id_tax_rules_group
INNER JOIN ' . _DB_PREFIX_ . 'tax t
ON tr.id_tax = t.id_tax
';
}
$sql .= Shop::addSqlAssociation('tax_rules_group', 'g') . ' WHERE g.deleted = 0'
. ($onlyActive ? ' AND g.`active` = 1' : '')
. ' ORDER BY name ASC';
return Db::getInstance()->executeS($sql);
}
public function delete()
{
$res = Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'tax_rule` WHERE `id_tax_rules_group`=' . (int) $this->id);
return parent::delete() && $res;
}
/**
* @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 = [];
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) . '\''
);
}
public function hasUniqueTaxRuleForCountry($id_country, $id_state, $id_tax_rule = false)
{
$rules = TaxRule::getTaxRulesByGroupId((int) Context::getContext()->language->id, (int) $this->id);
foreach ($rules as $rule) {
if ($rule['id_country'] == $id_country && $id_state == $rule['id_state'] && !$rule['behavior'] && (int) $id_tax_rule != $rule['id_tax_rule']) {
return true;
}
}
return false;
}
public function isUsed()
{
return Db::getInstance()->getValue(
'
SELECT `id_tax_rules_group`
FROM `' . _DB_PREFIX_ . 'order_detail`
WHERE `id_tax_rules_group` = ' . (int) $this->id
);
}
/**
* @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 [];
}
}

View File

@@ -0,0 +1,138 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* 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.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* 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 https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
use PrestaShop\PrestaShop\Adapter\ServiceLocator;
/**
* @since 1.5.0.1
*/
class TaxRulesTaxManagerCore implements TaxManagerInterface
{
public $address;
public $type;
public $tax_calculator;
/**
* @var \PrestaShop\PrestaShop\Core\ConfigurationInterface
*/
private $configurationManager;
/**
* @param Address $address
* @param mixed $type An additional parameter for the tax manager (ex: tax rules id for TaxRuleTaxManager)
*/
public function __construct(Address $address, $type, PrestaShop\PrestaShop\Core\ConfigurationInterface $configurationManager = null)
{
if ($configurationManager === null) {
$this->configurationManager = ServiceLocator::get('\\PrestaShop\\PrestaShop\\Core\\ConfigurationInterface');
} else {
$this->configurationManager = $configurationManager;
}
// We clone the address so that the information use by this TaxManager never change (address can be modified somewhere else)
$this->address = clone $address;
$this->type = $type;
}
/**
* Returns true if this tax manager is available for this address.
*
* @return bool
*/
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()
{
static $tax_enabled = null;
if (isset($this->tax_calculator)) {
return $this->tax_calculator;
}
if ($tax_enabled === null) {
$tax_enabled = $this->configurationManager->get('PS_TAX');
}
if (!$tax_enabled) {
return new TaxCalculator([]);
}
$taxes = [];
$postcode = 0;
if (!empty($this->address->postcode)) {
$postcode = $this->address->postcode;
}
$cache_id = (int) $this->address->id_country . '-' . (int) $this->address->id_state . '-' . $postcode . '-' . (int) $this->type;
if (!Cache::isStored($cache_id)) {
$rows = Db::getInstance()->executeS('
SELECT tr.*
FROM `' . _DB_PREFIX_ . 'tax_rule` tr
JOIN `' . _DB_PREFIX_ . 'tax_rules_group` trg ON (tr.`id_tax_rules_group` = trg.`id_tax_rules_group`)
WHERE trg.`active` = 1
AND tr.`id_country` = ' . (int) $this->address->id_country . '
AND tr.`id_tax_rules_group` = ' . (int) $this->type . '
AND tr.`id_state` IN (0, ' . (int) $this->address->id_state . ')
AND (\'' . pSQL($postcode) . '\' BETWEEN tr.`zipcode_from` AND tr.`zipcode_to`
OR (tr.`zipcode_to` = 0 AND tr.`zipcode_from` IN(0, \'' . pSQL($postcode) . '\')))
ORDER BY tr.`zipcode_from` DESC, tr.`zipcode_to` DESC, tr.`id_state` DESC, tr.`id_country` DESC');
$behavior = 0;
$first_row = true;
foreach ($rows as $row) {
$tax = new Tax((int) $row['id_tax']);
$taxes[] = $tax;
// the applied behavior correspond to the most specific rules
if ($first_row) {
$behavior = $row['behavior'];
$first_row = false;
}
if ($row['behavior'] == 0) {
break;
}
}
$result = new TaxCalculator($taxes, $behavior);
Cache::store($cache_id, $result);
return $result;
}
return Cache::retrieve($cache_id);
}
}

34
classes/tax/index.php Normal file
View File

@@ -0,0 +1,34 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* 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.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* 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 https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../../');
exit;