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         // mark free shipping products
00199         if ( $oProduct->oxarticles__oxfreeshipping->value ) {
00200             if ($this->_blFreeShipping !== false) {
00201                 $this->_blFreeShipping = true;
00202             }
00203         } else {
00204 
00205             $blExclNonMaterial = $this->getConfig()->getConfigParam( 'blExclNonMaterialFromDelivery' );
00206             if ( !( $oProduct->oxarticles__oxnonmaterial->value && $blExclNonMaterial ) ) {
00207                 $this->_blFreeShipping = false;
00208             }
00209 
00210             switch ( $this->oxdelivery__oxdeltype->value ) {
00211                 case 'p': // price
00212                     if ( $this->oxdelivery__oxfixed->value == 2 ) {
00213                         $dAmount += $oProduct->getPrice()->getPrice();
00214                     } else {
00215                         $dAmount += $oBasketItem->getPrice()->getPrice(); // 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 += $oProduct->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 
00238             if ( $oBasketItem->getPrice() ) {
00239                 $this->_dPrice   += $oBasketItem->getPrice()->getPrice();
00240             }
00241         }
00242 
00243         return $dAmount;
00244     }
00245 
00253     public function setDeliveryPrice( $oPrice )
00254     {
00255         $this->_oPrice = $oPrice;
00256     }
00257 
00265     public function getDeliveryPrice( $dVat = null )
00266     {
00267         if ( $this->_oPrice === null ) {
00268             // loading oxprice object for final price calculation
00269             $this->_oPrice = oxNew( 'oxPrice' );
00270 
00271             if ( !$this->_blDelVatOnTop ) {
00272                 $this->_oPrice->setBruttoPriceMode();
00273             } else {
00274                 $this->_oPrice->setNettoPriceMode();
00275             }
00276 
00277             $this->_oPrice->setVat( $dVat );
00278 
00279             // if article is free shipping, price for delivery will be not calculated
00280             if ( $this->_blFreeShipping ) {
00281                 return $this->_oPrice;
00282             }
00283 
00284             // calculating base price value
00285             switch ( $this->oxdelivery__oxaddsumtype->value ) {
00286                 case 'abs':
00287 
00288                     $dAmount = 0;
00289 
00290                     if ( $this->oxdelivery__oxfixed->value == 0 ) {
00291                         // 1. Once per Cart
00292                         $dAmount = 1;
00293                     } elseif ( $this->oxdelivery__oxfixed->value == 1 ) {
00294                         // 2. Once per Product overall
00295                         $dAmount = $this->_iProdCnt;
00296                     } elseif ( $this->oxdelivery__oxfixed->value == 2 ) {
00297                         // 3. Once per Product in Cart
00298                         $dAmount = $this->_iItemCnt;
00299                     }
00300 
00301                     $oCur = $this->getConfig()->getActShopCurrencyObject();
00302                     $this->_oPrice->add( $this->oxdelivery__oxaddsum->value * $oCur->rate );
00303                     $this->_oPrice->multiply( $dAmount );
00304                     break;
00305                 case '%':
00306 
00307                     $this->_oPrice->add( $this->_dPrice /100 * $this->oxdelivery__oxaddsum->value );
00308                     break;
00309             }
00310         }
00311 
00312         // calculating total price
00313         return $this->_oPrice;
00314     }
00315 
00323     public function delete( $sOXID = null )
00324     {
00325         if ( !$sOXID ) {
00326             $sOXID = $this->getId();
00327         }
00328         if ( !$sOXID ) {
00329             return false;
00330         }
00331 
00332 
00333         $oDb = oxDb::getDb();
00334         $sQ = "delete from oxobject2delivery where oxobject2delivery.oxdeliveryid = ".$oDb->quote($sOXID);
00335         $oDb->execute( $sQ );
00336 
00337         return parent::delete( $sOXID );
00338     }
00339 
00347     public function isForBasket( $oBasket )
00348     {
00349         // amount for conditional check
00350         $blHasArticles   = $this->hasArticles();
00351         $blHasCategories = $this->hasCategories();
00352         $blUse = true;
00353         $iAmount = 0;
00354         $blForBasket = false;
00355 
00356         // category & article check
00357         if ( $blHasCategories || $blHasArticles ) {
00358             $blUse = false;
00359 
00360             $aDeliveryArticles   = $this->getArticles();
00361             $aDeliveryCategories = $this->getCategories();
00362 
00363             foreach ( $oBasket->getContents() as $oContent ) {
00364 
00365                 //V FS#1954 - load delivery for variants from parent article
00366                 $oArticle   = $oContent->getArticle(false);
00367                 $sProductId = $oArticle->getProductId();
00368                 $sParentId  = $oArticle->getProductParentId();
00369 
00370                 if ( $blHasArticles && (in_array( $sProductId, $aDeliveryArticles ) || ( $sParentId && in_array( $sParentId, $aDeliveryArticles ) ) ) ) {
00371                     $blUse = true;
00372                     $iArtAmount = $this->getDeliveryAmount( $oContent );
00373                     if ( $this->oxdelivery__oxfixed->value > 0 ) {
00374                         if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00375                             $blForBasket = true;
00376                         }
00377                     }
00378                     if (!$blForBasket) {
00379                         $iAmount += $iArtAmount;
00380                     }
00381 
00382                 } elseif ( $blHasCategories ) {
00383 
00384                     if ( isset( self::$_aProductList[$sProductId] ) ) {
00385                         $oProduct = self::$_aProductList[$sProductId];
00386                     } else {
00387                         $oProduct = oxNew( 'oxArticle' );
00388                         $oProduct->setSkipAssign( true );
00389 
00390                         if ( !$oProduct->load( $sProductId ) ) {
00391                             continue;
00392                         }
00393 
00394                         $oProduct->setId($sProductId);
00395                         self::$_aProductList[$sProductId] = $oProduct;
00396                     }
00397 
00398                     foreach ( $aDeliveryCategories as $sCatId ) {
00399 
00400                         if ( $oProduct->inCategory( $sCatId ) ) {
00401                             $blUse = true;
00402                             $iArtAmount = $this->getDeliveryAmount( $oContent );
00403                             if ( $this->oxdelivery__oxfixed->value > 0 ) {
00404                                 if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00405                                     $blForBasket = true;
00406                                 }
00407                             }
00408                             if (!$blForBasket) {
00409                                 $iAmount += $iArtAmount;
00410                             }
00411                         }
00412                     }
00413 
00414                 }
00415             }
00416         } else {
00417             // regular amounts check
00418             foreach ( $oBasket->getContents() as $oContent ) {
00419                 $iArtAmount = $this->getDeliveryAmount( $oContent );
00420                 if ( $this->oxdelivery__oxfixed->value > 0 ) {
00421                     if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00422                         $blForBasket = true;
00423                     }
00424                 }
00425                 if (!$blForBasket) {
00426                     $iAmount += $iArtAmount;
00427                 }
00428             }
00429         }
00430 
00431         //#M1130: Single article in Basket, checked as free shipping, is not buyable (step 3 no payments found)
00432         if ( !$blForBasket && $blUse && ( $this->_checkDeliveryAmount($iAmount) || $this->_blFreeShipping ) ) {
00433             $blForBasket = true;
00434         }
00435         return $blForBasket;
00436     }
00437 
00446     protected function _isForArticle( $oContent, $iArtAmount )
00447     {
00448         if ( !$this->_blFreeShipping && $this->_checkDeliveryAmount($iArtAmount) ) {
00449             $this->_iItemCnt += $oContent->getAmount();
00450             $this->_iProdCnt += 1;
00451             return true;
00452         }
00453         return false;
00454     }
00455 
00463     protected function _checkDeliveryAmount($iAmount)
00464     {
00465         switch ( $this->oxdelivery__oxdeltype->value ) {
00466             case 'p': // price
00467                 $oCur = $this->getConfig()->getActShopCurrencyObject();
00468                 $iAmount /= $oCur->rate;
00469                 break;
00470             case 'w': // weight
00471             case 's': // size
00472             case 'a': // amount
00473                 break;
00474         }
00475 
00476         if ( $iAmount >= $this->oxdelivery__oxparam->value && $iAmount <= $this->oxdelivery__oxparamend->value ) {
00477             return true;
00478         }
00479 
00480         return false;
00481     }
00482 
00490     public function getIdByName( $sTitle )
00491     {
00492         $oDb = oxDb::getDb();
00493         $sQ = "SELECT `oxid` FROM `" . getViewName( 'oxdelivery' ) . "` WHERE  `oxtitle` = " . $oDb->quote( $sTitle );
00494         $sId = $oDb->getOne( $sQ );
00495 
00496         return $sId;
00497     }
00498 
00504     public function getCountriesISO()
00505     {
00506         if ( $this->_aCountriesISO === null ) {
00507             $oDb = oxDb::getDb();
00508             $this->_aCountriesISO = array();
00509             $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" ';
00510             $rs = $oDb->select( $sSelect );
00511             if ( $rs && $rs->recordCount()) {
00512                 while ( !$rs->EOF ) {
00513                     $this->_aCountriesISO[] = $rs->fields[0];
00514                     $rs->moveNext();
00515                 }
00516             }
00517         }
00518         return $this->_aCountriesISO;
00519     }
00520 
00521 }