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 ( $sParentId = $oArticle->getProductParentId() ) {
00096             $sArticleId = "(oxobjectid = '".$oArticle->getProductId()."' or oxobjectid = '{$sParentId}')";
00097         } else {
00098             $sArticleId = "oxobjectid = '".$oArticle->getProductId()."'";
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 ( $sParentId = $oArticle->getProductParentId() ) {
00144             $sArticleId = "(oxobjectid = '".$oArticle->getProductId()."' or oxobjectid = '{$sParentId}')";
00145         } else {
00146             $sArticleId = "oxobjectid = '".$oArticle->getProductId()."'";
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 
00155         return $blOk;
00156     }
00157 
00165     public function isForBasketAmount( $oBasket )
00166     {
00167         $dAmount = 0;
00168         $aBasketItems = $oBasket->getContents();
00169         foreach ( $aBasketItems as $oBasketItem ) {
00170 
00171             $oBasketArticle = $oBasketItem->getArticle();
00172 
00173             $blForBasketItem = false;
00174             if ( $this->oxdiscount__oxaddsumtype->value != 'itm' ) {
00175                 $blForBasketItem = $this->isForBasketItem( $oBasketArticle );
00176             } else {
00177                 $blForBasketItem = $this->isForBundleItem( $oBasketArticle );
00178             }
00179 
00180             if ( $blForBasketItem ) {
00181                 if ( $this->oxdiscount__oxprice->value ) {
00182                     $dAmount += $oBasketArticle->getPrice()->getBruttoPrice() * $oBasketItem->getAmount();
00183                 } elseif ( $this->oxdiscount__oxamount->value ) {
00184                     $dAmount += $oBasketItem->getAmount();
00185                 }
00186             }
00187         }
00188 
00189         return $this->isForAmount( $dAmount );
00190     }
00191 
00199     public function isForAmount( $dAmount )
00200     {
00201         $blIs = true;
00202         if ( $this->oxdiscount__oxprice->value &&
00203             ( $dAmount < $this->oxdiscount__oxprice->value || $dAmount > $this->oxdiscount__oxpriceto->value ) ) {
00204             $blIs = false;
00205         } elseif ( $this->oxdiscount__oxamount->value &&
00206             ( $dAmount < $this->oxdiscount__oxamount->value || $dAmount > $this->oxdiscount__oxamountto->value ) ) {
00207             $blIs = false;
00208         }
00209         return $blIs;
00210     }
00211 
00219     public function isForBasket( $oBasket )
00220     {
00221         // initial configuration check
00222         if ( $this->oxdiscount__oxamount->value == 0 && $this->oxdiscount__oxprice->value == 0 ) {
00223             return false;
00224         }
00225 
00226         $oSummary = $oBasket->getBasketSummary();
00227         // amounts check
00228         if ( $this->oxdiscount__oxamount->value && ( $oSummary->iArticleCount < $this->oxdiscount__oxamount->value || $oSummary->iArticleCount > $this->oxdiscount__oxamountto->value ) ) {
00229             return false;
00230             // price check
00231         } elseif ($this->oxdiscount__oxprice->value) {
00232             $dRate = $this->getConfig()->getActShopCurrencyObject()->rate;
00233             if ( $oSummary->dArticlePrice < $this->oxdiscount__oxprice->value*$dRate || $oSummary->dArticlePrice > $this->oxdiscount__oxpriceto->value*$dRate ) {
00234                 return false;
00235             }
00236         }
00237 
00238         // oxobject2discount configuration check
00239         $sQ = 'select 1 from oxobject2discount where oxdiscountid = "'.$this->oxdiscount__oxid->value.'" and oxtype in ("oxarticles", "oxcategories" ) ';
00240 
00241         return !( (bool) oxDb::getDb()->getOne( $sQ ) );
00242     }
00243 
00251     public function isForBundleItem( $oArticle )
00252     {
00253         if ( $this->oxdiscount__oxaddsumtype->value != 'itm' ) {
00254             return false;
00255         }
00256 
00257         $sSelect = "select 1 from oxobject2discount where oxdiscountid='".$this->getId()."' and oxobjectid='".$oArticle->getProductId()."' ";
00258         if ( !( $blOk = (bool) oxDb::getDb()->getOne( $sSelect ) ) ) {
00259             // additional checks for amounts and other dependencies
00260             $blOk = $this->_checkForArticleCategories( $oArticle );
00261         }
00262         return $blOk;
00263     }
00264 
00272     public function isForBundleBasket( $oBasket )
00273     {
00274         if ( $this->oxdiscount__oxaddsumtype->value != 'itm' ) {
00275             return false;
00276         }
00277 
00278         return $this->isForBasket( $oBasket );
00279     }
00280 
00289     public function getAbsValue( $dPrice, $dAmount = 1 )
00290     {
00291         if ( $this->oxdiscount__oxaddsumtype->value == '%' ) {
00292             return $dPrice * ( $this->oxdiscount__oxaddsum->value / 100 );
00293         } else {
00294             $oCur = $this->getConfig()->getActShopCurrencyObject();
00295             return $this->oxdiscount__oxaddsum->value * $dAmount * $oCur->rate;
00296         }
00297     }
00298 
00307     public function applyDiscount( $oPrice, $dAmount = 1 )
00308     {
00309         if ( $this->oxdiscount__oxaddsumtype->value == 'abs' ) {
00310 
00311             $oCur = $this->getConfig()->getActShopCurrencyObject();
00312 
00313             $oDiscountPrice = oxNew( 'oxprice' );
00314             $oDiscountPrice->setBruttoPriceMode();
00315             $oDiscountPrice->setPrice( $this->oxdiscount__oxaddsum->value * $oCur->rate, $oPrice->getVat() );
00316         } else {
00317 
00318             $oDiscountPrice = oxNew( 'oxprice' );
00319             $oDiscountPrice->setBruttoPriceMode();
00320             $oDiscountPrice->setPrice( $oPrice->getBruttoPrice() / 100 * $this->oxdiscount__oxaddsum->value, $oPrice->getVat() );
00321         }
00322 
00323         $oDiscountPrice->multiply( $dAmount * -1 );
00324         $oPrice->addPrice( $oDiscountPrice );
00325     }
00326 
00334     public function getBundleAmount( $dAmount )
00335     {
00336         $dItemAmount = $this->oxdiscount__oxitmamount->value;
00337 
00338         // Multiplying bundled articles count, if allowed
00339         if ( $this->oxdiscount__oxitmmultiple->value && $this->oxdiscount__oxamount->value > 0 ) {
00340             $dItemAmount = floor( $dAmount / $this->oxdiscount__oxamount->value ) * $this->oxdiscount__oxitmamount->value;
00341         }
00342 
00343         return $dItemAmount;
00344     }
00345 
00353     protected function _checkForArticleCategories( $oArticle )
00354     {
00355         // check if article is in some assigned category
00356         $aCatIds = $oArticle->getCategoryIds();
00357         if (!$aCatIds || !count($aCatIds)) {
00358             // no categories are set for article, so no discounts from categories..
00359             return false;
00360         }
00361 
00362         $sCatIds = "('".implode("','", $aCatIds)."')";
00363         // getOne appends limit 1, so this one should be fast enough
00364         $sQ = "select oxobjectid from oxobject2discount where oxdiscountid = '{$this->oxdiscount__oxid->value}' and oxobjectid in $sCatIds and oxtype = 'oxcategories'";
00365 
00366         return oxDb::getDb()->getOne( $sQ );
00367     }
00368 
00374     public function getSimpleDiscount()
00375     {
00376         $oDiscount = new OxStdClass();
00377         $oDiscount->sOXID     = $this->getId();
00378         $oDiscount->sDiscount = $this->oxdiscount__oxtitle->value;
00379         $oDiscount->sType     = $this->oxdiscount__oxaddsumtype->value;
00380 
00381         return $oDiscount;
00382     }
00383 
00384 }

Generated on Tue Aug 18 09:21:05 2009 for OXID eShop CE by  doxygen 1.5.5