00001 <?php
00002
00008 class oxDelivery extends oxI18n
00009 {
00015 protected $_sCoreTbl = 'oxdelivery';
00016
00022 protected $_sClassName = 'oxdelivery';
00023
00030 protected $_iItemCnt = 0;
00031
00038 protected $_iProdCnt = 0;
00039
00046 protected $_dPrice = 0;
00047
00053 protected $_oPrice = null;
00054
00060 protected $_aArtIds = null;
00061
00067 protected $_aCatIds = null;
00068
00074 protected $_blFreeShipping = true;
00075
00081 protected static $_aProductList = array();
00082
00088 protected $_blDelVatOnTop = false;
00089
00093 public function __construct()
00094 {
00095 parent::__construct();
00096 $this->init( 'oxdelivery' );
00097 $this->setDelVatOnTop( $this->getConfig()->getConfigParam( 'blDeliveryVatOnTop' ) );
00098 }
00099
00107 public function setDelVatOnTop( $blOnTop )
00108 {
00109 $this->_blDelVatOnTop = $blOnTop;
00110 }
00111
00117 public function getArticles()
00118 {
00119 if ( $this->_aArtIds !== null ) {
00120 return $this->_aArtIds;
00121 }
00122
00123 $sQ = 'select oxobjectid from oxobject2delivery where oxdeliveryid="'.$this->getId().'" and oxtype = "oxarticles" ';
00124
00125 $aArtIds = oxDb::getDb()->getArray( $sQ );
00126
00127
00128 foreach ( $aArtIds AS $aItem ) {
00129 $this->_aArtIds[] = $aItem[0];
00130 }
00131
00132 return $this->_aArtIds;
00133
00134 }
00135
00141 public function getCategories()
00142 {
00143 if ( $this->_aCatIds !== null ) {
00144 return $this->_aCatIds;
00145 }
00146
00147 $sQ = 'select oxobjectid from oxobject2delivery where oxdeliveryid="'.$this->getId().'" and oxtype = "oxcategories" ';
00148
00149 $aCatIds = oxDb::getDb()->getAll( $sQ );
00150
00151
00152 foreach ( $aCatIds AS $aItem ) {
00153 $this->_aCatIds[] = $aItem[0];
00154 }
00155
00156 return $this->_aCatIds;
00157 }
00158
00164 public function hasArtices()
00165 {
00166 return ( bool ) count( $this->getArticles() );
00167 }
00168
00174 public function hasCategories()
00175 {
00176 return ( bool ) count( $this->getCategories() );
00177 }
00178
00186 public function getDeliveryAmount( $oBasketItem )
00187 {
00188 $dAmount = 0 ;
00189
00190 $blExclNonMaterial = $this->getConfig()->getConfigParam( 'blExclNonMaterialFromDelivery' );
00191
00192 if ( !$oBasketItem->getArticle()->oxarticles__oxfreeshipping->value &&
00193 !( $oBasketItem->getArticle()->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 += $oBasketItem->getArticle()->oxarticles__oxlength->value *
00206 $oBasketItem->getArticle()->oxarticles__oxwidth->value *
00207 $oBasketItem->getArticle()->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 getDeliveryPrice( $dVat = null )
00231 {
00232
00233 if ( !$this->_oPrice ) {
00234 $this->_oPrice = oxNew( 'oxPrice' );
00235
00236 if ( !$this->_blDelVatOnTop ) {
00237 $this->_oPrice->setBruttoPriceMode();
00238 } else {
00239 $this->_oPrice->setNettoPriceMode();
00240 }
00241
00242 $this->_oPrice->setVat( $dVat );
00243 }
00244
00245
00246 if ( $this->_blFreeShipping ) {
00247 return $this->_oPrice;
00248 }
00249
00250
00251 switch ( $this->oxdelivery__oxaddsumtype->value ) {
00252 case 'abs':
00253
00254 $dAmount = 0;
00255
00256 if ( $this->oxdelivery__oxfixed->value == 0 ) {
00257 $dAmount = 1;
00258 } elseif ( $this->oxdelivery__oxfixed->value == 1 ) {
00259 $dAmount = $this->_iProdCnt;
00260 } elseif ( $this->oxdelivery__oxfixed->value == 2 ) {
00261 $dAmount = $this->_iItemCnt;
00262 }
00263
00264 $oCur = $this->getConfig()->getActShopCurrencyObject();
00265 $this->_oPrice->add( $this->oxdelivery__oxaddsum->value * $oCur->rate );
00266 $this->_oPrice->multiply( $dAmount );
00267 break;
00268 case '%':
00269
00270 $this->_oPrice->add( $this->_dPrice /100 * $this->oxdelivery__oxaddsum->value );
00271 break;
00272 }
00273
00274
00275 return $this->_oPrice;
00276 }
00277
00285 public function delete( $sOXID = null )
00286 {
00287 if ( !$sOXID ) {
00288 $sOXID = $this->getId();
00289 }
00290 if ( !$sOXID ) {
00291 return false;
00292 }
00293
00294
00295 $sQ = "delete from oxobject2delivery where oxobject2delivery.oxdeliveryid = '".$sOXID."' ";
00296 oxDb::getDb()->execute( $sQ );
00297
00298 return parent::delete( $sOXID );
00299 }
00300
00308 public function isForBasket( $oBasket )
00309 {
00310
00311 $blHasArticles = $this->hasArtices();
00312 $blHasCategories = $this->hasCategories();
00313 $blUse = true;
00314 $iAmount = 0;
00315
00316
00317 if ( $blHasCategories || $blHasArticles ) {
00318 $blUse = false;
00319
00320 $aDeliveryArticles = $this->getArticles();
00321 $aDeliveryCategories = $this->getCategories();
00322
00323 foreach ( $oBasket->getContents() as $oContent ) {
00324
00325
00326 $oArticle = $oContent->getArticle();
00327 $sProductId = $oArticle->getId();
00328 $sParentId = $oArticle->oxarticles__oxparentid->value ? $oArticle->oxarticles__oxparentid->value : false;
00329
00330 if ( $blHasArticles && (in_array( $sProductId, $aDeliveryArticles ) || ( $sParentId && in_array( $sParentId, $aDeliveryArticles ) ) ) ) {
00331 $blUse = true;
00332 $iAmount += $this->getDeliveryAmount( $oContent );
00333
00334 } elseif ( $blHasCategories ) {
00335
00336 if ( isset( self::$_aProductList[$sProductId] ) ) {
00337 $oProduct = self::$_aProductList[$sProductId];
00338 } else {
00339 $oProduct = oxNew( 'oxarticle' );
00340 $oProduct->setSkipAssign( true );
00341
00342 if ( !$oProduct->load( $sProductId ) ) {
00343 continue;
00344 }
00345
00346 $oProduct->setId($sProductId);
00347 self::$_aProductList[$sProductId] = $oProduct;
00348 }
00349
00350 foreach ( $aDeliveryCategories as $sCatId ) {
00351
00352 if ( $oProduct->inCategory( $sCatId ) ) {
00353 $blUse = true;
00354
00355 $iAmount += $this->getDeliveryAmount( $oContent );
00356
00357 break;
00358 }
00359 }
00360 }
00361 }
00362
00363 } else {
00364
00365 foreach ( $oBasket->getContents() as $oContent ) {
00366 $iAmount += $this->getDeliveryAmount( $oContent );
00367 }
00368 }
00369
00370 $blForBasket = false;
00371 if ( $blUse && $this->_checkDeliveryAmount($iAmount) ) {
00372 $blForBasket = true;
00373 }
00374 return $blForBasket;
00375 }
00376
00384 protected function _checkDeliveryAmount($iAmount)
00385 {
00386 switch ( $this->oxdelivery__oxdeltype->value ) {
00387 case 'p':
00388 $oCur = $this->getConfig()->getActShopCurrencyObject();
00389 $iAmount /= $oCur->rate;
00390 break;
00391 case 'w':
00392 case 's':
00393 case 'a':
00394 break;
00395 }
00396
00397 if ( $iAmount >= $this->oxdelivery__oxparam->value && $iAmount <= $this->oxdelivery__oxparamend->value ) {
00398 return true;
00399 }
00400
00401 return false;
00402 }
00403 }