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 
00163     public function hasArtices()
00164     {
00165         return ( bool ) count( $this->getArticles() );
00166     }
00167 
00173     public function hasCategories()
00174     {
00175         return ( bool ) count( $this->getCategories() );
00176     }
00177 
00185     public function getDeliveryAmount( $oBasketItem )
00186     {
00187         $dAmount = 0 ;
00188 
00189         $blExclNonMaterial = $this->getConfig()->getConfigParam( 'blExclNonMaterialFromDelivery' );
00190         // calculating only the price which is for non free shipping products
00191         $oProduct = $oBasketItem->getArticle(false);
00192         if ( !$oProduct->oxarticles__oxfreeshipping->value &&
00193               !( $oProduct->oxarticles__oxnonmaterial->value && $blExclNonMaterial ) ) {
00194 
00195             $this->_blFreeShipping = false;
00196 
00197             switch ( $this->oxdelivery__oxdeltype->value ) {
00198                 case 'p': // price
00199                     if ( $this->oxdelivery__oxfixed->value == 2 ) {
00200                         $dAmount += $oBasketItem->getArticle()->getPrice()->getBruttoPrice();
00201                     } else {
00202                         $dAmount += $oBasketItem->getPrice()->getBruttoPrice(); // price// currency conversion must allready be done in price class / $oCur->rate; // $oBasketItem->oPrice->getPrice() / $oCur->rate;
00203                     }
00204                     break;
00205                 case 'w': // weight
00206                     if ( $this->oxdelivery__oxfixed->value == 2 ) {
00207                         $dAmount += $oBasketItem->getArticle()->oxarticles__oxweight->value;
00208                     } else {
00209                         $dAmount += $oBasketItem->getWeight();
00210                     }
00211                     break;
00212                 case 's': // size
00213                     $dAmount += $oProduct->oxarticles__oxlength->value *
00214                                 $oProduct->oxarticles__oxwidth->value *
00215                                 $oProduct->oxarticles__oxheight->value;
00216                     if ( $this->oxdelivery__oxfixed->value < 2 ) {
00217                         $dAmount *= $oBasketItem->getAmount();
00218                     }
00219                     break;
00220                 case 'a': // amount
00221                     $dAmount += $oBasketItem->getAmount();
00222                     break;
00223             }
00224 
00225             $this->_dPrice   += $oBasketItem->getPrice()->getBruttoPrice();
00226         }
00227 
00228         return $dAmount;
00229     }
00230 
00238     public function setDeliveryPrice( $oPrice )
00239     {
00240         $this->_oPrice = $oPrice;
00241     }
00242 
00250     public function getDeliveryPrice( $dVat = null )
00251     {
00252         if ( $this->_oPrice === null ) {
00253             // loading oxprice object for final price calculation
00254             $this->_oPrice = oxNew( 'oxPrice' );
00255 
00256             if ( !$this->_blDelVatOnTop ) {
00257                 $this->_oPrice->setBruttoPriceMode();
00258             } else {
00259                 $this->_oPrice->setNettoPriceMode();
00260             }
00261 
00262             $this->_oPrice->setVat( $dVat );
00263 
00264             // if article is free shipping, price for delivery will be not calculated
00265             if ( $this->_blFreeShipping ) {
00266                 return $this->_oPrice;
00267             }
00268 
00269             // calculating base price value
00270             switch ( $this->oxdelivery__oxaddsumtype->value ) {
00271                 case 'abs':
00272 
00273                     $dAmount = 0;
00274 
00275                     if ( $this->oxdelivery__oxfixed->value == 0 ) {
00276                         // 1. Once per Cart
00277                         $dAmount = 1;
00278                     } elseif ( $this->oxdelivery__oxfixed->value == 1 ) {
00279                         // 2. Once per Product overall
00280                         $dAmount = $this->_iProdCnt;
00281                     } elseif ( $this->oxdelivery__oxfixed->value == 2 ) {
00282                         // 3. Once per Product in Cart
00283                         $dAmount = $this->_iItemCnt;
00284                     }
00285 
00286                     $oCur = $this->getConfig()->getActShopCurrencyObject();
00287                     $this->_oPrice->add( $this->oxdelivery__oxaddsum->value * $oCur->rate );
00288                     $this->_oPrice->multiply( $dAmount );
00289                     break;
00290                 case '%':
00291 
00292                     $this->_oPrice->add( $this->_dPrice /100 * $this->oxdelivery__oxaddsum->value );
00293                     break;
00294             }
00295         }
00296 
00297         // calculating total price
00298         return $this->_oPrice;
00299     }
00300 
00308     public function delete( $sOXID = null )
00309     {
00310         if ( !$sOXID ) {
00311             $sOXID = $this->getId();
00312         }
00313         if ( !$sOXID ) {
00314             return false;
00315         }
00316 
00317 
00318         $oDb = oxDb::getDb();
00319         $sQ = "delete from oxobject2delivery where oxobject2delivery.oxdeliveryid = ".$oDb->quote($sOXID);
00320         $oDb->execute( $sQ );
00321 
00322         return parent::delete( $sOXID );
00323     }
00324 
00332     public function isForBasket( $oBasket )
00333     {
00334         // amount for conditional check
00335         $blHasArticles   = $this->hasArtices();
00336         $blHasCategories = $this->hasCategories();
00337         $blUse = true;
00338         $iAmount = 0;
00339         $blForBasket = false;
00340 
00341         // category & article check
00342         if ( $blHasCategories || $blHasArticles ) {
00343             $blUse = false;
00344 
00345             $aDeliveryArticles   = $this->getArticles();
00346             $aDeliveryCategories = $this->getCategories();
00347 
00348             foreach ( $oBasket->getContents() as $oContent ) {
00349 
00350                 //V FS#1954 - load delivery for variants from parent article
00351                 $oArticle   = $oContent->getArticle(false);
00352                 $sProductId = $oArticle->getProductId();
00353                 $sParentId  = $oArticle->getProductParentId();
00354 
00355                 if ( $blHasArticles && (in_array( $sProductId, $aDeliveryArticles ) || ( $sParentId && in_array( $sParentId, $aDeliveryArticles ) ) ) ) {
00356                     $blUse = true;
00357                     $iArtAmount = $this->getDeliveryAmount( $oContent );
00358                     if ( $this->oxdelivery__oxfixed->value > 0 ) {
00359                         if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00360                             $blForBasket = true;
00361                         }
00362                     } else {
00363                         $iAmount += $iArtAmount;
00364                     }
00365 
00366                 } elseif ( $blHasCategories ) {
00367 
00368                     if ( isset( self::$_aProductList[$sProductId] ) ) {
00369                         $oProduct = self::$_aProductList[$sProductId];
00370                     } else {
00371                         $oProduct = oxNew( 'oxarticle' );
00372                         $oProduct->setSkipAssign( true );
00373 
00374                         if ( !$oProduct->load( $sProductId ) ) {
00375                             continue;
00376                         }
00377 
00378                         $oProduct->setId($sProductId);
00379                         self::$_aProductList[$sProductId] = $oProduct;
00380                     }
00381 
00382                     foreach ( $aDeliveryCategories as $sCatId ) {
00383 
00384                         if ( $oProduct->inCategory( $sCatId ) ) {
00385                             $blUse = true;
00386 
00387                             $iArtAmount = $this->getDeliveryAmount( $oContent );
00388                             if ( $this->oxdelivery__oxfixed->value > 0 ) {
00389                                 if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00390                                     $blForBasket = true;
00391                                 }
00392                             } else {
00393                                 $iAmount += $iArtAmount;
00394                             }
00395 
00396                             break;
00397                         }
00398                     }
00399                 }
00400             }
00401 
00402         } else {
00403             // regular amounts check
00404             foreach ( $oBasket->getContents() as $oContent ) {
00405                 $iArtAmount = $this->getDeliveryAmount( $oContent );
00406                 if ( $this->oxdelivery__oxfixed->value > 0 ) {
00407                     if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00408                         $blForBasket = true;
00409                     }
00410                 } else {
00411                     $iAmount += $iArtAmount;
00412                 }
00413             }
00414         }
00415 
00416         //#M1130: Single article in Basket, checked as free shipping, is not buyable (step 3 no payments found)
00417         if ( !$blForBasket && $blUse && ( $this->_checkDeliveryAmount($iAmount) || $this->_blFreeShipping ) ) {
00418             $blForBasket = true;
00419         }
00420         return $blForBasket;
00421     }
00422 
00431     protected function _isForArticle( $oContent, $iArtAmount )
00432     {
00433         if ( !$this->_blFreeShipping && $this->_checkDeliveryAmount($iArtAmount) ) {
00434             $this->_iItemCnt += $oContent->getAmount();
00435             $this->_iProdCnt += 1;
00436             return true;
00437         }
00438         return false;
00439     }
00440 
00448     protected function _checkDeliveryAmount($iAmount)
00449     {
00450         switch ( $this->oxdelivery__oxdeltype->value ) {
00451             case 'p': // price
00452                 $oCur = $this->getConfig()->getActShopCurrencyObject();
00453                 $iAmount /= $oCur->rate;
00454                 break;
00455             case 'w': // weight
00456             case 's': // size
00457             case 'a': // amount
00458                 break;
00459         }
00460 
00461         if ( $iAmount >= $this->oxdelivery__oxparam->value && $iAmount <= $this->oxdelivery__oxparamend->value ) {
00462             return true;
00463         }
00464 
00465         return false;
00466     }
00467 }

Generated by  doxygen 1.6.2