oxdelivery.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxDelivery extends oxI18n
00008 {
00014     protected $_sClassName = 'oxdelivery';
00015 
00022     protected $_iItemCnt = 0;
00023 
00030     protected $_iProdCnt = 0;
00031 
00038     protected $_dPrice = 0;
00039 
00045     protected $_oPrice = null;
00046 
00052     protected $_aArtIds = null;
00053 
00059     protected $_aCatIds = null;
00060 
00066     protected $_blFreeShipping = true;
00067 
00073     protected static $_aProductList = array();
00074 
00080     protected $_blDelVatOnTop = false;
00081 
00087     protected $_aCountriesISO = null;
00088 
00094     protected $_aRDFaDeliverySet = null;
00095 
00099     public function __construct()
00100     {
00101         parent::__construct();
00102         $this->init( 'oxdelivery' );
00103         $this->setDelVatOnTop( $this->getConfig()->getConfigParam( 'blDeliveryVatOnTop' ) );
00104     }
00105 
00113     public function setDelVatOnTop( $blOnTop )
00114     {
00115         $this->_blDelVatOnTop = $blOnTop;
00116     }
00117 
00123     public function getArticles()
00124     {
00125         if ( $this->_aArtIds !== null ) {
00126             return $this->_aArtIds;
00127         }
00128 
00129         $oDb = oxDb::getDb();
00130         $sQ = "select oxobjectid from oxobject2delivery where oxdeliveryid=".$oDb->quote($this->getId())." and oxtype = 'oxarticles'";
00131         $aArtIds = $oDb->getAll( $sQ );
00132 
00133         //make single dimension array
00134         foreach ( $aArtIds as $aItem ) {
00135             $this->_aArtIds[] = $aItem[0];
00136         }
00137 
00138         return $this->_aArtIds;
00139 
00140     }
00141 
00147     public function getCategories()
00148     {
00149         if ( $this->_aCatIds !== null ) {
00150             return $this->_aCatIds;
00151         }
00152 
00153         $oDb = oxDb::getDb();
00154         $sQ = "select oxobjectid from oxobject2delivery where oxdeliveryid=".$oDb->quote($this->getId())." and oxtype = 'oxcategories'";
00155         $aCatIds = $oDb->getAll( $sQ );
00156 
00157         //make single dimension array
00158         foreach ( $aCatIds AS $aItem ) {
00159             $this->_aCatIds[] = $aItem[0];
00160         }
00161 
00162         return $this->_aCatIds;
00163     }
00164 
00172     public function hasArtices()
00173     {
00174         return ( bool ) count( $this->getArticles() );
00175     }
00176 
00182     public function hasArticles()
00183     {
00184         return ( bool ) count( $this->getArticles() );
00185     }
00186 
00192     public function hasCategories()
00193     {
00194         return ( bool ) count( $this->getCategories() );
00195     }
00196 
00204     public function getDeliveryAmount( $oBasketItem )
00205     {
00206         $dAmount  = 0;
00207         $oProduct = $oBasketItem->getArticle( false );
00208 
00209         // mark free shipping products
00210         if ( $oProduct->oxarticles__oxfreeshipping->value ) {
00211             if ($this->_blFreeShipping !== false) {
00212                 $this->_blFreeShipping = true;
00213             }
00214         } else {
00215 
00216             $blExclNonMaterial = $this->getConfig()->getConfigParam( 'blExclNonMaterialFromDelivery' );
00217             if ( !( $oProduct->oxarticles__oxnonmaterial->value && $blExclNonMaterial ) ) {
00218                 $this->_blFreeShipping = false;
00219             }
00220 
00221             switch ( $this->oxdelivery__oxdeltype->value ) {
00222                 case 'p': // price
00223                     if ( $this->oxdelivery__oxfixed->value == 2 ) {
00224                         $dAmount += $oProduct->getPrice()->getBruttoPrice();
00225                     } else {
00226                         $dAmount += $oBasketItem->getPrice()->getBruttoPrice(); // price// currency conversion must allready be done in price class / $oCur->rate; // $oBasketItem->oPrice->getPrice() / $oCur->rate;
00227                     }
00228                     break;
00229                 case 'w': // weight
00230                     if ( $this->oxdelivery__oxfixed->value == 2 ) {
00231                         $dAmount += $oProduct->oxarticles__oxweight->value;
00232                     } else {
00233                         $dAmount += $oBasketItem->getWeight();
00234                     }
00235                     break;
00236                 case 's': // size
00237                     $dAmount += $oProduct->oxarticles__oxlength->value *
00238                                 $oProduct->oxarticles__oxwidth->value *
00239                                 $oProduct->oxarticles__oxheight->value;
00240                     if ( $this->oxdelivery__oxfixed->value < 2 ) {
00241                         $dAmount *= $oBasketItem->getAmount();
00242                     }
00243                     break;
00244                 case 'a': // amount
00245                     $dAmount += $oBasketItem->getAmount();
00246                     break;
00247             }
00248 
00249             if ( $oBasketItem->getPrice() ) {
00250                 $this->_dPrice   += $oBasketItem->getPrice()->getBruttoPrice();
00251             }
00252         }
00253 
00254         return $dAmount;
00255     }
00256 
00264     public function setDeliveryPrice( $oPrice )
00265     {
00266         $this->_oPrice = $oPrice;
00267     }
00268 
00276     public function getDeliveryPrice( $dVat = null )
00277     {
00278         if ( $this->_oPrice === null ) {
00279             // loading oxprice object for final price calculation
00280             $this->_oPrice = oxNew( 'oxPrice' );
00281 
00282             if ( !$this->_blDelVatOnTop ) {
00283                 $this->_oPrice->setBruttoPriceMode();
00284             } else {
00285                 $this->_oPrice->setNettoPriceMode();
00286             }
00287 
00288             $this->_oPrice->setVat( $dVat );
00289 
00290             // if article is free shipping, price for delivery will be not calculated
00291             if ( $this->_blFreeShipping ) {
00292                 return $this->_oPrice;
00293             }
00294 
00295             // calculating base price value
00296             switch ( $this->oxdelivery__oxaddsumtype->value ) {
00297                 case 'abs':
00298 
00299                     $dAmount = 0;
00300 
00301                     if ( $this->oxdelivery__oxfixed->value == 0 ) {
00302                         // 1. Once per Cart
00303                         $dAmount = 1;
00304                     } elseif ( $this->oxdelivery__oxfixed->value == 1 ) {
00305                         // 2. Once per Product overall
00306                         $dAmount = $this->_iProdCnt;
00307                     } elseif ( $this->oxdelivery__oxfixed->value == 2 ) {
00308                         // 3. Once per Product in Cart
00309                         $dAmount = $this->_iItemCnt;
00310                     }
00311 
00312                     $oCur = $this->getConfig()->getActShopCurrencyObject();
00313                     $this->_oPrice->add( $this->oxdelivery__oxaddsum->value * $oCur->rate );
00314                     $this->_oPrice->multiply( $dAmount );
00315                     break;
00316                 case '%':
00317 
00318                     $this->_oPrice->add( $this->_dPrice /100 * $this->oxdelivery__oxaddsum->value );
00319                     break;
00320             }
00321         }
00322 
00323         // calculating total price
00324         return $this->_oPrice;
00325     }
00326 
00334     public function delete( $sOXID = null )
00335     {
00336         if ( !$sOXID ) {
00337             $sOXID = $this->getId();
00338         }
00339         if ( !$sOXID ) {
00340             return false;
00341         }
00342 
00343 
00344         $oDb = oxDb::getDb();
00345         $sQ = "delete from oxobject2delivery where oxobject2delivery.oxdeliveryid = ".$oDb->quote($sOXID);
00346         $oDb->execute( $sQ );
00347 
00348         return parent::delete( $sOXID );
00349     }
00350 
00358     public function isForBasket( $oBasket )
00359     {
00360         // amount for conditional check
00361         $blHasArticles   = $this->hasArticles();
00362         $blHasCategories = $this->hasCategories();
00363         $blUse = true;
00364         $iAmount = 0;
00365         $blForBasket = false;
00366 
00367         // category & article check
00368         if ( $blHasCategories || $blHasArticles ) {
00369             $blUse = false;
00370 
00371             $aDeliveryArticles   = $this->getArticles();
00372             $aDeliveryCategories = $this->getCategories();
00373 
00374             foreach ( $oBasket->getContents() as $oContent ) {
00375 
00376                 //V FS#1954 - load delivery for variants from parent article
00377                 $oArticle   = $oContent->getArticle(false);
00378                 $sProductId = $oArticle->getProductId();
00379                 $sParentId  = $oArticle->getProductParentId();
00380 
00381                 if ( $blHasArticles && (in_array( $sProductId, $aDeliveryArticles ) || ( $sParentId && in_array( $sParentId, $aDeliveryArticles ) ) ) ) {
00382                     $blUse = true;
00383                     $iArtAmount = $this->getDeliveryAmount( $oContent );
00384                     if ( $this->oxdelivery__oxfixed->value > 0 ) {
00385                         if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00386                             $blForBasket = true;
00387                         }
00388                     }
00389                     if (!$blForBasket) {
00390                         $iAmount += $iArtAmount;
00391                     }
00392 
00393                 } elseif ( $blHasCategories ) {
00394 
00395                     if ( isset( self::$_aProductList[$sProductId] ) ) {
00396                         $oProduct = self::$_aProductList[$sProductId];
00397                     } else {
00398                         $oProduct = oxNew( 'oxarticle' );
00399                         $oProduct->setSkipAssign( true );
00400 
00401                         if ( !$oProduct->load( $sProductId ) ) {
00402                             continue;
00403                         }
00404 
00405                         $oProduct->setId($sProductId);
00406                         self::$_aProductList[$sProductId] = $oProduct;
00407                     }
00408 
00409                     foreach ( $aDeliveryCategories as $sCatId ) {
00410 
00411                         if ( $oProduct->inCategory( $sCatId ) ) {
00412                             $blUse = true;
00413 
00414                             $iArtAmount = $this->getDeliveryAmount( $oContent );
00415                             if ( $this->oxdelivery__oxfixed->value > 0 ) {
00416                                 if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00417                                     $blForBasket = true;
00418                                 }
00419                             }
00420 
00421                             break;
00422                         }
00423                     }
00424                     if (!$blForBasket) {
00425                         $iAmount += $iArtAmount;
00426                     }
00427                 }
00428             }
00429 
00430         } else {
00431             // regular amounts check
00432             foreach ( $oBasket->getContents() as $oContent ) {
00433                 $iArtAmount = $this->getDeliveryAmount( $oContent );
00434                 if ( $this->oxdelivery__oxfixed->value > 0 ) {
00435                     if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00436                         $blForBasket = true;
00437                     }
00438                 }
00439                 if (!$blForBasket) {
00440                     $iAmount += $iArtAmount;
00441                 }
00442             }
00443         }
00444 
00445         //#M1130: Single article in Basket, checked as free shipping, is not buyable (step 3 no payments found)
00446         if ( !$blForBasket && $blUse && ( $this->_checkDeliveryAmount($iAmount) || $this->_blFreeShipping ) ) {
00447             $blForBasket = true;
00448         }
00449         return $blForBasket;
00450     }
00451 
00460     protected function _isForArticle( $oContent, $iArtAmount )
00461     {
00462         if ( !$this->_blFreeShipping && $this->_checkDeliveryAmount($iArtAmount) ) {
00463             $this->_iItemCnt += $oContent->getAmount();
00464             $this->_iProdCnt += 1;
00465             return true;
00466         }
00467         return false;
00468     }
00469 
00477     protected function _checkDeliveryAmount($iAmount)
00478     {
00479         switch ( $this->oxdelivery__oxdeltype->value ) {
00480             case 'p': // price
00481                 $oCur = $this->getConfig()->getActShopCurrencyObject();
00482                 $iAmount /= $oCur->rate;
00483                 break;
00484             case 'w': // weight
00485             case 's': // size
00486             case 'a': // amount
00487                 break;
00488         }
00489 
00490         if ( $iAmount >= $this->oxdelivery__oxparam->value && $iAmount <= $this->oxdelivery__oxparamend->value ) {
00491             return true;
00492         }
00493 
00494         return false;
00495     }
00496 
00504     public function getIdByName( $sTitle )
00505     {
00506         $oDb = oxDb::getDb();
00507         $sQ = "SELECT `oxid` FROM `" . getViewName( 'oxdelivery' ) . "` WHERE  `oxtitle` = " . $oDb->quote( $sTitle );
00508         $sId = $oDb->getOne( $sQ );
00509 
00510         return $sId;
00511     }
00512 
00518     public function getCountriesISO()
00519     {
00520         if ( $this->_aCountriesISO === null ) {
00521             $oDb = oxDb::getDb();
00522             $this->_aCountriesISO = array();
00523             $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" ';
00524             $rs = $oDb->select( $sSelect );
00525             if ( $rs && $rs->recordCount()) {
00526                 while ( !$rs->EOF ) {
00527                     $this->_aCountriesISO[] = $rs->fields[0];
00528                     $rs->moveNext();
00529                 }
00530             }
00531         }
00532         return $this->_aCountriesISO;
00533     }
00534 
00535 }