oxdiscountlist.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxDiscountList extends oxList
00008 {
00014     static protected $_instance = null;
00015 
00021     protected $_sUserId = null;
00022 
00028     protected $_blReload = true;
00029 
00030 
00036     protected $_hasSkipDiscountCategories = null;
00037 
00043     public function __construct( $sObjectsInListName = 'oxdiscount' )
00044     {
00045         parent::__construct( 'oxdiscount' );
00046     }
00047 
00053     static public function getInstance()
00054     {
00055         // disable cashing for test modules
00056         if ( defined( 'OXID_PHP_UNIT' ) ) {
00057             self::$_instance = modInstances::getMod( __CLASS__ );
00058         }
00059 
00060         if ( !isset( self::$_instance ) ) {
00061             // allow modules
00062             self::$_instance = oxNew( 'oxDiscountList' );
00063 
00064             if ( defined( 'OXID_PHP_UNIT' ) ) {
00065                 modInstances::addMod( __CLASS__, self::$_instance);
00066             }
00067         }
00068 
00069         return self::$_instance;
00070     }
00071 
00079     protected function _getList( $oUser = null )
00080     {
00081         $sUserId = $oUser?$oUser->getId():'';
00082 
00083         if ( $this->_blReload || $sUserId !== $this->_sUserId ) {
00084             // loading list
00085             $this->selectString( $this->_getFilterSelect( $oUser ) );
00086 
00087             // setting list proterties
00088             $this->_blReload = false;    // reload marker
00089             $this->_sUserId  = $sUserId; // discount list user id
00090         }
00091 
00092         // resetting array pointer
00093         $this->rewind();
00094 
00095         return $this;
00096     }
00097 
00105     public function getCountryId( $oUser )
00106     {
00107         $sCountryId = null;
00108         if ( $oUser ) {
00109             $sCountryId = $oUser->getActiveCountry();
00110         }
00111 
00112         return $sCountryId;
00113     }
00114 
00120     public function forceReload()
00121     {
00122         $this->_blReload = true;
00123     }
00124 
00132     protected function _getFilterSelect( $oUser )
00133     {
00134         $oBaseObject = $this->getBaseObject();
00135 
00136         $sTable = $oBaseObject->getViewName();
00137         $sQ  = "select ".$oBaseObject->getSelectFields()." from $sTable ";
00138         $sQ .= "where ".$oBaseObject->getSqlActiveSnippet().' ';
00139 
00140 
00141         // defining initial filter parameters
00142         $sUserId    = null;
00143         $sGroupIds  = null;
00144         $sCountryId = $this->getCountryId( $oUser );
00145         $oDb = oxDb::getDb();
00146 
00147         // checking for current session user which gives additional restrictions for user itself, users group and country
00148         if ( $oUser ) {
00149 
00150             // user ID
00151             $sUserId = $oUser->getId();
00152 
00153             // user group ids
00154             foreach ( $oUser->getUserGroups() as $oGroup ) {
00155                 if ( $sGroupIds ) {
00156                     $sGroupIds .= ', ';
00157                 }
00158                 $sGroupIds .= $oDb->quote( $oGroup->getId() );
00159             }
00160         }
00161 
00162         $sUserTable    = getViewName( 'oxuser' );
00163         $sGroupTable   = getViewName( 'oxgroups' );
00164         $sCountryTable = getViewName( 'oxcountry' );
00165 
00166         $sCountrySql = $sCountryId?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' and oxobject2discount.OXOBJECTID=".$oDb->quote( $sCountryId ).")":'0';
00167         $sUserSql    = $sUserId   ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' and oxobject2discount.OXOBJECTID=".$oDb->quote( $sUserId ). ")":'0';
00168         $sGroupSql   = $sGroupIds ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' and oxobject2discount.OXOBJECTID in ($sGroupIds) )":'0';
00169 
00170         $sQ .= "and (
00171             select
00172                 if(EXISTS(select 1 from oxobject2discount, $sCountryTable where $sCountryTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' LIMIT 1),
00173                         $sCountrySql,
00174                         1) &&
00175                 if(EXISTS(select 1 from oxobject2discount, $sUserTable where $sUserTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' LIMIT 1),
00176                         $sUserSql,
00177                         1) &&
00178                 if(EXISTS(select 1 from oxobject2discount, $sGroupTable where $sGroupTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' LIMIT 1),
00179                         $sGroupSql,
00180                         1)
00181             )";
00182 
00183         return $sQ;
00184     }
00185 
00194     public function getArticleDiscounts( $oArticle, $oUser = null )
00195     {
00196         $aList = array();
00197         foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00198             if ( $oDiscount->isForArticle( $oArticle ) ) {
00199                 $aList[$oDiscount->getId()] = $oDiscount;
00200             }
00201         }
00202 
00203         return $aList;
00204     }
00205 
00215     public function getBasketItemDiscounts( $oArticle, $oBasket, $oUser = null )
00216     {
00217         $aList = array();
00218         $aDiscounts = $this->_getList( $oUser )->getArray();
00219         reset($aDiscounts);
00220         foreach ( $aDiscounts as $oDiscount ) {
00221             if ( $oDiscount->isForBasketItem( $oArticle ) && $oDiscount->isForBasketAmount( $oBasket ) ) {
00222                 $aList[$oDiscount->getId()] = $oDiscount;
00223             }
00224         }
00225 
00226         return $aList;
00227     }
00228 
00237     public function getBasketDiscounts( $oBasket, $oUser = null )
00238     {
00239         $aList = array();
00240         $oList = $this->_getList( $oUser );
00241         foreach ( $oList->getArray() as $oDiscount ) {
00242             if ( $oDiscount->isForBasket( $oBasket ) ) {
00243                 $aList[$oDiscount->getId()] = $oDiscount;
00244             }
00245         }
00246 
00247         return $aList;
00248     }
00249 
00259     public function getBasketItemBundleDiscounts( $oArticle, $oBasket, $oUser = null )
00260     {
00261         $aList = array();
00262         foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00263             if ( $oDiscount->isForBundleItem( $oArticle, $oBasket ) && $oDiscount->isForBasketAmount($oBasket) ) {
00264                 $aList[$oDiscount->getId()] = $oDiscount;
00265             }
00266         }
00267 
00268         return $aList;
00269     }
00270 
00279     public function getBasketBundleDiscounts( $oBasket, $oUser = null )
00280     {
00281         $aList = array();
00282         $aDiscList = $this->_getList( $oUser )->getArray();
00283         foreach ( $aDiscList as $oDiscount ) {
00284             if ( $oDiscount->isForBundleBasket( $oBasket ) ) {
00285                 $aList[$oDiscount->getId()] = $oDiscount;
00286             }
00287         }
00288 
00289         return $aList;
00290     }
00291 
00300     public function applyDiscounts( $oPrice, $aDiscounts )
00301     {
00302         reset( $aDiscounts );
00303         while ( list( , $oDiscount ) = each( $aDiscounts ) ) {
00304             $oDiscount->applyDiscount( $oPrice );
00305         }
00306     }
00307 
00318     public function applyBasketDiscounts( oxPrice $oPrice, $aDiscounts, $dAmount = 1 )
00319     {
00320         $aDiscLog = array();
00321         reset( $aDiscounts );
00322         //#3587
00323         $oPrice->multiply($dAmount);
00324 
00325         // price object to correctly perform calculations
00326         $dOldPrice = $oPrice->getBruttoPrice();
00327 
00328         while (list( , $oDiscount ) = each( $aDiscounts ) ) {
00329             //#3587
00330             $oDiscount->applyDiscount( $oPrice, $dAmount );
00331             $dNewPrice = $oPrice->getBruttoPrice();
00332 
00333             if ( !isset( $aDiscLog[$oDiscount->getId()] ) ) {
00334                 $aDiscLog[$oDiscount->getId()] = $oDiscount->getSimpleDiscount();
00335             }
00336 
00337             $aDiscLog[$oDiscount->getId()]->dDiscount += $dOldPrice - $dNewPrice;
00339             //$aDiscLog[$oDiscount->getId()]->dDiscount *= $dAmount;
00340             $dOldPrice = $dNewPrice;
00341         }
00343         $oPrice->divide($dAmount);
00344 
00345         return $aDiscLog;
00346     }       /*
00357     public function applyBasketDiscounts( oxPrice $oPrice, $aDiscounts, $dAmount = 1 )
00358     {
00359         $aDiscLog = array();
00360         reset( $aDiscounts );
00361 
00362         //$oPriceCopy = clone $oPrice;
00363 
00364         $oPrice->multiply($dAmount);
00365 
00366         // price object to correctly perform calculations
00367         $dOldPrice = $oPrice->getBruttoPrice();
00368 
00369         while (list( , $oDiscount ) = each( $aDiscounts ) ) {
00370 
00371             $oDiscount->applyDiscount( $oPrice );
00372             $dNewPrice = $oPrice->getBruttoPrice();
00373 
00374             if ( !isset( $aDiscLog[$oDiscount->getId()] ) ) {
00375                 $aDiscLog[$oDiscount->getId()] = $oDiscount->getSimpleDiscount();
00376             }
00377 
00378             $aDiscLog[$oDiscount->getId()]->dDiscount += $dOldPrice - $dNewPrice;
00379             //$aDiscLog[$oDiscount->getId()]->dDiscount *= $dAmount;
00380             $dOldPrice = $dNewPrice;
00381         }
00382 
00383         //setting the correct $oPrice value
00384         $oPrice->divide($dAmount);
00385 
00386         //$oPrice = clone $oPriceCopy;
00387 
00388         return $aDiscLog;
00389     }        */
00390 
00396     public function hasSkipDiscountCategories()
00397     {
00398         if ( $this->_hasSkipDiscountCategories === null  || $this->_blReload ) {
00399             $sViewName = getViewName( 'oxcategories' );
00400             $sQ = "select 1 from {$sViewName} where {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
00401 
00402             $this->_hasSkipDiscountCategories = (bool) oxDb::getDb()->getOne( $sQ );
00403         }
00404         return $this->_hasSkipDiscountCategories;
00405     }
00406 }