OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxdelivery.php
Go to the documentation of this file.
1 <?php
2 
8 class oxDelivery extends oxI18n
9 {
15  protected $_sClassName = 'oxdelivery';
16 
23  protected $_iItemCnt = 0;
24 
31  protected $_iProdCnt = 0;
32 
39  protected $_dPrice = 0;
40 
46  protected $_oPrice = null;
47 
53  protected $_aArtIds = null;
54 
60  protected $_aCatIds = null;
61 
67  protected $_blFreeShipping = true;
68 
74  protected static $_aProductList = array();
75 
81  protected $_blDelVatOnTop = false;
82 
88  protected $_aCountriesISO = null;
89 
95  protected $_aRDFaDeliverySet = null;
96 
100  public function __construct()
101  {
103  $this->init( 'oxdelivery' );
104  $this->setDelVatOnTop( $this->getConfig()->getConfigParam( 'blDeliveryVatOnTop' ) );
105  }
106 
114  public function setDelVatOnTop( $blOnTop )
115  {
116  $this->_blDelVatOnTop = $blOnTop;
117  }
118 
124  public function getArticles()
125  {
126  if ( $this->_aArtIds !== null ) {
127  return $this->_aArtIds;
128  }
129 
130  $oDb = oxDb::getDb();
131  $sQ = "select oxobjectid from oxobject2delivery where oxdeliveryid=".$oDb->quote($this->getId())." and oxtype = 'oxarticles'";
132  $aArtIds = $oDb->getAll( $sQ );
133 
134  //make single dimension array
135  foreach ( $aArtIds as $aItem ) {
136  $this->_aArtIds[] = $aItem[0];
137  }
138 
139  return $this->_aArtIds;
140 
141  }
142 
148  public function getCategories()
149  {
150  if ( $this->_aCatIds !== null ) {
151  return $this->_aCatIds;
152  }
153 
154  $oDb = oxDb::getDb();
155  $sQ = "select oxobjectid from oxobject2delivery where oxdeliveryid=".$oDb->quote($this->getId())." and oxtype = 'oxcategories'";
156  $aCatIds = $oDb->getAll( $sQ );
157 
158  //make single dimension array
159  foreach ( $aCatIds AS $aItem ) {
160  $this->_aCatIds[] = $aItem[0];
161  }
162 
163  return $this->_aCatIds;
164  }
165 
171  public function hasArticles()
172  {
173  return ( bool ) count( $this->getArticles() );
174  }
175 
181  public function hasCategories()
182  {
183  return ( bool ) count( $this->getCategories() );
184  }
185 
193  public function getDeliveryAmount( $oBasketItem )
194  {
195  $dAmount = 0;
196  $oProduct = $oBasketItem->getArticle( false );
197 
198  $blExclNonMaterial = $this->getConfig()->getConfigParam( 'blExclNonMaterialFromDelivery' );
199 
200  // mark free shipping products
201  if ( $oProduct->oxarticles__oxfreeshipping->value || ($oProduct->oxarticles__oxnonmaterial->value && $blExclNonMaterial) ) {
202  if ($this->_blFreeShipping !== false) {
203  $this->_blFreeShipping = true;
204  }
205  } else {
206 
207  $this->_blFreeShipping = false;
208 
209  switch ( $this->oxdelivery__oxdeltype->value ) {
210  case 'p': // price
211  if ( $this->oxdelivery__oxfixed->value == 2 ) {
212  $dAmount += $oProduct->getPrice()->getPrice();
213  } else {
214  $dAmount += $oBasketItem->getPrice()->getPrice(); // price// currency conversion must allready be done in price class / $oCur->rate; // $oBasketItem->oPrice->getPrice() / $oCur->rate;
215  }
216  break;
217  case 'w': // weight
218  if ( $this->oxdelivery__oxfixed->value == 2 ) {
219  $dAmount += $oProduct->oxarticles__oxweight->value;
220  } else {
221  $dAmount += $oBasketItem->getWeight();
222  }
223  break;
224  case 's': // size
225  $dAmount += $oProduct->oxarticles__oxlength->value *
226  $oProduct->oxarticles__oxwidth->value *
227  $oProduct->oxarticles__oxheight->value;
228  if ( $this->oxdelivery__oxfixed->value < 2 ) {
229  $dAmount *= $oBasketItem->getAmount();
230  }
231  break;
232  case 'a': // amount
233  $dAmount += $oBasketItem->getAmount();
234  break;
235  }
236 
237  if ( $oBasketItem->getPrice() ) {
238  $this->_dPrice += $oBasketItem->getPrice()->getPrice();
239  }
240  }
241 
242  return $dAmount;
243  }
244 
252  public function setDeliveryPrice( $oPrice )
253  {
254  $this->_oPrice = $oPrice;
255  }
256 
264  public function getDeliveryPrice( $dVat = null )
265  {
266  if ( $this->_oPrice === null ) {
267  // loading oxprice object for final price calculation
268  $this->_oPrice = oxNew( 'oxPrice' );
269 
270  if ( !$this->_blDelVatOnTop ) {
271  $this->_oPrice->setBruttoPriceMode();
272  } else {
273  $this->_oPrice->setNettoPriceMode();
274  }
275 
276  $this->_oPrice->setVat( $dVat );
277 
278  // if article is free shipping, price for delivery will be not calculated
279  if ( $this->_blFreeShipping ) {
280  return $this->_oPrice;
281  }
282 
283  // calculating base price value
284  switch ( $this->oxdelivery__oxaddsumtype->value ) {
285  case 'abs':
286 
287  $dAmount = 0;
288 
289  if ( $this->oxdelivery__oxfixed->value == 0 ) {
290  // 1. Once per Cart
291  $dAmount = 1;
292  } elseif ( $this->oxdelivery__oxfixed->value == 1 ) {
293  // 2. Once per Product overall
294  $dAmount = $this->_iProdCnt;
295  } elseif ( $this->oxdelivery__oxfixed->value == 2 ) {
296  // 3. Once per Product in Cart
297  $dAmount = $this->_iItemCnt;
298  }
299 
300  $oCur = $this->getConfig()->getActShopCurrencyObject();
301  $this->_oPrice->add( $this->oxdelivery__oxaddsum->value * $oCur->rate );
302  $this->_oPrice->multiply( $dAmount );
303  break;
304  case '%':
305 
306  $this->_oPrice->add( $this->_dPrice /100 * $this->oxdelivery__oxaddsum->value );
307  break;
308  }
309  }
310 
311  // calculating total price
312  return $this->_oPrice;
313  }
314 
322  public function delete( $sOXID = null )
323  {
324  if ( !$sOXID ) {
325  $sOXID = $this->getId();
326  }
327  if ( !$sOXID ) {
328  return false;
329  }
330 
331 
332  $oDb = oxDb::getDb();
333  $sQ = "delete from oxobject2delivery where oxobject2delivery.oxdeliveryid = ".$oDb->quote($sOXID);
334  $oDb->execute( $sQ );
335 
336  return parent::delete( $sOXID );
337  }
338 
346  public function isForBasket( $oBasket )
347  {
348  // amount for conditional check
349  $blHasArticles = $this->hasArticles();
350  $blHasCategories = $this->hasCategories();
351  $blUse = true;
352  $iAmount = 0;
353  $blForBasket = false;
354 
355  // category & article check
356  if ( $blHasCategories || $blHasArticles ) {
357  $blUse = false;
358 
359  $aDeliveryArticles = $this->getArticles();
360  $aDeliveryCategories = $this->getCategories();
361 
362  foreach ( $oBasket->getContents() as $oContent ) {
363 
364  //V FS#1954 - load delivery for variants from parent article
365  $oArticle = $oContent->getArticle(false);
366  $sProductId = $oArticle->getProductId();
367  $sParentId = $oArticle->getProductParentId();
368 
369  if ( $blHasArticles && (in_array( $sProductId, $aDeliveryArticles ) || ( $sParentId && in_array( $sParentId, $aDeliveryArticles ) ) ) ) {
370  $blUse = true;
371  $iArtAmount = $this->getDeliveryAmount( $oContent );
372  if ( $this->oxdelivery__oxfixed->value > 0 ) {
373  if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
374  $blForBasket = true;
375  }
376  }
377  if (!$blForBasket) {
378  $iAmount += $iArtAmount;
379  }
380 
381  } elseif ( $blHasCategories ) {
382 
383  if ( isset( self::$_aProductList[$sProductId] ) ) {
384  $oProduct = self::$_aProductList[$sProductId];
385  } else {
386  $oProduct = oxNew( 'oxArticle' );
387  $oProduct->setSkipAssign( true );
388 
389  if ( !$oProduct->load( $sProductId ) ) {
390  continue;
391  }
392 
393  $oProduct->setId($sProductId);
394  self::$_aProductList[$sProductId] = $oProduct;
395  }
396 
397  foreach ( $aDeliveryCategories as $sCatId ) {
398 
399  if ( $oProduct->inCategory( $sCatId ) ) {
400  $blUse = true;
401  $iArtAmount = $this->getDeliveryAmount( $oContent );
402  if ( $this->oxdelivery__oxfixed->value > 0 ) {
403  if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
404  $blForBasket = true;
405  }
406  }
407  if (!$blForBasket) {
408  $iAmount += $iArtAmount;
409  }
410  }
411  }
412 
413  }
414  }
415  } else {
416  // regular amounts check
417  foreach ( $oBasket->getContents() as $oContent ) {
418  $iArtAmount = $this->getDeliveryAmount( $oContent );
419  if ( $this->oxdelivery__oxfixed->value > 0 ) {
420  if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
421  $blForBasket = true;
422  }
423  }
424  if (!$blForBasket) {
425  $iAmount += $iArtAmount;
426  }
427  }
428  }
429 
430  //#M1130: Single article in Basket, checked as free shipping, is not buyable (step 3 no payments found)
431  if ( !$blForBasket && $blUse && ( $this->_checkDeliveryAmount($iAmount) || $this->_blFreeShipping ) ) {
432  $blForBasket = true;
433  }
434  return $blForBasket;
435  }
436 
445  protected function _isForArticle( $oContent, $iArtAmount )
446  {
447  if ( !$this->_blFreeShipping && $this->_checkDeliveryAmount($iArtAmount) ) {
448  $this->_iItemCnt += $oContent->getAmount();
449  $this->_iProdCnt += 1;
450  return true;
451  }
452  return false;
453  }
454 
462  protected function _checkDeliveryAmount($iAmount)
463  {
464  switch ( $this->oxdelivery__oxdeltype->value ) {
465  case 'p': // price
466  $oCur = $this->getConfig()->getActShopCurrencyObject();
467  $iAmount /= $oCur->rate;
468  break;
469  case 'w': // weight
470  case 's': // size
471  case 'a': // amount
472  break;
473  }
474 
475  if ( $iAmount >= $this->oxdelivery__oxparam->value && $iAmount <= $this->oxdelivery__oxparamend->value ) {
476  return true;
477  }
478 
479  return false;
480  }
481 
489  public function getIdByName( $sTitle )
490  {
491  $oDb = oxDb::getDb();
492  $sQ = "SELECT `oxid` FROM `" . getViewName( 'oxdelivery' ) . "` WHERE `oxtitle` = " . $oDb->quote( $sTitle );
493  $sId = $oDb->getOne( $sQ );
494 
495  return $sId;
496  }
497 
503  public function getCountriesISO()
504  {
505  if ( $this->_aCountriesISO === null ) {
506  $oDb = oxDb::getDb();
507  $this->_aCountriesISO = array();
508  $sSelect = 'select oxcountry.oxisoalpha2 from oxcountry left join oxobject2delivery on oxobject2delivery.oxobjectid = oxcountry.oxid where oxobject2delivery.oxdeliveryid='.$oDb->quote( $this->getId() ).' and oxobject2delivery.oxtype = "oxcountry" ';
509  $rs = $oDb->select( $sSelect );
510  if ( $rs && $rs->recordCount()) {
511  while ( !$rs->EOF ) {
512  $this->_aCountriesISO[] = $rs->fields[0];
513  $rs->moveNext();
514  }
515  }
516  }
517  return $this->_aCountriesISO;
518  }
519 
520 }