00001 <?php
00002
00007 class oxOrder extends oxBase
00008 {
00009
00014 const ORDER_STATE_MAILINGERROR = 0;
00015
00020 const ORDER_STATE_OK = 1;
00021
00026 const ORDER_STATE_PAYMENTERROR = 2;
00027
00032 const ORDER_STATE_ORDEREXISTS = 3;
00033
00038 const ORDER_STATE_INVALIDDELIVERY = 4;
00039
00044 const ORDER_STATE_INVALIDPAYMENT = 5;
00045
00051 protected $_aSkipSaveFields = array( 'oxorderdate' );
00052
00058 protected $_oArticles = null;
00059
00065 protected $_oDelSet = null;
00066
00072 protected $_oGiftCard = null;
00073
00079 protected $_oPaymentType = null;
00080
00086 protected $_oPayment = null;
00087
00093 protected $_aVoucherList = null;
00094
00100 protected $_oDelPrice = null;
00101
00107 protected $_oUser = null;
00108
00114 protected $_oBasket = null;
00115
00121 protected $_oWrappingPrice = null;
00122
00128 protected $_oPaymentPrice = null;
00129
00135 protected $_sClassName = 'oxorder';
00136
00142 protected $_blSeparateNumbering = null;
00143
00149 protected $_iOrderLang = null;
00150
00156 protected $_blReloadDelivery = true;
00157
00163 protected $_blReloadDiscount = true;
00164
00170 protected $_oOrderCurrency = null;
00171
00175 public function __construct()
00176 {
00177 parent::__construct();
00178 $this->init( 'oxorder' );
00179
00180
00181 $this->setSeparateNumbering( $this->getConfig()->getConfigParam( 'blSeparateNumbering') );
00182
00183 }
00184
00192 public function __get( $sName )
00193 {
00194 if ( $sName == 'oDelSet' ) {
00195 return $this->getDelSet();
00196 }
00197
00198 if ( $sName == 'oxorder__oxbillcountry' ) {
00199 return $this->getBillCountry();
00200 }
00201
00202 if ( $sName == 'oxorder__oxdelcountry' ) {
00203 return $this->getDelCountry();
00204 }
00205 }
00206
00214 public function assign( $dbRecord )
00215 {
00216
00217 parent::assign( $dbRecord );
00218
00219 $oUtilsDate = oxUtilsDate::getInstance();
00220
00221
00222 $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value));
00223 $this->oxorder__oxsenddate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value));
00224
00225
00226 $this->_setDeprecatedValues();
00227 }
00228
00236 protected function _getCountryTitle( $sCountryId )
00237 {
00238 $sTitle = null;
00239 if ( $sCountryId && $sCountryId != '-1' ) {
00240 $oCountry = oxNew( 'oxcountry' );
00241 $oCountry->load( $sCountryId );
00242 $sTitle = $oCountry->oxcountry__oxtitle->value;
00243 }
00244
00245 return $sTitle;
00246 }
00247
00255 public function getOrderArticles( $blExcludeCanceled = false )
00256 {
00257
00258 if ( $this->_oArticles === null ) {
00259 $blExcludeState = $blExcludeCanceled;
00260 $sTable = getViewName( "oxorderarticles" );
00261 $sSelect = "select {$sTable}.* from {$sTable}
00262 where {$sTable}.oxorderid = '".$this->getId() . "'" .
00263 ( $blExcludeCanceled ? " and {$sTable}.oxstorno != 1 ": " " ) ."
00264 order by {$sTable}.oxartid";
00265
00266
00267 $oArticles = oxNew( 'oxlist' );
00268 $oArticles->init( 'oxorderarticle' );
00269 $oArticles->selectString( $sSelect );
00270
00271
00272 return $oArticles;
00273 }
00274 return $this->_oArticles;
00275 }
00276
00284 public function setOrderArticleList( $aOrderArticleList )
00285 {
00286 $this->_oArticles = $aOrderArticleList;
00287 }
00288
00294 public function getOrderDeliveryPrice()
00295 {
00296 if ( $this->_oDelPrice != null ) {
00297 return $this->_oDelPrice;
00298 }
00299
00300 $this->_oDelPrice = oxNew( 'oxprice' );
00301 $this->_oDelPrice->setBruttoPriceMode();
00302 $this->_oDelPrice->setPrice( $this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value );
00303 return $this->_oDelPrice;
00304 }
00305
00311 public function getOrderWrappingPrice()
00312 {
00313 if ( $this->_oWrappingPrice != null ) {
00314 return $this->_oWrappingPrice;
00315 }
00316
00317 $this->_oWrappingPrice = oxNew( 'oxprice' );
00318 $this->_oWrappingPrice->setBruttoPriceMode();
00319 $this->_oWrappingPrice->setPrice( $this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value );
00320 return $this->_oWrappingPrice;
00321 }
00322
00328 public function getOrderPaymentPrice()
00329 {
00330 if ( $this->_oPaymentPrice != null ) {
00331 return $this->_oPaymentPrice;
00332 }
00333
00334 $this->_oPaymentPrice = oxNew( 'oxprice' );
00335 $this->_oPaymentPrice->setBruttoPriceMode();
00336 $this->_oPaymentPrice->setPrice( $this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value );
00337 return $this->_oPaymentPrice;
00338 }
00339
00346 public function getOrderNetSum()
00347 {
00348 $dTotalNetSum = 0;
00349
00350 $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
00351 $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
00352 $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
00353 $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
00354
00355 return $dTotalNetSum;
00356 }
00357
00378 public function finalizeOrder( oxBasket $oBasket, $oUser, $blRecalculatingOrder = false )
00379 {
00380
00381 $sGetChallenge = oxSession::getVar( 'sess_challenge' );
00382 if ( $this->_checkOrderExist( $sGetChallenge ) ) {
00383 oxUtils::getInstance()->logger( 'BLOCKER' );
00384
00385 return self::ORDER_STATE_ORDEREXISTS;
00386 }
00387
00388
00389 if ( !$blRecalculatingOrder ) {
00390
00391 $this->setId( $sGetChallenge );
00392
00393
00394 if ( $iOrderState = $this->validateOrder( $oBasket, $oUser ) ) {
00395 return $iOrderState;
00396 }
00397 }
00398
00399
00400 $this->_setUser( $oUser );
00401
00402
00403 $this->_loadFromBasket( $oBasket );
00404
00405
00406 $oUserPayment = $this->_setPayment( $oBasket->getPaymentId() );
00407
00408
00409
00410 if ( !$blRecalculatingOrder ) {
00411 $this->_setFolder();
00412 }
00413
00414
00415 $this->save();
00416
00417
00418
00419 if ( !$blRecalculatingOrder ) {
00420 $blRet = $this->_executePayment( $oBasket, $oUserPayment );
00421 if ( $blRet !== true ) {
00422 return $blRet;
00423 }
00424 }
00425
00426
00427 oxSession::deleteVar( 'ordrem' );
00428
00429
00430 $this->_setOrderStatus( 'OK' );
00431
00432
00433 $oBasket->setOrderId( $this->getId() );
00434
00435
00436 $this->_updateWishlist( $oBasket->getContents(), $oUser );
00437
00438
00439 $this->_updateNoticeList( $oBasket->getContents(), $oUser );
00440
00441
00442
00443 if ( !$blRecalculatingOrder ) {
00444 $this->_markVouchers( $oBasket, $oUser );
00445 }
00446
00447
00448
00449 if ( !$blRecalculatingOrder ) {
00450 $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
00451 } else {
00452 $iRet = self::ORDER_STATE_OK;
00453 }
00454
00455 return $iRet;
00456 }
00457
00465 protected function _setOrderStatus( $sStatus )
00466 {
00467 $oDb = oxDb::getDb();
00468 $sQ = 'update oxorder set oxtransstatus='.$oDb->quote( $sStatus ).' where oxid="'.$this->getId().'" ';
00469 $oDb->execute( $sQ );
00470
00471
00472 $this->oxorder__oxtransstatus = new oxField( $sStatus, oxField::T_RAW );
00473 }
00474
00485 protected function _loadFromBasket( oxBasket $oBasket )
00486 {
00487 $myConfig = $this->getConfig();
00488
00489
00490 if ( $myConfig->getConfigParam( 'blStoreIPs' ) && $this->oxorder__oxip->value === null ) {
00491 $this->oxorder__oxip = new oxField(oxUtilsServer::getInstance()->getRemoteAddress(), oxField::T_RAW);
00492 }
00493
00494
00495 $this->oxorder__oxtotalnetsum = new oxField(oxUtils::getInstance()->fRound($oBasket->getProductsPrice()->getNettoSum()), oxField::T_RAW);
00496 $this->oxorder__oxtotalbrutsum = new oxField($oBasket->getProductsPrice()->getBruttoSum(), oxField::T_RAW);
00497 $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00498
00499
00500 if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00501 $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00502 $this->oxorder__oxpayvat = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00503 }
00504
00505
00506 if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00507 $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00508
00509 $this->oxorder__oxdelvat = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW);
00510 $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00511 }
00512
00513
00514 if ( $this->oxorder__oxremark->value === null ) {
00515 $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00516 }
00517
00518
00519 $oCur = $myConfig->getActShopCurrencyObject();
00520 $this->oxorder__oxcurrency = new oxField($oCur->name);
00521 $this->oxorder__oxcurrate = new oxField($oCur->rate, oxField::T_RAW);
00522
00523
00524 if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00525 $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00526 }
00527
00528
00529 if ( $this->_blReloadDiscount ) {
00530 $dDiscount = 0;
00531 $aDiscounts = $oBasket->getDiscounts();
00532 if ( count($aDiscounts) > 0 ) {
00533 foreach ($aDiscounts as $oDiscount) {
00534 $dDiscount += $oDiscount->dDiscount;
00535 }
00536 }
00537 $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00538 }
00539
00540
00541 $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00542
00543
00544
00545 $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00546
00547
00548 $this->_setOrderArticles( $oBasket->getContents() );
00549
00550
00551 $this->_setWrapping( $oBasket );
00552 }
00553
00560 public function getOrderLanguage()
00561 {
00562 if ( $this->_iOrderLang === null ) {
00563 if ( isset( $this->oxorder__oxlang->value ) ) {
00564 $this->_iOrderLang = oxLang::getInstance()->validateLanguage( $this->oxorder__oxlang->value );
00565 } else {
00566 $this->_iOrderLang = oxLang::getInstance()->getBaseLanguage();
00567 }
00568 }
00569 return $this->_iOrderLang;
00570 }
00571
00579 protected function _setUser( $oUser )
00580 {
00581
00582 $this->oxorder__oxuserid = new oxField($oUser->getId());
00583
00584
00585 $this->oxorder__oxbillcompany = clone $oUser->oxuser__oxcompany;
00586 $this->oxorder__oxbillemail = clone $oUser->oxuser__oxusername;
00587 $this->oxorder__oxbillfname = clone $oUser->oxuser__oxfname;
00588 $this->oxorder__oxbilllname = clone $oUser->oxuser__oxlname;
00589 $this->oxorder__oxbillstreet = clone $oUser->oxuser__oxstreet;
00590 $this->oxorder__oxbillstreetnr = clone $oUser->oxuser__oxstreetnr;
00591 $this->oxorder__oxbilladdinfo = clone $oUser->oxuser__oxaddinfo;
00592 $this->oxorder__oxbillustid = clone $oUser->oxuser__oxustid;
00593 $this->oxorder__oxbillcity = clone $oUser->oxuser__oxcity;
00594 $this->oxorder__oxbillcountryid = clone $oUser->oxuser__oxcountryid;
00595 $this->oxorder__oxbillzip = clone $oUser->oxuser__oxzip;
00596 $this->oxorder__oxbillfon = clone $oUser->oxuser__oxfon;
00597 $this->oxorder__oxbillfax = clone $oUser->oxuser__oxfax;
00598 $this->oxorder__oxbillsal = clone $oUser->oxuser__oxsal;
00599
00600
00601
00602 if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00603
00604 $this->oxorder__oxdelcompany = clone $oDelAdress->oxaddress__oxcompany;
00605 $this->oxorder__oxdelfname = clone $oDelAdress->oxaddress__oxfname;
00606 $this->oxorder__oxdellname = clone $oDelAdress->oxaddress__oxlname;
00607 $this->oxorder__oxdelstreet = clone $oDelAdress->oxaddress__oxstreet;
00608 $this->oxorder__oxdelstreetnr = clone $oDelAdress->oxaddress__oxstreetnr;
00609 $this->oxorder__oxdeladdinfo = clone $oDelAdress->oxaddress__oxaddinfo;
00610 $this->oxorder__oxdelcity = clone $oDelAdress->oxaddress__oxcity;
00611 $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00612 $this->oxorder__oxdelzip = clone $oDelAdress->oxaddress__oxzip;
00613 $this->oxorder__oxdelfon = clone $oDelAdress->oxaddress__oxfon;
00614 $this->oxorder__oxdelfax = clone $oDelAdress->oxaddress__oxfax;
00615 $this->oxorder__oxdelsal = clone $oDelAdress->oxaddress__oxsal;
00616 }
00617 }
00618
00626 protected function _setWrapping( oxBasket $oBasket )
00627 {
00628 $myConfig = $this->getConfig();
00629
00630
00631 if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00632 $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00633
00634
00635 if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
00636 $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00637 }
00638 }
00639
00640
00641 $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00642
00643
00644 $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00645 }
00646
00655 protected function _setOrderArticles( $aArticleList )
00656 {
00657
00658 $this->_oArticles = oxNew( 'oxlist' );
00659 $iCurrLang = $this->getOrderLanguage();
00660
00661
00662 foreach ( $aArticleList as $oContent ) {
00663
00664
00665
00666 $oProduct = $oContent->getArticle( true, null, true);
00667
00668
00669 if ( $oProduct->isOrderArticle() ) {
00670 $oOrderArticle = $oProduct;
00671 } else {
00672
00673
00674 if ( $iCurrLang != $oProduct->getLanguage() ) {
00675 $oProduct->loadInLang( $iCurrLang, $oProduct->getProductId() );
00676 }
00677
00678
00679 $sSelList = '';
00680 if ( count( $aChosenSelList = $oContent->getChosenSelList() ) ) {
00681 foreach ( $aChosenSelList as $oItem ) {
00682 if ( $sSelList ) {
00683 $sSelList .= ", ";
00684 }
00685 $sSelList .= "{$oItem->name} : {$oItem->value}";
00686 }
00687 }
00688
00689 $oOrderArticle = oxNew( 'oxorderarticle' );
00690 $oOrderArticle->setIsNewOrderItem( true );
00691 $oOrderArticle->copyThis( $oProduct );
00692 $oOrderArticle->setId();
00693
00694 $oOrderArticle->oxorderarticles__oxartnum = clone $oProduct->oxarticles__oxartnum;
00695 $oOrderArticle->oxorderarticles__oxselvariant = new oxField( trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->value ), oxField::T_RAW );
00696 $oOrderArticle->oxorderarticles__oxshortdesc = new oxField( $oProduct->oxarticles__oxshortdesc->value, oxField::T_RAW );
00697
00698 $oOrderArticle->oxorderarticles__oxtitle = new oxField( trim( $oProduct->oxarticles__oxtitle->value ), oxField::T_RAW );
00699
00700
00701 if ( !is_array( $aPersParams = $oProduct->getPersParams() ) ) {
00702 $aPersParams = $oContent->getPersParams();
00703 }
00704 if ( is_array( $aPersParams ) && count( $aPersParams )) {
00705 $oOrderArticle->oxorderarticles__oxpersparam = new oxField( serialize( $aPersParams ), oxField::T_RAW );
00706 }
00707 }
00708
00709
00710 $oOrderArticle->oxorderarticles__oxorderid = new oxField( $this->getId() );
00711 $oOrderArticle->oxorderarticles__oxartid = new oxField( $oContent->getProductId() );
00712 $oOrderArticle->oxorderarticles__oxamount = new oxField( $oContent->getAmount() );
00713
00714
00715 $oPrice = $oContent->getPrice();
00716 $oOrderArticle->oxorderarticles__oxnetprice = new oxField( $oPrice->getNettoPrice(), oxField::T_RAW );
00717 $oOrderArticle->oxorderarticles__oxvatprice = new oxField( $oPrice->getVatValue(), oxField::T_RAW );
00718 $oOrderArticle->oxorderarticles__oxbrutprice = new oxField( $oPrice->getBruttoPrice(), oxField::T_RAW );
00719 $oOrderArticle->oxorderarticles__oxvat = new oxField( $oPrice->getVat(), oxField::T_RAW );
00720
00721 $oUnitPtice = $oContent->getUnitPrice();
00722 $oOrderArticle->oxorderarticles__oxnprice = new oxField( $oUnitPtice->getNettoPrice(), oxField::T_RAW );
00723 $oOrderArticle->oxorderarticles__oxbprice = new oxField( $oUnitPtice->getBruttoPrice(), oxField::T_RAW );
00724
00725
00726 $oOrderArticle->oxorderarticles__oxwrapid = new oxField( $oContent->getWrappingId(), oxField::T_RAW );
00727
00728
00729 $oOrderArticle->oxorderarticles__oxordershopid = new oxField( $oContent->getShopId(), oxField::T_RAW );
00730
00731
00732
00733
00734 $oOrderArticle->oProduct = $oProduct;
00735
00736
00737 $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
00738 }
00739 }
00740
00752 protected function _executePayment( oxBasket $oBasket, $oUserpayment )
00753 {
00754 $oPayTransaction = $this->_getGateway();
00755 $oPayTransaction->setPaymentParams( $oUserpayment );
00756
00757 if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
00758 $this->delete();
00759
00760
00761 if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
00762 if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
00763 return $sLastError;
00764 }
00765 }
00766
00767
00768 if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
00769 if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
00770 return $iLastErrorNo;
00771 }
00772 }
00773
00774 return self::ORDER_STATE_PAYMENTERROR;
00775 }
00776 return true;
00777 }
00778
00785 protected function _getGateway()
00786 {
00787 return oxNew( 'oxPaymentGateway' );
00788 }
00789
00797 protected function _setPayment( $sPaymentid )
00798 {
00799
00800 $aDynvalue = oxSession::getVar( 'dynvalue' );
00801 $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
00802
00803
00804 $oPayment = oxNew( 'oxpayment' );
00805
00806 if (!$oPayment->load( $sPaymentid )) {
00807 return null;
00808 }
00809
00810
00811 if ( !$aDynvalue && ( $oUserpayment = $this->getPaymentType() ) ) {
00812 if ( is_array( $aStoredDynvalue = $oUserpayment->getDynValues() ) ) {
00813 foreach ( $aStoredDynvalue as $oVal ) {
00814 $aDynvalue[$oVal->name] = $oVal->value;
00815 }
00816 }
00817 }
00818
00819 $oPayment->setDynValues( oxUtils::getInstance()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
00820
00821
00822 $aDynVal = array();
00823
00824 if ( is_array( $aPaymentDynValues = $oPayment->getDynValues() ) ) {
00825 foreach ( $aPaymentDynValues as $key => $oVal ) {
00826 if ( isset( $aDynvalue[$oVal->name] ) ) {
00827 $oVal->value = $aDynvalue[$oVal->name];
00828 }
00829
00830
00831 $aPaymentDynValues[$key] = $oVal;
00832 $aDynVal[$oVal->name] = $oVal->value;
00833 }
00834 }
00835
00836
00837
00838
00839 $oUserpayment = oxNew( 'oxuserpayment' );
00840 $oUserpayment->oxuserpayments__oxuserid = clone $this->oxorder__oxuserid;
00841 $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
00842 $oUserpayment->oxuserpayments__oxvalue = new oxField(oxUtils::getInstance()->assignValuesToText( $aDynVal ), oxField::T_RAW);
00843 $oUserpayment->oxpayments__oxdesc = clone $oPayment->oxpayments__oxdesc;
00844 $oUserpayment->setDynValues( $aPaymentDynValues );
00845 $oUserpayment->save();
00846
00847
00848 $this->oxorder__oxpaymentid = new oxField($oUserpayment->getId(), oxField::T_RAW);
00849 $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
00850
00851
00852 return $oUserpayment;
00853 }
00854
00860 protected function _setFolder()
00861 {
00862 $myConfig = $this->getConfig();
00863 $this->oxorder__oxfolder = new oxField(key( $myConfig->getShopConfVar( 'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
00864 }
00865
00875 protected function _updateWishlist( $aArticleList, $oUser )
00876 {
00877
00878 foreach ( $aArticleList as $oContent) {
00879 if ( ( $sWishId = $oContent->getWishId() ) ) {
00880
00881
00882 if ( $sWishId == $oUser->getId() ) {
00883 $oUserBasket = $oUser->getBasket( 'wishlist' );
00884 } else {
00885 $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
00886 $oUserBasket = oxNew( 'oxuserbasket' );
00887 $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
00888 }
00889
00890
00891 if ( $oUserBasket ) {
00892 if ( !($sProdId = $oContent->getWishArticleId() )) {
00893 $sProdId = $oContent->getProductId();
00894 }
00895 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00896 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00897 if ( $dNewAmount < 0) {
00898 $dNewAmount = 0;
00899 }
00900 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00901 }
00902 }
00903 }
00904 }
00905
00915 protected function _updateNoticeList( $aArticleList, $oUser )
00916 {
00917
00918 if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
00919
00920 foreach ( $aArticleList as $oContent) {
00921 $sProdId = $oContent->getProductId();
00922
00923
00924 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00925 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00926 if ( $dNewAmount < 0) {
00927 $dNewAmount = 0;
00928 }
00929 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00930 }
00931 }
00932 }
00933
00939 protected function _updateStock()
00940 {
00941 $myConfig = $this->getConfig();
00942
00943
00944 if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00945 $oOrderArticles = $this->getOrderArticles();
00946 foreach ( $oOrderArticles as $oOrderArticle ) {
00947 if ( $oOrderArticle->isNewOrderItem() ) {
00948 $oOrderArticle->updateArticleStock( $oOrderArticle->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
00949 }
00950 }
00951 }
00952 }
00953
00965 protected function _markVouchers( $oBasket, $oUser )
00966 {
00967 $this->_aVoucherList = $oBasket->getVouchers();
00968
00969 if ( is_array( $this->_aVoucherList ) ) {
00970 foreach ( $this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
00971 $oVoucher = oxNew( 'oxvoucher' );
00972 $oVoucher->load( $sVoucherId );
00973 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->fVoucherdiscount );
00974
00975 $this->_aVoucherList[$sVoucherId] = $oVoucher;
00976 }
00977 }
00978 }
00979
00985 public function save()
00986 {
00987 if ( ( $blSave = parent::save() ) ) {
00988
00989
00990 $oOrderArticles = $this->getOrderArticles();
00991 if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
00992 foreach ( $oOrderArticles as $oOrderArticle ) {
00993 $oOrderArticle->save();
00994 }
00995 }
00996 }
00997
00998 return $blSave;
00999 }
01000
01007 public function getDelAddressInfo()
01008 {
01009 $oDelAdress = null;
01010 if ( ( $soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
01011 $oDelAdress = oxNew( 'oxaddress' );
01012 $oDelAdress->load( $soxAddressId );
01013
01014
01015 if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
01016 $oCountry = oxNew( 'oxcountry' );
01017 $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
01018 $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
01019 }
01020 }
01021 return $oDelAdress;
01022 }
01023
01034 public function validateStock( $oBasket )
01035 {
01036 foreach ( $oBasket->getContents() as $key => $oContent ) {
01037 try {
01038 $oProd = $oContent->getArticle();
01039 } catch ( oxNoArticleException $oEx ) {
01040 $oBasket->removeItem( $key );
01041 throw $oEx;
01042 } catch ( oxArticleInputException $oEx ) {
01043 $oBasket->removeItem( $key );
01044 throw $oEx;
01045 }
01046
01047
01048 $iOnStock = $oProd->checkForStock( $oContent->getAmount() );
01049 if ( $iOnStock !== true ) {
01050 $oEx = oxNew( 'oxOutOfStockException' );
01051 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
01052 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
01053 $oEx->setProductId( $oProd->getId() );
01054 $oEx->setRemainingAmount( $oProd->oxarticles__oxstock->value );
01055 throw $oEx;
01056 }
01057 }
01058 }
01059
01065 protected function _insert()
01066 {
01067 $myConfig = $this->getConfig();
01068 $oUtilsDate = oxUtilsDate::getInstance();
01069
01070
01071 if ( !$this->oxorder__oxorderdate->value ) {
01072 $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
01073 } else {
01074 $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
01075 }
01076 $this->oxorder__oxshopid = new oxField($myConfig->getShopId(), oxField::T_RAW);
01077
01078 $this->oxorder__oxsenddate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01079
01080 if ( ( $blInsert = parent::_insert() ) ) {
01081
01082 if ( !$this->oxorder__oxordernr->value ) {
01083 $aWhere = '';
01084
01085 if ( $this->_blSeparateNumbering ) {
01086 $aWhere = array( 'oxshopid = "'.$myConfig->getShopId().'"' );
01087 }
01088 $this->_setRecordNumber( 'oxordernr', $aWhere );
01089 }
01090 }
01091 return $blInsert;
01092 }
01093
01099 protected function _update()
01100 {
01101 $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01102 return parent::_update();
01103 }
01104
01113 public function delete( $sOxId = null )
01114 {
01115 if ( $sOxId ) {
01116 if ( !$this->load( $sOxId ) ) {
01117
01118 return false;
01119 }
01120 } elseif ( !$sOxId ) {
01121 $sOxId = $this->getId();
01122 }
01123
01124
01125 if ( !$sOxId ) {
01126 return false;
01127 }
01128
01129
01130
01131 $oOrderArticles = $this->getOrderArticles( false );
01132 foreach ( $oOrderArticles as $oOrderArticle ) {
01133 $oOrderArticle->delete();
01134 }
01135
01136
01137 if ( $oPaymentType = $this->getPaymentType() ) {
01138 $oPaymentType->delete();
01139 }
01140
01141 return parent::delete( $sOxId );
01142 }
01143
01153 public function recalculateOrder( $aNewArticles = array() )
01154 {
01155 oxDb::startTransaction();
01156
01157 try {
01158 $oBasket = $this->_getOrderBasket();
01159
01160
01161 $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles( true ) );
01162
01163
01164 $this->_addArticlesToBasket( $oBasket, $aNewArticles );
01165
01166
01167 $oBasket->calculateBasket( true );
01168
01169
01170 $iRet = $this->finalizeOrder( $oBasket, $this->getOrderUser(), true );
01171
01172
01173 if ( $iRet !== 1 ) {
01174 oxDb::rollbackTransaction();
01175 } else {
01176 oxDb::commitTransaction();
01177 }
01178
01179 } catch( Exception $oE ) {
01180
01181 oxDb::rollbackTransaction();
01182 }
01183 }
01184
01185 protected $_oOrderBasket = null;
01193 protected function _getOrderBasket( $blStockCheck = true )
01194 {
01195 $this->_oOrderBasket = oxNew( "oxbasket" );
01196
01197
01198 $this->_oOrderBasket->setStockCheckMode( $blStockCheck );
01199
01200
01201 $this->_oOrderBasket->setBasketUser( $this->getOrderUser() );
01202
01203
01204 $this->_oOrderBasket->setOrderId( $this->getId() );
01205
01206
01207 $aCurrencies = $this->getConfig()->getCurrencyArray();
01208 foreach ( $aCurrencies as $oCur ) {
01209 if ($oCur->name == $this->oxorder__oxcurrency->value) {
01210 $oBasketCur = $oCur;
01211 break;
01212 }
01213 }
01214
01215
01216 $this->_oOrderBasket->setBasketCurrency( $oBasketCur );
01217
01218
01219 $this->_oOrderBasket->setCardId( $this->oxorder__oxcardid->value );
01220 $this->_oOrderBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01221
01222 if ( $this->_blReloadDiscount ) {
01223
01224 $this->_oOrderBasket->setSkipVouchersChecking( true );
01225
01226
01227 $sQ = 'select oxid from oxvouchers where oxorderid = "'.$this->getId().'"';
01228 $aVouchers = oxDb::getDb( true )->getAll( $sQ );
01229 foreach ( $aVouchers as $aVoucher ) {
01230 $this->_oOrderBasket->addVoucher( $aVoucher['oxid'] );
01231 }
01232 } else {
01233 $this->_oOrderBasket->setDiscountCalcMode( false );
01234 $this->_oOrderBasket->setVoucherDiscount( $this->oxorder__oxvoucherdiscount->value );
01235 $this->_oOrderBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
01236 }
01237
01238
01239 if ( !$this->_blReloadDelivery ) {
01240 $this->_oOrderBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01241 } else {
01242
01243 $this->_oOrderBasket->setShipping( $this->oxorder__oxdeltype->value );
01244 $this->_oOrderBasket->setDeliveryPrice( null );
01245 }
01246
01247
01248 $this->_oOrderBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01249
01250 return $this->_oOrderBasket;
01251 }
01252
01261 public function setDelivery( $sDeliveryId )
01262 {
01263 $this->reloadDelivery( true );
01264 $this->oxorder__oxdeltype = new oxField( $sDeliveryId );
01265 }
01266
01272 public function getOrderUser()
01273 {
01274 if ($this->_oUser) {
01275 return $this->_oUser;
01276 }
01277 $this->_oUser = oxNew( "oxuser" );
01278 $this->_oUser->load( $this->oxorder__oxuserid->value );
01279
01280 return $this->_oUser;
01281 }
01282
01290 public function pdfFooter( $oPdf )
01291 {
01292 }
01293
01301 public function pdfHeaderplus( $oPdf )
01302 {
01303 }
01304
01312 public function pdfHeader( $oPdf )
01313 {
01314 }
01315
01324 public function genPdf( $sFilename, $iSelLang = 0 )
01325 {
01326 }
01327
01333 public function getInvoiceNum()
01334 {
01335 $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01336 return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01337 }
01338
01344 public function getNextBillNum()
01345 {
01346 $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01347 return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01348 }
01349
01355 public function getShippingSetList()
01356 {
01357
01358 if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
01359 $sShipId = $this->oxorder__oxbillcountryid->value;
01360 }
01361
01362 $oBasket = $this->_getOrderBasket( false );
01363
01364
01365 $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles() );
01366
01367
01368 $oBasket->calculateBasket( true );
01369
01370
01371 $oDeliveryList = oxNew( "oxDeliveryList", "core" );
01372 $oDeliveryList->setCollectFittingDeliveriesSets( true );
01373
01374 return $oDeliveryList->getDeliveryList( $oBasket, $this->getOrderUser(), $sShipId );
01375 }
01376
01382 public function getVoucherNrList()
01383 {
01384 $oDB = oxDb::getDb( true );
01385 $aVouchers = array();
01386 $sSelect = "select oxvouchernr from oxvouchers where oxorderid = ".$oDB->quote( $this->oxorder__oxid->value );
01387 $rs = $oDB->execute( $sSelect);
01388 if ($rs != false && $rs->recordCount() > 0) {
01389 while (!$rs->EOF) {
01390 $aVouchers[] = $rs->fields['oxvouchernr'];
01391 $rs->moveNext();
01392 }
01393 }
01394 return $aVouchers;
01395 }
01396
01404 public function getOrderSum( $blToday = false )
01405 {
01406 $sSelect = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01407 $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01408
01409 if ( $blToday ) {
01410 $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01411 }
01412
01413 return ( double ) oxDb::getDb()->getOne( $sSelect );
01414 }
01415
01423 public function getOrderCnt( $blToday = false )
01424 {
01425 $sSelect = 'select count(*) from oxorder where ';
01426 $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01427
01428 if ( $blToday ) {
01429 $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01430 }
01431
01432 return ( int ) oxDb::getDb()->getOne( $sSelect );
01433 }
01434
01435
01443 protected function _checkOrderExist( $sOxId = null )
01444 {
01445 if ( !$sOxId) {
01446 return false;
01447 }
01448
01449 $oDb = oxDb::getDb();
01450 if ( $oDb->getOne( 'select oxid from oxorder where oxid = '.$oDb->quote( $sOxId ) ) ) {
01451 return true;
01452 }
01453
01454 return false;
01455 }
01456
01466 protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01467 {
01468 $iRet = self::ORDER_STATE_MAILINGERROR;
01469
01470
01471 $this->_oUser = $oUser;
01472 $this->_oBasket = $oBasket;
01473 $this->_oPayment = $oPayment;
01474
01475 $oxEmail = oxNew( 'oxemail' );
01476
01477
01478 if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01479
01480 $iRet = self::ORDER_STATE_OK;
01481 }
01482
01483
01484 $oxEmail->sendOrderEMailToOwner( $this );
01485
01486 return $iRet;
01487 }
01488
01494 public function getBasket()
01495 {
01496 return $this->_oBasket;
01497 }
01498
01504 public function getPayment()
01505 {
01506 return $this->_oPayment;
01507 }
01508
01514 public function getVoucherList()
01515 {
01516 return $this->_aVoucherList;
01517 }
01518
01524 public function getDelSet()
01525 {
01526 if ( $this->_oDelSet == null ) {
01527
01528 $this->_oDelSet = oxNew( 'oxdeliveryset' );
01529 $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01530 }
01531
01532 return $this->_oDelSet;
01533 }
01534
01540 public function getPaymentType()
01541 {
01542 if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType === null ) {
01543 $this->_oPaymentType = false;
01544 $oPaymentType = oxNew( 'oxuserpayment' );
01545 if ( $oPaymentType->load( $this->oxorder__oxpaymentid->value ) ) {
01546 $this->_oPaymentType = $oPaymentType;
01547 }
01548 }
01549
01550 return $this->_oPaymentType;
01551 }
01552
01558 public function getGiftCard()
01559 {
01560 if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01561 $this->_oGiftCard = oxNew( 'oxwrapping' );
01562 $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01563 }
01564
01565 return $this->_oGiftCard;
01566 }
01567
01575 public function setSeparateNumbering( $blSeparateNumbering = null )
01576 {
01577 $this->_blSeparateNumbering = $blSeparateNumbering;
01578 }
01579
01587 public function getLastUserPaymentType( $sUserId)
01588 {
01589 $oDb = oxDb::getDb();
01590 $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid='.$oDb->quote( $sUserId ).' order by oxorder.oxorderdate desc ';
01591 $sLastPaymentId = $oDb->getOne( $sQ );
01592 return $sLastPaymentId;
01593 }
01594
01607 protected function _makeSelListArray( $sArtId = null, $sOrderArtSelList = null )
01608 {
01609 $oOrder = oxNew( 'oxorderArticle' );
01610 return $oOrder->getOrderArticleSelectList( $sArtId, $sOrderArtSelList );
01611 }
01612
01621 protected function _addOrderArticlesToBasket( $oBasket, $aOrderArticles )
01622 {
01623
01624 if ( count( $aOrderArticles ) > 0 ) {
01625
01626
01627 foreach ( $aOrderArticles as $oOrderArticle ) {
01628 $oBasket->addOrderArticleToBasket( $oOrderArticle );
01629 }
01630 }
01631 }
01632
01641 protected function _addArticlesToBasket( $oBasket, $aArticles )
01642 {
01643
01644 if ( count($aArticles ) > 0 ) {
01645
01646
01647 foreach ( $aArticles as $oArticle ) {
01648 $aSel = isset( $oArticle->oxorderarticles__oxselvariant ) ? $oArticle->oxorderarticles__oxselvariant->value : null;
01649 $aPersParam = isset( $oArticle->oxorderarticles__oxpersparam ) ? $oArticle->getPersParams() : null;
01650 $oBasket->addToBasket( $oArticle->oxorderarticles__oxartid->value,
01651 $oArticle->oxorderarticles__oxamount->value,
01652 $aSel, $aPersParam );
01653 }
01654 }
01655 }
01656
01664 protected function _setDeprecatedValues()
01665 {
01666 if ( $this->oxorder__oxstorno->value != 1 ) {
01667 $oCur = $this->getConfig()->getActShopCurrencyObject();
01668 $oLang = oxLang::getInstance();
01669
01670 $this->totalnetsum = $this->oxorder__oxtotalnetsum->value;
01671 $this->totalbrutsum = $this->oxorder__oxtotalbrutsum->value;
01672 $this->totalorder = $this->oxorder__oxtotalordersum->value;
01673 $this->ftotalnetsum = $oLang->formatCurrency( $this->oxorder__oxtotalnetsum->value, $oCur );
01674 $this->ftotalbrutsum = $oLang->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $oCur );
01675 $this->fdelcost = $oLang->formatCurrency( $this->oxorder__oxdelcost->value, $oCur );
01676 $this->fpaycost = $oLang->formatCurrency( $this->oxorder__oxpaycost->value, $oCur );
01677 $this->fwrapcost = $oLang->formatCurrency( $this->oxorder__oxwrapcost->value, $oCur );
01678 $this->ftotalorder = $this->getTotalOrderSum();
01679 $this->totalvouchers = 0;
01680
01681 if ( $this->oxorder__oxvoucherdiscount->value ) {
01682 $this->totalvouchers = $oLang->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $oCur );
01683 }
01684
01685 if ( $this->oxorder__oxdiscount->value ) {
01686 $this->discount = $this->oxorder__oxdiscount->value;
01687 $this->fdiscount = $oLang->formatCurrency( $this->oxorder__oxdiscount->value, $oCur );
01688 }
01689 }
01690 }
01691
01697 public function getTotalOrderSum()
01698 {
01699 $oCur = $this->getConfig()->getActShopCurrencyObject();
01700 return number_format( $this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01701 }
01702
01708 public function getBillCountry()
01709 {
01710 if ( !$this->oxorder__oxbillcountry->value ) {
01711 $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
01712 }
01713 return $this->oxorder__oxbillcountry;
01714 }
01715
01721 public function getDelCountry()
01722 {
01723 if ( !$this->oxorder__oxdelcountry->value ) {
01724 $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
01725 }
01726 return $this->oxorder__oxdelcountry;
01727 }
01735 public function reloadDelivery( $blReload )
01736 {
01737 $this->_blReloadDelivery = $blReload;
01738 }
01739
01747 public function reloadDiscount( $blReload )
01748 {
01749 $this->_blReloadDiscount = $blReload;
01750 }
01751
01757 public function cancelOrder()
01758 {
01759 $this->oxorder__oxstorno = new oxField( 1 );
01760 if ( $this->save() ) {
01761
01762 foreach ( $this->getOrderArticles() as $oOrderArticle ) {
01763 $oOrderArticle->cancelOrderArticle();
01764 }
01765 }
01766 }
01767
01774 public function getOrderCurrency()
01775 {
01776 if ( $this->_oOrderCurrency === null ) {
01777
01778
01779 $aCurrencies = $this->getConfig()->getCurrencyArray();
01780 $this->_oOrderCurrency = current( $aCurrencies );
01781
01782 foreach ( $aCurrencies as $oCurr ) {
01783 if( $oCurr->name == $this->oxorder__oxcurrency->value ) {
01784 $this->_oOrderCurrency = $oCurr;
01785 break;
01786 }
01787 }
01788 }
01789 return $this->_oOrderCurrency;
01790 }
01791
01801 public function validateOrder( $oBasket, $oUser )
01802 {
01803
01804 $iValidState = $this->validateStock( $oBasket );
01805
01806 if ( !$iValidState ) {
01807
01808 $iValidState = $this->validateDelivery( $oBasket );
01809 }
01810
01811 if ( !$iValidState ) {
01812
01813 $iValidState = $this->validatePayment( $oBasket );
01814 }
01815
01816 return $iValidState;
01817 }
01818
01827 public function validateDelivery( $oBasket )
01828 {
01829
01830
01831 if ( $oBasket->getPaymentId() == 'oxempty') {
01832 return;
01833 }
01834 $oDb = oxDb::getDb();
01835
01836 $oDelSet = oxNew( "oxdeliveryset" );
01837 $sTable = $oDelSet->getViewName();
01838
01839 $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
01840 $oDb->quote( $oBasket->getShippingId() )." and ".$oDelSet->getSqlActiveSnippet();
01841
01842 if ( !$oDb->getOne( $sQ ) ) {
01843
01844 return self::ORDER_STATE_INVALIDDELIVERY;
01845 }
01846 }
01847
01856 public function validatePayment( $oBasket )
01857 {
01858 $oDb = oxDb::getDb();
01859
01860 $oPayment = oxNew( "oxpayment" );
01861 $sTable = $oPayment->getViewName();
01862
01863 $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
01864 $oDb->quote( $oBasket->getPaymentId() )." and ".$oPayment->getSqlActiveSnippet();
01865
01866 if ( !$oDb->getOne( $sQ ) ) {
01867 return self::ORDER_STATE_INVALIDPAYMENT;
01868 }
01869 }
01870 }