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