oxdelivery.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxDelivery extends oxI18n
00009 {
00015     protected $_sClassName = 'oxdelivery';
00016 
00023     protected $_iItemCnt = 0;
00024 
00031     protected $_iProdCnt = 0;
00032 
00039     protected $_dPrice = 0;
00040 
00046     protected $_oPrice = null;
00047 
00053     protected $_aArtIds = null;
00054 
00060     protected $_aCatIds = null;
00061 
00067     protected $_blFreeShipping = true;
00068 
00074     protected static $_aProductList = array();
00075 
00081     protected $_blDelVatOnTop = false;
00082 
00088     protected $_aCountriesISO = null;
00089 
00095     protected $_aRDFaDeliverySet = null;
00096 
00100     public function __construct()
00101     {
00102         parent::__construct();
00103         $this->init( 'oxdelivery' );
00104         $this->setDelVatOnTop( $this->getConfig()->getConfigParam( 'blDeliveryVatOnTop' ) );
00105     }
00106 
00114     public function setDelVatOnTop( $blOnTop )
00115     {
00116         $this->_blDelVatOnTop = $blOnTop;
00117     }
00118 
00124     public function getArticles()
00125     {
00126         if ( $this->_aArtIds !== null ) {
00127             return $this->_aArtIds;
00128         }
00129 
00130         $oDb = oxDb::getDb();
00131         $sQ = "select oxobjectid from oxobject2delivery where oxdeliveryid=".$oDb->quote($this->getId())." and oxtype = 'oxarticles'";
00132         $aArtIds = $oDb->getAll( $sQ );
00133 
00134         //make single dimension array
00135         foreach ( $aArtIds as $aItem ) {
00136             $this->_aArtIds[] = $aItem[0];
00137         }
00138 
00139         return $this->_aArtIds;
00140 
00141     }
00142 
00148     public function getCategories()
00149     {
00150         if ( $this->_aCatIds !== null ) {
00151             return $this->_aCatIds;
00152         }
00153 
00154         $oDb = oxDb::getDb();
00155         $sQ = "select oxobjectid from oxobject2delivery where oxdeliveryid=".$oDb->quote($this->getId())." and oxtype = 'oxcategories'";
00156         $aCatIds = $oDb->getAll( $sQ );
00157 
00158         //make single dimension array
00159         foreach ( $aCatIds AS $aItem ) {
00160             $this->_aCatIds[] = $aItem[0];
00161         }
00162 
00163         return $this->_aCatIds;
00164     }
00165 
00171     public function hasArticles()
00172     {
00173         return ( bool ) count( $this->getArticles() );
00174     }
00175 
00181     public function hasCategories()
00182     {
00183         return ( bool ) count( $this->getCategories() );
00184     }
00185 
00193     public function getDeliveryAmount( $oBasketItem )
00194     {
00195         $dAmount  = 0;
00196         $oProduct = $oBasketItem->getArticle( false );
00197 
00198         $blExclNonMaterial = $this->getConfig()->getConfigParam( 'blExclNonMaterialFromDelivery' );
00199 
00200         // mark free shipping products
00201         if ( $oProduct->oxarticles__oxfreeshipping->value || ($oProduct->oxarticles__oxnonmaterial->value && $blExclNonMaterial) ) {
00202             if ($this->_blFreeShipping !== false) {
00203                 $this->_blFreeShipping = true;
00204             }
00205         } else {
00206 
00207             $this->_blFreeShipping = false;
00208 
00209             switch ( $this->oxdelivery__oxdeltype->value ) {
00210                 case 'p': // price
00211                     if ( $this->oxdelivery__oxfixed->value == 2 ) {
00212                         $dAmount += $oProduct->getPrice()->getPrice();
00213                     } else {
00214                         $dAmount += $oBasketItem->getPrice()->getPrice(); // price// currency conversion must allready be done in price class / $oCur->rate; // $oBasketItem->oPrice->getPrice() / $oCur->rate;
00215                     }
00216                     break;
00217                 case 'w': // weight
00218                     if ( $this->oxdelivery__oxfixed->value == 2 ) {
00219                         $dAmount += $oProduct->oxarticles__oxweight->value;
00220                     } else {
00221                         $dAmount += $oBasketItem->getWeight();
00222                     }
00223                     break;
00224                 case 's': // size
00225                     $dAmount += $oProduct->oxarticles__oxlength->value *
00226                                 $oProduct->oxarticles__oxwidth->value *
00227                                 $oProduct->oxarticles__oxheight->value;
00228                     if ( $this->oxdelivery__oxfixed->value < 2 ) {
00229                         $dAmount *= $oBasketItem->getAmount();
00230                     }
00231                     break;
00232                 case 'a': // amount
00233                     $dAmount += $oBasketItem->getAmount();
00234                     break;
00235             }
00236 
00237             if ( $oBasketItem->getPrice() ) {
00238                 $this->_dPrice   += $oBasketItem->getPrice()->getPrice();
00239             }
00240         }
00241 
00242         return $dAmount;
00243     }
00244 
00252     public function setDeliveryPrice( $oPrice )
00253     {
00254         $this->_oPrice = $oPrice;
00255     }
00256 
00264     public function getDeliveryPrice( $dVat = null )
00265     {
00266         if ( $this->_oPrice === null ) {
00267             // loading oxprice object for final price calculation
00268             $this->_oPrice = oxNew( 'oxPrice' );
00269 
00270             if ( !$this->_blDelVatOnTop ) {
00271                 $this->_oPrice->setBruttoPriceMode();
00272             } else {
00273                 $this->_oPrice->setNettoPriceMode();
00274             }
00275 
00276             $this->_oPrice->setVat( $dVat );
00277 
00278             // if article is free shipping, price for delivery will be not calculated
00279             if ( $this->_blFreeShipping ) {
00280                 return $this->_oPrice;
00281             }
00282 
00283             // calculating base price value
00284             switch ( $this->oxdelivery__oxaddsumtype->value ) {
00285                 case 'abs':
00286 
00287                     $dAmount = 0;
00288 
00289                     if ( $this->oxdelivery__oxfixed->value == 0 ) {
00290                         // 1. Once per Cart
00291                         $dAmount = 1;
00292                     } elseif ( $this->oxdelivery__oxfixed->value == 1 ) {
00293                         // 2. Once per Product overall
00294                         $dAmount = $this->_iProdCnt;
00295                     } elseif ( $this->oxdelivery__oxfixed->value == 2 ) {
00296                         // 3. Once per Product in Cart
00297                         $dAmount = $this->_iItemCnt;
00298                     }
00299 
00300                     $oCur = $this->getConfig()->getActShopCurrencyObject();
00301                     $this->_oPrice->add( $this->oxdelivery__oxaddsum->value * $oCur->rate );
00302                     $this->_oPrice->multiply( $dAmount );
00303                     break;
00304                 case '%':
00305 
00306                     $this->_oPrice->add( $this->_dPrice /100 * $this->oxdelivery__oxaddsum->value );
00307                     break;
00308             }
00309         }
00310 
00311         // calculating total price
00312         return $this->_oPrice;
00313     }
00314 
00322     public function delete( $sOXID = null )
00323     {
00324         if ( !$sOXID ) {
00325             $sOXID = $this->getId();
00326         }
00327         if ( !$sOXID ) {
00328             return false;
00329         }
00330 
00331 
00332         $oDb = oxDb::getDb();
00333         $sQ = "delete from oxobject2delivery where oxobject2delivery.oxdeliveryid = ".$oDb->quote($sOXID);
00334         $oDb->execute( $sQ );
00335 
00336         return parent::delete( $sOXID );
00337     }
00338 
00346     public function isForBasket( $oBasket )
00347     {
00348         // amount for conditional check
00349         $blHasArticles   = $this->hasArticles();
00350         $blHasCategories = $this->hasCategories();
00351         $blUse = true;
00352         $iAmount = 0;
00353         $blForBasket = false;
00354 
00355         // category & article check
00356         if ( $blHasCategories || $blHasArticles ) {
00357             $blUse = false;
00358 
00359             $aDeliveryArticles   = $this->getArticles();
00360             $aDeliveryCategories = $this->getCategories();
00361 
00362             foreach ( $oBasket->getContents() as $oContent ) {
00363 
00364                 //V FS#1954 - load delivery for variants from parent article
00365                 $oArticle   = $oContent->getArticle(false);
00366                 $sProductId = $oArticle->getProductId();
00367                 $sParentId  = $oArticle->getProductParentId();
00368 
00369                 if ( $blHasArticles && (in_array( $sProductId, $aDeliveryArticles ) || ( $sParentId && in_array( $sParentId, $aDeliveryArticles ) ) ) ) {
00370                     $blUse = true;
00371                     $iArtAmount = $this->getDeliveryAmount( $oContent );
00372                     if ( $this->oxdelivery__oxfixed->value > 0 ) {
00373                         if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00374                             $blForBasket = true;
00375                         }
00376                     }
00377                     if (!$blForBasket) {
00378                         $iAmount += $iArtAmount;
00379                     }
00380 
00381                 } elseif ( $blHasCategories ) {
00382 
00383                     if ( isset( self::$_aProductList[$sProductId] ) ) {
00384                         $oProduct = self::$_aProductList[$sProductId];
00385                     } else {
00386                         $oProduct = oxNew( 'oxArticle' );
00387                         $oProduct->setSkipAssign( true );
00388 
00389                         if ( !$oProduct->load( $sProductId ) ) {
00390                             continue;
00391                         }
00392 
00393                         $oProduct->setId($sProductId);
00394                         self::$_aProductList[$sProductId] = $oProduct;
00395                     }
00396 
00397                     foreach ( $aDeliveryCategories as $sCatId ) {
00398 
00399                         if ( $oProduct->inCategory( $sCatId ) ) {
00400                             $blUse = true;
00401                             $iArtAmount = $this->getDeliveryAmount( $oContent );
00402                             if ( $this->oxdelivery__oxfixed->value > 0 ) {
00403                                 if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00404                                     $blForBasket = true;
00405                                 }
00406                             }
00407                             if (!$blForBasket) {
00408                                 $iAmount += $iArtAmount;
00409                             }
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                 }
00424                 if (!$blForBasket) {
00425                     $iAmount += $iArtAmount;
00426                 }
00427             }
00428         }
00429 
00430         //#M1130: Single article in Basket, checked as free shipping, is not buyable (step 3 no payments found)
00431         if ( !$blForBasket && $blUse && ( $this->_checkDeliveryAmount($iAmount) || $this->_blFreeShipping ) ) {
00432             $blForBasket = true;
00433         }
00434         return $blForBasket;
00435     }
00436 
00445     protected function _isForArticle( $oContent, $iArtAmount )
00446     {
00447         if ( !$this->_blFreeShipping && $this->_checkDeliveryAmount($iArtAmount) ) {
00448             $this->_iItemCnt += $oContent->getAmount();
00449             $this->_iProdCnt += 1;
00450             return true;
00451         }
00452         return false;
00453     }
00454 
00462     protected function _checkDeliveryAmount($iAmount)
00463     {
00464         switch ( $this->oxdelivery__oxdeltype->value ) {
00465             case 'p': // price
00466                 $oCur = $this->getConfig()->getActShopCurrencyObject();
00467                 $iAmount /= $oCur->rate;
00468                 break;
00469             case 'w': // weight
00470             case 's': // size
00471             case 'a': // amount
00472                 break;
00473         }
00474 
00475         if ( $iAmount >= $this->oxdelivery__oxparam->value && $iAmount <= $this->oxdelivery__oxparamend->value ) {
00476             return true;
00477         }
00478 
00479         return false;
00480     }
00481 
00489     public function getIdByName( $sTitle )
00490     {
00491         $oDb = oxDb::getDb();
00492         $sQ = "SELECT `oxid` FROM `" . getViewName( 'oxdelivery' ) . "` WHERE  `oxtitle` = " . $oDb->quote( $sTitle );
00493         $sId = $oDb->getOne( $sQ );
00494 
00495         return $sId;
00496     }
00497 
00503     public function getCountriesISO()
00504     {
00505         if ( $this->_aCountriesISO === null ) {
00506             $oDb = oxDb::getDb();
00507             $this->_aCountriesISO = array();
00508             $sSelect = 'select oxcountry.oxisoalpha2 from oxcountry left join oxobject2delivery on oxobject2delivery.oxobjectid = oxcountry.oxid where oxobject2delivery.oxdeliveryid='.$oDb->quote( $this->getId() ).' and oxobject2delivery.oxtype = "oxcountry" ';
00509             $rs = $oDb->select( $sSelect );
00510             if ( $rs && $rs->recordCount()) {
00511                 while ( !$rs->EOF ) {
00512                     $this->_aCountriesISO[] = $rs->fields[0];
00513                     $rs->moveNext();
00514                 }
00515             }
00516         }
00517         return $this->_aCountriesISO;
00518     }
00519 
00520 }