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 ;
2020-06-08 22:16:22 +02:00
use oxArticle ;
use oxConfig ;
use oxDb ;
use oxRegistry ;
use oxShop ;
2020-06-05 16:29:31 +02:00
abstract class taxRateAbstract
{
public $execPeriod = [
'2020-01-01' ,
'2019-12-31' ,
];
public $rateChanges = [
19 => 16 ,
7 => 5
];
/**
* @ return bool
*/
public function isInExecutableTimeRange ()
{
2020-06-06 13:25:49 +02:00
// skip time check, when parameter -d is set
$opts = getopt ( " d " );
if ( is_array ( $opts ) && isset ( $opts [ 'd' ])) {
return true ;
}
2020-06-05 16:29:31 +02:00
list ( $from , $to ) = $this -> execPeriod ;
return ( time () > strtotime ( $from )) && ( time () < strtotime ( $to ));
}
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
}
2020-06-07 14:39:01 +02:00
public function changeTaxRates ()
{
2020-06-08 22:16:22 +02:00
$shop = new oxShop ();
2020-06-05 16:29:31 +02:00
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 (
2020-06-08 22:16:22 +02:00
function ( $a ) { return oxDb :: getDb () -> quote ( trim ( $a ));},
2020-06-07 14:39:01 +02:00
explode ( ',' , $opts [ 's' ]))
) . " ) " :
" 1 " ;
$q = " SELECT oxid FROM " . $shop -> getCoreTableName () . " WHERE " . $where ;
2020-06-08 22:16:22 +02:00
foreach ( oxDb :: getDb ( oxDb :: FETCH_MODE_ASSOC ) -> getAll ( $q ) as $record ) {
2020-06-11 15:09:34 +02:00
$shopId = $record [ " oxid " ];
2020-06-05 16:29:31 +02:00
$this -> switchToShop ( $shopId );
$this -> changeDefaultTaxRate ( $shopId );
$this -> changeArticlesTaxRate ( $shopId );
}
}
/**
* @ param int $id
*
* @ throws \oxConnectionException
*/
public function switchToShop ( $id )
{
2020-06-08 22:16:22 +02:00
if ( oxRegistry :: getConfig () -> isMall ()
&& $id != oxRegistry :: getConfig () -> getActiveShop () -> getId ()
2020-06-05 16:29:31 +02:00
) {
2020-06-08 22:16:22 +02:00
/** @var oxConfig $oNewConf */
$oNewConf = new oxConfig ();
2020-06-05 16:29:31 +02:00
$oNewConf -> setShopId ( $id );
$oNewConf -> init ();
2020-06-08 22:16:22 +02:00
oxRegistry :: getConfig () -> onShopChange ();
oxRegistry :: getSession () -> setVariable ( 'actshop' , $id );
oxRegistry :: getSession () -> setVariable ( 'currentadminshop' , $id );
oxRegistry :: getConfig () -> setShopId ( $id );
2020-06-05 16:29:31 +02:00
}
}
/**
* @ param int $shopId
*/
public function changeDefaultTaxRate ( $shopId )
{
2020-06-08 22:16:22 +02:00
$oCurrConfig = new oxConfig ();
2020-06-05 16:29:31 +02:00
$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
*/
public function changeArticlesTaxRate ( $shopId )
{
2020-06-08 22:16:22 +02:00
$article = oxNew ( oxArticle :: class );
2020-06-05 16:29:31 +02:00
$q = " SELECT oxid FROM " . $article -> getCoreTableName () . "
WHERE oxvat IN ( " .implode(', ', array_keys( $this->rateChanges )). " )
2020-06-08 22:16:22 +02:00
AND oxshopid = " . oxDb::getDb()->quote( $shopId );
2020-06-05 16:29:31 +02:00
$counter = 0 ;
2020-06-08 22:16:22 +02:00
foreach ( oxDb :: getDb ( oxDb :: FETCH_MODE_ASSOC ) -> getAll ( $q ) as $articleRecord ) {
2020-06-05 16:29:31 +02:00
$articleId = $articleRecord [ 'oxid' ];
2020-06-08 22:16:22 +02:00
$article = oxNew ( oxArticle :: class );
2020-06-05 16:29:31 +02:00
$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 )). " )
2020-06-08 22:16:22 +02:00
AND oxshopid = " . oxDb::getDb()->quote( $shopId );
2020-06-05 16:29:31 +02:00
2020-06-08 22:16:22 +02:00
if ( $counter = oxDb :: getDb () -> getOne ( $q )) {
2020-06-05 16:29:31 +02:00
echo " the tax rate update for " . $counter . " article(s) was failed in shop " . $shopId . PHP_EOL ;
}
}
2020-06-06 13:25:49 +02:00
}