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
00131 public function __construct()
00132 {
00133 parent::__construct();
00134 $this->init( 'oxorder' );
00135
00136
00137 $this->setSeparateNumbering( $this->getConfig()->getConfigParam( 'blSeparateNumbering') );
00138
00139 }
00140
00148 public function __get( $sName )
00149 {
00150 if ( $sName == 'oDelSet' ) {
00151 return $this->getDelSet();
00152 }
00153
00154 if ( $sName == 'oxorder__oxbillcountry' ) {
00155 return $this->getBillCountry();
00156 }
00157
00158 if ( $sName == 'oxorder__oxdelcountry' ) {
00159 return $this->getDelCountry();
00160 }
00161 }
00162
00170 public function assign( $dbRecord )
00171 {
00172
00173 parent::assign( $dbRecord );
00174
00175 $oUtilsDate = oxUtilsDate::getInstance();
00176
00177
00178 $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value));
00179 $this->oxorder__oxsenddate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value));
00180
00181
00182 $this->_setDeprecatedValues();
00183 }
00184
00192 protected function _getCountryTitle( $sCountryId )
00193 {
00194 $sTitle = null;
00195 if ( $sCountryId && $sCountryId != '-1' ) {
00196 $oCountry = oxNew( 'oxcountry' );
00197 $oCountry->load( $sCountryId );
00198 $sTitle = $oCountry->oxcountry__oxtitle->value;
00199 }
00200
00201 return $sTitle;
00202 }
00203
00211 public function getOrderArticles( $blExcludeCanceled = false )
00212 {
00213
00214 if ( $this->_oArticles === null ) {
00215 $blExcludeState = $blExcludeCanceled;
00216 $sTable = getViewName( "oxorderarticles" );
00217 $sSelect = "select {$sTable}.* from {$sTable}
00218 where {$sTable}.oxorderid = '".$this->getId() . "'" .
00219 ( $blExcludeCanceled ? " and {$sTable}.oxstorno != 1 ": " " ) ."
00220 order by {$sTable}.oxartid";
00221
00222
00223 $oArticles = oxNew( 'oxlist' );
00224 $oArticles->init( 'oxorderarticle' );
00225 $oArticles->selectString( $sSelect );
00226
00227
00228 return $oArticles;
00229 }
00230 return $this->_oArticles;
00231 }
00232
00240 public function setOrderArticleList( $aOrderArticleList )
00241 {
00242 $this->_oArticles = $aOrderArticleList;
00243 }
00244
00250 public function getOrderDeliveryPrice()
00251 {
00252 if ( $this->_oDelPrice != null ) {
00253 return $this->_oDelPrice;
00254 }
00255
00256 $this->_oDelPrice = oxNew( 'oxprice' );
00257 $this->_oDelPrice->setBruttoPriceMode();
00258 $this->_oDelPrice->setPrice( $this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value );
00259 return $this->_oDelPrice;
00260 }
00261
00267 public function getOrderWrappingPrice()
00268 {
00269 if ( $this->_oWrappingPrice != null ) {
00270 return $this->_oWrappingPrice;
00271 }
00272
00273 $this->_oWrappingPrice = oxNew( 'oxprice' );
00274 $this->_oWrappingPrice->setBruttoPriceMode();
00275 $this->_oWrappingPrice->setPrice( $this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value );
00276 return $this->_oWrappingPrice;
00277 }
00278
00284 public function getOrderPaymentPrice()
00285 {
00286 if ( $this->_oPaymentPrice != null ) {
00287 return $this->_oPaymentPrice;
00288 }
00289
00290 $this->_oPaymentPrice = oxNew( 'oxprice' );
00291 $this->_oPaymentPrice->setBruttoPriceMode();
00292 $this->_oPaymentPrice->setPrice( $this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value );
00293 return $this->_oPaymentPrice;
00294 }
00295
00302 public function getOrderNetSum()
00303 {
00304 $dTotalNetSum = 0;
00305
00306 $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
00307 $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
00308 $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
00309 $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
00310
00311 return $dTotalNetSum;
00312 }
00313
00334 public function finalizeOrder( oxBasket $oBasket, $oUser, $blRecalculatingOrder = false )
00335 {
00336
00337 $sGetChallenge = oxSession::getVar( 'sess_challenge' );
00338 if ( $this->_checkOrderExist( $sGetChallenge ) ) {
00339 oxUtils::getInstance()->logger( 'BLOCKER' );
00340
00341 return 3;
00342 }
00343
00344
00345 if ( !$blRecalculatingOrder ) {
00346
00347 $this->setId( $sGetChallenge );
00348 }
00349
00350
00351 $this->_setUser( $oUser );
00352
00353
00354 $this->_loadFromBasket( $oBasket );
00355
00356
00357 $oUserPayment = $this->_setPayment( $oBasket->getPaymentId() );
00358
00359
00360
00361 if ( !$blRecalculatingOrder ) {
00362 $this->_setFolder();
00363 }
00364
00365
00366 $this->save();
00367
00368
00369
00370 if ( !$blRecalculatingOrder ) {
00371 $blRet = $this->_executePayment( $oBasket, $oUserPayment );
00372 if ( $blRet !== true ) {
00373 return $blRet;
00374 }
00375 }
00376
00377
00378 oxSession::deleteVar( 'ordrem' );
00379
00380
00381 $this->_setOrderStatus( 'OK' );
00382
00383
00384 $oBasket->setOrderId( $this->getId() );
00385
00386
00387 $this->_updateStock();
00388
00389
00390 $this->_updateWishlist( $oBasket->getContents(), $oUser );
00391
00392
00393 $this->_updateNoticeList( $oBasket->getContents(), $oUser );
00394
00395
00396
00397 if ( !$blRecalculatingOrder ) {
00398 $this->_markVouchers( $oBasket, $oUser );
00399 }
00400
00401
00402
00403 if ( !$blRecalculatingOrder ) {
00404 $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
00405 } else {
00406 $iRet = 1;
00407 }
00408
00409 return $iRet;
00410 }
00411
00419 protected function _setOrderStatus( $sStatus )
00420 {
00421 $sQ = 'update oxorder set oxtransstatus="'.$sStatus.'" where oxid="'.$this->getId().'" ';
00422 oxDb::getDb()->execute( $sQ );
00423 }
00424
00435 protected function _loadFromBasket( oxBasket $oBasket )
00436 {
00437 $myConfig = $this->getConfig();
00438
00439
00440 if ( $myConfig->getConfigParam( 'blStoreIPs' ) && $this->oxorder__oxip->value === null ) {
00441 $this->oxorder__oxip = new oxField(oxUtilsServer::getInstance()->getRemoteAddress(), oxField::T_RAW);
00442 }
00443
00444
00445 $this->oxorder__oxtotalnetsum = new oxField(oxUtils::getInstance()->fRound($oBasket->getProductsPrice()->getNettoSum()), oxField::T_RAW);
00446 $this->oxorder__oxtotalbrutsum = new oxField($oBasket->getProductsPrice()->getBruttoSum(), oxField::T_RAW);
00447 $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00448
00449
00450 if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00451 $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00452 $this->oxorder__oxpayvat = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00453 }
00454
00455
00456 if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00457 $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00458
00459 $this->oxorder__oxdelvat = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW);
00460 $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00461 }
00462
00463
00464 if ( $this->oxorder__oxremark->value === null ) {
00465 $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00466 }
00467
00468
00469 $oCur = $myConfig->getActShopCurrencyObject();
00470 $this->oxorder__oxcurrency = new oxField($oCur->name);
00471 $this->oxorder__oxcurrate = new oxField($oCur->rate, oxField::T_RAW);
00472
00473
00474 if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00475 $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00476 }
00477
00478
00479 $dDiscount = 0;
00480 $aDiscounts = $oBasket->getDiscounts();
00481 if ( count($aDiscounts) > 0 ) {
00482 foreach ($aDiscounts as $oDiscount) {
00483 $dDiscount += $oDiscount->dDiscount;
00484 }
00485 }
00486 if ( $dDiscount ) {
00487 $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00488 }
00489
00490
00491 $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00492
00493
00494
00495 $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00496
00497
00498 $this->_setOrderArticles( $oBasket->getContents() );
00499
00500
00501 $this->_setWrapping( $oBasket );
00502 }
00503
00510 public function getOrderLanguage()
00511 {
00512 if ( $this->_iOrderLang === null ) {
00513 if ( isset( $this->oxorder__oxlang->value ) ) {
00514 $this->_iOrderLang = oxLang::getInstance()->validateLanguage( $this->oxorder__oxlang->value );
00515 } else {
00516 $this->_iOrderLang = oxLang::getInstance()->getBaseLanguage();
00517 }
00518 }
00519 return $this->_iOrderLang;
00520 }
00521
00529 protected function _setUser( $oUser )
00530 {
00531
00532 $this->oxorder__oxuserid = new oxField($oUser->getId());
00533
00534
00535 $this->oxorder__oxbillcompany = clone $oUser->oxuser__oxcompany;
00536 $this->oxorder__oxbillemail = clone $oUser->oxuser__oxusername;
00537 $this->oxorder__oxbillfname = clone $oUser->oxuser__oxfname;
00538 $this->oxorder__oxbilllname = clone $oUser->oxuser__oxlname;
00539 $this->oxorder__oxbillstreet = clone $oUser->oxuser__oxstreet;
00540 $this->oxorder__oxbillstreetnr = clone $oUser->oxuser__oxstreetnr;
00541 $this->oxorder__oxbilladdinfo = clone $oUser->oxuser__oxaddinfo;
00542 $this->oxorder__oxbillustid = clone $oUser->oxuser__oxustid;
00543 $this->oxorder__oxbillcity = clone $oUser->oxuser__oxcity;
00544 $this->oxorder__oxbillcountryid = clone $oUser->oxuser__oxcountryid;
00545 $this->oxorder__oxbillzip = clone $oUser->oxuser__oxzip;
00546 $this->oxorder__oxbillfon = clone $oUser->oxuser__oxfon;
00547 $this->oxorder__oxbillfax = clone $oUser->oxuser__oxfax;
00548 $this->oxorder__oxbillsal = clone $oUser->oxuser__oxsal;
00549
00550
00551
00552 if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00553
00554 $this->oxorder__oxdelcompany = clone $oDelAdress->oxaddress__oxcompany;
00555 $this->oxorder__oxdelfname = clone $oDelAdress->oxaddress__oxfname;
00556 $this->oxorder__oxdellname = clone $oDelAdress->oxaddress__oxlname;
00557 $this->oxorder__oxdelstreet = clone $oDelAdress->oxaddress__oxstreet;
00558 $this->oxorder__oxdelstreetnr = clone $oDelAdress->oxaddress__oxstreetnr;
00559 $this->oxorder__oxdeladdinfo = clone $oDelAdress->oxaddress__oxaddinfo;
00560 $this->oxorder__oxdelcity = clone $oDelAdress->oxaddress__oxcity;
00561 $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00562 $this->oxorder__oxdelzip = clone $oDelAdress->oxaddress__oxzip;
00563 $this->oxorder__oxdelfon = clone $oDelAdress->oxaddress__oxfon;
00564 $this->oxorder__oxdelfax = clone $oDelAdress->oxaddress__oxfax;
00565 $this->oxorder__oxdelsal = clone $oDelAdress->oxaddress__oxsal;
00566 }
00567 }
00568
00576 protected function _setWrapping( oxBasket $oBasket )
00577 {
00578 $myConfig = $this->getConfig();
00579
00580
00581 if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00582 $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00583
00584
00585 if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
00586 $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00587 }
00588 }
00589
00590
00591 $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00592
00593
00594 $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00595 }
00596
00605 protected function _setOrderArticles( $aArticleList )
00606 {
00607
00608 $this->_oArticles = oxNew( 'oxlist' );
00609 $iCurrLang = $this->getOrderLanguage();
00610
00611
00612 foreach ( $aArticleList as $oContent ) {
00613
00614
00615 $oProduct = $oContent->getArticle();
00616
00617
00618 if ( $oProduct->isOrderArticle() ) {
00619 $oOrderArticle = $oProduct;
00620 } else {
00621
00622
00623 if ( $iCurrLang != $oProduct->getLanguage() ) {
00624 $oProduct->loadInLang( $iCurrLang, $oProduct->getProductId() );
00625 }
00626
00627
00628 $sSelList = '';
00629 if ( count( $aChosenSelList = $oContent->getChosenSelList() ) ) {
00630 foreach ( $aChosenSelList as $oItem ) {
00631 if ( $sSelList ) {
00632 $sSelList .= ", ";
00633 }
00634 $sSelList .= "{$oItem->name} : {$oItem->value}";
00635 }
00636 }
00637
00638 $oOrderArticle = oxNew( 'oxorderarticle' );
00639 $oOrderArticle->setIsNewOrderItem( true );
00640 $oOrderArticle->copyThis( $oProduct );
00641 $oOrderArticle->setId();
00642
00643 $oOrderArticle->oxorderarticles__oxartnum = clone $oProduct->oxarticles__oxartnum;
00644 $oOrderArticle->oxorderarticles__oxselvariant = new oxField( trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->value ), oxField::T_RAW );
00645 $oOrderArticle->oxorderarticles__oxshortdesc = new oxField( $oProduct->oxarticles__oxshortdesc->value, oxField::T_RAW );
00646 $oOrderArticle->oxorderarticles__oxtitle = new oxField( trim( $oProduct->oxarticles__oxtitle->value.' '.$oOrderArticle->oxorderarticles__oxselvariant->value ), oxField::T_RAW );
00647
00648
00649 if ( !is_array( $aPersParams = $oProduct->getPersParams() ) ) {
00650 $aPersParams = $oContent->getPersParams();
00651 }
00652 if ( is_array( $aPersParams ) && count( $aPersParams )) {
00653 $oOrderArticle->oxorderarticles__oxpersparam = new oxField( serialize( $aPersParams ), oxField::T_RAW );
00654 }
00655 }
00656
00657
00658 $oOrderArticle->oxorderarticles__oxorderid = new oxField( $this->getId() );
00659 $oOrderArticle->oxorderarticles__oxartid = new oxField( $oContent->getProductId() );
00660 $oOrderArticle->oxorderarticles__oxamount = new oxField( $oContent->getAmount() );
00661
00662
00663 $oPrice = $oContent->getPrice();
00664 $oOrderArticle->oxorderarticles__oxnetprice = new oxField( $oPrice->getNettoPrice(), oxField::T_RAW );
00665 $oOrderArticle->oxorderarticles__oxvatprice = new oxField( $oPrice->getVatValue(), oxField::T_RAW );
00666 $oOrderArticle->oxorderarticles__oxbrutprice = new oxField( $oPrice->getBruttoPrice(), oxField::T_RAW );
00667 $oOrderArticle->oxorderarticles__oxvat = new oxField( $oPrice->getVat(), oxField::T_RAW );
00668
00669 $oUnitPtice = $oContent->getUnitPrice();
00670 $oOrderArticle->oxorderarticles__oxnprice = new oxField( $oUnitPtice->getNettoPrice(), oxField::T_RAW );
00671 $oOrderArticle->oxorderarticles__oxbprice = new oxField( $oUnitPtice->getBruttoPrice(), oxField::T_RAW );
00672
00673
00674 $oOrderArticle->oxorderarticles__oxwrapid = new oxField( $oContent->getWrappingId(), oxField::T_RAW );
00675
00676
00677 $oOrderArticle->oxorderarticles__oxordershopid = new oxField( $oContent->getShopId(), oxField::T_RAW );
00678
00679
00680
00681
00682 $oOrderArticle->oProduct = $oProduct;
00683
00684
00685 $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
00686 }
00687 }
00688
00700 protected function _executePayment( oxBasket $oBasket, $oUserpayment )
00701 {
00702 $oPayTransaction = $this->_getGateway();
00703 $oPayTransaction->setPaymentParams( $oUserpayment );
00704
00705 if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
00706 $this->delete();
00707
00708
00709 if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
00710 if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
00711 return $sLastError;
00712 }
00713 }
00714
00715
00716 if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
00717 if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
00718 return $iLastErrorNo;
00719 }
00720 }
00721
00722 return 2;
00723 }
00724 return true;
00725 }
00726
00733 protected function _getGateway()
00734 {
00735 return oxNew( 'oxPaymentGateway' );
00736 }
00737
00745 protected function _setPayment( $sPaymentid )
00746 {
00747
00748 $aDynvalue = oxSession::getVar( 'dynvalue' );
00749 $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
00750
00751
00752 $oPayment = oxNew( 'oxpayment' );
00753
00754 if (!$oPayment->load( $sPaymentid )) {
00755 return null;
00756 }
00757
00758
00759 if ( !$aDynvalue && ( $oUserpayment = $this->getPaymentType() ) ) {
00760 $aStoredDynvalue = $oUserpayment->getDynValues();
00761 foreach ( $aStoredDynvalue as $oVal ) {
00762 $aDynvalue[$oVal->name] = $oVal->value;
00763 }
00764 }
00765
00766 $oPayment->setDynValues( oxUtils::getInstance()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
00767
00768
00769 $aDynVal = array();
00770
00771 $aPaymentDynValues = $oPayment->getDynValues();
00772 foreach ( $aPaymentDynValues as $key => $oVal ) {
00773 if ( isset( $aDynvalue[$oVal->name] ) ) {
00774 $oVal->value = $aDynvalue[$oVal->name];
00775 }
00776
00777
00778 $aPaymentDynValues[$key] = $oVal;
00779 $aDynVal[$oVal->name] = $oVal->value;
00780 }
00781
00782
00783
00784
00785 $oUserpayment = oxNew( 'oxuserpayment' );
00786 $oUserpayment->oxuserpayments__oxuserid = clone $this->oxorder__oxuserid;
00787 $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
00788 $oUserpayment->oxuserpayments__oxvalue = new oxField(oxUtils::getInstance()->assignValuesToText( $aDynVal ), oxField::T_RAW);
00789 $oUserpayment->oxpayments__oxdesc = clone $oPayment->oxpayments__oxdesc;
00790 $oUserpayment->setDynValues( $aPaymentDynValues );
00791 $oUserpayment->save();
00792
00793
00794 $this->oxorder__oxpaymentid = new oxField($oUserpayment->getId(), oxField::T_RAW);
00795 $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
00796
00797
00798 return $oUserpayment;
00799 }
00800
00806 protected function _setFolder()
00807 {
00808 $myConfig = $this->getConfig();
00809 $this->oxorder__oxfolder = new oxField(key( $myConfig->getShopConfVar( 'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
00810 }
00811
00821 protected function _updateWishlist( $aArticleList, $oUser )
00822 {
00823
00824 foreach ( $aArticleList as $oContent) {
00825 if ( ( $sWishId = $oContent->getWishId() ) ) {
00826
00827
00828 if ( $sWishId == $oUser->getId() ) {
00829 $oUserBasket = $oUser->getBasket( 'wishlist' );
00830 } else {
00831 $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
00832 $oUserBasket = oxNew( 'oxuserbasket' );
00833 $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
00834 }
00835
00836
00837 if ( $oUserBasket ) {
00838 if ( !($sProdId = $oContent->getWishArticleId() )) {
00839 $sProdId = $oContent->getProductId();
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 }
00851
00861 protected function _updateNoticeList( $aArticleList, $oUser )
00862 {
00863
00864 if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
00865
00866 foreach ( $aArticleList as $oContent) {
00867 $sProdId = $oContent->getProductId();
00868
00869
00870 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00871 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00872 if ( $dNewAmount < 0) {
00873 $dNewAmount = 0;
00874 }
00875 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00876 }
00877 }
00878 }
00879
00885 protected function _updateStock()
00886 {
00887 $myConfig = $this->getConfig();
00888
00889
00890 if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00891 $oOrderArticles = $this->getOrderArticles();
00892 foreach ( $oOrderArticles as $oOrderArticle ) {
00893 if ( $oOrderArticle->isNewOrderItem() ) {
00894 $oOrderArticle->updateArticleStock( $oOrderArticle->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
00895 }
00896 }
00897 }
00898 }
00899
00911 protected function _markVouchers( $oBasket, $oUser )
00912 {
00913 $this->_aVoucherList = $oBasket->getVouchers();
00914
00915 if ( is_array( $this->_aVoucherList ) ) {
00916 foreach ( array_keys( $this->_aVoucherList ) as $sVoucherId ) {
00917 $oVoucher = oxNew( 'oxvoucher' );
00918 $oVoucher->load( $sVoucherId );
00919 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value );
00920
00921
00922 $oVoucher->oxmodvouchers__oxvouchernr = $oVoucher->oxvouchers__oxvouchernr;
00923 $oVSerie = $oVoucher->getSerie();
00924 $oVoucher->oxmodvouchers__oxdiscount = clone $oVSerie->oxvoucherseries__oxdiscount;
00925 $oVoucher->oxmodvouchers__oxdiscounttype = clone $oVSerie->oxvoucherseries__oxdiscounttype;
00926
00927
00928 $this->_aVoucherList[$sVoucherId] = $oVoucher;
00929 }
00930 }
00931 }
00932
00938 public function save()
00939 {
00940 if ( ( $blSave = parent::save() ) ) {
00941
00942
00943 $oOrderArticles = $this->getOrderArticles();
00944 if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
00945 foreach ( $oOrderArticles as $oOrderArticle ) {
00946 $oOrderArticle->save();
00947 }
00948 }
00949 }
00950
00951 return $blSave;
00952 }
00953
00960 public function getDelAddressInfo()
00961 {
00962 $oDelAdress = null;
00963 if ( ( $soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
00964 $oDelAdress = oxNew( 'oxbase' );
00965 $oDelAdress->init( 'oxaddress' );
00966 $oDelAdress->load( $soxAddressId );
00967
00968
00969 if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
00970 $oCountry = oxNew( 'oxcountry' );
00971 $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
00972 $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
00973 }
00974 }
00975 return $oDelAdress;
00976 }
00977
00988 public function validateStock( $oBasket )
00989 {
00990 foreach ( $oBasket->getContents() as $oContent ) {
00991 $oProd = $oContent->getArticle();
00992
00993
00994 $iOnStock = $oProd->checkForStock( $oContent->getAmount() );
00995 if ( $iOnStock !== true ) {
00996 $oEx = oxNew( 'oxOutOfStockException' );
00997 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
00998 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
00999 $oEx->setRemainingAmount( $oProd->oxarticles__oxstock->value );
01000 throw $oEx;
01001 }
01002 }
01003 }
01004
01010 protected function _insert()
01011 {
01012 $myConfig = $this->getConfig();
01013 $oUtilsDate = oxUtilsDate::getInstance();
01014
01015
01016 if ( !$this->oxorder__oxorderdate->value ) {
01017 $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
01018 } else {
01019 $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
01020 }
01021 $this->oxorder__oxshopid = new oxField($myConfig->getShopId(), oxField::T_RAW);
01022
01023 $this->oxorder__oxsenddate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01024
01025 if ( ( $blInsert = parent::_insert() ) ) {
01026
01027 if ( !$this->oxorder__oxordernr->value ) {
01028 $aWhere = '';
01029
01030 if ( $this->_blSeparateNumbering ) {
01031 $aWhere = array( 'oxshopid = "'.$myConfig->getShopId().'"' );
01032 }
01033 $this->_setRecordNumber( 'oxordernr', $aWhere );
01034 }
01035 }
01036 return $blInsert;
01037 }
01038
01044 protected function _update()
01045 {
01046 $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01047 return parent::_update();
01048 }
01049
01058 public function delete( $sOxId = null )
01059 {
01060 if ( $sOxId ) {
01061 if ( !$this->load( $sOxId ) ) {
01062
01063 return false;
01064 }
01065 } elseif ( !$sOxId ) {
01066 $sOxId = $this->getId();
01067 }
01068
01069
01070 if ( !$sOxId ) {
01071 return false;
01072 }
01073
01074
01075
01076 $myConfig = $this->getConfig();
01077 $blUseStock = $myConfig->getConfigParam( 'blUseStock' );
01078 $oOrderArticles = $this->getOrderArticles( $blUseStock );
01079 foreach ( $oOrderArticles as $oOrderArticle ) {
01080 if ( $blUseStock ) {
01081 $oOrderArticle->updateArticleStock( $oOrderArticle->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
01082 }
01083 $oOrderArticle->delete();
01084 }
01085
01086
01087 if ( $oPaymentType = $this->getPaymentType() ) {
01088 $oPaymentType->delete();
01089 }
01090
01091 return parent::delete( $sOxId );
01092 }
01093
01103 public function recalculateOrder( $aNewArticles = array() )
01104 {
01105 oxDb::startTransaction();
01106
01107 try {
01108 $oBasket = $this->_getOrderBasket();
01109
01110
01111 $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles( true ) );
01112
01113
01114 $this->_addArticlesToBasket( $oBasket, $aNewArticles );
01115
01116
01117 $oBasket->calculateBasket( true );
01118
01119
01120 $iRet = $this->finalizeOrder( $oBasket, $this->getOrderUser(), true );
01121
01122
01123 if ( $iRet !== 1 ) {
01124 oxDb::rollbackTransaction();
01125 } else {
01126 oxDb::commitTransaction();
01127 }
01128
01129 } catch( Exception $oE ) {
01130
01131 oxDb::rollbackTransaction();
01132 }
01133 }
01134
01135 protected $_oOrderBasket = null;
01143 protected function _getOrderBasket( $blStockCheck = true )
01144 {
01145 $this->_oOrderBasket = oxNew( "oxbasket" );
01146
01147
01148 $this->_oOrderBasket->setStockCheckMode( $blStockCheck );
01149
01150
01151 $this->_oOrderBasket->setBasketUser( $this->getOrderUser() );
01152
01153
01154 $this->_oOrderBasket->setOrderId( $this->getId() );
01155
01156
01157 $aCurrencies = $this->getConfig()->getCurrencyArray();
01158 foreach ( $aCurrencies as $oCur ) {
01159 if ($oCur->name == $this->oxorder__oxcurrency->value) {
01160 $oBasketCur = $oCur;
01161 break;
01162 }
01163 }
01164
01165
01166 $this->_oOrderBasket->setBasketCurrency( $oBasketCur );
01167
01168
01169 $this->_oOrderBasket->setCardId( $this->oxorder__oxcardid->value );
01170 $this->_oOrderBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01171
01172 if ( $this->_blReloadDiscount ) {
01173
01174 $this->_oOrderBasket->setSkipVouchersChecking( true );
01175
01176
01177 $sQ = 'select oxid from oxvouchers where oxorderid = "'.$this->getId().'"';
01178 $aVouchers = oxDb::getDb( true )->getAll( $sQ );
01179 foreach ( $aVouchers as $aVoucher ) {
01180 $this->_oOrderBasket->addVoucher( $aVoucher['oxid'] );
01181 }
01182 } else {
01183 $this->_oOrderBasket->setDiscountCalcMode( false );
01184 $this->_oOrderBasket->setVoucherDiscount( $this->oxorder__oxvoucherdiscount->value );
01185 $this->_oOrderBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
01186 }
01187
01188
01189 if ( !$this->_blReloadDelivery ) {
01190 $this->_oOrderBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01191 } else {
01192
01193 $this->_oOrderBasket->setShipping( $this->oxorder__oxdeltype->value );
01194 $this->_oOrderBasket->setDeliveryPrice( null );
01195 }
01196
01197
01198 $this->_oOrderBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01199
01200 return $this->_oOrderBasket;
01201 }
01202
01211 public function setDelivery( $sDeliveryId )
01212 {
01213 $this->reloadDelivery( true );
01214 $this->oxorder__oxdeltype = new oxField( $sDeliveryId );
01215 }
01216
01222 public function getOrderUser()
01223 {
01224 if ($this->_oUser) {
01225 return $this->_oUser;
01226 }
01227 $this->_oUser = oxNew( "oxuser" );
01228 $this->_oUser->load( $this->oxorder__oxuserid->value );
01229
01230 return $this->_oUser;
01231 }
01232
01240 public function pdfFooter( $oPdf )
01241 {
01242 }
01243
01251 public function pdfHeaderplus( $oPdf )
01252 {
01253 }
01254
01262 public function pdfHeader( $oPdf )
01263 {
01264 }
01265
01274 public function genPdf( $sFilename, $iSelLang = 0 )
01275 {
01276 }
01277
01283 public function getInvoiceNum()
01284 {
01285 $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01286 return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01287 }
01288
01294 public function getNextBillNum()
01295 {
01296 $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01297 return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01298 }
01299
01305 public function getShippingSetList()
01306 {
01307
01308 if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
01309 $sShipId = $this->oxorder__oxbillcountryid->value;
01310 }
01311
01312 $oBasket = $this->_getOrderBasket( false );
01313
01314
01315 $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles() );
01316
01317
01318 $oBasket->calculateBasket( true );
01319
01320
01321 $oDeliveryList = oxNew( "oxDeliveryList", "core" );
01322 $oDeliveryList->setCollectFittingDeliveriesSets( true );
01323
01324 return $oDeliveryList->getDeliveryList( $oBasket, $this->getOrderUser(), $sShipId );
01325 }
01326
01332 public function getVoucherNrList()
01333 {
01334 $oDB = oxDb::getDb( true );
01335 $aVouchers = array();
01336 $sSelect = "select oxvouchernr from oxvouchers where oxorderid = '".$this->oxorder__oxid->value."'";
01337 $rs = $oDB->execute( $sSelect);
01338 if ($rs != false && $rs->recordCount() > 0) {
01339 while (!$rs->EOF) {
01340 $aVouchers[] = $rs->fields['oxvouchernr'];
01341 $rs->moveNext();
01342 }
01343 }
01344 return $aVouchers;
01345 }
01346
01354 public function getOrderSum( $blToday = false )
01355 {
01356 $sSelect = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01357 $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01358
01359 if ( $blToday ) {
01360 $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01361 }
01362
01363 return ( double ) oxDb::getDb()->getOne( $sSelect );
01364 }
01365
01373 public function getOrderCnt( $blToday = false )
01374 {
01375 $sSelect = 'select count(*) from oxorder where ';
01376 $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01377
01378 if ( $blToday ) {
01379 $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01380 }
01381
01382 return ( int ) oxDb::getDb()->getOne( $sSelect );
01383 }
01384
01385
01393 protected function _checkOrderExist( $sOxId = null )
01394 {
01395 if ( !$sOxId) {
01396 return false;
01397 }
01398
01399 if ( oxDb::getDb()->getOne( 'select oxid from oxorder where oxid = "'.$sOxId.'"' ) ) {
01400 return true;
01401 }
01402
01403 return false;
01404 }
01405
01415 protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01416 {
01417 $iRet = 0;
01418
01419
01420 $this->_oUser = $oUser;
01421 $this->_oBasket = $oBasket;
01422 $this->_oPayment = $oPayment;
01423
01424 $oxEmail = oxNew( 'oxemail' );
01425
01426
01427 if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01428
01429 $iRet = 1;
01430 }
01431
01432
01433 $oxEmail->sendOrderEMailToOwner( $this );
01434
01435 return $iRet;
01436 }
01437
01443 public function getBasket()
01444 {
01445 return $this->_oBasket;
01446 }
01447
01453 public function getPayment()
01454 {
01455 return $this->_oPayment;
01456 }
01457
01463 public function getVoucherList()
01464 {
01465 return $this->_aVoucherList;
01466 }
01467
01473 public function getDelSet()
01474 {
01475 if ( $this->_oDelSet == null ) {
01476
01477 $this->_oDelSet = oxNew( 'oxdeliveryset' );
01478 $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01479 }
01480
01481 return $this->_oDelSet;
01482 }
01483
01489 public function getPaymentType()
01490 {
01491 if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType === null ) {
01492 $this->_oPaymentType = false;
01493 $oPaymentType = oxNew( 'oxuserpayment' );
01494 if ( $oPaymentType->load( $this->oxorder__oxpaymentid->value ) ) {
01495 $this->_oPaymentType = $oPaymentType;
01496 }
01497 }
01498
01499 return $this->_oPaymentType;
01500 }
01501
01507 public function getGiftCard()
01508 {
01509 if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01510 $this->_oGiftCard = oxNew( 'oxwrapping' );
01511 $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01512 }
01513
01514 return $this->_oGiftCard;
01515 }
01516
01524 public function setSeparateNumbering( $blSeparateNumbering = null )
01525 {
01526 $this->_blSeparateNumbering = $blSeparateNumbering;
01527 }
01528
01536 public function getLastUserPaymentType( $sUserId)
01537 {
01538 $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid="'.$sUserId.'" order by oxorder.oxorderdate desc ';
01539 $sLastPaymentId = oxDb::getDb()->getOne( $sQ );
01540 return $sLastPaymentId;
01541 }
01542
01555 protected function _makeSelListArray( $sArtId = null, $sOrderArtSelList = null )
01556 {
01557 $oOrder = oxNew( 'oxorderArticle' );
01558 return $oOrder->getOrderArticleSelectList( $sArtId, $sOrderArtSelList );
01559 }
01560
01569 protected function _addOrderArticlesToBasket( $oBasket, $aOrderArticles )
01570 {
01571
01572 if ( count( $aOrderArticles ) > 0 ) {
01573
01574
01575 foreach ( $aOrderArticles as $oOrderArticle ) {
01576 $oBasket->addOrderArticleToBasket( $oOrderArticle );
01577 }
01578 }
01579 }
01580
01587 protected function _addArticlesToBasket( $oBasket, $aArticles )
01588 {
01589
01590 if ( count($aArticles ) > 0 ) {
01591
01592
01593 foreach ( $aArticles as $oArticle ) {
01594 $aSel = isset( $oArticle->oxorderarticles__oxselvariant ) ? $oArticle->oxorderarticles__oxselvariant->value : null;
01595 $aPersParam = isset( $oArticle->oxorderarticles__oxpersparam ) ? $oArticle->getPersParams() : null;
01596 $oBasket->addToBasket( $oArticle->oxorderarticles__oxartid->value,
01597 $oArticle->oxorderarticles__oxamount->value,
01598 $aSel, $aPersParam );
01599 }
01600 }
01601 }
01602
01610 protected function _setDeprecatedValues()
01611 {
01612 if ( $this->oxorder__oxstorno->value != 1 ) {
01613 $oCur = $this->getConfig()->getActShopCurrencyObject();
01614 $oLang = oxLang::getInstance();
01615
01616 $this->totalnetsum = $this->oxorder__oxtotalnetsum->value;
01617 $this->totalbrutsum = $this->oxorder__oxtotalbrutsum->value;
01618 $this->totalorder = $this->oxorder__oxtotalordersum->value;
01619 $this->ftotalnetsum = $oLang->formatCurrency( $this->oxorder__oxtotalnetsum->value, $oCur );
01620 $this->ftotalbrutsum = $oLang->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $oCur );
01621 $this->fdelcost = $oLang->formatCurrency( $this->oxorder__oxdelcost->value, $oCur );
01622 $this->fpaycost = $oLang->formatCurrency( $this->oxorder__oxpaycost->value, $oCur );
01623 $this->fwrapcost = $oLang->formatCurrency( $this->oxorder__oxwrapcost->value, $oCur );
01624 $this->ftotalorder = $this->getTotalOrderSum();
01625 $this->totalvouchers = 0;
01626
01627 if ( $this->oxorder__oxvoucherdiscount->value ) {
01628 $this->totalvouchers = $oLang->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $oCur );
01629 }
01630
01631 if ( $this->oxorder__oxdiscount->value ) {
01632 $this->discount = $this->oxorder__oxdiscount->value;
01633 $this->fdiscount = $oLang->formatCurrency( $this->oxorder__oxdiscount->value, $oCur );
01634 }
01635 }
01636 }
01637
01643 public function getTotalOrderSum()
01644 {
01645 $oCur = $this->getConfig()->getActShopCurrencyObject();
01646 return number_format( $this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01647 }
01648
01654 public function getBillCountry()
01655 {
01656 if ( !$this->oxorder__oxbillcountry->value ) {
01657 $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
01658 }
01659 return $this->oxorder__oxbillcountry;
01660 }
01661
01667 public function getDelCountry()
01668 {
01669 if ( !$this->oxorder__oxdelcountry->value ) {
01670 $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
01671 }
01672 return $this->oxorder__oxdelcountry;
01673 }
01674
01682 public function reloadDelivery( $blReload )
01683 {
01684 $this->_blReloadDelivery = $blReload;
01685 }
01686
01694 public function reloadDiscount( $blReload )
01695 {
01696 $this->_blReloadDiscount = $blReload;
01697 }
01698
01699 }