oxdiscountlist.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxDiscountList extends oxList
00009 {
00015     static protected $_instance = null;
00016 
00022     protected $_sUserId = null;
00023 
00029     protected $_blReload = true;
00030 
00031 
00037     protected $_hasSkipDiscountCategories = null;
00038 
00044     public function __construct( $sObjectsInListName = 'oxdiscount' )
00045     {
00046         parent::__construct( 'oxdiscount' );
00047     }
00048 
00056     static public function getInstance()
00057     {
00058         return oxRegistry::get("oxDiscountList");
00059     }
00060 
00070     protected function _getList( $oUser = null )
00071     {
00072         $sUserId = $oUser?$oUser->getId():'';
00073 
00074         if ( $this->_blReload || $sUserId !== $this->_sUserId ) {
00075             // loading list
00076             $this->selectString( $this->_getFilterSelect( $oUser ) );
00077 
00078             // setting list proterties
00079             $this->_blReload = false;    // reload marker
00080             $this->_sUserId  = $sUserId; // discount list user id
00081         }
00082 
00083         // resetting array pointer
00084         $this->rewind();
00085 
00086         return $this;
00087     }
00088 
00096     public function getCountryId( $oUser )
00097     {
00098         $sCountryId = null;
00099         if ( $oUser ) {
00100             $sCountryId = $oUser->getActiveCountry();
00101         }
00102 
00103         return $sCountryId;
00104     }
00105 
00111     public function forceReload()
00112     {
00113         $this->_blReload = true;
00114     }
00115 
00123     protected function _getFilterSelect( $oUser )
00124     {
00125         $oBaseObject = $this->getBaseObject();
00126 
00127         $sTable = $oBaseObject->getViewName();
00128         $sQ  = "select ".$oBaseObject->getSelectFields()." from $sTable ";
00129         $sQ .= "where ".$oBaseObject->getSqlActiveSnippet().' ';
00130 
00131 
00132         // defining initial filter parameters
00133         $sUserId    = null;
00134         $sGroupIds  = null;
00135         $sCountryId = $this->getCountryId( $oUser );
00136         $oDb = oxDb::getDb();
00137 
00138         // checking for current session user which gives additional restrictions for user itself, users group and country
00139         if ( $oUser ) {
00140 
00141             // user ID
00142             $sUserId = $oUser->getId();
00143 
00144             // user group ids
00145             foreach ( $oUser->getUserGroups() as $oGroup ) {
00146                 if ( $sGroupIds ) {
00147                     $sGroupIds .= ', ';
00148                 }
00149                 $sGroupIds .= $oDb->quote( $oGroup->getId() );
00150             }
00151         }
00152 
00153         $sUserTable    = getViewName( 'oxuser' );
00154         $sGroupTable   = getViewName( 'oxgroups' );
00155         $sCountryTable = getViewName( 'oxcountry' );
00156 
00157         $sCountrySql = $sCountryId?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' and oxobject2discount.OXOBJECTID=".$oDb->quote( $sCountryId ).")":'0';
00158         $sUserSql    = $sUserId   ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' and oxobject2discount.OXOBJECTID=".$oDb->quote( $sUserId ). ")":'0';
00159         $sGroupSql   = $sGroupIds ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' and oxobject2discount.OXOBJECTID in ($sGroupIds) )":'0';
00160 
00161         $sQ .= "and (
00162             select
00163                 if(EXISTS(select 1 from oxobject2discount, $sCountryTable where $sCountryTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' LIMIT 1),
00164                         $sCountrySql,
00165                         1) &&
00166                 if(EXISTS(select 1 from oxobject2discount, $sUserTable where $sUserTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' LIMIT 1),
00167                         $sUserSql,
00168                         1) &&
00169                 if(EXISTS(select 1 from oxobject2discount, $sGroupTable where $sGroupTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' LIMIT 1),
00170                         $sGroupSql,
00171                         1)
00172             )";
00173 
00174         return $sQ;
00175     }
00176 
00185     public function getArticleDiscounts( $oArticle, $oUser = null )
00186     {
00187         $aList = array();
00188         $aDiscList = $this->_getList( $oUser )->getArray();
00189         foreach ( $aDiscList as $oDiscount ) {
00190             if ( $oDiscount->isForArticle( $oArticle ) ) {
00191                 $aList[$oDiscount->getId()] = $oDiscount;
00192             }
00193         }
00194 
00195         return $aList;
00196     }
00197 
00207     public function getBasketItemDiscounts( $oArticle, $oBasket, $oUser = null )
00208     {
00209         $aList = array();
00210         $aDiscList = $this->_getList( $oUser )->getArray();
00211         foreach ( $aDiscList as $oDiscount ) {
00212             if ( $oDiscount->isForBasketItem( $oArticle ) && $oDiscount->isForBasketAmount( $oBasket ) ) {
00213                 $aList[$oDiscount->getId()] = $oDiscount;
00214             }
00215         }
00216 
00217         return $aList;
00218     }
00219 
00228     public function getBasketDiscounts( $oBasket, $oUser = null )
00229     {
00230         $aList = array();
00231         $aDiscList = $this->_getList( $oUser )->getArray();
00232         foreach ( $aDiscList as $oDiscount ) {
00233             if ( $oDiscount->isForBasket( $oBasket ) ) {
00234                 $aList[$oDiscount->getId()] = $oDiscount;
00235             }
00236         }
00237 
00238         return $aList;
00239     }
00240 
00250     public function getBasketItemBundleDiscounts( $oArticle, $oBasket, $oUser = null )
00251     {
00252         $aList = array();
00253         $aDiscList = $this->_getList( $oUser )->getArray();
00254         foreach ( $aDiscList as $oDiscount ) {
00255             if ( $oDiscount->isForBundleItem( $oArticle, $oBasket ) && $oDiscount->isForBasketAmount($oBasket) ) {
00256                 $aList[$oDiscount->getId()] = $oDiscount;
00257             }
00258         }
00259 
00260         return $aList;
00261     }
00262 
00271     public function getBasketBundleDiscounts( $oBasket, $oUser = null )
00272     {
00273         $aList = array();
00274         $aDiscList = $this->_getList( $oUser )->getArray();
00275         foreach ( $aDiscList as $oDiscount ) {
00276             if ( $oDiscount->isForBundleBasket( $oBasket ) ) {
00277                 $aList[$oDiscount->getId()] = $oDiscount;
00278             }
00279         }
00280 
00281         return $aList;
00282     }
00283 
00294     public function applyDiscounts( $oPrice, $aDiscounts )
00295     {
00296         reset( $aDiscounts );
00297         while ( list( , $oDiscount ) = each( $aDiscounts ) ) {
00298             $oDiscount->applyDiscount( $oPrice );
00299         }
00300     }
00301 
00314     public function applyBasketDiscounts( oxPrice $oPrice, $aDiscounts, $dAmount = 1 )
00315     {
00316         $aDiscLog = array();
00317         reset( $aDiscounts );
00318 
00319         // price object to correctly perform calculations
00320         $dOldPrice = $oPrice->getPrice();
00321 
00322         while (list( , $oDiscount ) = each( $aDiscounts ) ) {
00323             $oDiscount->applyDiscount( $oPrice );
00324             $dNewPrice = $oPrice->getPrice();
00325 
00326             if ( !isset( $aDiscLog[$oDiscount->getId()] ) ) {
00327                 $aDiscLog[$oDiscount->getId()] = $oDiscount->getSimpleDiscount();
00328             }
00329 
00330             $aDiscLog[$oDiscount->getId()]->dDiscount += $dOldPrice - $dNewPrice;
00331             $aDiscLog[$oDiscount->getId()]->dDiscount *= $dAmount;
00332             $dOldPrice = $dNewPrice;
00333         }
00334         return $aDiscLog;
00335     }
00336 
00337 
00343     public function hasSkipDiscountCategories()
00344     {
00345         if ( $this->_hasSkipDiscountCategories === null  || $this->_blReload ) {
00346             $sViewName = getViewName( 'oxcategories' );
00347             $sQ = "select 1 from {$sViewName} where {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
00348 
00349             $this->_hasSkipDiscountCategories = (bool) oxDb::getDb()->getOne( $sQ );
00350         }
00351         return $this->_hasSkipDiscountCategories;
00352     }
00353 }