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 = "'.$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         //check for global discount (no articles, no categories)
00086         $sQ = "select 1 from oxobject2discount where oxdiscountid = '{$this->oxdiscount__oxid->value}' and ( oxtype = 'oxarticles' or oxtype = 'oxcategories')";
00087         $blOk = (bool) $myDB->getOne( $sQ );
00088 
00089         if ( !$blOk ) {
00090             return true;
00091         }
00092 
00093         // check if this article is assigned
00094         $sArticleID = "";
00095         if ($oArticle->oxarticles__oxparentid->value) {
00096             $sArticleID = "(oxobjectid = '".$oArticle->getId()."' or oxobjectid = '{$oArticle->oxarticles__oxparentid->value}')";
00097         } else {
00098             $sArticleID = "oxobjectid = '".$oArticle->getId()."'";
00099         }
00100 
00101         $sQ = "select 1 from oxobject2discount where oxdiscountid = '{$this->oxdiscount__oxid->value}' and $sArticleID and oxtype = 'oxarticles'";
00102         $blOk = (bool) $myDB->getOne( $sQ );
00103         if ( $blOk ) {
00104             return true;
00105         } else {
00106             // check if article is in some assigned category
00107             $aCatIds = $oArticle->getCategoryIds();
00108             if (!$aCatIds || !count($aCatIds)) {
00109                 // no categories are set for article, so no discounts from categories..
00110                 return false;
00111             }
00112             $sCatIds = "('".implode("','", $aCatIds)."')";
00113             // getOne appends limit 1, so this one should be fast enough
00114             $sQ = "select oxobjectid from oxobject2discount where oxdiscountid = '{$this->oxdiscount__oxid->value}' and oxobjectid in $sCatIds and oxtype = 'oxcategories'";
00115             if ( ( bool ) $myDB->getOne( $sQ ) ) {
00116                 return true;
00117             }
00118         }
00119         return false;
00120     }
00121 
00129     public function isForBasketItem( $oArticle )
00130     {
00131         if ( $this->oxdiscount__oxamount->value == 0 && $this->oxdiscount__oxprice->value == 0 ) {
00132             return false;
00133         }
00134 
00135         // skipping bundle discounts
00136         if ( $this->oxdiscount__oxaddsumtype->value == 'itm' ) {
00137             return false;
00138         }
00139 
00140         $myDB = oxDb::getDb();
00141         // check if this article is assigned
00142         $sArticleID = "";
00143         if ( $oArticle->oxarticles__oxparentid->value ) {
00144             $sArticleID = "(oxobjectid = '".$oArticle->getId()."' or oxobjectid = '{$oArticle->oxarticles__oxparentid->value}')";
00145         } else {
00146             $sArticleID = "oxobjectid = '".$oArticle->getId()."'";
00147         }
00148         $sQ = "select 1 from oxobject2discount where oxdiscountid = '{$this->oxdiscount__oxid->value}' and $sArticleID and oxtype = 'oxarticles'";
00149         if ( !( $blOk = ( bool ) $myDB->getOne( $sQ ) ) ) {
00150 
00151             // checkin article cateogry
00152             $blOk = $this->_checkForArticleCategories( $oArticle );
00153         }
00154         return $blOk;
00155     }
00156 
00164     public function isForBasketAmount( $oBasket )
00165     {
00166         $dAmount = 0;
00167         $aBasketItems = $oBasket->getContents();
00168         foreach ( $aBasketItems as $oBasketItem ) {
00169 
00170             $oBasketArticle = $oBasketItem->getArticle();
00171 
00172             $blForBasketItem = false;
00173             if ( $this->oxdiscount__oxaddsumtype->value != 'itm' ) {
00174                 $blForBasketItem = $this->isForBasketItem( $oBasketArticle );
00175             } else {
00176                 $blForBasketItem = $this->isForBundleItem( $oBasketArticle );
00177             }
00178 
00179             if ( $blForBasketItem ) {
00180                 if ( $this->oxdiscount__oxprice->value ) {
00181                     $dAmount += $oBasketArticle->getPrice()->getBruttoPrice() * $oBasketItem->getAmount();
00182                 } elseif ( $this->oxdiscount__oxamount->value ) {
00183                     $dAmount += $oBasketItem->getAmount();
00184                 }
00185             }
00186         }
00187 
00188         return $this->isForAmount( $dAmount );
00189     }
00190 
00198     public function isForAmount( $dAmount )
00199     {
00200         $blIs = true;
00201         if ( $this->oxdiscount__oxprice->value &&
00202             ( $dAmount < $this->oxdiscount__oxprice->value || $dAmount > $this->oxdiscount__oxpriceto->value ) ) {
00203             $blIs = false;
00204         } elseif ( $this->oxdiscount__oxamount->value &&
00205             ( $dAmount < $this->oxdiscount__oxamount->value || $dAmount > $this->oxdiscount__oxamountto->value ) ) {
00206             $blIs = false;
00207         }
00208         return $blIs;
00209     }
00210 
00218     public function isForBasket( $oBasket )
00219     {
00220         // initial configuration check
00221         if ( $this->oxdiscount__oxamount->value == 0 && $this->oxdiscount__oxprice->value == 0 ) {
00222             return false;
00223         }
00224 
00225         $oSummary = $oBasket->getBasketSummary();
00226         // amounts check
00227         if ( $this->oxdiscount__oxamount->value && ( $oSummary->iArticleCount < $this->oxdiscount__oxamount->value || $oSummary->iArticleCount > $this->oxdiscount__oxamountto->value ) ) {
00228             return false;
00229             // price check
00230         } elseif ( $this->oxdiscount__oxprice->value && ( $oSummary->dArticlePrice < $this->oxdiscount__oxprice->value || $oSummary->dArticlePrice > $this->oxdiscount__oxpriceto->value ) ) {
00231             return false;
00232         }
00233 
00234         // oxobject2discount configuration check
00235         $sQ = 'select 1 from oxobject2discount where oxdiscountid = "'.$this->oxdiscount__oxid->value.'" and oxtype in ("oxarticles", "oxcategories" ) ';
00236 
00237         return !( (bool) oxDb::getDb()->getOne( $sQ ) );
00238     }
00239 
00247     public function isForBundleItem( $oArticle )
00248     {
00249         if ( $this->oxdiscount__oxaddsumtype->value != 'itm' ) {
00250             return false;
00251         }
00252 
00253         $sSelect = "select 1 from oxobject2discount where oxdiscountid='".$this->getId()."' and oxobjectid='".$oArticle->getId()."' ";
00254         if ( !( $blOk = (bool) oxDb::getDb()->getOne( $sSelect ) ) ) {
00255             // additional checks for amounts and other dependencies
00256             $blOk = $this->_checkForArticleCategories( $oArticle );
00257         }
00258         return $blOk;
00259     }
00260 
00268     public function isForBundleBasket( $oBasket )
00269     {
00270         if ( $this->oxdiscount__oxaddsumtype->value != 'itm' ) {
00271             return false;
00272         }
00273 
00274         return $this->isForBasket( $oBasket );
00275     }
00276 
00285     public function getAbsValue( $dPrice, $dAmount = 1 )
00286     {
00287         if ( $this->oxdiscount__oxaddsumtype->value == '%' ) {
00288             return $dPrice * ( $this->oxdiscount__oxaddsum->value / 100 );
00289         } else {
00290             $oCur = $this->getConfig()->getActShopCurrencyObject();
00291             return $this->oxdiscount__oxaddsum->value * $dAmount * $oCur->rate;
00292         }
00293     }
00294 
00303     public function applyDiscount( $oPrice, $dAmount = 1 )
00304     {
00305         if ( $this->oxdiscount__oxaddsumtype->value == 'abs' ) {
00306 
00307             $oCur = $this->getConfig()->getActShopCurrencyObject();
00308 
00309             $oDiscountPrice = oxNew( 'oxprice' );
00310             $oDiscountPrice->setBruttoPriceMode();
00311             $oDiscountPrice->setPrice( $this->oxdiscount__oxaddsum->value * $oCur->rate, $oPrice->getVat() );
00312         } else {
00313 
00314             $oDiscountPrice = oxNew( 'oxprice' );
00315             $oDiscountPrice->setBruttoPriceMode();
00316             $oDiscountPrice->setPrice( $oPrice->getBruttoPrice() / 100 * $this->oxdiscount__oxaddsum->value, $oPrice->getVat() );
00317         }
00318 
00319         $oDiscountPrice->multiply( $dAmount * -1 );
00320         $oPrice->addPrice( $oDiscountPrice );
00321     }
00322 
00330     public function getBundleAmount( $dAmount )
00331     {
00332         $dItemAmount = $this->oxdiscount__oxitmamount->value;
00333 
00334         // Multiplying bundled articles count, if allowed
00335         if ( $this->oxdiscount__oxitmmultiple->value && $this->oxdiscount__oxamount->value > 0 ) {
00336             $dItemAmount = floor( $dAmount / $this->oxdiscount__oxamount->value ) * $this->oxdiscount__oxitmamount->value;
00337         }
00338 
00339         return $dItemAmount;
00340     }
00341 
00349     protected function _checkForArticleCategories( $oArticle )
00350     {
00351         // check if article is in some assigned category
00352         $aCatIds = $oArticle->getCategoryIds();
00353         if (!$aCatIds || !count($aCatIds)) {
00354             // no categories are set for article, so no discounts from categories..
00355             return false;
00356         }
00357 
00358         $sCatIds = "('".implode("','", $aCatIds)."')";
00359         // getOne appends limit 1, so this one should be fast enough
00360         $sQ = "select oxobjectid from oxobject2discount where oxdiscountid = '{$this->oxdiscount__oxid->value}' and oxobjectid in $sCatIds and oxtype = 'oxcategories'";
00361 
00362         return oxDb::getDb()->getOne( $sQ );
00363     }
00364 
00370     public function getSimpleDiscount()
00371     {
00372         $oDiscount = new OxStdClass();
00373         $oDiscount->sOXID     = $this->getId();
00374         $oDiscount->sDiscount = $this->oxdiscount__oxtitle->value;
00375         $oDiscount->sType     = $this->oxdiscount__oxaddsumtype->value;
00376 
00377         return $oDiscount;
00378     }
00379 
00380 }

Generated on Wed May 13 13:25:51 2009 for OXID eShop CE by  doxygen 1.5.5