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 protected $_oDeliveryPrice = null;
00196
00202 public function isEnabled()
00203 {
00204 return !oxUtils::getInstance()->isSearchEngine();
00205 }
00206
00216 protected function _changeBasketItemKey($sOldKey, $sNewKey, $value = null)
00217 {
00218 reset($this->_aBasketContents);
00219 $iOldKeyPlace = 0;
00220 while (key($this->_aBasketContents) != $sOldKey && next($this->_aBasketContents)) {
00221 ++$iOldKeyPlace;
00222 }
00223 $aNewCopy = array_merge(
00224 array_slice($this->_aBasketContents, 0, $iOldKeyPlace, true),
00225 array($sNewKey => $value),
00226 array_slice($this->_aBasketContents, $iOldKeyPlace+1, count($this->_aBasketContents)-$iOldKeyPlace, true)
00227 );
00228 $this->_aBasketContents = $aNewCopy;
00229 }
00230
00246 public function addToBasket( $sProductID, $dAmount, $aSel = null, $aPersParam = null, $blOverride = false, $blBundle = false, $sOldBasketItemId = null )
00247 {
00248
00249 if ( !$this->isEnabled() )
00250 return null;
00251
00252
00253
00254 $sItemId = $this->getItemKey( $sProductID, $aSel, $aPersParam, $blBundle );
00255 if ($sOldBasketItemId && strcmp($sOldBasketItemId, $sItemId)) {
00256 if (isset( $this->_aBasketContents[$sItemId] )) {
00257
00258 unset( $this->_aBasketContents[$sOldBasketItemId] );
00259 } else {
00260
00261 $this->_changeBasketItemKey($sOldBasketItemId, $sItemId);
00262 }
00263 }
00264
00265
00266 $blRemoveItem = false;
00267
00268
00269 $oEx = null;
00270
00271 if ( isset( $this->_aBasketContents[$sItemId] ) ) {
00272
00273
00274 try {
00275 $this->_aBasketContents[$sItemId]->setAmount( $dAmount, $blOverride );
00276 } catch( oxOutOfStockException $oEx ) {
00277
00278 }
00279
00280 } else {
00281
00282 $oBasketItem = oxNew( 'oxbasketitem' );
00283 try {
00284 $oBasketItem->init( $sProductID, $dAmount, $aSel, $aPersParam, $blBundle );
00285 } catch( oxNoArticleException $oEx ) {
00286
00287
00288 $blRemoveItem = true;
00289
00290 } catch( oxOutOfStockException $oEx ){
00291
00292 }
00293
00294 $this->_aBasketContents[$sItemId] = $oBasketItem;
00295 }
00296
00297
00298 if ( $this->_aBasketContents[$sItemId]->getAmount() == 0 || $blRemoveItem ) {
00299 $this->removeItem( $sItemId );
00300 $blRemoveItem = true;
00301 } elseif ( $blBundle ) {
00302 $this->_aBasketContents[$sItemId]->setBundle( true );
00303 }
00304
00305
00306 $this->onUpdate();
00307
00308
00309 if ( !$blBundle && !$blRemoveItem ) {
00310 $this->_addItemToSavedBasket( $this->_aBasketContents[$sItemId]->getProductId(), $dAmount, $aSel, $blOverride );
00311 }
00312
00313 if ( $oEx ) {
00314 throw $oEx;
00315 }
00316 return $this->_aBasketContents[$sItemId];
00317 }
00318
00319
00320
00333 public function getItemKey( $sProductId, $aSel = null, $aPersParam = null, $blBundle = false, $sAdditionalParam = '' )
00334 {
00335 $aSel = ( $aSel != null) ? $aSel : array (0=>'0');
00336
00337 $sItemKey = md5( $sProductId.'|'.serialize( $aSel ).'|'.serialize( $aPersParam ).'|'.( int ) $blBundle . '|' . serialize( $sAdditionalParam ) );
00338
00339 return $sItemKey;
00340 }
00341
00342
00350 public function removeItem( $sItemKey )
00351 {
00352 unset( $this->_aBasketContents[$sItemKey] );
00353 }
00354
00360 protected function _clearBundles()
00361 {
00362 reset( $this->_aBasketContents );
00363 while ( list( $sItemKey, $oBasketItem ) = each( $this->_aBasketContents ) )
00364 if ( $oBasketItem->isBundle() ) {
00365 $this->removeItem( $sItemKey );
00366 }
00367 }
00368
00376 protected function _getArticleBundles( $oBasketItem )
00377 {
00378 $aBundles = array();
00379
00380 if ( $oBasketItem->isBundle() ) {
00381 return $aBundles;
00382 }
00383
00384 $oArticle = $oBasketItem->getArticle();
00385 if ( $oArticle && $oArticle->oxarticles__oxbundleid->value ) {
00386 $aBundles[$oArticle->oxarticles__oxbundleid->value] = 1;
00387 }
00388
00389 return $aBundles;
00390 }
00391
00399 protected function _getItemBundles( $oBasketItem )
00400 {
00401 if ( $oBasketItem->isBundle() ) {
00402 return array();
00403 }
00404
00405 $aBundles = array();
00406
00407
00408 if ( $oArticle = $oBasketItem->getArticle() ) {
00409 $aDiscounts = oxDiscountList::getInstance()->getBasketItemBundleDiscounts( $oArticle, $this, $this->getBasketUser() );
00410
00411 foreach ( $aDiscounts as $oDiscount ) {
00412
00413
00414 if ( !isset( $aBundles[$oDiscount->oxdiscount__oxitmartid->value] ) ) {
00415 $aBundles[$oDiscount->oxdiscount__oxitmartid->value] = 0;
00416 }
00417
00418 $aBundles[$oDiscount->oxdiscount__oxitmartid->value] += $oDiscount->getBundleAmount( $oBasketItem->getAmount() );
00419
00420 }
00421 }
00422
00423 return $aBundles;
00424 }
00425
00431 protected function _getBasketBundles()
00432 {
00433 $aBundles = array();
00434 $aDiscounts = oxDiscountList::getInstance()->getBasketBundleDiscounts( $this, $this->getBasketUser() );
00435
00436
00437 $dAmount = 0;
00438 foreach ( $this->_aBasketContents as $oBasketItem ) {
00439 if ( !( $oBasketItem->isBundle() || $oBasketItem->isDiscountArticle() ) ) {
00440 $dAmount += $oBasketItem->getAmount();
00441 }
00442 }
00443
00444 foreach ( $aDiscounts as $oDiscount ) {
00445 if ($oDiscount->oxdiscount__oxitmartid->value) {
00446 if ( !isset( $aBundles[$oDiscount->oxdiscount__oxitmartid->value] ) ) {
00447 $aBundles[$oDiscount->oxdiscount__oxitmartid->value] = 0;
00448 }
00449
00450 $aBundles[$oDiscount->oxdiscount__oxitmartid->value] += $oDiscount->getBundleAmount( $dAmount );
00451
00452 }
00453 }
00454
00455 return $aBundles;
00456 }
00457
00464 protected function _addBundles()
00465 {
00466
00467 foreach ( $this->_aBasketContents as $key => $oBasketItem ) {
00468
00469
00470 if ( !$oBasketItem->isDiscountArticle() && !$oBasketItem->isBundle() ) {
00471 $aBundles = $this->_getItemBundles( $oBasketItem );
00472 } else {
00473 continue;
00474 }
00475
00476 $this->_addBundlesToBasket( $aBundles );
00477
00478
00479 $aBundles = $this->_getArticleBundles( $oBasketItem );
00480
00481
00482 $this->_addBundlesToBasket( $aBundles );
00483 }
00484
00485
00486 if ( $aBundles = $this->_getBasketBundles() ) {
00487 $this->_addBundlesToBasket( $aBundles );
00488 }
00489
00490 }
00491
00499 protected function _addBundlesToBasket( $aBundles )
00500 {
00501 foreach ( $aBundles as $sBundleId => $dAmount ) {
00502 try {
00503 if ( $oBundleItem = $this->addToBasket( $sBundleId, $dAmount, null, null, true, true ) ) {
00504 $oBundleItem->setAsDiscountArticle( true );
00505 }
00506 } catch(oxArticleException $oEx) {
00507
00508 }
00509 }
00510
00511 }
00512
00518 protected function _calcItemsPrice()
00519 {
00520
00521 $this->setSkipDiscounts( false );
00522 $this->_iProductsCnt = 0;
00523 $this->_dItemsCnt = 0;
00524 $this->_dWeight = 0;
00525
00526
00527 $this->_aItemDiscounts = array();
00528
00529 $this->_oProductsPriceList = oxNew( 'oxpricelist' );
00530 $this->_oDiscountProductsPriceList = oxNew( 'oxpricelist' );
00531 $this->_oNotDiscountedProductsPriceList = oxNew( 'oxpricelist' );
00532
00533 foreach ( $this->_aBasketContents as $oBasketItem ) {
00534 $this->_iProductsCnt++;
00535 $this->_dItemsCnt += $oBasketItem->getAmount();
00536 $this->_dWeight += $oBasketItem->getWeight();
00537
00538 if ( !$oBasketItem->isDiscountArticle() && ( $oArticle = $oBasketItem->getArticle() ) ) {
00539 $oBasketPrice = $oArticle->getBasketPrice( $oBasketItem->getAmount(), $oBasketItem->getSelList(), $this );
00540 $oBasketItem->setPrice( $oBasketPrice );
00541
00542 $this->_oProductsPriceList->addToPriceList( $oBasketItem->getPrice() );
00543
00544 $oBasketPrice->setBruttoPriceMode();
00545 if ( !$oArticle->skipDiscounts() ) {
00546
00547 $aItemDiscounts = $oArticle->applyBasketDiscounts( $oBasketPrice, oxDiscountList::getInstance()->getBasketItemDiscounts( $oArticle, $this, $this->getBasketUser() ), $oBasketItem->getAmount() );
00548 if ( is_array($this->_aItemDiscounts) && is_array($aItemDiscounts) ) {
00549 $this->_aItemDiscounts = $this->_mergeDiscounts( $this->_aItemDiscounts, $aItemDiscounts);
00550 }
00551 } else {
00552 $oBasketItem->setSkipDiscounts( true );
00553 $this->setSkipDiscounts( true );
00554 }
00555 $oBasketPrice->multiply( $oBasketItem->getAmount() );
00556
00557
00558 if ( !$oArticle->skipDiscounts() ) {
00559 $this->_oDiscountProductsPriceList->addToPriceList( $oBasketPrice );
00560 } else {
00561 $this->_oNotDiscountedProductsPriceList->addToPriceList( $oBasketPrice );
00562 $oBasketItem->setSkipDiscounts( true );
00563 $this->setSkipDiscounts( true );
00564 }
00565 } elseif ( $oBasketItem->isBundle() ) {
00566
00567 $oPrice = oxNew( "oxprice");
00568 $oBasketItem->setPrice( $oPrice );
00569 }
00570 }
00571 }
00572
00582 protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
00583 {
00584 foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
00585
00586 if ( array_key_exists ($sKey, $aDiscounts) ) {
00587 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
00588 } else {
00589 $aDiscounts[$sKey] = $oDiscount;
00590 }
00591 }
00592 return $aDiscounts;
00593 }
00594
00600 protected function _calcDeliveryCost()
00601 {
00602 if ( $this->_oDeliveryPrice !== null ) {
00603 return $this->_oDeliveryPrice;
00604 }
00605 $myConfig = $this->getConfig();
00606 $oDeliveryPrice = oxNew( 'oxprice' );
00607 $oDeliveryPrice->setBruttoPriceMode();
00608
00609
00610 $oUser = $this->getBasketUser();
00611
00612 if ( !$oUser && !$myConfig->getConfigParam( 'blCalculateDelCostIfNotLoggedIn' ) ) {
00613 return $oDeliveryPrice;
00614 }
00615
00616
00617 $fDelVATPercent = 0;
00618 if ( $myConfig->getConfigParam( 'blCalcVATForDelivery' ) ) {
00619 $fDelVATPercent = $this->getMostUsedVatPercent();
00620 $oDeliveryPrice->setVat( $fDelVATPercent );
00621 }
00622
00623
00624 if ( $myConfig->getConfigParam('bl_perfLoadDelivery') ) {
00625 $aDeliveryList = oxDeliveryList::getInstance()->getDeliveryList( $this,
00626 $oUser,
00627 $this->_findDelivCountry(),
00628 oxConfig::getParameter( 'sShipSet' )
00629 );
00630
00631 if ( count( $aDeliveryList ) > 0 ) {
00632 foreach ( $aDeliveryList as $oDelivery ) {
00633
00634 if ( $myConfig->getConfigParam( 'iDebug' ) == 5 ) {
00635 echo( "DelCost : ".$oDelivery->oxdelivery__oxtitle->value."<br>" );
00636 }
00637 $oDeliveryPrice->addPrice( $oDelivery->getDeliveryPrice( $fDelVATPercent ) );
00638 }
00639 }
00640 }
00641
00642 return $oDeliveryPrice;
00643 }
00644
00650 public function getBasketUser()
00651 {
00652 if ( $this->_oUser == null ) {
00653 return $this->getUser();
00654 }
00655
00656 return $this->_oUser;
00657 }
00658
00666 public function setBasketUser( $oUser )
00667 {
00668 $this->_oUser = $oUser;
00669 }
00670
00671
00677 public function getMostUsedVatPercent()
00678 {
00679 return $this->_oProductsPriceList->getMostUsedVatPercent();
00680 }
00681
00682
00689 protected function _calcTotalPrice()
00690 {
00691
00692 $dprice = $this->_oProductsPriceList->getBruttoSum();
00693 $this->_oPrice->setPrice( $dprice );
00694
00695
00696 if ( $dprice ) {
00697
00698
00699 foreach ( $this->_aItemDiscounts as $oDiscount ) {
00700
00701
00702 if ( $oDiscount->sType == 'itm' ) {
00703 continue;
00704 }
00705 $this->_oPrice->subtract( $oDiscount->dDiscount );
00706 }
00707
00708
00709 $this->_oPrice->subtract( $this->_oTotalDiscount->getBruttoPrice() );
00710
00711
00712 $this->_oPrice->subtract( $this->_oVoucherDiscount->getBruttoPrice() );
00713 }
00714
00715
00716 if ( isset( $this->_aCosts['oxdelivery'] ) ) {
00717 $this->_oPrice->add( $this->_aCosts['oxdelivery']->getBruttoPrice() );
00718 }
00719
00720
00721 if ( isset( $this->_aCosts['oxwrapping'] ) ) {
00722 $this->_oPrice->add( $this->_aCosts['oxwrapping']->getBruttoPrice() );
00723 }
00724
00725
00726 if ( isset( $this->_aCosts['oxpayment'] ) ) {
00727 $this->_oPrice->add( $this->_aCosts['oxpayment']->getBruttoPrice() );
00728 }
00729
00730 }
00731
00737 protected function _calcVoucherDiscount()
00738 {
00739 $this->_oVoucherDiscount = oxNew( 'oxPrice' );
00740 $this->_oVoucherDiscount->setBruttoPriceMode();
00741
00742
00743
00744 $dPrice = $this->_oDiscountProductsPriceList->getBruttoSum() - $this->_oTotalDiscount->getBruttoPrice();
00745
00746
00747 if ( count( $this->_aVouchers ) ) {
00748 foreach ( $this->_aVouchers as $sVoucherId => $oStdVoucher ) {
00749 $oVoucher = oxNew( 'oxvoucher' );
00750 try {
00751 $oVoucher->load( $oStdVoucher->sVoucherId );
00752
00753 if ( !$this->_blSkipVouchersAvailabilityChecking ) {
00754 $oVoucher->checkBasketVoucherAvailability( $this->_aVouchers, $dPrice );
00755 $oVoucher->checkUserAvailability( $this->getBasketUser() );
00756 }
00757
00758
00759 $dVoucherdiscount = $oVoucher->getDiscountValue( $dPrice );
00760
00761
00762 $this->_oVoucherDiscount->add( $dVoucherdiscount );
00763
00764
00765 $oStdVoucher->fVoucherdiscount = oxLang::getInstance()->formatCurrency( $dVoucherdiscount, $this->getBasketCurrency() );
00766
00767
00768 $dPrice -= $dVoucherdiscount;
00769 } catch ( oxVoucherException $oEx ) {
00770
00771
00772 $oVoucher->unMarkAsReserved();
00773 unset( $this->_aVouchers[$sVoucherId] );
00774
00775
00776 oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00777 }
00778 }
00779 }
00780 }
00781
00782
00789 protected function _applyDiscounts()
00790 {
00791 $dBruttoPrice = $this->_oDiscountProductsPriceList->getBruttoSum();
00792 $this->_aDiscountedVats = $this->_oDiscountProductsPriceList->getVatInfo();
00793
00794
00795 $dDiscountedBruttoPrice = $dBruttoPrice - $this->_oTotalDiscount->getBruttoPrice() - $this->_oVoucherDiscount->getBruttoPrice();
00796
00797
00798 if ( $dBruttoPrice && ( $this->_oTotalDiscount->getBruttoPrice() || $this->_oVoucherDiscount->getBruttoPrice() )) {
00799 $dPercent = ( $dDiscountedBruttoPrice / $dBruttoPrice) * 100;
00800 foreach ( $this->_aDiscountedVats as $sKey => $dVat ) {
00801 $this->_aDiscountedVats[$sKey] = oxPrice::percent( $dVat, $dPercent);
00802 }
00803 }
00804
00805 $oUtils = oxUtils::getInstance();
00806 $dDiscVatSum = 0;
00807 foreach ( $this->_aDiscountedVats as $dVat ) {
00808 $dDiscVatSum += $oUtils->fRound( -$dVat, $this->_oCurrency);
00809 }
00810
00811 $this->_dDiscountedProductNettoPrice = $dDiscountedBruttoPrice + $dDiscVatSum;
00812 }
00813
00819 protected function _calcBasketDiscount()
00820 {
00821
00822 $this->_aDiscounts = array();
00823
00824
00825 $dOldprice = $this->_oDiscountProductsPriceList->getBruttoSum();
00826
00827
00828 $aDiscounts = oxDiscountList::getInstance()->getBasketDiscounts( $this, $this->getBasketUser() );
00829
00830 foreach ( $aDiscounts as $oDiscount ) {
00831
00832
00833 $oStdDiscount = $oDiscount->getSimpleDiscount();
00834
00835
00836 if ( $oDiscount->oxdiscount__oxaddsumtype->value == 'itm' ) {
00837 continue;
00838 }
00839
00840
00841 $oStdDiscount->dDiscount = $oDiscount->getAbsValue( $dOldprice );
00842
00843 $this->_aDiscounts[$oDiscount->getId()] = $oStdDiscount;
00844
00845
00846 $dOldprice = $dOldprice - $oStdDiscount->dDiscount;
00847 }
00848 }
00849
00855 protected function _calcBasketTotalDiscount()
00856 {
00857 $this->_oTotalDiscount = oxNew( 'oxPrice' );
00858 $this->_oTotalDiscount->setBruttoPriceMode();
00859
00860 if ( is_array($this->_aDiscounts) ) {
00861 foreach ( $this->_aDiscounts as $oDiscount ) {
00862
00863
00864 if ( $oDiscount->sType == 'itm' ) {
00865 continue;
00866 }
00867
00868
00869 $this->_oTotalDiscount->add( $oDiscount->dDiscount );
00870 }
00871 }
00872 }
00873
00883 protected function _calcBasketWrapping()
00884 {
00885 $myConfig = $this->getConfig();
00886 $oWrappingPrice = oxNew( 'oxPrice' );
00887 $oWrappingPrice->setBruttoPriceMode();
00888
00889
00890 if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
00891 $oWrappingPrice->setVat( $this->getMostUsedVatPercent() );
00892 }
00893
00894
00895 foreach ( $this->_aBasketContents as $oBasketItem ) {
00896
00897 if ( ( $oWrapping = $oBasketItem->getWrapping() ) ) {
00898 $oWrapPrice = $oWrapping->getWrappingPrice( $oBasketItem->getAmount() );
00899 $oWrappingPrice->add( $oWrapPrice->getBruttoPrice() );
00900 }
00901 }
00902
00903
00904 if ( ( $oCard = $this->getCard() ) ) {
00905 $oCardPrice = $oCard->getWrappingPrice();
00906 $oWrappingPrice->add( $oCardPrice->getBruttoPrice() );
00907 }
00908
00909 return $oWrappingPrice;
00910 }
00911
00918 protected function _calcPaymentCost()
00919 {
00920
00921 $oPaymentPrice = oxNew( 'oxPrice' );
00922 $oPaymentPrice->setBruttoPriceMode();
00923
00924
00925 if ( ( $this->_sPaymentId = $this->getPaymentId() ) ) {
00926
00927 $oPayment = oxNew( 'oxpayment' );
00928 $oPayment->load( $this->_sPaymentId );
00929
00930 $oPaymentPrice = $oPayment->getPaymentPrice( $this );
00931 }
00932
00933 return $oPaymentPrice;
00934 }
00935
00944 public function setCost( $sCostName, $oPrice = null )
00945 {
00946 $this->_aCosts[$sCostName] = $oPrice;
00947 }
00948
00957 public function calculateBasket( $blForceUpdate = false )
00958 {
00959 if ( !$this->isEnabled() )
00960 return;
00961
00962 if ( !$this->_blUpdateNeeded && !$blForceUpdate )
00963 return;
00964
00965 $this->_aCosts = array();
00966
00967 $this->_oPrice = oxNew( 'oxprice' );
00968 $this->_oPrice->setBruttoPriceMode();
00969
00970
00971 if ( !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' ) ) {
00972 $this->_mergeSavedBasket();
00973 }
00974
00975
00976 $this->_clearBundles();
00977
00978
00979 $this->_addBundles();
00980
00981
00982 $this->_calcItemsPrice();
00983
00984
00985 $this->_calcBasketDiscount();
00986
00987
00988 $this->_calcBasketTotalDiscount();
00989
00990
00991 $this->_calcVoucherDiscount();
00992
00993
00994 $this->_applyDiscounts();
00995
00996
00997
00998 $this->setCost( 'oxdelivery', $this->_calcDeliveryCost() );
00999
01000
01001 $this->setCost( 'oxwrapping', $this->_calcBasketWrapping() );
01002
01003
01004 $this->setCost( 'oxpayment', $this->_calcPaymentCost() );
01005
01006
01007 $this->_calcTotalPrice();
01008
01009
01010 $this->_setDeprecatedValues();
01011
01012
01013 $this->afterUpdate();
01014 }
01015
01021 public function onUpdate()
01022 {
01023 $this->_blUpdateNeeded = true;
01024 }
01025
01031 public function afterUpdate()
01032 {
01033 $this->_blUpdateNeeded = false;
01034 }
01035
01043 public function getBasketSummary()
01044 {
01045 if ( $this->_blUpdateNeeded || $this->_aBasketSummary === null ) {
01046 $this->_aBasketSummary = new Oxstdclass();
01047 $this->_aBasketSummary->aArticles = array();
01048 $this->_aBasketSummary->aCategories = array();
01049 $this->_aBasketSummary->iArticleCount = 0;
01050 $this->_aBasketSummary->dArticlePrice = 0;
01051 }
01052
01053 if ( !$this->isEnabled() ) {
01054 return $this->_aBasketSummary;
01055 }
01056
01057 $myConfig = $this->getConfig();
01058 foreach ( $this->_aBasketContents as $oBasketItem ) {
01059 if ( !$oBasketItem->isBundle() ) {
01060 if ( ( $oArticle = $oBasketItem->getArticle() ) ) {
01061
01062 $aCatIds = $oArticle->getCategoryIds();
01063 $dPrice = $oArticle->getPrice()->getBruttoPrice();
01064
01065 foreach ( $aCatIds as $sCatId ) {
01066 if ( !isset( $this->_aBasketSummary->aCategories[$sCatId] ) ) {
01067 $this->_aBasketSummary->aCategories[$sCatId] = new Oxstdclass();
01068 }
01069
01070 $this->_aBasketSummary->aCategories[$sCatId]->dPrice += $dPrice * $oBasketItem->getAmount();
01071 $this->_aBasketSummary->aCategories[$sCatId]->dAmount += $oBasketItem->getAmount();
01072 $this->_aBasketSummary->aCategories[$sCatId]->iCount++;
01073 }
01074
01075
01076 if ( $oArticle->oxarticles__oxparentid->value && $myConfig->getConfigParam( 'blVariantParentBuyable' ) ) {
01077 if ( !isset( $this->_aBasketSummary->aArticles[$oArticle->oxarticles__oxparentid->value] ) ) {
01078 $this->_aBasketSummary->aArticles[$oArticle->oxarticles__oxparentid->value] = 0;
01079 }
01080 $this->_aBasketSummary->aArticles[$oArticle->oxarticles__oxparentid->value] += $oBasketItem->getAmount();
01081 }
01082
01083 if ( !isset( $this->_aBasketSummary->aArticles[$oBasketItem->getProductId()] ) ) {
01084 $this->_aBasketSummary->aArticles[$oBasketItem->getProductId()] = 0;
01085 }
01086
01087 $this->_aBasketSummary->aArticles[$oBasketItem->getProductId()] += $oBasketItem->getAmount();
01088 $this->_aBasketSummary->iArticleCount += $oBasketItem->getAmount();
01089 $this->_aBasketSummary->dArticlePrice += $dPrice * $oBasketItem->getAmount();
01090 }
01091 }
01092 }
01093 return $this->_aBasketSummary;
01094 }
01095
01107 public function addVoucher( $sVoucherId )
01108 {
01109
01110
01111 $dPrice = 0;
01112 if ( $this->_oDiscountProductsPriceList ) {
01113 $dPrice = $this->_oDiscountProductsPriceList->getBruttoSum();
01114 }
01115
01116 try {
01117
01118 $oVoucher = oxNew( 'oxvoucher' );
01119
01120 if ( !$this->_blSkipVouchersAvailabilityChecking ) {
01121 $oVoucher->getVoucherByNr( $sVoucherId, $this->_aVouchers, true );
01122 $oVoucher->checkVoucherAvailability( $this->_aVouchers, $dPrice );
01123 $oVoucher->checkUserAvailability( $this->getBasketUser() );
01124 $oVoucher->markAsReserved();
01125 } else {
01126 $oVoucher->load( $sVoucherId );
01127 }
01128
01129
01130 $this->_aVouchers[$oVoucher->oxvouchers__oxid->value] = $oVoucher->getSimpleVoucher();
01131 } catch ( oxVoucherException $oEx ) {
01132
01133
01134 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
01135 }
01136
01137 $this->onUpdate();
01138 }
01139
01147 public function removeVoucher( $sVoucherId )
01148 {
01149
01150 if ( isset( $this->_aVouchers[$sVoucherId] ) ) {
01151
01152 $oVoucher = oxNew( 'oxvoucher' );
01153 $oVoucher->load( $sVoucherId );
01154
01155 $oVoucher->unMarkAsReserved();
01156
01157
01158 unset( $this->_aVouchers[$sVoucherId] );
01159 $this->onUpdate();
01160 }
01161
01162 }
01163
01169 public function resetUserInfo()
01170 {
01171 $this->setPayment( null );
01172 $this->setShipping( null );
01173 }
01174
01182 protected function _setDeprecatedValues()
01183 {
01184
01185 $this->dproductsprice = $this->_oProductsPriceList->getBruttoSum();
01186 $this->dproductsnetprice = $this->getDiscountedNettoPrice();
01187
01188
01189 $this->dVAT = array_sum( $this->_oProductsPriceList->getVatInfo() );
01190
01191
01192 $this->fproductsprice = $this->getFProductsPrice();
01193 $this->fproductsnetprice = $this->getProductsNetPrice();
01194 $this->fVAT = oxLang::getInstance()->formatCurrency( $this->dVAT, $this->getBasketCurrency());
01195
01196
01197 if ( $oDeliveryCost = $this->getCosts( 'oxdelivery' ) ) {
01198
01199 $this->ddeliverycost = $oDeliveryCost->getBruttoPrice();
01200 $this->ddeliverynetcost = $oDeliveryCost->getNettoPrice();
01201 $this->dDelVAT = $oDeliveryCost->getVatValue();
01202 $this->fDelVATPercent = $oDeliveryCost->getVat() / 100;
01203
01204
01205 $this->fdeliverycost = oxLang::getInstance()->formatCurrency( $this->ddeliverycost, $this->getBasketCurrency() );
01206 $this->fdeliverynetcost = oxLang::getInstance()->formatCurrency( $this->ddeliverynetcost, $this->getBasketCurrency() );
01207 $this->fDelVAT = $this->getDelCostVat();
01208 }
01209
01210
01211
01212 if ( $oWrappingCost = $this->getCosts( 'oxwrapping' ) ) {
01213
01214 $this->dWrappingPrice = $oWrappingCost->getBruttoPrice();
01215 $this->dWrappingNetto = $oWrappingCost->getNettoPrice();
01216 $this->dWrappingVAT = $oWrappingCost->getVatValue();
01217
01218
01219 $this->fWrappingPrice = oxLang::getInstance()->formatCurrency( $this->dWrappingPrice, $this->getBasketCurrency() );
01220 $this->fWrappingNetto = $this->getWrappCostNet();
01221 $this->fWrappingVAT = $this->getWrappCostVat();
01222 $this->fWrappingVATPercent = $this->getWrappCostVatPercent();
01223 }
01224
01225
01226
01227 if ( $oPaymentCost = $this->getCosts( 'oxpayment' ) ) {
01228
01229 $this->dAddPaymentSum = $this->getPaymentCosts();
01230 $this->dAddPaymentSumVAT = $oPaymentCost->getVatValue();
01231
01232
01233 $this->fAddPaymentSum = oxLang::getInstance()->formatCurrency( $this->dAddPaymentSum, $this->getBasketCurrency() );
01234 $this->fAddPaymentSumVAT = $this->getPayCostVat();
01235 $this->fAddPaymentSumVATPercent = $this->getPayCostVatPercent();
01236 $this->fAddPaymentNetSum = $this->getPayCostNet();
01237 }
01238
01239
01240
01241 $this->dprice = $this->_oPrice->getBruttoPrice();
01242 $this->fprice = oxLang::getInstance()->formatCurrency( $this->dprice, $this->getBasketCurrency() );
01243
01244
01245 $this->iCntProducts = $this->getProductsCount();
01246 $this->dCntItems = $this->getItemsCount();
01247 $this->aVATs = $this->getProductVats();
01248 $this->aBasketContents = $this->getContents();
01249
01250
01251 $this->giftmessage = $this->getCardMessage();
01252 $this->chosencard = $this->getCardId();
01253
01254 $this->oCard = $this->getCard();
01255
01256
01257
01258 $this->aDiscounts = $this->getDiscounts();
01259 if ( count($this->aDiscounts) > 0 ) {
01260 foreach ($this->aDiscounts as $oDiscount) {
01261 $oDiscount->fDiscount = oxLang::getInstance()->formatCurrency( $oDiscount->dDiscount, $this->getBasketCurrency() );
01262 }
01263 }
01264 $this->dDiscount = $this->getTotalDiscount()->getBruttoPrice();
01265
01266
01267 $this->aVouchers = $this->getVouchers();
01268 $this->dVoucherDiscount = $this->getVoucherDiscValue();
01269 $this->fVoucherDiscount = oxLang::getInstance()->formatCurrency( $this->dVoucherDiscount, $this->getBasketCurrency() );
01270 $this->dSkippedDiscount = $this->hasSkipedDiscount();
01271
01272 }
01273
01274
01281 protected function _mergeSavedBasket()
01282 {
01283 if ( $this->_blBasketMerged ) {
01284 return;
01285 }
01286
01287 $oUser = $this->getBasketUser();
01288 if ( !$oUser ) {
01289 $this->_blBasketMerged = false;
01290 return;
01291 }
01292
01293 $oBasket = $oUser->getBasket( 'savedbasket' );
01294
01295
01296 $aSavedItems = $oBasket->getItems();
01297 foreach ( $aSavedItems as $oItem ) {
01298 try {
01299 $this->addToBasket( $oItem->getId(), $oItem->dAmount, $oItem->aSelList, null, true );
01300 } catch( oxArticleException $oEx ) {
01301
01302 }
01303 }
01304
01305
01306 foreach ( $this->_aBasketContents as $oBasketItem ) {
01307 $oBasket->addItemToBasket( $oBasketItem->getProductId(), $oBasketItem->getAmount(), $oBasketItem->getSelList(), true );
01308 }
01309
01310
01311 $this->_blBasketMerged = true;
01312 }
01313
01324 protected function _addItemToSavedBasket( $sProductId , $dAmount, $aSel, $blOverride = false )
01325 {
01326
01327 if ( $oUser = $this->getBasketUser() ) {
01328 $oUser->getBasket( 'savedbasket' )->addItemToBasket( $sProductId, $dAmount, $aSel, $blOverride );
01329 }
01330 }
01331
01339 protected function _deleteSavedBasket()
01340 {
01341
01342 if ( $oUser = $this->getBasketUser() ) {
01343 $oUser->getBasket( 'savedbasket' )->delete();
01344 }
01345 }
01346
01352 protected function _findDelivCountry()
01353 {
01354 $myConfig = $this->getConfig();
01355 $oUser = $this->getBasketUser();
01356
01357 $sDelivCountry = null;
01358 if ( !$oUser ) {
01359
01360 $aHomeCountry = $myConfig->getConfigParam( 'aHomeCountry' );
01361 if ( $myConfig->getConfigParam( 'blCalculateDelCostIfNotLoggedIn' ) && is_array( $aHomeCountry ) ) {
01362 $sDelivCountry = current( $aHomeCountry );
01363 }
01364 } else {
01365
01366 if ( $sCountryId = $myConfig->getGlobalParameter( 'delcountryid' ) ) {
01367 $sDelivCountry = $sCountryId;
01368 } elseif ( $sAddressId = oxConfig::getParameter( 'deladrid' ) ) {
01369
01370 $oDelAdress = oxNew( 'oxbase' );
01371 $oDelAdress->init( 'oxaddress' );
01372 if ( $oDelAdress->load( $sAddressId ) ) {
01373 $sDelivCountry = $oDelAdress->oxaddress__oxcountryid->value;
01374 }
01375 }
01376
01377
01378 if ( !$sDelivCountry ) {
01379 $sDelivCountry = $oUser->oxuser__oxcountryid->value;
01380 }
01381 }
01382
01383 return $sDelivCountry;
01384 }
01385
01391 public function deleteBasket()
01392 {
01393 $this->getSession()->delBasket();
01394
01395
01396 if ( !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' ) ) {
01397 $this->_deleteSavedBasket();
01398 }
01399 }
01400
01408 public function setPayment( $sPaymentId = null )
01409 {
01410 $this->_sPaymentId = $sPaymentId;
01411 }
01412
01418 public function getPaymentId()
01419 {
01420 if ( !$this->_sPaymentId ) {
01421 $this->_sPaymentId = oxConfig::getParameter( 'paymentid' );
01422 }
01423 return $this->_sPaymentId;
01424 }
01425
01433 public function setShipping( $sShippingSetId = null )
01434 {
01435 $this->_sShippingSetId = $sShippingSetId;
01436 oxSession::setVar( 'sShipSet', $sShippingSetId );
01437 }
01438
01446 public function setDeliveryPrice( $oShippingPrice = null )
01447 {
01448 $this->_oDeliveryPrice = $oShippingPrice;
01449 }
01450
01456 public function getShippingId()
01457 {
01458 if ( !$this->_sShippingSetId ) {
01459 $this->_sShippingSetId = oxConfig::getParameter( 'sShipSet' );
01460 }
01461
01462
01463 if ( !$this->_sShippingSetId && $this->getPaymentId() != 'oxempty' ) {
01464 $this->_sShippingSetId = 'oxidstandard';
01465 }
01466
01467 return $this->_sShippingSetId;
01468 }
01469
01475 public function getBasketArticles()
01476 {
01477 $aBasketArticles = array();
01478 foreach ( $this->_aBasketContents as $sItemKey => $oBasketItem ) {
01479
01480 $oProduct = $oBasketItem->getArticle();
01481
01482 if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) ) {
01483
01484 $aSelList = $oBasketItem->getSelList();
01485 if ( is_array( $aSelList ) && ( $aSelectlist = $oProduct->getSelectLists( $sItemKey ) ) ) {
01486 reset( $aSelList );
01487 while ( list( $conkey, $iSel ) = each( $aSelList ) ) {
01488 $aSelectlist[$conkey][$iSel]->selected = 1;
01489 }
01490 $oProduct->setSelectlist( $aSelectlist );
01491 }
01492 }
01493
01494 $aBasketArticles[$sItemKey] = $oProduct;
01495 }
01496 return $aBasketArticles;
01497 }
01498
01504 public function getDiscountProductsPrice()
01505 {
01506 return $this->_oDiscountProductsPriceList;
01507 }
01508
01514 public function getProductsPrice()
01515 {
01516 if ( is_null($this->_oProductsPriceList) ) {
01517 $this->_oProductsPriceList = oxNew( 'oxPriceList' );
01518 }
01519
01520 return $this->_oProductsPriceList;
01521 }
01522
01528 public function getPrice()
01529 {
01530 if ( is_null($this->_oPrice) ) {
01531 $this->_oPrice = oxNew( 'oxprice' );
01532 }
01533
01534 return $this->_oPrice;
01535 }
01536
01543 public function getOrderId()
01544 {
01545 return $this->_sOrderId;
01546 }
01547
01555 public function setOrderId( $sId )
01556 {
01557 $this->_sOrderId = $sId;
01558 }
01559
01568 public function getCosts( $sId = null )
01569 {
01570
01571 if ( $sId ) {
01572 return isset( $this->_aCosts[$sId] )?$this->_aCosts[$sId]:null;
01573 }
01574 return $this->_aCosts;
01575 }
01576
01582 public function getVouchers()
01583 {
01584 return $this->_aVouchers;
01585 }
01586
01592 public function getProductsCount()
01593 {
01594 return $this->_iProductsCnt;
01595 }
01596
01602 public function getItemsCount()
01603 {
01604 return $this->_dItemsCnt;
01605 }
01606
01612 public function getWeight()
01613 {
01614 return $this->_dWeight;
01615 }
01616
01622 public function getContents()
01623 {
01624 return $this->_aBasketContents;
01625 }
01626
01632 public function getProductVats()
01633 {
01634 if ( !$this->_oNotDiscountedProductsPriceList ) {
01635 return array();
01636 }
01637
01638 $aVats = array();
01639
01640 $aAllVats = $this->_oNotDiscountedProductsPriceList->getVatInfo();
01641
01642 $oUtils = oxUtils::getInstance();
01643 foreach ( $this->_aDiscountedVats as $sKey => $dVat ) {
01644
01645 if ( array_key_exists ($sKey, $aAllVats) ) {
01646 $aAllVats[$sKey] += $oUtils->fRound( $dVat, $this->_oCurrency);
01647 } else {
01648 $aAllVats[$sKey] = $dVat;
01649 }
01650 }
01651
01652 foreach ( $aAllVats as $sKey => $dVat ) {
01653 $aVats[$sKey] = oxLang::getInstance()->formatCurrency( $dVat, $this->getBasketCurrency() );
01654 }
01655
01656 return $aVats;
01657 }
01658
01664 public function getDiscountedNettoPrice()
01665 {
01666 if ( $this->_oNotDiscountedProductsPriceList ) {
01667 return $this->_dDiscountedProductNettoPrice + $this->_oNotDiscountedProductsPriceList->getNettoSum();
01668 }
01669 return false;
01670 }
01671
01679 public function setCardMessage( $sMessage )
01680 {
01681 $this->_sCardMessage = $sMessage;
01682 }
01683
01689 public function getCardMessage()
01690 {
01691 return $this->_sCardMessage;
01692 }
01693
01701 public function setCardId( $sCardId )
01702 {
01703 $this->_sCardId = $sCardId;
01704 }
01705
01711 public function getCardId()
01712 {
01713 return $this->_sCardId;
01714 }
01715
01721 public function getCard()
01722 {
01723 $oCard = null;
01724 if ( $sCardId = $this->getCardId() ) {
01725 $oCard = oxNew( 'oxwrapping' );
01726 $oCard->load( $sCardId );
01727 }
01728 return $oCard;
01729 }
01730
01736 public function getTotalDiscount()
01737 {
01738 return $this->_oTotalDiscount;
01739 }
01740
01746 public function getDiscounts()
01747 {
01748 if ( $this->getTotalDiscount() && $this->getTotalDiscount()->getBruttoPrice() == 0 && count($this->_aItemDiscounts) == 0) {
01749 return null;
01750 }
01751
01752 return array_merge($this->_aItemDiscounts, $this->_aDiscounts);
01753 }
01754
01760 public function getVoucherDiscount()
01761 {
01762 return $this->_oVoucherDiscount;
01763 }
01764
01772 public function setBasketCurrency( $oCurrency )
01773 {
01774 $this->_oCurrency = $oCurrency;
01775 }
01776
01782 public function getBasketCurrency()
01783 {
01784 if ( $this->_oCurrency === null ) {
01785 $this->_oCurrency = $this->getConfig()->getActShopCurrencyObject();
01786 }
01787
01788 return $this->_oCurrency;
01789 }
01790
01798 public function setSkipVouchersChecking( $blSkipChecking = null )
01799 {
01800 $this->_blSkipVouchersAvailabilityChecking = $blSkipChecking;
01801 }
01802
01808 public function hasSkipedDiscount()
01809 {
01810 return $this->_blSkipDiscounts;
01811 }
01812
01820 public function setSkipDiscounts( $blSkip )
01821 {
01822 $this->_blSkipDiscounts = $blSkip;
01823 }
01824
01830 public function getProductsNetPrice()
01831 {
01832 return oxLang::getInstance()->formatCurrency( $this->getDiscountedNettoPrice(), $this->getBasketCurrency() );
01833 }
01834
01840 public function getFProductsPrice()
01841 {
01842 if ( $this->_oProductsPriceList ) {
01843 return oxLang::getInstance()->formatCurrency( $this->_oProductsPriceList->getBruttoSum(), $this->getBasketCurrency() );
01844 }
01845 return null;
01846 }
01847
01853 public function getDelCostVatPercent()
01854 {
01855 return $this->getCosts( 'oxdelivery' )->getVat();
01856 }
01857
01863 public function getDelCostVat()
01864 {
01865 $dDelVAT = $this->getCosts( 'oxdelivery' )->getVatValue();
01866 if ( $dDelVAT > 0 ) {
01867 return oxLang::getInstance()->formatCurrency( $dDelVAT, $this->getBasketCurrency() );
01868 }
01869 return false;
01870 }
01871
01877 public function getDelCostNet()
01878 {
01879 return oxLang::getInstance()->formatCurrency( $this->getCosts( 'oxdelivery' )->getNettoPrice(), $this->getBasketCurrency() );
01880 }
01881
01887 public function getPayCostVatPercent()
01888 {
01889 return $this->getCosts( 'oxpayment' )->getVat();
01890 }
01891
01897 public function getPayCostVat()
01898 {
01899 $dPayVAT = $this->getCosts( 'oxpayment' )->getVatValue();
01900 if ( $dPayVAT > 0 ) {
01901 return oxLang::getInstance()->formatCurrency( $dPayVAT, $this->getBasketCurrency() );
01902 }
01903 return false;
01904 }
01905
01911 public function getPayCostNet()
01912 {
01913 return oxLang::getInstance()->formatCurrency( $this->getCosts( 'oxpayment' )->getNettoPrice(), $this->getBasketCurrency() );
01914 }
01915
01921 public function getPaymentCosts()
01922 {
01923 return $this->getCosts( 'oxpayment' )->getBruttoPrice();
01924 }
01925
01931 public function getVoucherDiscValue()
01932 {
01933 if ( $this->getVoucherDiscount() ) {
01934 return $this->getVoucherDiscount()->getBruttoPrice();
01935 }
01936 return false;
01937 }
01938
01944 public function getWrappCostVatPercent()
01945 {
01946 return $this->getCosts( 'oxwrapping' )->getVat();
01947 }
01948
01954 public function getWrappCostVat()
01955 {
01956 $dWrappVAT = $this->getCosts( 'oxwrapping' )->getVatValue();
01957 if ( $dWrappVAT > 0 ) {
01958 return oxLang::getInstance()->formatCurrency( $dWrappVAT, $this->getBasketCurrency() );
01959 }
01960 return false;
01961
01962 }
01963
01969 public function getWrappCostNet()
01970 {
01971 $dWrappNet = $this->getCosts( 'oxwrapping' )->getNettoPrice();
01972 if ( $dWrappNet > 0 ) {
01973 return oxLang::getInstance()->formatCurrency( $dWrappNet, $this->getBasketCurrency() );
01974 }
01975 return false;
01976 }
01977
01983 public function getFPrice()
01984 {
01985 return oxLang::getInstance()->formatCurrency( $this->getPrice()->getBruttoPrice(), $this->getBasketCurrency() );
01986 }
01987
01993 public function getFDeliveryCosts()
01994 {
01995 if ( $oDeliveryCost = $this->getCosts( 'oxdelivery' ) ) {
01996 return oxLang::getInstance()->formatCurrency( $oDeliveryCost->getBruttoPrice(), $this->getBasketCurrency() );
01997 }
01998 return false;
01999 }
02000
02006 public function getDeliveryCosts()
02007 {
02008 if ( $oDeliveryCost = $this->getCosts( 'oxdelivery' ) ) {
02009 return $oDeliveryCost->getBruttoPrice();
02010 }
02011 return false;
02012 }
02013
02014 }