00001 <?php
00002
00005 class oxBasket extends oxSuperCfg
00006 {
00012 protected $_aBasketContents = array();
00013
00019 protected $_iProductsCnt = 0;
00020
00026 protected $_dItemsCnt = 0.0;
00027
00033 protected $_dWeight = 0.0;
00034
00040 protected $_oPrice = null;
00041
00047 protected $_oProductsPriceList = null;
00048
00054 protected $_aDiscounts = array();
00055
00061 protected $_aItemDiscounts = array();
00062
00068 protected $_sOrderId = null;
00069
00075 protected $_aVouchers = array();
00076
00082 protected $_aCosts = array();
00083
00089 protected $_oDiscountProductsPriceList = null;
00090
00096 protected $_oNotDiscountedProductsPriceList = null;
00097
00103 protected $_blUpdateNeeded = true;
00104
00110 protected $_aBasketSummary = null;
00111
00117 protected $_blBasketMerged = false;
00118
00124 protected $_sPaymentId = null;
00125
00131 protected $_sShippingSetId = null;
00132
00138 protected $_oUser = null;
00139
00145 protected $_oTotalDiscount = null;
00146
00152 protected $_oVoucherDiscount = null;
00153
00159 protected $_oCurrency = null;
00160
00166 protected $_blSkipVouchersAvailabilityChecking = null;
00167
00173 protected $_dDiscountedProductNettoPrice = null;
00174
00180 protected $_aDiscountedVats = null;
00181
00187 protected $_blSkipDiscounts = false;
00188
00194 protected $_oDeliveryPrice = null;
00195
00201 protected $_blCheckStock = true;
00202
00208 public function isEnabled()
00209 {
00210 return !oxUtils::getInstance()->isSearchEngine();
00211 }
00212
00222 protected function _changeBasketItemKey($sOldKey, $sNewKey, $value = null)
00223 {
00224 reset($this->_aBasketContents);
00225 $iOldKeyPlace = 0;
00226 while (key($this->_aBasketContents) != $sOldKey && next($this->_aBasketContents)) {
00227 ++$iOldKeyPlace;
00228 }
00229 $aNewCopy = array_merge(
00230 array_slice($this->_aBasketContents, 0, $iOldKeyPlace, true),
00231 array($sNewKey => $value),
00232 array_slice($this->_aBasketContents, $iOldKeyPlace+1, count($this->_aBasketContents)-$iOldKeyPlace, true)
00233 );
00234 $this->_aBasketContents = $aNewCopy;
00235 }
00236
00252 public function addToBasket( $sProductID, $dAmount, $aSel = null, $aPersParam = null, $blOverride = false, $blBundle = false, $sOldBasketItemId = null )
00253 {
00254
00255 if ( !$this->isEnabled() )
00256 return null;
00257
00258
00259
00260 $sItemId = $this->getItemKey( $sProductID, $aSel, $aPersParam, $blBundle );
00261 if ($sOldBasketItemId && strcmp($sOldBasketItemId, $sItemId)) {
00262 if (isset( $this->_aBasketContents[$sItemId] )) {
00263
00264 unset( $this->_aBasketContents[$sOldBasketItemId] );
00265 } else {
00266
00267 $this->_changeBasketItemKey($sOldBasketItemId, $sItemId);
00268 }
00269 }
00270
00271
00272 $blRemoveItem = false;
00273
00274
00275 $oEx = null;
00276
00277 if ( isset( $this->_aBasketContents[$sItemId] ) ) {
00278
00279
00280 try {
00281
00282 $this->_aBasketContents[$sItemId]->setStockCheckStatus( $this->getStockCheckMode() );
00283 $this->_aBasketContents[$sItemId]->setAmount( $dAmount, $blOverride );
00284 } catch( oxOutOfStockException $oEx ) {
00285
00286 }
00287
00288 } else {
00289
00290 $oBasketItem = oxNew( 'oxbasketitem' );
00291 try {
00292 $oBasketItem->setStockCheckStatus( $this->getStockCheckMode() );
00293 $oBasketItem->init( $sProductID, $dAmount, $aSel, $aPersParam, $blBundle );
00294 } catch( oxNoArticleException $oEx ) {
00295
00296
00297 $blRemoveItem = true;
00298
00299 } catch( oxOutOfStockException $oEx ){
00300
00301 }
00302
00303 $this->_aBasketContents[$sItemId] = $oBasketItem;
00304 }
00305
00306
00307 if ( $this->_aBasketContents[$sItemId]->getAmount() == 0 || $blRemoveItem ) {
00308 $this->removeItem( $sItemId );
00309 $blRemoveItem = true;
00310 } elseif ( $blBundle ) {
00311 $this->_aBasketContents[$sItemId]->setBundle( true );
00312 }
00313
00314
00315 $this->onUpdate();
00316
00317
00318 if ( !$blBundle ) {
00319 $this->_addItemToSavedBasket( $sProductID, $dAmount, $aSel, $blOverride );
00320 }
00321
00322 if ( $oEx ) {
00323 throw $oEx;
00324 }
00325 return $this->_aBasketContents[$sItemId];
00326 }
00327
00335 public function setStockCheckMode( $blCheck )
00336 {
00337 $this->_blCheckStock = $blCheck;
00338 }
00339
00345 public function getStockCheckMode()
00346 {
00347 return $this->_blCheckStock;
00348 }
00349
00362 public function getItemKey( $sProductId, $aSel = null, $aPersParam = null, $blBundle = false, $sAdditionalParam = '' )
00363 {
00364 $aSel = ( $aSel != null) ? $aSel : array (0=>'0');
00365
00366 $sItemKey = md5( $sProductId.'|'.serialize( $aSel ).'|'.serialize( $aPersParam ).'|'.( int ) $blBundle . '|' . serialize( $sAdditionalParam ) );
00367
00368 return $sItemKey;
00369 }
00370
00371
00379 public function removeItem( $sItemKey )
00380 {
00381 unset( $this->_aBasketContents[$sItemKey] );
00382 }
00383
00389 protected function _clearBundles()
00390 {
00391 reset( $this->_aBasketContents );
00392 while ( list( $sItemKey, $oBasketItem ) = each( $this->_aBasketContents ) ) {
00393 if ( $oBasketItem->isBundle() ) {
00394 $this->removeItem( $sItemKey );
00395 }
00396 }
00397 }
00398
00406 protected function _getArticleBundles( $oBasketItem )
00407 {
00408 $aBundles = array();
00409
00410 if ( $oBasketItem->isBundle() ) {
00411 return $aBundles;
00412 }
00413
00414 $oArticle = $oBasketItem->getArticle();
00415 if ( $oArticle && $oArticle->oxarticles__oxbundleid->value ) {
00416 $aBundles[$oArticle->oxarticles__oxbundleid->value] = 1;
00417 }
00418
00419 return $aBundles;
00420 }
00421
00429 protected function _getItemBundles( $oBasketItem )
00430 {
00431 if ( $oBasketItem->isBundle() ) {
00432 return array();
00433 }
00434
00435 $aBundles = array();
00436
00437
00438 if ( $oArticle = $oBasketItem->getArticle() ) {
00439 $aDiscounts = oxDiscountList::getInstance()->getBasketItemBundleDiscounts( $oArticle, $this, $this->getBasketUser() );
00440
00441 foreach ( $aDiscounts as $oDiscount ) {
00442
00443
00444 if ( !isset( $aBundles[$oDiscount->oxdiscount__oxitmartid->value] ) ) {
00445 $aBundles[$oDiscount->oxdiscount__oxitmartid->value] = 0;
00446 }
00447
00448 $aBundles[$oDiscount->oxdiscount__oxitmartid->value] += $oDiscount->getBundleAmount( $oBasketItem->getAmount() );
00449
00450 }
00451 }
00452
00453 return $aBundles;
00454 }
00455
00461 protected function _getBasketBundles()
00462 {
00463 $aBundles = array();
00464 $aDiscounts = oxDiscountList::getInstance()->getBasketBundleDiscounts( $this, $this->getBasketUser() );
00465
00466
00467 $dAmount = 0;
00468 foreach ( $this->_aBasketContents as $oBasketItem ) {
00469 if ( !( $oBasketItem->isBundle() || $oBasketItem->isDiscountArticle() ) ) {
00470 $dAmount += $oBasketItem->getAmount();
00471 }
00472 }
00473
00474 foreach ( $aDiscounts as $oDiscount ) {
00475 if ($oDiscount->oxdiscount__oxitmartid->value) {
00476 if ( !isset( $aBundles[$oDiscount->oxdiscount__oxitmartid->value] ) ) {
00477 $aBundles[$oDiscount->oxdiscount__oxitmartid->value] = 0;
00478 }
00479
00480 $aBundles[$oDiscount->oxdiscount__oxitmartid->value] += $oDiscount->getBundleAmount( $dAmount );
00481
00482 }
00483 }
00484
00485 return $aBundles;
00486 }
00487
00494 protected function _addBundles()
00495 {
00496
00497 foreach ( $this->_aBasketContents as $key => $oBasketItem ) {
00498
00499
00500 if ( !$oBasketItem->isDiscountArticle() && !$oBasketItem->isBundle() ) {
00501 $aBundles = $this->_getItemBundles( $oBasketItem );
00502 } else {
00503 continue;
00504 }
00505
00506 $this->_addBundlesToBasket( $aBundles );
00507
00508
00509 $aBundles = $this->_getArticleBundles( $oBasketItem );
00510
00511
00512 $this->_addBundlesToBasket( $aBundles );
00513 }
00514
00515
00516 if ( $aBundles = $this->_getBasketBundles() ) {
00517 $this->_addBundlesToBasket( $aBundles );
00518 }
00519
00520 }
00521
00529 protected function _addBundlesToBasket( $aBundles )
00530 {
00531 foreach ( $aBundles as $sBundleId => $dAmount ) {
00532 try {
00533 if ( $oBundleItem = $this->addToBasket( $sBundleId, $dAmount, null, null, true, true ) ) {
00534 $oBundleItem->setAsDiscountArticle( true );
00535 }
00536 } catch(oxArticleException $oEx) {
00537
00538 }
00539 }
00540
00541 }
00542
00548 protected function _calcItemsPrice()
00549 {
00550
00551 $this->setSkipDiscounts( false );
00552 $this->_iProductsCnt = 0;
00553 $this->_dItemsCnt = 0;
00554 $this->_dWeight = 0;
00555
00556
00557 $this->_aItemDiscounts = array();
00558
00559 $this->_oProductsPriceList = oxNew( 'oxpricelist' );
00560 $this->_oDiscountProductsPriceList = oxNew( 'oxpricelist' );
00561 $this->_oNotDiscountedProductsPriceList = oxNew( 'oxpricelist' );
00562
00563 foreach ( $this->_aBasketContents as $oBasketItem ) {
00564 $this->_iProductsCnt++;
00565 $this->_dItemsCnt += $oBasketItem->getAmount();
00566 $this->_dWeight += $oBasketItem->getWeight();
00567
00568 if ( !$oBasketItem->isDiscountArticle() && ( $oArticle = $oBasketItem->getArticle() ) ) {
00569 $oBasketPrice = $oArticle->getBasketPrice( $oBasketItem->getAmount(), $oBasketItem->getSelList(), $this );
00570 $oBasketItem->setPrice( $oBasketPrice );
00571
00572 $this->_oProductsPriceList->addToPriceList( $oBasketItem->getPrice() );
00573
00574 $oBasketPrice->setBruttoPriceMode();
00575 if ( !$oArticle->skipDiscounts() ) {
00576
00577 $aItemDiscounts = $oArticle->applyBasketDiscounts( $oBasketPrice, oxDiscountList::getInstance()->getBasketItemDiscounts( $oArticle, $this, $this->getBasketUser() ), $oBasketItem->getAmount() );
00578 if ( is_array($this->_aItemDiscounts) && is_array($aItemDiscounts) ) {
00579 $this->_aItemDiscounts = $this->_mergeDiscounts( $this->_aItemDiscounts, $aItemDiscounts);
00580 }
00581 } else {
00582 $oBasketItem->setSkipDiscounts( true );
00583 $this->setSkipDiscounts( true );
00584 }
00585 $oBasketPrice->multiply( $oBasketItem->getAmount() );
00586
00587
00588 if ( !$oArticle->skipDiscounts() ) {
00589 $this->_oDiscountProductsPriceList->addToPriceList( $oBasketPrice );
00590 } else {
00591 $this->_oNotDiscountedProductsPriceList->addToPriceList( $oBasketPrice );
00592 $oBasketItem->setSkipDiscounts( true );
00593 $this->setSkipDiscounts( true );
00594 }
00595 } elseif ( $oBasketItem->isBundle() ) {
00596
00597 $oPrice = oxNew( "oxprice");
00598 $oBasketItem->setPrice( $oPrice );
00599 }
00600 }
00601 }
00602
00612 protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
00613 {
00614 foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
00615
00616 if ( array_key_exists ($sKey, $aDiscounts) ) {
00617 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
00618 } else {
00619 $aDiscounts[$sKey] = $oDiscount;
00620 }
00621 }
00622 return $aDiscounts;
00623 }
00624
00630 protected function _calcDeliveryCost()
00631 {
00632 if ( $this->_oDeliveryPrice !== null ) {
00633 return $this->_oDeliveryPrice;
00634 }
00635 $myConfig = $this->getConfig();
00636 $oDeliveryPrice = oxNew( 'oxprice' );
00637 $oDeliveryPrice->setBruttoPriceMode();
00638
00639
00640 $oUser = $this->getBasketUser();
00641
00642 if ( !$oUser && !$myConfig->getConfigParam( 'blCalculateDelCostIfNotLoggedIn' ) ) {
00643 return $oDeliveryPrice;
00644 }
00645
00646
00647 $fDelVATPercent = 0;
00648 if ( $myConfig->getConfigParam( 'blCalcVATForDelivery' ) ) {
00649 $fDelVATPercent = $this->getMostUsedVatPercent();
00650 $oDeliveryPrice->setVat( $fDelVATPercent );
00651 }
00652
00653
00654 if ( $myConfig->getConfigParam('bl_perfLoadDelivery') ) {
00655 $aDeliveryList = oxDeliveryList::getInstance()->getDeliveryList( $this,
00656 $oUser,
00657 $this->_findDelivCountry(),
00658 oxConfig::getParameter( 'sShipSet' )
00659 );
00660
00661 if ( count( $aDeliveryList ) > 0 ) {
00662 foreach ( $aDeliveryList as $oDelivery ) {
00663
00664 if ( $myConfig->getConfigParam( 'iDebug' ) == 5 ) {
00665 echo( "DelCost : ".$oDelivery->oxdelivery__oxtitle->value."<br>" );
00666 }
00667 $oDeliveryPrice->addPrice( $oDelivery->getDeliveryPrice( $fDelVATPercent ) );
00668 }
00669 }
00670 }
00671
00672 return $oDeliveryPrice;
00673 }
00674
00680 public function getBasketUser()
00681 {
00682 if ( $this->_oUser == null ) {
00683 return $this->getUser();
00684 }
00685
00686 return $this->_oUser;
00687 }
00688
00696 public function setBasketUser( $oUser )
00697 {
00698 $this->_oUser = $oUser;
00699 }
00700
00701
00707 public function getMostUsedVatPercent()
00708 {
00709 return $this->_oProductsPriceList->getMostUsedVatPercent();
00710 }
00711
00712
00719 protected function _calcTotalPrice()
00720 {
00721
00722 $dprice = $this->_oProductsPriceList->getBruttoSum();
00723 $this->_oPrice->setPrice( $dprice );
00724
00725
00726 if ( $dprice ) {
00727
00728
00729 foreach ( $this->_aItemDiscounts as $oDiscount ) {
00730
00731
00732 if ( $oDiscount->sType == 'itm' ) {
00733 continue;
00734 }
00735 $this->_oPrice->subtract( $oDiscount->dDiscount );
00736 }
00737
00738
00739 $this->_oPrice->subtract( $this->_oTotalDiscount->getBruttoPrice() );
00740
00741
00742 $this->_oPrice->subtract( $this->_oVoucherDiscount->getBruttoPrice() );
00743 }
00744
00745
00746 if ( isset( $this->_aCosts['oxdelivery'] ) ) {
00747 $this->_oPrice->add( $this->_aCosts['oxdelivery']->getBruttoPrice() );
00748 }
00749
00750
00751 if ( isset( $this->_aCosts['oxwrapping'] ) ) {
00752 $this->_oPrice->add( $this->_aCosts['oxwrapping']->getBruttoPrice() );
00753 }
00754
00755
00756 if ( isset( $this->_aCosts['oxpayment'] ) ) {
00757 $this->_oPrice->add( $this->_aCosts['oxpayment']->getBruttoPrice() );
00758 }
00759
00760 }
00761
00767 protected function _calcVoucherDiscount()
00768 {
00769 $this->_oVoucherDiscount = oxNew( 'oxPrice' );
00770 $this->_oVoucherDiscount->setBruttoPriceMode();
00771
00772
00773
00774 $dPrice = $this->_oDiscountProductsPriceList->getBruttoSum() - $this->_oTotalDiscount->getBruttoPrice();
00775
00776
00777 if ( count( $this->_aVouchers ) ) {
00778 $oLang = oxLang::getInstance();
00779 foreach ( $this->_aVouchers as $sVoucherId => $oStdVoucher ) {
00780 $oVoucher = oxNew( 'oxvoucher' );
00781 try {
00782 $oVoucher->load( $oStdVoucher->sVoucherId );
00783
00784 if ( !$this->_blSkipVouchersAvailabilityChecking ) {
00785 $oVoucher->checkBasketVoucherAvailability( $this->_aVouchers, $dPrice );
00786 $oVoucher->checkUserAvailability( $this->getBasketUser() );
00787 }
00788
00789
00790 $dVoucherdiscount = $oVoucher->getDiscountValue( $dPrice );
00791
00792
00793 $this->_oVoucherDiscount->add( $dVoucherdiscount );
00794
00795
00796 $oStdVoucher->fVoucherdiscount = $oLang->formatCurrency( $dVoucherdiscount, $this->getBasketCurrency() );
00797
00798
00799 $dPrice -= $dVoucherdiscount;
00800 } catch ( oxVoucherException $oEx ) {
00801
00802
00803 $oVoucher->unMarkAsReserved();
00804 unset( $this->_aVouchers[$sVoucherId] );
00805
00806
00807 oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00808 }
00809 }
00810 }
00811 }
00812
00813
00820 protected function _applyDiscounts()
00821 {
00822 $dBruttoPrice = $this->_oDiscountProductsPriceList->getBruttoSum();
00823 $this->_aDiscountedVats = $this->_oDiscountProductsPriceList->getVatInfo();
00824
00825
00826 $dDiscountedBruttoPrice = $dBruttoPrice - $this->_oTotalDiscount->getBruttoPrice() - $this->_oVoucherDiscount->getBruttoPrice();
00827
00828
00829 if ( $dBruttoPrice && ( $this->_oTotalDiscount->getBruttoPrice() || $this->_oVoucherDiscount->getBruttoPrice() )) {
00830 $dPercent = ( $dDiscountedBruttoPrice / $dBruttoPrice) * 100;
00831 foreach ( $this->_aDiscountedVats as $sKey => $dVat ) {
00832 $this->_aDiscountedVats[$sKey] = oxPrice::percent( $dVat, $dPercent);
00833 }
00834 }
00835
00836 $oUtils = oxUtils::getInstance();
00837 $dDiscVatSum = 0;
00838 foreach ( $this->_aDiscountedVats as $dVat ) {
00839 $dDiscVatSum += $oUtils->fRound( -$dVat, $this->_oCurrency);
00840 }
00841
00842 $this->_dDiscountedProductNettoPrice = $dDiscountedBruttoPrice + $dDiscVatSum;
00843 }
00844
00850 protected function _calcBasketDiscount()
00851 {
00852
00853 $this->_aDiscounts = array();
00854
00855
00856 $dOldprice = $this->_oDiscountProductsPriceList->getBruttoSum();
00857
00858
00859 $aDiscounts = oxDiscountList::getInstance()->getBasketDiscounts( $this, $this->getBasketUser() );
00860
00861 foreach ( $aDiscounts as $oDiscount ) {
00862
00863
00864 $oStdDiscount = $oDiscount->getSimpleDiscount();
00865
00866
00867 if ( $oDiscount->oxdiscount__oxaddsumtype->value == 'itm' ) {
00868 continue;
00869 }
00870
00871
00872 $oStdDiscount->dDiscount = $oDiscount->getAbsValue( $dOldprice );
00873
00874 $this->_aDiscounts[$oDiscount->getId()] = $oStdDiscount;
00875
00876
00877 $dOldprice = $dOldprice - $oStdDiscount->dDiscount;
00878 }
00879 }
00880
00886 protected function _calcBasketTotalDiscount()
00887 {
00888 if ( $this->_oTotalDiscount && $this->_oTotalDiscount->getBruttoPrice() != 0 ) {
00889 return;
00890 }
00891 $this->_oTotalDiscount = oxNew( 'oxPrice' );
00892 $this->_oTotalDiscount->setBruttoPriceMode();
00893
00894 if ( is_array($this->_aDiscounts) ) {
00895 foreach ( $this->_aDiscounts as $oDiscount ) {
00896
00897
00898 if ( $oDiscount->sType == 'itm' ) {
00899 continue;
00900 }
00901
00902
00903 $this->_oTotalDiscount->add( $oDiscount->dDiscount );
00904 }
00905 }
00906 }
00907
00917 protected function _calcBasketWrapping()
00918 {
00919 $myConfig = $this->getConfig();
00920 $oWrappingPrice = oxNew( 'oxPrice' );
00921 $oWrappingPrice->setBruttoPriceMode();
00922
00923
00924 if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
00925 $oWrappingPrice->setVat( $this->getMostUsedVatPercent() );
00926 }
00927
00928
00929 foreach ( $this->_aBasketContents as $oBasketItem ) {
00930
00931 if ( ( $oWrapping = $oBasketItem->getWrapping() ) ) {
00932 $oWrapPrice = $oWrapping->getWrappingPrice( $oBasketItem->getAmount() );
00933 $oWrappingPrice->add( $oWrapPrice->getBruttoPrice() );
00934 }
00935 }
00936
00937
00938 if ( ( $oCard = $this->getCard() ) ) {
00939 $oCardPrice = $oCard->getWrappingPrice();
00940 $oWrappingPrice->add( $oCardPrice->getBruttoPrice() );
00941 }
00942
00943 return $oWrappingPrice;
00944 }
00945
00952 protected function _calcPaymentCost()
00953 {
00954
00955 $oPaymentPrice = oxNew( 'oxPrice' );
00956 $oPaymentPrice->setBruttoPriceMode();
00957
00958
00959 if ( ( $this->_sPaymentId = $this->getPaymentId() ) ) {
00960
00961 $oPayment = oxNew( 'oxpayment' );
00962 $oPayment->load( $this->_sPaymentId );
00963
00964 $oPaymentPrice = $oPayment->getPaymentPrice( $this );
00965 }
00966
00967 return $oPaymentPrice;
00968 }
00969
00978 public function setCost( $sCostName, $oPrice = null )
00979 {
00980 $this->_aCosts[$sCostName] = $oPrice;
00981 }
00982
00991 public function calculateBasket( $blForceUpdate = false )
00992 {
00993 if ( !$this->isEnabled() )
00994 return;
00995
00996 if ( !$this->_blUpdateNeeded && !$blForceUpdate )
00997 return;
00998
00999 $this->_aCosts = array();
01000
01001 $this->_oPrice = oxNew( 'oxprice' );
01002 $this->_oPrice->setBruttoPriceMode();
01003
01004
01005 $this->_mergeSavedBasket();
01006
01007
01008 $this->_clearBundles();
01009
01010
01011 $this->_addBundles();
01012
01013
01014 $this->_calcItemsPrice();
01015
01016
01017 $this->_calcBasketDiscount();
01018
01019
01020 $this->_calcBasketTotalDiscount();
01021
01022
01023 $this->_calcVoucherDiscount();
01024
01025
01026 $this->_applyDiscounts();
01027
01028
01029
01030 $this->setCost( 'oxdelivery', $this->_calcDeliveryCost() );
01031
01032
01033 $this->setCost( 'oxwrapping', $this->_calcBasketWrapping() );
01034
01035
01036 $this->setCost( 'oxpayment', $this->_calcPaymentCost() );
01037
01038
01039 $this->_calcTotalPrice();
01040
01041
01042 $this->_setDeprecatedValues();
01043
01044
01045 $this->afterUpdate();
01046 }
01047
01053 public function onUpdate()
01054 {
01055 $this->_blUpdateNeeded = true;
01056 }
01057
01063 public function afterUpdate()
01064 {
01065 $this->_blUpdateNeeded = false;
01066 }
01067
01075 public function getBasketSummary()
01076 {
01077 if ( $this->_blUpdateNeeded || $this->_aBasketSummary === null ) {
01078 $this->_aBasketSummary = new Oxstdclass();
01079 $this->_aBasketSummary->aArticles = array();
01080 $this->_aBasketSummary->aCategories = array();
01081 $this->_aBasketSummary->iArticleCount = 0;
01082 $this->_aBasketSummary->dArticlePrice = 0;
01083 }
01084
01085 if ( !$this->isEnabled() ) {
01086 return $this->_aBasketSummary;
01087 }
01088
01089 $myConfig = $this->getConfig();
01090 foreach ( $this->_aBasketContents as $oBasketItem ) {
01091 if ( !$oBasketItem->isBundle() ) {
01092 if ( ( $oArticle = $oBasketItem->getArticle() ) ) {
01093
01094 $aCatIds = $oArticle->getCategoryIds();
01095
01096 $dPrice = 0;
01097 if ( $oArticle->getPrice() != null ) {
01098 $dPrice = $oArticle->getPrice()->getBruttoPrice();
01099 }
01100
01101 foreach ( $aCatIds as $sCatId ) {
01102 if ( !isset( $this->_aBasketSummary->aCategories[$sCatId] ) ) {
01103 $this->_aBasketSummary->aCategories[$sCatId] = new Oxstdclass();
01104 }
01105
01106 $this->_aBasketSummary->aCategories[$sCatId]->dPrice += $dPrice * $oBasketItem->getAmount();
01107 $this->_aBasketSummary->aCategories[$sCatId]->dAmount += $oBasketItem->getAmount();
01108 $this->_aBasketSummary->aCategories[$sCatId]->iCount++;
01109 }
01110
01111
01112 if ( $oArticle->oxarticles__oxparentid->value && $myConfig->getConfigParam( 'blVariantParentBuyable' ) ) {
01113 if ( !isset( $this->_aBasketSummary->aArticles[$oArticle->oxarticles__oxparentid->value] ) ) {
01114 $this->_aBasketSummary->aArticles[$oArticle->oxarticles__oxparentid->value] = 0;
01115 }
01116 $this->_aBasketSummary->aArticles[$oArticle->oxarticles__oxparentid->value] += $oBasketItem->getAmount();
01117 }
01118
01119 if ( !isset( $this->_aBasketSummary->aArticles[$oBasketItem->getProductId()] ) ) {
01120 $this->_aBasketSummary->aArticles[$oBasketItem->getProductId()] = 0;
01121 }
01122
01123 $this->_aBasketSummary->aArticles[$oBasketItem->getProductId()] += $oBasketItem->getAmount();
01124 $this->_aBasketSummary->iArticleCount += $oBasketItem->getAmount();
01125 $this->_aBasketSummary->dArticlePrice += $dPrice * $oBasketItem->getAmount();
01126 }
01127 }
01128 }
01129 return $this->_aBasketSummary;
01130 }
01131
01143 public function addVoucher( $sVoucherId )
01144 {
01145
01146
01147 $dPrice = 0;
01148 if ( $this->_oDiscountProductsPriceList ) {
01149 $dPrice = $this->_oDiscountProductsPriceList->getBruttoSum();
01150 }
01151
01152 try {
01153
01154 $oVoucher = oxNew( 'oxvoucher' );
01155
01156 if ( !$this->_blSkipVouchersAvailabilityChecking ) {
01157 $oVoucher->getVoucherByNr( $sVoucherId, $this->_aVouchers, true );
01158 $oVoucher->checkVoucherAvailability( $this->_aVouchers, $dPrice );
01159 $oVoucher->checkUserAvailability( $this->getBasketUser() );
01160 $oVoucher->markAsReserved();
01161 } else {
01162 $oVoucher->load( $sVoucherId );
01163 }
01164
01165
01166 $this->_aVouchers[$oVoucher->oxvouchers__oxid->value] = $oVoucher->getSimpleVoucher();
01167 } catch ( oxVoucherException $oEx ) {
01168
01169
01170 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
01171 }
01172
01173 $this->onUpdate();
01174 }
01175
01183 public function removeVoucher( $sVoucherId )
01184 {
01185
01186 if ( isset( $this->_aVouchers[$sVoucherId] ) ) {
01187
01188 $oVoucher = oxNew( 'oxvoucher' );
01189 $oVoucher->load( $sVoucherId );
01190
01191 $oVoucher->unMarkAsReserved();
01192
01193
01194 unset( $this->_aVouchers[$sVoucherId] );
01195 $this->onUpdate();
01196 }
01197
01198 }
01199
01205 public function resetUserInfo()
01206 {
01207 $this->setPayment( null );
01208 $this->setShipping( null );
01209 }
01210
01218 protected function _setDeprecatedValues()
01219 {
01220
01221 $this->dproductsprice = $this->_oProductsPriceList->getBruttoSum();
01222 $this->dproductsnetprice = $this->getDiscountedNettoPrice();
01223
01224
01225 $this->dVAT = array_sum( $this->_oProductsPriceList->getVatInfo() );
01226 $oLang = oxLang::getInstance();
01227
01228
01229 $this->fproductsprice = $this->getFProductsPrice();
01230 $this->fproductsnetprice = $this->getProductsNetPrice();
01231 $this->fVAT = $oLang->formatCurrency( $this->dVAT, $this->getBasketCurrency());
01232
01233
01234 if ( $oDeliveryCost = $this->getCosts( 'oxdelivery' ) ) {
01235
01236 $this->ddeliverycost = $oDeliveryCost->getBruttoPrice();
01237 $this->ddeliverynetcost = $oDeliveryCost->getNettoPrice();
01238 $this->dDelVAT = $oDeliveryCost->getVatValue();
01239 $this->fDelVATPercent = $oDeliveryCost->getVat() / 100;
01240
01241
01242 $this->fdeliverycost = $oLang->formatCurrency( $this->ddeliverycost, $this->getBasketCurrency() );
01243 $this->fdeliverynetcost = $oLang->formatCurrency( $this->ddeliverynetcost, $this->getBasketCurrency() );
01244 $this->fDelVAT = $this->getDelCostVat();
01245 }
01246
01247
01248
01249 if ( $oWrappingCost = $this->getCosts( 'oxwrapping' ) ) {
01250
01251 $this->dWrappingPrice = $oWrappingCost->getBruttoPrice();
01252 $this->dWrappingNetto = $oWrappingCost->getNettoPrice();
01253 $this->dWrappingVAT = $oWrappingCost->getVatValue();
01254
01255
01256 $this->fWrappingPrice = $oLang->formatCurrency( $this->dWrappingPrice, $this->getBasketCurrency() );
01257 $this->fWrappingNetto = $this->getWrappCostNet();
01258 $this->fWrappingVAT = $this->getWrappCostVat();
01259 $this->fWrappingVATPercent = $this->getWrappCostVatPercent();
01260 }
01261
01262
01263
01264 if ( $oPaymentCost = $this->getCosts( 'oxpayment' ) ) {
01265
01266 $this->dAddPaymentSum = $this->getPaymentCosts();
01267 $this->dAddPaymentSumVAT = $oPaymentCost->getVatValue();
01268
01269
01270 $this->fAddPaymentSum = $oLang->formatCurrency( $this->dAddPaymentSum, $this->getBasketCurrency() );
01271 $this->fAddPaymentSumVAT = $this->getPayCostVat();
01272 $this->fAddPaymentSumVATPercent = $this->getPayCostVatPercent();
01273 $this->fAddPaymentNetSum = $this->getPayCostNet();
01274 }
01275
01276
01277
01278 $this->dprice = $this->_oPrice->getBruttoPrice();
01279 $this->fprice = $oLang->formatCurrency( $this->dprice, $this->getBasketCurrency() );
01280
01281
01282 $this->iCntProducts = $this->getProductsCount();
01283 $this->dCntItems = $this->getItemsCount();
01284 $this->aVATs = $this->getProductVats();
01285 $this->aBasketContents = $this->getContents();
01286
01287
01288 $this->giftmessage = $this->getCardMessage();
01289 $this->chosencard = $this->getCardId();
01290
01291 $this->oCard = $this->getCard();
01292
01293
01294
01295 $this->aDiscounts = $this->getDiscounts();
01296 if ( count($this->aDiscounts) > 0 ) {
01297 foreach ($this->aDiscounts as $oDiscount) {
01298 $oDiscount->fDiscount = $oLang->formatCurrency( $oDiscount->dDiscount, $this->getBasketCurrency() );
01299 }
01300 }
01301 $this->dDiscount = $this->getTotalDiscount()->getBruttoPrice();
01302
01303
01304 $this->aVouchers = $this->getVouchers();
01305 $this->dVoucherDiscount = $this->getVoucherDiscValue();
01306 $this->fVoucherDiscount = $oLang->formatCurrency( $this->dVoucherDiscount, $this->getBasketCurrency() );
01307 $this->dSkippedDiscount = $this->hasSkipedDiscount();
01308
01309 }
01310
01311
01317 protected function _canMergeBasket()
01318 {
01319 $blCan = true;
01320 if ( $this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' ) ||
01321 $this->_blBasketMerged || $this->isAdmin() ) {
01322 $blCan = false;
01323 }
01324 return $blCan;
01325 }
01326
01333 protected function _mergeSavedBasket()
01334 {
01335 if ( $this->_canMergeBasket() ) {
01336
01337 $oUser = $this->getBasketUser();
01338 if ( !$oUser ) {
01339 $this->_blBasketMerged = false;
01340 return;
01341 }
01342
01343 $oBasket = $oUser->getBasket( 'savedbasket' );
01344
01345
01346 $aSavedItems = $oBasket->getItems();
01347 foreach ( $aSavedItems as $oItem ) {
01348 try {
01349 $this->addToBasket( $oItem->oxuserbasketitems__oxartid->value, $oItem->oxuserbasketitems__oxamount->value, $oItem->getSelList(), null, true );
01350 } catch( oxArticleException $oEx ) {
01351
01352 }
01353 }
01354
01355
01356 foreach ( $this->_aBasketContents as $oBasketItem ) {
01357 $oBasket->addItemToBasket( $oBasketItem->getProductId(), $oBasketItem->getAmount(), $oBasketItem->getSelList(), true );
01358 }
01359
01360
01361 $this->_blBasketMerged = true;
01362 }
01363 }
01364
01375 protected function _addItemToSavedBasket( $sProductId , $dAmount, $aSel, $blOverride = false )
01376 {
01377
01378 if ( $oUser = $this->getBasketUser() ) {
01379 $oUser->getBasket( 'savedbasket' )->addItemToBasket( $sProductId, $dAmount, $aSel, $blOverride );
01380 }
01381 }
01382
01390 protected function _deleteSavedBasket()
01391 {
01392
01393 if ( $oUser = $this->getBasketUser() ) {
01394 $oUser->getBasket( 'savedbasket' )->delete();
01395 }
01396 }
01397
01403 protected function _findDelivCountry()
01404 {
01405 $myConfig = $this->getConfig();
01406 $oUser = $this->getBasketUser();
01407
01408 $sDelivCountry = null;
01409 if ( !$oUser ) {
01410
01411 $aHomeCountry = $myConfig->getConfigParam( 'aHomeCountry' );
01412 if ( $myConfig->getConfigParam( 'blCalculateDelCostIfNotLoggedIn' ) && is_array( $aHomeCountry ) ) {
01413 $sDelivCountry = current( $aHomeCountry );
01414 }
01415 } else {
01416
01417 if ( $sCountryId = $myConfig->getGlobalParameter( 'delcountryid' ) ) {
01418 $sDelivCountry = $sCountryId;
01419 } elseif ( $sAddressId = oxConfig::getParameter( 'deladrid' ) ) {
01420
01421 $oDelAdress = oxNew( 'oxbase' );
01422 $oDelAdress->init( 'oxaddress' );
01423 if ( $oDelAdress->load( $sAddressId ) ) {
01424 $sDelivCountry = $oDelAdress->oxaddress__oxcountryid->value;
01425 }
01426 }
01427
01428
01429 if ( !$sDelivCountry ) {
01430 $sDelivCountry = $oUser->oxuser__oxcountryid->value;
01431 }
01432 }
01433
01434 return $sDelivCountry;
01435 }
01436
01442 public function deleteBasket()
01443 {
01444 $this->getSession()->delBasket();
01445
01446
01447 if ( !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' ) ) {
01448 $this->_deleteSavedBasket();
01449 }
01450 }
01451
01459 public function setPayment( $sPaymentId = null )
01460 {
01461 $this->_sPaymentId = $sPaymentId;
01462 }
01463
01469 public function getPaymentId()
01470 {
01471 if ( !$this->_sPaymentId ) {
01472 $this->_sPaymentId = oxConfig::getParameter( 'paymentid' );
01473 }
01474 return $this->_sPaymentId;
01475 }
01476
01484 public function setShipping( $sShippingSetId = null )
01485 {
01486 $this->_sShippingSetId = $sShippingSetId;
01487 oxSession::setVar( 'sShipSet', $sShippingSetId );
01488 }
01489
01497 public function setDeliveryPrice( $oShippingPrice = null )
01498 {
01499 $this->_oDeliveryPrice = $oShippingPrice;
01500 }
01501
01507 public function getShippingId()
01508 {
01509 if ( !$this->_sShippingSetId ) {
01510 $this->_sShippingSetId = oxConfig::getParameter( 'sShipSet' );
01511 }
01512
01513
01514 if ( !$this->_sShippingSetId && $this->getPaymentId() != 'oxempty' ) {
01515 $this->_sShippingSetId = 'oxidstandard';
01516 }
01517
01518 return $this->_sShippingSetId;
01519 }
01520
01526 public function getBasketArticles()
01527 {
01528 $aBasketArticles = array();
01529 foreach ( $this->_aBasketContents as $sItemKey => $oBasketItem ) {
01530
01531 $oProduct = $oBasketItem->getArticle();
01532
01533 if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) ) {
01534
01535 $aSelList = $oBasketItem->getSelList();
01536 if ( is_array( $aSelList ) && ( $aSelectlist = $oProduct->getSelectLists( $sItemKey ) ) ) {
01537 reset( $aSelList );
01538 while ( list( $conkey, $iSel ) = each( $aSelList ) ) {
01539 $aSelectlist[$conkey][$iSel]->selected = 1;
01540 }
01541 $oProduct->setSelectlist( $aSelectlist );
01542 }
01543 }
01544
01545 $aBasketArticles[$sItemKey] = $oProduct;
01546 }
01547 return $aBasketArticles;
01548 }
01549
01555 public function getDiscountProductsPrice()
01556 {
01557 return $this->_oDiscountProductsPriceList;
01558 }
01559
01565 public function getProductsPrice()
01566 {
01567 if ( is_null($this->_oProductsPriceList) ) {
01568 $this->_oProductsPriceList = oxNew( 'oxPriceList' );
01569 }
01570
01571 return $this->_oProductsPriceList;
01572 }
01573
01579 public function getPrice()
01580 {
01581 if ( is_null($this->_oPrice) ) {
01582 $this->_oPrice = oxNew( 'oxprice' );
01583 }
01584
01585 return $this->_oPrice;
01586 }
01587
01594 public function getOrderId()
01595 {
01596 return $this->_sOrderId;
01597 }
01598
01606 public function setOrderId( $sId )
01607 {
01608 $this->_sOrderId = $sId;
01609 }
01610
01619 public function getCosts( $sId = null )
01620 {
01621
01622 if ( $sId ) {
01623 return isset( $this->_aCosts[$sId] )?$this->_aCosts[$sId]:null;
01624 }
01625 return $this->_aCosts;
01626 }
01627
01633 public function getVouchers()
01634 {
01635 return $this->_aVouchers;
01636 }
01637
01643 public function getProductsCount()
01644 {
01645 return $this->_iProductsCnt;
01646 }
01647
01653 public function getItemsCount()
01654 {
01655 return $this->_dItemsCnt;
01656 }
01657
01663 public function getWeight()
01664 {
01665 return $this->_dWeight;
01666 }
01667
01673 public function getContents()
01674 {
01675 return $this->_aBasketContents;
01676 }
01677
01683 public function getProductVats()
01684 {
01685 if ( !$this->_oNotDiscountedProductsPriceList ) {
01686 return array();
01687 }
01688
01689 $aVats = array();
01690
01691 $aAllVats = $this->_oNotDiscountedProductsPriceList->getVatInfo();
01692
01693 $oUtils = oxUtils::getInstance();
01694 foreach ( $this->_aDiscountedVats as $sKey => $dVat ) {
01695
01696 if ( array_key_exists ($sKey, $aAllVats) ) {
01697 $aAllVats[$sKey] += $oUtils->fRound( $dVat, $this->_oCurrency);
01698 } else {
01699 $aAllVats[$sKey] = $dVat;
01700 }
01701 }
01702
01703 $oLang = oxLang::getInstance();
01704 foreach ( $aAllVats as $sKey => $dVat ) {
01705 $aVats[$sKey] = $oLang->formatCurrency( $dVat, $this->getBasketCurrency() );
01706 }
01707
01708 return $aVats;
01709 }
01710
01716 public function getDiscountedNettoPrice()
01717 {
01718 if ( $this->_oNotDiscountedProductsPriceList ) {
01719 return $this->_dDiscountedProductNettoPrice + $this->_oNotDiscountedProductsPriceList->getNettoSum();
01720 }
01721 return false;
01722 }
01723
01731 public function setCardMessage( $sMessage )
01732 {
01733 $this->_sCardMessage = $sMessage;
01734 }
01735
01741 public function getCardMessage()
01742 {
01743 return $this->_sCardMessage;
01744 }
01745
01753 public function setCardId( $sCardId )
01754 {
01755 $this->_sCardId = $sCardId;
01756 }
01757
01763 public function getCardId()
01764 {
01765 return $this->_sCardId;
01766 }
01767
01773 public function getCard()
01774 {
01775 $oCard = null;
01776 if ( $sCardId = $this->getCardId() ) {
01777 $oCard = oxNew( 'oxwrapping' );
01778 $oCard->load( $sCardId );
01779 }
01780 return $oCard;
01781 }
01782
01788 public function getTotalDiscount()
01789 {
01790 return $this->_oTotalDiscount;
01791 }
01792
01798 public function getDiscounts()
01799 {
01800 if ( $this->getTotalDiscount() && $this->getTotalDiscount()->getBruttoPrice() == 0 && count($this->_aItemDiscounts) == 0) {
01801 return null;
01802 }
01803
01804 return array_merge($this->_aItemDiscounts, $this->_aDiscounts);
01805 }
01806
01812 public function getVoucherDiscount()
01813 {
01814 return $this->_oVoucherDiscount;
01815 }
01816
01824 public function setBasketCurrency( $oCurrency )
01825 {
01826 $this->_oCurrency = $oCurrency;
01827 }
01828
01834 public function getBasketCurrency()
01835 {
01836 if ( $this->_oCurrency === null ) {
01837 $this->_oCurrency = $this->getConfig()->getActShopCurrencyObject();
01838 }
01839
01840 return $this->_oCurrency;
01841 }
01842
01850 public function setSkipVouchersChecking( $blSkipChecking = null )
01851 {
01852 $this->_blSkipVouchersAvailabilityChecking = $blSkipChecking;
01853 }
01854
01860 public function hasSkipedDiscount()
01861 {
01862 return $this->_blSkipDiscounts;
01863 }
01864
01872 public function setSkipDiscounts( $blSkip )
01873 {
01874 $this->_blSkipDiscounts = $blSkip;
01875 }
01876
01882 public function getProductsNetPrice()
01883 {
01884 return oxLang::getInstance()->formatCurrency( $this->getDiscountedNettoPrice(), $this->getBasketCurrency() );
01885 }
01886
01892 public function getFProductsPrice()
01893 {
01894 if ( $this->_oProductsPriceList ) {
01895 return oxLang::getInstance()->formatCurrency( $this->_oProductsPriceList->getBruttoSum(), $this->getBasketCurrency() );
01896 }
01897 return null;
01898 }
01899
01905 public function getDelCostVatPercent()
01906 {
01907 return $this->getCosts( 'oxdelivery' )->getVat();
01908 }
01909
01915 public function getDelCostVat()
01916 {
01917 $dDelVAT = $this->getCosts( 'oxdelivery' )->getVatValue();
01918 if ( $dDelVAT > 0 ) {
01919 return oxLang::getInstance()->formatCurrency( $dDelVAT, $this->getBasketCurrency() );
01920 }
01921 return false;
01922 }
01923
01929 public function getDelCostNet()
01930 {
01931 return oxLang::getInstance()->formatCurrency( $this->getCosts( 'oxdelivery' )->getNettoPrice(), $this->getBasketCurrency() );
01932 }
01933
01939 public function getPayCostVatPercent()
01940 {
01941 return $this->getCosts( 'oxpayment' )->getVat();
01942 }
01943
01949 public function getPayCostVat()
01950 {
01951 $dPayVAT = $this->getCosts( 'oxpayment' )->getVatValue();
01952 if ( $dPayVAT > 0 ) {
01953 return oxLang::getInstance()->formatCurrency( $dPayVAT, $this->getBasketCurrency() );
01954 }
01955 return false;
01956 }
01957
01963 public function getPayCostNet()
01964 {
01965 return oxLang::getInstance()->formatCurrency( $this->getCosts( 'oxpayment' )->getNettoPrice(), $this->getBasketCurrency() );
01966 }
01967
01973 public function getPaymentCosts()
01974 {
01975 return $this->getCosts( 'oxpayment' )->getBruttoPrice();
01976 }
01977
01983 public function getVoucherDiscValue()
01984 {
01985 if ( $this->getVoucherDiscount() ) {
01986 return $this->getVoucherDiscount()->getBruttoPrice();
01987 }
01988 return false;
01989 }
01990
01996 public function getWrappCostVatPercent()
01997 {
01998 return $this->getCosts( 'oxwrapping' )->getVat();
01999 }
02000
02006 public function getWrappCostVat()
02007 {
02008 $dWrappVAT = $this->getCosts( 'oxwrapping' )->getVatValue();
02009 if ( $dWrappVAT > 0 ) {
02010 return oxLang::getInstance()->formatCurrency( $dWrappVAT, $this->getBasketCurrency() );
02011 }
02012 return false;
02013
02014 }
02015
02021 public function getWrappCostNet()
02022 {
02023 $dWrappNet = $this->getCosts( 'oxwrapping' )->getNettoPrice();
02024 if ( $dWrappNet > 0 ) {
02025 return oxLang::getInstance()->formatCurrency( $dWrappNet, $this->getBasketCurrency() );
02026 }
02027 return false;
02028 }
02029
02035 public function getFPrice()
02036 {
02037 return oxLang::getInstance()->formatCurrency( $this->getPrice()->getBruttoPrice(), $this->getBasketCurrency() );
02038 }
02039
02045 public function getFDeliveryCosts()
02046 {
02047 $oDeliveryCost = $this->getCosts( 'oxdelivery' );
02048 if ( $oDeliveryCost && $oDeliveryCost->getBruttoPrice()) {
02049 return oxLang::getInstance()->formatCurrency( $oDeliveryCost->getBruttoPrice(), $this->getBasketCurrency() );
02050 }
02051 return false;
02052 }
02053
02059 public function getDeliveryCosts()
02060 {
02061 if ( $oDeliveryCost = $this->getCosts( 'oxdelivery' ) ) {
02062 return $oDeliveryCost->getBruttoPrice();
02063 }
02064 return false;
02065 }
02066
02074 public function setTotalDiscount( $dDiscount )
02075 {
02076 $this->_oTotalDiscount = oxNew( 'oxPrice' );
02077 $this->_oTotalDiscount->setBruttoPriceMode();
02078 $this->_oTotalDiscount->add( $dDiscount );
02079 }
02080
02081 }