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