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
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
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
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':
00200 if ( $this->oxdelivery__oxfixed->value == 2 ) {
00201 $dAmount += $oBasketItem->getArticle()->getPrice()->getBruttoPrice();
00202 } else {
00203 $dAmount += $oBasketItem->getPrice()->getBruttoPrice();
00204 }
00205 break;
00206 case 'w':
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':
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':
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
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
00266 if ( $this->_blFreeShipping ) {
00267 return $this->_oPrice;
00268 }
00269
00270
00271 switch ( $this->oxdelivery__oxaddsumtype->value ) {
00272 case 'abs':
00273
00274 $dAmount = 0;
00275
00276 if ( $this->oxdelivery__oxfixed->value == 0 ) {
00277
00278 $dAmount = 1;
00279 } elseif ( $this->oxdelivery__oxfixed->value == 1 ) {
00280
00281 $dAmount = $this->_iProdCnt;
00282 } elseif ( $this->oxdelivery__oxfixed->value == 2 ) {
00283
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
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
00336 $blHasArticles = $this->hasArtices();
00337 $blHasCategories = $this->hasCategories();
00338 $blUse = true;
00339 $iAmount = 0;
00340 $blForBasket = false;
00341
00342
00343 if ( $blHasCategories || $blHasArticles ) {
00344 $aDeliveryArticles = $this->getArticles();
00345 $aDeliveryCategories = $this->getCategories();
00346
00347 foreach ( $oBasket->getContents() as $oContent ) {
00348 if ( !$blUse ) {
00349 break;
00350 }
00351 $blUse = false;
00352
00353 $oArticle = $oContent->getArticle(false);
00354 $sProductId = $oArticle->getProductId();
00355 $sParentId = $oArticle->getProductParentId();
00356
00357 if ( $blHasArticles && (in_array( $sProductId, $aDeliveryArticles ) || ( $sParentId && in_array( $sParentId, $aDeliveryArticles ) ) ) ) {
00358 $blUse = true;
00359 $iArtAmount = $this->getDeliveryAmount( $oContent );
00360 if ( $this->oxdelivery__oxfixed->value > 0 ) {
00361 if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00362 $blForBasket = true;
00363 }
00364 } else {
00365 $iAmount += $iArtAmount;
00366 }
00367
00368 } elseif ( $blHasCategories ) {
00369
00370 if ( isset( self::$_aProductList[$sProductId] ) ) {
00371 $oProduct = self::$_aProductList[$sProductId];
00372 } else {
00373 $oProduct = oxNew( 'oxarticle' );
00374 $oProduct->setSkipAssign( true );
00375
00376 if ( !$oProduct->load( $sProductId ) ) {
00377 continue;
00378 }
00379
00380 $oProduct->setId($sProductId);
00381 self::$_aProductList[$sProductId] = $oProduct;
00382 }
00383
00384 foreach ( $aDeliveryCategories as $sCatId ) {
00385
00386 if ( $oProduct->inCategory( $sCatId ) ) {
00387 $blUse = true;
00388
00389 $iArtAmount = $this->getDeliveryAmount( $oContent );
00390 if ( $this->oxdelivery__oxfixed->value > 0 ) {
00391 if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00392 $blForBasket = true;
00393 }
00394 } else {
00395 $iAmount += $iArtAmount;
00396 }
00397
00398 break;
00399 }
00400 }
00401 }
00402 }
00403
00404 } else {
00405
00406 foreach ( $oBasket->getContents() as $oContent ) {
00407 $iArtAmount = $this->getDeliveryAmount( $oContent );
00408 if ( $this->oxdelivery__oxfixed->value > 0 ) {
00409 if ( $this->_isForArticle( $oContent, $iArtAmount ) ) {
00410 $blForBasket = true;
00411 }
00412 } else {
00413 $iAmount += $iArtAmount;
00414 }
00415 }
00416 }
00417
00418
00419 if ( !$blForBasket && $blUse && ( $this->_checkDeliveryAmount($iAmount) || $this->_blFreeShipping ) ) {
00420 $blForBasket = true;
00421 }
00422 return $blForBasket;
00423 }
00424
00433 protected function _isForArticle( $oContent, $iArtAmount )
00434 {
00435 if ( !$this->_blFreeShipping && $this->_checkDeliveryAmount($iArtAmount) ) {
00436 $this->_iItemCnt += $oContent->getAmount();
00437 $this->_iProdCnt += 1;
00438 return true;
00439 }
00440 return false;
00441 }
00442
00450 protected function _checkDeliveryAmount($iAmount)
00451 {
00452 switch ( $this->oxdelivery__oxdeltype->value ) {
00453 case 'p':
00454 $oCur = $this->getConfig()->getActShopCurrencyObject();
00455 $iAmount /= $oCur->rate;
00456 break;
00457 case 'w':
00458 case 's':
00459 case 'a':
00460 break;
00461 }
00462
00463 if ( $iAmount >= $this->oxdelivery__oxparam->value && $iAmount <= $this->oxdelivery__oxparamend->value ) {
00464 return true;
00465 }
00466
00467 return false;
00468 }
00469 }