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
00035 public function __construct( $sObjectsInListName = 'oxdiscount' )
00036 {
00037 parent::__construct( 'oxdiscount' );
00038 }
00039
00045 static public function getInstance()
00046 {
00047
00048 if ( defined( 'OXID_PHP_UNIT' ) ) {
00049 static $inst = array();
00050 self::$_instance = $inst[oxClassCacheKey()];
00051 }
00052
00053 if ( !isset( self::$_instance ) ) {
00054
00055 self::$_instance = oxNew( 'oxDiscountList' );
00056
00057 if ( defined( 'OXID_PHP_UNIT' ) ) {
00058 $inst[oxClassCacheKey()] = self::$_instance;
00059 }
00060 }
00061
00062 return self::$_instance;
00063 }
00064
00072 protected function _getList( $oUser = null )
00073 {
00074 $sUserId = $oUser?$oUser->getId():'';
00075
00076 if ( $this->_blReload || $sUserId !== $this->_sUserId ) {
00077
00078 $this->selectString( $this->_getFilterSelect( $oUser ) );
00079
00080
00081 $this->_blReload = false;
00082 $this->_sUserId = $sUserId;
00083 }
00084
00085
00086 $this->rewind();
00087
00088 return $this;
00089 }
00090
00098 public function getCountryId( $oUser )
00099 {
00100 $sCountryId = null;
00101 if ( $oUser ) {
00102 $sCountryId = $oUser->getActiveCountry();
00103 }
00104
00105 return $sCountryId;
00106 }
00107
00113 public function forceReload()
00114 {
00115 $this->_blReload = true;
00116 }
00117
00125 protected function _getFilterSelect( $oUser )
00126 {
00127
00128 $sTable = getViewName( 'oxdiscount' );
00129 $oBaseObject = $this->getBaseObject();
00130
00131 $sQ = "select ".$oBaseObject->getSelectFields()." from $sTable ";
00132 $sQ .= "where ".$oBaseObject->getSqlActiveSnippet().' ';
00133
00134
00135
00136 $sUserId = null;
00137 $sGroupIds = null;
00138 $sCountryId = $this->getCountryId( $oUser );
00139
00140
00141 if ( $oUser ) {
00142
00143
00144 $sUserId = $oUser->getId();
00145
00146
00147 foreach ( $oUser->getUserGroups() as $oGroup ) {
00148 if ( $sGroupIds ) {
00149 $sGroupIds .= ', ';
00150 }
00151 $sGroupIds .= "'".$oGroup->getId()."'";
00152 }
00153 }
00154
00155 $sUserTable = getViewName( 'oxuser' );
00156 $sGroupTable = getViewName( 'oxgroups' );
00157 $sCountryTable = getViewName( 'oxcountry' );
00158
00159 $oDb = oxDb::getDb();
00160 $sCountrySql = $sCountryId?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' and oxobject2discount.OXOBJECTID=".$oDb->quote( $sCountryId ).")":'0';
00161 $sUserSql = $sUserId ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' and oxobject2discount.OXOBJECTID='$sUserId')":'0';
00162 $sGroupSql = $sGroupIds ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' and oxobject2discount.OXOBJECTID in ($sGroupIds) )":'0';
00163
00164 $sQ .= "and (
00165 select
00166 if(EXISTS(select 1 from oxobject2discount, $sCountryTable where $sCountryTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' LIMIT 1),
00167 $sCountrySql,
00168 1) &&
00169 if(EXISTS(select 1 from oxobject2discount, $sUserTable where $sUserTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' LIMIT 1),
00170 $sUserSql,
00171 1) &&
00172 if(EXISTS(select 1 from oxobject2discount, $sGroupTable where $sGroupTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' LIMIT 1),
00173 $sGroupSql,
00174 1)
00175 )";
00176
00177 return $sQ;
00178 }
00179
00188 public function getArticleDiscounts( $oArticle, $oUser = null )
00189 {
00190 $aList = array();
00191 foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00192 if ( $oDiscount->isForArticle( $oArticle ) ) {
00193 $aList[$oDiscount->getId()] = $oDiscount;
00194 }
00195 }
00196
00197 return $aList;
00198 }
00199
00209 public function getBasketItemDiscounts( $oArticle, $oBasket, $oUser = null )
00210 {
00211 $aList = array();
00212 foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00213 if ( $oDiscount->isForBasketItem( $oArticle ) && $oDiscount->isForBasketAmount( $oBasket ) ) {
00214 $aList[$oDiscount->getId()] = $oDiscount;
00215 }
00216 }
00217
00218 return $aList;
00219 }
00220
00229 public function getBasketDiscounts( $oBasket, $oUser = null )
00230 {
00231 $aList = array();
00232 foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00233 if ( $oDiscount->isForBasket( $oBasket ) ) {
00234 $aList[$oDiscount->getId()] = $oDiscount;
00235 }
00236 }
00237
00238 return $aList;
00239 }
00240
00250 public function getBasketItemBundleDiscounts( $oArticle, $oBasket, $oUser = null )
00251 {
00252 $aList = array();
00253 foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00254 if ( $oDiscount->isForBundleItem( $oArticle, $oBasket ) && $oDiscount->isForBasketAmount($oBasket) ) {
00255 $aList[$oDiscount->getId()] = $oDiscount;
00256 }
00257 }
00258
00259 return $aList;
00260 }
00261
00270 public function getBasketBundleDiscounts( $oBasket, $oUser = null )
00271 {
00272 $aList = array();
00273 foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00274 if ( $oDiscount->isForBundleBasket( $oBasket ) ) {
00275 $aList[$oDiscount->getId()] = $oDiscount;
00276 }
00277 }
00278
00279 return $aList;
00280 }
00281
00290 public function applyDiscounts( $oPrice, $aDiscounts )
00291 {
00292 reset( $aDiscounts );
00293 while ( list( , $oDiscount ) = each( $aDiscounts ) ) {
00294 $oDiscount->applyDiscount( $oPrice );
00295 }
00296 }
00297
00308 public function applyBasketDiscounts( oxPrice $oPrice, $aDiscounts, $dAmount = 1 )
00309 {
00310 $aDiscLog = array();
00311 reset( $aDiscounts );
00312
00313
00314 $dOldPrice = $oPrice->getBruttoPrice();
00315
00316 while (list( , $oDiscount ) = each( $aDiscounts ) ) {
00317 $oDiscount->applyDiscount( $oPrice );
00318 $dNewPrice = $oPrice->getBruttoPrice();
00319
00320 if ( !isset( $aDiscLog[$oDiscount->getId()] ) ) {
00321 $aDiscLog[$oDiscount->getId()] = $oDiscount->getSimpleDiscount();
00322 }
00323
00324 $aDiscLog[$oDiscount->getId()]->dDiscount += $dOldPrice - $dNewPrice;
00325 $aDiscLog[$oDiscount->getId()]->dDiscount *= $dAmount;
00326 $dOldPrice = $dNewPrice;
00327 }
00328 return $aDiscLog;
00329 }
00330 }