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 
00176 
00182     protected $_iLanguageId = null;
00183     
00202     public function init( $sProductID, $dAmount, $aSel = null, $aPersParam = null, $blBundle = null )
00203     {
00204         $this->_setArticle( $sProductID );
00205         $this->setAmount( $dAmount );
00206         $this->_setSelectList( $aSel );
00207         $this->setPersParams( $aPersParam );
00208         $this->setBundle( $blBundle );
00209         $this->setLanguageId( oxLang::getInstance()->getBaseLanguage() );
00210     }
00211 
00225     public function initFromOrderArticle( $oOrderArticle )
00226     {
00227         $this->_setFromOrderArticle( $oOrderArticle );
00228         $this->setAmount( $oOrderArticle->oxorderarticles__oxamount->value );
00229         $this->_setSelectList( $oOrderArticle->getOrderArticleSelectList() );
00230         $this->setPersParams( $oOrderArticle->getPersParams() );
00231     }
00232 
00240     public function setAsDiscountArticle( $blIsDiscountArticle )
00241     {
00242         $this->_blIsDiscountArticle = $blIsDiscountArticle;
00243     }
00244 
00252     public function setStockCheckStatus( $blStatus )
00253     {
00254         $this->_blCheckArticleStock = $blStatus;
00255     }
00256 
00262     public function getStockCheckStatus()
00263     {
00264         return $this->_blCheckArticleStock;
00265     }
00266 
00279     public function setAmount( $dAmount, $blOverride = true, $sItemKey = null )
00280     {
00281         //validating amount
00282         $oValidator = oxNew( 'oxinputvalidator' );
00283 
00284         try {
00285             $dAmount = $oValidator->validateBasketAmount( $dAmount );
00286         } catch( oxArticleInputException $oEx ) {
00287             $oEx->setArticleNr( $this->getProductId() );
00288             $oEx->setProductId( $this->getProductId() );
00289             // setting additional information for excp and then rethrowing
00290             throw $oEx;
00291         }
00292 
00293         $oArticle = $this->getArticle();
00294 
00295 
00296         // setting default
00297         $iOnStock = true;
00298 
00299         if ( $blOverride ) {
00300             $this->_dAmount  = $dAmount;
00301         } else {
00302             $this->_dAmount += $dAmount;
00303         }
00304 
00305         // checking for stock
00306         if ( $this->getStockCheckStatus() == true ) {
00307             $dArtStockAmount = $this->getSession()->getBasket()->getArtStockInBasket( $oArticle->getId(), $sItemKey );
00308             $iOnStock = $oArticle->checkForStock( $this->_dAmount, $dArtStockAmount );
00309             if ( $iOnStock !== true ) {
00310                 if ( $iOnStock === false ) {
00311                     // no stock !
00312                     $this->_dAmount = 0;
00313                 } else {
00314                     // limited stock
00315                     $this->_dAmount = $iOnStock;
00316                     $blOverride = true;
00317                 }
00318             }
00319         }
00320 
00321         // calculating general weight
00322         $this->_dWeight = $oArticle->oxarticles__oxweight->value * $this->_dAmount;
00323 
00324         if ( $iOnStock !== true ) {
00325             $oEx = oxNew( 'oxOutOfStockException' );
00326             $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
00327             $oEx->setArticleNr( $oArticle->oxarticles__oxartnum->value );
00328             $oEx->setProductId( $oArticle->getProductId() );
00329             $oEx->setRemainingAmount( $this->_dAmount );
00330             throw $oEx;
00331         }
00332     }
00333 
00341     public function setPrice( $oPrice )
00342     {
00343         $this->_oPrice = oxNew( 'oxprice' );
00344         $this->_oPrice->setBruttoPriceMode();
00345         $this->_oPrice->setVat( $oPrice->getVAT() );
00346         $this->_oPrice->addPrice( $oPrice );
00347         $this->_oPrice->multiply( $this->getAmount() );
00348 
00349         $this->_oUnitPrice = oxNew( 'oxprice' );
00350         $this->_oUnitPrice->setBruttoPriceMode();
00351         $this->_oUnitPrice->setVat( $oPrice->getVAT() );
00352         $this->_oUnitPrice->addPrice( $oPrice );
00353 
00354         $this->_setDeprecatedValues();
00355     }
00356 
00362     public function getImageUrl()
00363     {
00364         $blIsSSl = $this->getConfig()->isSsl();
00365         if ( $blIsSSl ) {
00366             return $this->_sDimageDirSsl;
00367         } else {
00368             return $this->_sDimageDirNoSsl;
00369         }
00370     }
00371 
00377     public function getIconUrl()
00378     {
00379         return $this->getConfig()->getIconUrl( $this->getIcon() );
00380     }
00381 
00394     public function getArticle( $blCheckProduct = true, $sProductId = null, $blDisableLazyLoading = false )
00395     {
00396         if ( $this->_oArticle === null || ( !$this->_oArticle->isOrderArticle() && $blDisableLazyLoading ) ) {
00397             $sProductId = $sProductId ? $sProductId : $this->_sProductId;
00398             if ( !$sProductId ) {
00399                 //this excpetion may not be caught, anyhow this is a critical exception
00400                 $oEx = oxNew( 'oxArticleException' );
00401                 $oEx->setMessage( 'EXCEPTION_ARTICLE_NOPRODUCTID' );
00402                 throw $oEx;
00403             }
00404 
00405             $this->_oArticle = oxNew( 'oxarticle' );
00406             // #M773 Do not use article lazy loading on order save
00407             if ( $blDisableLazyLoading ) {
00408                 $this->_oArticle->modifyCacheKey('_allviews');
00409                 $this->_oArticle->disableLazyLoading();
00410             }
00411 
00412             // performance:
00413             // - skipping variants loading
00414             // - skipping 'ab' price info
00415             // - load parent field
00416             $this->_oArticle->setNoVariantLoading( true );
00417             $this->_oArticle->setSkipAbPrice( true );
00418             $this->_oArticle->setLoadParentData( true );
00419             if ( !$this->_oArticle->load( $sProductId ) ) {
00420                 $oEx = oxNew( 'oxNoArticleException' );
00421                 $oLang = oxLang::getInstance();
00422                 $oEx->setMessage( sprintf($oLang->translateString( 'EXCEPTION_ARTICLE_ARTICELDOESNOTEXIST', $oLang->getBaseLanguage() ), $sProductId) );
00423                 $oEx->setArticleNr( $sProductId );
00424                 $oEx->setProductId( $sProductId );
00425                 throw $oEx;
00426             }
00427 
00428             // cant put not visible product to basket (M:1286)
00429             if ( $blCheckProduct && !$this->_oArticle->isVisible() ) {
00430                 $oEx = oxNew( 'oxNoArticleException' );
00431                 $oLang = oxLang::getInstance();
00432                 $oEx->setMessage( sprintf($oLang->translateString( 'EXCEPTION_ARTICLE_ARTICELDOESNOTEXIST', $oLang->getBaseLanguage() ), $this->_oArticle->oxarticles__oxartnum->value) );
00433                 $oEx->setArticleNr( $sProductId );
00434                 $oEx->setProductId( $sProductId );
00435                 throw $oEx;
00436             }
00437 
00438             // cant put not buyable product to basket
00439             if ( $blCheckProduct && !$this->_oArticle->isBuyable() ) {
00440                 $oEx = oxNew( 'oxArticleInputException' );
00441                 $oEx->setMessage( 'EXCEPTION_ARTICLE_ARTICELNOTBUYABLE' );
00442                 $oEx->setArticleNr( $sProductId );
00443                 $oEx->setProductId( $sProductId );
00444                 throw $oEx;
00445             }
00446         }
00447 
00448         return $this->_oArticle;
00449     }
00450 
00456     public function getdBundledAmount()
00457     {
00458         return $this->isBundle()?$this->_dAmount:0;
00459     }
00460 
00466     public function getPrice()
00467     {
00468         return $this->_oPrice;
00469     }
00470 
00476     public function getUnitPrice()
00477     {
00478         return $this->_oUnitPrice;
00479     }
00480 
00486     public function getAmount()
00487     {
00488         return $this->_dAmount;
00489     }
00490 
00496     public function getWeight()
00497     {
00498         return $this->_dWeight;
00499     }
00500 
00506     public function getTitle()
00507     {
00508         if ( $this->_sTitle === null || $this->getLanguageId() != oxLang::getInstance()->getBaseLanguage() ) {
00509 
00510             $this->setLanguageId( oxLang::getInstance()->getBaseLanguage() );    
00511             
00512             $oArticle = $this->getArticle( );
00513             $this->_sTitle = $oArticle->oxarticles__oxtitle->value;
00514             
00515             if ( $oArticle->oxarticles__oxvarselect->value ) {
00516                 $this->_sTitle = $this->_sTitle. ', ' . $this->getVarSelect();
00517             }
00518         }
00519         
00520         return $this->_sTitle;
00521     }
00522 
00528     public function getIcon()
00529     {
00530         return $this->_sIcon;
00531     }
00532 
00538     public function getLink()
00539     {
00540         return $this->_sLink;
00541     }
00542 
00548     public function getShopId()
00549     {
00550         return $this->_sShopId;
00551     }
00552 
00558     public function getSelList()
00559     {
00560         return $this->_aSelList;
00561     }
00562 
00568     public function getChosenSelList()
00569     {
00570         return $this->_aChosenSelectlist;
00571     }
00572 
00578     public function isBundle()
00579     {
00580         return $this->_blBundle;
00581     }
00582 
00588     public function isDiscountArticle()
00589     {
00590         return $this->_blIsDiscountArticle;
00591     }
00592 
00598     public function isSkipDiscount()
00599     {
00600         return $this->_blSkipDiscounts;
00601     }
00602 
00612     public function __get( $sName )
00613     {
00614         if ( $sName == 'oProduct' ) {
00615             return $this->getArticle();
00616         }
00617     }
00618 
00624     public function __sleep()
00625     {
00626         $aRet = array();
00627         foreach ( get_object_vars( $this ) as $sKey => $sVar ) {
00628             if ( $sKey != '_oArticle' ) {
00629                 $aRet[] = $sKey;
00630             }
00631         }
00632         return $aRet;
00633     }
00634 
00642     protected function _setDeprecatedValues()
00643     {
00644         $oUnitPrice = $this->getUnitPrice();
00645         $oPrice = $this->getPrice();
00646 
00647         // product VAT percent
00648         $this->vatPercent = $this->getVatPercent();
00649 
00650         // VAT value
00651         $this->dvat = $oUnitPrice->getVATValue();
00652 
00653         // unit non formatted price
00654         $this->dprice = $oUnitPrice->getBruttoPrice();
00655 
00656         // formatted unit price
00657         $this->fprice = $this->getFUnitPrice();
00658 
00659         // non formatted unit NETTO price
00660         $this->dnetprice = $oUnitPrice->getNettoPrice();
00661 
00662         // non formatted total NETTO price
00663         $this->dtotalnetprice = $oPrice->getNettoPrice();
00664 
00665         // formatter total NETTO price
00666         $this->ftotalnetprice = oxLang::getInstance()->formatCurrency( $oPrice->getNettoPrice() );
00667 
00668         // non formatted total BRUTTO price
00669         $this->dtotalprice = $oPrice->getBruttoPrice();
00670 
00671         // formatted total BRUTTO price
00672         $this->ftotalprice = $this->getFTotalPrice();
00673 
00674         // total VAT
00675         $this->dtotalvat = $oPrice->getVATValue();
00676 
00677         // formatted title
00678         $this->title = $this->getTitle();
00679 
00680         // icon URL
00681         $this->icon  = $this->getIcon();
00682 
00683         // details URL
00684         $this->link  = $this->getLink();
00685 
00686         // amount of items in basket
00687         $this->dAmount  = $this->getAmount();
00688 
00689         // weight
00690         $this->dWeight  = $this->getWeight();
00691 
00692         // select list
00693         $this->aSelList = $this->getSelList();
00694 
00695         // product id
00696         $this->sProduct = $this->getProductId();
00697 
00698         // product id
00699         $this->varselect = $this->getVarSelect();
00700 
00701         // is bundle ?
00702         $this->blBundle = $this->isBundle();
00703 
00704         // bundle amount
00705         $this->dBundledAmount = $this->getdBundledAmount();
00706 
00707         // skip discounts ?
00708         $this->blSkipDiscounts     = $this->isSkipDiscount();
00709 
00710         // is discount item ?
00711         $this->blIsDiscountArticle = $this->isDiscountArticle();
00712 
00713         // dyn image location
00714         $this->dimagedir = $this->getImageUrl();
00715 
00716         // setting wrapping paper info
00717         $this->wrapping  = $this->getWrappingId();
00718 
00719         $this->oWrap = $this->getWrapping();
00720 
00721         $this->aPersParam = $this->getPersParams();
00722 
00723         //chosen select list
00724         $this->chosen_selectlist = $this->getChosenSelList();
00725     }
00726 
00744     protected function _setArticle( $sProductId )
00745     {
00746         $oArticle = $this->getArticle( true, $sProductId );
00747 
00748         // product ID
00749         $this->_sProductId = $sProductId;
00750 
00751         $this->getTitle();
00752        
00753         // icon and details URL's
00754         $this->_sIcon = $oArticle->oxarticles__oxicon->value;
00755         $this->_sLink = $oArticle->getLink();
00756 
00757         // shop Ids
00758         $this->_sShopId       = $this->getConfig()->getShopId();
00759         $this->_sNativeShopId = $oArticle->oxarticles__oxshopid->value;
00760 
00761         // SSL/NON SSL image paths
00762         $this->_sDimageDirNoSsl = $oArticle->nossl_dimagedir;
00763         $this->_sDimageDirSsl   = $oArticle->ssl_dimagedir;
00764     }
00765 
00777     protected function _setFromOrderArticle( $oOrderArticle )
00778     {
00779         // overriding whole article
00780         $this->_oArticle = $oOrderArticle;
00781 
00782         // product ID
00783         $this->_sProductId = $oOrderArticle->getProductId();
00784 
00785         // products title
00786         $this->_sTitle = $oOrderArticle->oxarticles__oxtitle->value;
00787 
00788         // shop Ids
00789         $this->_sShopId       = $this->getConfig()->getShopId();
00790         $this->_sNativeShopId = $oOrderArticle->oxarticles__oxshopid->value;
00791     }
00792 
00800     protected function _setSelectList( $aSelList )
00801     {
00802         // checking for default select list
00803         $aSelectLists = $this->getArticle()->getSelectLists();
00804         if ( !$aSelList || is_array($aSelList) && count($aSelList) == 0 ) {
00805             if ( $iSelCnt = count( $aSelectLists ) ) {
00806                 $aSelList = array_fill( 0, $iSelCnt, '0' );
00807             }
00808         }
00809 
00810         $this->_aSelList = $aSelList;
00811 
00812         //
00813         if ( count( $this->_aSelList ) && is_array($this->_aSelList) ) {
00814             foreach ( $this->_aSelList as $conkey => $iSel ) {
00815                 $this->_aChosenSelectlist[$conkey] = new Oxstdclass();
00816                 $this->_aChosenSelectlist[$conkey]->name  = $aSelectLists[$conkey]['name'];
00817                 $this->_aChosenSelectlist[$conkey]->value = $aSelectLists[$conkey][$iSel]->name;
00818             }
00819         }
00820     }
00821 
00827     public function getPersParams()
00828     {
00829         return $this->_aPersistentParameters;
00830     }
00831 
00839     public function setPersParams( $aPersParam )
00840     {
00841         $this->_aPersistentParameters = $aPersParam;
00842     }
00843 
00851     public function setBundle( $blBundle )
00852     {
00853         $this->_blBundle = $blBundle;
00854     }
00855 
00863     public function setSkipDiscounts( $blSkip )
00864     {
00865         $this->_blSkipDiscounts = $blSkip;
00866     }
00867 
00873     public function getProductId()
00874     {
00875         return $this->_sProductId;
00876     }
00877 
00885     public function setWrapping( $sWrapId )
00886     {
00887         $this->_sWrappingId = $sWrapId;
00888     }
00889 
00895     public function getWrappingId()
00896     {
00897         return $this->_sWrappingId;
00898     }
00899 
00905     public function getWrapping()
00906     {
00907         $oWrap = null;
00908         if ( $sWrapId = $this->getWrappingId() ) {
00909             $oWrap = oxNew( 'oxwrapping' );
00910             $oWrap->load( $sWrapId );
00911         }
00912         return $oWrap;
00913     }
00914 
00920     public function getWishId()
00921     {
00922         return $this->_sWishId;
00923     }
00924 
00932     public function setWishId( $sWishId )
00933     {
00934         $this->_sWishId = $sWishId;
00935     }
00936 
00944     public function setWishArticleId( $sArticleId )
00945     {
00946         $this->_sWishArticleId = $sArticleId;
00947     }
00948 
00954     public function getWishArticleId()
00955     {
00956         return $this->_sWishArticleId;
00957     }
00958 
00964     public function getFUnitPrice()
00965     {
00966         return oxLang::getInstance()->formatCurrency( $this->getUnitPrice()->getBruttoPrice() );
00967     }
00968 
00974     public function getFTotalPrice()
00975     {
00976         return oxLang::getInstance()->formatCurrency( $this->getPrice()->getBruttoPrice() );
00977     }
00978 
00984     public function getVatPercent()
00985     {
00986         return oxLang::getInstance()->formatVat( $this->getPrice()->getVat() );
00987     }
00988 
00994     public function getVarSelect()
00995     {
00996         if ( $this->_sVarSelect === null || $this->getLanguageId() != oxLang::getInstance()->getBaseLanguage() ) {
00997             $oArticle = $this->getArticle( );
00998             $this->_sVarSelect = $oArticle->oxarticles__oxvarselect->value ? $oArticle->oxarticles__oxvarselect->value : '';
00999         }
01000         
01001         return $this->_sVarSelect;
01002     }
01003     
01009     public function getLanguageId()
01010     {
01011         return $this->_iLanguageId;
01012     }
01013     
01021     public function setLanguageId( $iLanguageId )
01022     {
01023         $this->_iLanguageId = $iLanguageId;
01024     }
01025 }