oxbasket.php

Go to the documentation of this file.
00001 <?php
00002 
00005 class oxBasket extends oxSuperCfg
00006 {
00012     protected $_aBasketContents = array();
00013 
00019     protected $_iProductsCnt = 0;
00020 
00026     protected $_dItemsCnt = 0.0;
00027 
00033     protected $_dWeight = 0.0;
00034 
00040     protected $_oPrice = null;
00041 
00047     protected $_oProductsPriceList = null;
00048 
00054     protected $_aDiscounts = array();
00055 
00061     protected $_aItemDiscounts = array();
00062 
00068     protected $_sOrderId = null;
00069 
00075     protected $_aVouchers = array();
00076 
00082     protected $_aCosts = array();
00083 
00089     protected $_oDiscountProductsPriceList = null;
00090 
00096     protected $_oNotDiscountedProductsPriceList = null;
00097 
00103     protected $_blUpdateNeeded = true;
00104 
00110     protected $_aBasketSummary = null;
00111 
00117     protected $_blBasketMerged = false;
00118 
00124     protected $_sPaymentId = null;
00125 
00131     protected $_sShippingSetId = null;
00132 
00138     protected $_oUser = null;
00139 
00145     protected $_oTotalDiscount = null;
00146 
00152     protected $_oVoucherDiscount = null;
00153 
00159     protected $_oCurrency = null;
00160 
00166     protected $_blSkipVouchersAvailabilityChecking = null;
00167 
00173     protected $_dDiscountedProductNettoPrice = null;
00174 
00180     protected $_aDiscountedVats = null;
00181 
00187     protected $_blSkipDiscounts = false;
00188 
00194     protected $_oDeliveryPrice = null;
00195 
00201      protected $_blCheckStock = true;
00202 
00208     protected $_blCalcDiscounts = true;
00209 
00215     protected $_sBasketCategoryId = null;
00216 
00222     protected $_blShowCatChangeWarning = false;
00223 
00229     protected $_sTsProductId = null;
00230 
00236     public function isEnabled()
00237     {
00238         return !oxUtils::getInstance()->isSearchEngine();
00239     }
00240 
00250     protected function _changeBasketItemKey($sOldKey, $sNewKey, $value = null)
00251     {
00252         reset($this->_aBasketContents);
00253         $iOldKeyPlace = 0;
00254         while (key($this->_aBasketContents) != $sOldKey && next($this->_aBasketContents)) {
00255             ++$iOldKeyPlace;
00256         }
00257         $aNewCopy = array_merge(
00258             array_slice($this->_aBasketContents, 0, $iOldKeyPlace, true),
00259             array($sNewKey => $value),
00260             array_slice($this->_aBasketContents, $iOldKeyPlace+1, count($this->_aBasketContents)-$iOldKeyPlace, true)
00261         );
00262         $this->_aBasketContents = $aNewCopy;
00263     }
00264 
00280     public function addToBasket( $sProductID, $dAmount, $aSel = null, $aPersParam = null, $blOverride = false, $blBundle = false, $sOldBasketItemId = null )
00281     {
00282         // enabled ?
00283         if ( !$this->isEnabled() )
00284             return null;
00285 
00286         // basket exclude
00287         if ( $this->getConfig()->getConfigParam( 'blBasketExcludeEnabled' ) ) {
00288             if ( !$this->canAddProductToBasket( $sProductID ) ) {
00289                 $this->setCatChangeWarningState( true );
00290                 return null;
00291             } else {
00292                 $this->setCatChangeWarningState( false );
00293             }
00294         }
00295 
00296         $sItemId = $this->getItemKey( $sProductID, $aSel, $aPersParam, $blBundle );
00297         if ( $sOldBasketItemId && ( strcmp( $sOldBasketItemId, $sItemId ) != 0 ) ) {
00298             if ( isset( $this->_aBasketContents[$sItemId] ) ) {
00299                 // we are merging, so params will just go to the new key
00300                 unset( $this->_aBasketContents[$sOldBasketItemId] );
00301                 // do not override stock
00302                 $blOverride = false;
00303             } else {
00304                 // value is null - means isset will fail and real values will be filled
00305                 $this->_changeBasketItemKey( $sOldBasketItemId, $sItemId );
00306             }
00307         }
00308 
00309         // after some checks item must be removed from basket
00310         $blRemoveItem = false;
00311 
00312         // initialting exception storage
00313         $oEx = null;
00314 
00315         if ( isset( $this->_aBasketContents[$sItemId] ) ) {
00316 
00317             //updating existing
00318             try {
00319                 // setting stock check status
00320                 $this->_aBasketContents[$sItemId]->setStockCheckStatus( $this->getStockCheckMode() );
00321                 //validate amount
00322                 //possibly throws exception
00323                 $this->_aBasketContents[$sItemId]->setAmount( $dAmount, $blOverride, $sItemId );
00324             } catch( oxOutOfStockException $oEx ) {
00325                 // rethrow later
00326             }
00327 
00328         } else {
00329             //inserting new
00330             $oBasketItem = oxNew( 'oxbasketitem' );
00331             try {
00332                 $oBasketItem->setStockCheckStatus( $this->getStockCheckMode() );
00333                 $oBasketItem->init( $sProductID, $dAmount, $aSel, $aPersParam, $blBundle );
00334             } catch( oxNoArticleException $oEx ) {
00335                 // in this case that the article does not exist remove the item from the basket by setting its amount to 0
00336                 //$oBasketItem->dAmount = 0;
00337                 $blRemoveItem = true;
00338 
00339             } catch( oxOutOfStockException $oEx ) {
00340                 // rethrow later
00341             } catch ( oxArticleInputException $oEx ) {
00342                 // rethrow later
00343                 $blRemoveItem = true;
00344             }
00345 
00346             $this->_aBasketContents[$sItemId] = $oBasketItem;
00347         }
00348 
00349         //in case amount is 0 removing item
00350         if ( $this->_aBasketContents[$sItemId]->getAmount() == 0 || $blRemoveItem ) {
00351             $this->removeItem( $sItemId );
00352         } elseif ( $blBundle ) {
00353             //marking bundles
00354             $this->_aBasketContents[$sItemId]->setBundle( true );
00355         }
00356 
00357         //calling update method
00358         $this->onUpdate();
00359 
00360         // updating basket history
00361         if ( !$blBundle ) {
00362             $this->_addItemToSavedBasket( $sProductID, $dAmount, $aSel, $blOverride );
00363         }
00364 
00365         if ( $oEx ) {
00366             throw $oEx;
00367         }
00368         return $this->_aBasketContents[$sItemId];
00369     }
00370 
00378     public function addOrderArticleToBasket( $oOrderArticle )
00379     {
00380         // adding only if amount > 0
00381         if ( $oOrderArticle->oxorderarticles__oxamount->value > 0 ) {
00382             $sItemId = $oOrderArticle->getId();
00383 
00384             //inserting new
00385             $this->_aBasketContents[$sItemId] = oxNew( 'oxbasketitem' );
00386             $this->_aBasketContents[$sItemId]->initFromOrderArticle( $oOrderArticle );
00387             $this->_aBasketContents[$sItemId]->setWrapping( $oOrderArticle->oxorderarticles__oxwrapid->value );
00388 
00389             //calling update method
00390             $this->onUpdate();
00391 
00392             return $this->_aBasketContents[$sItemId];
00393         }
00394     }
00395 
00403     public function setStockCheckMode( $blCheck )
00404     {
00405         $this->_blCheckStock = $blCheck;
00406     }
00407 
00413     public function getStockCheckMode()
00414     {
00415         return $this->_blCheckStock;
00416     }
00417 
00430     public function getItemKey( $sProductId, $aSel = null, $aPersParam = null, $blBundle = false, $sAdditionalParam = '' )
00431     {
00432         $aSel = ( $aSel != null) ? $aSel : array (0=>'0');
00433 
00434         $sItemKey = md5( $sProductId.'|'.serialize( $aSel ).'|'.serialize( $aPersParam ).'|'.( int ) $blBundle . '|' . serialize( $sAdditionalParam ) );
00435 
00436         return $sItemKey;
00437     }
00438 
00439 
00447     public function removeItem( $sItemKey )
00448     {
00449         if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
00450             if (isset($this->_aBasketContents[$sItemKey])) {
00451                 $sArticleId = $this->_aBasketContents[$sItemKey]->getProductId();
00452                 if ($sArticleId) {
00453                     $this->getSession()
00454                             ->getBasketReservations()
00455                             ->discardArticleReservation($sArticleId);
00456                 }
00457             }
00458         }
00459         unset( $this->_aBasketContents[$sItemKey] );
00460 
00461         // basket exclude
00462         if ( !count($this->_aBasketContents) && $this->getConfig()->getConfigParam( 'blBasketExcludeEnabled' ) ) {
00463             $this->setBasketRootCatId(null);
00464         }
00465     }
00466 
00472     protected function _clearBundles()
00473     {
00474         reset( $this->_aBasketContents );
00475         while ( list( $sItemKey, $oBasketItem ) = each( $this->_aBasketContents ) ) {
00476             if ( $oBasketItem->isBundle() ) {
00477                 $this->removeItem( $sItemKey );
00478             }
00479         }
00480     }
00481 
00489     protected function _getArticleBundles( $oBasketItem )
00490     {
00491         $aBundles = array();
00492 
00493         if ( $oBasketItem->isBundle() ) {
00494             return $aBundles;
00495         }
00496 
00497         $oArticle = $oBasketItem->getArticle();
00498         if ( $oArticle && $oArticle->oxarticles__oxbundleid->value ) {
00499             $aBundles[$oArticle->oxarticles__oxbundleid->value] = 1;
00500         }
00501 
00502         return $aBundles;
00503     }
00504 
00512     protected function _getItemBundles( $oBasketItem )
00513     {
00514         if ( $oBasketItem->isBundle() ) {
00515             return array();
00516         }
00517 
00518         $aBundles = array();
00519 
00520         // does this object still exists ?
00521         if ( $oArticle = $oBasketItem->getArticle() ) {
00522             $aDiscounts = oxDiscountList::getInstance()->getBasketItemBundleDiscounts( $oArticle, $this, $this->getBasketUser() );
00523 
00524             foreach ( $aDiscounts as $oDiscount ) {
00525 
00526                 //init array element
00527                 if ( !isset( $aBundles[$oDiscount->oxdiscount__oxitmartid->value] ) ) {
00528                     $aBundles[$oDiscount->oxdiscount__oxitmartid->value] = 0;
00529                 }
00530 
00531                 $aBundles[$oDiscount->oxdiscount__oxitmartid->value] += $oDiscount->getBundleAmount( $oBasketItem->getAmount() );
00532 
00533             }
00534         }
00535 
00536         return $aBundles;
00537     }
00538 
00544     protected function _getBasketBundles()
00545     {
00546         $aBundles = array();
00547         $aDiscounts = oxDiscountList::getInstance()->getBasketBundleDiscounts( $this, $this->getBasketUser() );
00548 
00549         // calculating amount of non bundled/discount items
00550         $dAmount = 0;
00551         foreach ( $this->_aBasketContents as $oBasketItem ) {
00552             if ( !( $oBasketItem->isBundle() || $oBasketItem->isDiscountArticle() ) ) {
00553                 $dAmount += $oBasketItem->getAmount();
00554             }
00555         }
00556 
00557         foreach ( $aDiscounts as $oDiscount ) {
00558             if ($oDiscount->oxdiscount__oxitmartid->value) {
00559                 if ( !isset( $aBundles[$oDiscount->oxdiscount__oxitmartid->value] ) ) {
00560                     $aBundles[$oDiscount->oxdiscount__oxitmartid->value] = 0;
00561                 }
00562 
00563                 $aBundles[$oDiscount->oxdiscount__oxitmartid->value] += $oDiscount->getBundleAmount( $dAmount );
00564             }
00565         }
00566 
00567         return $aBundles;
00568     }
00569 
00576     protected function _addBundles()
00577     {
00578         // iterating through articles and binding bundles
00579         foreach ( $this->_aBasketContents as $key => $oBasketItem ) {
00580             try {
00581                 // adding discount type bundles
00582                 if ( !$oBasketItem->isDiscountArticle() && !$oBasketItem->isBundle() ) {
00583                     $aBundles = $this->_getItemBundles( $oBasketItem );
00584                 } else {
00585                     continue;
00586                 }
00587 
00588                 $this->_addBundlesToBasket( $aBundles );
00589 
00590                     // adding item type bundles
00591                     $aBundles = $this->_getArticleBundles( $oBasketItem );
00592 
00593                     // adding bundles to basket
00594                     $this->_addBundlesToBasket( $aBundles );
00595             } catch ( oxNoArticleException $oEx ) {
00596                 $this->removeItem( $key );
00597                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00598             } catch( oxArticleInputException $oEx ) {
00599                 $this->removeItem( $key );
00600                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00601             }
00602         }
00603 
00604         // adding global basket bundles
00605         if ( $aBundles = $this->_getBasketBundles() ) {
00606             $this->_addBundlesToBasket( $aBundles );
00607         }
00608 
00609     }
00610 
00618     protected function _addBundlesToBasket( $aBundles )
00619     {
00620         foreach ( $aBundles as $sBundleId => $dAmount ) {
00621             try {
00622                 if ( $oBundleItem = $this->addToBasket( $sBundleId, $dAmount, null, null, true, true ) ) {
00623                     $oBundleItem->setAsDiscountArticle( true );
00624                 }
00625             } catch(oxArticleException $oEx) {
00626                 // caught and ignored
00627             }
00628         }
00629 
00630     }
00631 
00637     protected function _calcItemsPrice()
00638     {
00639         // resetting
00640         $this->setSkipDiscounts( false );
00641         $this->_iProductsCnt = 0; // count different types
00642         $this->_dItemsCnt    = 0; // count of item units
00643         $this->_dWeight      = 0; // basket weight
00644 
00645         // resetting
00646         $this->_aItemDiscounts = array();
00647 
00648         $this->_oProductsPriceList = oxNew( 'oxpricelist' );
00649         $this->_oDiscountProductsPriceList = oxNew( 'oxpricelist' );
00650         $this->_oNotDiscountedProductsPriceList = oxNew( 'oxpricelist' );
00651 
00652         $oDiscountList = oxDiscountList::getInstance();
00653 
00654         foreach ( $this->_aBasketContents as $oBasketItem ) {
00655             $this->_iProductsCnt++;
00656             $this->_dItemsCnt += $oBasketItem->getAmount();
00657             $this->_dWeight   += $oBasketItem->getWeight();
00658 
00659             if ( !$oBasketItem->isDiscountArticle() && ( $oArticle = $oBasketItem->getArticle() ) ) {
00660                 $oBasketPrice = $oArticle->getBasketPrice( $oBasketItem->getAmount(), $oBasketItem->getSelList(), $this );
00661                 $oBasketItem->setPrice( $oBasketPrice );
00662                 //P adding product price
00663                 $this->_oProductsPriceList->addToPriceList( $oBasketItem->getPrice() );
00664 
00665                 $oBasketPrice->setBruttoPriceMode();
00666                 if ( !$oArticle->skipDiscounts() && $this->canCalcDiscounts() ) {
00667                     // apply basket type discounts
00668                     $aItemDiscounts = $oDiscountList->applyBasketDiscounts( $oBasketPrice, $oDiscountList->getBasketItemDiscounts( $oArticle, $this, $this->getBasketUser() ), $oBasketItem->getAmount() );
00669                     if ( is_array($this->_aItemDiscounts) && is_array($aItemDiscounts) ) {
00670                         $this->_aItemDiscounts = $this->_mergeDiscounts( $this->_aItemDiscounts, $aItemDiscounts);
00671                     }
00672                 } else {
00673                     $oBasketItem->setSkipDiscounts( true );
00674                     $this->setSkipDiscounts( true );
00675                 }
00676                 $oBasketPrice->multiply( $oBasketItem->getAmount() );
00677 
00678                 //P collect discount values for basket items which are discountable
00679                 if ( !$oArticle->skipDiscounts() ) {
00680                     $this->_oDiscountProductsPriceList->addToPriceList( $oBasketPrice );
00681                 } else {
00682                     $this->_oNotDiscountedProductsPriceList->addToPriceList( $oBasketPrice );
00683                     $oBasketItem->setSkipDiscounts( true );
00684                     $this->setSkipDiscounts( true );
00685                 }
00686             } elseif ( $oBasketItem->isBundle() ) {
00687                 // if bundles price is set to zero
00688                 $oPrice = oxNew( "oxprice");
00689                 $oBasketItem->setPrice( $oPrice );
00690             }
00691         }
00692     }
00693 
00701     public function setDiscountCalcMode( $blCalcDiscounts )
00702     {
00703         $this->_blCalcDiscounts = $blCalcDiscounts;
00704     }
00705 
00711     public function canCalcDiscounts()
00712     {
00713         return $this->_blCalcDiscounts;
00714     }
00715 
00725     protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
00726     {
00727         foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
00728             // add prices of the same discounts
00729             if ( array_key_exists ($sKey, $aDiscounts) ) {
00730                 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
00731             } else {
00732                 $aDiscounts[$sKey] = $oDiscount;
00733             }
00734         }
00735         return $aDiscounts;
00736     }
00737 
00743     protected function _calcDeliveryCost()
00744     {
00745         if ( $this->_oDeliveryPrice !== null ) {
00746             return $this->_oDeliveryPrice;
00747         }
00748         $myConfig  = $this->getConfig();
00749         $oDeliveryPrice = oxNew( 'oxprice' );
00750         $oDeliveryPrice->setBruttoPriceMode();
00751 
00752         // don't calculate if not logged in
00753         $oUser = $this->getBasketUser();
00754 
00755         if ( !$oUser && !$myConfig->getConfigParam( 'blCalculateDelCostIfNotLoggedIn' ) ) {
00756             return $oDeliveryPrice;
00757         }
00758 
00759         // VAT for delivery ?
00760         $fDelVATPercent = 0;
00761         if ( $myConfig->getConfigParam( 'blCalcVATForDelivery' ) ) {
00762             $fDelVATPercent = $this->getMostUsedVatPercent();
00763             $oDeliveryPrice->setVat( $fDelVATPercent );
00764         }
00765 
00766         // list of active delivery costs
00767         if ( $myConfig->getConfigParam('bl_perfLoadDelivery') ) {
00768             $aDeliveryList = oxDeliveryList::getInstance()->getDeliveryList( $this,
00769                                         $oUser,
00770                                         $this->_findDelivCountry(),
00771                                         $this->getShippingId()
00772                                     );
00773 
00774             if ( count( $aDeliveryList ) > 0 ) {
00775                 foreach ( $aDeliveryList as $oDelivery ) {
00776                     //debug trace
00777                     if ( $myConfig->getConfigParam( 'iDebug' ) == 5 ) {
00778                         echo( "DelCost : ".$oDelivery->oxdelivery__oxtitle->value."<br>" );
00779                     }
00780                     $oDeliveryPrice->addPrice( $oDelivery->getDeliveryPrice( $fDelVATPercent ) );
00781                 }
00782             }
00783         }
00784 
00785         return $oDeliveryPrice;
00786     }
00787 
00793     public function getBasketUser()
00794     {
00795         if ( $this->_oUser == null ) {
00796             return $this->getUser();
00797         }
00798 
00799         return $this->_oUser;
00800     }
00801 
00809     public function setBasketUser( $oUser )
00810     {
00811         $this->_oUser = $oUser;
00812     }
00813 
00814     //P
00820     public function getMostUsedVatPercent()
00821     {
00822         return $this->_oProductsPriceList->getMostUsedVatPercent();
00823     }
00824 
00825     //P
00832     protected function _calcTotalPrice()
00833     {
00834         // 1. add products price
00835         $dprice = $this->_oProductsPriceList->getBruttoSum();
00836         $this->_oPrice->setPrice( $dprice );
00837 
00838         // 2. substract discounts
00839         if ( $dprice ) {
00840 
00841             // 2.1 applying basket item discounts
00842             foreach ( $this->_aItemDiscounts as $oDiscount ) {
00843 
00844                 // skipping bundle discounts
00845                 if ( $oDiscount->sType == 'itm' ) {
00846                     continue;
00847                 }
00848                 $this->_oPrice->subtract( $oDiscount->dDiscount );
00849             }
00850 
00851             // 2.2 applying basket discounts
00852             $this->_oPrice->subtract( $this->_oTotalDiscount->getBruttoPrice() );
00853 
00854             // 2.3 applying voucher discounts
00855             if ($oVoucherDisc = $this->getVoucherDiscount()) {
00856                 $this->_oPrice->subtract( $oVoucherDisc->getBruttoPrice() );
00857             }
00858         }
00859 
00860         // 2.3 add delivery cost
00861         if ( isset( $this->_aCosts['oxdelivery'] ) ) {
00862             $this->_oPrice->add( $this->_aCosts['oxdelivery']->getBruttoPrice() );
00863         }
00864 
00865         // 2.4 add wrapping price
00866         if ( isset( $this->_aCosts['oxwrapping'] ) ) {
00867             $this->_oPrice->add( $this->_aCosts['oxwrapping']->getBruttoPrice() );
00868         }
00869 
00870         // 2.5 add payment price
00871         if ( isset( $this->_aCosts['oxpayment'] ) ) {
00872             $this->_oPrice->add( $this->_aCosts['oxpayment']->getBruttoPrice() );
00873         }
00874 
00875         // 2.6 add TS protection price
00876         if ( isset( $this->_aCosts['oxtsprotection'] ) ) {
00877             $this->_oPrice->add( $this->_aCosts['oxtsprotection']->getBruttoPrice() );
00878         }
00879 
00880     }
00881 
00889     public function setVoucherDiscount( $dDiscount )
00890     {
00891         $this->_oVoucherDiscount = oxNew( 'oxPrice' );
00892         $this->_oVoucherDiscount->setBruttoPriceMode();
00893         $this->_oVoucherDiscount->add( $dDiscount );
00894     }
00895 
00901     protected function _calcVoucherDiscount()
00902     {
00903         if ( $this->getConfig()->getConfigParam( 'bl_showVouchers' ) && ($this->_oVoucherDiscount === null || ( $this->_blUpdateNeeded && !$this->isAdmin() ) ) ) {
00904 
00905             $this->_oVoucherDiscount = oxNew( 'oxPrice' );
00906             $this->_oVoucherDiscount->setBruttoPriceMode();
00907 
00908 
00909             // calculating price to apply discount
00910             $dPrice = $this->_oDiscountProductsPriceList->getBruttoSum() - $this->_oTotalDiscount->getBruttoPrice();
00911 
00912             // recalculating
00913             if ( count( $this->_aVouchers ) ) {
00914                 $oLang = oxLang::getInstance();
00915                 foreach ( $this->_aVouchers as $sVoucherId => $oStdVoucher ) {
00916                     $oVoucher = oxNew( 'oxvoucher' );
00917                     try { // checking
00918                         $oVoucher->load( $oStdVoucher->sVoucherId );
00919 
00920                         if ( !$this->_blSkipVouchersAvailabilityChecking ) {
00921                             $oVoucher->checkBasketVoucherAvailability( $this->_aVouchers, $dPrice );
00922                             $oVoucher->checkUserAvailability( $this->getBasketUser() );
00923                         }
00924 
00925                         // assigning real voucher discount value as this is the only place where real value is calculated
00926                         $dVoucherdiscount = $oVoucher->getDiscountValue( $dPrice );
00927 
00928                         // acumulating discount value
00929                         $this->_oVoucherDiscount->add( $dVoucherdiscount );
00930 
00931                         // collecting formatted for preview
00932                         $oStdVoucher->fVoucherdiscount = $oLang->formatCurrency( $dVoucherdiscount, $this->getBasketCurrency() );
00933 
00934                         // substracting voucher discount
00935                         $dPrice -= $dVoucherdiscount;
00936                     } catch ( oxVoucherException $oEx ) {
00937 
00938                         // removing voucher on error
00939                         $oVoucher->unMarkAsReserved();
00940                         unset( $this->_aVouchers[$sVoucherId] );
00941 
00942                         // storing voucher error info
00943                         oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00944                     }
00945                 }
00946             }
00947         }
00948     }
00949 
00950     //V
00957     protected function _applyDiscounts()
00958     {
00959         $dBruttoPrice = 0;
00960         $this->_aDiscountedVats = array();
00961         if ( $oPriceList = $this->getDiscountProductsPrice() ) {
00962             $dBruttoPrice = $oPriceList->getBruttoSum();
00963             $this->_aDiscountedVats = $oPriceList->getVatInfo();
00964         }
00965 
00966         //apply discounts for brutto price
00967         $dDiscountedBruttoPrice = $this->getDiscountedProductsBruttoPrice();
00968         $oTotalDiscount   = $this->getTotalDiscount();
00969         $oVoucherDiscount = $this->getVoucherDiscount();
00970 
00971         //apply discount for VATs
00972         if ( $dBruttoPrice &&
00973              ( ( $oTotalDiscount && $oTotalDiscount->getBruttoPrice() ) ||
00974                ( $oVoucherDiscount && $oVoucherDiscount->getBruttoPrice() )
00975              )
00976            ) {
00977             $dPercent = ( $dDiscountedBruttoPrice / $dBruttoPrice) * 100;
00978             foreach ( $this->_aDiscountedVats as $sKey => $dVat ) {
00979                 $this->_aDiscountedVats[$sKey] = oxPrice::percent( $dVat, $dPercent);
00980             }
00981         }
00982 
00983         $oUtils = oxUtils::getInstance();
00984         $dDiscVatSum = 0;
00985         foreach ( $this->_aDiscountedVats as $dVat ) {
00986             $dDiscVatSum += $oUtils->fRound( -$dVat, $this->_oCurrency);
00987         }
00988         //calculate netto price with discounts
00989         $this->_dDiscountedProductNettoPrice = $dDiscountedBruttoPrice + $dDiscVatSum;
00990     }
00991 
00997     protected function _calcBasketDiscount()
00998     {
00999         // resetting
01000         $this->_aDiscounts = array();
01001 
01002         // P using prices sum which has discount, not sum of skipped discounts
01003         $dOldprice = $this->_oDiscountProductsPriceList->getBruttoSum();
01004 
01005         // add basket discounts
01006         $aDiscounts = oxDiscountList::getInstance()->getBasketDiscounts( $this, $this->getBasketUser() );
01007 
01008         foreach ( $aDiscounts as $oDiscount ) {
01009 
01010             // storing applied discounts
01011             $oStdDiscount = $oDiscount->getSimpleDiscount();
01012 
01013             // skipping bundle discounts
01014             if ( $oDiscount->oxdiscount__oxaddsumtype->value == 'itm' ) {
01015                 continue;
01016             }
01017 
01018             // saving discount info
01019             $oStdDiscount->dDiscount = $oDiscount->getAbsValue( $dOldprice );
01020             if ($dOldprice < $oStdDiscount->dDiscount) {
01021                 $oStdDiscount->dDiscount = $dOldprice;
01022             }
01023 
01024             if ($oStdDiscount->dDiscount > 0) {
01025                 $this->_aDiscounts[$oDiscount->getId()] = $oStdDiscount;
01026                 // substracting product price after discount
01027                 $dOldprice = $dOldprice - $oStdDiscount->dDiscount;
01028             }
01029         }
01030     }
01031 
01037     protected function _calcBasketTotalDiscount()
01038     {
01039         if ( $this->_oTotalDiscount === null || ( $this->_blUpdateNeeded && !$this->isAdmin() ) ) {
01040             $this->_oTotalDiscount = oxNew( 'oxPrice' );
01041             $this->_oTotalDiscount->setBruttoPriceMode();
01042 
01043             if ( is_array($this->_aDiscounts) ) {
01044                 foreach ( $this->_aDiscounts as $oDiscount ) {
01045 
01046                     // skipping bundle discounts
01047                     if ( $oDiscount->sType == 'itm' ) {
01048                         continue;
01049                     }
01050 
01051                     // add discount value to total basket discount
01052                     $this->_oTotalDiscount->add( $oDiscount->dDiscount );
01053                 }
01054             }
01055         }
01056     }
01057 
01067     protected function _calcBasketWrapping()
01068     {
01069         $myConfig = $this->getConfig();
01070         $oWrappingPrice = oxNew( 'oxPrice' );
01071         $oWrappingPrice->setBruttoPriceMode();
01072 
01073         // wrapping VAT
01074         if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
01075             $oWrappingPrice->setVat( $this->getMostUsedVatPercent() );
01076         }
01077 
01078         // calculating basket items wrapping
01079         foreach ( $this->_aBasketContents as $oBasketItem ) {
01080 
01081             if ( ( $oWrapping = $oBasketItem->getWrapping() ) ) {
01082                 $oWrapPrice = $oWrapping->getWrappingPrice( $oBasketItem->getAmount() );
01083                 $oWrappingPrice->add( $oWrapPrice->getBruttoPrice() );
01084             }
01085         }
01086 
01087         // gift card price calculation
01088         if ( ( $oCard = $this->getCard() ) ) {
01089             $oCardPrice = $oCard->getWrappingPrice();
01090             $oWrappingPrice->add( $oCardPrice->getBruttoPrice() );
01091         }
01092 
01093         return $oWrappingPrice;
01094     }
01095 
01102     protected function _calcPaymentCost()
01103     {
01104         // resetting values
01105         $oPaymentPrice = oxNew( 'oxPrice' );
01106         $oPaymentPrice->setBruttoPriceMode();
01107 
01108         // payment
01109         if ( ( $this->_sPaymentId = $this->getPaymentId() ) ) {
01110 
01111             $oPayment = oxNew( 'oxpayment' );
01112             $oPayment->load( $this->_sPaymentId );
01113 
01114             $oPaymentPrice = $oPayment->getPaymentPrice( $this );
01115         }
01116 
01117         return $oPaymentPrice;
01118     }
01119 
01126     protected function _calcTsProtectionCost()
01127     {
01128         // resetting values
01129         $oProtectionPrice = oxNew( 'oxPrice' );
01130         $oProtectionPrice->setBruttoPriceMode();
01131 
01132         // payment
01133         if ( ( $this->_sTsProductId = $this->getTsProductId() ) ) {
01134 
01135             $oTsProtection = oxNew('oxtsprotection');
01136             $oTsProduct = $oTsProtection->getTsProduct( $this->_sTsProductId );
01137 
01138             $oProtectionPrice = $oTsProduct->oPrice;
01139         }
01140         return $oProtectionPrice;
01141     }
01142 
01151     public function setCost( $sCostName, $oPrice = null )
01152     {
01153         $this->_aCosts[$sCostName] = $oPrice;
01154     }
01155 
01164     public function calculateBasket( $blForceUpdate = false )
01165     {
01166         if ( !$this->isEnabled() ) {
01167             return;
01168         }
01169 
01170         if ( !$this->_blUpdateNeeded && !$blForceUpdate ) {
01171             return;
01172         }
01173 
01174         $this->_aCosts = array();
01175 
01176         $this->_oPrice = oxNew( 'oxprice' );
01177         $this->_oPrice->setBruttoPriceMode();
01178 
01179         //  1. merging basket history
01180         $this->_mergeSavedBasket();
01181 
01182         //  2. remove all bundles
01183         $this->_clearBundles();
01184 
01185         //  3. generate bundle items
01186         $this->_addBundles();
01187 
01188         // reserve active basket
01189         if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
01190             $this->getSession()->getBasketReservations()->reserveBasket($this);
01191         }
01192 
01193         //  4. calculating item prices
01194         $this->_calcItemsPrice();
01195 
01196         //  5. calculating/applying discounts
01197         $this->_calcBasketDiscount();
01198 
01199         //  6. calculating basket total discount
01200         $this->_calcBasketTotalDiscount();
01201 
01202         //  7. check for vouchers
01203         $this->_calcVoucherDiscount();
01204 
01205         //  8. applies all discounts to pricelist
01206         $this->_applyDiscounts();
01207 
01208         //  9. calculating additional costs:
01209         //  9.1: delivery
01210         $this->setCost( 'oxdelivery', $this->_calcDeliveryCost() );
01211 
01212         //  9.2: adding wrapping costs
01213         $this->setCost( 'oxwrapping', $this->_calcBasketWrapping() );
01214 
01215         //  9.3: adding payment cost
01216         $this->setCost( 'oxpayment', $this->_calcPaymentCost() );
01217 
01218         //  9.4: adding TS protection cost
01219         $this->setCost( 'oxtsprotection', $this->_calcTsProtectionCost() );
01220 
01221         //  10. calculate total price
01222         $this->_calcTotalPrice();
01223 
01224         //  11. setting deprecated values
01225         $this->_setDeprecatedValues();
01226 
01227         //  12.setting to up-to-date status
01228         $this->afterUpdate();
01229     }
01230 
01236     public function onUpdate()
01237     {
01238         $this->_blUpdateNeeded = true;
01239     }
01240 
01246     public function afterUpdate()
01247     {
01248         $this->_blUpdateNeeded = false;
01249     }
01250 
01258     public function getBasketSummary()
01259     {
01260         if ( $this->_blUpdateNeeded || $this->_aBasketSummary === null ) {
01261             $this->_aBasketSummary = new Oxstdclass();
01262             $this->_aBasketSummary->aArticles = array();
01263             $this->_aBasketSummary->aCategories = array();
01264             $this->_aBasketSummary->iArticleCount = 0;
01265             $this->_aBasketSummary->dArticlePrice = 0;
01266             $this->_aBasketSummary->dArticleDiscountablePrice = 0;
01267         }
01268 
01269         if ( !$this->isEnabled() ) {
01270             return $this->_aBasketSummary;
01271         }
01272 
01273         $myConfig = $this->getConfig();
01274         foreach ( $this->_aBasketContents as $oBasketItem ) {
01275             if ( !$oBasketItem->isBundle() && $oArticle = $oBasketItem->getArticle(false) ) {
01276                 $aCatIds = $oArticle->getCategoryIds();
01277                 //#M530 if price is not loaded for articles
01278                 $dPrice = 0;
01279                 $dDiscountablePrice = 0;
01280                 if ( ( $oPrice = $oArticle->getPrice( $oBasketItem->getAmount() ) ) ) {
01281                     $dPrice = $oPrice->getBruttoPrice();
01282                     if ( !$oArticle->skipDiscounts() ) {
01283                         $dDiscountablePrice = $dPrice;
01284                     }
01285                 }
01286 
01287                 foreach ( $aCatIds as $sCatId ) {
01288                     if ( !isset( $this->_aBasketSummary->aCategories[$sCatId] ) ) {
01289                         $this->_aBasketSummary->aCategories[$sCatId] = new Oxstdclass();
01290                     }
01291 
01292                     $this->_aBasketSummary->aCategories[$sCatId]->dPrice  += $dPrice * $oBasketItem->getAmount();
01293                     $this->_aBasketSummary->aCategories[$sCatId]->dDiscountablePrice += $dDiscountablePrice * $oBasketItem->getAmount();
01294                     $this->_aBasketSummary->aCategories[$sCatId]->dAmount += $oBasketItem->getAmount();
01295                     $this->_aBasketSummary->aCategories[$sCatId]->iCount++;
01296                 }
01297 
01298                 // variant handling
01299                 if ( ($sParentId = $oArticle->getProductParentId()) && $myConfig->getConfigParam( 'blVariantParentBuyable' ) ) {
01300                     if ( !isset( $this->_aBasketSummary->aArticles[$sParentId] ) ) {
01301                         $this->_aBasketSummary->aArticles[$sParentId] = 0;
01302                     }
01303                     $this->_aBasketSummary->aArticles[$sParentId] += $oBasketItem->getAmount();
01304                 }
01305 
01306                 if ( !isset( $this->_aBasketSummary->aArticles[$oBasketItem->getProductId()] ) ) {
01307                     $this->_aBasketSummary->aArticles[$oBasketItem->getProductId()] = 0;
01308                 }
01309 
01310                 $this->_aBasketSummary->aArticles[$oBasketItem->getProductId()] += $oBasketItem->getAmount();
01311                 $this->_aBasketSummary->iArticleCount += $oBasketItem->getAmount();
01312                 $this->_aBasketSummary->dArticlePrice += $dPrice * $oBasketItem->getAmount();
01313                 $this->_aBasketSummary->dArticleDiscountablePrice += $dDiscountablePrice * $oBasketItem->getAmount();
01314             }
01315         }
01316         return $this->_aBasketSummary;
01317     }
01318 
01330     public function addVoucher( $sVoucherId )
01331     {
01332         // calculating price to check
01333         // P using prices sum which has discount, not sum of skipped discounts
01334         $dPrice = 0;
01335         if ( $this->_oDiscountProductsPriceList ) {
01336             $dPrice = $this->_oDiscountProductsPriceList->getBruttoSum();
01337         }
01338 
01339         try { // trying to load voucher and apply it
01340 
01341             $oVoucher = oxNew( 'oxvoucher' );
01342 
01343             if ( !$this->_blSkipVouchersAvailabilityChecking ) {
01344                 $oVoucher->getVoucherByNr( $sVoucherId, $this->_aVouchers, true );
01345                 $oVoucher->checkVoucherAvailability( $this->_aVouchers, $dPrice );
01346                 $oVoucher->checkUserAvailability( $this->getBasketUser() );
01347                 $oVoucher->markAsReserved();
01348             } else {
01349                 $oVoucher->load( $sVoucherId );
01350             }
01351 
01352             // saving voucher info
01353             $this->_aVouchers[$oVoucher->oxvouchers__oxid->value] = $oVoucher->getSimpleVoucher();
01354         } catch ( oxVoucherException $oEx ) {
01355 
01356             // problems adding voucher
01357             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
01358         }
01359 
01360         $this->onUpdate();
01361     }
01362 
01370     public function removeVoucher( $sVoucherId )
01371     {
01372         // removing if it exists
01373         if ( isset( $this->_aVouchers[$sVoucherId] ) ) {
01374 
01375             $oVoucher = oxNew( 'oxvoucher' );
01376             $oVoucher->load( $sVoucherId );
01377 
01378             $oVoucher->unMarkAsReserved();
01379 
01380             // unsetting it if exists this voucher in DB or not
01381             unset( $this->_aVouchers[$sVoucherId] );
01382             $this->onUpdate();
01383         }
01384 
01385     }
01386 
01392     public function resetUserInfo()
01393     {
01394         $this->setPayment( null );
01395         $this->setShipping( null );
01396     }
01397 
01405     protected function _setDeprecatedValues()
01406     {
01407         $this->dproductsprice    = $this->_oProductsPriceList->getBruttoSum(); // products brutto price
01408         $this->dproductsnetprice = $this->getDiscountedNettoPrice();  // products netto price();
01409 
01410         //P sum vat values
01411         $this->dVAT = array_sum( $this->_oProductsPriceList->getVatInfo() );
01412         $oLang = oxLang::getInstance();
01413 
01414         // formatting final values
01415         $this->fproductsprice    = $this->getFProductsPrice();
01416         $this->fproductsnetprice = $this->getProductsNetPrice();
01417         $this->fVAT = $oLang->formatCurrency( $this->dVAT, $this->getBasketCurrency());
01418 
01419         // delivery costs
01420         if ( $oDeliveryCost = $this->getCosts( 'oxdelivery' ) ) {
01421 
01422             $this->ddeliverycost    = $oDeliveryCost->getBruttoPrice();
01423             $this->ddeliverynetcost = $oDeliveryCost->getNettoPrice();
01424             $this->dDelVAT          = $oDeliveryCost->getVatValue();
01425             $this->fDelVATPercent   = $oDeliveryCost->getVat() / 100; // needed to divide, because in template value is multyplied by 100
01426 
01427             // formating values
01428             $this->fdeliverycost    = $oLang->formatCurrency( $this->ddeliverycost, $this->getBasketCurrency() );
01429             $this->fdeliverynetcost = $oLang->formatCurrency( $this->ddeliverynetcost, $this->getBasketCurrency() );
01430             $this->fDelVAT          = $this->getDelCostVat();
01431         }
01432 
01433         //P
01434         // wrapping costs
01435         if ( $oWrappingCost = $this->getCosts( 'oxwrapping' ) ) {
01436 
01437             $this->dWrappingPrice = $oWrappingCost->getBruttoPrice();
01438             $this->dWrappingNetto = $oWrappingCost->getNettoPrice();
01439             $this->dWrappingVAT   = $oWrappingCost->getVatValue();
01440 
01441             //formating values
01442             $this->fWrappingPrice      = $oLang->formatCurrency( $this->dWrappingPrice, $this->getBasketCurrency() );
01443             $this->fWrappingNetto      = $this->getWrappCostNet();
01444             $this->fWrappingVAT        = $this->getWrappCostVat();
01445             $this->fWrappingVATPercent = $this->getWrappCostVatPercent();
01446         }
01447 
01448         //P
01449         // payment costs
01450         if ( $oPaymentCost = $this->getCosts( 'oxpayment' ) ) {
01451 
01452             $this->dAddPaymentSum    = $this->getPaymentCosts();
01453             $this->dAddPaymentSumVAT = $oPaymentCost->getVatValue();
01454 
01455             //formating values
01456             $this->fAddPaymentSum    = $oLang->formatCurrency( $this->dAddPaymentSum, $this->getBasketCurrency() );
01457             $this->fAddPaymentSumVAT = $this->getPayCostVat();
01458             $this->fAddPaymentSumVATPercent = $this->getPayCostVatPercent();
01459             $this->fAddPaymentNetSum = $this->getPayCostNet();
01460         }
01461 
01462         //P
01463         // basket total prices
01464         $this->dprice = $this->_oPrice->getBruttoPrice();
01465         $this->fprice = $oLang->formatCurrency( $this->dprice, $this->getBasketCurrency() );
01466 
01467         // product info
01468         $this->iCntProducts = $this->getProductsCount();
01469         $this->dCntItems    = $this->getItemsCount();
01470         $this->aVATs        = $this->getProductVats();
01471         $this->aBasketContents = $this->getContents();
01472 
01473         // setting gift card information
01474         $this->giftmessage = $this->getCardMessage();
01475         $this->chosencard  = $this->getCardId();
01476 
01477         $this->oCard = $this->getCard();
01478 
01479         // discount information
01480         // formating discount value
01481         $this->aDiscounts = $this->getDiscounts();
01482         if ( count($this->aDiscounts) > 0 ) {
01483             foreach ($this->aDiscounts as $oDiscount) {
01484                 $oDiscount->fDiscount = $oLang->formatCurrency( $oDiscount->dDiscount, $this->getBasketCurrency() );
01485             }
01486         }
01487         $this->dDiscount  = $this->getTotalDiscount()->getBruttoPrice();
01488 
01489         // voucher info
01490         $this->aVouchers = $this->getVouchers();
01491         $this->dVoucherDiscount = $this->getVoucherDiscValue();
01492         $this->fVoucherDiscount = $oLang->formatCurrency( $this->dVoucherDiscount, $this->getBasketCurrency() );
01493         $this->dSkippedDiscount = $this->hasSkipedDiscount();
01494 
01495     }
01496 
01497 
01503     protected function _canMergeBasket()
01504     {
01505         $blCan = true;
01506         if ( $this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' ) ||
01507              $this->_blBasketMerged || $this->isAdmin() ) {
01508             $blCan = false;
01509         }
01510         return $blCan;
01511     }
01512 
01519     protected function _mergeSavedBasket()
01520     {
01521         if ( $this->_canMergeBasket() ) {
01522 
01523             $oUser = $this->getBasketUser();
01524             if ( !$oUser ) {
01525                 $this->_blBasketMerged = false;
01526                 return;
01527             }
01528 
01529             $oBasket = $oUser->getBasket( 'savedbasket' );
01530 
01531             // restoring from saved history
01532             $aSavedItems = $oBasket->getItems();
01533             foreach ( $aSavedItems as $oItem ) {
01534                 try {
01535                     $this->addToBasket( $oItem->oxuserbasketitems__oxartid->value, $oItem->oxuserbasketitems__oxamount->value, $oItem->getSelList(), null, true );
01536                 } catch( oxArticleException $oEx ) {
01537                     // caught and ignored
01538                 }
01539             }
01540 
01541             // refreshing history
01542             foreach ( $this->_aBasketContents as $oBasketItem ) {
01543                 $oBasket->addItemToBasket( $oBasketItem->getProductId(), $oBasketItem->getAmount(), $oBasketItem->getSelList(), true );
01544             }
01545 
01546             // marking basked as saved
01547             $this->_blBasketMerged = true;
01548         }
01549     }
01550 
01561     protected function _addItemToSavedBasket( $sProductId , $dAmount, $aSel, $blOverride = false )
01562     {
01563         // updating basket history
01564         if ( $oUser = $this->getBasketUser() ) {
01565             $oUser->getBasket( 'savedbasket' )->addItemToBasket( $sProductId, $dAmount, $aSel, $blOverride );
01566         }
01567     }
01568 
01576     protected function _deleteSavedBasket()
01577     {
01578         // deleting basket if session user available
01579         if ( $oUser = $this->getBasketUser() ) {
01580             $oUser->getBasket( 'savedbasket' )->delete();
01581         }
01582 
01583         // basket exclude
01584         if ( $this->getConfig()->getConfigParam( 'blBasketExcludeEnabled' )) {
01585             $this->setBasketRootCatId(null);
01586         }
01587     }
01588 
01594     protected function _findDelivCountry()
01595     {
01596         $myConfig = $this->getConfig();
01597         $oUser    = $this->getBasketUser();
01598 
01599         $sDelivCountry = null;
01600 
01601         if ( !$oUser ) {
01602             // don't calculate if not logged in unless specified otherwise
01603             $aHomeCountry = $myConfig->getConfigParam( 'aHomeCountry' );
01604             if ( $myConfig->getConfigParam( 'blCalculateDelCostIfNotLoggedIn' ) && is_array( $aHomeCountry ) ) {
01605                 $sDelivCountry = current( $aHomeCountry );
01606             }
01607         } else {
01608 
01609             // ok, logged in
01610             if ( $sCountryId = $myConfig->getGlobalParameter( 'delcountryid' ) ) {
01611                 $sDelivCountry = $sCountryId;
01612             } elseif ( $sAddressId = oxConfig::getParameter( 'deladrid' ) ) {
01613 
01614                 $oDelAdress = oxNew( 'oxaddress' );
01615                 if ( $oDelAdress->load( $sAddressId ) ) {
01616                     $sDelivCountry = $oDelAdress->oxaddress__oxcountryid->value;
01617                 }
01618             }
01619 
01620             // still not found ?
01621             if ( !$sDelivCountry ) {
01622                 $sDelivCountry = $oUser->oxuser__oxcountryid->value;
01623             }
01624         }
01625 
01626         return $sDelivCountry;
01627     }
01628 
01634     public function deleteBasket()
01635     {
01636         $this->_aBasketContents = array();
01637         $this->getSession()->delBasket();
01638 
01639         if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
01640             $this->getSession()->getBasketReservations()->discardReservations();
01641         }
01642 
01643         // merging basket history
01644         if ( !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' ) ) {
01645             $this->_deleteSavedBasket();
01646         }
01647     }
01648 
01656     public function setPayment( $sPaymentId = null )
01657     {
01658         $this->_sPaymentId = $sPaymentId;
01659     }
01660 
01666     public function getPaymentId()
01667     {
01668         if ( !$this->_sPaymentId ) {
01669              $this->_sPaymentId = oxConfig::getParameter( 'paymentid' );
01670         }
01671         return $this->_sPaymentId;
01672     }
01673 
01681     public function setShipping( $sShippingSetId = null )
01682     {
01683         $this->_sShippingSetId = $sShippingSetId;
01684         oxSession::setVar( 'sShipSet', $sShippingSetId );
01685     }
01686 
01694     public function setDeliveryPrice( $oShippingPrice = null )
01695     {
01696         $this->_oDeliveryPrice = $oShippingPrice;
01697     }
01698 
01704     public function getShippingId()
01705     {
01706         if ( !$this->_sShippingSetId ) {
01707              $this->_sShippingSetId = oxConfig::getParameter( 'sShipSet' );
01708         }
01709 
01710         $sActPaymentId = $this->getPaymentId();
01711         // setting default if none is set
01712         if ( !$this->_sShippingSetId && $sActPaymentId != 'oxempty' ) {
01713             $oUser = $this->getUser();
01714 
01715             // choosing first preferred delivery set
01716             list( , $sActShipSet ) = oxDeliverySetList::getInstance()->getDeliverySetData( null, $oUser, $this );
01717             // in case nothing was found and no user set - choosing default
01718             $this->_sShippingSetId = $sActShipSet ? $sActShipSet : ( $oUser ? null : 'oxidstandard' );
01719         } elseif ( !$this->isAdmin() && $sActPaymentId == 'oxempty' ) {
01720             // in case 'oxempty' is payment id - delivery set must be reset
01721             $this->_sShippingSetId = null;
01722         }
01723 
01724         return $this->_sShippingSetId;
01725     }
01726 
01732     public function getBasketArticles()
01733     {
01734         $aBasketArticles = array();
01735 
01736         foreach ( $this->_aBasketContents as $sItemKey => $oBasketItem ) {
01737             try {
01738                 $oProduct = $oBasketItem->getArticle();
01739 
01740                 if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) ) {
01741                     // marking chosen select list
01742                     $aSelList = $oBasketItem->getSelList();
01743                     if ( is_array( $aSelList ) && ( $aSelectlist = $oProduct->getSelectLists( $sItemKey ) ) ) {
01744                         reset( $aSelList );
01745                         while ( list( $conkey, $iSel ) = each( $aSelList ) ) {
01746                             $aSelectlist[$conkey][$iSel]->selected = 1;
01747                         }
01748                         $oProduct->setSelectlist( $aSelectlist );
01749                     }
01750                 }
01751             } catch ( oxNoArticleException $oEx ) {
01752                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
01753                 $this->removeItem( $sItemKey );
01754                 $this->calculateBasket( true );
01755                 continue;
01756             } catch ( oxArticleInputException $oEx ) {
01757                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
01758                 $this->removeItem( $sItemKey );
01759                 $this->calculateBasket( true );
01760                 continue;
01761             }
01762 
01763             $aBasketArticles[$sItemKey] = $oProduct;
01764         }
01765         return $aBasketArticles;
01766     }
01767 
01773     public function getDiscountProductsPrice()
01774     {
01775         return $this->_oDiscountProductsPriceList;
01776     }
01777 
01783     public function getProductsPrice()
01784     {
01785         if ( is_null($this->_oProductsPriceList) ) {
01786             $this->_oProductsPriceList = oxNew( 'oxPriceList' );
01787         }
01788 
01789         return $this->_oProductsPriceList;
01790     }
01791 
01797     public function getPrice()
01798     {
01799         if ( is_null($this->_oPrice) ) {
01800             $this->_oPrice = oxNew( 'oxprice' );
01801         }
01802 
01803         return $this->_oPrice;
01804     }
01805 
01812     public function getOrderId()
01813     {
01814         return $this->_sOrderId;
01815     }
01816 
01824     public function setOrderId( $sId )
01825     {
01826         $this->_sOrderId = $sId;
01827     }
01828 
01837     public function getCosts( $sId = null )
01838     {
01839         // if user want some specific cost - return it
01840         if ( $sId ) {
01841             return isset( $this->_aCosts[$sId] )?$this->_aCosts[$sId]:null;
01842         }
01843         return $this->_aCosts;
01844     }
01845 
01851     public function getVouchers()
01852     {
01853         return $this->_aVouchers;
01854     }
01855 
01861     public function getProductsCount()
01862     {
01863         return $this->_iProductsCnt;
01864     }
01865 
01871     public function getItemsCount()
01872     {
01873         return $this->_dItemsCnt;
01874     }
01875 
01881     public function getWeight()
01882     {
01883         return $this->_dWeight;
01884     }
01885 
01891     public function getContents()
01892     {
01893         return $this->_aBasketContents;
01894     }
01895 
01903     public function getProductVats( $blFormatCurrency = true)
01904     {
01905         if ( !$this->_oNotDiscountedProductsPriceList ) {
01906             return array();
01907         }
01908 
01909         $aVats = $this->_oNotDiscountedProductsPriceList->getVatInfo();
01910 
01911         $oUtils = oxUtils::getInstance();
01912         foreach ( $this->_aDiscountedVats as $sKey => $dVat ) {
01913             // add prices of the same discounts
01914             $aVats[$sKey] += $oUtils->fRound( $dVat, $this->_oCurrency);
01915         }
01916 
01917         if ( $blFormatCurrency ) {
01918             $oLang = oxLang::getInstance();
01919             foreach ( $aVats as $sKey => $dVat ) {
01920                 $aVats[$sKey] = $oLang->formatCurrency( $dVat, $this->getBasketCurrency() );
01921             }
01922         }
01923 
01924         return $aVats;
01925     }
01926 
01932     public function getDiscountedNettoPrice()
01933     {
01934         if ( $this->_oNotDiscountedProductsPriceList ) {
01935             return $this->_dDiscountedProductNettoPrice + $this->_oNotDiscountedProductsPriceList->getNettoSum();
01936         }
01937         return false;
01938     }
01939 
01947     public function setCardMessage( $sMessage )
01948     {
01949         $this->_sCardMessage = $sMessage;
01950     }
01951 
01957     public function getCardMessage()
01958     {
01959         return $this->_sCardMessage;
01960     }
01961 
01969     public function setCardId( $sCardId )
01970     {
01971         $this->_sCardId = $sCardId;
01972     }
01973 
01979     public function getCardId()
01980     {
01981         return $this->_sCardId;
01982     }
01983 
01989     public function getCard()
01990     {
01991         $oCard = null;
01992         if ( $sCardId = $this->getCardId() ) {
01993             $oCard = oxNew( 'oxwrapping' );
01994             $oCard->load( $sCardId );
01995         }
01996         return $oCard;
01997     }
01998 
02004     public function getTotalDiscount()
02005     {
02006         return $this->_oTotalDiscount;
02007     }
02008 
02014     public function getDiscounts()
02015     {
02016         if ( $this->getTotalDiscount() && $this->getTotalDiscount()->getBruttoPrice() == 0 && count($this->_aItemDiscounts) == 0) {
02017             return null;
02018         }
02019 
02020         return array_merge($this->_aItemDiscounts, $this->_aDiscounts);
02021     }
02022 
02028     public function getVoucherDiscount()
02029     {
02030         if ($this->getConfig()->getConfigParam( 'bl_showVouchers' )) {
02031             return $this->_oVoucherDiscount;
02032         }
02033         return null;
02034     }
02035 
02043     public function setBasketCurrency( $oCurrency )
02044     {
02045         $this->_oCurrency = $oCurrency;
02046     }
02047 
02053     public function getBasketCurrency()
02054     {
02055         if ( $this->_oCurrency === null ) {
02056             $this->_oCurrency = $this->getConfig()->getActShopCurrencyObject();
02057         }
02058 
02059         return $this->_oCurrency;
02060     }
02061 
02069     public function setSkipVouchersChecking( $blSkipChecking = null )
02070     {
02071         $this->_blSkipVouchersAvailabilityChecking = $blSkipChecking;
02072     }
02073 
02079     public function hasSkipedDiscount()
02080     {
02081         return $this->_blSkipDiscounts;
02082     }
02083 
02091     public function setSkipDiscounts( $blSkip )
02092     {
02093         $this->_blSkipDiscounts = $blSkip;
02094     }
02095 
02101     public function getProductsNetPrice()
02102     {
02103         return oxLang::getInstance()->formatCurrency( $this->getDiscountedNettoPrice(), $this->getBasketCurrency() );
02104     }
02105 
02111     public function getFProductsPrice()
02112     {
02113         if ( $this->_oProductsPriceList ) {
02114             return oxLang::getInstance()->formatCurrency( $this->_oProductsPriceList->getBruttoSum(), $this->getBasketCurrency() );
02115         }
02116         return null;
02117     }
02118 
02124     public function getDelCostVatPercent()
02125     {
02126         return $this->getCosts( 'oxdelivery' )->getVat();
02127     }
02128 
02134     public function getDelCostVat()
02135     {
02136         $dDelVAT = $this->getCosts( 'oxdelivery' )->getVatValue();
02137         if ( $dDelVAT > 0 ) {
02138             return oxLang::getInstance()->formatCurrency( $dDelVAT, $this->getBasketCurrency() );
02139         }
02140         return false;
02141     }
02142 
02148     public function getDelCostNet()
02149     {
02150         if ( $this->getBasketUser() || $this->getConfig()->getConfigParam( 'blCalculateDelCostIfNotLoggedIn' ) ) {
02151             return oxLang::getInstance()->formatCurrency( $this->getCosts( 'oxdelivery' )->getNettoPrice(), $this->getBasketCurrency() );
02152         }
02153         return false;
02154     }
02155 
02161     public function getPayCostVatPercent()
02162     {
02163         return $this->getCosts( 'oxpayment' )->getVat();
02164     }
02165 
02171     public function getPayCostVat()
02172     {
02173         $dPayVAT = $this->getCosts( 'oxpayment' )->getVatValue();
02174         if ( $dPayVAT > 0 ) {
02175             return oxLang::getInstance()->formatCurrency( $dPayVAT, $this->getBasketCurrency() );
02176         }
02177         return false;
02178     }
02179 
02185     public function getPayCostNet()
02186     {
02187         return oxLang::getInstance()->formatCurrency( $this->getCosts( 'oxpayment' )->getNettoPrice(), $this->getBasketCurrency() );
02188     }
02189 
02195     public function getPaymentCosts()
02196     {
02197         return $this->getCosts( 'oxpayment' )->getBruttoPrice();
02198     }
02199 
02205     public function getFPaymentCosts()
02206     {
02207         $oPaymentCost = $this->getCosts( 'oxpayment' );
02208 
02209         if ( $oPaymentCost && $oPaymentCost->getBruttoPrice() ) {
02210             return oxLang::getInstance()->formatCurrency( $oPaymentCost->getBruttoPrice(), $this->getBasketCurrency() );
02211         }
02212         return false;
02213     }
02214 
02220     public function getVoucherDiscValue()
02221     {
02222         if ( $this->getVoucherDiscount() ) {
02223             return $this->getVoucherDiscount()->getBruttoPrice();
02224         }
02225         return false;
02226     }
02227 
02233     public function getWrappCostVatPercent()
02234     {
02235         return $this->getCosts( 'oxwrapping' )->getVat();
02236     }
02237 
02243     public function getWrappCostVat()
02244     {
02245         $dWrappVAT = $this->getCosts( 'oxwrapping' )->getVatValue();
02246         if ( $dWrappVAT > 0 ) {
02247             return oxLang::getInstance()->formatCurrency( $dWrappVAT, $this->getBasketCurrency() );
02248         }
02249         return false;
02250 
02251     }
02252 
02258     public function getWrappCostNet()
02259     {
02260         $dWrappNet = $this->getCosts( 'oxwrapping' )->getNettoPrice();
02261         if ( $dWrappNet > 0 ) {
02262             return  oxLang::getInstance()->formatCurrency( $dWrappNet, $this->getBasketCurrency() );
02263         }
02264         return false;
02265     }
02266 
02272     public function getFWrappingCosts()
02273     {
02274         $oWrappingCost = $this->getCosts( 'oxwrapping' );
02275         if ( $oWrappingCost && $oWrappingCost->getBruttoPrice() ) {
02276             return oxLang::getInstance()->formatCurrency( $oWrappingCost->getBruttoPrice(), $this->getBasketCurrency() );
02277         }
02278         return false;
02279     }
02280 
02286     public function getFPrice()
02287     {
02288         return oxLang::getInstance()->formatCurrency( $this->getPrice()->getBruttoPrice(), $this->getBasketCurrency() );
02289     }
02290 
02296     public function getFDeliveryCosts()
02297     {
02298         $oDeliveryCost = $this->getCosts( 'oxdelivery' );
02299         if ( $oDeliveryCost && $oDeliveryCost->getBruttoPrice()) {
02300             return oxLang::getInstance()->formatCurrency( $oDeliveryCost->getBruttoPrice(), $this->getBasketCurrency() );
02301         }
02302         return false;
02303     }
02304 
02310     public function getDeliveryCosts()
02311     {
02312         if ( $oDeliveryCost = $this->getCosts( 'oxdelivery' ) ) {
02313             return $oDeliveryCost->getBruttoPrice();
02314         }
02315         return false;
02316     }
02317 
02325     public function setTotalDiscount( $dDiscount )
02326     {
02327         $this->_oTotalDiscount = oxNew( 'oxPrice' );
02328         $this->_oTotalDiscount->setBruttoPriceMode();
02329         $this->_oTotalDiscount->add( $dDiscount );
02330     }
02331 
02338     public function getPriceForPayment()
02339     {
02340         $dPrice = $this->getDiscountedProductsBruttoPrice();
02341 
02342         // adding delivery price to final price
02343         if ( $oDeliveryPrice = $this->_aCosts['oxdelivery'] ) {
02344             $dPrice += $oDeliveryPrice->getBruttoPrice();
02345         }
02346 
02347         return $dPrice;
02348     }
02349 
02355     public function getDiscountedProductsBruttoPrice()
02356     {
02357         if ( $oProductsPrice = $this->getDiscountProductsPrice() ) {
02358             $dPrice = $oProductsPrice->getBruttoSum();
02359         }
02360 
02361         // substracting total discount
02362         if ( $oPrice = $this->getTotalDiscount() ) {
02363             $dPrice -= $oPrice->getBruttoPrice();
02364         }
02365 
02366         if ( $oVoucherPrice = $this->getVoucherDiscount() ) {
02367             $dPrice -= $oVoucherPrice->getBruttoPrice();
02368         }
02369 
02370         return $dPrice;
02371     }
02372 
02378     public function isBelowMinOrderPrice()
02379     {
02380         $blIsBelowMinOrderPrice = false;
02381         $sConfValue = $this->getConfig()->getConfigParam( 'iMinOrderPrice' );
02382         if ( is_numeric($sConfValue) && $this->getProductsCount() ) {
02383             $dMinOrderPrice = oxPrice::getPriceInActCurrency( ( int ) $sConfValue );
02384             $blIsBelowMinOrderPrice = ($dMinOrderPrice > $this->getDiscountedProductsBruttoPrice());
02385         }
02386 
02387         return $blIsBelowMinOrderPrice;
02388 
02389     }
02390 
02399     public function getArtStockInBasket( $sArtId, $sExpiredArtId = null )
02400     {
02401         $dArtStock = 0;
02402         foreach ( $this->_aBasketContents as $sItemKey => $oOrderArticle ) {
02403             if ( $oOrderArticle && ( $sExpiredArtId == null || $sExpiredArtId != $sItemKey ) ) {
02404                 if ( $oOrderArticle->getArticle()->getId() == $sArtId ) {
02405                     $dArtStock += $oOrderArticle->getAmount();
02406                 }
02407             }
02408         }
02409 
02410         return $dArtStock;
02411     }
02412 
02420     public function canAddProductToBasket( $sProductId )
02421     {
02422         $blCanAdd = null;
02423 
02424         // if basket category is not set..
02425         if ( $this->_sBasketCategoryId === null ) {
02426             $oCat = null;
02427 
02428             // request category
02429             if ( $oView = $this->getConfig()->getActiveView() ) {
02430                 if ( $oCat = $oView->getActCategory() ) {
02431                     if ( !$this->_isProductInRootCategory( $sProductId, $oCat->oxcategories__oxrootid->value ) ) {
02432                         $oCat = null;
02433                     } else {
02434                         $blCanAdd = true;
02435                     }
02436                 }
02437             }
02438 
02439             // product main category
02440             if ( !$oCat ) {
02441                 $oProduct = oxNew( "oxarticle" );
02442                 if ( $oProduct->load( $sProductId ) ) {
02443                     $oCat = $oProduct->getCategory();
02444                 }
02445             }
02446 
02447             // root category id
02448             if ( $oCat ) {
02449                 $this->setBasketRootCatId($oCat->oxcategories__oxrootid->value);
02450             }
02451         }
02452 
02453         // avoiding double check..
02454         if ( $blCanAdd === null ) {
02455             $blCanAdd = $this->_sBasketCategoryId ? $this->_isProductInRootCategory( $sProductId, $this->getBasketRootCatId() ) : true;
02456         }
02457 
02458         return $blCanAdd;
02459     }
02460 
02469     protected function _isProductInRootCategory( $sProductId, $sRootCatId )
02470     {
02471         $sO2CTable = getViewName( 'oxobject2category' );
02472         $sCatTable = getViewName( 'oxcategories' );
02473 
02474         $oDb = oxDb::getDb();
02475         $sParentId  = $oDb->getOne( "select oxparentid from oxarticles where oxid = ".$oDb->quote( $sProductId ) );
02476         $sProductId = $sParentId ? $sParentId : $sProductId;
02477 
02478         $sQ = "select 1 from {$sO2CTable}
02479                  left join {$sCatTable} on {$sCatTable}.oxid = {$sO2CTable}.oxcatnid
02480                  where {$sO2CTable}.oxobjectid = ".$oDb->quote( $sProductId )." and
02481                        {$sCatTable}.oxrootid = ".$oDb->quote( $sRootCatId );
02482 
02483         return (bool) $oDb->getOne( $sQ );
02484     }
02485 
02493     public function setBasketRootCatId($sRoot)
02494     {
02495         $this->_sBasketCategoryId = $sRoot;
02496     }
02497 
02503     public function getBasketRootCatId()
02504     {
02505         return $this->_sBasketCategoryId;
02506     }
02507 
02515     public function setCatChangeWarningState( $blShow )
02516     {
02517         $this->_blShowCatChangeWarning = $blShow;
02518     }
02519 
02525     public function showCatChangeWarning()
02526     {
02527         return $this->_blShowCatChangeWarning;
02528     }
02529 
02537     public function setTsProductId( $sProductId )
02538     {
02539         $this->_sTsProductId = $sProductId;
02540     }
02541 
02547     public function getTsProductId()
02548     {
02549         return $this->_sTsProductId;
02550     }
02551 
02557     public function getFTsProtectionCosts()
02558     {
02559         $oProtectionCost = $this->getCosts( 'oxtsprotection' );
02560         if ( $oProtectionCost && $oProtectionCost->getBruttoPrice() ) {
02561             return oxLang::getInstance()->formatCurrency( $oProtectionCost->getBruttoPrice(), $this->getBasketCurrency() );
02562         }
02563         return false;
02564     }
02565 
02571     public function getTsProtectionVatPercent()
02572     {
02573         return $this->getCosts( 'oxtsprotection' )->getVat();
02574     }
02575 
02581     public function getTsProtectionVat()
02582     {
02583         $dProtectionVAT = $this->getCosts( 'oxtsprotection' )->getVatValue();
02584         if ( $dProtectionVAT > 0 ) {
02585             return oxLang::getInstance()->formatCurrency( $dProtectionVAT, $this->getBasketCurrency() );
02586         }
02587         return false;
02588     }
02589 
02595     public function getTsProtectionNet()
02596     {
02597         return oxLang::getInstance()->formatCurrency( $this->getCosts( 'oxtsprotection' )->getNettoPrice(), $this->getBasketCurrency() );
02598     }
02599 
02605     public function getTsProtectionCosts()
02606     {
02607         $oProtection = $this->getCosts( 'oxtsprotection' );
02608         if ( $oProtection ) {
02609             return $oProtection->getBruttoPrice();
02610         }
02611         return false;
02612     }
02613 
02614 }

Generated by  doxygen 1.6.2