oxdiscount.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxDiscount extends oxI18n
00008 {
00014     protected $_sClassName = 'oxdiscount';
00015 
00021     protected $_dAmount = null;
00022 
00028     protected $_sBasketIdent = null;
00029 
00033     public function __construct()
00034     {
00035         parent::__construct();
00036         $this->init( 'oxdiscount' );
00037     }
00038 
00046     public function delete( $sOXID = null )
00047     {
00048         if ( !$sOXID ) {
00049             $sOXID = $this->getId();
00050         }
00051         if ( !$sOXID ) {
00052             return false;
00053         }
00054 
00055 
00056         $oDB = oxDb::getDb();
00057         $oDB->execute( 'delete from oxobject2discount where oxobject2discount.oxdiscountid = "'.$sOXID.'"' );
00058 
00059         return parent::delete( $sOXID );
00060     }
00061 
00069     public function isForArticle( $oArticle )
00070     {
00071         // item discounts may only be applied for basket
00072         if ( $this->oxdiscount__oxaddsumtype->value == 'itm' ) {
00073             return false;
00074         }
00075 
00076         if ( $this->oxdiscount__oxamount->value || $this->oxdiscount__oxprice->value ) {
00077             return false;
00078         }
00079 
00080         $myDB = oxDb::getDb();
00081 
00082         //check for global discount (no articles, no categories)
00083         $sQ = "select 1 from oxobject2discount where oxdiscountid = '{$this->oxdiscount__oxid->value}' and ( oxtype = 'oxarticles' or oxtype = 'oxcategories')";
00084         $blOk = (bool) $myDB->getOne( $sQ );
00085         
00086         if ( !$blOk ) {
00087             return true;
00088         }
00089 
00090         // check if this article is assigned
00091         $sArticleID = "";
00092         if ($oArticle->oxarticles__oxparentid->value) {
00093             $sArticleID = "(oxobjectid = '".$oArticle->getId()."' or oxobjectid = '{$oArticle->oxarticles__oxparentid->value}')";
00094         } else {
00095             $sArticleID = "oxobjectid = '".$oArticle->getId()."'";
00096         }
00097 
00098         $sQ = "select 1 from oxobject2discount where oxdiscountid = '{$this->oxdiscount__oxid->value}' and $sArticleID and oxtype = 'oxarticles'";
00099         $blOk = (bool) $myDB->getOne( $sQ );
00100         if ( $blOk ) {
00101             return true;
00102         } else {
00103             // check if article is in some assigned category
00104             $aCatIds = $oArticle->getCategoryIds();
00105             if (!$aCatIds || !count($aCatIds)) {
00106                 // no categories are set for article, so no discounts from categories..
00107                 return false;
00108             }
00109             $sCatIds = "('".implode("','", $aCatIds)."')";
00110             // getOne appends limit 1, so this one should be fast enough
00111             $sQ = "select oxobjectid from oxobject2discount where oxdiscountid = '{$this->oxdiscount__oxid->value}' and oxobjectid in $sCatIds and oxtype = 'oxcategories'";
00112             if ( ( bool ) $myDB->getOne( $sQ ) ) {
00113                 return true;
00114             }
00115         }
00116         return false;
00117     }
00118 
00126     public function isForBasketItem( $oArticle )
00127     {
00128         if ( $this->oxdiscount__oxamount->value == 0 && $this->oxdiscount__oxprice->value == 0 ) {
00129             return false;
00130         }
00131 
00132         // skipping bundle discounts
00133         if ( $this->oxdiscount__oxaddsumtype->value == 'itm' ) {
00134             return false;
00135         }
00136 
00137         $myDB = oxDb::getDb();
00138         // check if this article is assigned
00139         $sArticleID = "";
00140         if ( $oArticle->oxarticles__oxparentid->value ) {
00141             $sArticleID = "(oxobjectid = '".$oArticle->getId()."' or oxobjectid = '{$oArticle->oxarticles__oxparentid->value}')";
00142         } else {
00143             $sArticleID = "oxobjectid = '".$oArticle->getId()."'";
00144         }
00145         $sQ = "select 1 from oxobject2discount where oxdiscountid = '{$this->oxdiscount__oxid->value}' and $sArticleID and oxtype = 'oxarticles'";
00146         if ( !( $blOk = ( bool ) $myDB->getOne( $sQ ) ) ) {
00147 
00148             // checkin article cateogry
00149             $blOk = $this->_checkForArticleCategories( $oArticle );
00150         }
00151         return $blOk;
00152     }
00153 
00161     public function isForBasketAmount( $oBasket )
00162     {
00163         $dAmount = 0;
00164         $oBasketInfo  = $oBasket->getBasketSummary();
00165         $aBasketItems = $oBasket->getContents();
00166         foreach ( $aBasketItems as $oBasketItem ) {
00167 
00168             $oBasketArticle = $oBasketItem->getArticle();
00169 
00170             $blForBasketItem = false;
00171             if ( $this->oxdiscount__oxaddsumtype->value != 'itm' ) {
00172                 $blForBasketItem = $this->isForBasketItem( $oBasketArticle );
00173             } else {
00174                 $blForBasketItem = $this->isForBundleItem( $oBasketArticle );
00175             }
00176 
00177             if ( $blForBasketItem ) {
00178                 if ( $this->oxdiscount__oxprice->value ) {
00179                     $dAmount += $oBasketArticle->getPrice()->getBruttoPrice() * $oBasketInfo->aArticles[$oBasketArticle->getId()];
00180                 } elseif ( $this->oxdiscount__oxamount->value ) {
00181                     $dAmount += $oBasketInfo->aArticles[$oBasketArticle->getId()];
00182                 }
00183             }
00184         }
00185 
00186         return $this->isForAmount( $dAmount );
00187     }
00188 
00196     public function isForAmount( $dAmount )
00197     {
00198         $blIs = true;
00199         if ( $this->oxdiscount__oxprice->value &&
00200             ( $dAmount < $this->oxdiscount__oxprice->value || $dAmount > $this->oxdiscount__oxpriceto->value ) ) {
00201             $blIs = false;
00202         } elseif ( $this->oxdiscount__oxamount->value &&
00203             ( $dAmount < $this->oxdiscount__oxamount->value || $dAmount > $this->oxdiscount__oxamountto->value ) ) {
00204             $blIs = false;
00205         }
00206         return $blIs;
00207     }
00208 
00216     public function isForBasket( $oBasket )
00217     {
00218         // initial configuration check
00219         if ( $this->oxdiscount__oxamount->value == 0 && $this->oxdiscount__oxprice->value == 0 ) {
00220             return false;
00221         }
00222 
00223         $oSummary = $oBasket->getBasketSummary();
00224         // amounts check
00225         if ( $this->oxdiscount__oxamount->value && ( $oSummary->iArticleCount < $this->oxdiscount__oxamount->value || $oSummary->iArticleCount > $this->oxdiscount__oxamountto->value ) ) {
00226             return false;
00227             // price check
00228         } elseif ( $this->oxdiscount__oxprice->value && ( $oSummary->dArticlePrice < $this->oxdiscount__oxprice->value || $oSummary->dArticlePrice > $this->oxdiscount__oxpriceto->value ) ) {
00229             return false;
00230         }
00231 
00232         // oxobject2discount configuration check
00233         $sQ = 'select 1 from oxobject2discount where oxdiscountid = "'.$this->oxdiscount__oxid->value.'" and oxtype in ("oxarticles", "oxcategories" ) ';
00234 
00235         return !( (bool) oxDb::getDb()->getOne( $sQ ) );
00236     }
00237 
00245     public function isForBundleItem( $oArticle )
00246     {
00247         if ( $this->oxdiscount__oxaddsumtype->value != 'itm' ) {
00248             return false;
00249         }
00250 
00251         $sSelect = "select 1 from oxobject2discount where oxdiscountid='".$this->getId()."' and oxobjectid='".$oArticle->getId()."' ";
00252         if ( !( $blOk = (bool) oxDb::getDb()->getOne( $sSelect ) ) ) {
00253             // additional checks for amounts and other dependencies
00254             $blOk = $this->_checkForArticleCategories( $oArticle );
00255         }
00256         return $blOk;
00257     }
00258 
00266     public function isForBundleBasket( $oBasket )
00267     {
00268         if ( $this->oxdiscount__oxaddsumtype->value != 'itm' ) {
00269             return false;
00270         }
00271 
00272         return $this->isForBasket( $oBasket );
00273     }
00274 
00283     public function getAbsValue( $dPrice, $dAmount = 1 )
00284     {
00285         if ( $this->oxdiscount__oxaddsumtype->value == '%' ) {
00286             return $dPrice * ( $this->oxdiscount__oxaddsum->value / 100 );
00287         } else {
00288             $oCur = $this->getConfig()->getActShopCurrencyObject();
00289             return $this->oxdiscount__oxaddsum->value * $dAmount * $oCur->rate;
00290         }
00291     }
00292 
00301     public function applyDiscount( $oPrice, $dAmount = 1 )
00302     {
00303         if ( $this->oxdiscount__oxaddsumtype->value == 'abs' ) {
00304 
00305             $oCur = $this->getConfig()->getActShopCurrencyObject();
00306 
00307             $oDiscountPrice = oxNew( 'oxprice' );
00308             $oDiscountPrice->setBruttoPriceMode();
00309             $oDiscountPrice->setPrice( $this->oxdiscount__oxaddsum->value * $oCur->rate, $oPrice->getVat() );
00310         } else {
00311 
00312             $oDiscountPrice = oxNew( 'oxprice' );
00313             $oDiscountPrice->setBruttoPriceMode();
00314             $oDiscountPrice->setPrice( $oPrice->getBruttoPrice() / 100 * $this->oxdiscount__oxaddsum->value, $oPrice->getVat() );
00315         }
00316 
00317         $oDiscountPrice->multiply( $dAmount * -1 );
00318         $oPrice->addPrice( $oDiscountPrice );
00319     }
00320 
00328     public function getBundleAmount( $dAmount )
00329     {
00330         $dItemAmount = $this->oxdiscount__oxitmamount->value;
00331 
00332         // Multiplying bundled articles count, if allowed
00333         if ( $this->oxdiscount__oxitmmultiple->value && $this->oxdiscount__oxamount->value > 0 ) {
00334             $dItemAmount = floor( $dAmount / $this->oxdiscount__oxamount->value ) * $this->oxdiscount__oxitmamount->value;
00335         }
00336 
00337         return $dItemAmount;
00338     }
00339 
00347     protected function _checkForArticleCategories( $oArticle )
00348     {
00349         // check if article is in some assigned category
00350         $aCatIds = $oArticle->getCategoryIds();
00351         if (!$aCatIds || !count($aCatIds)) {
00352             // no categories are set for article, so no discounts from categories..
00353             return false;
00354         }
00355 
00356         $sCatIds = "('".implode("','", $aCatIds)."')";
00357         // getOne appends limit 1, so this one should be fast enough
00358         $sQ = "select oxobjectid from oxobject2discount where oxdiscountid = '{$this->oxdiscount__oxid->value}' and oxobjectid in $sCatIds and oxtype = 'oxcategories'";
00359 
00360         return oxDb::getDb()->getOne( $sQ );
00361     }
00362 
00368     public function getSimpleDiscount()
00369     {
00370         $oDiscount = new OxStdClass();
00371         $oDiscount->sOXID     = $this->getId();
00372         $oDiscount->sDiscount = $this->oxdiscount__oxtitle->value;
00373         $oDiscount->sType     = $this->oxdiscount__oxaddsumtype->value;
00374 
00375         return $oDiscount;
00376     }
00377 
00378 }

Generated on Thu Dec 4 12:04:56 2008 for OXID eShop CE by  doxygen 1.5.5