oxdelivery.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxDelivery extends oxI18n
00008 {
00014     protected $_sCoreTbl = 'oxdelivery';
00015 
00021     protected $_sClassName = 'oxdelivery';
00022 
00029     protected $_iItemCnt = 0;
00030 
00037     protected $_iProdCnt = 0;
00038 
00045     protected $_dPrice = 0;
00046 
00052     protected $_oPrice = null;
00053 
00059     protected $_aArtIds = null;
00060 
00066     protected $_aCatIds = null;
00067 
00073     protected $_blFreeShipping = true;
00074 
00080     protected static $_aProductList = array();
00081 
00087     protected $_blDelVatOnTop = false;
00088 
00092     public function __construct()
00093     {
00094         parent::__construct();
00095         $this->init( 'oxdelivery' );
00096         $this->setDelVatOnTop( $this->getConfig()->getConfigParam( 'blDeliveryVatOnTop' ) );
00097     }
00098 
00106     public function setDelVatOnTop( $blOnTop )
00107     {
00108         $this->_blDelVatOnTop = $blOnTop;
00109     }
00110 
00116     public function getArticles()
00117     {
00118         if ( $this->_aArtIds !== null ) {
00119             return $this->_aArtIds;
00120         }
00121 
00122         $oDb = oxDb::getDb();
00123         $sQ = "select oxobjectid from oxobject2delivery where oxdeliveryid=".$oDb->quote($this->getId())." and oxtype = 'oxarticles'";
00124         $aArtIds = $oDb->getArray( $sQ );
00125 
00126         //make single dimension array
00127         foreach ( $aArtIds as $aItem ) {
00128             $this->_aArtIds[] = $aItem[0];
00129         }
00130 
00131         return $this->_aArtIds;
00132 
00133     }
00134 
00140     public function getCategories()
00141     {
00142         if ( $this->_aCatIds !== null ) {
00143             return $this->_aCatIds;
00144         }
00145 
00146         $oDb = oxDb::getDb();
00147         $sQ = "select oxobjectid from oxobject2delivery where oxdeliveryid=".$oDb->quote($this->getId())." and oxtype = 'oxcategories'";
00148         $aCatIds = $oDb->getAll( $sQ );
00149 
00150         //make single dimension array
00151         foreach ( $aCatIds AS $aItem ) {
00152             $this->_aCatIds[] = $aItem[0];
00153         }
00154 
00155         return $this->_aCatIds;
00156     }
00157 
00165     public function hasArtices()
00166     {
00167         return ( bool ) count( $this->getArticles() );
00168     }
00169 
00175     public function hasArticles()
00176     {
00177         return ( bool ) count( $this->getArticles() );
00178     }
00179 
00185     public function hasCategories()
00186     {
00187         return ( bool ) count( $this->getCategories() );
00188     }
00189 
00197     public function getDeliveryAmount( $oBasketItem )
00198     {
00199         $dAmount  = 0;
00200         $oProduct = $oBasketItem->getArticle( false );
00201 
00202         // mark free shipping products
00203         if ( $oProduct->oxarticles__oxfreeshipping->value ) {
00204             if ($this->_blFreeShipping !== false) {
00205                 $this->_blFreeShipping = true;
00206             }
00207         } else {
00208 
00209             $blExclNonMaterial = $this->getConfig()->getConfigParam( 'blExclNonMaterialFromDelivery' );
00210             if ( !( $oProduct->oxarticles__oxnonmaterial->value && $blExclNonMaterial ) ) {
00211                 $this->_blFreeShipping = false;
00212             }
00213 
00214             switch ( $this->oxdelivery__oxdeltype->value ) {
00215                 case 'p': // price
00216                     if ( $this->oxdelivery__oxfixed->value == 2 ) {
00217                         $dAmount += $oProduct->getPrice()->getBruttoPrice();
00218                     } else {
00219                         $dAmount += $oBasketItem->getPrice()->getBruttoPrice(); // price// currency conversion must allready be done in price class / $oCur->rate; // $oBasketItem->oPrice->getPrice() / $oCur->rate;
00220                     }
00221                     break;
00222                 case 'w': // weight
00223                     if ( $this->oxdelivery__oxfixed->value == 2 ) {
00224                         $dAmount += $oProduct->oxarticles__oxweight->value;
00225                     } else {
00226                         $dAmount += $oBasketItem->getWeight();
00227                     }
00228                     break;
00229                 case 's': // size
00230                     $dAmount += $oProduct->oxarticles__oxlength->value *
00231                                 $oProduct->oxarticles__oxwidth->value *
00232                                 $oProduct->oxarticles__oxheight->value;
00233                     if ( $this->oxdelivery__oxfixed->value < 2 ) {
00234                         $dAmount *= $oBasketItem->getAmount();
00235                     }
00236                     break;
00237                 case 'a': // amount
00238                     $dAmount += $oBasketItem->getAmount();
00239                     break;
00240             }
00241 
00242             if ( $oBasketItem->getPrice() ) {
00243                 $this->_dPrice   += $oBasketItem->getPrice()->getBruttoPrice();
00244             }
00245         }
00246 
00247         return $dAmount;
00248     }
00249 
00257     public function setDeliveryPrice( $oPrice )
00258     {
00259         $this->_oPrice = $oPrice;
00260     }
00261 
00269     public function getDeliveryPrice( $dVat = null )
00270     {
00271         if ( $this->_oPrice === null ) {
00272             // loading oxprice object for final price calculation
00273             $this->_oPrice = oxNew( 'oxPrice' );
00274 
00275             if ( !$this->_blDelVatOnTop ) {
00276                 $this->_oPrice->setBruttoPriceMode();
00277             } else {
00278                 $this->_oPrice->setNettoPriceMode();
00279             }
00280 
00281             $this->_oPrice->setVat( $dVat );
00282 
00283             // if article is free shipping, price for delivery will be not calculated
00284             if ( $this->_blFreeShipping ) {
00285                 return $this->_oPrice;
00286             }
00287 
00288             // calculating base price value
00289             switch ( $this->oxdelivery__oxaddsumtype->value ) {
00290                 case 'abs':
00291 
00292                     $dAmount = 0;
00293 
00294                     if ( $this->oxdelivery__oxfixed->value == 0 ) {
00295                         // 1. Once per Cart
00296                         $dAmount = 1;
00297                     } elseif ( $this->oxdelivery__oxfixed->value == 1 ) {
00298                         // 2. Once per Product overall
00299                         $dAmount = $this->_iProdCnt;
00300                     } elseif ( $this->oxdelivery__oxfixed->value == 2 ) {
00301                         // 3. Once per Product in Cart
00302                         $dAmount = $this->_iItemCnt;
00303                     }
00304 
00305                     $oCur = $this->getConfig()->getActShopCurrencyObject();
00306                     $this->_oPrice->add( $this->oxdelivery__oxaddsum->value * $oCur->rate );
00307                     $this->_oPrice->multiply( $dAmount );
00308                     break;
00309                 case '%':
00310 
00311                     $this->_oPrice->add( $this->_dPrice /100 * $this->oxdelivery__oxaddsum->value );
00312                     break;
00313             }
00314         }
00315 
00316         // calculating total price
00317         return $this->_oPrice;
00318     }
00319 
00327     public function delete( $sOXID = null )
00328     {
00329         if ( !$sOXID ) {
00330             $sOXID = $this->getId();
00331         }
00332         if ( !$sOXID ) {
00333             return false;
00334         }
00335 
00336 
00337         $oDb = oxDb::getDb();
00338         $sQ = "delete from oxobject2delivery where oxobject2delivery.oxdeliveryid = ".$oDb->quote($sOXID);
00339         $oDb->execute( $sQ );
00340 
00341         return parent::delete( $sOXID );
00342     }
00343 
00351     public function isForBasket( $oBasket )
00352     {
00353         // amount for conditional check
00354         $blHasArticles   = $this->hasArticles();
00355         $blHasCategories = $this->hasCategories();
00356         $blUse = true;
00357         $iAmount = 0;
00358         $blForBasket = false;
00359 
00360         // category & article check
00361         if ( $blHasCategories || $blHasArticles ) {
00362             $blUse = false;
00363 
00364             $aDeliveryArticles   = $this->getArticles();
00365             $aDeliveryCategories = $this->getCategories();
00366 
00367             foreach ( $oBasket->getContents() as $oContent ) {
00368 
00369                 //V FS#1954 - load delivery for variants from parent article
00370                 $oArticle   = $oContent->getArticle(false);
00371                 $sProductId = $oArticle->getProductId();
00372                 $sParentId  = $oArticle->getProductParentId();
00373 
00374                 if ( $blHasArticles && (in_array( $sProductId, $aDeliveryArticles ) || ( $sParentId && in_array( $sParentId, $aDeliveryArticles ) ) ) ) {
00375                     $blUse = true;
00376                     $iArtAmount = $this->getDeliveryAmount( $oContent );
00377                     if ( $this->oxdelivery__oxfixed->value > 0 ) {
00378                         if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00379                             $blForBasket = true;
00380                         }
00381                     }
00382                     if (!$blForBasket) {
00383                         $iAmount += $iArtAmount;
00384                     }
00385 
00386                 } elseif ( $blHasCategories ) {
00387 
00388                     if ( isset( self::$_aProductList[$sProductId] ) ) {
00389                         $oProduct = self::$_aProductList[$sProductId];
00390                     } else {
00391                         $oProduct = oxNew( 'oxarticle' );
00392                         $oProduct->setSkipAssign( true );
00393 
00394                         if ( !$oProduct->load( $sProductId ) ) {
00395                             continue;
00396                         }
00397 
00398                         $oProduct->setId($sProductId);
00399                         self::$_aProductList[$sProductId] = $oProduct;
00400                     }
00401 
00402                     foreach ( $aDeliveryCategories as $sCatId ) {
00403 
00404                         if ( $oProduct->inCategory( $sCatId ) ) {
00405                             $blUse = true;
00406 
00407                             $iArtAmount = $this->getDeliveryAmount( $oContent );
00408                             if ( $this->oxdelivery__oxfixed->value > 0 ) {
00409                                 if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00410                                     $blForBasket = true;
00411                                 }
00412                             }
00413 
00414                             break;
00415                         }
00416                     }
00417                     if (!$blForBasket) {
00418                         $iAmount += $iArtAmount;
00419                     }
00420                 }
00421             }
00422 
00423         } else {
00424             // regular amounts check
00425             foreach ( $oBasket->getContents() as $oContent ) {
00426                 $iArtAmount = $this->getDeliveryAmount( $oContent );
00427                 if ( $this->oxdelivery__oxfixed->value > 0 ) {
00428                     if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00429                         $blForBasket = true;
00430                     }
00431                 }
00432                 if (!$blForBasket) {
00433                     $iAmount += $iArtAmount;
00434                 }
00435             }
00436         }
00437 
00438         //#M1130: Single article in Basket, checked as free shipping, is not buyable (step 3 no payments found)
00439         if ( !$blForBasket && $blUse && ( $this->_checkDeliveryAmount($iAmount) || $this->_blFreeShipping ) ) {
00440             $blForBasket = true;
00441         }
00442         return $blForBasket;
00443     }
00444 
00453     protected function _isForArticle( $oContent, $iArtAmount )
00454     {
00455         if ( !$this->_blFreeShipping && $this->_checkDeliveryAmount($iArtAmount) ) {
00456             $this->_iItemCnt += $oContent->getAmount();
00457             $this->_iProdCnt += 1;
00458             return true;
00459         }
00460         return false;
00461     }
00462 
00470     protected function _checkDeliveryAmount($iAmount)
00471     {
00472         switch ( $this->oxdelivery__oxdeltype->value ) {
00473             case 'p': // price
00474                 $oCur = $this->getConfig()->getActShopCurrencyObject();
00475                 $iAmount /= $oCur->rate;
00476                 break;
00477             case 'w': // weight
00478             case 's': // size
00479             case 'a': // amount
00480                 break;
00481         }
00482 
00483         if ( $iAmount >= $this->oxdelivery__oxparam->value && $iAmount <= $this->oxdelivery__oxparamend->value ) {
00484             return true;
00485         }
00486 
00487         return false;
00488     }
00489 
00497     public function getIdByName( $sTitle )
00498     {
00499         $sQ = "SELECT `oxid` FROM `" . getViewName( 'oxdelivery' ) . "` WHERE  `oxtitle` = " . oxDb::getDb()->quote( $sTitle );
00500         $sId = oxDb::getDb()->getOne( $sQ );
00501 
00502         return $sId;
00503     }
00504 }