From f742024e79d6a54183177897422fc67390ea8b5c Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Fri, 5 Jun 2020 16:29:31 +0200 Subject: [PATCH] initial implementation --- Models/raiseTaxRate.php | 28 ++++++- Models/reduceTaxRate.php | 26 +++++++ Models/taxRateAbstract.php | 154 +++++++++++++++++++++++++++++++++++++ README.md | 14 +++- bin/reduceTaxRate | 13 ++++ 5 files changed, 231 insertions(+), 4 deletions(-) create mode 100644 Models/reduceTaxRate.php create mode 100644 Models/taxRateAbstract.php diff --git a/Models/raiseTaxRate.php b/Models/raiseTaxRate.php index 67d344d..3f49cff 100644 --- a/Models/raiseTaxRate.php +++ b/Models/raiseTaxRate.php @@ -1,11 +1,33 @@ + * @link http://www.oxidmodule.com + */ + +// ShopId + namespace D3\TaxRatesAdjustment\Models; -class raiseTaxRate +use OxidEsales\Eshop\Core\Registry; + +class raiseTaxRate extends taxRateAbstract { - public function run() + public $execPeriod = [ + '2020-12-30', + '2021-01-03', + ]; + + public function __construct() { - dumpvar(__METHOD__); + $this->rateChanges = array_flip($this->rateChanges); } } \ No newline at end of file diff --git a/Models/reduceTaxRate.php b/Models/reduceTaxRate.php new file mode 100644 index 0000000..9734984 --- /dev/null +++ b/Models/reduceTaxRate.php @@ -0,0 +1,26 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\TaxRatesAdjustment\Models; + +use OxidEsales\Eshop\Core\Registry; + +class reduceTaxRate extends taxRateAbstract +{ + public $execPeriod = [ + '2020-06-01', + '2020-07-03', + ]; +} \ No newline at end of file diff --git a/Models/taxRateAbstract.php b/Models/taxRateAbstract.php new file mode 100644 index 0000000..f88470f --- /dev/null +++ b/Models/taxRateAbstract.php @@ -0,0 +1,154 @@ + + * @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; + +abstract class taxRateAbstract +{ + public $execPeriod = [ + '2020-01-01', + '2019-12-31', + ]; + + public $rateChanges = [ + 19 => 16, + 7 => 5 + ]; + + /** + * @return bool + */ + public function isInExecutableTimeRange() + { + list($from, $to) = $this->execPeriod; + + return (time() > strtotime($from)) && (time() < strtotime($to)); + } + + /** + * @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(); + }; + + $this->changeDefaultTaxRates(); + } + + /** + * @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException + * @throws \OxidEsales\Eshop\Core\Exception\DatabaseErrorException + */ + public function changeDefaultTaxRates() { + $shop = new Shop(); + + $q = "SELECT oxid FROM " . $shop->getCoreTableName() . " WHERE 1"; + foreach ( DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC )->getAll( $q ) as $record ) { + $shopId = (int) $record['oxid']; + $this->switchToShop($shopId); + $this->changeDefaultTaxRate( $shopId ); + $this->changeArticlesTaxRate( $shopId ); + } + } + + /** + * @param int $id + * + * @throws \oxConnectionException + */ + public function switchToShop($id) + { + if (Registry::getConfig()->isMall() + && $id != Registry::getConfig()->getActiveShop()->getId() + ) { + /** @var Config $oNewConf */ + $oNewConf = new Config(); + $oNewConf->setShopId($id); + $oNewConf->init(); + + Registry::getConfig()->onShopChange(); + Registry::getSession()->setVariable('actshop', $id); + Registry::getSession()->setVariable('currentadminshop', $id); + Registry::getConfig()->setShopId($id); + } + } + + /** + * @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; + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index a885a2c..48c84b1 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,15 @@ # TaxRatesAdjustment -Anpassung der Steuersätze (speziell für den 01.07.2020 + 01.01.2021) \ No newline at end of file +Anpassung der Steuersätze im Rahmen des deutschen Konjunkturpaketes 2020 / 2021 + +per Cronjob am 01.07.2020 auszufuehren: + +``` +./vendor/bin/reduceTaxRate +``` + +per Cronjob am 01.01.2021 auszufuehren: + +``` +./vendor/bin/raiseTaxRate +``` \ No newline at end of file diff --git a/bin/reduceTaxRate b/bin/reduceTaxRate index c8ff8c2..0e47ebc 100644 --- a/bin/reduceTaxRate +++ b/bin/reduceTaxRate @@ -1,2 +1,15 @@ #!/usr/bin/env php run(); +} catch ( \Exception $e) { + echo $e->getMessage(); +} \ No newline at end of file