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             $this->_blFreeShipping = true;
00205         } else {
00206 
00207             $blExclNonMaterial = $this->getConfig()->getConfigParam( 'blExclNonMaterialFromDelivery' );
00208             if ( !( $oProduct->oxarticles__oxnonmaterial->value && $blExclNonMaterial ) ) {
00209                 $this->_blFreeShipping = false;
00210             }
00211 
00212             switch ( $this->oxdelivery__oxdeltype->value ) {
00213                 case 'p': // price
00214                     if ( $this->oxdelivery__oxfixed->value == 2 ) {
00215                         $dAmount += $oProduct->getPrice()->getBruttoPrice();
00216                     } else {
00217                         $dAmount += $oBasketItem->getPrice()->getBruttoPrice(); // price// currency conversion must allready be done in price class / $oCur->rate; // $oBasketItem->oPrice->getPrice() / $oCur->rate;
00218                     }
00219                     break;
00220                 case 'w': // weight
00221                     if ( $this->oxdelivery__oxfixed->value == 2 ) {
00222                         $dAmount += $oProduct->oxarticles__oxweight->value;
00223                     } else {
00224                         $dAmount += $oBasketItem->getWeight();
00225                     }
00226                     break;
00227                 case 's': // size
00228                     $dAmount += $oProduct->oxarticles__oxlength->value *
00229                                 $oProduct->oxarticles__oxwidth->value *
00230                                 $oProduct->oxarticles__oxheight->value;
00231                     if ( $this->oxdelivery__oxfixed->value < 2 ) {
00232                         $dAmount *= $oBasketItem->getAmount();
00233                     }
00234                     break;
00235                 case 'a': // amount
00236                     $dAmount += $oBasketItem->getAmount();
00237                     break;
00238             }
00239 
00240             if ( $oBasketItem->getPrice() ) {
00241                 $this->_dPrice   += $oBasketItem->getPrice()->getBruttoPrice();
00242             }
00243         }
00244 
00245         return $dAmount;
00246     }
00247 
00255     public function setDeliveryPrice( $oPrice )
00256     {
00257         $this->_oPrice = $oPrice;
00258     }
00259 
00267     public function getDeliveryPrice( $dVat = null )
00268     {
00269         if ( $this->_oPrice === null ) {
00270             // loading oxprice object for final price calculation
00271             $this->_oPrice = oxNew( 'oxPrice' );
00272 
00273             if ( !$this->_blDelVatOnTop ) {
00274                 $this->_oPrice->setBruttoPriceMode();
00275             } else {
00276                 $this->_oPrice->setNettoPriceMode();
00277             }
00278 
00279             $this->_oPrice->setVat( $dVat );
00280 
00281             // if article is free shipping, price for delivery will be not calculated
00282             if ( $this->_blFreeShipping ) {
00283                 return $this->_oPrice;
00284             }
00285 
00286             // calculating base price value
00287             switch ( $this->oxdelivery__oxaddsumtype->value ) {
00288                 case 'abs':
00289 
00290                     $dAmount = 0;
00291 
00292                     if ( $this->oxdelivery__oxfixed->value == 0 ) {
00293                         // 1. Once per Cart
00294                         $dAmount = 1;
00295                     } elseif ( $this->oxdelivery__oxfixed->value == 1 ) {
00296                         // 2. Once per Product overall
00297                         $dAmount = $this->_iProdCnt;
00298                     } elseif ( $this->oxdelivery__oxfixed->value == 2 ) {
00299                         // 3. Once per Product in Cart
00300                         $dAmount = $this->_iItemCnt;
00301                     }
00302 
00303                     $oCur = $this->getConfig()->getActShopCurrencyObject();
00304                     $this->_oPrice->add( $this->oxdelivery__oxaddsum->value * $oCur->rate );
00305                     $this->_oPrice->multiply( $dAmount );
00306                     break;
00307                 case '%':
00308 
00309                     $this->_oPrice->add( $this->_dPrice /100 * $this->oxdelivery__oxaddsum->value );
00310                     break;
00311             }
00312         }
00313 
00314         // calculating total price
00315         return $this->_oPrice;
00316     }
00317 
00325     public function delete( $sOXID = null )
00326     {
00327         if ( !$sOXID ) {
00328             $sOXID = $this->getId();
00329         }
00330         if ( !$sOXID ) {
00331             return false;
00332         }
00333 
00334 
00335         $oDb = oxDb::getDb();
00336         $sQ = "delete from oxobject2delivery where oxobject2delivery.oxdeliveryid = ".$oDb->quote($sOXID);
00337         $oDb->execute( $sQ );
00338 
00339         return parent::delete( $sOXID );
00340     }
00341 
00349     public function isForBasket( $oBasket )
00350     {
00351         // amount for conditional check
00352         $blHasArticles   = $this->hasArticles();
00353         $blHasCategories = $this->hasCategories();
00354         $blUse = true;
00355         $iAmount = 0;
00356         $blForBasket = false;
00357 
00358         // category & article check
00359         if ( $blHasCategories || $blHasArticles ) {
00360             $blUse = false;
00361 
00362             $aDeliveryArticles   = $this->getArticles();
00363             $aDeliveryCategories = $this->getCategories();
00364 
00365             foreach ( $oBasket->getContents() as $oContent ) {
00366 
00367                 //V FS#1954 - load delivery for variants from parent article
00368                 $oArticle   = $oContent->getArticle(false);
00369                 $sProductId = $oArticle->getProductId();
00370                 $sParentId  = $oArticle->getProductParentId();
00371 
00372                 if ( $blHasArticles && (in_array( $sProductId, $aDeliveryArticles ) || ( $sParentId && in_array( $sParentId, $aDeliveryArticles ) ) ) ) {
00373                     $blUse = true;
00374                     $iArtAmount = $this->getDeliveryAmount( $oContent );
00375                     if ( $this->oxdelivery__oxfixed->value > 0 ) {
00376                         if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00377                             $blForBasket = true;
00378                         }
00379                     }
00380                     if (!$blForBasket) {
00381                         $iAmount += $iArtAmount;
00382                     }
00383 
00384                 } elseif ( $blHasCategories ) {
00385 
00386                     if ( isset( self::$_aProductList[$sProductId] ) ) {
00387                         $oProduct = self::$_aProductList[$sProductId];
00388                     } else {
00389                         $oProduct = oxNew( 'oxarticle' );
00390                         $oProduct->setSkipAssign( true );
00391 
00392                         if ( !$oProduct->load( $sProductId ) ) {
00393                             continue;
00394                         }
00395 
00396                         $oProduct->setId($sProductId);
00397                         self::$_aProductList[$sProductId] = $oProduct;
00398                     }
00399 
00400                     foreach ( $aDeliveryCategories as $sCatId ) {
00401 
00402                         if ( $oProduct->inCategory( $sCatId ) ) {
00403                             $blUse = true;
00404 
00405                             $iArtAmount = $this->getDeliveryAmount( $oContent );
00406                             if ( $this->oxdelivery__oxfixed->value > 0 ) {
00407                                 if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00408                                     $blForBasket = true;
00409                                 }
00410                             }
00411 
00412                             break;
00413                         }
00414                     }
00415                     if (!$blForBasket) {
00416                         $iAmount += $iArtAmount;
00417                     }
00418                 }
00419             }
00420 
00421         } else {
00422             // regular amounts check
00423             foreach ( $oBasket->getContents() as $oContent ) {
00424                 $iArtAmount = $this->getDeliveryAmount( $oContent );
00425                 if ( $this->oxdelivery__oxfixed->value > 0 ) {
00426                     if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00427                         $blForBasket = true;
00428                     }
00429                 }
00430                 if (!$blForBasket) {
00431                     $iAmount += $iArtAmount;
00432                 }
00433             }
00434         }
00435 
00436         //#M1130: Single article in Basket, checked as free shipping, is not buyable (step 3 no payments found)
00437         if ( !$blForBasket && $blUse && ( $this->_checkDeliveryAmount($iAmount) || $this->_blFreeShipping ) ) {
00438             $blForBasket = true;
00439         }
00440         return $blForBasket;
00441     }
00442 
00451     protected function _isForArticle( $oContent, $iArtAmount )
00452     {
00453         if ( !$this->_blFreeShipping && $this->_checkDeliveryAmount($iArtAmount) ) {
00454             $this->_iItemCnt += $oContent->getAmount();
00455             $this->_iProdCnt += 1;
00456             return true;
00457         }
00458         return false;
00459     }
00460 
00468     protected function _checkDeliveryAmount($iAmount)
00469     {
00470         switch ( $this->oxdelivery__oxdeltype->value ) {
00471             case 'p': // price
00472                 $oCur = $this->getConfig()->getActShopCurrencyObject();
00473                 $iAmount /= $oCur->rate;
00474                 break;
00475             case 'w': // weight
00476             case 's': // size
00477             case 'a': // amount
00478                 break;
00479         }
00480 
00481         if ( $iAmount >= $this->oxdelivery__oxparam->value && $iAmount <= $this->oxdelivery__oxparamend->value ) {
00482             return true;
00483         }
00484 
00485         return false;
00486     }
00487 
00495     public function getIdByName( $sTitle )
00496     {
00497         $sQ = "SELECT `oxid` FROM `" . getViewName( 'oxdelivery' ) . "` WHERE  `oxtitle` = " . oxDb::getDb()->quote( $sTitle );
00498         $sId = oxDb::getDb()->getOne( $sQ );
00499 
00500         return $sId;
00501     }
00502 }