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
01064 $dPrice = 0;
01065 if ( $oArticle->getPrice() != null ) {
01066 $dPrice = $oArticle->getPrice()->getBruttoPrice();
01067 }
01068
01069 foreach ( $aCatIds as $sCatId ) {
01070 if ( !isset( $this->_aBasketSummary->aCategories[$sCatId] ) ) {
01071 $this->_aBasketSummary->aCategories[$sCatId] = new Oxstdclass();
01072 }
01073
01074 $this->_aBasketSummary->aCategories[$sCatId]->dPrice += $dPrice * $oBasketItem->getAmount();
01075 $this->_aBasketSummary->aCategories[$sCatId]->dAmount += $oBasketItem->getAmount();
01076 $this->_aBasketSummary->aCategories[$sCatId]->iCount++;
01077 }
01078
01079
01080 if ( $oArticle->oxarticles__oxparentid->value && $myConfig->getConfigParam( 'blVariantParentBuyable' ) ) {
01081 if ( !isset( $this->_aBasketSummary->aArticles[$oArticle->oxarticles__oxparentid->value] ) ) {
01082 $this->_aBasketSummary->aArticles[$oArticle->oxarticles__oxparentid->value] = 0;
01083 }
01084 $this->_aBasketSummary->aArticles[$oArticle->oxarticles__oxparentid->value] += $oBasketItem->getAmount();
01085 }
01086
01087 if ( !isset( $this->_aBasketSummary->aArticles[$oBasketItem->getProductId()] ) ) {
01088 $this->_aBasketSummary->aArticles[$oBasketItem->getProductId()] = 0;
01089 }
01090
01091 $this->_aBasketSummary->aArticles[$oBasketItem->getProductId()] += $oBasketItem->getAmount();
01092 $this->_aBasketSummary->iArticleCount += $oBasketItem->getAmount();
01093 $this->_aBasketSummary->dArticlePrice += $dPrice * $oBasketItem->getAmount();
01094 }
01095 }
01096 }
01097 return $this->_aBasketSummary;
01098 }
01099
01111 public function addVoucher( $sVoucherId )
01112 {
01113
01114
01115 $dPrice = 0;
01116 if ( $this->_oDiscountProductsPriceList ) {
01117 $dPrice = $this->_oDiscountProductsPriceList->getBruttoSum();
01118 }
01119
01120 try {
01121
01122 $oVoucher = oxNew( 'oxvoucher' );
01123
01124 if ( !$this->_blSkipVouchersAvailabilityChecking ) {
01125 $oVoucher->getVoucherByNr( $sVoucherId, $this->_aVouchers, true );
01126 $oVoucher->checkVoucherAvailability( $this->_aVouchers, $dPrice );
01127 $oVoucher->checkUserAvailability( $this->getBasketUser() );
01128 $oVoucher->markAsReserved();
01129 } else {
01130 $oVoucher->load( $sVoucherId );
01131 }
01132
01133
01134 $this->_aVouchers[$oVoucher->oxvouchers__oxid->value] = $oVoucher->getSimpleVoucher();
01135 } catch ( oxVoucherException $oEx ) {
01136
01137
01138 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
01139 }
01140
01141 $this->onUpdate();
01142 }
01143
01151 public function removeVoucher( $sVoucherId )
01152 {
01153
01154 if ( isset( $this->_aVouchers[$sVoucherId] ) ) {
01155
01156 $oVoucher = oxNew( 'oxvoucher' );
01157 $oVoucher->load( $sVoucherId );
01158
01159 $oVoucher->unMarkAsReserved();
01160
01161
01162 unset( $this->_aVouchers[$sVoucherId] );
01163 $this->onUpdate();
01164 }
01165
01166 }
01167
01173 public function resetUserInfo()
01174 {
01175 $this->setPayment( null );
01176 $this->setShipping( null );
01177 }
01178
01186 protected function _setDeprecatedValues()
01187 {
01188
01189 $this->dproductsprice = $this->_oProductsPriceList->getBruttoSum();
01190 $this->dproductsnetprice = $this->getDiscountedNettoPrice();
01191
01192
01193 $this->dVAT = array_sum( $this->_oProductsPriceList->getVatInfo() );
01194
01195
01196 $this->fproductsprice = $this->getFProductsPrice();
01197 $this->fproductsnetprice = $this->getProductsNetPrice();
01198 $this->fVAT = oxLang::getInstance()->formatCurrency( $this->dVAT, $this->getBasketCurrency());
01199
01200
01201 if ( $oDeliveryCost = $this->getCosts( 'oxdelivery' ) ) {
01202
01203 $this->ddeliverycost = $oDeliveryCost->getBruttoPrice();
01204 $this->ddeliverynetcost = $oDeliveryCost->getNettoPrice();
01205 $this->dDelVAT = $oDeliveryCost->getVatValue();
01206 $this->fDelVATPercent = $oDeliveryCost->getVat() / 100;
01207
01208
01209 $this->fdeliverycost = oxLang::getInstance()->formatCurrency( $this->ddeliverycost, $this->getBasketCurrency() );
01210 $this->fdeliverynetcost = oxLang::getInstance()->formatCurrency( $this->ddeliverynetcost, $this->getBasketCurrency() );
01211 $this->fDelVAT = $this->getDelCostVat();
01212 }
01213
01214
01215
01216 if ( $oWrappingCost = $this->getCosts( 'oxwrapping' ) ) {
01217
01218 $this->dWrappingPrice = $oWrappingCost->getBruttoPrice();
01219 $this->dWrappingNetto = $oWrappingCost->getNettoPrice();
01220 $this->dWrappingVAT = $oWrappingCost->getVatValue();
01221
01222
01223 $this->fWrappingPrice = oxLang::getInstance()->formatCurrency( $this->dWrappingPrice, $this->getBasketCurrency() );
01224 $this->fWrappingNetto = $this->getWrappCostNet();
01225 $this->fWrappingVAT = $this->getWrappCostVat();
01226 $this->fWrappingVATPercent = $this->getWrappCostVatPercent();
01227 }
01228
01229
01230
01231 if ( $oPaymentCost = $this->getCosts( 'oxpayment' ) ) {
01232
01233 $this->dAddPaymentSum = $this->getPaymentCosts();
01234 $this->dAddPaymentSumVAT = $oPaymentCost->getVatValue();
01235
01236
01237 $this->fAddPaymentSum = oxLang::getInstance()->formatCurrency( $this->dAddPaymentSum, $this->getBasketCurrency() );
01238 $this->fAddPaymentSumVAT = $this->getPayCostVat();
01239 $this->fAddPaymentSumVATPercent = $this->getPayCostVatPercent();
01240 $this->fAddPaymentNetSum = $this->getPayCostNet();
01241 }
01242
01243
01244
01245 $this->dprice = $this->_oPrice->getBruttoPrice();
01246 $this->fprice = oxLang::getInstance()->formatCurrency( $this->dprice, $this->getBasketCurrency() );
01247
01248
01249 $this->iCntProducts = $this->getProductsCount();
01250 $this->dCntItems = $this->getItemsCount();
01251 $this->aVATs = $this->getProductVats();
01252 $this->aBasketContents = $this->getContents();
01253
01254
01255 $this->giftmessage = $this->getCardMessage();
01256 $this->chosencard = $this->getCardId();
01257
01258 $this->oCard = $this->getCard();
01259
01260
01261
01262 $this->aDiscounts = $this->getDiscounts();
01263 if ( count($this->aDiscounts) > 0 ) {
01264 foreach ($this->aDiscounts as $oDiscount) {
01265 $oDiscount->fDiscount = oxLang::getInstance()->formatCurrency( $oDiscount->dDiscount, $this->getBasketCurrency() );
01266 }
01267 }
01268 $this->dDiscount = $this->getTotalDiscount()->getBruttoPrice();
01269
01270
01271 $this->aVouchers = $this->getVouchers();
01272 $this->dVoucherDiscount = $this->getVoucherDiscValue();
01273 $this->fVoucherDiscount = oxLang::getInstance()->formatCurrency( $this->dVoucherDiscount, $this->getBasketCurrency() );
01274 $this->dSkippedDiscount = $this->hasSkipedDiscount();
01275
01276 }
01277
01278
01285 protected function _mergeSavedBasket()
01286 {
01287 if ( $this->_blBasketMerged ) {
01288 return;
01289 }
01290
01291 $oUser = $this->getBasketUser();
01292 if ( !$oUser ) {
01293 $this->_blBasketMerged = false;
01294 return;
01295 }
01296
01297 $oBasket = $oUser->getBasket( 'savedbasket' );
01298
01299
01300 $aSavedItems = $oBasket->getItems();
01301 foreach ( $aSavedItems as $oItem ) {
01302 try {
01303 $this->addToBasket( $oItem->getId(), $oItem->dAmount, $oItem->aSelList, null, true );
01304 } catch( oxArticleException $oEx ) {
01305
01306 }
01307 }
01308
01309
01310 foreach ( $this->_aBasketContents as $oBasketItem ) {
01311 $oBasket->addItemToBasket( $oBasketItem->getProductId(), $oBasketItem->getAmount(), $oBasketItem->getSelList(), true );
01312 }
01313
01314
01315 $this->_blBasketMerged = true;
01316 }
01317
01328 protected function _addItemToSavedBasket( $sProductId , $dAmount, $aSel, $blOverride = false )
01329 {
01330
01331 if ( $oUser = $this->getBasketUser() ) {
01332 $oUser->getBasket( 'savedbasket' )->addItemToBasket( $sProductId, $dAmount, $aSel, $blOverride );
01333 }
01334 }
01335
01343 protected function _deleteSavedBasket()
01344 {
01345
01346 if ( $oUser = $this->getBasketUser() ) {
01347 $oUser->getBasket( 'savedbasket' )->delete();
01348 }
01349 }
01350
01356 protected function _findDelivCountry()
01357 {
01358 $myConfig = $this->getConfig();
01359 $oUser = $this->getBasketUser();
01360
01361 $sDelivCountry = null;
01362 if ( !$oUser ) {
01363
01364 $aHomeCountry = $myConfig->getConfigParam( 'aHomeCountry' );
01365 if ( $myConfig->getConfigParam( 'blCalculateDelCostIfNotLoggedIn' ) && is_array( $aHomeCountry ) ) {
01366 $sDelivCountry = current( $aHomeCountry );
01367 }
01368 } else {
01369
01370 if ( $sCountryId = $myConfig->getGlobalParameter( 'delcountryid' ) ) {
01371 $sDelivCountry = $sCountryId;
01372 } elseif ( $sAddressId = oxConfig::getParameter( 'deladrid' ) ) {
01373
01374 $oDelAdress = oxNew( 'oxbase' );
01375 $oDelAdress->init( 'oxaddress' );
01376 if ( $oDelAdress->load( $sAddressId ) ) {
01377 $sDelivCountry = $oDelAdress->oxaddress__oxcountryid->value;
01378 }
01379 }
01380
01381
01382 if ( !$sDelivCountry ) {
01383 $sDelivCountry = $oUser->oxuser__oxcountryid->value;
01384 }
01385 }
01386
01387 return $sDelivCountry;
01388 }
01389
01395 public function deleteBasket()
01396 {
01397 $this->getSession()->delBasket();
01398
01399
01400 if ( !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' ) ) {
01401 $this->_deleteSavedBasket();
01402 }
01403 }
01404
01412 public function setPayment( $sPaymentId = null )
01413 {
01414 $this->_sPaymentId = $sPaymentId;
01415 }
01416
01422 public function getPaymentId()
01423 {
01424 if ( !$this->_sPaymentId ) {
01425 $this->_sPaymentId = oxConfig::getParameter( 'paymentid' );
01426 }
01427 return $this->_sPaymentId;
01428 }
01429
01437 public function setShipping( $sShippingSetId = null )
01438 {
01439 $this->_sShippingSetId = $sShippingSetId;
01440 oxSession::setVar( 'sShipSet', $sShippingSetId );
01441 }
01442
01450 public function setDeliveryPrice( $oShippingPrice = null )
01451 {
01452 $this->_oDeliveryPrice = $oShippingPrice;
01453 }
01454
01460 public function getShippingId()
01461 {
01462 if ( !$this->_sShippingSetId ) {
01463 $this->_sShippingSetId = oxConfig::getParameter( 'sShipSet' );
01464 }
01465
01466
01467 if ( !$this->_sShippingSetId && $this->getPaymentId() != 'oxempty' ) {
01468 $this->_sShippingSetId = 'oxidstandard';
01469 }
01470
01471 return $this->_sShippingSetId;
01472 }
01473
01479 public function getBasketArticles()
01480 {
01481 $aBasketArticles = array();
01482 foreach ( $this->_aBasketContents as $sItemKey => $oBasketItem ) {
01483
01484 $oProduct = $oBasketItem->getArticle();
01485
01486 if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) ) {
01487
01488 $aSelList = $oBasketItem->getSelList();
01489 if ( is_array( $aSelList ) && ( $aSelectlist = $oProduct->getSelectLists( $sItemKey ) ) ) {
01490 reset( $aSelList );
01491 while ( list( $conkey, $iSel ) = each( $aSelList ) ) {
01492 $aSelectlist[$conkey][$iSel]->selected = 1;
01493 }
01494 $oProduct->setSelectlist( $aSelectlist );
01495 }
01496 }
01497
01498 $aBasketArticles[$sItemKey] = $oProduct;
01499 }
01500 return $aBasketArticles;
01501 }
01502
01508 public function getDiscountProductsPrice()
01509 {
01510 return $this->_oDiscountProductsPriceList;
01511 }
01512
01518 public function getProductsPrice()
01519 {
01520 if ( is_null($this->_oProductsPriceList) ) {
01521 $this->_oProductsPriceList = oxNew( 'oxPriceList' );
01522 }
01523
01524 return $this->_oProductsPriceList;
01525 }
01526
01532 public function getPrice()
01533 {
01534 if ( is_null($this->_oPrice) ) {
01535 $this->_oPrice = oxNew( 'oxprice' );
01536 }
01537
01538 return $this->_oPrice;
01539 }
01540
01547 public function getOrderId()
01548 {
01549 return $this->_sOrderId;
01550 }
01551
01559 public function setOrderId( $sId )
01560 {
01561 $this->_sOrderId = $sId;
01562 }
01563
01572 public function getCosts( $sId = null )
01573 {
01574
01575 if ( $sId ) {
01576 return isset( $this->_aCosts[$sId] )?$this->_aCosts[$sId]:null;
01577 }
01578 return $this->_aCosts;
01579 }
01580
01586 public function getVouchers()
01587 {
01588 return $this->_aVouchers;
01589 }
01590
01596 public function getProductsCount()
01597 {
01598 return $this->_iProductsCnt;
01599 }
01600
01606 public function getItemsCount()
01607 {
01608 return $this->_dItemsCnt;
01609 }
01610
01616 public function getWeight()
01617 {
01618 return $this->_dWeight;
01619 }
01620
01626 public function getContents()
01627 {
01628 return $this->_aBasketContents;
01629 }
01630
01636 public function getProductVats()
01637 {
01638 if ( !$this->_oNotDiscountedProductsPriceList ) {
01639 return array();
01640 }
01641
01642 $aVats = array();
01643
01644 $aAllVats = $this->_oNotDiscountedProductsPriceList->getVatInfo();
01645
01646 $oUtils = oxUtils::getInstance();
01647 foreach ( $this->_aDiscountedVats as $sKey => $dVat ) {
01648
01649 if ( array_key_exists ($sKey, $aAllVats) ) {
01650 $aAllVats[$sKey] += $oUtils->fRound( $dVat, $this->_oCurrency);
01651 } else {
01652 $aAllVats[$sKey] = $dVat;
01653 }
01654 }
01655
01656 foreach ( $aAllVats as $sKey => $dVat ) {
01657 $aVats[$sKey] = oxLang::getInstance()->formatCurrency( $dVat, $this->getBasketCurrency() );
01658 }
01659
01660 return $aVats;
01661 }
01662
01668 public function getDiscountedNettoPrice()
01669 {
01670 if ( $this->_oNotDiscountedProductsPriceList ) {
01671 return $this->_dDiscountedProductNettoPrice + $this->_oNotDiscountedProductsPriceList->getNettoSum();
01672 }
01673 return false;
01674 }
01675
01683 public function setCardMessage( $sMessage )
01684 {
01685 $this->_sCardMessage = $sMessage;
01686 }
01687
01693 public function getCardMessage()
01694 {
01695 return $this->_sCardMessage;
01696 }
01697
01705 public function setCardId( $sCardId )
01706 {
01707 $this->_sCardId = $sCardId;
01708 }
01709
01715 public function getCardId()
01716 {
01717 return $this->_sCardId;
01718 }
01719
01725 public function getCard()
01726 {
01727 $oCard = null;
01728 if ( $sCardId = $this->getCardId() ) {
01729 $oCard = oxNew( 'oxwrapping' );
01730 $oCard->load( $sCardId );
01731 }
01732 return $oCard;
01733 }
01734
01740 public function getTotalDiscount()
01741 {
01742 return $this->_oTotalDiscount;
01743 }
01744
01750 public function getDiscounts()
01751 {
01752 if ( $this->getTotalDiscount() && $this->getTotalDiscount()->getBruttoPrice() == 0 && count($this->_aItemDiscounts) == 0) {
01753 return null;
01754 }
01755
01756 return array_merge($this->_aItemDiscounts, $this->_aDiscounts);
01757 }
01758
01764 public function getVoucherDiscount()
01765 {
01766 return $this->_oVoucherDiscount;
01767 }
01768
01776 public function setBasketCurrency( $oCurrency )
01777 {
01778 $this->_oCurrency = $oCurrency;
01779 }
01780
01786 public function getBasketCurrency()
01787 {
01788 if ( $this->_oCurrency === null ) {
01789 $this->_oCurrency = $this->getConfig()->getActShopCurrencyObject();
01790 }
01791
01792 return $this->_oCurrency;
01793 }
01794
01802 public function setSkipVouchersChecking( $blSkipChecking = null )
01803 {
01804 $this->_blSkipVouchersAvailabilityChecking = $blSkipChecking;
01805 }
01806
01812 public function hasSkipedDiscount()
01813 {
01814 return $this->_blSkipDiscounts;
01815 }
01816
01824 public function setSkipDiscounts( $blSkip )
01825 {
01826 $this->_blSkipDiscounts = $blSkip;
01827 }
01828
01834 public function getProductsNetPrice()
01835 {
01836 return oxLang::getInstance()->formatCurrency( $this->getDiscountedNettoPrice(), $this->getBasketCurrency() );
01837 }
01838
01844 public function getFProductsPrice()
01845 {
01846 if ( $this->_oProductsPriceList ) {
01847 return oxLang::getInstance()->formatCurrency( $this->_oProductsPriceList->getBruttoSum(), $this->getBasketCurrency() );
01848 }
01849 return null;
01850 }
01851
01857 public function getDelCostVatPercent()
01858 {
01859 return $this->getCosts( 'oxdelivery' )->getVat();
01860 }
01861
01867 public function getDelCostVat()
01868 {
01869 $dDelVAT = $this->getCosts( 'oxdelivery' )->getVatValue();
01870 if ( $dDelVAT > 0 ) {
01871 return oxLang::getInstance()->formatCurrency( $dDelVAT, $this->getBasketCurrency() );
01872 }
01873 return false;
01874 }
01875
01881 public function getDelCostNet()
01882 {
01883 return oxLang::getInstance()->formatCurrency( $this->getCosts( 'oxdelivery' )->getNettoPrice(), $this->getBasketCurrency() );
01884 }
01885
01891 public function getPayCostVatPercent()
01892 {
01893 return $this->getCosts( 'oxpayment' )->getVat();
01894 }
01895
01901 public function getPayCostVat()
01902 {
01903 $dPayVAT = $this->getCosts( 'oxpayment' )->getVatValue();
01904 if ( $dPayVAT > 0 ) {
01905 return oxLang::getInstance()->formatCurrency( $dPayVAT, $this->getBasketCurrency() );
01906 }
01907 return false;
01908 }
01909
01915 public function getPayCostNet()
01916 {
01917 return oxLang::getInstance()->formatCurrency( $this->getCosts( 'oxpayment' )->getNettoPrice(), $this->getBasketCurrency() );
01918 }
01919
01925 public function getPaymentCosts()
01926 {
01927 return $this->getCosts( 'oxpayment' )->getBruttoPrice();
01928 }
01929
01935 public function getVoucherDiscValue()
01936 {
01937 if ( $this->getVoucherDiscount() ) {
01938 return $this->getVoucherDiscount()->getBruttoPrice();
01939 }
01940 return false;
01941 }
01942
01948 public function getWrappCostVatPercent()
01949 {
01950 return $this->getCosts( 'oxwrapping' )->getVat();
01951 }
01952
01958 public function getWrappCostVat()
01959 {
01960 $dWrappVAT = $this->getCosts( 'oxwrapping' )->getVatValue();
01961 if ( $dWrappVAT > 0 ) {
01962 return oxLang::getInstance()->formatCurrency( $dWrappVAT, $this->getBasketCurrency() );
01963 }
01964 return false;
01965
01966 }
01967
01973 public function getWrappCostNet()
01974 {
01975 $dWrappNet = $this->getCosts( 'oxwrapping' )->getNettoPrice();
01976 if ( $dWrappNet > 0 ) {
01977 return oxLang::getInstance()->formatCurrency( $dWrappNet, $this->getBasketCurrency() );
01978 }
01979 return false;
01980 }
01981
01987 public function getFPrice()
01988 {
01989 return oxLang::getInstance()->formatCurrency( $this->getPrice()->getBruttoPrice(), $this->getBasketCurrency() );
01990 }
01991
01997 public function getFDeliveryCosts()
01998 {
01999 $oDeliveryCost = $this->getCosts( 'oxdelivery' );
02000 if ( $oDeliveryCost && $oDeliveryCost->getBruttoPrice()) {
02001 return oxLang::getInstance()->formatCurrency( $oDeliveryCost->getBruttoPrice(), $this->getBasketCurrency() );
02002 }
02003 return false;
02004 }
02005
02011 public function getDeliveryCosts()
02012 {
02013 if ( $oDeliveryCost = $this->getCosts( 'oxdelivery' ) ) {
02014 return $oDeliveryCost->getBruttoPrice();
02015 }
02016 return false;
02017 }
02018
02019 }