00001 <?php
00002
00007 class oxOrder extends oxBase
00008 {
00009
00015 protected $_aSkipSaveFields = array( 'oxorderdate' );
00016
00022 protected $_oArticles = null;
00023
00029 protected $_oDelSet = null;
00030
00036 protected $_oGiftCard = null;
00037
00043 protected $_oPaymentType = null;
00044
00050 protected $_oPayment = null;
00051
00057 protected $_aVoucherList = null;
00058
00064 protected $_oDelPrice = null;
00065
00071 protected $_oUser = null;
00072
00078 protected $_oBasket = null;
00079
00085 protected $_oWrappingPrice = null;
00086
00092 protected $_oPaymentPrice = null;
00093
00099 protected $_sClassName = 'oxorder';
00100
00106 protected $_blSeparateNumbering = null;
00107
00113 protected $_iOrderLang = null;
00114
00118 public function __construct()
00119 {
00120 parent::__construct();
00121 $this->init( 'oxorder' );
00122
00123
00124 $this->setSeparateNumbering( $this->getConfig()->getConfigParam( 'blSeparateNumbering') );
00125
00126 }
00127
00135 public function __get( $sName )
00136 {
00137 if ( $sName == 'oDelSet' ) {
00138 return $this->getDelSet();
00139 }
00140
00141 if ( $sName == 'oxorder__oxbillcountry' ) {
00142 return $this->getBillCountry();
00143 }
00144
00145 if ( $sName == 'oxorder__oxdelcountry' ) {
00146 return $this->getDelCountry();
00147 }
00148 }
00149
00157 public function assign( $dbRecord )
00158 {
00159
00160 parent::assign( $dbRecord );
00161
00162 $oUtilsDate = oxUtilsDate::getInstance();
00163
00164
00165 $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value));
00166 $this->oxorder__oxsenddate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value));
00167
00168
00169 $this->_setDeprecatedValues();
00170 }
00171
00179 protected function _getCountryTitle( $sCountryId )
00180 {
00181 $sTitle = null;
00182 if ( $sCountryId && $sCountryId != '-1' ) {
00183 $oCountry = oxNew( 'oxcountry' );
00184 $oCountry->load( $sCountryId );
00185 $sTitle = $oCountry->oxcountry__oxtitle->value;
00186 }
00187
00188 return $sTitle;
00189 }
00190
00196 public function getOrderArticles()
00197 {
00198 if ( $this->_oArticles == null ) {
00199
00200
00201 $this->_oArticles = oxNew( 'oxlist' );
00202 $this->_oArticles->init( 'oxorderarticle' );
00203
00204 $sSelect = 'select oxorderarticles.* from oxorderarticles
00205 where oxorderarticles.oxorderid="'.$this->getId().'"
00206 order by oxorderarticles.oxartid';
00207 $this->_oArticles->selectString( $sSelect );
00208 }
00209
00210 return $this->_oArticles;
00211 }
00212
00218 public function getOrderDeliveryPrice()
00219 {
00220 if ( $this->_oDelPrice != null ) {
00221 return $this->_oDelPrice;
00222 }
00223
00224 $this->_oDelPrice = oxNew( 'oxprice' );
00225 $this->_oDelPrice->setBruttoPriceMode();
00226 $this->_oDelPrice->setPrice( $this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value );
00227 return $this->_oDelPrice;
00228 }
00229
00235 public function getOrderWrappingPrice()
00236 {
00237 if ( $this->_oWrappingPrice != null ) {
00238 return $this->_oWrappingPrice;
00239 }
00240
00241 $this->_oWrappingPrice = oxNew( 'oxprice' );
00242 $this->_oWrappingPrice->setBruttoPriceMode();
00243 $this->_oWrappingPrice->setPrice( $this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value );
00244 return $this->_oWrappingPrice;
00245 }
00246
00252 public function getOrderPaymentPrice()
00253 {
00254 if ( $this->_oPaymentPrice != null ) {
00255 return $this->_oPaymentPrice;
00256 }
00257
00258 $this->_oPaymentPrice = oxNew( 'oxprice' );
00259 $this->_oPaymentPrice->setBruttoPriceMode();
00260 $this->_oPaymentPrice->setPrice( $this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value );
00261 return $this->_oPaymentPrice;
00262 }
00263
00270 public function getOrderNetSum()
00271 {
00272 $dTotalNetSum = 0;
00273
00274 $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
00275 $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
00276 $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
00277 $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
00278
00279 return $dTotalNetSum;
00280 }
00281
00302 public function finalizeOrder( oxBasket $oBasket, $oUser, $blRecalculatingOrder = false )
00303 {
00304
00305 $sGetChallenge = oxSession::getVar( 'sess_challenge' );
00306 if ( $this->_checkOrderExist( $sGetChallenge ) ) {
00307 oxUtils::getInstance()->logger( 'BLOCKER' );
00308
00309 return 3;
00310 }
00311
00312
00313 if ( !$blRecalculatingOrder ) {
00314
00315 $this->setId( $sGetChallenge );
00316 }
00317
00318
00319 $this->_setUser( $oUser );
00320
00321
00322 $this->_loadFromBasket( $oBasket );
00323
00324
00325 $oUserPayment = $this->_setPayment( $oBasket->getPaymentId() );
00326
00327
00328
00329 if ( !$blRecalculatingOrder ) {
00330 $this->_setFolder();
00331 }
00332
00333
00334 $this->save();
00335
00336
00337
00338 if ( !$blRecalculatingOrder ) {
00339 $blRet = $this->_executePayment( $oBasket, $oUserPayment );
00340 if ( $blRet !== true ) {
00341 return $blRet;
00342 }
00343 }
00344
00345
00346 oxSession::deleteVar( 'ordrem' );
00347
00348
00349 $this->_setOrderStatus( 'OK' );
00350
00351
00352 $oBasket->setOrderId( $this->getId() );
00353
00354
00355 $this->_updateStock();
00356
00357
00358 $this->_updateWishlist( $oBasket->getContents(), $oUser );
00359
00360
00361 $this->_updateNoticeList( $oBasket->getContents(), $oUser );
00362
00363
00364
00365 if ( !$blRecalculatingOrder ) {
00366 $this->_markVouchers( $oBasket, $oUser );
00367 }
00368
00369
00370
00371 if ( !$blRecalculatingOrder ) {
00372 $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
00373 } else {
00374 $iRet = 1;
00375 }
00376
00377 return $iRet;
00378 }
00379
00387 protected function _setOrderStatus( $sStatus )
00388 {
00389 $sQ = 'update oxorder set oxtransstatus="'.$sStatus.'" where oxid="'.$this->getId().'" ';
00390 oxDb::getDb()->execute( $sQ );
00391 }
00392
00403 protected function _loadFromBasket( oxBasket $oBasket )
00404 {
00405 $myConfig = $this->getConfig();
00406
00407
00408 if ( $myConfig->getConfigParam( 'blStoreIPs' ) && $this->oxorder__oxip->value === null ) {
00409 $this->oxorder__oxip = new oxField(oxUtilsServer::getInstance()->getRemoteAddress(), oxField::T_RAW);
00410 }
00411
00412
00413 $this->oxorder__oxtotalnetsum = new oxField(oxUtils::getInstance()->fRound($oBasket->getProductsPrice()->getNettoSum()), oxField::T_RAW);
00414 $this->oxorder__oxtotalbrutsum = new oxField($oBasket->getProductsPrice()->getBruttoSum(), oxField::T_RAW);
00415 $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00416
00417
00418 if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00419 $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00420 $this->oxorder__oxpayvat = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00421 }
00422
00423
00424 if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00425 $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00426
00427 $this->oxorder__oxdelvat = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW);
00428 $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00429 }
00430
00431
00432 if ( $this->oxorder__oxremark->value === null ) {
00433 $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00434 }
00435
00436
00437 $oCur = $myConfig->getActShopCurrencyObject();
00438 $this->oxorder__oxcurrency = new oxField($oCur->name);
00439 $this->oxorder__oxcurrate = new oxField($oCur->rate, oxField::T_RAW);
00440
00441
00442 if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00443 $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00444 }
00445
00446
00447 $dDiscount = 0;
00448 $aDiscounts = $oBasket->getDiscounts();
00449 if ( count($aDiscounts) > 0 ) {
00450 foreach ($aDiscounts as $oDiscount) {
00451 $dDiscount += $oDiscount->dDiscount;
00452 }
00453 }
00454 if ( $dDiscount ) {
00455 $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00456 }
00457
00458
00459 $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00460
00461
00462
00463 $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00464
00465
00466 $this->_setOrderArticles( $oBasket->getContents() );
00467
00468
00469 $this->_setWrapping( $oBasket );
00470 }
00471
00478 public function getOrderLanguage()
00479 {
00480 if ( $this->_iOrderLang === null ) {
00481 if ( isset( $this->oxorder__oxlang->value ) ) {
00482 $this->_iOrderLang = oxLang::getInstance()->validateLanguage( $this->oxorder__oxlang->value );
00483 } else {
00484 $this->_iOrderLang = oxLang::getInstance()->getBaseLanguage();
00485 }
00486 }
00487 return $this->_iOrderLang;
00488 }
00489
00497 protected function _setUser( $oUser )
00498 {
00499
00500 $this->oxorder__oxuserid = new oxField($oUser->getId());
00501
00502
00503 $this->oxorder__oxbillcompany = clone $oUser->oxuser__oxcompany;
00504 $this->oxorder__oxbillemail = clone $oUser->oxuser__oxusername;
00505 $this->oxorder__oxbillfname = clone $oUser->oxuser__oxfname;
00506 $this->oxorder__oxbilllname = clone $oUser->oxuser__oxlname;
00507 $this->oxorder__oxbillstreet = clone $oUser->oxuser__oxstreet;
00508 $this->oxorder__oxbillstreetnr = clone $oUser->oxuser__oxstreetnr;
00509 $this->oxorder__oxbilladdinfo = clone $oUser->oxuser__oxaddinfo;
00510 $this->oxorder__oxbillustid = clone $oUser->oxuser__oxustid;
00511 $this->oxorder__oxbillcity = clone $oUser->oxuser__oxcity;
00512 $this->oxorder__oxbillcountryid = clone $oUser->oxuser__oxcountryid;
00513 $this->oxorder__oxbillzip = clone $oUser->oxuser__oxzip;
00514 $this->oxorder__oxbillfon = clone $oUser->oxuser__oxfon;
00515 $this->oxorder__oxbillfax = clone $oUser->oxuser__oxfax;
00516 $this->oxorder__oxbillsal = clone $oUser->oxuser__oxsal;
00517
00518
00519
00520 if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00521
00522 $this->oxorder__oxdelcompany = clone $oDelAdress->oxaddress__oxcompany;
00523 $this->oxorder__oxdelfname = clone $oDelAdress->oxaddress__oxfname;
00524 $this->oxorder__oxdellname = clone $oDelAdress->oxaddress__oxlname;
00525 $this->oxorder__oxdelstreet = clone $oDelAdress->oxaddress__oxstreet;
00526 $this->oxorder__oxdelstreetnr = clone $oDelAdress->oxaddress__oxstreetnr;
00527 $this->oxorder__oxdeladdinfo = clone $oDelAdress->oxaddress__oxaddinfo;
00528 $this->oxorder__oxdelcity = clone $oDelAdress->oxaddress__oxcity;
00529 $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00530 $this->oxorder__oxdelzip = clone $oDelAdress->oxaddress__oxzip;
00531 $this->oxorder__oxdelfon = clone $oDelAdress->oxaddress__oxfon;
00532 $this->oxorder__oxdelfax = clone $oDelAdress->oxaddress__oxfax;
00533 $this->oxorder__oxdelsal = clone $oDelAdress->oxaddress__oxsal;
00534 }
00535 }
00536
00544 protected function _setWrapping( oxBasket $oBasket )
00545 {
00546 $myConfig = $this->getConfig();
00547
00548
00549 if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00550 $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00551
00552
00553 if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
00554 $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00555 }
00556 }
00557
00558
00559 $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00560
00561
00562 $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00563 }
00564
00573 protected function _setOrderArticles( $aArticleList )
00574 {
00575
00576 $this->_oArticles = oxNew( 'oxlist' );
00577 $iCurrLang = $this->getOrderLanguage();
00578
00579
00580 foreach ( $aArticleList as $oContent ) {
00581
00582
00583 $oProduct = $oContent->getArticle();
00584
00585
00586 if ( $iCurrLang != $oProduct->getLanguage() ) {
00587 $oProduct->loadInLang( $iCurrLang, $oProduct->getId() );
00588 }
00589
00590 $aSelList = array();
00591
00592 $sSelList = '';
00593 if ( count($oContent->getChosenSelList()) ) {
00594 foreach ( $oContent->getChosenSelList() as $oItem ) {
00595 $aSelList[] = $oItem->name.' : '.$oItem->value;
00596 }
00597 $sSelList = implode( ', ', $aSelList );
00598 }
00599
00600 $oOrderArticle = oxNew( 'oxorderarticle' );
00601
00602
00603 $oOrderArticle->copyThis( $oProduct );
00604
00605
00606
00607 $oOrderArticle->setId();
00608 $oOrderArticle->oxorderarticles__oxorderid = new oxField($this->getId());
00609 $oOrderArticle->oxorderarticles__oxartid = new oxField($oContent->getProductId());
00610 $oOrderArticle->oxorderarticles__oxamount = new oxField($oContent->getAmount());
00611
00612
00613 $oOrderArticle->oxorderarticles__oxartnum = clone $oProduct->oxarticles__oxartnum;
00614
00615
00616 $oOrderArticle->oxorderarticles__oxselvariant = new oxField(trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->value ), oxField::T_RAW);
00617
00618
00619 $oOrderArticle->oxorderarticles__oxtitle = new oxField(trim( $oProduct->oxarticles__oxtitle->value.' '.$oOrderArticle->oxorderarticles__oxselvariant->value ), oxField::T_RAW);
00620
00621
00622 $oOrderArticle->oxorderarticles__oxshortdesc = new oxField( $oProduct->oxarticles__oxshortdesc->value, oxField::T_RAW);
00623
00624
00625 $oOrderArticle->oxorderarticles__oxnetprice = new oxField($oContent->getPrice()->getNettoPrice(), oxField::T_RAW);
00626 $oOrderArticle->oxorderarticles__oxvatprice = new oxField($oContent->getPrice()->getVATValue(), oxField::T_RAW);
00627 $oOrderArticle->oxorderarticles__oxbrutprice = new oxField($oContent->getPrice()->getBruttoPrice(), oxField::T_RAW);
00628 $oOrderArticle->oxorderarticles__oxnprice = new oxField($oContent->getUnitPrice()->getNettoPrice(), oxField::T_RAW);
00629 $oOrderArticle->oxorderarticles__oxbprice = new oxField($oContent->getUnitPrice()->getBruttoPrice(), oxField::T_RAW);
00630 $oOrderArticle->oxorderarticles__oxvat = new oxField($oContent->getPrice()->getVAT(), oxField::T_RAW);
00631
00632
00633 $oOrderArticle->oxorderarticles__oxwrapid = new oxField($oContent->getWrappingId(), oxField::T_RAW);
00634
00635
00636 $oOrderArticle->oxorderarticles__oxordershopid = new oxField($oContent->getShopId(), oxField::T_RAW);
00637
00638
00639 if ( count( $oProduct->getPersParams() ) ) {
00640 $oOrderArticle->oxorderarticles__oxpersparam = new oxField(serialize( $oProduct->getPersParams() ), oxField::T_RAW);
00641 } elseif ( count( $oContent->getPersParams() ) ) {
00642 $oOrderArticle->oxorderarticles__oxpersparam = new oxField(serialize( $oContent->getPersParams()), oxField::T_RAW);
00643 }
00644
00645
00646
00647
00648 $oOrderArticle->oProduct = $oProduct;
00649
00650
00651 $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
00652 }
00653 }
00654
00666 protected function _executePayment( oxBasket $oBasket, $oUserpayment )
00667 {
00668 $oPayTransaction = $this->_getGateway();
00669 $oPayTransaction->setPaymentParams( $oUserpayment );
00670
00671 if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
00672 $this->delete();
00673
00674
00675 if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
00676 if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
00677 return $sLastError;
00678 }
00679 }
00680
00681
00682 if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
00683 if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
00684 return $iLastErrorNo;
00685 }
00686 }
00687
00688 return 2;
00689 }
00690 return true;
00691 }
00692
00699 protected function _getGateway()
00700 {
00701 return oxNew( 'oxPaymentGateway' );
00702 }
00703
00711 protected function _setPayment( $sPaymentid )
00712 {
00713
00714 $aDynvalue = oxSession::getVar( 'dynvalue' );
00715 $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
00716
00717
00718 $oPayment = oxNew( 'oxpayment' );
00719
00720 if (!$oPayment->load( $sPaymentid )) {
00721 return null;
00722 }
00723
00724
00725 if ( !$aDynvalue && ( $oUserpayment = $this->getPaymentType() ) ) {
00726 $aStoredDynvalue = $oUserpayment->getDynValues();
00727 foreach ( $aStoredDynvalue as $oVal ) {
00728 $aDynvalue[$oVal->name] = $oVal->value;
00729 }
00730 }
00731
00732 $oPayment->setDynValues( oxUtils::getInstance()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
00733
00734
00735 $aDynVal = array();
00736
00737 $aPaymentDynValues = $oPayment->getDynValues();
00738 foreach ( $aPaymentDynValues as $key => $oVal ) {
00739 if ( isset( $aDynvalue[$oVal->name] ) ) {
00740 $oVal->value = $aDynvalue[$oVal->name];
00741 }
00742
00743
00744 $aPaymentDynValues[$key] = $oVal;
00745 $aDynVal[$oVal->name] = $oVal->value;
00746 }
00747
00748
00749
00750
00751 $oUserpayment = oxNew( 'oxuserpayment' );
00752 $oUserpayment->oxuserpayments__oxuserid = clone $this->oxorder__oxuserid;
00753 $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
00754 $oUserpayment->oxuserpayments__oxvalue = new oxField(oxUtils::getInstance()->assignValuesToText( $aDynVal ), oxField::T_RAW);
00755 $oUserpayment->oxpayments__oxdesc = clone $oPayment->oxpayments__oxdesc;
00756 $oUserpayment->setDynValues( $aPaymentDynValues );
00757 $oUserpayment->save();
00758
00759
00760 $this->oxorder__oxpaymentid = new oxField($oUserpayment->getId(), oxField::T_RAW);
00761 $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
00762
00763
00764 return $oUserpayment;
00765 }
00766
00772 protected function _setFolder()
00773 {
00774 $myConfig = $this->getConfig();
00775 $this->oxorder__oxfolder = new oxField(key( $myConfig->getShopConfVar( 'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
00776 }
00777
00787 protected function _updateWishlist( $aArticleList, $oUser )
00788 {
00789
00790 foreach ( $aArticleList as $oContent) {
00791 if ( ( $sWishId = $oContent->getWishId() ) ) {
00792
00793
00794 if ( $sWishId == $oUser->getId() ) {
00795 $oUserBasket = $oUser->getBasket( 'wishlist' );
00796 } else {
00797 $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
00798 $oUserBasket = oxNew( 'oxuserbasket' );
00799 $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
00800 }
00801
00802
00803 if ( $oUserBasket ) {
00804 if ( !($sProdId = $oContent->getWishArticleId() )) {
00805 $sProdId = $oContent->getProductId();
00806 }
00807 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00808 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00809 if ( $dNewAmount < 0) {
00810 $dNewAmount = 0;
00811 }
00812 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00813 }
00814 }
00815 }
00816 }
00817
00827 protected function _updateNoticeList( $aArticleList, $oUser )
00828 {
00829
00830 if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
00831
00832 foreach ( $aArticleList as $oContent) {
00833 $sProdId = $oContent->getProductId();
00834
00835
00836 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00837 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00838 if ( $dNewAmount < 0) {
00839 $dNewAmount = 0;
00840 }
00841 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00842 }
00843 }
00844 }
00845
00851 protected function _updateStock()
00852 {
00853 $myConfig = $this->getConfig();
00854 $blUseStock = $myConfig->getConfigParam( 'blUseStock' );
00855
00856
00857 $oOrderArticles = $this->getOrderArticles();
00858 if ( $oOrderArticles && count( $oOrderArticles ) > 0 && $blUseStock ) {
00859 foreach ( $oOrderArticles as $oOrderArticle ) {
00860 $oOrderArticle->updateArticleStock( $oOrderArticle->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
00861 }
00862 }
00863 }
00864
00876 protected function _markVouchers( $oBasket, $oUser )
00877 {
00878 $this->_aVoucherList = $oBasket->getVouchers();
00879
00880 if ( is_array( $this->_aVoucherList ) ) {
00881 foreach ( array_keys( $this->_aVoucherList ) as $sVoucherId ) {
00882 $oVoucher = oxNew( 'oxvoucher' );
00883 $oVoucher->load( $sVoucherId );
00884 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value );
00885
00886
00887 $oVoucher->oxmodvouchers__oxvouchernr = $oVoucher->oxvouchers__oxvouchernr;
00888 $oVSerie = $oVoucher->getSerie();
00889 $oVoucher->oxmodvouchers__oxdiscount = clone $oVSerie->oxvoucherseries__oxdiscount;
00890 $oVoucher->oxmodvouchers__oxdiscounttype = clone $oVSerie->oxvoucherseries__oxdiscounttype;
00891
00892
00893 $this->_aVoucherList[$sVoucherId] = $oVoucher;
00894 }
00895 }
00896 }
00897
00903 public function save()
00904 {
00905 if ( ( $blSave = parent::save() ) ) {
00906
00907
00908 $oOrderArticles = $this->getOrderArticles();
00909 if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
00910 foreach ( $oOrderArticles as $oOrderArticle ) {
00911 $oOrderArticle->save();
00912 }
00913 }
00914 }
00915
00916 return $blSave;
00917 }
00918
00925 public function getDelAddressInfo()
00926 {
00927 $oDelAdress = null;
00928 if ( ( $soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
00929 $oDelAdress = oxNew( 'oxbase' );
00930 $oDelAdress->init( 'oxaddress' );
00931 $oDelAdress->load( $soxAddressId );
00932
00933
00934 if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
00935 $oCountry = oxNew( 'oxcountry' );
00936 $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
00937 $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
00938 }
00939 }
00940 return $oDelAdress;
00941 }
00942
00953 public function validateStock( $oBasket )
00954 {
00955 foreach ( $oBasket->getContents() as $oContent ) {
00956 $oProd = $oContent->getArticle();
00957
00958
00959 $iOnStock = $oProd->checkForStock( $oContent->getAmount() );
00960 if ( $iOnStock !== true ) {
00961 $oEx = oxNew( 'oxOutOfStockException' );
00962 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
00963 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
00964 $oEx->setRemainingAmount( $oProd->oxarticles__oxstock->value );
00965 throw $oEx;
00966 }
00967 }
00968 }
00969
00975 protected function _insert()
00976 {
00977 $myConfig = $this->getConfig();
00978 $oUtilsDate = oxUtilsDate::getInstance();
00979
00980
00981 if ( !$this->oxorder__oxorderdate->value ) {
00982 $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
00983 } else {
00984 $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
00985 }
00986 $this->oxorder__oxshopid = new oxField($myConfig->getShopId(), oxField::T_RAW);
00987
00988 $this->oxorder__oxsenddate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
00989
00990 if ( ( $blInsert = parent::_insert() ) ) {
00991
00992 if ( !$this->oxorder__oxordernr->value ) {
00993 $aWhere = '';
00994
00995 if ( $this->_blSeparateNumbering ) {
00996 $aWhere = array( 'oxshopid = "'.$myConfig->getShopId().'"' );
00997 }
00998 $this->_setRecordNumber( 'oxordernr', $aWhere );
00999 }
01000 }
01001 return $blInsert;
01002 }
01003
01009 protected function _update()
01010 {
01011 $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01012 return parent::_update();
01013 }
01014
01023 public function delete( $sOxId = null )
01024 {
01025 if ( $sOxId ) {
01026 if ( !$this->load( $sOxId ) ) {
01027
01028 return false;
01029 }
01030 } elseif ( !$sOxId ) {
01031 $sOxId = $this->getId();
01032 }
01033
01034
01035 if ( !$sOxId ) {
01036 return false;
01037 }
01038
01039
01040
01041 $myConfig = $this->getConfig();
01042 $blUseStock = $myConfig->getConfigParam( 'blUseStock' );
01043
01044 $oOrderArticles = $this->getOrderArticles();
01045 foreach ( $oOrderArticles as $oOrderArticle ) {
01046
01047 if ( $blUseStock ) {
01048 if ( !$oOrderArticle->oxorderarticles__oxstorno->value ) {
01049 $oOrderArticle->updateArticleStock( $oOrderArticle->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
01050 }
01051 }
01052 $oOrderArticle->delete();
01053 }
01054
01055
01056 if ( $oPaymentType = $this->getPaymentType() ) {
01057 $oPaymentType->delete();
01058 }
01059
01060 return parent::delete( $sOxId );
01061 }
01062
01074 public function recalculateOrder( $aNewOrderArticles = array(), $blChangeDelivery = false, $blChangeDiscount = false )
01075 {
01076 oxDb::startTransaction();
01077
01078 try {
01079
01080
01081 $this->delete();
01082
01083 $oUser = oxNew( "oxuser" );
01084 $oUser->load( $this->oxorder__oxuserid->value );
01085
01086
01087 $oBasket = oxNew( "oxBasket" );
01088
01089 $aCanceledArticles = array();
01090 $aArticlesIds = array();
01091
01092
01093 if ( $this->_oArticles = $this->getOrderArticles() ) {
01094 $this->_oArticles->rewind();
01095 while ( $oOrderArticle = $this->_oArticles->current() ) {
01096 $sOrderArticleId = $this->_oArticles->key();
01097
01098
01099 $aArticlesIds[$sOrderArticleId] = $oOrderArticle->oxorderarticles__oxartid->value;
01100
01101
01102
01103 if ( $oOrderArticle->oxorderarticles__oxstorno->value == '1') {
01104 $aCanceledArticles[$sOrderArticleId] = $oOrderArticle;
01105
01106
01107
01108 $this->_oArticles->offsetUnset( $sOrderArticleId );
01109 } else {
01110 $this->_oArticles->next();
01111 }
01112 }
01113 }
01114
01115
01116 foreach ($aNewOrderArticles as $oNewOrderArticle) {
01117
01118 $sNewOrderArtId = null;
01119 $blIsOldOrderArticle = false;
01120
01121
01122 if ( ( $sNewOrderArtId = array_search( $oNewOrderArticle->oxorderarticles__oxartid->value, $aArticlesIds ) ) !== false ) {
01123 $blIsOldOrderArticle = true;
01124 }
01125
01126
01127 if ( $oNewOrderArticle->oxorderarticles__oxamount->value == 0 ) {
01128
01129
01130 if ( array_key_exists( $sNewOrderArtId, $aCanceledArticles) ) {
01131
01132
01133 $this->_oArticles->offsetSet( $sNewOrderArtId, $aCanceledArticles[$sNewOrderArtId] );
01134
01135
01136
01137 unset($aCanceledArticles[$sNewOrderArtId]);
01138 }
01139 }
01140
01141 if ( $blIsOldOrderArticle ) {
01142
01143 $this->_oArticles->offsetGet( $sNewOrderArtId )->oxorderarticles__oxamount = clone $oNewOrderArticle->oxorderarticles__oxamount;
01144
01145 } else {
01146
01147 $this->_oArticles->offsetSet( $oNewOrderArticle->getId(), $oNewOrderArticle );
01148
01149 }
01150 }
01151
01152
01153 $oBasket = $this->_addOrderArticlesToBasket( $oUser, $this->_oArticles, $blChangeDelivery, $blChangeDiscount );
01154
01155
01156 $iRet = $this->finalizeOrder( $oBasket, $oUser, true );
01157
01158
01159 if ( count($aCanceledArticles) > 0 ) {
01160 foreach ($aCanceledArticles as $oCanceledOrderArticle ) {
01161 $oCanceledOrderArticle->save();
01162 }
01163 }
01164
01165
01166 if ( $iRet !== 1 ) {
01167 oxDb::rollbackTransaction();
01168 } else {
01169 oxDb::commitTransaction();
01170 }
01171
01172 } catch( Exception $oE ) {
01173
01174 oxDb::rollbackTransaction();
01175 }
01176 }
01177
01185 public function pdfFooter( $oPdf )
01186 {
01187 }
01188
01196 public function pdfHeaderplus( $oPdf )
01197 {
01198 }
01199
01207 public function pdfHeader( $oPdf )
01208 {
01209 }
01210
01219 public function genPdf( $sFilename, $iSelLang = 0 )
01220 {
01221 }
01222
01228 public function getInvoiceNum()
01229 {
01230 $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01231 return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01232 }
01233
01239 public function getNextBillNum()
01240 {
01241 $sQ = 'select max(oxorder.oxbillnr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01242 return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01243 }
01244
01250 public function getShippingSetList()
01251 {
01252
01253 if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
01254 $sShipId = $this->oxorder__oxbillcountryid->value;
01255 }
01256
01257 $oUser = oxNew( "oxuser" );
01258 $oUser->load( $this->oxorder__oxuserid->value );
01259
01260
01261 $oBasket = $this->_addOrderArticlesToBasket( $oUser, $this->getOrderArticles(), false, false, false );
01262
01263
01264 $oDeliveryList = oxNew( "oxDeliveryList", "core" );
01265 $oDeliveryList->setCollectFittingDeliveriesSets( true );
01266
01267 return $oDeliveryList->getDeliveryList( $oBasket, $oUser, $sShipId );
01268 }
01269
01275 public function getVoucherNrList()
01276 {
01277 $oDB = oxDb::getDb( true );
01278 $aVouchers = array();
01279 $sSelect = "select oxvouchernr from oxvouchers where oxorderid = '".$this->oxorder__oxid->value."'";
01280 $rs = $oDB->execute( $sSelect);
01281 if ($rs != false && $rs->recordCount() > 0) {
01282 while (!$rs->EOF) {
01283 $aVouchers[] = $rs->fields['oxvouchernr'];
01284 $rs->moveNext();
01285 }
01286 }
01287 return $aVouchers;
01288 }
01289
01297 public function getOrderSum( $blToday = false )
01298 {
01299 $sSelect = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01300 $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01301
01302 if ( $blToday ) {
01303 $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01304 }
01305
01306 return ( double ) oxDb::getDb()->getOne( $sSelect );
01307 }
01308
01316 public function getOrderCnt( $blToday = false )
01317 {
01318 $sSelect = 'select count(*) from oxorder where ';
01319 $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01320
01321 if ( $blToday ) {
01322 $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01323 }
01324
01325 return ( int ) oxDb::getDb()->getOne( $sSelect );
01326 }
01327
01328
01336 protected function _checkOrderExist( $sOxId = null )
01337 {
01338 if ( !$sOxId) {
01339 return false;
01340 }
01341
01342 if ( oxDb::getDb()->getOne( 'select oxid from oxorder where oxid = "'.$sOxId.'"' ) ) {
01343 return true;
01344 }
01345
01346 return false;
01347 }
01348
01358 protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01359 {
01360 $iRet = 0;
01361
01362
01363 $this->_oUser = $oUser;
01364 $this->_oBasket = $oBasket;
01365 $this->_oPayment = $oPayment;
01366
01367 $oxEmail = oxNew( 'oxemail' );
01368
01369
01370 if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01371
01372 $iRet = 1;
01373 }
01374
01375
01376 $oxEmail->sendOrderEMailToOwner( $this );
01377
01378 return $iRet;
01379 }
01380
01386 public function getUser()
01387 {
01388 return $this->_oUser;
01389 }
01390
01396 public function getBasket()
01397 {
01398 return $this->_oBasket;
01399 }
01400
01406 public function getPayment()
01407 {
01408 return $this->_oPayment;
01409 }
01410
01416 public function getVoucherList()
01417 {
01418 return $this->_aVoucherList;
01419 }
01420
01426 public function getDelSet()
01427 {
01428 if ( $this->_oDelSet == null ) {
01429
01430 $this->_oDelSet = oxNew( 'oxdeliveryset' );
01431 $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01432 }
01433
01434 return $this->_oDelSet;
01435 }
01436
01442 public function getPaymentType()
01443 {
01444 if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType == null ) {
01445 $this->_oPaymentType = oxNew( 'oxuserpayment' );
01446 $this->_oPaymentType->load( $this->oxorder__oxpaymentid->value );
01447 }
01448
01449 return $this->_oPaymentType;
01450 }
01451
01457 public function getGiftCard()
01458 {
01459 if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01460 $this->_oGiftCard = oxNew( 'oxwrapping' );
01461 $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01462 }
01463
01464 return $this->_oGiftCard;
01465 }
01466
01474 public function setSeparateNumbering( $blSeparateNumbering = null )
01475 {
01476 $this->_blSeparateNumbering = $blSeparateNumbering;
01477 }
01478
01486 public function getLastUserPaymentType( $sUserId)
01487 {
01488 $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid="'.$sUserId.'" order by oxorder.oxorderdate desc ';
01489 $sLastPaymentId = oxDb::getDb()->getOne( $sQ );
01490 return $sLastPaymentId;
01491 }
01492
01503 protected function _makeSelListArray( $sArtId = null, $sOrderArtSelList = null )
01504 {
01505 $aList = array();
01506 $aRet = array();
01507
01508 if ( $sArtId ) {
01509 $aList = explode( ",", $sOrderArtSelList );
01510 $oStr = getStr();
01511
01512
01513 $oArticle = oxNew( "oxArticle" );
01514 $oArticle->load( $sArtId );
01515 $aArticleSelList = $oArticle->getSelectLists();
01516
01517
01518 foreach ( $aList as $sList ) {
01519 if ( $sList ) {
01520
01521 $aVal = explode( ":", $sList );
01522 if ( isset($aVal[0]) && isset($aVal[1])) {
01523 $sOrderArtListTitle = $oStr->strtolower( trim($aVal[0]) );
01524 $sOrderArtSelValue = $oStr->strtolower( trim($aVal[1]) );
01525
01526
01527 $iSelListNum = 0;
01528 if ( count($aArticleSelList) > 0 ) {
01529 foreach ( $aArticleSelList as $aSelect ) {
01530
01531
01532 if ( $oStr->strtolower($aSelect['name']) == $sOrderArtListTitle ) {
01533
01534 $iSelValueNum = 0;
01535 foreach ( $aSelect as $oSel ) {
01536 if ( $oStr->strtolower($oSel->name) == $sOrderArtSelValue ) {
01537
01538 $aRet[$iSelListNum] = $iSelValueNum;
01539 }
01540
01541 $iSelValueNum++;
01542 }
01543 }
01544
01545 $iSelListNum++;
01546 }
01547 }
01548 }
01549 }
01550 }
01551 }
01552
01553 return $aRet;
01554 }
01555
01567 protected function _addOrderArticlesToBasket( $oUser = null, $aOrderArticles = null, $blChangeDelivery = false, $blChangeDiscount = false, $blStockCheck = true )
01568 {
01569 $oBasket = oxNew( "oxbasket" );
01570
01571
01572 $oBasket->setStockCheckMode( $blStockCheck );
01573
01574
01575 $oBasket->setBasketUser( $oUser );
01576
01577
01578 $aCurrencies = $this->getConfig()->getCurrencyArray();
01579 foreach ( $aCurrencies as $oCur ) {
01580 if ($oCur->name == $this->oxorder__oxcurrency->value) {
01581 $oBasketCur = $oCur;
01582 break;
01583 }
01584 }
01585
01586 $oBasket->setBasketCurrency( $oBasketCur );
01587
01588
01589 if ( count($aOrderArticles ) < 1 ) {
01590 return $oBasket;
01591 }
01592
01593
01594 foreach ( $aOrderArticles as $oOrderArticle ) {
01595
01596 $aPersParam = null;
01597 if ( $oOrderArticle->oxorderarticles__oxpersparam->value ) {
01598 $aPersParam = unserialize( $oOrderArticle->oxorderarticles__oxpersparam->value );
01599 }
01600
01601 $aSel = $this->_makeSelListArray( $oOrderArticle->oxorderarticles__oxartid->value,
01602 $oOrderArticle->oxorderarticles__oxselvariant->value );
01603
01604 $oBasketItem = $oBasket->addToBasket( $oOrderArticle->oxorderarticles__oxartid->value,
01605 $oOrderArticle->oxorderarticles__oxamount->value, $aSel, $aPersParam );
01606 if ( $oBasketItem ) {
01607 $oBasketItem->setWrapping( $oOrderArticle->oxorderarticles__oxwrapid->value );
01608 }
01609 }
01610
01611
01612 $oBasket->setCardId( $this->oxorder__oxcardid->value );
01613 $oBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01614
01615
01616 $oBasket->setSkipVouchersChecking( true );
01617
01618
01619 $sQ = 'select oxid from oxvouchers where oxorderid = "'.$this->getId().'"';
01620 $aVouchers = oxDb::getDb( true )->getAll( $sQ );
01621 foreach ( $aVouchers AS $aVoucher ) {
01622 $oBasket->addVoucher($aVoucher['oxid']);
01623 }
01624
01625
01626 $oBasket->setShipping( $this->oxorder__oxdeltype->value );
01627
01628
01629 if ( $blChangeDelivery ) {
01630 $oBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01631 }
01632 if ( $blChangeDiscount ) {
01633 $oBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
01634 }
01635
01636 $oBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01637
01638
01639 $oBasket->calculateBasket( true );
01640
01641 return $oBasket;
01642 }
01643
01651 protected function _setDeprecatedValues()
01652 {
01653 if ( $this->oxorder__oxstorno->value != 1 ) {
01654 $oCur = $this->getConfig()->getActShopCurrencyObject();
01655 $oLang = oxLang::getInstance();
01656
01657 $this->totalnetsum = $this->oxorder__oxtotalnetsum->value;
01658 $this->totalbrutsum = $this->oxorder__oxtotalbrutsum->value;
01659 $this->totalorder = $this->oxorder__oxtotalordersum->value;
01660 $this->ftotalnetsum = $oLang->formatCurrency( $this->oxorder__oxtotalnetsum->value, $oCur );
01661 $this->ftotalbrutsum = $oLang->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $oCur );
01662 $this->fdelcost = $oLang->formatCurrency( $this->oxorder__oxdelcost->value, $oCur );
01663 $this->fpaycost = $oLang->formatCurrency( $this->oxorder__oxpaycost->value, $oCur );
01664 $this->fwrapcost = $oLang->formatCurrency( $this->oxorder__oxwrapcost->value, $oCur );
01665 $this->ftotalorder = $this->getTotalOrderSum();
01666 $this->totalvouchers = 0;
01667
01668 if ( $this->oxorder__oxvoucherdiscount->value ) {
01669 $this->totalvouchers = $oLang->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $oCur );
01670 }
01671
01672 if ( $this->oxorder__oxdiscount->value ) {
01673 $this->discount = $this->oxorder__oxdiscount->value;
01674 $this->fdiscount = $oLang->formatCurrency( $this->oxorder__oxdiscount->value, $oCur );
01675 }
01676 }
01677 }
01678
01684 public function getTotalOrderSum()
01685 {
01686 $oCur = $this->getConfig()->getActShopCurrencyObject();
01687 return number_format( $this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01688 }
01689
01695 public function getBillCountry()
01696 {
01697 if( !$this->oxorder__oxbillcountry->value ) {
01698 $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
01699 }
01700 return $this->oxorder__oxbillcountry;
01701 }
01702
01708 public function getDelCountry()
01709 {
01710 if( !$this->oxorder__oxdelcountry->value ) {
01711 $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
01712 }
01713 return $this->oxorder__oxdelcountry;
01714 }
01715
01716 }