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