00001 <?php
00002
00006 require_once oxConfig::getInstance()->getConfigParam( 'sCoreDir' ) . 'fpdf/oxpdf.php';
00007
00012 class oxOrder extends oxBase
00013 {
00014
00020 protected $_aSkipSaveFields = array( 'oxorderdate' );
00021
00027 protected $_oArticles = null;
00028
00034 protected $_oDelSet = null;
00035
00041 protected $_oGiftCard = null;
00042
00048 protected $_oPaymentType = null;
00049
00055 protected $_oPayment = null;
00056
00062 protected $_aVoucherList = null;
00063
00069 protected $_oDelPrice = null;
00070
00076 protected $_oUser = null;
00077
00083 protected $_oBasket = null;
00084
00090 protected $_oWrappingPrice = null;
00091
00097 protected $_oPaymentPrice = null;
00098
00104 protected $_sClassName = 'oxorder';
00105
00111 protected $_blSeparateNumbering = null;
00112
00118 protected $_iOrderLang = null;
00119
00123 public function __construct()
00124 {
00125 parent::__construct();
00126 $this->init( 'oxorder' );
00127
00128
00129 $this->setSeparateNumbering( $this->getConfig()->getConfigParam( 'blSeparateNumbering') );
00130
00131 }
00132
00140 public function __get( $sName )
00141 {
00142 if ( $sName == 'oDelSet' ) {
00143 return $this->getDelSet();
00144 }
00145
00146 if ( $sName == 'oxorder__oxbillcountry' ) {
00147 return $this->getBillCountry();
00148 }
00149
00150 if ( $sName == 'oxorder__oxdelcountry' ) {
00151 return $this->getDelCountry();
00152 }
00153 }
00154
00162 public function assign( $dbRecord )
00163 {
00164
00165 parent::assign( $dbRecord );
00166
00167 $oUtilsDate = oxUtilsDate::getInstance();
00168
00169
00170 $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value));
00171 $this->oxorder__oxsenddate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value));
00172
00173
00174 $this->_setDeprecatedValues();
00175 }
00176
00184 protected function _getCountryTitle( $sCountryId )
00185 {
00186 $sTitle = null;
00187 if ( $sCountryId && $sCountryId != '-1' ) {
00188 $oCountry = oxNew( 'oxcountry' );
00189 $oCountry->load( $sCountryId );
00190 $sTitle = $oCountry->oxcountry__oxtitle->value;
00191 }
00192
00193 return $sTitle;
00194 }
00195
00201 public function getOrderArticles()
00202 {
00203 if ( $this->_oArticles == null ) {
00204
00205
00206 $this->_oArticles = oxNew( 'oxlist' );
00207 $this->_oArticles->init( 'oxorderarticle' );
00208
00209 $sSelect = 'select oxorderarticles.* from oxorderarticles
00210 where oxorderarticles.oxorderid="'.$this->getId().'"
00211 order by oxorderarticles.oxartid';
00212 $this->_oArticles->selectString( $sSelect );
00213 }
00214
00215 return $this->_oArticles;
00216 }
00217
00223 public function getOrderDeliveryPrice()
00224 {
00225 if ( $this->_oDelPrice != null ) {
00226 return $this->_oDelPrice;
00227 }
00228
00229 $this->_oDelPrice = oxNew( 'oxprice' );
00230 $this->_oDelPrice->setBruttoPriceMode();
00231 $this->_oDelPrice->setPrice( $this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value );
00232 return $this->_oDelPrice;
00233 }
00234
00240 public function getOrderWrappingPrice()
00241 {
00242 if ( $this->_oWrappingPrice != null ) {
00243 return $this->_oWrappingPrice;
00244 }
00245
00246 $this->_oWrappingPrice = oxNew( 'oxprice' );
00247 $this->_oWrappingPrice->setBruttoPriceMode();
00248 $this->_oWrappingPrice->setPrice( $this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value );
00249 return $this->_oWrappingPrice;
00250 }
00251
00257 public function getOrderPaymentPrice()
00258 {
00259 if ( $this->_oPaymentPrice != null ) {
00260 return $this->_oPaymentPrice;
00261 }
00262
00263 $this->_oPaymentPrice = oxNew( 'oxprice' );
00264 $this->_oPaymentPrice->setBruttoPriceMode();
00265 $this->_oPaymentPrice->setPrice( $this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value );
00266 return $this->_oPaymentPrice;
00267 }
00268
00275 public function getOrderNetSum()
00276 {
00277 $dTotalNetSum = 0;
00278
00279 $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
00280 $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
00281 $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
00282 $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
00283
00284 return $dTotalNetSum;
00285 }
00286
00307 public function finalizeOrder( oxBasket $oBasket, $oUser, $blRecalculatingOrder = false )
00308 {
00309
00310 $sGetChallenge = oxSession::getVar( 'sess_challenge' );
00311 if ( $this->_checkOrderExist( $sGetChallenge ) ) {
00312 oxUtils::getInstance()->logger( 'BLOCKER' );
00313
00314 return 3;
00315 }
00316
00317
00318 if ( !$blRecalculatingOrder ) {
00319
00320 $this->setId( $sGetChallenge );
00321 }
00322
00323
00324 $this->_setUser( $oUser );
00325
00326
00327 $this->_loadFromBasket( $oBasket );
00328
00329
00330 $oUserPayment = $this->_setPayment( $oBasket->getPaymentId() );
00331
00332
00333
00334 if ( !$blRecalculatingOrder ) {
00335 $this->_setFolder();
00336 }
00337
00338
00339 $this->save();
00340
00341
00342
00343 if ( !$blRecalculatingOrder ) {
00344 $blRet = $this->_executePayment( $oBasket, $oUserPayment );
00345 if ( $blRet !== true ) {
00346 return $blRet;
00347 }
00348 }
00349
00350
00351 oxSession::deleteVar( 'ordrem' );
00352
00353
00354 $this->_setOrderStatus( 'OK' );
00355
00356
00357 $oBasket->setOrderId( $this->getId() );
00358
00359
00360 $this->_updateStock();
00361
00362
00363 $this->_updateWishlist( $oBasket->getContents(), $oUser );
00364
00365
00366 $this->_updateNoticeList( $oBasket->getContents(), $oUser );
00367
00368
00369
00370 if ( !$blRecalculatingOrder ) {
00371 $this->_markVouchers( $oBasket, $oUser );
00372 }
00373
00374
00375
00376 if ( !$blRecalculatingOrder ) {
00377 $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
00378 } else {
00379 $iRet = 1;
00380 }
00381
00382 return $iRet;
00383 }
00384
00392 protected function _setOrderStatus( $sStatus )
00393 {
00394 $sQ = 'update oxorder set oxtransstatus="'.$sStatus.'" where oxid="'.$this->getId().'" ';
00395 oxDb::getDb()->execute( $sQ );
00396 }
00397
00408 protected function _loadFromBasket( oxBasket $oBasket )
00409 {
00410 $myConfig = $this->getConfig();
00411
00412
00413 if ( $myConfig->getConfigParam( 'blStoreIPs' ) && $this->oxorder__oxip->value === null ) {
00414 $this->oxorder__oxip = new oxField(oxUtilsServer::getInstance()->getRemoteAddress(), oxField::T_RAW);
00415 }
00416
00417
00418 $this->oxorder__oxtotalnetsum = new oxField(oxUtils::getInstance()->fRound($oBasket->getProductsPrice()->getNettoSum()), oxField::T_RAW);
00419 $this->oxorder__oxtotalbrutsum = new oxField($oBasket->getProductsPrice()->getBruttoSum(), oxField::T_RAW);
00420 $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00421
00422
00423 if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00424 $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00425 $this->oxorder__oxpayvat = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00426 }
00427
00428
00429 if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00430 $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00431
00432 $this->oxorder__oxdelvat = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW);
00433 $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00434 }
00435
00436
00437 if ( $this->oxorder__oxremark->value === null ) {
00438 $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00439 }
00440
00441
00442 $oCur = $myConfig->getActShopCurrencyObject();
00443 $this->oxorder__oxcurrency = new oxField($oCur->name);
00444 $this->oxorder__oxcurrate = new oxField($oCur->rate, oxField::T_RAW);
00445
00446
00447 if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00448 $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00449 }
00450
00451
00452 $dDiscount = 0;
00453 $aDiscounts = $oBasket->getDiscounts();
00454 if ( count($aDiscounts) > 0 ) {
00455 foreach ($aDiscounts as $oDiscount) {
00456 $dDiscount += $oDiscount->dDiscount;
00457 }
00458 }
00459 if ( $dDiscount ) {
00460 $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00461 }
00462
00463
00464 $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00465
00466
00467
00468 $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00469
00470
00471 $this->_setOrderArticles( $oBasket->getContents() );
00472
00473
00474 $this->_setWrapping( $oBasket );
00475 }
00476
00483 public function getOrderLanguage()
00484 {
00485 if ( $this->_iOrderLang === null ) {
00486 if ( isset( $this->oxorder__oxlang->value ) ) {
00487 $this->_iOrderLang = oxLang::getInstance()->validateLanguage( $this->oxorder__oxlang->value );
00488 } else {
00489 $this->_iOrderLang = oxLang::getInstance()->getBaseLanguage();
00490 }
00491 }
00492 return $this->_iOrderLang;
00493 }
00494
00502 protected function _setUser( $oUser )
00503 {
00504
00505 $this->oxorder__oxuserid = new oxField($oUser->getId());
00506
00507
00508 $this->oxorder__oxbillcompany = clone $oUser->oxuser__oxcompany;
00509 $this->oxorder__oxbillemail = clone $oUser->oxuser__oxusername;
00510 $this->oxorder__oxbillfname = clone $oUser->oxuser__oxfname;
00511 $this->oxorder__oxbilllname = clone $oUser->oxuser__oxlname;
00512 $this->oxorder__oxbillstreet = clone $oUser->oxuser__oxstreet;
00513 $this->oxorder__oxbillstreetnr = clone $oUser->oxuser__oxstreetnr;
00514 $this->oxorder__oxbilladdinfo = clone $oUser->oxuser__oxaddinfo;
00515 $this->oxorder__oxbillustid = clone $oUser->oxuser__oxustid;
00516 $this->oxorder__oxbillcity = clone $oUser->oxuser__oxcity;
00517 $this->oxorder__oxbillcountryid = clone $oUser->oxuser__oxcountryid;
00518 $this->oxorder__oxbillzip = clone $oUser->oxuser__oxzip;
00519 $this->oxorder__oxbillfon = clone $oUser->oxuser__oxfon;
00520 $this->oxorder__oxbillfax = clone $oUser->oxuser__oxfax;
00521 $this->oxorder__oxbillsal = clone $oUser->oxuser__oxsal;
00522
00523
00524
00525 if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00526
00527 $this->oxorder__oxdelcompany = clone $oDelAdress->oxaddress__oxcompany;
00528 $this->oxorder__oxdelfname = clone $oDelAdress->oxaddress__oxfname;
00529 $this->oxorder__oxdellname = clone $oDelAdress->oxaddress__oxlname;
00530 $this->oxorder__oxdelstreet = clone $oDelAdress->oxaddress__oxstreet;
00531 $this->oxorder__oxdelstreetnr = clone $oDelAdress->oxaddress__oxstreetnr;
00532 $this->oxorder__oxdeladdinfo = clone $oDelAdress->oxaddress__oxaddinfo;
00533 $this->oxorder__oxdelcity = clone $oDelAdress->oxaddress__oxcity;
00534 $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00535 $this->oxorder__oxdelzip = clone $oDelAdress->oxaddress__oxzip;
00536 $this->oxorder__oxdelfon = clone $oDelAdress->oxaddress__oxfon;
00537 $this->oxorder__oxdelfax = clone $oDelAdress->oxaddress__oxfax;
00538 $this->oxorder__oxdelsal = clone $oDelAdress->oxaddress__oxsal;
00539 }
00540 }
00541
00549 protected function _setWrapping( oxBasket $oBasket )
00550 {
00551 $myConfig = $this->getConfig();
00552
00553
00554 if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00555 $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00556
00557
00558 if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
00559 $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00560 }
00561 }
00562
00563
00564 $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00565
00566
00567 $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00568 }
00569
00578 protected function _setOrderArticles( $aArticleList )
00579 {
00580
00581 $this->_oArticles = oxNew( 'oxlist' );
00582 $iCurrLang = $this->getOrderLanguage();
00583
00584
00585 foreach ( $aArticleList as $oContent ) {
00586
00587
00588 $oProduct = $oContent->getArticle();
00589
00590
00591 if ( $iCurrLang != $oProduct->getLanguage() ) {
00592 $oProduct->loadInLang( $iCurrLang, $oProduct->getId() );
00593 }
00594
00595 $aSelList = array();
00596
00597 $sSelList = '';
00598 if ( count($oContent->getChosenSelList()) ) {
00599 foreach ( $oContent->getChosenSelList() as $oItem ) {
00600 $aSelList[] = $oItem->name.' : '.$oItem->value;
00601 }
00602 $sSelList = implode( ', ', $aSelList );
00603 }
00604
00605 $oOrderArticle = oxNew( 'oxorderarticle' );
00606
00607
00608 $oOrderArticle->copyThis( $oProduct );
00609
00610
00611
00612 $oOrderArticle->setId();
00613 $oOrderArticle->oxorderarticles__oxorderid = new oxField($this->getId());
00614 $oOrderArticle->oxorderarticles__oxartid = new oxField($oContent->getProductId());
00615 $oOrderArticle->oxorderarticles__oxamount = new oxField($oContent->getAmount());
00616
00617
00618 $oOrderArticle->oxorderarticles__oxartnum = clone $oProduct->oxarticles__oxartnum;
00619
00620
00621 $oOrderArticle->oxorderarticles__oxselvariant = new oxField(trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->value ), oxField::T_RAW);
00622
00623
00624 $oOrderArticle->oxorderarticles__oxtitle = new oxField(trim( $oProduct->oxarticles__oxtitle->value.' '.$oOrderArticle->oxorderarticles__oxselvariant->value ), oxField::T_RAW);
00625
00626
00627 $oOrderArticle->oxorderarticles__oxshortdesc = new oxField( $oProduct->oxarticles__oxshortdesc->value, oxField::T_RAW);
00628
00629
00630 $oOrderArticle->oxorderarticles__oxnetprice = new oxField($oContent->getPrice()->getNettoPrice(), oxField::T_RAW);
00631 $oOrderArticle->oxorderarticles__oxvatprice = new oxField($oContent->getPrice()->getVATValue(), oxField::T_RAW);
00632 $oOrderArticle->oxorderarticles__oxbrutprice = new oxField($oContent->getPrice()->getBruttoPrice(), oxField::T_RAW);
00633 $oOrderArticle->oxorderarticles__oxnprice = new oxField($oContent->getUnitPrice()->getNettoPrice(), oxField::T_RAW);
00634 $oOrderArticle->oxorderarticles__oxbprice = new oxField($oContent->getUnitPrice()->getBruttoPrice(), oxField::T_RAW);
00635 $oOrderArticle->oxorderarticles__oxvat = new oxField($oContent->getPrice()->getVAT(), oxField::T_RAW);
00636
00637
00638 $oOrderArticle->oxorderarticles__oxwrapid = new oxField($oContent->getWrappingId(), oxField::T_RAW);
00639
00640
00641 $oOrderArticle->oxorderarticles__oxordershopid = new oxField($oContent->getShopId(), oxField::T_RAW);
00642
00643
00644 if ( count( $oProduct->getPersParams() ) ) {
00645 $oOrderArticle->oxorderarticles__oxpersparam = new oxField(serialize( $oProduct->getPersParams() ), oxField::T_RAW);
00646 } elseif ( count( $oContent->getPersParams() ) ) {
00647 $oOrderArticle->oxorderarticles__oxpersparam = new oxField(serialize( $oContent->getPersParams()), oxField::T_RAW);
00648 }
00649
00650
00651
00652
00653 $oOrderArticle->oProduct = $oProduct;
00654
00655
00656 $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
00657 }
00658 }
00659
00671 protected function _executePayment( oxBasket $oBasket, $oUserpayment )
00672 {
00673 $oPayTransaction = $this->_getGateway();
00674 $oPayTransaction->setPaymentParams( $oUserpayment );
00675
00676 if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
00677 $this->delete();
00678
00679
00680 if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
00681 if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
00682 return $sLastError;
00683 }
00684 }
00685
00686
00687 if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
00688 if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
00689 return $iLastErrorNo;
00690 }
00691 }
00692
00693 return 2;
00694 }
00695 return true;
00696 }
00697
00704 protected function _getGateway()
00705 {
00706 return oxNew( 'oxPaymentGateway' );
00707 }
00708
00716 protected function _setPayment( $sPaymentid )
00717 {
00718
00719 $aDynvalue = oxSession::getVar( 'dynvalue' );
00720 $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
00721
00722
00723 $oPayment = oxNew( 'oxpayment' );
00724
00725 if (!$oPayment->load( $sPaymentid )) {
00726 return null;
00727 }
00728
00729
00730 if ( !$aDynvalue && ( $oUserpayment = $this->getPaymentType() ) ) {
00731 $aStoredDynvalue = $oUserpayment->getDynValues();
00732 foreach ( $aStoredDynvalue as $oVal ) {
00733 $aDynvalue[$oVal->name] = $oVal->value;
00734 }
00735 }
00736
00737 $oPayment->setDynValues( oxUtils::getInstance()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
00738
00739
00740 $aDynVal = array();
00741
00742 $aPaymentDynValues = $oPayment->getDynValues();
00743 foreach ( $aPaymentDynValues as $key => $oVal ) {
00744 if ( isset( $aDynvalue[$oVal->name] ) ) {
00745 $oVal->value = $aDynvalue[$oVal->name];
00746 }
00747
00748
00749 $aPaymentDynValues[$key] = $oVal;
00750 $aDynVal[$oVal->name] = $oVal->value;
00751 }
00752
00753
00754
00755
00756 $oUserpayment = oxNew( 'oxuserpayment' );
00757 $oUserpayment->oxuserpayments__oxuserid = clone $this->oxorder__oxuserid;
00758 $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
00759 $oUserpayment->oxuserpayments__oxvalue = new oxField(oxUtils::getInstance()->assignValuesToText( $aDynVal ), oxField::T_RAW);
00760 $oUserpayment->oxpayments__oxdesc = clone $oPayment->oxpayments__oxdesc;
00761 $oUserpayment->setDynValues( $aPaymentDynValues );
00762 $oUserpayment->save();
00763
00764
00765 $this->oxorder__oxpaymentid = new oxField($oUserpayment->getId(), oxField::T_RAW);
00766 $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
00767
00768
00769 return $oUserpayment;
00770 }
00771
00777 protected function _setFolder()
00778 {
00779 $myConfig = $this->getConfig();
00780 $this->oxorder__oxfolder = new oxField(key( $myConfig->getShopConfVar( 'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
00781 }
00782
00792 protected function _updateWishlist( $aArticleList, $oUser )
00793 {
00794
00795 foreach ( $aArticleList as $oContent) {
00796 if ( ( $sWishId = $oContent->getWishId() ) ) {
00797
00798
00799 if ( $sWishId == $oUser->getId() ) {
00800 $oUserBasket = $oUser->getBasket( 'wishlist' );
00801 } else {
00802 $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
00803 $oUserBasket = oxNew( 'oxuserbasket' );
00804 $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
00805 }
00806
00807
00808 if ( $oUserBasket ) {
00809 if ( !($sProdId = $oContent->getWishArticleId() )) {
00810 $sProdId = $oContent->getProductId();
00811 }
00812 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00813 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00814 if ( $dNewAmount < 0) {
00815 $dNewAmount = 0;
00816 }
00817 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00818 }
00819 }
00820 }
00821 }
00822
00832 protected function _updateNoticeList( $aArticleList, $oUser )
00833 {
00834
00835 if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
00836
00837 foreach ( $aArticleList as $oContent) {
00838 $sProdId = $oContent->getProductId();
00839
00840
00841 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00842 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00843 if ( $dNewAmount < 0) {
00844 $dNewAmount = 0;
00845 }
00846 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00847 }
00848 }
00849 }
00850
00856 protected function _updateStock()
00857 {
00858 $myConfig = $this->getConfig();
00859 $blUseStock = $myConfig->getConfigParam( 'blUseStock' );
00860
00861
00862 $oOrderArticles = $this->getOrderArticles();
00863 if ( $oOrderArticles && count( $oOrderArticles ) > 0 && $blUseStock ) {
00864 foreach ( $oOrderArticles as $oOrderArticle ) {
00865 $oOrderArticle->updateArticleStock( $oOrderArticle->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
00866 }
00867 }
00868 }
00869
00881 protected function _markVouchers( $oBasket, $oUser )
00882 {
00883 $this->_aVoucherList = $oBasket->getVouchers();
00884
00885 if ( is_array( $this->_aVoucherList ) ) {
00886 foreach ( array_keys( $this->_aVoucherList ) as $sVoucherId ) {
00887 $oVoucher = oxNew( 'oxvoucher' );
00888 $oVoucher->load( $sVoucherId );
00889 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value );
00890
00891
00892 $oVoucher->oxmodvouchers__oxvouchernr = $oVoucher->oxvouchers__oxvouchernr;
00893 $oVSerie = $oVoucher->getSerie();
00894 $oVoucher->oxmodvouchers__oxdiscount = clone $oVSerie->oxvoucherseries__oxdiscount;
00895 $oVoucher->oxmodvouchers__oxdiscounttype = clone $oVSerie->oxvoucherseries__oxdiscounttype;
00896
00897
00898 $this->_aVoucherList[$sVoucherId] = $oVoucher;
00899 }
00900 }
00901 }
00902
00908 public function save()
00909 {
00910 if ( ( $blSave = parent::save() ) ) {
00911
00912
00913 $oOrderArticles = $this->getOrderArticles();
00914 if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
00915 foreach ( $oOrderArticles as $oOrderArticle ) {
00916 $oOrderArticle->save();
00917 }
00918 }
00919 }
00920
00921 return $blSave;
00922 }
00923
00930 public function getDelAddressInfo()
00931 {
00932 $oDelAdress = null;
00933 if ( ( $soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
00934 $oDelAdress = oxNew( 'oxbase' );
00935 $oDelAdress->init( 'oxaddress' );
00936 $oDelAdress->load( $soxAddressId );
00937
00938
00939 if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
00940 $oCountry = oxNew( 'oxcountry' );
00941 $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
00942 $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
00943 }
00944 }
00945 return $oDelAdress;
00946 }
00947
00958 public function validateStock( $oBasket )
00959 {
00960 foreach ( $oBasket->getContents() as $oContent ) {
00961 $oProd = $oContent->getArticle();
00962
00963
00964 $iOnStock = $oProd->checkForStock( $oContent->getAmount() );
00965 if ( $iOnStock !== true ) {
00966 $oEx = oxNew( 'oxOutOfStockException' );
00967 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
00968 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
00969 $oEx->setRemainingAmount( $oProd->oxarticles__oxstock->value );
00970 throw $oEx;
00971 }
00972 }
00973 }
00974
00980 protected function _insert()
00981 {
00982 $myConfig = $this->getConfig();
00983 $oUtilsDate = oxUtilsDate::getInstance();
00984
00985
00986 if ( !$this->oxorder__oxorderdate->value ) {
00987 $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
00988 } else {
00989 $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
00990 }
00991 $this->oxorder__oxshopid = new oxField($myConfig->getShopId(), oxField::T_RAW);
00992
00993 $this->oxorder__oxsenddate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
00994
00995 if ( ( $blInsert = parent::_insert() ) ) {
00996
00997 if ( !$this->oxorder__oxordernr->value ) {
00998 $aWhere = '';
00999
01000 if ( $this->_blSeparateNumbering ) {
01001 $aWhere = array( 'oxshopid = "'.$myConfig->getShopId().'"' );
01002 }
01003 $this->_setRecordNumber( 'oxordernr', $aWhere );
01004 }
01005 }
01006 return $blInsert;
01007 }
01008
01014 protected function _update()
01015 {
01016 $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01017 return parent::_update();
01018 }
01019
01028 public function delete( $sOxId = null )
01029 {
01030 if ( $sOxId ) {
01031 if ( !$this->load( $sOxId ) ) {
01032
01033 return false;
01034 }
01035 } elseif ( !$sOxId ) {
01036 $sOxId = $this->getId();
01037 }
01038
01039
01040 if ( !$sOxId ) {
01041 return false;
01042 }
01043
01044
01045
01046 $myConfig = $this->getConfig();
01047 $blUseStock = $myConfig->getConfigParam( 'blUseStock' );
01048
01049 $oOrderArticles = $this->getOrderArticles();
01050 foreach ( $oOrderArticles as $oOrderArticle ) {
01051
01052 if ( $blUseStock ) {
01053 if ( !$oOrderArticle->oxorderarticles__oxstorno->value ) {
01054 $oOrderArticle->updateArticleStock( $oOrderArticle->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
01055 }
01056 }
01057 $oOrderArticle->delete();
01058 }
01059
01060
01061 if ( $oPaymentType = $this->getPaymentType() ) {
01062 $oPaymentType->delete();
01063 }
01064
01065 return parent::delete( $sOxId );
01066 }
01067
01079 public function recalculateOrder( $aNewOrderArticles = array(), $blChangeDelivery = false, $blChangeDiscount = false )
01080 {
01081 oxDb::startTransaction();
01082
01083 try {
01084
01085
01086 $this->delete();
01087
01088 $oUser = oxNew( "oxuser" );
01089 $oUser->load( $this->oxorder__oxuserid->value );
01090
01091
01092 $oBasket = oxNew( "oxBasket" );
01093
01094 $aCanceledArticles = array();
01095 $aArticlesIds = array();
01096
01097
01098 if ( $this->_oArticles = $this->getOrderArticles() ) {
01099 $this->_oArticles->rewind();
01100 while ( $oOrderArticle = $this->_oArticles->current() ) {
01101 $sOrderArticleId = $this->_oArticles->key();
01102
01103
01104 $aArticlesIds[$sOrderArticleId] = $oOrderArticle->oxorderarticles__oxartid->value;
01105
01106
01107
01108 if ( $oOrderArticle->oxorderarticles__oxstorno->value == '1') {
01109 $aCanceledArticles[$sOrderArticleId] = $oOrderArticle;
01110
01111
01112
01113 $this->_oArticles->offsetUnset( $sOrderArticleId );
01114 } else {
01115 $this->_oArticles->next();
01116 }
01117 }
01118 }
01119
01120
01121 foreach ($aNewOrderArticles as $oNewOrderArticle) {
01122
01123 $sNewOrderArtId = null;
01124 $blIsOldOrderArticle = false;
01125
01126
01127 if ( ( $sNewOrderArtId = array_search( $oNewOrderArticle->oxorderarticles__oxartid->value, $aArticlesIds ) ) !== false ) {
01128 $blIsOldOrderArticle = true;
01129 }
01130
01131
01132 if ( $oNewOrderArticle->oxorderarticles__oxamount->value == 0 ) {
01133
01134
01135 if ( array_key_exists( $sNewOrderArtId, $aCanceledArticles) ) {
01136
01137
01138 $this->_oArticles->offsetSet( $sNewOrderArtId, $aCanceledArticles[$sNewOrderArtId] );
01139
01140
01141
01142 unset($aCanceledArticles[$sNewOrderArtId]);
01143 }
01144 }
01145
01146 if ( $blIsOldOrderArticle ) {
01147
01148 $this->_oArticles->offsetGet( $sNewOrderArtId )->oxorderarticles__oxamount = clone $oNewOrderArticle->oxorderarticles__oxamount;
01149
01150 } else {
01151
01152 $this->_oArticles->offsetSet( $oNewOrderArticle->getId(), $oNewOrderArticle );
01153
01154 }
01155 }
01156
01157
01158 $oBasket = $this->_addOrderArticlesToBasket( $oUser, $this->_oArticles, $blChangeDelivery, $blChangeDiscount );
01159
01160
01161 $iRet = $this->finalizeOrder( $oBasket, $oUser, true );
01162
01163
01164 if ( count($aCanceledArticles) > 0 ) {
01165 foreach ($aCanceledArticles as $oCanceledOrderArticle ) {
01166 $oCanceledOrderArticle->save();
01167 }
01168 }
01169
01170
01171 if ( $iRet !== 1 ) {
01172 oxDb::rollbackTransaction();
01173 } else {
01174 oxDb::commitTransaction();
01175 }
01176
01177 } catch( Exception $oE ) {
01178
01179 oxDb::rollbackTransaction();
01180 }
01181 }
01182
01190 public function pdfFooter( $oPdf )
01191 {
01192 }
01193
01201 public function pdfHeaderplus( $oPdf )
01202 {
01203 }
01204
01212 public function pdfHeader( $oPdf )
01213 {
01214 }
01215
01224 public function genPdf( $sFilename, $iSelLang = 0 )
01225 {
01226 }
01227
01233 public function getInvoiceNum()
01234 {
01235 $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01236 return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01237 }
01238
01244 public function getNextBillNum()
01245 {
01246 $sQ = 'select max(oxorder.oxbillnr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01247 return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01248 }
01249
01255 public function getShippingSetList()
01256 {
01257
01258 if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
01259 $sShipId = $this->oxorder__oxbillcountryid->value;
01260 }
01261
01262 $oUser = oxNew( "oxuser" );
01263 $oUser->load( $this->oxorder__oxuserid->value );
01264
01265
01266 $oBasket = $this->_addOrderArticlesToBasket( $oUser, $this->getOrderArticles(), false, false, false );
01267
01268
01269 $oDeliveryList = oxNew( "oxDeliveryList", "core" );
01270 $oDeliveryList->setCollectFittingDeliveriesSets( true );
01271
01272 return $oDeliveryList->getDeliveryList( $oBasket, $oUser, $sShipId );
01273 }
01274
01280 public function getVoucherNrList()
01281 {
01282 $oDB = oxDb::getDb( true );
01283 $aVouchers = array();
01284 $sSelect = "select oxvouchernr from oxvouchers where oxorderid = '".$this->oxorder__oxid->value."'";
01285 $rs = $oDB->execute( $sSelect);
01286 if ($rs != false && $rs->recordCount() > 0) {
01287 while (!$rs->EOF) {
01288 $aVouchers[] = $rs->fields['oxvouchernr'];
01289 $rs->moveNext();
01290 }
01291 }
01292 return $aVouchers;
01293 }
01294
01302 public function getOrderSum( $blToday = false )
01303 {
01304 $sSelect = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01305 $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01306
01307 if ( $blToday ) {
01308 $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01309 }
01310
01311 return ( double ) oxDb::getDb()->getOne( $sSelect );
01312 }
01313
01321 public function getOrderCnt( $blToday = false )
01322 {
01323 $sSelect = 'select count(*) from oxorder where ';
01324 $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01325
01326 if ( $blToday ) {
01327 $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01328 }
01329
01330 return ( int ) oxDb::getDb()->getOne( $sSelect );
01331 }
01332
01333
01341 protected function _checkOrderExist( $sOxId = null )
01342 {
01343 if ( !$sOxId) {
01344 return false;
01345 }
01346
01347 if ( oxDb::getDb()->getOne( 'select oxid from oxorder where oxid = "'.$sOxId.'"' ) ) {
01348 return true;
01349 }
01350
01351 return false;
01352 }
01353
01363 protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01364 {
01365 $iRet = 0;
01366
01367
01368 $this->_oUser = $oUser;
01369 $this->_oBasket = $oBasket;
01370 $this->_oPayment = $oPayment;
01371
01372 $oxEmail = oxNew( 'oxemail' );
01373
01374
01375 if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01376
01377 $iRet = 1;
01378 }
01379
01380
01381 $oxEmail->sendOrderEMailToOwner( $this );
01382
01383 return $iRet;
01384 }
01385
01391 public function getUser()
01392 {
01393 return $this->_oUser;
01394 }
01395
01401 public function getBasket()
01402 {
01403 return $this->_oBasket;
01404 }
01405
01411 public function getPayment()
01412 {
01413 return $this->_oPayment;
01414 }
01415
01421 public function getVoucherList()
01422 {
01423 return $this->_aVoucherList;
01424 }
01425
01431 public function getDelSet()
01432 {
01433 if ( $this->_oDelSet == null ) {
01434
01435 $this->_oDelSet = oxNew( 'oxdeliveryset' );
01436 $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01437 }
01438
01439 return $this->_oDelSet;
01440 }
01441
01447 public function getPaymentType()
01448 {
01449 if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType == null ) {
01450 $this->_oPaymentType = oxNew( 'oxuserpayment' );
01451 $this->_oPaymentType->load( $this->oxorder__oxpaymentid->value );
01452 }
01453
01454 return $this->_oPaymentType;
01455 }
01456
01462 public function getGiftCard()
01463 {
01464 if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01465 $this->_oGiftCard = oxNew( 'oxwrapping' );
01466 $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01467 }
01468
01469 return $this->_oGiftCard;
01470 }
01471
01479 public function setSeparateNumbering( $blSeparateNumbering = null )
01480 {
01481 $this->_blSeparateNumbering = $blSeparateNumbering;
01482 }
01483
01491 public function getLastUserPaymentType( $sUserId)
01492 {
01493 $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid="'.$sUserId.'" order by oxorder.oxorderdate desc ';
01494 $sLastPaymentId = oxDb::getDb()->getOne( $sQ );
01495 return $sLastPaymentId;
01496 }
01497
01508 protected function _makeSelListArray( $sArtId = null, $sOrderArtSelList = null )
01509 {
01510 $aList = array();
01511 $aRet = array();
01512
01513 if ( $sArtId ) {
01514 $aList = explode( ",", $sOrderArtSelList );
01515 $oStr = getStr();
01516
01517
01518 $oArticle = oxNew( "oxArticle" );
01519 $oArticle->load( $sArtId );
01520 $aArticleSelList = $oArticle->getSelectLists();
01521
01522
01523 foreach ( $aList as $sList ) {
01524 if ( $sList ) {
01525
01526 $aVal = explode( ":", $sList );
01527 if ( isset($aVal[0]) && isset($aVal[1])) {
01528 $sOrderArtListTitle = $oStr->strtolower( trim($aVal[0]) );
01529 $sOrderArtSelValue = $oStr->strtolower( trim($aVal[1]) );
01530
01531
01532 $iSelListNum = 0;
01533 if ( count($aArticleSelList) > 0 ) {
01534 foreach ( $aArticleSelList as $aSelect ) {
01535
01536
01537 if ( $oStr->strtolower($aSelect['name']) == $sOrderArtListTitle ) {
01538
01539 $iSelValueNum = 0;
01540 foreach ( $aSelect as $oSel ) {
01541 if ( $oStr->strtolower($oSel->name) == $sOrderArtSelValue ) {
01542
01543 $aRet[$iSelListNum] = $iSelValueNum;
01544 }
01545
01546 $iSelValueNum++;
01547 }
01548 }
01549
01550 $iSelListNum++;
01551 }
01552 }
01553 }
01554 }
01555 }
01556 }
01557
01558 return $aRet;
01559 }
01560
01572 protected function _addOrderArticlesToBasket( $oUser = null, $aOrderArticles = null, $blChangeDelivery = false, $blChangeDiscount = false, $blStockCheck = true )
01573 {
01574 $oBasket = oxNew( "oxbasket" );
01575
01576
01577 $oBasket->setStockCheckMode( $blStockCheck );
01578
01579
01580 $oBasket->setBasketUser( $oUser );
01581
01582
01583 $aCurrencies = $this->getConfig()->getCurrencyArray();
01584 foreach ( $aCurrencies as $oCur ) {
01585 if ($oCur->name == $this->oxorder__oxcurrency->value) {
01586 $oBasketCur = $oCur;
01587 break;
01588 }
01589 }
01590
01591 $oBasket->setBasketCurrency( $oBasketCur );
01592
01593
01594 if ( count($aOrderArticles ) < 1 ) {
01595 return $oBasket;
01596 }
01597
01598
01599 foreach ( $aOrderArticles as $oOrderArticle ) {
01600
01601 $aPersParam = null;
01602 if ( $oOrderArticle->oxorderarticles__oxpersparam->value ) {
01603 $aPersParam = unserialize( $oOrderArticle->oxorderarticles__oxpersparam->value );
01604 }
01605
01606 $aSel = $this->_makeSelListArray( $oOrderArticle->oxorderarticles__oxartid->value,
01607 $oOrderArticle->oxorderarticles__oxselvariant->value );
01608
01609 $oBasketItem = $oBasket->addToBasket( $oOrderArticle->oxorderarticles__oxartid->value,
01610 $oOrderArticle->oxorderarticles__oxamount->value, $aSel, $aPersParam );
01611 if ( $oBasketItem ) {
01612 $oBasketItem->setWrapping( $oOrderArticle->oxorderarticles__oxwrapid->value );
01613 }
01614 }
01615
01616
01617 $oBasket->setCardId( $this->oxorder__oxcardid->value );
01618 $oBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01619
01620
01621 $oBasket->setSkipVouchersChecking( true );
01622
01623
01624 $sQ = 'select oxid from oxvouchers where oxorderid = "'.$this->getId().'"';
01625 $aVouchers = oxDb::getDb( true )->getAll( $sQ );
01626 foreach ( $aVouchers AS $aVoucher ) {
01627 $oBasket->addVoucher($aVoucher['oxid']);
01628 }
01629
01630
01631 $oBasket->setShipping( $this->oxorder__oxdeltype->value );
01632
01633
01634 if ( $blChangeDelivery ) {
01635 $oBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01636 }
01637 if ( $blChangeDiscount ) {
01638 $oBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
01639 }
01640
01641 $oBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01642
01643
01644 $oBasket->calculateBasket( true );
01645
01646 return $oBasket;
01647 }
01648
01656 protected function _setDeprecatedValues()
01657 {
01658 if ( $this->oxorder__oxstorno->value != 1 ) {
01659 $oCur = $this->getConfig()->getActShopCurrencyObject();
01660 $oLang = oxLang::getInstance();
01661
01662 $this->totalnetsum = $this->oxorder__oxtotalnetsum->value;
01663 $this->totalbrutsum = $this->oxorder__oxtotalbrutsum->value;
01664 $this->totalorder = $this->oxorder__oxtotalordersum->value;
01665 $this->ftotalnetsum = $oLang->formatCurrency( $this->oxorder__oxtotalnetsum->value, $oCur );
01666 $this->ftotalbrutsum = $oLang->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $oCur );
01667 $this->fdelcost = $oLang->formatCurrency( $this->oxorder__oxdelcost->value, $oCur );
01668 $this->fpaycost = $oLang->formatCurrency( $this->oxorder__oxpaycost->value, $oCur );
01669 $this->fwrapcost = $oLang->formatCurrency( $this->oxorder__oxwrapcost->value, $oCur );
01670 $this->ftotalorder = $this->getTotalOrderSum();
01671 $this->totalvouchers = 0;
01672
01673 if ( $this->oxorder__oxvoucherdiscount->value ) {
01674 $this->totalvouchers = $oLang->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $oCur );
01675 }
01676
01677 if ( $this->oxorder__oxdiscount->value ) {
01678 $this->discount = $this->oxorder__oxdiscount->value;
01679 $this->fdiscount = $oLang->formatCurrency( $this->oxorder__oxdiscount->value, $oCur );
01680 }
01681 }
01682 }
01683
01689 public function getTotalOrderSum()
01690 {
01691 $oCur = $this->getConfig()->getActShopCurrencyObject();
01692 return number_format( $this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01693 }
01694
01700 public function getBillCountry()
01701 {
01702 if( !$this->oxorder__oxbillcountry->value ) {
01703 $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
01704 }
01705 return $this->oxorder__oxbillcountry;
01706 }
01707
01713 public function getDelCountry()
01714 {
01715 if( !$this->oxorder__oxdelcountry->value ) {
01716 $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
01717 }
01718 return $this->oxorder__oxdelcountry;
01719 }
01720
01721 }