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         foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00219             if ( $oDiscount->isForBasketItem( $oArticle ) && $oDiscount->isForBasketAmount( $oBasket ) ) {
00220                 $aList[$oDiscount->getId()] = $oDiscount;
00221             }
00222         }
00223 
00224         return $aList;
00225     }
00226 
00235     public function getBasketDiscounts( $oBasket, $oUser = null )
00236     {
00237         $aList = array();
00238         $oList = $this->_getList( $oUser );
00239         foreach ( $oList->getArray() as $oDiscount ) {
00240             if ( $oDiscount->isForBasket( $oBasket ) ) {
00241                 $aList[$oDiscount->getId()] = $oDiscount;
00242             }
00243         }
00244 
00245         return $aList;
00246     }
00247 
00257     public function getBasketItemBundleDiscounts( $oArticle, $oBasket, $oUser = null )
00258     {
00259         $aList = array();
00260         foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00261             if ( $oDiscount->isForBundleItem( $oArticle, $oBasket ) && $oDiscount->isForBasketAmount($oBasket) ) {
00262                 $aList[$oDiscount->getId()] = $oDiscount;
00263             }
00264         }
00265 
00266         return $aList;
00267     }
00268 
00277     public function getBasketBundleDiscounts( $oBasket, $oUser = null )
00278     {
00279         $aList = array();
00280         $aDiscList = $this->_getList( $oUser )->getArray();
00281         foreach ( $aDiscList as $oDiscount ) {
00282             if ( $oDiscount->isForBundleBasket( $oBasket ) ) {
00283                 $aList[$oDiscount->getId()] = $oDiscount;
00284             }
00285         }
00286 
00287         return $aList;
00288     }
00289 
00298     public function applyDiscounts( $oPrice, $aDiscounts )
00299     {
00300         reset( $aDiscounts );
00301         while ( list( , $oDiscount ) = each( $aDiscounts ) ) {
00302             $oDiscount->applyDiscount( $oPrice );
00303         }
00304     }
00305 
00316     public function applyBasketDiscounts( oxPrice $oPrice, $aDiscounts, $dAmount = 1 )
00317     {
00318         $aDiscLog = array();
00319         reset( $aDiscounts );
00320 
00321         // price object to correctly perform calculations
00322         $dOldPrice = $oPrice->getBruttoPrice();
00323 
00324         while (list( , $oDiscount ) = each( $aDiscounts ) ) {
00325             $oDiscount->applyDiscount( $oPrice );
00326             $dNewPrice = $oPrice->getBruttoPrice();
00327 
00328             if ( !isset( $aDiscLog[$oDiscount->getId()] ) ) {
00329                 $aDiscLog[$oDiscount->getId()] = $oDiscount->getSimpleDiscount();
00330             }
00331 
00332             $aDiscLog[$oDiscount->getId()]->dDiscount += $dOldPrice - $dNewPrice;
00333             $aDiscLog[$oDiscount->getId()]->dDiscount *= $dAmount;
00334             $dOldPrice = $dNewPrice;
00335         }
00336         return $aDiscLog;
00337     }
00338 
00344     public function hasSkipDiscountCategories()
00345     {
00346         if ( $this->_hasSkipDiscountCategories === null  || $this->_blReload ) {
00347             $sViewName = getViewName( 'oxcategories' );
00348             $sQ = "select 1 from {$sViewName} where {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
00349 
00350             $this->_hasSkipDiscountCategories = (bool) oxDb::getDb()->getOne( $sQ );
00351         }
00352         return $this->_hasSkipDiscountCategories;
00353     }
00354 }