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 foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00208 if ( $oDiscount->isForBasketItem( $oArticle ) && $oDiscount->isForBasketAmount( $oBasket ) ) {
00209 $aList[$oDiscount->getId()] = $oDiscount;
00210 }
00211 }
00212
00213 return $aList;
00214 }
00215
00224 public function getBasketDiscounts( $oBasket, $oUser = null )
00225 {
00226 $aList = array();
00227 $oList = $this->_getList( $oUser );
00228 foreach ( $oList->getArray() as $oDiscount ) {
00229 if ( $oDiscount->isForBasket( $oBasket ) ) {
00230 $aList[$oDiscount->getId()] = $oDiscount;
00231 }
00232 }
00233
00234 return $aList;
00235 }
00236
00246 public function getBasketItemBundleDiscounts( $oArticle, $oBasket, $oUser = null )
00247 {
00248 $aList = array();
00249 foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00250 if ( $oDiscount->isForBundleItem( $oArticle, $oBasket ) && $oDiscount->isForBasketAmount($oBasket) ) {
00251 $aList[$oDiscount->getId()] = $oDiscount;
00252 }
00253 }
00254
00255 return $aList;
00256 }
00257
00266 public function getBasketBundleDiscounts( $oBasket, $oUser = null )
00267 {
00268 $aList = array();
00269 $aDiscList = $this->_getList( $oUser )->getArray();
00270 foreach ( $aDiscList as $oDiscount ) {
00271 if ( $oDiscount->isForBundleBasket( $oBasket ) ) {
00272 $aList[$oDiscount->getId()] = $oDiscount;
00273 }
00274 }
00275
00276 return $aList;
00277 }
00278
00289 public function applyDiscounts( $oPrice, $aDiscounts )
00290 {
00291 reset( $aDiscounts );
00292 while ( list( , $oDiscount ) = each( $aDiscounts ) ) {
00293 $oDiscount->applyDiscount( $oPrice );
00294 }
00295 }
00296
00309 public function applyBasketDiscounts( oxPrice $oPrice, $aDiscounts, $dAmount = 1 )
00310 {
00311 $aDiscLog = array();
00312 reset( $aDiscounts );
00313
00314
00315 $dOldPrice = $oPrice->getPrice();
00316
00317 while (list( , $oDiscount ) = each( $aDiscounts ) ) {
00318 $oDiscount->applyDiscount( $oPrice );
00319 $dNewPrice = $oPrice->getPrice();
00320
00321 if ( !isset( $aDiscLog[$oDiscount->getId()] ) ) {
00322 $aDiscLog[$oDiscount->getId()] = $oDiscount->getSimpleDiscount();
00323 }
00324
00325 $aDiscLog[$oDiscount->getId()]->dDiscount += $dOldPrice - $dNewPrice;
00326 $aDiscLog[$oDiscount->getId()]->dDiscount *= $dAmount;
00327 $dOldPrice = $dNewPrice;
00328 }
00329 return $aDiscLog;
00330 }
00331
00332
00338 public function hasSkipDiscountCategories()
00339 {
00340 if ( $this->_hasSkipDiscountCategories === null || $this->_blReload ) {
00341 $sViewName = getViewName( 'oxcategories' );
00342 $sQ = "select 1 from {$sViewName} where {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
00343
00344 $this->_hasSkipDiscountCategories = (bool) oxDb::getDb()->getOne( $sQ );
00345 }
00346 return $this->_hasSkipDiscountCategories;
00347 }
00348 }