This repository has been archived on 2021-01-04. You can view files and clone it, but cannot push or open issues or pull requests.
TaxRatesAdjustment/Models/taxRateAbstract.php

125 lines
4.3 KiB
PHP
Raw Normal View History

2020-06-05 16:29:31 +02:00
<?php
/**
* This Software is the property of Data Development and is protected
* by copyright law - it is NOT Freeware.
* Any unauthorized use of this software without a valid license
* is a violation of the license agreement and will be prosecuted by
* civil and criminal law.
* http://www.shopmodule.com
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link http://www.oxidmodule.com
*/
namespace D3\TaxRatesAdjustment\Models;
use OxidEsales\Eshop\Application\Model\Article;
use OxidEsales\Eshop\Application\Model\Shop;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\DatabaseProvider;
use OxidEsales\Eshop\Core\Exception\StandardException;
use OxidEsales\Eshop\Core\Registry;
2020-06-25 11:37:04 +02:00
abstract class taxRateAbstract extends genericAbstract
2020-06-05 16:29:31 +02:00
{
/**
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseErrorException
*/
public function run()
{
if (false === $this->isInExecutableTimeRange()) {
trigger_error("script shouldn't run outside the defined time range", E_USER_WARNING);
die();
};
2020-06-07 14:39:01 +02:00
$this->changeTaxRates();
2020-06-05 16:29:31 +02:00
}
/**
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseErrorException
*/
2020-06-07 14:39:01 +02:00
public function changeTaxRates()
{
2020-06-05 16:29:31 +02:00
$shop = new Shop();
2020-06-07 14:39:01 +02:00
// use shop list, when parameter -d is set
$opts = getopt("s:");
$where = isset($opts['s']) ?
"oxid IN (".implode(', ', array_map(
function ($a) {return DatabaseProvider::getDb()->quote(trim($a));},
explode(',', $opts['s']))
).")" :
"1";
$q = "SELECT oxid FROM " . $shop->getCoreTableName() . " WHERE ".$where ;
2020-06-05 16:29:31 +02:00
foreach ( DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC )->getAll( $q ) as $record ) {
2020-06-07 14:39:01 +02:00
$shopId = (int) $record["oxid"];
2020-06-05 16:29:31 +02:00
$this->switchToShop($shopId);
$this->changeDefaultTaxRate( $shopId );
$this->changeArticlesTaxRate( $shopId );
}
}
/**
* @param int $shopId
*/
public function changeDefaultTaxRate($shopId)
{
$oCurrConfig = new Config();
$newVat = $this->rateChanges[(int) $oCurrConfig->getConfigParam('dDefaultVAT')];
if ($newVat) {
$oCurrConfig->saveShopConfVar('num', 'dDefaultVAT', $newVat);
echo "default tax rate was sucessfully changed in shop ".$shopId.PHP_EOL;
} else {
echo "no changeable default tax rate found in shop ".$shopId.PHP_EOL;
}
}
/**
* @param int $shopId
*
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseErrorException
*/
public function changeArticlesTaxRate($shopId)
{
$article = oxNew(Article::class);
$q = "SELECT oxid FROM ".$article->getCoreTableName()."
WHERE oxvat IN (".implode(', ', array_keys($this->rateChanges)).")
AND oxshopid = ".DatabaseProvider::getDb()->quote($shopId);
$counter = 0;
foreach (DatabaseProvider::getDb(DatabaseProvider::FETCH_MODE_ASSOC)->getAll($q) as $articleRecord) {
$articleId = $articleRecord['oxid'];
$article = oxNew(Article::class);
$article->load($articleId);
$article->assign(
[
'oxvat' => $this->rateChanges[(int) $article->getFieldData('oxvat')]
]
);
$article->save();
$counter++;
}
if ($counter) {
echo "the tax rate for " . $counter . " article(s) was changed in shop " . $shopId . PHP_EOL;
}
$q = "SELECT count(*) FROM ".$article->getCoreTableName()."
WHERE oxvat IN (".implode(', ', array_keys($this->rateChanges)).")
AND oxshopid = ".DatabaseProvider::getDb()->quote($shopId);
if ($counter = DatabaseProvider::getDb()->getOne($q)) {
echo "the tax rate update for " . $counter . " article(s) was failed in shop " . $shopId . PHP_EOL;
}
}
2020-06-06 13:25:49 +02:00
}