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
00068 protected function _getList( $oUser = null )
00069 {
00070 $sUserId = $oUser?$oUser->getId():'';
00071
00072 if ( $this->_blReload || $sUserId !== $this->_sUserId ) {
00073
00074 $this->selectString( $this->_getFilterSelect( $oUser ) );
00075
00076
00077 $this->_blReload = false;
00078 $this->_sUserId = $sUserId;
00079 }
00080
00081
00082 $this->rewind();
00083
00084 return $this;
00085 }
00086
00094 public function getCountryId( $oUser )
00095 {
00096 $sCountryId = null;
00097 if ( $oUser ) {
00098 $sCountryId = $oUser->getActiveCountry();
00099 }
00100
00101 return $sCountryId;
00102 }
00103
00109 public function forceReload()
00110 {
00111 $this->_blReload = true;
00112 }
00113
00121 protected function _getFilterSelect( $oUser )
00122 {
00123 $oBaseObject = $this->getBaseObject();
00124
00125 $sTable = $oBaseObject->getViewName();
00126 $sQ = "select ".$oBaseObject->getSelectFields()." from $sTable ";
00127 $sQ .= "where ".$oBaseObject->getSqlActiveSnippet().' ';
00128
00129
00130
00131 $sUserId = null;
00132 $sGroupIds = null;
00133 $sCountryId = $this->getCountryId( $oUser );
00134 $oDb = oxDb::getDb();
00135
00136
00137 if ( $oUser ) {
00138
00139
00140 $sUserId = $oUser->getId();
00141
00142
00143 foreach ( $oUser->getUserGroups() as $oGroup ) {
00144 if ( $sGroupIds ) {
00145 $sGroupIds .= ', ';
00146 }
00147 $sGroupIds .= $oDb->quote( $oGroup->getId() );
00148 }
00149 }
00150
00151 $sUserTable = getViewName( 'oxuser' );
00152 $sGroupTable = getViewName( 'oxgroups' );
00153 $sCountryTable = getViewName( 'oxcountry' );
00154
00155 $sCountrySql = $sCountryId?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' and oxobject2discount.OXOBJECTID=".$oDb->quote( $sCountryId ).")":'0';
00156 $sUserSql = $sUserId ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' and oxobject2discount.OXOBJECTID=".$oDb->quote( $sUserId ). ")":'0';
00157 $sGroupSql = $sGroupIds ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' and oxobject2discount.OXOBJECTID in ($sGroupIds) )":'0';
00158
00159 $sQ .= "and (
00160 select
00161 if(EXISTS(select 1 from oxobject2discount, $sCountryTable where $sCountryTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' LIMIT 1),
00162 $sCountrySql,
00163 1) &&
00164 if(EXISTS(select 1 from oxobject2discount, $sUserTable where $sUserTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' LIMIT 1),
00165 $sUserSql,
00166 1) &&
00167 if(EXISTS(select 1 from oxobject2discount, $sGroupTable where $sGroupTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' LIMIT 1),
00168 $sGroupSql,
00169 1)
00170 )";
00171
00172 return $sQ;
00173 }
00174
00183 public function getArticleDiscounts( $oArticle, $oUser = null )
00184 {
00185 $aList = array();
00186 foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00187 if ( $oDiscount->isForArticle( $oArticle ) ) {
00188 $aList[$oDiscount->getId()] = $oDiscount;
00189 }
00190 }
00191
00192 return $aList;
00193 }
00194
00204 public function getBasketItemDiscounts( $oArticle, $oBasket, $oUser = null )
00205 {
00206 $aList = array();
00207 $aDiscList = $this->_getList( $oUser )->getArray();
00208 foreach ( $aDiscList as $oDiscount ) {
00209 if ( $oDiscount->isForBasketItem( $oArticle ) && $oDiscount->isForBasketAmount( $oBasket ) ) {
00210 $aList[$oDiscount->getId()] = $oDiscount;
00211 }
00212 }
00213
00214 return $aList;
00215 }
00216
00225 public function getBasketDiscounts( $oBasket, $oUser = null )
00226 {
00227 $aList = array();
00228 $oList = $this->_getList( $oUser );
00229 foreach ( $oList->getArray() as $oDiscount ) {
00230 if ( $oDiscount->isForBasket( $oBasket ) ) {
00231 $aList[$oDiscount->getId()] = $oDiscount;
00232 }
00233 }
00234
00235 return $aList;
00236 }
00237
00247 public function getBasketItemBundleDiscounts( $oArticle, $oBasket, $oUser = null )
00248 {
00249 $aList = array();
00250 foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00251 if ( $oDiscount->isForBundleItem( $oArticle, $oBasket ) && $oDiscount->isForBasketAmount($oBasket) ) {
00252 $aList[$oDiscount->getId()] = $oDiscount;
00253 }
00254 }
00255
00256 return $aList;
00257 }
00258
00267 public function getBasketBundleDiscounts( $oBasket, $oUser = null )
00268 {
00269 $aList = array();
00270 $aDiscList = $this->_getList( $oUser )->getArray();
00271 foreach ( $aDiscList as $oDiscount ) {
00272 if ( $oDiscount->isForBundleBasket( $oBasket ) ) {
00273 $aList[$oDiscount->getId()] = $oDiscount;
00274 }
00275 }
00276
00277 return $aList;
00278 }
00279
00290 public function applyDiscounts( $oPrice, $aDiscounts )
00291 {
00292 reset( $aDiscounts );
00293 while ( list( , $oDiscount ) = each( $aDiscounts ) ) {
00294 $oDiscount->applyDiscount( $oPrice );
00295 }
00296 }
00297
00310 public function applyBasketDiscounts( oxPrice $oPrice, $aDiscounts, $dAmount = 1 )
00311 {
00312 $aDiscLog = array();
00313 reset( $aDiscounts );
00314
00315
00316 $dOldPrice = $oPrice->getPrice();
00317
00318 while (list( , $oDiscount ) = each( $aDiscounts ) ) {
00319 $oDiscount->applyDiscount( $oPrice );
00320 $dNewPrice = $oPrice->getPrice();
00321
00322 if ( !isset( $aDiscLog[$oDiscount->getId()] ) ) {
00323 $aDiscLog[$oDiscount->getId()] = $oDiscount->getSimpleDiscount();
00324 }
00325
00326 $aDiscLog[$oDiscount->getId()]->dDiscount += $dOldPrice - $dNewPrice;
00327 $aDiscLog[$oDiscount->getId()]->dDiscount *= $dAmount;
00328 $dOldPrice = $dNewPrice;
00329 }
00330 return $aDiscLog;
00331 }
00332
00333
00339 public function hasSkipDiscountCategories()
00340 {
00341 if ( $this->_hasSkipDiscountCategories === null || $this->_blReload ) {
00342 $sViewName = getViewName( 'oxcategories' );
00343 $sQ = "select 1 from {$sViewName} where {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
00344
00345 $this->_hasSkipDiscountCategories = (bool) oxDb::getDb()->getOne( $sQ );
00346 }
00347 return $this->_hasSkipDiscountCategories;
00348 }
00349 }