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 if ( !$oBasketItem->getArticle()->oxarticles__oxfreeshipping->value &&
00192 !( $oBasketItem->getArticle()->oxarticles__oxnonmaterial->value && $blExclNonMaterial ) ) {
00193
00194 $this->_blFreeShipping = false;
00195
00196 switch ( $this->oxdelivery__oxdeltype->value ) {
00197 case 'p':
00198 $dAmount += $oBasketItem->getPrice()->getBruttoPrice();
00199 break;
00200 case 'w':
00201 $dAmount += $oBasketItem->getWeight();
00202 break;
00203 case 's':
00204 $dAmount += $oBasketItem->getArticle()->oxarticles__oxlength->value *
00205 $oBasketItem->getArticle()->oxarticles__oxwidth->value *
00206 $oBasketItem->getArticle()->oxarticles__oxheight->value *
00207 $oBasketItem->getAmount();
00208 break;
00209 case 'a':
00210 $dAmount += $oBasketItem->getAmount();
00211 break;
00212 }
00213
00214 $this->_iItemCnt += $oBasketItem->getAmount();
00215 $this->_iProdCnt += 1;
00216 $this->_dPrice += $oBasketItem->getPrice()->getBruttoPrice();
00217 }
00218
00219 return $dAmount;
00220 }
00221
00229 public function setDeliveryPrice( $oPrice )
00230 {
00231 $this->_oPrice = $oPrice;
00232 }
00233
00241 public function getDeliveryPrice( $dVat = null )
00242 {
00243 if ( $this->_oPrice === null ) {
00244
00245 $this->_oPrice = oxNew( 'oxPrice' );
00246
00247 if ( !$this->_blDelVatOnTop ) {
00248 $this->_oPrice->setBruttoPriceMode();
00249 } else {
00250 $this->_oPrice->setNettoPriceMode();
00251 }
00252
00253 $this->_oPrice->setVat( $dVat );
00254
00255
00256 if ( $this->_blFreeShipping ) {
00257 return $this->_oPrice;
00258 }
00259
00260
00261 switch ( $this->oxdelivery__oxaddsumtype->value ) {
00262 case 'abs':
00263
00264 $dAmount = 0;
00265
00266 if ( $this->oxdelivery__oxfixed->value == 0 ) {
00267 $dAmount = 1;
00268 } elseif ( $this->oxdelivery__oxfixed->value == 1 ) {
00269 $dAmount = $this->_iProdCnt;
00270 } elseif ( $this->oxdelivery__oxfixed->value == 2 ) {
00271 $dAmount = $this->_iItemCnt;
00272 }
00273
00274 $oCur = $this->getConfig()->getActShopCurrencyObject();
00275 $this->_oPrice->add( $this->oxdelivery__oxaddsum->value * $oCur->rate );
00276 $this->_oPrice->multiply( $dAmount );
00277 break;
00278 case '%':
00279
00280 $this->_oPrice->add( $this->_dPrice /100 * $this->oxdelivery__oxaddsum->value );
00281 break;
00282 }
00283 }
00284
00285
00286 return $this->_oPrice;
00287 }
00288
00296 public function delete( $sOXID = null )
00297 {
00298 if ( !$sOXID ) {
00299 $sOXID = $this->getId();
00300 }
00301 if ( !$sOXID ) {
00302 return false;
00303 }
00304
00305
00306 $sQ = "delete from oxobject2delivery where oxobject2delivery.oxdeliveryid = '".$sOXID."' ";
00307 oxDb::getDb()->execute( $sQ );
00308
00309 return parent::delete( $sOXID );
00310 }
00311
00319 public function isForBasket( $oBasket )
00320 {
00321
00322 $blHasArticles = $this->hasArtices();
00323 $blHasCategories = $this->hasCategories();
00324 $blUse = true;
00325 $iAmount = 0;
00326
00327
00328 if ( $blHasCategories || $blHasArticles ) {
00329 $blUse = false;
00330
00331 $aDeliveryArticles = $this->getArticles();
00332 $aDeliveryCategories = $this->getCategories();
00333
00334 foreach ( $oBasket->getContents() as $oContent ) {
00335
00336
00337 $oArticle = $oContent->getArticle();
00338 $sProductId = $oArticle->getProductId();
00339 $sParentId = $oArticle->getProductParentId();
00340
00341 if ( $blHasArticles && (in_array( $sProductId, $aDeliveryArticles ) || ( $sParentId && in_array( $sParentId, $aDeliveryArticles ) ) ) ) {
00342 $blUse = true;
00343 $iAmount += $this->getDeliveryAmount( $oContent );
00344
00345 } elseif ( $blHasCategories ) {
00346
00347 if ( isset( self::$_aProductList[$sProductId] ) ) {
00348 $oProduct = self::$_aProductList[$sProductId];
00349 } else {
00350 $oProduct = oxNew( 'oxarticle' );
00351 $oProduct->setSkipAssign( true );
00352
00353 if ( !$oProduct->load( $sProductId ) ) {
00354 continue;
00355 }
00356
00357 $oProduct->setId($sProductId);
00358 self::$_aProductList[$sProductId] = $oProduct;
00359 }
00360
00361 foreach ( $aDeliveryCategories as $sCatId ) {
00362
00363 if ( $oProduct->inCategory( $sCatId ) ) {
00364 $blUse = true;
00365
00366 $iAmount += $this->getDeliveryAmount( $oContent );
00367
00368 break;
00369 }
00370 }
00371 }
00372 }
00373
00374 } else {
00375
00376 foreach ( $oBasket->getContents() as $oContent ) {
00377 $iAmount += $this->getDeliveryAmount( $oContent );
00378 }
00379 }
00380
00381 $blForBasket = false;
00382
00383 if ( $blUse && ( $this->_checkDeliveryAmount($iAmount) || $this->_blFreeShipping ) ) {
00384 $blForBasket = true;
00385 }
00386 return $blForBasket;
00387 }
00388
00396 protected function _checkDeliveryAmount($iAmount)
00397 {
00398 switch ( $this->oxdelivery__oxdeltype->value ) {
00399 case 'p':
00400 $oCur = $this->getConfig()->getActShopCurrencyObject();
00401 $iAmount /= $oCur->rate;
00402 break;
00403 case 'w':
00404 case 's':
00405 case 'a':
00406 break;
00407 }
00408
00409 if ( $iAmount >= $this->oxdelivery__oxparam->value && $iAmount <= $this->oxdelivery__oxparamend->value ) {
00410 return true;
00411 }
00412
00413 return false;
00414 }
00415 }