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 getDeliveryPrice( $dVat = null )
00230 {
00231
00232 if ( !$this->_oPrice ) {
00233 $this->_oPrice = oxNew( 'oxPrice' );
00234
00235 if ( !$this->_blDelVatOnTop ) {
00236 $this->_oPrice->setBruttoPriceMode();
00237 } else {
00238 $this->_oPrice->setNettoPriceMode();
00239 }
00240
00241 $this->_oPrice->setVat( $dVat );
00242 }
00243
00244
00245 if ( $this->_blFreeShipping ) {
00246 return $this->_oPrice;
00247 }
00248
00249
00250 switch ( $this->oxdelivery__oxaddsumtype->value ) {
00251 case 'abs':
00252
00253 $dAmount = 0;
00254
00255 if ( $this->oxdelivery__oxfixed->value == 0 ) {
00256 $dAmount = 1;
00257 } elseif ( $this->oxdelivery__oxfixed->value == 1 ) {
00258 $dAmount = $this->_iProdCnt;
00259 } elseif ( $this->oxdelivery__oxfixed->value == 2 ) {
00260 $dAmount = $this->_iItemCnt;
00261 }
00262
00263 $oCur = $this->getConfig()->getActShopCurrencyObject();
00264 $this->_oPrice->add( $this->oxdelivery__oxaddsum->value * $oCur->rate );
00265 $this->_oPrice->multiply( $dAmount );
00266 break;
00267 case '%':
00268
00269 $this->_oPrice->add( $this->_dPrice /100 * $this->oxdelivery__oxaddsum->value );
00270 break;
00271 }
00272
00273
00274 return $this->_oPrice;
00275 }
00276
00284 public function delete( $sOXID = null )
00285 {
00286 if ( !$sOXID ) {
00287 $sOXID = $this->getId();
00288 }
00289 if ( !$sOXID ) {
00290 return false;
00291 }
00292
00293
00294 $sQ = "delete from oxobject2delivery where oxobject2delivery.oxdeliveryid = '".$sOXID."' ";
00295 oxDb::getDb()->execute( $sQ );
00296
00297 return parent::delete( $sOXID );
00298 }
00299
00307 public function isForBasket( $oBasket )
00308 {
00309
00310 $blHasArticles = $this->hasArtices();
00311 $blHasCategories = $this->hasCategories();
00312 $blUse = true;
00313 $iAmount = 0;
00314
00315
00316 if ( $blHasCategories || $blHasArticles ) {
00317 $blUse = false;
00318
00319 $aDeliveryArticles = $this->getArticles();
00320 $aDeliveryCategories = $this->getCategories();
00321
00322 foreach ( $oBasket->getContents() as $oContent ) {
00323
00324
00325 $oArticle = $oContent->getArticle();
00326 $sProductId = $oArticle->getId();
00327 $sParentId = $oArticle->oxarticles__oxparentid->value ? $oArticle->oxarticles__oxparentid->value : false;
00328
00329 if ( $blHasArticles && (in_array( $sProductId, $aDeliveryArticles ) || ( $sParentId && in_array( $sParentId, $aDeliveryArticles ) ) ) ) {
00330 $blUse = true;
00331 $iAmount += $this->getDeliveryAmount( $oContent );
00332
00333 } elseif ( $blHasCategories ) {
00334
00335 if ( isset( self::$_aProductList[$sProductId] ) ) {
00336 $oProduct = self::$_aProductList[$sProductId];
00337 } else {
00338 $oProduct = oxNew( 'oxarticle' );
00339 $oProduct->setSkipAssign( true );
00340
00341 if ( !$oProduct->load( $sProductId ) ) {
00342 continue;
00343 }
00344
00345 $oProduct->setId($sProductId);
00346 self::$_aProductList[$sProductId] = $oProduct;
00347 }
00348
00349 foreach ( $aDeliveryCategories as $sCatId ) {
00350
00351 if ( $oProduct->inCategory( $sCatId ) ) {
00352 $blUse = true;
00353
00354 $iAmount += $this->getDeliveryAmount( $oContent );
00355
00356 break;
00357 }
00358 }
00359 }
00360 }
00361
00362 } else {
00363
00364 foreach ( $oBasket->getContents() as $oContent ) {
00365 $iAmount += $this->getDeliveryAmount( $oContent );
00366 }
00367 }
00368
00369 $blForBasket = false;
00370 if ( $blUse && $this->_checkDeliveryAmount($iAmount) ) {
00371 $blForBasket = true;
00372 }
00373 return $blForBasket;
00374 }
00375
00383 protected function _checkDeliveryAmount($iAmount)
00384 {
00385 switch ( $this->oxdelivery__oxdeltype->value ) {
00386 case 'p':
00387 $oCur = $this->getConfig()->getActShopCurrencyObject();
00388 $iAmount /= $oCur->rate;
00389 break;
00390 case 'w':
00391 case 's':
00392 case 'a':
00393 break;
00394 }
00395
00396 if ( $iAmount >= $this->oxdelivery__oxparam->value && $iAmount <= $this->oxdelivery__oxparamend->value ) {
00397 return true;
00398 }
00399
00400 return false;
00401 }
00402 }