OXID eShop CE  4.9.5
 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  }
380  }
381 
382  }
383  }
384  } else {
385  // regular amounts check
386  foreach ($oBasket->getContents() as $oContent) {
387  $iArtAmount = $this->getDeliveryAmount($oContent);
388  if ($this->getCalculationRule() != self::CALCULATION_RULE_ONCE_PER_CART) {
389  if ($this->_isForArticle($oContent, $iArtAmount)) {
390  $blForBasket = true;
391  }
392  }
393  if (!$blForBasket) {
394  $iAmount += $iArtAmount;
395  }
396  }
397  }
398 
399  /* if ( $this->getConditionType() == self::CONDITION_TYPE_PRICE ) {
400  $iAmount = $oBasket->_getDiscountedProductsSum();
401  }*/
402 
403  //#M1130: Single article in Basket, checked as free shipping, is not buyable (step 3 no payments found)
404  if (!$blForBasket && $blUse && ($this->_checkDeliveryAmount($iAmount) || $this->_blFreeShipping)) {
405  $blForBasket = true;
406  }
407 
408  return $blForBasket;
409  }
410 
419  protected function _isForArticle($oContent, $iArtAmount)
420  {
421  $blResult = false;
422  if (!$this->_blFreeShipping && $this->_checkDeliveryAmount($iArtAmount)) {
423  $this->_iItemCnt += $oContent->getAmount();
424  $this->_iProdCnt += 1;
425  $blResult = true;
426  }
427 
428  return $blResult;
429  }
430 
438  protected function _checkDeliveryAmount($iAmount)
439  {
440  $blResult = false;
441 
442  if ($this->getConditionType() == self::CONDITION_TYPE_PRICE) {
443  $oCur = $this->getConfig()->getActShopCurrencyObject();
444  $iAmount /= $oCur->rate;
445  }
446 
447  if ($iAmount >= $this->getConditionFrom() && $iAmount <= $this->getConditionTo()) {
448  $blResult = true;
449  }
450 
451  return $blResult;
452  }
453 
461  public function getIdByName($sTitle)
462  {
463  $oDb = oxDb::getDb();
464  $sQ = "SELECT `oxid` FROM `" . getViewName('oxdelivery') . "` WHERE `oxtitle` = " . $oDb->quote($sTitle);
465  $sId = $oDb->getOne($sQ);
466 
467  return $sId;
468  }
469 
475  public function getCountriesISO()
476  {
477  if ($this->_aCountriesISO === null) {
478 
479  $oDb = oxDb::getDb();
480  $this->_aCountriesISO = array();
481 
482  $sSelect = "
483  SELECT
484  `oxcountry`.`oxisoalpha2`
485  FROM `oxcountry`
486  LEFT JOIN `oxobject2delivery` ON `oxobject2delivery`.`oxobjectid` = `oxcountry`.`oxid`
487  WHERE `oxobject2delivery`.`oxdeliveryid` = " . $oDb->quote($this->getId()) . "
488  AND `oxobject2delivery`.`oxtype` = 'oxcountry'";
489 
490  $rs = $oDb->getCol($sSelect);
491  $this->_aCountriesISO = $rs;
492 
493  }
494 
495  return $this->_aCountriesISO;
496  }
497 
503  public function getConditionType()
504  {
505  return $this->oxdelivery__oxdeltype->value;
506  }
507 
513  public function getConditionFrom()
514  {
515  return $this->oxdelivery__oxparam->value;
516  }
517 
523  public function getConditionTo()
524  {
525  return $this->oxdelivery__oxparamend->value;
526  }
527 
533  public function getCalculationRule()
534  {
535  return $this->oxdelivery__oxfixed->value;
536  }
537 
543  public function getAddSum()
544  {
545  return $this->oxdelivery__oxaddsum->value;
546  }
547 
553  public function getAddSumType()
554  {
555  return $this->oxdelivery__oxaddsumtype->value;
556  }
557 
563  protected function _getMultiplier()
564  {
565  $dAmount = 0;
566 
567  if ($this->getCalculationRule() == self::CALCULATION_RULE_ONCE_PER_CART) {
568  $dAmount = 1;
569  } elseif ($this->getCalculationRule() == self::CALCULATION_RULE_FOR_EACH_DIFFERENT_PRODUCT) {
570  $dAmount = $this->_iProdCnt;
571  } elseif ($this->getCalculationRule() == self::CALCULATION_RULE_FOR_EACH_PRODUCT) {
572  $dAmount = $this->_iItemCnt;
573  }
574 
575  return $dAmount;
576  }
577 
583  protected function _getCostSum()
584  {
585  if ($this->getAddSumType() == 'abs') {
586  $oCur = $this->getConfig()->getActShopCurrencyObject();
587  $dPrice = $this->getAddSum() * $oCur->rate * $this->_getMultiplier();
588  } else {
589  $dPrice = $this->_dPrice / 100 * $this->getAddSum();
590  }
591 
592  return $dPrice;
593  }
594 }