OXID eShop CE  4.9.7
 All Classes Files Functions Variables Pages
oxdelivery.php
Go to the documentation of this file.
1 <?php
2 
8 class oxDelivery extends oxI18n
9 {
10 
17 
21  const CONDITION_TYPE_PRICE = 'p';
22  const CONDITION_TYPE_AMOUNT = 'a';
23  const CONDITION_TYPE_SIZE = 's';
24  const CONDITION_TYPE_WEIGHT = 'w';
25 
31  protected $_sClassName = 'oxdelivery';
32 
39  protected $_iItemCnt = 0;
40 
47  protected $_iProdCnt = 0;
48 
55  protected $_dPrice = 0;
56 
62  protected $_oPrice = null;
63 
69  protected $_aArtIds = null;
70 
76  protected $_aCatIds = null;
77 
83  protected $_blFreeShipping = true;
84 
90  protected static $_aProductList = array();
91 
97  protected $_blDelVatOnTop = false;
98 
104  protected $_aCountriesISO = null;
105 
111  protected $_aRDFaDeliverySet = null;
112 
116  public function __construct()
117  {
119  $this->init('oxdelivery');
120  $this->setDelVatOnTop($this->getConfig()->getConfigParam('blDeliveryVatOnTop'));
121  }
122 
128  public function setDelVatOnTop($blOnTop)
129  {
130  $this->_blDelVatOnTop = $blOnTop;
131  }
132 
138  public function getArticles()
139  {
140  if (is_null($this->_aArtIds)) {
141  $oDb = oxDb::getDb();
142  $sQ = "select oxobjectid from oxobject2delivery where oxdeliveryid=" . $oDb->quote($this->getId()) . " and oxtype = 'oxarticles'";
143  $aArtIds = $oDb->getCol($sQ);
144  $this->_aArtIds = $aArtIds;
145  }
146 
147  return $this->_aArtIds;
148  }
149 
155  public function getCategories()
156  {
157  if (is_null($this->_aCatIds)) {
158  $oDb = oxDb::getDb();
159  $sQ = "select oxobjectid from oxobject2delivery where oxdeliveryid=" . $oDb->quote($this->getId()) . " and oxtype = 'oxcategories'";
160  $aCatIds = $oDb->getCol($sQ);
161  $this->_aCatIds = $aCatIds;
162  }
163 
164  return $this->_aCatIds;
165  }
166 
172  public function hasArticles()
173  {
174  return ( bool ) count($this->getArticles());
175  }
176 
182  public function hasCategories()
183  {
184  return ( bool ) count($this->getCategories());
185  }
186 
194  public function getDeliveryAmount($oBasketItem)
195  {
196  $dAmount = 0;
197  $oProduct = $oBasketItem->getArticle(false);
198 
199  if ($oProduct->isOrderArticle()) {
200  $oProduct = $oProduct->getArticle();
201  }
202 
203  $blExclNonMaterial = $this->getConfig()->getConfigParam('blExclNonMaterialFromDelivery');
204 
205  // mark free shipping products
206  if ($oProduct->oxarticles__oxfreeshipping->value || ($oProduct->oxarticles__oxnonmaterial->value && $blExclNonMaterial)) {
207  if ($this->_blFreeShipping !== false) {
208  $this->_blFreeShipping = true;
209  }
210  } else {
211 
212  $this->_blFreeShipping = false;
213 
214  switch ($this->getConditionType()) {
215  case self::CONDITION_TYPE_PRICE: // price
216  if ($this->getCalculationRule() == self::CALCULATION_RULE_FOR_EACH_PRODUCT) {
217  $dAmount += $oProduct->getPrice()->getPrice();
218  } else {
219  $dAmount += $oBasketItem->getPrice()->getPrice(); // price// currency conversion must allready be done in price class / $oCur->rate; // $oBasketItem->oPrice->getPrice() / $oCur->rate;
220  }
221  break;
222  case self::CONDITION_TYPE_WEIGHT: // weight
223  if ($this->getCalculationRule() == self::CALCULATION_RULE_FOR_EACH_PRODUCT) {
224  $dAmount += $oProduct->getWeight();
225  } else {
226  $dAmount += $oBasketItem->getWeight();
227  }
228  break;
229  case self::CONDITION_TYPE_SIZE: // size
230  $dAmount += $oProduct->getSize();
231  if ($this->getCalculationRule() != self::CALCULATION_RULE_FOR_EACH_PRODUCT) {
232  $dAmount *= $oBasketItem->getAmount();
233  }
234  break;
235  case self::CONDITION_TYPE_AMOUNT: // amount
236  $dAmount += $oBasketItem->getAmount();
237  break;
238  }
239 
240  if ($oBasketItem->getPrice()) {
241  $this->_dPrice += $oBasketItem->getPrice()->getPrice();
242  }
243  }
244 
245  return $dAmount;
246  }
247 
253  public function setDeliveryPrice($oPrice)
254  {
255  $this->_oPrice = $oPrice;
256  }
257 
265  public function getDeliveryPrice($dVat = null)
266  {
267  if ($this->_oPrice === null) {
268 
269  // loading oxPrice object for final price calculation
270  $oPrice = oxNew('oxPrice');
271  $oPrice->setNettoMode($this->_blDelVatOnTop);
272  $oPrice->setVat($dVat);
273 
274  // if article is free shipping, price for delivery will be not calculated
275  if (!$this->_blFreeShipping) {
276  $oPrice->add($this->_getCostSum());
277  }
278  $this->setDeliveryPrice($oPrice);
279  }
280 
281  return $this->_oPrice;
282  }
283 
291  public function delete($sOxId = null)
292  {
293  if (!$sOxId) {
294  $sOxId = $this->getId();
295  }
296  if (!$sOxId) {
297  return false;
298  }
299 
300 
301  $oDb = oxDb::getDb();
302  $sQ = "delete from `oxobject2delivery` where `oxobject2delivery`.`oxdeliveryid` = " . $oDb->quote($sOxId);
303  $oDb->execute($sQ);
304 
305  return parent::delete($sOxId);
306  }
307 
315  public function isForBasket($oBasket)
316  {
317  // amount for conditional check
318  $blHasArticles = $this->hasArticles();
319  $blHasCategories = $this->hasCategories();
320  $blUse = true;
321  $iAmount = 0;
322  $blForBasket = false;
323 
324  // category & article check
325  if ($blHasCategories || $blHasArticles) {
326  $blUse = false;
327 
328  $aDeliveryArticles = $this->getArticles();
329  $aDeliveryCategories = $this->getCategories();
330 
331  foreach ($oBasket->getContents() as $oContent) {
332 
333  //V FS#1954 - load delivery for variants from parent article
334  $oArticle = $oContent->getArticle(false);
335  $sProductId = $oArticle->getProductId();
336  $sParentId = $oArticle->getParentId();
337 
338  if ($blHasArticles && (in_array($sProductId, $aDeliveryArticles) || ($sParentId && in_array($sParentId, $aDeliveryArticles)))) {
339  $blUse = true;
340  $iArtAmount = $this->getDeliveryAmount($oContent);
341  if ($this->getCalculationRule() != self::CALCULATION_RULE_ONCE_PER_CART) {
342  if ($this->_isForArticle($oContent, $iArtAmount)) {
343  $blForBasket = true;
344  }
345  }
346  if (!$blForBasket) {
347  $iAmount += $iArtAmount;
348  }
349 
350  } elseif ($blHasCategories) {
351 
352  if (isset(self::$_aProductList[$sProductId])) {
353  $oProduct = self::$_aProductList[$sProductId];
354  } else {
355  $oProduct = oxNew('oxArticle');
356  $oProduct->setSkipAssign(true);
357 
358  if (!$oProduct->load($sProductId)) {
359  continue;
360  }
361 
362  $oProduct->setId($sProductId);
363  self::$_aProductList[$sProductId] = $oProduct;
364  }
365 
366  foreach ($aDeliveryCategories as $sCatId) {
367 
368  if ($oProduct->inCategory($sCatId)) {
369  $blUse = true;
370  $iArtAmount = $this->getDeliveryAmount($oContent);
371  if ($this->getCalculationRule() != self::CALCULATION_RULE_ONCE_PER_CART) {
372  if ($this->_isForArticle($oContent, $iArtAmount)) {
373  $blForBasket = true;
374  }
375  }
376  if (!$blForBasket) {
377  $iAmount += $iArtAmount;
378  }
379  //HR#5650 product might be in multiple rule categories, counting it once is enough
380  break;
381  }
382  }
383 
384  }
385  }
386  } else {
387  // regular amounts check
388  foreach ($oBasket->getContents() as $oContent) {
389  $iArtAmount = $this->getDeliveryAmount($oContent);
390  if ($this->getCalculationRule() != self::CALCULATION_RULE_ONCE_PER_CART) {
391  if ($this->_isForArticle($oContent, $iArtAmount)) {
392  $blForBasket = true;
393  }
394  }
395  if (!$blForBasket) {
396  $iAmount += $iArtAmount;
397  }
398  }
399  }
400 
401  /* if ( $this->getConditionType() == self::CONDITION_TYPE_PRICE ) {
402  $iAmount = $oBasket->_getDiscountedProductsSum();
403  }*/
404 
405  //#M1130: Single article in Basket, checked as free shipping, is not buyable (step 3 no payments found)
406  if (!$blForBasket && $blUse && ($this->_checkDeliveryAmount($iAmount) || $this->_blFreeShipping)) {
407  $blForBasket = true;
408  }
409 
410  return $blForBasket;
411  }
412 
421  protected function _isForArticle($oContent, $iArtAmount)
422  {
423  $blResult = false;
424  if (!$this->_blFreeShipping && $this->_checkDeliveryAmount($iArtAmount)) {
425  $this->_iItemCnt += $oContent->getAmount();
426  $this->_iProdCnt += 1;
427  $blResult = true;
428  }
429 
430  return $blResult;
431  }
432 
440  protected function _checkDeliveryAmount($iAmount)
441  {
442  $blResult = false;
443 
444  if ($this->getConditionType() == self::CONDITION_TYPE_PRICE) {
445  $oCur = $this->getConfig()->getActShopCurrencyObject();
446  $iAmount /= $oCur->rate;
447  }
448 
449  if ($iAmount >= $this->getConditionFrom() && $iAmount <= $this->getConditionTo()) {
450  $blResult = true;
451  }
452 
453  return $blResult;
454  }
455 
463  public function getIdByName($sTitle)
464  {
465  $oDb = oxDb::getDb();
466  $sQ = "SELECT `oxid` FROM `" . getViewName('oxdelivery') . "` WHERE `oxtitle` = " . $oDb->quote($sTitle);
467  $sId = $oDb->getOne($sQ);
468 
469  return $sId;
470  }
471 
477  public function getCountriesISO()
478  {
479  if ($this->_aCountriesISO === null) {
480 
481  $oDb = oxDb::getDb();
482  $this->_aCountriesISO = array();
483 
484  $sSelect = "
485  SELECT
486  `oxcountry`.`oxisoalpha2`
487  FROM `oxcountry`
488  LEFT JOIN `oxobject2delivery` ON `oxobject2delivery`.`oxobjectid` = `oxcountry`.`oxid`
489  WHERE `oxobject2delivery`.`oxdeliveryid` = " . $oDb->quote($this->getId()) . "
490  AND `oxobject2delivery`.`oxtype` = 'oxcountry'";
491 
492  $rs = $oDb->getCol($sSelect);
493  $this->_aCountriesISO = $rs;
494 
495  }
496 
497  return $this->_aCountriesISO;
498  }
499 
505  public function getConditionType()
506  {
507  return $this->oxdelivery__oxdeltype->value;
508  }
509 
515  public function getConditionFrom()
516  {
517  return $this->oxdelivery__oxparam->value;
518  }
519 
525  public function getConditionTo()
526  {
527  return $this->oxdelivery__oxparamend->value;
528  }
529 
535  public function getCalculationRule()
536  {
537  return $this->oxdelivery__oxfixed->value;
538  }
539 
545  public function getAddSum()
546  {
547  return $this->oxdelivery__oxaddsum->value;
548  }
549 
555  public function getAddSumType()
556  {
557  return $this->oxdelivery__oxaddsumtype->value;
558  }
559 
565  protected function _getMultiplier()
566  {
567  $dAmount = 0;
568 
569  if ($this->getCalculationRule() == self::CALCULATION_RULE_ONCE_PER_CART) {
570  $dAmount = 1;
571  } elseif ($this->getCalculationRule() == self::CALCULATION_RULE_FOR_EACH_DIFFERENT_PRODUCT) {
572  $dAmount = $this->_iProdCnt;
573  } elseif ($this->getCalculationRule() == self::CALCULATION_RULE_FOR_EACH_PRODUCT) {
574  $dAmount = $this->_iItemCnt;
575  }
576 
577  return $dAmount;
578  }
579 
585  protected function _getCostSum()
586  {
587  if ($this->getAddSumType() == 'abs') {
588  $oCur = $this->getConfig()->getActShopCurrencyObject();
589  $dPrice = $this->getAddSum() * $oCur->rate * $this->_getMultiplier();
590  } else {
591  $dPrice = $this->_dPrice / 100 * $this->getAddSum();
592  }
593 
594  return $dPrice;
595  }
596 }