00001 <?php
00002
00007 class oxOrder extends oxBase
00008 {
00014 protected $_aSkipSaveFields = array( 'oxorderdate' );
00015
00021 protected $_oArticles = null;
00022
00028 protected $_oDelSet = null;
00029
00035 protected $_oGiftCard = null;
00036
00042 protected $_oPaymentType = null;
00043
00049 protected $_oPayment = null;
00050
00056 protected $_aVoucherList = null;
00057
00063 protected $_oDelPrice = null;
00064
00070 protected $_oUser = null;
00071
00077 protected $_oBasket = null;
00078
00084 protected $_oWrappingPrice = null;
00085
00091 protected $_oPaymentPrice = null;
00092
00098 protected $_sClassName = 'oxorder';
00099
00105 protected $_blSeparateNumbering = null;
00106
00112 protected $_iOrderLang = null;
00113
00119 protected $_blReloadDelivery = true;
00120
00126 protected $_blReloadDiscount = true;
00127
00133 protected $_oOrderCurrency = null;
00134
00138 public function __construct()
00139 {
00140 parent::__construct();
00141 $this->init( 'oxorder' );
00142
00143
00144 $this->setSeparateNumbering( $this->getConfig()->getConfigParam( 'blSeparateNumbering') );
00145
00146 }
00147
00155 public function __get( $sName )
00156 {
00157 if ( $sName == 'oDelSet' ) {
00158 return $this->getDelSet();
00159 }
00160
00161 if ( $sName == 'oxorder__oxbillcountry' ) {
00162 return $this->getBillCountry();
00163 }
00164
00165 if ( $sName == 'oxorder__oxdelcountry' ) {
00166 return $this->getDelCountry();
00167 }
00168 }
00169
00177 public function assign( $dbRecord )
00178 {
00179
00180 parent::assign( $dbRecord );
00181
00182 $oUtilsDate = oxUtilsDate::getInstance();
00183
00184
00185 $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value));
00186 $this->oxorder__oxsenddate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value));
00187
00188
00189 $this->_setDeprecatedValues();
00190 }
00191
00199 protected function _getCountryTitle( $sCountryId )
00200 {
00201 $sTitle = null;
00202 if ( $sCountryId && $sCountryId != '-1' ) {
00203 $oCountry = oxNew( 'oxcountry' );
00204 $oCountry->load( $sCountryId );
00205 $sTitle = $oCountry->oxcountry__oxtitle->value;
00206 }
00207
00208 return $sTitle;
00209 }
00210
00218 public function getOrderArticles( $blExcludeCanceled = false )
00219 {
00220
00221 if ( $this->_oArticles === null ) {
00222 $blExcludeState = $blExcludeCanceled;
00223 $sTable = getViewName( "oxorderarticles" );
00224 $sSelect = "select {$sTable}.* from {$sTable}
00225 where {$sTable}.oxorderid = '".$this->getId() . "'" .
00226 ( $blExcludeCanceled ? " and {$sTable}.oxstorno != 1 ": " " ) ."
00227 order by {$sTable}.oxartid";
00228
00229
00230 $oArticles = oxNew( 'oxlist' );
00231 $oArticles->init( 'oxorderarticle' );
00232 $oArticles->selectString( $sSelect );
00233
00234
00235 return $oArticles;
00236 }
00237 return $this->_oArticles;
00238 }
00239
00247 public function setOrderArticleList( $aOrderArticleList )
00248 {
00249 $this->_oArticles = $aOrderArticleList;
00250 }
00251
00257 public function getOrderDeliveryPrice()
00258 {
00259 if ( $this->_oDelPrice != null ) {
00260 return $this->_oDelPrice;
00261 }
00262
00263 $this->_oDelPrice = oxNew( 'oxprice' );
00264 $this->_oDelPrice->setBruttoPriceMode();
00265 $this->_oDelPrice->setPrice( $this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value );
00266 return $this->_oDelPrice;
00267 }
00268
00274 public function getOrderWrappingPrice()
00275 {
00276 if ( $this->_oWrappingPrice != null ) {
00277 return $this->_oWrappingPrice;
00278 }
00279
00280 $this->_oWrappingPrice = oxNew( 'oxprice' );
00281 $this->_oWrappingPrice->setBruttoPriceMode();
00282 $this->_oWrappingPrice->setPrice( $this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value );
00283 return $this->_oWrappingPrice;
00284 }
00285
00291 public function getOrderPaymentPrice()
00292 {
00293 if ( $this->_oPaymentPrice != null ) {
00294 return $this->_oPaymentPrice;
00295 }
00296
00297 $this->_oPaymentPrice = oxNew( 'oxprice' );
00298 $this->_oPaymentPrice->setBruttoPriceMode();
00299 $this->_oPaymentPrice->setPrice( $this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value );
00300 return $this->_oPaymentPrice;
00301 }
00302
00309 public function getOrderNetSum()
00310 {
00311 $dTotalNetSum = 0;
00312
00313 $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
00314 $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
00315 $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
00316 $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
00317
00318 return $dTotalNetSum;
00319 }
00320
00341 public function finalizeOrder( oxBasket $oBasket, $oUser, $blRecalculatingOrder = false )
00342 {
00343
00344 $sGetChallenge = oxSession::getVar( 'sess_challenge' );
00345 if ( $this->_checkOrderExist( $sGetChallenge ) ) {
00346 oxUtils::getInstance()->logger( 'BLOCKER' );
00347
00348 return 3;
00349 }
00350
00351
00352 if ( !$blRecalculatingOrder ) {
00353
00354 $this->setId( $sGetChallenge );
00355 }
00356
00357
00358 $this->_setUser( $oUser );
00359
00360
00361 $this->_loadFromBasket( $oBasket );
00362
00363
00364 $oUserPayment = $this->_setPayment( $oBasket->getPaymentId() );
00365
00366
00367
00368 if ( !$blRecalculatingOrder ) {
00369 $this->_setFolder();
00370 }
00371
00372
00373 $this->save();
00374
00375
00376
00377 if ( !$blRecalculatingOrder ) {
00378 $blRet = $this->_executePayment( $oBasket, $oUserPayment );
00379 if ( $blRet !== true ) {
00380 return $blRet;
00381 }
00382 }
00383
00384
00385 oxSession::deleteVar( 'ordrem' );
00386
00387
00388 $this->_setOrderStatus( 'OK' );
00389
00390
00391 $oBasket->setOrderId( $this->getId() );
00392
00393
00394 $this->_updateStock();
00395
00396
00397 $this->_updateWishlist( $oBasket->getContents(), $oUser );
00398
00399
00400 $this->_updateNoticeList( $oBasket->getContents(), $oUser );
00401
00402
00403
00404 if ( !$blRecalculatingOrder ) {
00405 $this->_markVouchers( $oBasket, $oUser );
00406 }
00407
00408
00409
00410 if ( !$blRecalculatingOrder ) {
00411 $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
00412 } else {
00413 $iRet = 1;
00414 }
00415
00416 return $iRet;
00417 }
00418
00426 protected function _setOrderStatus( $sStatus )
00427 {
00428 $oDb = oxDb::getDb();
00429 $sQ = 'update oxorder set oxtransstatus='.$oDb->quote( $sStatus ).' where oxid="'.$this->getId().'" ';
00430 $oDb->execute( $sQ );
00431
00432
00433 $this->oxorder__oxtransstatus = new oxField( $sStatus, oxField::T_RAW );
00434 }
00435
00446 protected function _loadFromBasket( oxBasket $oBasket )
00447 {
00448 $myConfig = $this->getConfig();
00449
00450
00451 if ( $myConfig->getConfigParam( 'blStoreIPs' ) && $this->oxorder__oxip->value === null ) {
00452 $this->oxorder__oxip = new oxField(oxUtilsServer::getInstance()->getRemoteAddress(), oxField::T_RAW);
00453 }
00454
00455
00456 $this->oxorder__oxtotalnetsum = new oxField(oxUtils::getInstance()->fRound($oBasket->getProductsPrice()->getNettoSum()), oxField::T_RAW);
00457 $this->oxorder__oxtotalbrutsum = new oxField($oBasket->getProductsPrice()->getBruttoSum(), oxField::T_RAW);
00458 $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00459
00460
00461 if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00462 $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00463 $this->oxorder__oxpayvat = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00464 }
00465
00466
00467 if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00468 $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00469
00470 $this->oxorder__oxdelvat = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW);
00471 $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00472 }
00473
00474
00475 if ( $this->oxorder__oxremark->value === null ) {
00476 $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00477 }
00478
00479
00480 $oCur = $myConfig->getActShopCurrencyObject();
00481 $this->oxorder__oxcurrency = new oxField($oCur->name);
00482 $this->oxorder__oxcurrate = new oxField($oCur->rate, oxField::T_RAW);
00483
00484
00485 if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00486 $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00487 }
00488
00489
00490 if ( $this->_blReloadDiscount ) {
00491 $dDiscount = 0;
00492 $aDiscounts = $oBasket->getDiscounts();
00493 if ( count($aDiscounts) > 0 ) {
00494 foreach ($aDiscounts as $oDiscount) {
00495 $dDiscount += $oDiscount->dDiscount;
00496 }
00497 }
00498 $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00499 }
00500
00501
00502 $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00503
00504
00505
00506 $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00507
00508
00509 $this->_setOrderArticles( $oBasket->getContents() );
00510
00511
00512 $this->_setWrapping( $oBasket );
00513 }
00514
00521 public function getOrderLanguage()
00522 {
00523 if ( $this->_iOrderLang === null ) {
00524 if ( isset( $this->oxorder__oxlang->value ) ) {
00525 $this->_iOrderLang = oxLang::getInstance()->validateLanguage( $this->oxorder__oxlang->value );
00526 } else {
00527 $this->_iOrderLang = oxLang::getInstance()->getBaseLanguage();
00528 }
00529 }
00530 return $this->_iOrderLang;
00531 }
00532
00540 protected function _setUser( $oUser )
00541 {
00542
00543 $this->oxorder__oxuserid = new oxField($oUser->getId());
00544
00545
00546 $this->oxorder__oxbillcompany = clone $oUser->oxuser__oxcompany;
00547 $this->oxorder__oxbillemail = clone $oUser->oxuser__oxusername;
00548 $this->oxorder__oxbillfname = clone $oUser->oxuser__oxfname;
00549 $this->oxorder__oxbilllname = clone $oUser->oxuser__oxlname;
00550 $this->oxorder__oxbillstreet = clone $oUser->oxuser__oxstreet;
00551 $this->oxorder__oxbillstreetnr = clone $oUser->oxuser__oxstreetnr;
00552 $this->oxorder__oxbilladdinfo = clone $oUser->oxuser__oxaddinfo;
00553 $this->oxorder__oxbillustid = clone $oUser->oxuser__oxustid;
00554 $this->oxorder__oxbillcity = clone $oUser->oxuser__oxcity;
00555 $this->oxorder__oxbillcountryid = clone $oUser->oxuser__oxcountryid;
00556 $this->oxorder__oxbillzip = clone $oUser->oxuser__oxzip;
00557 $this->oxorder__oxbillfon = clone $oUser->oxuser__oxfon;
00558 $this->oxorder__oxbillfax = clone $oUser->oxuser__oxfax;
00559 $this->oxorder__oxbillsal = clone $oUser->oxuser__oxsal;
00560
00561
00562
00563 if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00564
00565 $this->oxorder__oxdelcompany = clone $oDelAdress->oxaddress__oxcompany;
00566 $this->oxorder__oxdelfname = clone $oDelAdress->oxaddress__oxfname;
00567 $this->oxorder__oxdellname = clone $oDelAdress->oxaddress__oxlname;
00568 $this->oxorder__oxdelstreet = clone $oDelAdress->oxaddress__oxstreet;
00569 $this->oxorder__oxdelstreetnr = clone $oDelAdress->oxaddress__oxstreetnr;
00570 $this->oxorder__oxdeladdinfo = clone $oDelAdress->oxaddress__oxaddinfo;
00571 $this->oxorder__oxdelcity = clone $oDelAdress->oxaddress__oxcity;
00572 $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00573 $this->oxorder__oxdelzip = clone $oDelAdress->oxaddress__oxzip;
00574 $this->oxorder__oxdelfon = clone $oDelAdress->oxaddress__oxfon;
00575 $this->oxorder__oxdelfax = clone $oDelAdress->oxaddress__oxfax;
00576 $this->oxorder__oxdelsal = clone $oDelAdress->oxaddress__oxsal;
00577 }
00578 }
00579
00587 protected function _setWrapping( oxBasket $oBasket )
00588 {
00589 $myConfig = $this->getConfig();
00590
00591
00592 if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00593 $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00594
00595
00596 if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
00597 $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00598 }
00599 }
00600
00601
00602 $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00603
00604
00605 $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00606 }
00607
00616 protected function _setOrderArticles( $aArticleList )
00617 {
00618
00619 $this->_oArticles = oxNew( 'oxlist' );
00620 $iCurrLang = $this->getOrderLanguage();
00621
00622
00623 foreach ( $aArticleList as $oContent ) {
00624
00625
00626
00627 $oProduct = $oContent->getArticle( true, null, true);
00628
00629
00630 if ( $oProduct->isOrderArticle() ) {
00631 $oOrderArticle = $oProduct;
00632 } else {
00633
00634
00635 if ( $iCurrLang != $oProduct->getLanguage() ) {
00636 $oProduct->loadInLang( $iCurrLang, $oProduct->getProductId() );
00637 }
00638
00639
00640 $sSelList = '';
00641 if ( count( $aChosenSelList = $oContent->getChosenSelList() ) ) {
00642 foreach ( $aChosenSelList as $oItem ) {
00643 if ( $sSelList ) {
00644 $sSelList .= ", ";
00645 }
00646 $sSelList .= "{$oItem->name} : {$oItem->value}";
00647 }
00648 }
00649
00650 $oOrderArticle = oxNew( 'oxorderarticle' );
00651 $oOrderArticle->setIsNewOrderItem( true );
00652 $oOrderArticle->copyThis( $oProduct );
00653 $oOrderArticle->setId();
00654
00655 $oOrderArticle->oxorderarticles__oxartnum = clone $oProduct->oxarticles__oxartnum;
00656 $oOrderArticle->oxorderarticles__oxselvariant = new oxField( trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->value ), oxField::T_RAW );
00657 $oOrderArticle->oxorderarticles__oxshortdesc = new oxField( $oProduct->oxarticles__oxshortdesc->value, oxField::T_RAW );
00658 $oOrderArticle->oxorderarticles__oxtitle = new oxField( trim( $oProduct->oxarticles__oxtitle->value.' '.$oOrderArticle->oxorderarticles__oxselvariant->value ), oxField::T_RAW );
00659
00660
00661 if ( !is_array( $aPersParams = $oProduct->getPersParams() ) ) {
00662 $aPersParams = $oContent->getPersParams();
00663 }
00664 if ( is_array( $aPersParams ) && count( $aPersParams )) {
00665 $oOrderArticle->oxorderarticles__oxpersparam = new oxField( serialize( $aPersParams ), oxField::T_RAW );
00666 }
00667 }
00668
00669
00670 $oOrderArticle->oxorderarticles__oxorderid = new oxField( $this->getId() );
00671 $oOrderArticle->oxorderarticles__oxartid = new oxField( $oContent->getProductId() );
00672 $oOrderArticle->oxorderarticles__oxamount = new oxField( $oContent->getAmount() );
00673
00674
00675 $oPrice = $oContent->getPrice();
00676 $oOrderArticle->oxorderarticles__oxnetprice = new oxField( $oPrice->getNettoPrice(), oxField::T_RAW );
00677 $oOrderArticle->oxorderarticles__oxvatprice = new oxField( $oPrice->getVatValue(), oxField::T_RAW );
00678 $oOrderArticle->oxorderarticles__oxbrutprice = new oxField( $oPrice->getBruttoPrice(), oxField::T_RAW );
00679 $oOrderArticle->oxorderarticles__oxvat = new oxField( $oPrice->getVat(), oxField::T_RAW );
00680
00681 $oUnitPtice = $oContent->getUnitPrice();
00682 $oOrderArticle->oxorderarticles__oxnprice = new oxField( $oUnitPtice->getNettoPrice(), oxField::T_RAW );
00683 $oOrderArticle->oxorderarticles__oxbprice = new oxField( $oUnitPtice->getBruttoPrice(), oxField::T_RAW );
00684
00685
00686 $oOrderArticle->oxorderarticles__oxwrapid = new oxField( $oContent->getWrappingId(), oxField::T_RAW );
00687
00688
00689 $oOrderArticle->oxorderarticles__oxordershopid = new oxField( $oContent->getShopId(), oxField::T_RAW );
00690
00691
00692
00693
00694 $oOrderArticle->oProduct = $oProduct;
00695
00696
00697 $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
00698 }
00699 }
00700
00712 protected function _executePayment( oxBasket $oBasket, $oUserpayment )
00713 {
00714 $oPayTransaction = $this->_getGateway();
00715 $oPayTransaction->setPaymentParams( $oUserpayment );
00716
00717 if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
00718 $this->delete();
00719
00720
00721 if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
00722 if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
00723 return $sLastError;
00724 }
00725 }
00726
00727
00728 if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
00729 if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
00730 return $iLastErrorNo;
00731 }
00732 }
00733
00734 return 2;
00735 }
00736 return true;
00737 }
00738
00745 protected function _getGateway()
00746 {
00747 return oxNew( 'oxPaymentGateway' );
00748 }
00749
00757 protected function _setPayment( $sPaymentid )
00758 {
00759
00760 $aDynvalue = oxSession::getVar( 'dynvalue' );
00761 $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
00762
00763
00764 $oPayment = oxNew( 'oxpayment' );
00765
00766 if (!$oPayment->load( $sPaymentid )) {
00767 return null;
00768 }
00769
00770
00771 if ( !$aDynvalue && ( $oUserpayment = $this->getPaymentType() ) ) {
00772 if ( is_array( $aStoredDynvalue = $oUserpayment->getDynValues() ) ) {
00773 foreach ( $aStoredDynvalue as $oVal ) {
00774 $aDynvalue[$oVal->name] = $oVal->value;
00775 }
00776 }
00777 }
00778
00779 $oPayment->setDynValues( oxUtils::getInstance()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
00780
00781
00782 $aDynVal = array();
00783
00784 if ( is_array( $aPaymentDynValues = $oPayment->getDynValues() ) ) {
00785 foreach ( $aPaymentDynValues as $key => $oVal ) {
00786 if ( isset( $aDynvalue[$oVal->name] ) ) {
00787 $oVal->value = $aDynvalue[$oVal->name];
00788 }
00789
00790
00791 $aPaymentDynValues[$key] = $oVal;
00792 $aDynVal[$oVal->name] = $oVal->value;
00793 }
00794 }
00795
00796
00797
00798
00799 $oUserpayment = oxNew( 'oxuserpayment' );
00800 $oUserpayment->oxuserpayments__oxuserid = clone $this->oxorder__oxuserid;
00801 $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
00802 $oUserpayment->oxuserpayments__oxvalue = new oxField(oxUtils::getInstance()->assignValuesToText( $aDynVal ), oxField::T_RAW);
00803 $oUserpayment->oxpayments__oxdesc = clone $oPayment->oxpayments__oxdesc;
00804 $oUserpayment->setDynValues( $aPaymentDynValues );
00805 $oUserpayment->save();
00806
00807
00808 $this->oxorder__oxpaymentid = new oxField($oUserpayment->getId(), oxField::T_RAW);
00809 $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
00810
00811
00812 return $oUserpayment;
00813 }
00814
00820 protected function _setFolder()
00821 {
00822 $myConfig = $this->getConfig();
00823 $this->oxorder__oxfolder = new oxField(key( $myConfig->getShopConfVar( 'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
00824 }
00825
00835 protected function _updateWishlist( $aArticleList, $oUser )
00836 {
00837
00838 foreach ( $aArticleList as $oContent) {
00839 if ( ( $sWishId = $oContent->getWishId() ) ) {
00840
00841
00842 if ( $sWishId == $oUser->getId() ) {
00843 $oUserBasket = $oUser->getBasket( 'wishlist' );
00844 } else {
00845 $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
00846 $oUserBasket = oxNew( 'oxuserbasket' );
00847 $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
00848 }
00849
00850
00851 if ( $oUserBasket ) {
00852 if ( !($sProdId = $oContent->getWishArticleId() )) {
00853 $sProdId = $oContent->getProductId();
00854 }
00855 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00856 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00857 if ( $dNewAmount < 0) {
00858 $dNewAmount = 0;
00859 }
00860 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00861 }
00862 }
00863 }
00864 }
00865
00875 protected function _updateNoticeList( $aArticleList, $oUser )
00876 {
00877
00878 if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
00879
00880 foreach ( $aArticleList as $oContent) {
00881 $sProdId = $oContent->getProductId();
00882
00883
00884 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00885 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00886 if ( $dNewAmount < 0) {
00887 $dNewAmount = 0;
00888 }
00889 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00890 }
00891 }
00892 }
00893
00899 protected function _updateStock()
00900 {
00901 $myConfig = $this->getConfig();
00902
00903
00904 if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00905 $oOrderArticles = $this->getOrderArticles();
00906 foreach ( $oOrderArticles as $oOrderArticle ) {
00907 if ( $oOrderArticle->isNewOrderItem() ) {
00908 $oOrderArticle->updateArticleStock( $oOrderArticle->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
00909 }
00910 }
00911 }
00912 }
00913
00925 protected function _markVouchers( $oBasket, $oUser )
00926 {
00927 $this->_aVoucherList = $oBasket->getVouchers();
00928
00929 if ( is_array( $this->_aVoucherList ) ) {
00930 foreach ( array_keys( $this->_aVoucherList ) as $sVoucherId ) {
00931 $oVoucher = oxNew( 'oxvoucher' );
00932 $oVoucher->load( $sVoucherId );
00933 $oVSerie = $oVoucher->getSerie();
00934 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oVSerie->oxvoucherseries__oxdiscount );
00935
00936
00937 $oVoucher->oxmodvouchers__oxvouchernr = $oVoucher->oxvouchers__oxvouchernr;
00938 $oVoucher->oxmodvouchers__oxdiscount = clone $oVSerie->oxvoucherseries__oxdiscount;
00939 $oVoucher->oxmodvouchers__oxdiscounttype = clone $oVSerie->oxvoucherseries__oxdiscounttype;
00940
00941
00942 $this->_aVoucherList[$sVoucherId] = $oVoucher;
00943 }
00944 }
00945 }
00946
00952 public function save()
00953 {
00954 if ( ( $blSave = parent::save() ) ) {
00955
00956
00957 $oOrderArticles = $this->getOrderArticles();
00958 if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
00959 foreach ( $oOrderArticles as $oOrderArticle ) {
00960 $oOrderArticle->save();
00961 }
00962 }
00963 }
00964
00965 return $blSave;
00966 }
00967
00974 public function getDelAddressInfo()
00975 {
00976 $oDelAdress = null;
00977 if ( ( $soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
00978 $oDelAdress = oxNew( 'oxaddress' );
00979 $oDelAdress->load( $soxAddressId );
00980
00981
00982 if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
00983 $oCountry = oxNew( 'oxcountry' );
00984 $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
00985 $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
00986 }
00987 }
00988 return $oDelAdress;
00989 }
00990
01001 public function validateStock( $oBasket )
01002 {
01003 foreach ( $oBasket->getContents() as $key => $oContent ) {
01004 try {
01005 $oProd = $oContent->getArticle();
01006 } catch ( oxNoArticleException $oEx ) {
01007 $oBasket->removeItem( $key );
01008 throw $oEx;
01009 } catch ( oxArticleInputException $oEx ) {
01010 $oBasket->removeItem( $key );
01011 throw $oEx;
01012 }
01013
01014
01015 $iOnStock = $oProd->checkForStock( $oContent->getAmount() );
01016 if ( $iOnStock !== true ) {
01017 $oEx = oxNew( 'oxOutOfStockException' );
01018 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
01019 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
01020 $oEx->setProductId( $oProd->getId() );
01021 $oEx->setRemainingAmount( $oProd->oxarticles__oxstock->value );
01022 throw $oEx;
01023 }
01024 }
01025 }
01026
01032 protected function _insert()
01033 {
01034 $myConfig = $this->getConfig();
01035 $oUtilsDate = oxUtilsDate::getInstance();
01036
01037
01038 if ( !$this->oxorder__oxorderdate->value ) {
01039 $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
01040 } else {
01041 $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
01042 }
01043 $this->oxorder__oxshopid = new oxField($myConfig->getShopId(), oxField::T_RAW);
01044
01045 $this->oxorder__oxsenddate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01046
01047 if ( ( $blInsert = parent::_insert() ) ) {
01048
01049 if ( !$this->oxorder__oxordernr->value ) {
01050 $aWhere = '';
01051
01052 if ( $this->_blSeparateNumbering ) {
01053 $aWhere = array( 'oxshopid = "'.$myConfig->getShopId().'"' );
01054 }
01055 $this->_setRecordNumber( 'oxordernr', $aWhere );
01056 }
01057 }
01058 return $blInsert;
01059 }
01060
01066 protected function _update()
01067 {
01068 $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01069 return parent::_update();
01070 }
01071
01080 public function delete( $sOxId = null )
01081 {
01082 if ( $sOxId ) {
01083 if ( !$this->load( $sOxId ) ) {
01084
01085 return false;
01086 }
01087 } elseif ( !$sOxId ) {
01088 $sOxId = $this->getId();
01089 }
01090
01091
01092 if ( !$sOxId ) {
01093 return false;
01094 }
01095
01096
01097
01098 $myConfig = $this->getConfig();
01099 $blUseStock = $myConfig->getConfigParam( 'blUseStock' );
01100 $oOrderArticles = $this->getOrderArticles( false );
01101 foreach ( $oOrderArticles as $oOrderArticle ) {
01102 if ( $blUseStock && $oOrderArticle->oxorderarticles__oxstorno->value != 1 ) {
01103 $oOrderArticle->updateArticleStock( $oOrderArticle->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
01104 }
01105 $oOrderArticle->delete();
01106 }
01107
01108
01109 if ( $oPaymentType = $this->getPaymentType() ) {
01110 $oPaymentType->delete();
01111 }
01112
01113 return parent::delete( $sOxId );
01114 }
01115
01125 public function recalculateOrder( $aNewArticles = array() )
01126 {
01127 oxDb::startTransaction();
01128
01129 try {
01130 $oBasket = $this->_getOrderBasket();
01131
01132
01133 $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles( true ) );
01134
01135
01136 $this->_addArticlesToBasket( $oBasket, $aNewArticles );
01137
01138
01139 $oBasket->calculateBasket( true );
01140
01141
01142 $iRet = $this->finalizeOrder( $oBasket, $this->getOrderUser(), true );
01143
01144
01145 if ( $iRet !== 1 ) {
01146 oxDb::rollbackTransaction();
01147 } else {
01148 oxDb::commitTransaction();
01149 }
01150
01151 } catch( Exception $oE ) {
01152
01153 oxDb::rollbackTransaction();
01154 }
01155 }
01156
01157 protected $_oOrderBasket = null;
01165 protected function _getOrderBasket( $blStockCheck = true )
01166 {
01167 $this->_oOrderBasket = oxNew( "oxbasket" );
01168
01169
01170 $this->_oOrderBasket->setStockCheckMode( $blStockCheck );
01171
01172
01173 $this->_oOrderBasket->setBasketUser( $this->getOrderUser() );
01174
01175
01176 $this->_oOrderBasket->setOrderId( $this->getId() );
01177
01178
01179 $aCurrencies = $this->getConfig()->getCurrencyArray();
01180 foreach ( $aCurrencies as $oCur ) {
01181 if ($oCur->name == $this->oxorder__oxcurrency->value) {
01182 $oBasketCur = $oCur;
01183 break;
01184 }
01185 }
01186
01187
01188 $this->_oOrderBasket->setBasketCurrency( $oBasketCur );
01189
01190
01191 $this->_oOrderBasket->setCardId( $this->oxorder__oxcardid->value );
01192 $this->_oOrderBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01193
01194 if ( $this->_blReloadDiscount ) {
01195
01196 $this->_oOrderBasket->setSkipVouchersChecking( true );
01197
01198
01199 $sQ = 'select oxid from oxvouchers where oxorderid = "'.$this->getId().'"';
01200 $aVouchers = oxDb::getDb( true )->getAll( $sQ );
01201 foreach ( $aVouchers as $aVoucher ) {
01202 $this->_oOrderBasket->addVoucher( $aVoucher['oxid'] );
01203 }
01204 } else {
01205 $this->_oOrderBasket->setDiscountCalcMode( false );
01206 $this->_oOrderBasket->setVoucherDiscount( $this->oxorder__oxvoucherdiscount->value );
01207 $this->_oOrderBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
01208 }
01209
01210
01211 if ( !$this->_blReloadDelivery ) {
01212 $this->_oOrderBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01213 } else {
01214
01215 $this->_oOrderBasket->setShipping( $this->oxorder__oxdeltype->value );
01216 $this->_oOrderBasket->setDeliveryPrice( null );
01217 }
01218
01219
01220 $this->_oOrderBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01221
01222 return $this->_oOrderBasket;
01223 }
01224
01233 public function setDelivery( $sDeliveryId )
01234 {
01235 $this->reloadDelivery( true );
01236 $this->oxorder__oxdeltype = new oxField( $sDeliveryId );
01237 }
01238
01244 public function getOrderUser()
01245 {
01246 if ($this->_oUser) {
01247 return $this->_oUser;
01248 }
01249 $this->_oUser = oxNew( "oxuser" );
01250 $this->_oUser->load( $this->oxorder__oxuserid->value );
01251
01252 return $this->_oUser;
01253 }
01254
01262 public function pdfFooter( $oPdf )
01263 {
01264 }
01265
01273 public function pdfHeaderplus( $oPdf )
01274 {
01275 }
01276
01284 public function pdfHeader( $oPdf )
01285 {
01286 }
01287
01296 public function genPdf( $sFilename, $iSelLang = 0 )
01297 {
01298 }
01299
01305 public function getInvoiceNum()
01306 {
01307 $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01308 return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01309 }
01310
01316 public function getNextBillNum()
01317 {
01318 $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01319 return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01320 }
01321
01327 public function getShippingSetList()
01328 {
01329
01330 if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
01331 $sShipId = $this->oxorder__oxbillcountryid->value;
01332 }
01333
01334 $oBasket = $this->_getOrderBasket( false );
01335
01336
01337 $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles() );
01338
01339
01340 $oBasket->calculateBasket( true );
01341
01342
01343 $oDeliveryList = oxNew( "oxDeliveryList", "core" );
01344 $oDeliveryList->setCollectFittingDeliveriesSets( true );
01345
01346 return $oDeliveryList->getDeliveryList( $oBasket, $this->getOrderUser(), $sShipId );
01347 }
01348
01354 public function getVoucherNrList()
01355 {
01356 $oDB = oxDb::getDb( true );
01357 $aVouchers = array();
01358 $sSelect = "select oxvouchernr from oxvouchers where oxorderid = ".$oDB->quote( $this->oxorder__oxid->value );
01359 $rs = $oDB->execute( $sSelect);
01360 if ($rs != false && $rs->recordCount() > 0) {
01361 while (!$rs->EOF) {
01362 $aVouchers[] = $rs->fields['oxvouchernr'];
01363 $rs->moveNext();
01364 }
01365 }
01366 return $aVouchers;
01367 }
01368
01376 public function getOrderSum( $blToday = false )
01377 {
01378 $sSelect = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01379 $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01380
01381 if ( $blToday ) {
01382 $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01383 }
01384
01385 return ( double ) oxDb::getDb()->getOne( $sSelect );
01386 }
01387
01395 public function getOrderCnt( $blToday = false )
01396 {
01397 $sSelect = 'select count(*) from oxorder where ';
01398 $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01399
01400 if ( $blToday ) {
01401 $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01402 }
01403
01404 return ( int ) oxDb::getDb()->getOne( $sSelect );
01405 }
01406
01407
01415 protected function _checkOrderExist( $sOxId = null )
01416 {
01417 if ( !$sOxId) {
01418 return false;
01419 }
01420
01421 $oDb = oxDb::getDb();
01422 if ( $oDb->getOne( 'select oxid from oxorder where oxid = '.$oDb->quote( $sOxId ) ) ) {
01423 return true;
01424 }
01425
01426 return false;
01427 }
01428
01438 protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01439 {
01440 $iRet = 0;
01441
01442
01443 $this->_oUser = $oUser;
01444 $this->_oBasket = $oBasket;
01445 $this->_oPayment = $oPayment;
01446
01447 $oxEmail = oxNew( 'oxemail' );
01448
01449
01450 if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01451
01452 $iRet = 1;
01453 }
01454
01455
01456 $oxEmail->sendOrderEMailToOwner( $this );
01457
01458 return $iRet;
01459 }
01460
01466 public function getBasket()
01467 {
01468 return $this->_oBasket;
01469 }
01470
01476 public function getPayment()
01477 {
01478 return $this->_oPayment;
01479 }
01480
01486 public function getVoucherList()
01487 {
01488 return $this->_aVoucherList;
01489 }
01490
01496 public function getDelSet()
01497 {
01498 if ( $this->_oDelSet == null ) {
01499
01500 $this->_oDelSet = oxNew( 'oxdeliveryset' );
01501 $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01502 }
01503
01504 return $this->_oDelSet;
01505 }
01506
01512 public function getPaymentType()
01513 {
01514 if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType === null ) {
01515 $this->_oPaymentType = false;
01516 $oPaymentType = oxNew( 'oxuserpayment' );
01517 if ( $oPaymentType->load( $this->oxorder__oxpaymentid->value ) ) {
01518 $this->_oPaymentType = $oPaymentType;
01519 }
01520 }
01521
01522 return $this->_oPaymentType;
01523 }
01524
01530 public function getGiftCard()
01531 {
01532 if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01533 $this->_oGiftCard = oxNew( 'oxwrapping' );
01534 $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01535 }
01536
01537 return $this->_oGiftCard;
01538 }
01539
01547 public function setSeparateNumbering( $blSeparateNumbering = null )
01548 {
01549 $this->_blSeparateNumbering = $blSeparateNumbering;
01550 }
01551
01559 public function getLastUserPaymentType( $sUserId)
01560 {
01561 $oDb = oxDb::getDb();
01562 $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid='.$oDb->quote( $sUserId ).' order by oxorder.oxorderdate desc ';
01563 $sLastPaymentId = $oDb->getOne( $sQ );
01564 return $sLastPaymentId;
01565 }
01566
01579 protected function _makeSelListArray( $sArtId = null, $sOrderArtSelList = null )
01580 {
01581 $oOrder = oxNew( 'oxorderArticle' );
01582 return $oOrder->getOrderArticleSelectList( $sArtId, $sOrderArtSelList );
01583 }
01584
01593 protected function _addOrderArticlesToBasket( $oBasket, $aOrderArticles )
01594 {
01595
01596 if ( count( $aOrderArticles ) > 0 ) {
01597
01598
01599 foreach ( $aOrderArticles as $oOrderArticle ) {
01600 $oBasket->addOrderArticleToBasket( $oOrderArticle );
01601 }
01602 }
01603 }
01604
01613 protected function _addArticlesToBasket( $oBasket, $aArticles )
01614 {
01615
01616 if ( count($aArticles ) > 0 ) {
01617
01618
01619 foreach ( $aArticles as $oArticle ) {
01620 $aSel = isset( $oArticle->oxorderarticles__oxselvariant ) ? $oArticle->oxorderarticles__oxselvariant->value : null;
01621 $aPersParam = isset( $oArticle->oxorderarticles__oxpersparam ) ? $oArticle->getPersParams() : null;
01622 $oBasket->addToBasket( $oArticle->oxorderarticles__oxartid->value,
01623 $oArticle->oxorderarticles__oxamount->value,
01624 $aSel, $aPersParam );
01625 }
01626 }
01627 }
01628
01636 protected function _setDeprecatedValues()
01637 {
01638 if ( $this->oxorder__oxstorno->value != 1 ) {
01639 $oCur = $this->getConfig()->getActShopCurrencyObject();
01640 $oLang = oxLang::getInstance();
01641
01642 $this->totalnetsum = $this->oxorder__oxtotalnetsum->value;
01643 $this->totalbrutsum = $this->oxorder__oxtotalbrutsum->value;
01644 $this->totalorder = $this->oxorder__oxtotalordersum->value;
01645 $this->ftotalnetsum = $oLang->formatCurrency( $this->oxorder__oxtotalnetsum->value, $oCur );
01646 $this->ftotalbrutsum = $oLang->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $oCur );
01647 $this->fdelcost = $oLang->formatCurrency( $this->oxorder__oxdelcost->value, $oCur );
01648 $this->fpaycost = $oLang->formatCurrency( $this->oxorder__oxpaycost->value, $oCur );
01649 $this->fwrapcost = $oLang->formatCurrency( $this->oxorder__oxwrapcost->value, $oCur );
01650 $this->ftotalorder = $this->getTotalOrderSum();
01651 $this->totalvouchers = 0;
01652
01653 if ( $this->oxorder__oxvoucherdiscount->value ) {
01654 $this->totalvouchers = $oLang->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $oCur );
01655 }
01656
01657 if ( $this->oxorder__oxdiscount->value ) {
01658 $this->discount = $this->oxorder__oxdiscount->value;
01659 $this->fdiscount = $oLang->formatCurrency( $this->oxorder__oxdiscount->value, $oCur );
01660 }
01661 }
01662 }
01663
01669 public function getTotalOrderSum()
01670 {
01671 $oCur = $this->getConfig()->getActShopCurrencyObject();
01672 return number_format( $this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01673 }
01674
01680 public function getBillCountry()
01681 {
01682 if ( !$this->oxorder__oxbillcountry->value ) {
01683 $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
01684 }
01685 return $this->oxorder__oxbillcountry;
01686 }
01687
01693 public function getDelCountry()
01694 {
01695 if ( !$this->oxorder__oxdelcountry->value ) {
01696 $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
01697 }
01698 return $this->oxorder__oxdelcountry;
01699 }
01700
01708 public function reloadDelivery( $blReload )
01709 {
01710 $this->_blReloadDelivery = $blReload;
01711 }
01712
01720 public function reloadDiscount( $blReload )
01721 {
01722 $this->_blReloadDiscount = $blReload;
01723 }
01724
01730 public function cancelOrder()
01731 {
01732 $this->oxorder__oxstorno = new oxField( 1 );
01733 if ( $this->save() ) {
01734
01735 foreach ( $this->getOrderArticles() as $oOrderArticle ) {
01736 $oOrderArticle->cancelOrderArticle();
01737 }
01738 }
01739 }
01740
01747 public function getOrderCurrency()
01748 {
01749 if ( $this->_oOrderCurrency === null ) {
01750
01751
01752 $aCurrencies = $this->getConfig()->getCurrencyArray();
01753 $this->_oOrderCurrency = current( $aCurrencies );
01754
01755 foreach ( $aCurrencies as $oCurr ) {
01756 if( $oCurr->name == $this->oxorder__oxcurrency->value ) {
01757 $this->_oOrderCurrency = $oCurr;
01758 break;
01759 }
01760 }
01761 }
01762 return $this->_oOrderCurrency;
01763 }
01764 }