00001 <?php
00002
00006 class oxDiscount extends oxI18n
00007 {
00013 protected $_sClassName = 'oxdiscount';
00014
00020 protected $_dAmount = null;
00021
00027 protected $_sBasketIdent = null;
00028
00032 public function __construct()
00033 {
00034 parent::__construct();
00035 $this->init( 'oxdiscount' );
00036 }
00037
00045 public function delete( $sOXID = null )
00046 {
00047 if ( !$sOXID ) {
00048 $sOXID = $this->getId();
00049 }
00050 if ( !$sOXID ) {
00051 return false;
00052 }
00053
00054
00055 $oDB = oxDb::getDb();
00056 $oDB->execute( 'delete from oxobject2discount where oxobject2discount.oxdiscountid = '.$oDB->quote($sOXID) );
00057
00058 return parent::delete( $sOXID );
00059 }
00060
00068 public function isForArticle( $oArticle )
00069 {
00070
00071 if ( $this->oxdiscount__oxaddsumtype->value == 'itm' ) {
00072 return false;
00073 }
00074
00075 if ( $this->oxdiscount__oxamount->value || $this->oxdiscount__oxprice->value ) {
00076 return false;
00077 }
00078
00079 if ( $this->oxdiscount__oxpriceto->value && ($this->oxdiscount__oxpriceto->value < $oArticle->getBasePrice()) ) {
00080 return false;
00081 }
00082
00083 $myDB = oxDb::getDb();
00084
00085 $sDiscountIdQuoted = $myDB->quote($this->oxdiscount__oxid->value);
00086
00087 $sQ = "select 1 from oxobject2discount where oxdiscountid = $sDiscountIdQuoted and ( oxtype = 'oxarticles' or oxtype = 'oxcategories')";
00088 $blOk = (bool) $myDB->getOne( $sQ );
00089
00090 if ( !$blOk ) {
00091 return true;
00092 }
00093
00094
00095 $sArticleId = "";
00096 if ( $sParentId = $oArticle->getProductParentId() ) {
00097 $sArticleId = "(oxobjectid = '".$oArticle->getProductId()."' or oxobjectid = ".$myDB->quote($sParentId).")";
00098 } else {
00099 $sArticleId = "oxobjectid = '".$oArticle->getProductId()."'";
00100 }
00101
00102 $sQ = "select 1 from oxobject2discount where oxdiscountid = $sDiscountIdQuoted and {$sArticleId} and oxtype = 'oxarticles'";
00103 $blOk = (bool) $myDB->getOne( $sQ );
00104 if ( $blOk ) {
00105 return true;
00106 } else {
00107
00108 $aCatIds = $oArticle->getCategoryIds();
00109 if (!$aCatIds || !count($aCatIds)) {
00110
00111 return false;
00112 }
00113 $sCatIds = "(".implode(",", oxDb::getInstance()->quoteArray($aCatIds)).")";
00114
00115 $sQ = "select oxobjectid from oxobject2discount where oxdiscountid = $sDiscountIdQuoted and oxobjectid in $sCatIds and oxtype = 'oxcategories'";
00116 if ( ( bool ) $myDB->getOne( $sQ ) ) {
00117 return true;
00118 }
00119 }
00120 return false;
00121 }
00122
00130 public function isForBasketItem( $oArticle )
00131 {
00132 if ( $this->oxdiscount__oxamount->value == 0 && $this->oxdiscount__oxprice->value == 0 ) {
00133 return false;
00134 }
00135
00136
00137 if ( $this->oxdiscount__oxaddsumtype->value == 'itm' ) {
00138 return false;
00139 }
00140
00141 $myDB = oxDb::getDb();
00142
00143 $sArticleId = "";
00144 if ( $sParentId = $oArticle->getProductParentId() ) {
00145 $sArticleId = "(oxobjectid = '".$oArticle->getProductId()."' or oxobjectid = '{$sParentId}')";
00146 } else {
00147 $sArticleId = "oxobjectid = '".$oArticle->getProductId()."'";
00148 }
00149 $sQ = "select 1 from oxobject2discount where oxdiscountid = '{$this->oxdiscount__oxid->value}' and ($sArticleId) and oxtype = 'oxarticles'";
00150 if ( !( $blOk = ( bool ) $myDB->getOne( $sQ ) ) ) {
00151
00152
00153 $blOk = $this->_checkForArticleCategories( $oArticle );
00154 }
00155
00156 return $blOk;
00157 }
00158
00166 public function isForBasketAmount( $oBasket )
00167 {
00168 $dAmount = 0;
00169 $aBasketItems = $oBasket->getContents();
00170 foreach ( $aBasketItems as $oBasketItem ) {
00171
00172 $oBasketArticle = $oBasketItem->getArticle(false);
00173
00174 $blForBasketItem = false;
00175 if ( $this->oxdiscount__oxaddsumtype->value != 'itm' ) {
00176 $blForBasketItem = $this->isForBasketItem( $oBasketArticle );
00177 } else {
00178 $blForBasketItem = $this->isForBundleItem( $oBasketArticle );
00179 }
00180
00181 if ( $blForBasketItem ) {
00182 if ( $this->oxdiscount__oxprice->value ) {
00183 $dAmount += $oBasketArticle->getPrice()->getBruttoPrice() * $oBasketItem->getAmount();
00184 } elseif ( $this->oxdiscount__oxamount->value ) {
00185 $dAmount += $oBasketItem->getAmount();
00186 }
00187 }
00188 }
00189
00190 return $this->isForAmount( $dAmount );
00191 }
00192
00200 public function isForAmount( $dAmount )
00201 {
00202 $blIs = true;
00203 if ( $this->oxdiscount__oxprice->value &&
00204 ( $dAmount < $this->oxdiscount__oxprice->value || $dAmount > $this->oxdiscount__oxpriceto->value ) ) {
00205 $blIs = false;
00206 } elseif ( $this->oxdiscount__oxamount->value &&
00207 ( $dAmount < $this->oxdiscount__oxamount->value || $dAmount > $this->oxdiscount__oxamountto->value ) ) {
00208 $blIs = false;
00209 }
00210 return $blIs;
00211 }
00212
00220 public function isForBasket( $oBasket )
00221 {
00222
00223 if ( $this->oxdiscount__oxamount->value == 0 && $this->oxdiscount__oxprice->value == 0 ) {
00224 return false;
00225 }
00226
00227 $oSummary = $oBasket->getBasketSummary();
00228
00229 if ( $this->oxdiscount__oxamount->value && ( $oSummary->iArticleCount < $this->oxdiscount__oxamount->value || $oSummary->iArticleCount > $this->oxdiscount__oxamountto->value ) ) {
00230 return false;
00231
00232 } elseif ($this->oxdiscount__oxprice->value) {
00233 $dRate = $this->getConfig()->getActShopCurrencyObject()->rate;
00234 if ( $oSummary->dArticlePrice < $this->oxdiscount__oxprice->value*$dRate || $oSummary->dArticlePrice > $this->oxdiscount__oxpriceto->value*$dRate ) {
00235 return false;
00236 }
00237 }
00238
00239
00240 $sQ = 'select 1 from oxobject2discount where oxdiscountid = '.oxDb::getDb()->quote($this->oxdiscount__oxid->value).' and oxtype in ("oxarticles", "oxcategories" ) ';
00241
00242 return !( (bool) oxDb::getDb()->getOne( $sQ ) );
00243 }
00244
00252 public function isForBundleItem( $oArticle )
00253 {
00254 if ( $this->oxdiscount__oxaddsumtype->value != 'itm' ) {
00255 return false;
00256 }
00257
00258 $sSelect = "select 1 from oxobject2discount where oxdiscountid='".$this->getId()."' and oxobjectid='".$oArticle->getProductId()."' ";
00259 if ( !( $blOk = (bool) oxDb::getDb()->getOne( $sSelect ) ) ) {
00260
00261 $blOk = $this->_checkForArticleCategories( $oArticle );
00262 }
00263 return $blOk;
00264 }
00265
00273 public function isForBundleBasket( $oBasket )
00274 {
00275 if ( $this->oxdiscount__oxaddsumtype->value != 'itm' ) {
00276 return false;
00277 }
00278
00279 return $this->isForBasket( $oBasket );
00280 }
00281
00290 public function getAbsValue( $dPrice, $dAmount = 1 )
00291 {
00292 if ( $this->oxdiscount__oxaddsumtype->value == '%' ) {
00293 return $dPrice * ( $this->oxdiscount__oxaddsum->value / 100 );
00294 } else {
00295 $oCur = $this->getConfig()->getActShopCurrencyObject();
00296 return $this->oxdiscount__oxaddsum->value * $dAmount * $oCur->rate;
00297 }
00298 }
00299
00308 public function applyDiscount( $oPrice, $dAmount = 1 )
00309 {
00310 if ( $this->oxdiscount__oxaddsumtype->value == 'abs' ) {
00311
00312 $oCur = $this->getConfig()->getActShopCurrencyObject();
00313
00314 $oDiscountPrice = oxNew( 'oxprice' );
00315 $oDiscountPrice->setBruttoPriceMode();
00316 $oDiscountPrice->setPrice( $this->oxdiscount__oxaddsum->value * $oCur->rate, $oPrice->getVat() );
00317 } else {
00318
00319 $oDiscountPrice = oxNew( 'oxprice' );
00320 $oDiscountPrice->setBruttoPriceMode();
00321 $oDiscountPrice->setPrice( $oPrice->getBruttoPrice() / 100 * $this->oxdiscount__oxaddsum->value, $oPrice->getVat() );
00322 }
00323
00324 $oDiscountPrice->multiply( $dAmount * -1 );
00325 $oPrice->addPrice( $oDiscountPrice );
00326 }
00327
00335 public function getBundleAmount( $dAmount )
00336 {
00337 $dItemAmount = $this->oxdiscount__oxitmamount->value;
00338
00339
00340 if ( $this->oxdiscount__oxitmmultiple->value && $this->oxdiscount__oxamount->value > 0 ) {
00341 $dItemAmount = floor( $dAmount / $this->oxdiscount__oxamount->value ) * $this->oxdiscount__oxitmamount->value;
00342 }
00343
00344 return $dItemAmount;
00345 }
00346
00354 protected function _checkForArticleCategories( $oArticle )
00355 {
00356
00357 $aCatIds = $oArticle->getCategoryIds();
00358 if (!$aCatIds || !count($aCatIds)) {
00359
00360 return false;
00361 }
00362
00363 $sCatIds = "(".implode(",", oxDb::getInstance()->quoteArray($aCatIds)).")";
00364
00365 $oDb = oxDb::getDb();
00366
00367 $sQ = "select oxobjectid from oxobject2discount where oxdiscountid = ".$oDb->quote($this->oxdiscount__oxid->value)." and oxobjectid in $sCatIds and oxtype = 'oxcategories'";
00368
00369 return $oDb->getOne( $sQ );
00370 }
00371
00377 public function getSimpleDiscount()
00378 {
00379 $oDiscount = new OxStdClass();
00380 $oDiscount->sOXID = $this->getId();
00381 $oDiscount->sDiscount = $this->oxdiscount__oxtitle->value;
00382 $oDiscount->sType = $this->oxdiscount__oxaddsumtype->value;
00383
00384 return $oDiscount;
00385 }
00386
00387 }