OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxdiscountlist.php
Go to the documentation of this file.
1 <?php
2 
8 class oxDiscountList extends oxList
9 {
15  static protected $_instance = null;
16 
22  protected $_sUserId = null;
23 
29  protected $_blReload = true;
30 
31 
37  protected $_hasSkipDiscountCategories = null;
38 
44  public function __construct( $sObjectsInListName = 'oxdiscount' )
45  {
46  parent::__construct( 'oxdiscount' );
47  }
48 
56  static public function getInstance()
57  {
58  return oxRegistry::get("oxDiscountList");
59  }
60 
70  protected function _getList( $oUser = null )
71  {
72  $sUserId = $oUser?$oUser->getId():'';
73 
74  if ( $this->_blReload || $sUserId !== $this->_sUserId ) {
75  // loading list
76  $this->selectString( $this->_getFilterSelect( $oUser ) );
77 
78  // setting list proterties
79  $this->_blReload = false; // reload marker
80  $this->_sUserId = $sUserId; // discount list user id
81  }
82 
83  // resetting array pointer
84  $this->rewind();
85 
86  return $this;
87  }
88 
96  public function getCountryId( $oUser )
97  {
98  $sCountryId = null;
99  if ( $oUser ) {
100  $sCountryId = $oUser->getActiveCountry();
101  }
102 
103  return $sCountryId;
104  }
105 
111  public function forceReload()
112  {
113  $this->_blReload = true;
114  }
115 
123  protected function _getFilterSelect( $oUser )
124  {
125  $oBaseObject = $this->getBaseObject();
126 
127  $sTable = $oBaseObject->getViewName();
128  $sQ = "select ".$oBaseObject->getSelectFields()." from $sTable ";
129  $sQ .= "where ".$oBaseObject->getSqlActiveSnippet().' ';
130 
131 
132  // defining initial filter parameters
133  $sUserId = null;
134  $sGroupIds = null;
135  $sCountryId = $this->getCountryId( $oUser );
136  $oDb = oxDb::getDb();
137 
138  // checking for current session user which gives additional restrictions for user itself, users group and country
139  if ( $oUser ) {
140 
141  // user ID
142  $sUserId = $oUser->getId();
143 
144  // user group ids
145  foreach ( $oUser->getUserGroups() as $oGroup ) {
146  if ( $sGroupIds ) {
147  $sGroupIds .= ', ';
148  }
149  $sGroupIds .= $oDb->quote( $oGroup->getId() );
150  }
151  }
152 
153  $sUserTable = getViewName( 'oxuser' );
154  $sGroupTable = getViewName( 'oxgroups' );
155  $sCountryTable = getViewName( 'oxcountry' );
156 
157  $sCountrySql = $sCountryId?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' and oxobject2discount.OXOBJECTID=".$oDb->quote( $sCountryId ).")":'0';
158  $sUserSql = $sUserId ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' and oxobject2discount.OXOBJECTID=".$oDb->quote( $sUserId ). ")":'0';
159  $sGroupSql = $sGroupIds ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' and oxobject2discount.OXOBJECTID in ($sGroupIds) )":'0';
160 
161  $sQ .= "and (
162  select
163  if(EXISTS(select 1 from oxobject2discount, $sCountryTable where $sCountryTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' LIMIT 1),
164  $sCountrySql,
165  1) &&
166  if(EXISTS(select 1 from oxobject2discount, $sUserTable where $sUserTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' LIMIT 1),
167  $sUserSql,
168  1) &&
169  if(EXISTS(select 1 from oxobject2discount, $sGroupTable where $sGroupTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' LIMIT 1),
170  $sGroupSql,
171  1)
172  )";
173 
174  return $sQ;
175  }
176 
185  public function getArticleDiscounts( $oArticle, $oUser = null )
186  {
187  $aList = array();
188  $aDiscList = $this->_getList( $oUser )->getArray();
189  foreach ( $aDiscList as $oDiscount ) {
190  if ( $oDiscount->isForArticle( $oArticle ) ) {
191  $aList[$oDiscount->getId()] = $oDiscount;
192  }
193  }
194 
195  return $aList;
196  }
197 
207  public function getBasketItemDiscounts( $oArticle, $oBasket, $oUser = null )
208  {
209  $aList = array();
210  $aDiscList = $this->_getList( $oUser )->getArray();
211  foreach ( $aDiscList as $oDiscount ) {
212  if ( $oDiscount->isForBasketItem( $oArticle ) && $oDiscount->isForBasketAmount( $oBasket ) ) {
213  $aList[$oDiscount->getId()] = $oDiscount;
214  }
215  }
216 
217  return $aList;
218  }
219 
228  public function getBasketDiscounts( $oBasket, $oUser = null )
229  {
230  $aList = array();
231  $aDiscList = $this->_getList( $oUser )->getArray();
232  foreach ( $aDiscList as $oDiscount ) {
233  if ( $oDiscount->isForBasket( $oBasket ) ) {
234  $aList[$oDiscount->getId()] = $oDiscount;
235  }
236  }
237 
238  return $aList;
239  }
240 
250  public function getBasketItemBundleDiscounts( $oArticle, $oBasket, $oUser = null )
251  {
252  $aList = array();
253  $aDiscList = $this->_getList( $oUser )->getArray();
254  foreach ( $aDiscList as $oDiscount ) {
255  if ( $oDiscount->isForBundleItem( $oArticle, $oBasket ) && $oDiscount->isForBasketAmount($oBasket) ) {
256  $aList[$oDiscount->getId()] = $oDiscount;
257  }
258  }
259 
260  return $aList;
261  }
262 
271  public function getBasketBundleDiscounts( $oBasket, $oUser = null )
272  {
273  $aList = array();
274  $aDiscList = $this->_getList( $oUser )->getArray();
275  foreach ( $aDiscList as $oDiscount ) {
276  if ( $oDiscount->isForBundleBasket( $oBasket ) ) {
277  $aList[$oDiscount->getId()] = $oDiscount;
278  }
279  }
280 
281  return $aList;
282  }
283 
294  public function applyDiscounts( $oPrice, $aDiscounts )
295  {
296  reset( $aDiscounts );
297  while ( list( , $oDiscount ) = each( $aDiscounts ) ) {
298  $oDiscount->applyDiscount( $oPrice );
299  }
300  }
301 
314  public function applyBasketDiscounts( oxPrice $oPrice, $aDiscounts, $dAmount = 1 )
315  {
316  $aDiscLog = array();
317  reset( $aDiscounts );
318 
319  // price object to correctly perform calculations
320  $dOldPrice = $oPrice->getPrice();
321 
322  while (list( , $oDiscount ) = each( $aDiscounts ) ) {
323  $oDiscount->applyDiscount( $oPrice );
324  $dNewPrice = $oPrice->getPrice();
325 
326  if ( !isset( $aDiscLog[$oDiscount->getId()] ) ) {
327  $aDiscLog[$oDiscount->getId()] = $oDiscount->getSimpleDiscount();
328  }
329 
330  $aDiscLog[$oDiscount->getId()]->dDiscount += $dOldPrice - $dNewPrice;
331  $aDiscLog[$oDiscount->getId()]->dDiscount *= $dAmount;
332  $dOldPrice = $dNewPrice;
333  }
334  return $aDiscLog;
335  }
336 
337 
343  public function hasSkipDiscountCategories()
344  {
345  if ( $this->_hasSkipDiscountCategories === null || $this->_blReload ) {
346  $sViewName = getViewName( 'oxcategories' );
347  $sQ = "select 1 from {$sViewName} where {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
348 
349  $this->_hasSkipDiscountCategories = (bool) oxDb::getDb()->getOne( $sQ );
350  }
352  }
353 }