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         $sQ = 'select oxobjectid from oxobject2delivery where oxdeliveryid="'.$this->getId().'" and oxtype = "oxarticles" ';
00123 
00124         $aArtIds = oxDb::getDb()->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         $sQ = 'select oxobjectid from oxobject2delivery where oxdeliveryid="'.$this->getId().'" and oxtype = "oxcategories" ';
00147 
00148         $aCatIds = oxDb::getDb()->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                     } else {
00376                         $iAmount += $iArtAmount;
00377                     }
00378 
00379                 } elseif ( $blHasCategories ) {
00380 
00381                     if ( isset( self::$_aProductList[$sProductId] ) ) {
00382                         $oProduct = self::$_aProductList[$sProductId];
00383                     } else {
00384                         $oProduct = oxNew( 'oxarticle' );
00385                         $oProduct->setSkipAssign( true );
00386 
00387                         if ( !$oProduct->load( $sProductId ) ) {
00388                             continue;
00389                         }
00390 
00391                         $oProduct->setId($sProductId);
00392                         self::$_aProductList[$sProductId] = $oProduct;
00393                     }
00394 
00395                     foreach ( $aDeliveryCategories as $sCatId ) {
00396 
00397                         if ( $oProduct->inCategory( $sCatId ) ) {
00398                             $blUse = true;
00399 
00400                             $iArtAmount = $this->getDeliveryAmount( $oContent );
00401                             if ( $this->oxdelivery__oxfixed->value > 0 ) {
00402                                 if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00403                                     $blForBasket = true;
00404                                 }
00405                             } else {
00406                                 $iAmount += $iArtAmount;
00407                             }
00408 
00409                             break;
00410                         }
00411                     }
00412                 }
00413             }
00414 
00415         } else {
00416             // regular amounts check
00417             foreach ( $oBasket->getContents() as $oContent ) {
00418                 $iArtAmount = $this->getDeliveryAmount( $oContent );
00419                 if ( $this->oxdelivery__oxfixed->value > 0 ) {
00420                     if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00421                         $blForBasket = true;
00422                     }
00423                 } else {
00424                     $iAmount += $iArtAmount;
00425                 }
00426             }
00427         }
00428 
00429         //#M1130: Single article in Basket, checked as free shipping, is not buyable (step 3 no payments found)
00430         if ( !$blForBasket && $blUse && ( $this->_checkDeliveryAmount($iAmount) || $this->_blFreeShipping ) ) {
00431             $blForBasket = true;
00432         }
00433         return $blForBasket;
00434     }
00435 
00444     protected function _isForArticle( $oContent, $iArtAmount )
00445     {
00446         if ( !$this->_blFreeShipping && $this->_checkDeliveryAmount($iArtAmount) ) {
00447             $this->_iItemCnt += $oContent->getAmount();
00448             $this->_iProdCnt += 1;
00449             return true;
00450         }
00451         return false;
00452     }
00453 
00461     protected function _checkDeliveryAmount($iAmount)
00462     {
00463         switch ( $this->oxdelivery__oxdeltype->value ) {
00464             case 'p': // price
00465                 $oCur = $this->getConfig()->getActShopCurrencyObject();
00466                 $iAmount /= $oCur->rate;
00467                 break;
00468             case 'w': // weight
00469             case 's': // size
00470             case 'a': // amount
00471                 break;
00472         }
00473 
00474         if ( $iAmount >= $this->oxdelivery__oxparam->value && $iAmount <= $this->oxdelivery__oxparamend->value ) {
00475             return true;
00476         }
00477 
00478         return false;
00479     }
00480 }