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 $sCountrySql = $sCountryId?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' and oxobject2discount.OXOBJECTID='$sCountryId')":'0';
00156 $sUserSql = $sUserId ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' and oxobject2discount.OXOBJECTID='$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 where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' LIMIT 1),
00162 $sCountrySql,
00163 1) &&
00164 if(EXISTS(select 1 from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' LIMIT 1),
00165 $sUserSql,
00166 1) &&
00167 if(EXISTS(select 1 from oxobject2discount where 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 foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00228 if ( $oDiscount->isForBasket( $oBasket ) ) {
00229 $aList[$oDiscount->getId()] = $oDiscount;
00230 }
00231 }
00232
00233 return $aList;
00234 }
00235
00245 public function getBasketItemBundleDiscounts( $oArticle, $oBasket, $oUser = null )
00246 {
00247 $aList = array();
00248 foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00249 if ( $oDiscount->isForBundleItem( $oArticle, $oBasket ) && $oDiscount->isForBasketAmount($oBasket) ) {
00250 $aList[$oDiscount->getId()] = $oDiscount;
00251 }
00252 }
00253
00254 return $aList;
00255 }
00256
00265 public function getBasketBundleDiscounts( $oBasket, $oUser = null )
00266 {
00267 $aList = array();
00268 foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00269 if ( $oDiscount->isForBundleBasket( $oBasket ) ) {
00270 $aList[$oDiscount->getId()] = $oDiscount;
00271 }
00272 }
00273
00274 return $aList;
00275 }
00276
00285 public function applyDiscounts( $oPrice, $aDiscounts )
00286 {
00287 reset( $aDiscounts );
00288 while ( list( , $oDiscount ) = each( $aDiscounts ) ) {
00289 $oDiscount->applyDiscount( $oPrice );
00290 }
00291 }
00292
00303 public function applyBasketDiscounts( oxPrice $oPrice, $aDiscounts, $dAmount = 1 )
00304 {
00305 $aDiscLog = array();
00306 reset( $aDiscounts );
00307
00308
00309 $dOldPrice = $oPrice->getBruttoPrice();
00310
00311 while (list( , $oDiscount ) = each( $aDiscounts ) ) {
00312 $oDiscount->applyDiscount( $oPrice );
00313 $dNewPrice = $oPrice->getBruttoPrice();
00314
00315 if ( !isset( $aDiscLog[$oDiscount->getId()] ) ) {
00316 $aDiscLog[$oDiscount->getId()] = $oDiscount->getSimpleDiscount();
00317 }
00318
00319 $aDiscLog[$oDiscount->getId()]->dDiscount += $dOldPrice - $dNewPrice;
00320 $aDiscLog[$oDiscount->getId()]->dDiscount *= $dAmount;
00321 $dOldPrice = $dNewPrice;
00322 }
00323 return $aDiscLog;
00324 }
00325 }