oxbasketitem.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxBasketItem extends oxSuperCfg
00007 {
00013     protected $_sProductId = null;
00014 
00020     protected $_sTitle = null;
00021 
00027     protected $_sVarSelect = null;
00028 
00034     protected $_sIcon = null;
00035 
00041     protected $_sLink = null;
00042 
00048     protected $_oPrice = null;
00049 
00055     protected $_oUnitPrice = null;
00056 
00062     protected $_dAmount = 0.0;
00063 
00069     protected $_dWeight = 0;
00070 
00076     protected $_aSelList = array();
00077 
00083     protected $_sShopId = null;
00084 
00090     protected $_sNativeShopId = null;
00091 
00097     protected $_blSkipDiscounts = false;
00098 
00104     protected $_aPersistentParameters = array();
00105 
00111     protected $_blBundle = false;
00112 
00118     protected $_blIsDiscountArticle = false;
00119 
00125     protected $_oArticle = null;
00126 
00132     protected $_sDimageDirNoSsl = null;
00133 
00139     protected $_sDimageDirSsl = null;
00140 
00146     protected $_aChosenSelectlist = array();
00147 
00153     protected $_sWrappingId = null;
00154 
00160      protected $_sWishId = null;
00161 
00167     protected $_sWishArticleId = null;
00168 
00174     protected $_blCheckArticleStock = true;
00175 
00194     public function init( $sProductID, $dAmount, $aSel = null, $aPersParam = null, $blBundle = null )
00195     {
00196         $this->_setArticle( $sProductID );
00197         $this->setAmount( $dAmount );
00198         $this->_setSelectList( $aSel );
00199         $this->setPersParams( $aPersParam );
00200         $this->setBundle( $blBundle );
00201     }
00202 
00216     public function initFromOrderArticle( $oOrderArticle )
00217     {
00218         $this->_setFromOrderArticle( $oOrderArticle );
00219         $this->setAmount( $oOrderArticle->oxorderarticles__oxamount->value );
00220         $this->_setSelectList( $oOrderArticle->getOrderArticleSelectList() );
00221         $this->setPersParams( $oOrderArticle->getPersParams() );
00222     }
00223 
00231     public function setAsDiscountArticle( $blIsDiscountArticle )
00232     {
00233         $this->_blIsDiscountArticle = $blIsDiscountArticle;
00234     }
00235 
00243     public function setStockCheckStatus( $blStatus )
00244     {
00245         $this->_blCheckArticleStock = $blStatus;
00246     }
00247 
00253     public function getStockCheckStatus()
00254     {
00255         return $this->_blCheckArticleStock;
00256     }
00257 
00270     public function setAmount( $dAmount, $blOverride = true, $sItemKey = null )
00271     {
00272         //validating amount
00273         $oValidator = oxNew( 'oxinputvalidator' );
00274 
00275         try {
00276             $dAmount = $oValidator->validateBasketAmount( $dAmount );
00277         } catch( oxArticleInputException $oEx ) {
00278             $oEx->setArticleNr( $this->getProductId() );
00279             $oEx->setProductId( $this->getProductId() );
00280             // setting additional information for excp and then rethrowing
00281             throw $oEx;
00282         }
00283 
00284         $oArticle = $this->getArticle();
00285 
00286 
00287         // setting default
00288         $iOnStock = true;
00289 
00290         if ( $blOverride ) {
00291             $this->_dAmount  = $dAmount;
00292         } else {
00293             $this->_dAmount += $dAmount;
00294         }
00295 
00296         // checking for stock
00297         if ( $this->getStockCheckStatus() == true ) {
00298             $dArtStockAmount = $this->getSession()->getBasket()->getArtStockInBasket( $oArticle->getId(), $sItemKey );
00299             $iOnStock = $oArticle->checkForStock( $this->_dAmount, $dArtStockAmount );
00300             if ( $iOnStock !== true ) {
00301                 if ( $iOnStock === false ) {
00302                     // no stock !
00303                     $this->_dAmount = 0;
00304                 } else {
00305                     // limited stock
00306                     $this->_dAmount = $iOnStock;
00307                     $blOverride = true;
00308                 }
00309             }
00310         }
00311 
00312         // calculating general weight
00313         $this->_dWeight = $oArticle->oxarticles__oxweight->value * $this->_dAmount;
00314 
00315         if ( $iOnStock !== true ) {
00316             $oEx = oxNew( 'oxOutOfStockException' );
00317             $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
00318             $oEx->setArticleNr( $oArticle->oxarticles__oxartnum->value );
00319             $oEx->setProductId( $oArticle->getProductId() );
00320             $oEx->setRemainingAmount( $this->_dAmount );
00321             throw $oEx;
00322         }
00323     }
00324 
00332     public function setPrice( $oPrice )
00333     {
00334         $this->_oPrice = oxNew( 'oxprice' );
00335         $this->_oPrice->setBruttoPriceMode();
00336         $this->_oPrice->setVat( $oPrice->getVAT() );
00337         $this->_oPrice->addPrice( $oPrice );
00338         $this->_oPrice->multiply( $this->getAmount() );
00339 
00340         $this->_oUnitPrice = oxNew( 'oxprice' );
00341         $this->_oUnitPrice->setBruttoPriceMode();
00342         $this->_oUnitPrice->setVat( $oPrice->getVAT() );
00343         $this->_oUnitPrice->addPrice( $oPrice );
00344 
00345         $this->_setDeprecatedValues();
00346     }
00347 
00353     public function getImageUrl()
00354     {
00355         $blIsSSl = $this->getConfig()->isSsl();
00356         if ( $blIsSSl ) {
00357             return $this->_sDimageDirSsl;
00358         } else {
00359             return $this->_sDimageDirNoSsl;
00360         }
00361     }
00362 
00368     public function getIconUrl()
00369     {
00370         return $this->getConfig()->getIconUrl( $this->getIcon() );
00371     }
00372 
00385     public function getArticle( $blCheckProduct = true, $sProductId = null, $blDisableLazyLoading = false )
00386     {
00387         if ( $this->_oArticle === null || ( !$this->_oArticle->isOrderArticle() && $blDisableLazyLoading ) ) {
00388             $sProductId = $sProductId ? $sProductId : $this->_sProductId;
00389             if ( !$sProductId ) {
00390                 //this excpetion may not be caught, anyhow this is a critical exception
00391                 $oEx = oxNew( 'oxArticleException' );
00392                 $oEx->setMessage( 'EXCEPTION_ARTICLE_NOPRODUCTID' );
00393                 throw $oEx;
00394             }
00395 
00396             $this->_oArticle = oxNew( 'oxarticle' );
00397             // #M773 Do not use article lazy loading on order save
00398             if ( $blDisableLazyLoading ) {
00399                 $this->_oArticle->modifyCacheKey('_allviews');
00400                 $this->_oArticle->disableLazyLoading();
00401             }
00402 
00403             // performance:
00404             // - skipping variants loading
00405             // - skipping 'ab' price info
00406             // - load parent field
00407             $this->_oArticle->setNoVariantLoading( true );
00408             $this->_oArticle->setSkipAbPrice( true );
00409             $this->_oArticle->setLoadParentData( true );
00410             if ( !$this->_oArticle->load( $sProductId ) ) {
00411                 $oEx = oxNew( 'oxNoArticleException' );
00412                 $oLang = oxLang::getInstance();
00413                 $oEx->setMessage( sprintf($oLang->translateString( 'EXCEPTION_ARTICLE_ARTICELDOESNOTEXIST', $oLang->getBaseLanguage() ), $sProductId) );
00414                 $oEx->setArticleNr( $sProductId );
00415                 $oEx->setProductId( $sProductId );
00416                 throw $oEx;
00417             }
00418 
00419             // cant put not visible product to basket (M:1286)
00420             if ( $blCheckProduct && !$this->_oArticle->isVisible() ) {
00421                 $oEx = oxNew( 'oxNoArticleException' );
00422                 $oLang = oxLang::getInstance();
00423                 $oEx->setMessage( sprintf($oLang->translateString( 'EXCEPTION_ARTICLE_ARTICELDOESNOTEXIST', $oLang->getBaseLanguage() ), $this->_oArticle->oxarticles__oxartnum->value) );
00424                 $oEx->setArticleNr( $sProductId );
00425                 $oEx->setProductId( $sProductId );
00426                 throw $oEx;
00427             }
00428 
00429             // cant put not buyable product to basket
00430             if ( $blCheckProduct && !$this->_oArticle->isBuyable() ) {
00431                 $oEx = oxNew( 'oxArticleInputException' );
00432                 $oEx->setMessage( 'EXCEPTION_ARTICLE_ARTICELNOTBUYABLE' );
00433                 $oEx->setArticleNr( $sProductId );
00434                 $oEx->setProductId( $sProductId );
00435                 throw $oEx;
00436             }
00437         }
00438 
00439         return $this->_oArticle;
00440     }
00441 
00447     public function getdBundledAmount()
00448     {
00449         return $this->isBundle()?$this->_dAmount:0;
00450     }
00451 
00457     public function getPrice()
00458     {
00459         return $this->_oPrice;
00460     }
00461 
00467     public function getUnitPrice()
00468     {
00469         return $this->_oUnitPrice;
00470     }
00471 
00477     public function getAmount()
00478     {
00479         return $this->_dAmount;
00480     }
00481 
00487     public function getWeight()
00488     {
00489         return $this->_dWeight;
00490     }
00491 
00497     public function getTitle()
00498     {
00499         return $this->_sTitle;
00500     }
00501 
00507     public function getIcon()
00508     {
00509         return $this->_sIcon;
00510     }
00511 
00517     public function getLink()
00518     {
00519         return $this->_sLink;
00520     }
00521 
00527     public function getShopId()
00528     {
00529         return $this->_sShopId;
00530     }
00531 
00537     public function getSelList()
00538     {
00539         return $this->_aSelList;
00540     }
00541 
00547     public function getChosenSelList()
00548     {
00549         return $this->_aChosenSelectlist;
00550     }
00551 
00557     public function isBundle()
00558     {
00559         return $this->_blBundle;
00560     }
00561 
00567     public function isDiscountArticle()
00568     {
00569         return $this->_blIsDiscountArticle;
00570     }
00571 
00577     public function isSkipDiscount()
00578     {
00579         return $this->_blSkipDiscounts;
00580     }
00581 
00591     public function __get( $sName )
00592     {
00593         if ( $sName == 'oProduct' ) {
00594             return $this->getArticle();
00595         }
00596     }
00597 
00603     public function __sleep()
00604     {
00605         $aRet = array();
00606         foreach ( get_object_vars( $this ) as $sKey => $sVar ) {
00607             if ( $sKey != '_oArticle' ) {
00608                 $aRet[] = $sKey;
00609             }
00610         }
00611         return $aRet;
00612     }
00613 
00621     protected function _setDeprecatedValues()
00622     {
00623         $oUnitPrice = $this->getUnitPrice();
00624         $oPrice = $this->getPrice();
00625 
00626         // product VAT percent
00627         $this->vatPercent = $this->getVatPercent();
00628 
00629         // VAT value
00630         $this->dvat = $oUnitPrice->getVATValue();
00631 
00632         // unit non formatted price
00633         $this->dprice = $oUnitPrice->getBruttoPrice();
00634 
00635         // formatted unit price
00636         $this->fprice = $this->getFUnitPrice();
00637 
00638         // non formatted unit NETTO price
00639         $this->dnetprice = $oUnitPrice->getNettoPrice();
00640 
00641         // non formatted total NETTO price
00642         $this->dtotalnetprice = $oPrice->getNettoPrice();
00643 
00644         // formatter total NETTO price
00645         $this->ftotalnetprice = oxLang::getInstance()->formatCurrency( $oPrice->getNettoPrice() );
00646 
00647         // non formatted total BRUTTO price
00648         $this->dtotalprice = $oPrice->getBruttoPrice();
00649 
00650         // formatted total BRUTTO price
00651         $this->ftotalprice = $this->getFTotalPrice();
00652 
00653         // total VAT
00654         $this->dtotalvat = $oPrice->getVATValue();
00655 
00656         // formatted title
00657         $this->title = $this->getTitle();
00658 
00659         // icon URL
00660         $this->icon  = $this->getIcon();
00661 
00662         // details URL
00663         $this->link  = $this->getLink();
00664 
00665         // amount of items in basket
00666         $this->dAmount  = $this->getAmount();
00667 
00668         // weight
00669         $this->dWeight  = $this->getWeight();
00670 
00671         // select list
00672         $this->aSelList = $this->getSelList();
00673 
00674         // product id
00675         $this->sProduct = $this->getProductId();
00676 
00677         // product id
00678         $this->varselect = $this->getVarSelect();
00679 
00680         // is bundle ?
00681         $this->blBundle = $this->isBundle();
00682 
00683         // bundle amount
00684         $this->dBundledAmount = $this->getdBundledAmount();
00685 
00686         // skip discounts ?
00687         $this->blSkipDiscounts     = $this->isSkipDiscount();
00688 
00689         // is discount item ?
00690         $this->blIsDiscountArticle = $this->isDiscountArticle();
00691 
00692         // dyn image location
00693         $this->dimagedir = $this->getImageUrl();
00694 
00695         // setting wrapping paper info
00696         $this->wrapping  = $this->getWrappingId();
00697 
00698         $this->oWrap = $this->getWrapping();
00699 
00700         $this->aPersParam = $this->getPersParams();
00701 
00702         //chosen select list
00703         $this->chosen_selectlist = $this->getChosenSelList();
00704     }
00705 
00723     protected function _setArticle( $sProductId )
00724     {
00725         $oArticle = $this->getArticle( true, $sProductId );
00726 
00727         // product ID
00728         $this->_sProductId = $sProductId;
00729 
00730         // products title
00731         $this->_sTitle = $oArticle->oxarticles__oxtitle->value;
00732         if ( $oArticle->oxarticles__oxvarselect->value ) {
00733             $this->_sTitle     = $this->_sTitle. ', ' . $oArticle->oxarticles__oxvarselect->value;
00734             $this->_sVarSelect = $oArticle->oxarticles__oxvarselect->value;
00735         }
00736 
00737         // icon and details URL's
00738         $this->_sIcon = $oArticle->oxarticles__oxicon->value;
00739         $this->_sLink = $oArticle->getLink();
00740 
00741         // shop Ids
00742         $this->_sShopId       = $this->getConfig()->getShopId();
00743         $this->_sNativeShopId = $oArticle->oxarticles__oxshopid->value;
00744 
00745         // SSL/NON SSL image paths
00746         $this->_sDimageDirNoSsl = $oArticle->nossl_dimagedir;
00747         $this->_sDimageDirSsl   = $oArticle->ssl_dimagedir;
00748     }
00749 
00761     protected function _setFromOrderArticle( $oOrderArticle )
00762     {
00763         // overriding whole article
00764         $this->_oArticle = $oOrderArticle;
00765 
00766         // product ID
00767         $this->_sProductId = $oOrderArticle->getProductId();
00768 
00769         // products title
00770         $this->_sTitle = $oOrderArticle->oxarticles__oxtitle->value;
00771 
00772         // shop Ids
00773         $this->_sShopId       = $this->getConfig()->getShopId();
00774         $this->_sNativeShopId = $oOrderArticle->oxarticles__oxshopid->value;
00775     }
00776 
00784     protected function _setSelectList( $aSelList )
00785     {
00786         // checking for default select list
00787         $aSelectLists = $this->getArticle()->getSelectLists();
00788         if ( !$aSelList || is_array($aSelList) && count($aSelList) == 0 ) {
00789             if ( $iSelCnt = count( $aSelectLists ) ) {
00790                 $aSelList = array_fill( 0, $iSelCnt, '0' );
00791             }
00792         }
00793 
00794         $this->_aSelList = $aSelList;
00795 
00796         //
00797         if ( count( $this->_aSelList ) && is_array($this->_aSelList) ) {
00798             foreach ( $this->_aSelList as $conkey => $iSel ) {
00799                 $this->_aChosenSelectlist[$conkey] = new Oxstdclass();
00800                 $this->_aChosenSelectlist[$conkey]->name  = $aSelectLists[$conkey]['name'];
00801                 $this->_aChosenSelectlist[$conkey]->value = $aSelectLists[$conkey][$iSel]->name;
00802             }
00803         }
00804     }
00805 
00811     public function getPersParams()
00812     {
00813         return $this->_aPersistentParameters;
00814     }
00815 
00823     public function setPersParams( $aPersParam )
00824     {
00825         $this->_aPersistentParameters = $aPersParam;
00826     }
00827 
00835     public function setBundle( $blBundle )
00836     {
00837         $this->_blBundle = $blBundle;
00838     }
00839 
00847     public function setSkipDiscounts( $blSkip )
00848     {
00849         $this->_blSkipDiscounts = $blSkip;
00850     }
00851 
00857     public function getProductId()
00858     {
00859         return $this->_sProductId;
00860     }
00861 
00869     public function setWrapping( $sWrapId )
00870     {
00871         $this->_sWrappingId = $sWrapId;
00872     }
00873 
00879     public function getWrappingId()
00880     {
00881         return $this->_sWrappingId;
00882     }
00883 
00889     public function getWrapping()
00890     {
00891         $oWrap = null;
00892         if ( $sWrapId = $this->getWrappingId() ) {
00893             $oWrap = oxNew( 'oxwrapping' );
00894             $oWrap->load( $sWrapId );
00895         }
00896         return $oWrap;
00897     }
00898 
00904     public function getWishId()
00905     {
00906         return $this->_sWishId;
00907     }
00908 
00916     public function setWishId( $sWishId )
00917     {
00918         $this->_sWishId = $sWishId;
00919     }
00920 
00928     public function setWishArticleId( $sArticleId )
00929     {
00930         $this->_sWishArticleId = $sArticleId;
00931     }
00932 
00938     public function getWishArticleId()
00939     {
00940         return $this->_sWishArticleId;
00941     }
00942 
00948     public function getFUnitPrice()
00949     {
00950         return oxLang::getInstance()->formatCurrency( $this->getUnitPrice()->getBruttoPrice() );
00951     }
00952 
00958     public function getFTotalPrice()
00959     {
00960         return oxLang::getInstance()->formatCurrency( $this->getPrice()->getBruttoPrice() );
00961     }
00962 
00968     public function getVatPercent()
00969     {
00970         return oxLang::getInstance()->formatVat( $this->getPrice()->getVat() );
00971     }
00972 
00978     public function getVarSelect()
00979     {
00980         return $this->_sVarSelect;
00981     }
00982 }