oxdiscount.php

Go to the documentation of this file.
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         // item discounts may only be applied for basket
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         //check for global discount (no articles, no categories)
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         // check if this article is assigned
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             // check if article is in some assigned category
00108             $aCatIds = $oArticle->getCategoryIds();
00109             if (!$aCatIds || !count($aCatIds)) {
00110                 // no categories are set for article, so no discounts from categories..
00111                 return false;
00112             }
00113             $sCatIds = "(".implode(",", oxDb::getInstance()->quoteArray($aCatIds)).")";
00114             // getOne appends limit 1, so this one should be fast enough
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         // skipping bundle discounts
00137         if ( $this->oxdiscount__oxaddsumtype->value == 'itm' ) {
00138             return false;
00139         }
00140 
00141         $myDB = oxDb::getDb();
00142         // check if this article is assigned
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             // checkin article cateogry
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                     if ( ( $oPrice = $oBasketArticle->getPrice() ) ) {
00184                         $dAmount += $oPrice->getBruttoPrice() * $oBasketItem->getAmount();
00185                     }
00186                 } elseif ( $this->oxdiscount__oxamount->value ) {
00187                     $dAmount += $oBasketItem->getAmount();
00188                 }
00189             }
00190         }
00191 
00192         return $this->isForAmount( $dAmount );
00193     }
00194 
00202     public function isForAmount( $dAmount )
00203     {
00204         $blIs = true;
00205         if ( $this->oxdiscount__oxprice->value &&
00206             ( $dAmount < $this->oxdiscount__oxprice->value || $dAmount > $this->oxdiscount__oxpriceto->value ) ) {
00207             $blIs = false;
00208         } elseif ( $this->oxdiscount__oxamount->value &&
00209             ( $dAmount < $this->oxdiscount__oxamount->value || $dAmount > $this->oxdiscount__oxamountto->value ) ) {
00210             $blIs = false;
00211         }
00212         return $blIs;
00213     }
00214 
00222     public function isForBasket( $oBasket )
00223     {
00224         // initial configuration check
00225         if ( $this->oxdiscount__oxamount->value == 0 && $this->oxdiscount__oxprice->value == 0 ) {
00226             return false;
00227         }
00228 
00229         $oSummary = $oBasket->getBasketSummary();
00230         // amounts check
00231         if ( $this->oxdiscount__oxamount->value && ( $oSummary->iArticleCount < $this->oxdiscount__oxamount->value || $oSummary->iArticleCount > $this->oxdiscount__oxamountto->value ) ) {
00232             return false;
00233             // price check
00234         } elseif ($this->oxdiscount__oxprice->value) {
00235             $dRate = $this->getConfig()->getActShopCurrencyObject()->rate;
00236             if ( $oSummary->dArticlePrice < $this->oxdiscount__oxprice->value*$dRate || $oSummary->dArticlePrice > $this->oxdiscount__oxpriceto->value*$dRate ) {
00237                 return false;
00238             }
00239         }
00240 
00241         // oxobject2discount configuration check
00242         $sQ = 'select 1 from oxobject2discount where oxdiscountid = '.oxDb::getDb()->quote($this->oxdiscount__oxid->value).' and oxtype in ("oxarticles", "oxcategories" ) ';
00243 
00244         return !( (bool) oxDb::getDb()->getOne( $sQ ) );
00245     }
00246 
00254     public function isForBundleItem( $oArticle )
00255     {
00256         if ( $this->oxdiscount__oxaddsumtype->value != 'itm' ) {
00257             return false;
00258         }
00259 
00260         $sSelect = "select 1 from oxobject2discount where oxdiscountid='".$this->getId()."' and oxobjectid='".$oArticle->getProductId()."' ";
00261         if ( !( $blOk = (bool) oxDb::getDb()->getOne( $sSelect ) ) ) {
00262             // additional checks for amounts and other dependencies
00263             $blOk = $this->_checkForArticleCategories( $oArticle );
00264         }
00265         return $blOk;
00266     }
00267 
00275     public function isForBundleBasket( $oBasket )
00276     {
00277         if ( $this->oxdiscount__oxaddsumtype->value != 'itm' ) {
00278             return false;
00279         }
00280 
00281         return $this->isForBasket( $oBasket );
00282     }
00283 
00292     public function getAbsValue( $dPrice, $dAmount = 1 )
00293     {
00294         if ( $this->oxdiscount__oxaddsumtype->value == '%' ) {
00295             return $dPrice * ( $this->oxdiscount__oxaddsum->value / 100 );
00296         } else {
00297             $oCur = $this->getConfig()->getActShopCurrencyObject();
00298             return $this->oxdiscount__oxaddsum->value * $dAmount * $oCur->rate;
00299         }
00300     }
00301 
00310     public function applyDiscount( $oPrice, $dAmount = 1 )
00311     {
00312         if ( $this->oxdiscount__oxaddsumtype->value == 'abs' ) {
00313 
00314             $oCur = $this->getConfig()->getActShopCurrencyObject();
00315 
00316             $oDiscountPrice = oxNew( 'oxprice' );
00317             $oDiscountPrice->setBruttoPriceMode();
00318             $oDiscountPrice->setPrice( $this->oxdiscount__oxaddsum->value * $oCur->rate, $oPrice->getVat() );
00319         } else {
00320 
00321             $oDiscountPrice = oxNew( 'oxprice' );
00322             $oDiscountPrice->setBruttoPriceMode();
00323             $oDiscountPrice->setPrice( $oPrice->getBruttoPrice() / 100 * $this->oxdiscount__oxaddsum->value, $oPrice->getVat() );
00324         }
00325 
00326         $oDiscountPrice->multiply( $dAmount * -1 );
00327         $oPrice->addPrice( $oDiscountPrice );
00328     }
00329 
00337     public function getBundleAmount( $dAmount )
00338     {
00339         $dItemAmount = $this->oxdiscount__oxitmamount->value;
00340 
00341         // Multiplying bundled articles count, if allowed
00342         if ( $this->oxdiscount__oxitmmultiple->value && $this->oxdiscount__oxamount->value > 0 ) {
00343             $dItemAmount = floor( $dAmount / $this->oxdiscount__oxamount->value ) * $this->oxdiscount__oxitmamount->value;
00344         }
00345 
00346         return $dItemAmount;
00347     }
00348 
00356     protected function _checkForArticleCategories( $oArticle )
00357     {
00358         // check if article is in some assigned category
00359         $aCatIds = $oArticle->getCategoryIds();
00360         if (!$aCatIds || !count($aCatIds)) {
00361             // no categories are set for article, so no discounts from categories..
00362             return false;
00363         }
00364 
00365         $sCatIds = "(".implode(",", oxDb::getInstance()->quoteArray($aCatIds)).")";
00366 
00367         $oDb = oxDb::getDb();
00368         // getOne appends limit 1, so this one should be fast enough
00369         $sQ = "select oxobjectid from oxobject2discount where oxdiscountid = ".$oDb->quote($this->oxdiscount__oxid->value)." and oxobjectid in $sCatIds and oxtype = 'oxcategories'";
00370 
00371         return $oDb->getOne( $sQ );
00372     }
00373 
00379     public function getSimpleDiscount()
00380     {
00381         $oDiscount = new OxStdClass();
00382         $oDiscount->sOXID     = $this->getId();
00383         $oDiscount->sDiscount = $this->oxdiscount__oxtitle->value;
00384         $oDiscount->sType     = $this->oxdiscount__oxaddsumtype->value;
00385 
00386         return $oDiscount;
00387     }
00388 
00389 }

Generated on Mon Oct 26 20:07:16 2009 for OXID eShop CE by  doxygen 1.5.5