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 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 $oPrice->multiply($dAmount);
00322
00323
00324 $dOldPrice = $oPrice->getBruttoPrice();
00325
00326 while (list( , $oDiscount ) = each( $aDiscounts ) ) {
00327
00328 $oDiscount->applyDiscount( $oPrice, $dAmount );
00329 $dNewPrice = $oPrice->getBruttoPrice();
00330
00331 if ( !isset( $aDiscLog[$oDiscount->getId()] ) ) {
00332 $aDiscLog[$oDiscount->getId()] = $oDiscount->getSimpleDiscount();
00333 }
00334
00335 $aDiscLog[$oDiscount->getId()]->dDiscount += $dOldPrice - $dNewPrice;
00337
00338 $dOldPrice = $dNewPrice;
00339 }
00341 $oPrice->divide($dAmount);
00342
00343 return $aDiscLog;
00344 }
00355
00356
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
00394 public function hasSkipDiscountCategories()
00395 {
00396 if ( $this->_hasSkipDiscountCategories === null || $this->_blReload ) {
00397 $sViewName = getViewName( 'oxcategories' );
00398 $sQ = "select 1 from {$sViewName} where {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
00399
00400 $this->_hasSkipDiscountCategories = (bool) oxDb::getDb()->getOne( $sQ );
00401 }
00402 return $this->_hasSkipDiscountCategories;
00403 }
00404 }