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
00056 if ( defined( 'OXID_PHP_UNIT' ) ) {
00057 self::$_instance = modInstances::getMod( __CLASS__ );
00058 }
00059
00060 if ( !isset( self::$_instance ) ) {
00061
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
00085 $this->selectString( $this->_getFilterSelect( $oUser ) );
00086
00087
00088 $this->_blReload = false;
00089 $this->_sUserId = $sUserId;
00090 }
00091
00092
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
00142 $sUserId = null;
00143 $sGroupIds = null;
00144 $sCountryId = $this->getCountryId( $oUser );
00145 $oDb = oxDb::getDb();
00146
00147
00148 if ( $oUser ) {
00149
00150
00151 $sUserId = $oUser->getId();
00152
00153
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
00323 $oPrice->multiply($dAmount);
00324
00325
00326 $dOldPrice = $oPrice->getBruttoPrice();
00327
00328 while (list( , $oDiscount ) = each( $aDiscounts ) ) {
00329
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
00340 $dOldPrice = $dNewPrice;
00341 }
00343 $oPrice->divide($dAmount);
00344
00345 return $aDiscLog;
00346 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
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 }