00001 <?php
00002
00006 class oxBasket extends oxSuperCfg
00007 {
00013 protected $_aBasketContents = array();
00014
00020 protected $_iProductsCnt = 0;
00021
00027 protected $_dItemsCnt = 0.0;
00028
00034 protected $_dWeight = 0.0;
00035
00041 protected $_oPrice = null;
00042
00048 protected $_oProductsPriceList = null;
00049
00055 protected $_aDiscounts = array();
00056
00062 protected $_aItemDiscounts = array();
00063
00069 protected $_sOrderId = null;
00070
00076 protected $_aVouchers = array();
00077
00083 protected $_aCosts = array();
00084
00090 protected $_oDiscountProductsPriceList = null;
00091
00097 protected $_oNotDiscountedProductsPriceList = null;
00098
00104 protected $_blUpdateNeeded = true;
00105
00111 protected $_aBasketSummary = null;
00112
00118 protected $_blBasketMerged = false;
00119
00125 protected $_sPaymentId = null;
00126
00132 protected $_sShippingSetId = null;
00133
00139 protected $_oUser = null;
00140
00146 protected $_oTotalDiscount = null;
00147
00153 protected $_oVoucherDiscount = null;
00154
00160 protected $_oCurrency = null;
00161
00167 protected $_blSkipVouchersAvailabilityChecking = null;
00168
00174 protected $_dDiscountedProductNettoPrice = null;
00175
00181 protected $_aDiscountedVats = null;
00182
00188 protected $_blSkipDiscounts = false;
00189
00195 public function isEnabled()
00196 {
00197 return !oxUtils::getInstance()->isSearchEngine();
00198 }
00199
00209 protected function _changeBasketItemKey($sOldKey, $sNewKey, $value = null)
00210 {
00211 reset($this->_aBasketContents);
00212 $iOldKeyPlace = 0;
00213 while (key($this->_aBasketContents) != $sOldKey && next($this->_aBasketContents)) {
00214 ++$iOldKeyPlace;
00215 }
00216 $aNewCopy = array_merge(
00217 array_slice($this->_aBasketContents, 0, $iOldKeyPlace, true),
00218 array($sNewKey => $value),
00219 array_slice($this->_aBasketContents, $iOldKeyPlace+1, count($this->_aBasketContents)-$iOldKeyPlace, true)
00220 );
00221 $this->_aBasketContents = $aNewCopy;
00222 }
00223
00239 public function addToBasket( $sProductID, $dAmount, $aSel = null, $aPersParam = null, $blOverride = false, $blBundle = false, $sOldBasketItemId = null )
00240 {
00241
00242 if ( !$this->isEnabled() )
00243 return null;
00244
00245
00246
00247 $sItemId = $this->getItemKey( $sProductID, $aSel, $aPersParam, $blBundle );
00248 if ($sOldBasketItemId && strcmp($sOldBasketItemId, $sItemId)) {
00249 if (isset( $this->_aBasketContents[$sItemId] )) {
00250
00251 unset( $this->_aBasketContents[$sOldBasketItemId] );
00252 } else {
00253
00254 $this->_changeBasketItemKey($sOldBasketItemId, $sItemId);
00255 }
00256 }
00257
00258
00259 $blRemoveItem = false;
00260
00261
00262 $oEx = null;
00263
00264 if ( isset( $this->_aBasketContents[$sItemId] ) ) {
00265
00266
00267 try {
00268 $this->_aBasketContents[$sItemId]->setAmount( $dAmount, $blOverride );
00269 } catch( oxOutOfStockException $oEx ) {
00270
00271 }
00272
00273 } else {
00274
00275 $oBasketItem = oxNew( 'oxbasketitem' );
00276 try {
00277 $oBasketItem->init( $sProductID, $dAmount, $aSel, $aPersParam, $blBundle );
00278 } catch( oxNoArticleException $oEx ) {
00279
00280
00281 $blRemoveItem = true;
00282
00283 } catch( oxOutOfStockException $oEx ){
00284
00285 }
00286
00287 $this->_aBasketContents[$sItemId] = $oBasketItem;
00288 }
00289
00290
00291 if ( $this->_aBasketContents[$sItemId]->getAmount() == 0 || $blRemoveItem ) {
00292 $this->removeItem( $sItemId );
00293 $blRemoveItem = true;
00294 } elseif ( $blBundle ) {
00295 $this->_aBasketContents[$sItemId]->setBundle( true );
00296 }
00297
00298
00299 $this->onUpdate();
00300
00301
00302 if ( !$blBundle && !$blRemoveItem ) {
00303 $this->_addItemToSavedBasket( $this->_aBasketContents[$sItemId]->getProductId(), $dAmount, $aSel, $blOverride );
00304 }
00305
00306 if ( $oEx ) {
00307 throw $oEx;
00308 }
00309 return $this->_aBasketContents[$sItemId];
00310 }
00311
00312
00313
00326 public function getItemKey( $sProductId, $aSel = null, $aPersParam = null, $blBundle = false, $sAdditionalParam = '' )
00327 {
00328 $aSel = ( $aSel != null) ? $aSel : array (0=>'0');
00329
00330 $sItemKey = md5( $sProductId.'|'.serialize( $aSel ).'|'.serialize( $aPersParam ).'|'.( int ) $blBundle . '|' . serialize( $sAdditionalParam ) );
00331
00332 return $sItemKey;
00333 }
00334
00335
00343 public function removeItem( $sItemKey )
00344 {
00345 unset( $this->_aBasketContents[$sItemKey] );
00346 }
00347
00353 protected function _clearBundles()
00354 {
00355 reset( $this->_aBasketContents );
00356 while ( list( $sItemKey, $oBasketItem ) = each( $this->_aBasketContents ) )
00357 if ( $oBasketItem->isBundle() ) {
00358 $this->removeItem( $sItemKey );
00359 }
00360 }
00361
00369 protected function _getArticleBundles( $oBasketItem )
00370 {
00371 $aBundles = array();
00372
00373 if ( $oBasketItem->isBundle() ) {
00374 return $aBundles;
00375 }
00376
00377 $oArticle = $oBasketItem->getArticle();
00378 if ( $oArticle && $oArticle->oxarticles__oxbundleid->value ) {
00379 $aBundles[$oArticle->oxarticles__oxbundleid->value] = 1;
00380 }
00381
00382 return $aBundles;
00383 }
00384
00392 protected function _getItemBundles( $oBasketItem )
00393 {
00394 if ( $oBasketItem->isBundle() ) {
00395 return array();
00396 }
00397
00398 $aBundles = array();
00399
00400
00401 if ( $oArticle = $oBasketItem->getArticle() ) {
00402 $aDiscounts = oxDiscountList::getInstance()->getBasketItemBundleDiscounts( $oArticle, $this, $this->getBasketUser() );
00403
00404 foreach ( $aDiscounts as $oDiscount ) {
00405
00406
00407 if ( !isset( $aBundles[$oDiscount->oxdiscount__oxitmartid->value] ) ) {
00408 $aBundles[$oDiscount->oxdiscount__oxitmartid->value] = 0;
00409 }
00410
00411 $aBundles[$oDiscount->oxdiscount__oxitmartid->value] += $oDiscount->getBundleAmount( $oBasketItem->getAmount() );
00412
00413 }
00414 }
00415
00416 return $aBundles;
00417 }
00418
00424 protected function _getBasketBundles()
00425 {
00426 $aBundles = array();
00427 $aDiscounts = oxDiscountList::getInstance()->getBasketBundleDiscounts( $this, $this->getBasketUser() );
00428
00429
00430 $dAmount = 0;
00431 foreach ( $this->_aBasketContents as $oBasketItem ) {
00432 if ( !( $oBasketItem->isBundle() || $oBasketItem->isDiscountArticle() ) ) {
00433 $dAmount += $oBasketItem->getAmount();
00434 }
00435 }
00436
00437 foreach ( $aDiscounts as $oDiscount ) {
00438 if ($oDiscount->oxdiscount__oxitmartid->value) {
00439 if ( !isset( $aBundles[$oDiscount->oxdiscount__oxitmartid->value] ) ) {
00440 $aBundles[$oDiscount->oxdiscount__oxitmartid->value] = 0;
00441 }
00442
00443 $aBundles[$oDiscount->oxdiscount__oxitmartid->value] += $oDiscount->getBundleAmount( $dAmount );
00444
00445 }
00446 }
00447
00448 return $aBundles;
00449 }
00450
00457 protected function _addBundles()
00458 {
00459
00460 foreach ( $this->_aBasketContents as $key => $oBasketItem ) {
00461
00462
00463 if ( !$oBasketItem->isDiscountArticle() && !$oBasketItem->isBundle() ) {
00464 $aBundles = $this->_getItemBundles( $oBasketItem );
00465 } else {
00466 continue;
00467 }
00468
00469 $this->_addBundlesToBasket( $aBundles );
00470
00471
00472 $aBundles = $this->_getArticleBundles( $oBasketItem );
00473
00474
00475 $this->_addBundlesToBasket( $aBundles );
00476 }
00477
00478
00479 if ( $aBundles = $this->_getBasketBundles() ) {
00480 $this->_addBundlesToBasket( $aBundles );
00481 }
00482
00483 }
00484
00492 protected function _addBundlesToBasket( $aBundles )
00493 {
00494 foreach ( $aBundles as $sBundleId => $dAmount ) {
00495 try {
00496 if ( $oBundleItem = $this->addToBasket( $sBundleId, $dAmount, null, null, true, true ) ) {
00497 $oBundleItem->setAsDiscountArticle( true );
00498 }
00499 } catch(oxArticleException $oEx) {
00500
00501 }
00502 }
00503
00504 }
00505
00511 protected function _calcItemsPrice()
00512 {
00513
00514 $this->setSkipDiscounts( false );
00515 $this->_iProductsCnt = 0;
00516 $this->_dItemsCnt = 0;
00517 $this->_dWeight = 0;
00518
00519
00520 $this->_aItemDiscounts = array();
00521
00522 $this->_oProductsPriceList = oxNew( 'oxpricelist' );
00523 $this->_oDiscountProductsPriceList = oxNew( 'oxpricelist' );
00524 $this->_oNotDiscountedProductsPriceList = oxNew( 'oxpricelist' );
00525
00526 foreach ( $this->_aBasketContents as $oBasketItem ) {
00527 $this->_iProductsCnt++;
00528 $this->_dItemsCnt += $oBasketItem->getAmount();
00529 $this->_dWeight += $oBasketItem->getWeight();
00530
00531 if ( !$oBasketItem->isDiscountArticle() && ( $oArticle = $oBasketItem->getArticle() ) ) {
00532 $oBasketPrice = $oArticle->getBasketPrice( $oBasketItem->getAmount(), $oBasketItem->getSelList(), $this );
00533 $oBasketItem->setPrice( $oBasketPrice );
00534
00535 $this->_oProductsPriceList->addToPriceList( $oBasketItem->getPrice() );
00536
00537 $oBasketPrice->setBruttoPriceMode();
00538 if ( !$oArticle->skipDiscounts() ) {
00539
00540 $aItemDiscounts = $oArticle->applyBasketDiscounts( $oBasketPrice, oxDiscountList::getInstance()->getBasketItemDiscounts( $oArticle, $this, $this->getBasketUser() ), $oBasketItem->getAmount() );
00541 if ( is_array($this->_aItemDiscounts) && is_array($aItemDiscounts) ) {
00542 $this->_aItemDiscounts = $this->_mergeDiscounts( $this->_aItemDiscounts, $aItemDiscounts);
00543 }
00544 } else {
00545 $oBasketItem->setSkipDiscounts( true );
00546 $this->setSkipDiscounts( true );
00547 }
00548 $oBasketPrice->multiply( $oBasketItem->getAmount() );
00549
00550
00551 if ( !$oArticle->skipDiscounts() ) {
00552 $this->_oDiscountProductsPriceList->addToPriceList( $oBasketPrice );
00553 } else {
00554 $this->_oNotDiscountedProductsPriceList->addToPriceList( $oBasketPrice );
00555 $oBasketItem->setSkipDiscounts( true );
00556 $this->setSkipDiscounts( true );
00557 }
00558 } elseif ( $oBasketItem->isBundle() ) {
00559
00560 $oPrice = oxNew( "oxprice");
00561 $oBasketItem->setPrice( $oPrice );
00562 }
00563 }
00564 }
00565
00575 protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
00576 {
00577 foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
00578
00579 if ( array_key_exists ($sKey, $aDiscounts) ) {
00580 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
00581 } else {
00582 $aDiscounts[$sKey] = $oDiscount;
00583 }
00584 }
00585 return $aDiscounts;
00586 }
00587
00593 protected function _calcDeliveryCost()
00594 {
00595 $myConfig = $this->getConfig();
00596 $oDeliveryPrice = oxNew( 'oxprice' );
00597 $oDeliveryPrice->setBruttoPriceMode();
00598
00599
00600 $oUser = $this->getBasketUser();
00601
00602 if ( !$oUser && !$myConfig->getConfigParam( 'blCalculateDelCostIfNotLoggedIn' ) ) {
00603 return $oDeliveryPrice;
00604 }
00605
00606
00607 $fDelVATPercent = 0;
00608 if ( $myConfig->getConfigParam( 'blCalcVATForDelivery' ) ) {
00609 $fDelVATPercent = $this->getMostUsedVatPercent();
00610 $oDeliveryPrice->setVat( $fDelVATPercent );
00611 }
00612
00613
00614 if ( $myConfig->getConfigParam('bl_perfLoadDelivery') ) {
00615 $aDeliveryList = oxDeliveryList::getInstance()->getDeliveryList( $this,
00616 $oUser,
00617 $this->_findDelivCountry(),
00618 oxConfig::getParameter( 'sShipSet' )
00619 );
00620
00621 if ( count( $aDeliveryList ) > 0 ) {
00622 foreach ( $aDeliveryList as $oDelivery ) {
00623
00624 if ( $myConfig->getConfigParam( 'iDebug' ) == 5 ) {
00625 echo( "DelCost : ".$oDelivery->oxdelivery__oxtitle->value."<br>" );
00626 }
00627 $oDeliveryPrice->addPrice( $oDelivery->getDeliveryPrice( $fDelVATPercent ) );
00628 }
00629 }
00630 }
00631
00632 return $oDeliveryPrice;
00633 }
00634
00640 public function getBasketUser()
00641 {
00642 if ( $this->_oUser == null ) {
00643 return $this->getUser();
00644 }
00645
00646 return $this->_oUser;
00647 }
00648
00656 public function setBasketUser( $oUser )
00657 {
00658 $this->_oUser = $oUser;
00659 }
00660
00661
00667 public function getMostUsedVatPercent()
00668 {
00669 return $this->_oProductsPriceList->getMostUsedVatPercent();
00670 }
00671
00672
00679 protected function _calcTotalPrice()
00680 {
00681
00682 $dprice = $this->_oProductsPriceList->getBruttoSum();
00683 $this->_oPrice->setPrice( $dprice );
00684
00685
00686 if ( $dprice ) {
00687
00688
00689 foreach ( $this->_aItemDiscounts as $oDiscount ) {
00690
00691
00692 if ( $oDiscount->sType == 'itm' ) {
00693 continue;
00694 }
00695 $this->_oPrice->subtract( $oDiscount->dDiscount );
00696 }
00697
00698
00699 $this->_oPrice->subtract( $this->_oTotalDiscount->getBruttoPrice() );
00700
00701
00702 $this->_oPrice->subtract( $this->_oVoucherDiscount->getBruttoPrice() );
00703 }
00704
00705
00706 if ( isset( $this->_aCosts['oxdelivery'] ) ) {
00707 $this->_oPrice->add( $this->_aCosts['oxdelivery']->getBruttoPrice() );
00708 }
00709
00710
00711 if ( isset( $this->_aCosts['oxwrapping'] ) ) {
00712 $this->_oPrice->add( $this->_aCosts['oxwrapping']->getBruttoPrice() );
00713 }
00714
00715
00716 if ( isset( $this->_aCosts['oxpayment'] ) ) {
00717 $this->_oPrice->add( $this->_aCosts['oxpayment']->getBruttoPrice() );
00718 }
00719
00720 }
00721
00727 protected function _calcVoucherDiscount()
00728 {
00729 $this->_oVoucherDiscount = oxNew( 'oxPrice' );
00730 $this->_oVoucherDiscount->setBruttoPriceMode();
00731
00732
00733
00734 $dPrice = $this->_oDiscountProductsPriceList->getBruttoSum() - $this->_oTotalDiscount->getBruttoPrice();
00735
00736
00737 if ( count( $this->_aVouchers ) ) {
00738 foreach ( $this->_aVouchers as $sVoucherId => $oStdVoucher ) {
00739 $oVoucher = oxNew( 'oxvoucher' );
00740 try {
00741 $oVoucher->load( $oStdVoucher->sVoucherId );
00742
00743 if ( !$this->_blSkipVouchersAvailabilityChecking ) {
00744 $oVoucher->checkBasketVoucherAvailability( $this->_aVouchers, $dPrice );
00745 $oVoucher->checkUserAvailability( $this->getBasketUser() );
00746 }
00747
00748
00749 $dVoucherdiscount = $oVoucher->getDiscountValue( $dPrice );
00750
00751
00752 $this->_oVoucherDiscount->add( $dVoucherdiscount );
00753
00754
00755 $oStdVoucher->fVoucherdiscount = oxLang::getInstance()->formatCurrency( $dVoucherdiscount, $this->getBasketCurrency() );
00756
00757
00758 $dPrice -= $dVoucherdiscount;
00759 } catch ( oxVoucherException $oEx ) {
00760
00761
00762 $oVoucher->unMarkAsReserved();
00763 unset( $this->_aVouchers[$sVoucherId] );
00764
00765
00766 oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00767 }
00768 }
00769 }
00770 }
00771
00772
00779 protected function _applyDiscounts()
00780 {
00781 $dBruttoPrice = $this->_oDiscountProductsPriceList->getBruttoSum();
00782 $this->_aDiscountedVats = $this->_oDiscountProductsPriceList->getVatInfo();
00783
00784
00785 $dDiscountedBruttoPrice = $dBruttoPrice - $this->_oTotalDiscount->getBruttoPrice() - $this->_oVoucherDiscount->getBruttoPrice();
00786
00787
00788 if ( $dBruttoPrice && ( $this->_oTotalDiscount->getBruttoPrice() || $this->_oVoucherDiscount->getBruttoPrice() )) {
00789 $dPercent = ( $dDiscountedBruttoPrice / $dBruttoPrice) * 100;
00790 foreach ( $this->_aDiscountedVats as $sKey => $dVat ) {
00791 $this->_aDiscountedVats[$sKey] = oxPrice::percent( $dVat, $dPercent);
00792 }
00793 }
00794
00795 $oUtils = oxUtils::getInstance();
00796 $dDiscVatSum = 0;
00797 foreach ( $this->_aDiscountedVats as $dVat ) {
00798 $dDiscVatSum += $oUtils->fRound( -$dVat, $this->_oCurrency);
00799 }
00800
00801 $this->_dDiscountedProductNettoPrice = $dDiscountedBruttoPrice + $dDiscVatSum;
00802 }
00803
00809 protected function _calcBasketDiscount()
00810 {
00811
00812 $this->_aDiscounts = array();
00813
00814
00815 $dOldprice = $this->_oDiscountProductsPriceList->getBruttoSum();
00816
00817
00818 $aDiscounts = oxDiscountList::getInstance()->getBasketDiscounts( $this, $this->getBasketUser() );
00819
00820 foreach ( $aDiscounts as $oDiscount ) {
00821
00822
00823 $oStdDiscount = $oDiscount->getSimpleDiscount();
00824
00825
00826 if ( $oDiscount->oxdiscount__oxaddsumtype->value == 'itm' ) {
00827 continue;
00828 }
00829
00830
00831 $oStdDiscount->dDiscount = $oDiscount->getAbsValue( $dOldprice );
00832
00833 $this->_aDiscounts[$oDiscount->getId()] = $oStdDiscount;
00834
00835
00836 $dOldprice = $dOldprice - $oStdDiscount->dDiscount;
00837 }
00838 }
00839
00845 protected function _calcBasketTotalDiscount()
00846 {
00847 $this->_oTotalDiscount = oxNew( 'oxPrice' );
00848 $this->_oTotalDiscount->setBruttoPriceMode();
00849
00850 if ( is_array($this->_aDiscounts) ) {
00851 foreach ( $this->_aDiscounts as $oDiscount ) {
00852
00853
00854 if ( $oDiscount->sType == 'itm' ) {
00855 continue;
00856 }
00857
00858
00859 $this->_oTotalDiscount->add( $oDiscount->dDiscount );
00860 }
00861 }
00862 }
00863
00873 protected function _calcBasketWrapping()
00874 {
00875 $myConfig = $this->getConfig();
00876 $oWrappingPrice = oxNew( 'oxPrice' );
00877 $oWrappingPrice->setBruttoPriceMode();
00878
00879
00880 if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
00881 $oWrappingPrice->setVat( $this->getMostUsedVatPercent() );
00882 }
00883
00884
00885 foreach ( $this->_aBasketContents as $oBasketItem ) {
00886
00887 if ( ( $oWrapping = $oBasketItem->getWrapping() ) ) {
00888 $oWrapPrice = $oWrapping->getWrappingPrice( $oBasketItem->getAmount() );
00889 $oWrappingPrice->add( $oWrapPrice->getBruttoPrice() );
00890 }
00891 }
00892
00893
00894 if ( ( $oCard = $this->getCard() ) ) {
00895 $oCardPrice = $oCard->getWrappingPrice();
00896 $oWrappingPrice->add( $oCardPrice->getBruttoPrice() );
00897 }
00898
00899 return $oWrappingPrice;
00900 }
00901
00908 protected function _calcPaymentCost()
00909 {
00910
00911 $oPaymentPrice = oxNew( 'oxPrice' );
00912 $oPaymentPrice->setBruttoPriceMode();
00913
00914
00915 if ( ( $this->_sPaymentId = $this->getPaymentId() ) ) {
00916
00917 $oPayment = oxNew( 'oxpayment' );
00918 $oPayment->load( $this->_sPaymentId );
00919
00920 $oPaymentPrice = $oPayment->getPaymentPrice( $this );
00921 }
00922
00923 return $oPaymentPrice;
00924 }
00925
00934 public function setCost( $sCostName, $oPrice = null )
00935 {
00936 $this->_aCosts[$sCostName] = $oPrice;
00937 }
00938
00947 public function calculateBasket( $blForceUpdate = false )
00948 {
00949 if ( !$this->isEnabled() )
00950 return;
00951
00952 if ( !$this->_blUpdateNeeded && !$blForceUpdate )
00953 return;
00954
00955 $this->_aCosts = array();
00956
00957 $this->_oPrice = oxNew( 'oxprice' );
00958 $this->_oPrice->setBruttoPriceMode();
00959
00960
00961 if ( !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' ) ) {
00962 $this->_mergeSavedBasket();
00963 }
00964
00965
00966 $this->_clearBundles();
00967
00968
00969 $this->_addBundles();
00970
00971
00972 $this->_calcItemsPrice();
00973
00974
00975 $this->_calcBasketDiscount();
00976
00977
00978 $this->_calcBasketTotalDiscount();
00979
00980
00981 $this->_calcVoucherDiscount();
00982
00983
00984 $this->_applyDiscounts();
00985
00986
00987
00988 $this->setCost( 'oxdelivery', $this->_calcDeliveryCost() );
00989
00990
00991 $this->setCost( 'oxwrapping', $this->_calcBasketWrapping() );
00992
00993
00994 $this->setCost( 'oxpayment', $this->_calcPaymentCost() );
00995
00996
00997 $this->_calcTotalPrice();
00998
00999
01000 $this->_setDeprecatedValues();
01001
01002
01003 $this->afterUpdate();
01004 }
01005
01011 public function onUpdate()
01012 {
01013 $this->_blUpdateNeeded = true;
01014 }
01015
01021 public function afterUpdate()
01022 {
01023 $this->_blUpdateNeeded = false;
01024 }
01025
01033 public function getBasketSummary()
01034 {
01035 if ( $this->_blUpdateNeeded || $this->_aBasketSummary === null ) {
01036 $this->_aBasketSummary = new Oxstdclass();
01037 $this->_aBasketSummary->aArticles = array();
01038 $this->_aBasketSummary->aCategories = array();
01039 $this->_aBasketSummary->iArticleCount = 0;
01040 $this->_aBasketSummary->dArticlePrice = 0;
01041 }
01042
01043 if ( !$this->isEnabled() ) {
01044 return $this->_aBasketSummary;
01045 }
01046
01047 $myConfig = $this->getConfig();
01048 foreach ( $this->_aBasketContents as $oBasketItem ) {
01049 if ( !$oBasketItem->isBundle() ) {
01050 if ( ( $oArticle = $oBasketItem->getArticle() ) ) {
01051
01052 $aCatIds = $oArticle->getCategoryIds();
01053 $dPrice = $oArticle->getPrice()->getBruttoPrice();
01054
01055 foreach ( $aCatIds as $sCatId ) {
01056 if ( !isset( $this->_aBasketSummary->aCategories[$sCatId] ) ) {
01057 $this->_aBasketSummary->aCategories[$sCatId] = new Oxstdclass();
01058 }
01059
01060 $this->_aBasketSummary->aCategories[$sCatId]->dPrice += $dPrice * $oBasketItem->getAmount();
01061 $this->_aBasketSummary->aCategories[$sCatId]->dAmount += $oBasketItem->getAmount();
01062 $this->_aBasketSummary->aCategories[$sCatId]->iCount++;
01063 }
01064
01065
01066 if ( $oArticle->oxarticles__oxparentid->value && $myConfig->getConfigParam( 'blVariantParentBuyable' ) ) {
01067 if ( !isset( $this->_aBasketSummary->aArticles[$oArticle->oxarticles__oxparentid->value] ) ) {
01068 $this->_aBasketSummary->aArticles[$oArticle->oxarticles__oxparentid->value] = 0;
01069 }
01070 $this->_aBasketSummary->aArticles[$oArticle->oxarticles__oxparentid->value] += $oBasketItem->getAmount();
01071 }
01072
01073 if ( !isset( $this->_aBasketSummary->aArticles[$oBasketItem->getProductId()] ) ) {
01074 $this->_aBasketSummary->aArticles[$oBasketItem->getProductId()] = 0;
01075 }
01076
01077 $this->_aBasketSummary->aArticles[$oBasketItem->getProductId()] += $oBasketItem->getAmount();
01078 $this->_aBasketSummary->iArticleCount += $oBasketItem->getAmount();
01079 $this->_aBasketSummary->dArticlePrice += $dPrice * $oBasketItem->getAmount();
01080 }
01081 }
01082 }
01083 return $this->_aBasketSummary;
01084 }
01085
01097 public function addVoucher( $sVoucherId )
01098 {
01099
01100
01101 $dPrice = 0;
01102 if ( $this->_oDiscountProductsPriceList ) {
01103 $dPrice = $this->_oDiscountProductsPriceList->getBruttoSum();
01104 }
01105
01106 try {
01107
01108 $oVoucher = oxNew( 'oxvoucher' );
01109
01110 if ( !$this->_blSkipVouchersAvailabilityChecking ) {
01111 $oVoucher->getVoucherByNr( $sVoucherId, $this->_aVouchers, true );
01112 $oVoucher->checkVoucherAvailability( $this->_aVouchers, $dPrice );
01113 $oVoucher->checkUserAvailability( $this->getBasketUser() );
01114 $oVoucher->markAsReserved();
01115 } else {
01116 $oVoucher->load( $sVoucherId );
01117 }
01118
01119
01120 $this->_aVouchers[$oVoucher->oxvouchers__oxid->value] = $oVoucher->getSimpleVoucher();
01121 } catch ( oxVoucherException $oEx ) {
01122
01123
01124 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
01125 }
01126
01127 $this->onUpdate();
01128 }
01129
01137 public function removeVoucher( $sVoucherId )
01138 {
01139
01140 if ( isset( $this->_aVouchers[$sVoucherId] ) ) {
01141
01142 $oVoucher = oxNew( 'oxvoucher' );
01143 $oVoucher->load( $sVoucherId );
01144
01145 $oVoucher->unMarkAsReserved();
01146
01147
01148 unset( $this->_aVouchers[$sVoucherId] );
01149 $this->onUpdate();
01150 }
01151
01152 }
01153
01159 public function resetUserInfo()
01160 {
01161 $this->setPayment( null );
01162 $this->setShipping( null );
01163 }
01164
01172 protected function _setDeprecatedValues()
01173 {
01174
01175 $this->dproductsprice = $this->_oProductsPriceList->getBruttoSum();
01176 $this->dproductsnetprice = $this->getDiscountedNettoPrice();
01177
01178
01179 $this->dVAT = array_sum( $this->_oProductsPriceList->getVatInfo() );
01180
01181
01182 $this->fproductsprice = $this->getFProductsPrice();
01183 $this->fproductsnetprice = $this->getProductsNetPrice();
01184 $this->fVAT = oxLang::getInstance()->formatCurrency( $this->dVAT, $this->getBasketCurrency());
01185
01186
01187 if ( $oDeliveryCost = $this->getCosts( 'oxdelivery' ) ) {
01188
01189 $this->ddeliverycost = $oDeliveryCost->getBruttoPrice();
01190 $this->ddeliverynetcost = $oDeliveryCost->getNettoPrice();
01191 $this->dDelVAT = $oDeliveryCost->getVatValue();
01192 $this->fDelVATPercent = $oDeliveryCost->getVat() / 100;
01193
01194
01195 $this->fdeliverycost = oxLang::getInstance()->formatCurrency( $this->ddeliverycost, $this->getBasketCurrency() );
01196 $this->fdeliverynetcost = oxLang::getInstance()->formatCurrency( $this->ddeliverynetcost, $this->getBasketCurrency() );
01197 $this->fDelVAT = $this->getDelCostVat();
01198 }
01199
01200
01201
01202 if ( $oWrappingCost = $this->getCosts( 'oxwrapping' ) ) {
01203
01204 $this->dWrappingPrice = $oWrappingCost->getBruttoPrice();
01205 $this->dWrappingNetto = $oWrappingCost->getNettoPrice();
01206 $this->dWrappingVAT = $oWrappingCost->getVatValue();
01207
01208
01209 $this->fWrappingPrice = oxLang::getInstance()->formatCurrency( $this->dWrappingPrice, $this->getBasketCurrency() );
01210 $this->fWrappingNetto = $this->getWrappCostNet();
01211 $this->fWrappingVAT = $this->getWrappCostVat();
01212 $this->fWrappingVATPercent = $this->getWrappCostVatPercent();
01213 }
01214
01215
01216
01217 if ( $oPaymentCost = $this->getCosts( 'oxpayment' ) ) {
01218
01219 $this->dAddPaymentSum = $this->getPaymentCosts();
01220 $this->dAddPaymentSumVAT = $oPaymentCost->getVatValue();
01221
01222
01223 $this->fAddPaymentSum = oxLang::getInstance()->formatCurrency( $this->dAddPaymentSum, $this->getBasketCurrency() );
01224 $this->fAddPaymentSumVAT = $this->getPayCostVat();
01225 $this->fAddPaymentSumVATPercent = $this->getPayCostVatPercent();
01226 $this->fAddPaymentNetSum = $this->getPayCostNet();
01227 }
01228
01229
01230
01231 $this->dprice = $this->_oPrice->getBruttoPrice();
01232 $this->fprice = oxLang::getInstance()->formatCurrency( $this->dprice, $this->getBasketCurrency() );
01233
01234
01235 $this->iCntProducts = $this->getProductsCount();
01236 $this->dCntItems = $this->getItemsCount();
01237 $this->aVATs = $this->getProductVats();
01238 $this->aBasketContents = $this->getContents();
01239
01240
01241 $this->giftmessage = $this->getCardMessage();
01242 $this->chosencard = $this->getCardId();
01243
01244 $this->oCard = $this->getCard();
01245
01246
01247
01248 $this->aDiscounts = $this->getDiscounts();
01249 if ( count($this->aDiscounts) > 0 ) {
01250 foreach ($this->aDiscounts as $oDiscount) {
01251 $oDiscount->fDiscount = oxLang::getInstance()->formatCurrency( $oDiscount->dDiscount, $this->getBasketCurrency() );
01252 }
01253 }
01254 $this->dDiscount = $this->getTotalDiscount()->getBruttoPrice();
01255
01256
01257 $this->aVouchers = $this->getVouchers();
01258 $this->dVoucherDiscount = $this->getVoucherDiscValue();
01259 $this->fVoucherDiscount = oxLang::getInstance()->formatCurrency( $this->dVoucherDiscount, $this->getBasketCurrency() );
01260 $this->dSkippedDiscount = $this->hasSkipedDiscount();
01261
01262 }
01263
01264
01271 protected function _mergeSavedBasket()
01272 {
01273 if ( $this->_blBasketMerged ) {
01274 return;
01275 }
01276
01277 $oUser = $this->getBasketUser();
01278 if ( !$oUser ) {
01279 $this->_blBasketMerged = false;
01280 return;
01281 }
01282
01283 $oBasket = $oUser->getBasket( 'savedbasket' );
01284
01285
01286 $aSavedItems = $oBasket->getItems();
01287 foreach ( $aSavedItems as $oItem ) {
01288 try {
01289 $this->addToBasket( $oItem->getId(), $oItem->dAmount, $oItem->aSelList, null, true );
01290 } catch( oxArticleException $oEx ) {
01291
01292 }
01293 }
01294
01295
01296 foreach ( $this->_aBasketContents as $oBasketItem ) {
01297 $oBasket->addItemToBasket( $oBasketItem->getProductId(), $oBasketItem->getAmount(), $oBasketItem->getSelList(), true );
01298 }
01299
01300
01301 $this->_blBasketMerged = true;
01302 }
01303
01314 protected function _addItemToSavedBasket( $sProductId , $dAmount, $aSel, $blOverride = false )
01315 {
01316
01317 if ( $oUser = $this->getBasketUser() ) {
01318 $oUser->getBasket( 'savedbasket' )->addItemToBasket( $sProductId, $dAmount, $aSel, $blOverride );
01319 }
01320 }
01321
01329 protected function _deleteSavedBasket()
01330 {
01331
01332 if ( $oUser = $this->getBasketUser() ) {
01333 $oUser->getBasket( 'savedbasket' )->delete();
01334 }
01335 }
01336
01342 protected function _findDelivCountry()
01343 {
01344 $myConfig = $this->getConfig();
01345 $oUser = $this->getBasketUser();
01346
01347 $sDelivCountry = null;
01348 if ( !$oUser ) {
01349
01350 $aHomeCountry = $myConfig->getConfigParam( 'aHomeCountry' );
01351 if ( $myConfig->getConfigParam( 'blCalculateDelCostIfNotLoggedIn' ) && is_array( $aHomeCountry ) ) {
01352 $sDelivCountry = current( $aHomeCountry );
01353 }
01354 } else {
01355
01356 if ( $sCountryId = $myConfig->getGlobalParameter( 'delcountryid' ) ) {
01357 $sDelivCountry = $sCountryId;
01358 } elseif ( $sAddressId = oxConfig::getParameter( 'deladrid' ) ) {
01359
01360 $oDelAdress = oxNew( 'oxbase' );
01361 $oDelAdress->init( 'oxaddress' );
01362 if ( $oDelAdress->load( $sAddressId ) ) {
01363 $sDelivCountry = $oDelAdress->oxaddress__oxcountryid->value;
01364 }
01365 }
01366
01367
01368 if ( !$sDelivCountry ) {
01369 $sDelivCountry = $oUser->oxuser__oxcountryid->value;
01370 }
01371 }
01372
01373 return $sDelivCountry;
01374 }
01375
01381 public function deleteBasket()
01382 {
01383 $this->getSession()->delBasket();
01384
01385
01386 if ( !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' ) ) {
01387 $this->_deleteSavedBasket();
01388 }
01389 }
01390
01398 public function setPayment( $sPaymentId = null )
01399 {
01400 $this->_sPaymentId = $sPaymentId;
01401 }
01402
01408 public function getPaymentId()
01409 {
01410 if ( !$this->_sPaymentId ) {
01411 $this->_sPaymentId = oxConfig::getParameter( 'paymentid' );
01412 }
01413 return $this->_sPaymentId;
01414 }
01415
01423 public function setShipping( $sShippingSetId = null )
01424 {
01425 $this->_sShippingSetId = $sShippingSetId;
01426 }
01427
01433 public function getShippingId()
01434 {
01435 if ( !$this->_sShippingSetId ) {
01436 $this->_sShippingSetId = oxConfig::getParameter( 'sShipSet' );
01437 }
01438
01439
01440 if ( !$this->_sShippingSetId && $this->getPaymentId() != 'oxempty' ) {
01441 $this->_sShippingSetId = 'oxidstandard';
01442 }
01443
01444 return $this->_sShippingSetId;
01445 }
01446
01452 public function getBasketArticles()
01453 {
01454 $aBasketArticles = array();
01455 foreach ( $this->_aBasketContents as $sItemKey => $oBasketItem ) {
01456
01457 $oProduct = $oBasketItem->getArticle();
01458
01459 if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) ) {
01460
01461 $aSelList = $oBasketItem->getSelList();
01462 if ( is_array( $aSelList ) && ( $aSelectlist = $oProduct->getSelectLists( $sItemKey ) ) ) {
01463 reset( $aSelList );
01464 while ( list( $conkey, $iSel ) = each( $aSelList ) ) {
01465 $aSelectlist[$conkey][$iSel]->selected = 1;
01466 }
01467 $oProduct->setSelectlist( $aSelectlist );
01468 }
01469 }
01470
01471 $aBasketArticles[$sItemKey] = $oProduct;
01472 }
01473 return $aBasketArticles;
01474 }
01475
01481 public function getDiscountProductsPrice()
01482 {
01483 return $this->_oDiscountProductsPriceList;
01484 }
01485
01491 public function getProductsPrice()
01492 {
01493 if ( is_null($this->_oProductsPriceList) ) {
01494 $this->_oProductsPriceList = oxNew( 'oxPriceList' );
01495 }
01496
01497 return $this->_oProductsPriceList;
01498 }
01499
01505 public function getPrice()
01506 {
01507 if ( is_null($this->_oPrice) ) {
01508 $this->_oPrice = oxNew( 'oxprice' );
01509 }
01510
01511 return $this->_oPrice;
01512 }
01513
01520 public function getOrderId()
01521 {
01522 return $this->_sOrderId;
01523 }
01524
01532 public function setOrderId( $sId )
01533 {
01534 $this->_sOrderId = $sId;
01535 }
01536
01545 public function getCosts( $sId = null )
01546 {
01547
01548 if ( $sId ) {
01549 return isset( $this->_aCosts[$sId] )?$this->_aCosts[$sId]:null;
01550 }
01551 return $this->_aCosts;
01552 }
01553
01559 public function getVouchers()
01560 {
01561 return $this->_aVouchers;
01562 }
01563
01569 public function getProductsCount()
01570 {
01571 return $this->_iProductsCnt;
01572 }
01573
01579 public function getItemsCount()
01580 {
01581 return $this->_dItemsCnt;
01582 }
01583
01589 public function getWeight()
01590 {
01591 return $this->_dWeight;
01592 }
01593
01599 public function getContents()
01600 {
01601 return $this->_aBasketContents;
01602 }
01603
01609 public function getProductVats()
01610 {
01611 if ( !$this->_oNotDiscountedProductsPriceList ) {
01612 return array();
01613 }
01614
01615 $aVats = array();
01616
01617 $aAllVats = $this->_oNotDiscountedProductsPriceList->getVatInfo();
01618
01619 $oUtils = oxUtils::getInstance();
01620 foreach ( $this->_aDiscountedVats as $sKey => $dVat ) {
01621
01622 if ( array_key_exists ($sKey, $aAllVats) ) {
01623 $aAllVats[$sKey] += $oUtils->fRound( $dVat, $this->_oCurrency);
01624 } else {
01625 $aAllVats[$sKey] = $dVat;
01626 }
01627 }
01628
01629 foreach ( $aAllVats as $sKey => $dVat ) {
01630 $aVats[$sKey] = oxLang::getInstance()->formatCurrency( $dVat, $this->getBasketCurrency() );
01631 }
01632
01633 return $aVats;
01634 }
01635
01641 public function getDiscountedNettoPrice()
01642 {
01643 if ( $this->_oNotDiscountedProductsPriceList ) {
01644 return $this->_dDiscountedProductNettoPrice + $this->_oNotDiscountedProductsPriceList->getNettoSum();
01645 }
01646 return false;
01647 }
01648
01656 public function setCardMessage( $sMessage )
01657 {
01658 $this->_sCardMessage = $sMessage;
01659 }
01660
01666 public function getCardMessage()
01667 {
01668 return $this->_sCardMessage;
01669 }
01670
01678 public function setCardId( $sCardId )
01679 {
01680 $this->_sCardId = $sCardId;
01681 }
01682
01688 public function getCardId()
01689 {
01690 return $this->_sCardId;
01691 }
01692
01698 public function getCard()
01699 {
01700 $oCard = null;
01701 if ( $sCardId = $this->getCardId() ) {
01702 $oCard = oxNew( 'oxwrapping' );
01703 $oCard->load( $sCardId );
01704 }
01705 return $oCard;
01706 }
01707
01713 public function getTotalDiscount()
01714 {
01715 return $this->_oTotalDiscount;
01716 }
01717
01723 public function getDiscounts()
01724 {
01725 if ( $this->getTotalDiscount() && $this->getTotalDiscount()->getBruttoPrice() == 0 && count($this->_aItemDiscounts) == 0) {
01726 return null;
01727 }
01728
01729 return array_merge($this->_aItemDiscounts, $this->_aDiscounts);
01730 }
01731
01737 public function getVoucherDiscount()
01738 {
01739 return $this->_oVoucherDiscount;
01740 }
01741
01749 public function setBasketCurrency( $oCurrency )
01750 {
01751 $this->_oCurrency = $oCurrency;
01752 }
01753
01759 public function getBasketCurrency()
01760 {
01761 if ( $this->_oCurrency === null ) {
01762 $this->_oCurrency = $this->getConfig()->getActShopCurrencyObject();
01763 }
01764
01765 return $this->_oCurrency;
01766 }
01767
01775 public function setSkipVouchersChecking( $blSkipChecking = null )
01776 {
01777 $this->_blSkipVouchersAvailabilityChecking = $blSkipChecking;
01778 }
01779
01785 public function hasSkipedDiscount()
01786 {
01787 return $this->_blSkipDiscounts;
01788 }
01789
01797 public function setSkipDiscounts( $blSkip )
01798 {
01799 $this->_blSkipDiscounts = $blSkip;
01800 }
01801
01807 public function getProductsNetPrice()
01808 {
01809 return oxLang::getInstance()->formatCurrency( $this->getDiscountedNettoPrice(), $this->getBasketCurrency() );
01810 }
01811
01817 public function getFProductsPrice()
01818 {
01819 if ( $this->_oProductsPriceList ) {
01820 return oxLang::getInstance()->formatCurrency( $this->_oProductsPriceList->getBruttoSum(), $this->getBasketCurrency() );
01821 }
01822 return null;
01823 }
01824
01830 public function getDelCostVatPercent()
01831 {
01832 return $this->getCosts( 'oxdelivery' )->getVat();
01833 }
01834
01840 public function getDelCostVat()
01841 {
01842 $dDelVAT = $this->getCosts( 'oxdelivery' )->getVatValue();
01843 if ( $dDelVAT > 0 ) {
01844 return oxLang::getInstance()->formatCurrency( $dDelVAT, $this->getBasketCurrency() );
01845 }
01846 return false;
01847 }
01848
01854 public function getDelCostNet()
01855 {
01856 return oxLang::getInstance()->formatCurrency( $this->getCosts( 'oxdelivery' )->getNettoPrice(), $this->getBasketCurrency() );
01857 }
01858
01864 public function getPayCostVatPercent()
01865 {
01866 return $this->getCosts( 'oxpayment' )->getVat();
01867 }
01868
01874 public function getPayCostVat()
01875 {
01876 $dPayVAT = $this->getCosts( 'oxpayment' )->getVatValue();
01877 if ( $dPayVAT > 0 ) {
01878 return oxLang::getInstance()->formatCurrency( $dPayVAT, $this->getBasketCurrency() );
01879 }
01880 return false;
01881 }
01882
01888 public function getPayCostNet()
01889 {
01890 return oxLang::getInstance()->formatCurrency( $this->getCosts( 'oxpayment' )->getNettoPrice(), $this->getBasketCurrency() );
01891 }
01892
01898 public function getPaymentCosts()
01899 {
01900 return $this->getCosts( 'oxpayment' )->getBruttoPrice();
01901 }
01902
01908 public function getVoucherDiscValue()
01909 {
01910 if ( $this->getVoucherDiscount() ) {
01911 return $this->getVoucherDiscount()->getBruttoPrice();
01912 }
01913 return false;
01914 }
01915
01921 public function getWrappCostVatPercent()
01922 {
01923 return $this->getCosts( 'oxwrapping' )->getVat();
01924 }
01925
01931 public function getWrappCostVat()
01932 {
01933 $dWrappVAT = $this->getCosts( 'oxwrapping' )->getVatValue();
01934 if ( $dWrappVAT > 0 ) {
01935 return oxLang::getInstance()->formatCurrency( $dWrappVAT, $this->getBasketCurrency() );
01936 }
01937 return false;
01938
01939 }
01940
01946 public function getWrappCostNet()
01947 {
01948 $dWrappNet = $this->getCosts( 'oxwrapping' )->getNettoPrice();
01949 if ( $dWrappNet > 0 ) {
01950 return oxLang::getInstance()->formatCurrency( $dWrappNet, $this->getBasketCurrency() );
01951 }
01952 return false;
01953 }
01954
01960 public function getFPrice()
01961 {
01962 return oxLang::getInstance()->formatCurrency( $this->getPrice()->getBruttoPrice(), $this->getBasketCurrency() );
01963 }
01964
01970 public function getFDeliveryCosts()
01971 {
01972 if ( $oDeliveryCost = $this->getCosts( 'oxdelivery' ) ) {
01973 return oxLang::getInstance()->formatCurrency( $oDeliveryCost->getBruttoPrice(), $this->getBasketCurrency() );
01974 }
01975 return false;
01976 }
01977
01983 public function getDeliveryCosts()
01984 {
01985 if ( $oDeliveryCost = $this->getCosts( 'oxdelivery' ) ) {
01986 return $oDeliveryCost->getBruttoPrice();
01987 }
01988 return false;
01989 }
01990
01991 }