oxorder.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxOrder extends oxBase
00009 {
00010     // defining order state constants
00015     const ORDER_STATE_MAILINGERROR = 0;
00016 
00021     const ORDER_STATE_OK = 1;
00022 
00027     const ORDER_STATE_PAYMENTERROR = 2;
00028 
00033     const ORDER_STATE_ORDEREXISTS = 3;
00034 
00039     const ORDER_STATE_INVALIDDELIVERY = 4;
00040 
00045     const ORDER_STATE_INVALIDPAYMENT = 5;
00046 
00051     const ORDER_STATE_INVALIDTSPROTECTION = 6;
00052 
00057     const ORDER_STATE_INVALIDDElADDRESSCHANGED = 7;
00058 
00063     const ORDER_STATE_BELOWMINPRICE = 8;
00064 
00070     protected $_aSkipSaveFields = array( 'oxtimestamp' );
00071 
00077     protected $_oArticles = null;
00078 
00084     protected $_oDelSet   = null;
00085 
00091     protected $_oGiftCard = null;
00092 
00098     protected $_oPaymentType = null;
00099 
00105     protected $_oPayment = null;
00106 
00112     protected $_aVoucherList = null;
00113 
00119     protected $_oDelPrice = null;
00120 
00126     protected $_oUser = null;
00127 
00133     protected $_oBasket = null;
00134 
00140     protected $_oWrappingPrice = null;
00141 
00147     protected $_oGiftCardPrice = null;
00148 
00154     protected $_oPaymentPrice = null;
00155 
00161     protected $_oTsProtectionPrice = null;
00162 
00168     protected $_sClassName = 'oxorder';
00169 
00175     protected $_blSeparateNumbering = null;
00176 
00182     protected $_iOrderLang = null;
00183 
00189     protected $_blReloadDelivery = true;
00190 
00196     protected $_blReloadDiscount = true;
00197 
00203     protected $_oOrderCurrency = null;
00204 
00210     protected $_oOrderFiles = null;
00211 
00215     public function __construct()
00216     {
00217         parent::__construct();
00218         $this->init( 'oxorder' );
00219 
00220         // set usage of separate orders numbering for different shops
00221         $this->setSeparateNumbering( $this->getConfig()->getConfigParam( 'blSeparateNumbering') );
00222 
00223     }
00224 
00232     public function __get( $sName )
00233     {
00234         if ( $sName == 'oDelSet' ) {
00235             return $this->getDelSet();
00236         }
00237 
00238         if ( $sName == 'oxorder__oxbillcountry' ) {
00239             return $this->getBillCountry();
00240         }
00241 
00242         if ( $sName == 'oxorder__oxdelcountry' ) {
00243             return $this->getDelCountry();
00244         }
00245     }
00246 
00254     public function assign( $dbRecord )
00255     {
00256 
00257         parent::assign( $dbRecord );
00258 
00259         $oUtilsDate = oxRegistry::get("oxUtilsDate");
00260 
00261         // convert date's to international format
00262         $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value));
00263         $this->oxorder__oxsenddate  = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value));
00264     }
00265 
00273     protected function _getCountryTitle( $sCountryId )
00274     {
00275         $sTitle = null;
00276         if ( $sCountryId && $sCountryId != '-1' ) {
00277             $oCountry = oxNew( 'oxcountry' );
00278             $oCountry->loadInLang( $this->getOrderLanguage(), $sCountryId );
00279             $sTitle = $oCountry->oxcountry__oxtitle->value;
00280         }
00281 
00282         return $sTitle;
00283     }
00284 
00292     protected function _getArticles( $blExcludeCanceled = false )
00293     {
00294         $sSelect = "SELECT `oxorderarticles`.* FROM `oxorderarticles`
00295                         WHERE `oxorderarticles`.`oxorderid` = '".$this->getId() . "'" .
00296                         ( $blExcludeCanceled ? " AND `oxorderarticles`.`oxstorno` != 1 ": " " ) ."
00297                         ORDER BY `oxorderarticles`.`oxartid`, `oxorderarticles`.`oxselvariant`, `oxorderarticles`.`oxpersparam` ";
00298 
00299             // order articles
00300         $oArticles = oxNew( 'oxlist' );
00301         $oArticles->init( 'oxorderarticle' );
00302         $oArticles->selectString( $sSelect );
00303 
00304         return $oArticles;
00305     }
00306 
00314     public function getOrderArticles( $blExcludeCanceled = false )
00315     {
00316         // checking set value
00317         if ( $blExcludeCanceled ) {
00318 
00319             return $this->_getArticles( true );
00320 
00321         } elseif ( $this->_oArticles === null  ) {
00322                 $this->_oArticles = $this->_getArticles();
00323         }
00324 
00325         return $this->_oArticles;
00326     }
00327 
00335     public function setOrderArticleList( $oOrderArticleList )
00336     {
00337         $this->_oArticles = $oOrderArticleList;
00338     }
00339 
00345     public function getOrderDeliveryPrice()
00346     {
00347         if ( $this->_oDelPrice != null ) {
00348             return $this->_oDelPrice;
00349         }
00350 
00351         $this->_oDelPrice = oxNew( 'oxprice' );
00352         $this->_oDelPrice->setBruttoPriceMode();
00353         $this->_oDelPrice->setPrice( $this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value );
00354         return $this->_oDelPrice;
00355     }
00356 
00362     public function getOrderWrappingPrice()
00363     {
00364         if ( $this->_oWrappingPrice != null ) {
00365             return $this->_oWrappingPrice;
00366         }
00367 
00368         $this->_oWrappingPrice = oxNew( 'oxprice' );
00369         $this->_oWrappingPrice->setBruttoPriceMode();
00370         $this->_oWrappingPrice->setPrice( $this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value );
00371         return $this->_oWrappingPrice;
00372     }
00373 
00379     public function getOrderGiftCardPrice()
00380     {
00381         if ( $this->_oGidtCardPrice != null ) {
00382             return $this->_oGidtCardPrice;
00383         }
00384 
00385         $this->_oGidtCardPrice = oxNew( 'oxprice' );
00386         $this->_oGidtCardPrice->setBruttoPriceMode();
00387         $this->_oGidtCardPrice->setPrice( $this->oxorder__oxgiftcardcost->value, $this->oxorder__oxgiftcardvat->value );
00388         return $this->_oGidtCardPrice;
00389     }
00390 
00391 
00397     public function getOrderPaymentPrice()
00398     {
00399         if ( $this->_oPaymentPrice != null ) {
00400             return $this->_oPaymentPrice;
00401         }
00402 
00403         $this->_oPaymentPrice = oxNew( 'oxprice' );
00404         $this->_oPaymentPrice->setBruttoPriceMode();
00405         $this->_oPaymentPrice->setPrice( $this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value );
00406         return $this->_oPaymentPrice;
00407     }
00408 
00414     public function getOrderTsProtectionPrice()
00415     {
00416         if ( $this->_oTsProtectionPrice != null ) {
00417             return $this->_oTsProtectionPrice;
00418         }
00419 
00420         $this->_oTsProtectionPrice = oxNew( 'oxprice' );
00421         $this->_oTsProtectionPrice->setBruttoPriceMode();
00422         $this->_oTsProtectionPrice->setPrice( $this->oxorder__oxtsprotectcosts->value, $this->getConfig()->getConfigParam( 'dDefaultVAT' ) );
00423         return $this->_oTsProtectionPrice;
00424     }
00425 
00432     public function getOrderNetSum()
00433     {
00434         $dTotalNetSum = 0;
00435 
00436         $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
00437         $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
00438         $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
00439         $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
00440 
00441         return $dTotalNetSum;
00442     }
00443 
00464     public function finalizeOrder( oxBasket $oBasket, $oUser, $blRecalculatingOrder = false )
00465     {
00466         // check if this order is already stored
00467         $sGetChallenge = oxSession::getVar( 'sess_challenge' );
00468         if ( $this->_checkOrderExist( $sGetChallenge ) ) {
00469             oxRegistry::getUtils()->logger( 'BLOCKER' );
00470             // we might use this later, this means that somebody klicked like mad on order button
00471             return self::ORDER_STATE_ORDEREXISTS;
00472         }
00473 
00474         // if not recalculating order, use sess_challenge id, else leave old order id
00475         if ( !$blRecalculatingOrder ) {
00476             // use this ID
00477             $this->setId( $sGetChallenge );
00478 
00479             // validating various order/basket parameters before finalizing
00480             if ( $iOrderState = $this->validateOrder( $oBasket, $oUser ) ) {
00481                 return $iOrderState;
00482             }
00483         }
00484 
00485         // copies user info
00486         $this->_setUser( $oUser );
00487 
00488         // copies basket info
00489         $this->_loadFromBasket( $oBasket );
00490 
00491         // payment information
00492         $oUserPayment = $this->_setPayment( $oBasket->getPaymentId() );
00493 
00494         // set folder information, if order is new
00495         // #M575 in recalculating order case folder must be the same as it was
00496         if ( !$blRecalculatingOrder ) {
00497             $this->_setFolder();
00498         }
00499 
00500         // marking as not finished
00501         $this->_setOrderStatus( 'NOT_FINISHED' );
00502 
00503         //saving all order data to DB
00504         $this->save();
00505 
00506         // executing payment (on failure deletes order and returns error code)
00507         // in case when recalculating order, payment execution is skipped
00508         if ( !$blRecalculatingOrder ) {
00509             $blRet = $this->_executePayment( $oBasket, $oUserPayment );
00510             if ( $blRet !== true ) {
00511                 return $blRet;
00512             }
00513         }
00514 
00515         // executing TS protection
00516         if ( !$blRecalculatingOrder && $oBasket->getTsProductId()) {
00517             $blRet = $this->_executeTsProtection( $oBasket );
00518             if ( $blRet !== true ) {
00519                 return $blRet;
00520             }
00521         }
00522 
00523         // deleting remark info only when order is finished
00524         oxSession::deleteVar( 'ordrem' );
00525         oxSession::deleteVar( 'stsprotection' );
00526 
00527         if ( !$this->oxorder__oxordernr->value ) {
00528             $this->_setNumber();
00529         } else {
00530             oxNew( 'oxCounter' )->update( $this->_getCounterIdent(), $this->oxorder__oxordernr->value );
00531         }
00532 
00533         //#4005: Order creation time is not updated when order processing is complete
00534         if ( !$blRecalculatingOrder ) {
00535            $this-> _updateOrderDate();
00536         }
00537 
00538         // updating order trans status (success status)
00539         $this->_setOrderStatus( 'OK' );
00540 
00541         // store orderid
00542         $oBasket->setOrderId( $this->getId() );
00543 
00544         // updating wish lists
00545         $this->_updateWishlist( $oBasket->getContents(), $oUser );
00546 
00547         // updating users notice list
00548         $this->_updateNoticeList( $oBasket->getContents(), $oUser );
00549 
00550         // marking vouchers as used and sets them to $this->_aVoucherList (will be used in order email)
00551         // skipping this action in case of order recalculation
00552         if ( !$blRecalculatingOrder ) {
00553             $this->_markVouchers( $oBasket, $oUser );
00554         }
00555 
00556         // send order by email to shop owner and current user
00557         // skipping this action in case of order recalculation
00558         if ( !$blRecalculatingOrder ) {
00559             $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
00560         } else {
00561             $iRet = self::ORDER_STATE_OK;
00562         }
00563 
00564         return $iRet;
00565     }
00566 
00572     public function isNettoMode()
00573     {
00574         return (bool) $this->oxorder__oxisnettomode->value;
00575     }
00576 
00577 
00585     protected function _setOrderStatus( $sStatus )
00586     {
00587         $oDb = oxDb::getDb();
00588         $sQ = 'update oxorder set oxtransstatus='.$oDb->quote( $sStatus ).' where oxid='.$oDb->quote( $this->getId() );
00589         $oDb->execute( $sQ );
00590 
00591         //updating order object
00592         $this->oxorder__oxtransstatus = new oxField( $sStatus, oxField::T_RAW );
00593     }
00594 
00602     protected function _convertVat( $sVat )
00603     {
00604         if ( strpos( $sVat, '.' ) < strpos( $sVat, ',' ) ) {
00605             $sVat = str_replace( array( '.', ',' ), array( '', '.' ), $sVat );
00606         } else {
00607             $sVat = str_replace( ',', '', $sVat );
00608         }
00609         return (float) $sVat;
00610     }
00611 
00617     protected function _resetVats()
00618     {
00619         $this->oxorder__oxartvat1      = new oxField( null );
00620         $this->oxorder__oxartvatprice1 = new oxField( null );
00621         $this->oxorder__oxartvat2      = new oxField( null );
00622         $this->oxorder__oxartvatprice2 = new oxField( null );
00623     }
00624 
00635     protected function _loadFromBasket( oxBasket $oBasket )
00636     {
00637         $myConfig = $this->getConfig();
00638 
00639         // store IP Address - default must be FALSE as it is illegal to store
00640         if ( $myConfig->getConfigParam( 'blStoreIPs' ) &&  $this->oxorder__oxip->value === null ) {
00641             $this->oxorder__oxip = new oxField(oxRegistry::get("oxUtilsServer")->getRemoteAddress(), oxField::T_RAW);
00642         }
00643 
00644         //setting view mode
00645         $this->oxorder__oxisnettomode   = new oxField($oBasket->isCalculationModeNetto());
00646 
00647         // copying main price info
00648         $this->oxorder__oxtotalnetsum   = new oxField($oBasket->getNettoSum());
00649         $this->oxorder__oxtotalbrutsum  = new oxField($oBasket->getBruttoSum());
00650         $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00651 
00652         // copying discounted VAT info
00653         $this->_resetVats();
00654         $iVatIndex = 1;
00655         foreach ( $oBasket->getProductVats(false) as $iVat => $dPrice ) {
00656             $this->{"oxorder__oxartvat$iVatIndex"}      = new oxField( $this->_convertVat( $iVat ), oxField::T_RAW);
00657             $this->{"oxorder__oxartvatprice$iVatIndex"} = new oxField($dPrice, oxField::T_RAW);
00658             $iVatIndex ++;
00659         }
00660 
00661         // payment costs if available
00662         if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00663             $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00664             $this->oxorder__oxpayvat  = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00665         }
00666 
00667         // delivery info
00668         if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00669             $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00670             //V #M382: Save VAT, not VAT value for delivery costs
00671             $this->oxorder__oxdelvat  = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW); //V #M382
00672             $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00673         }
00674 
00675         // user remark
00676         if ( !isset( $this->oxorder__oxremark ) || $this->oxorder__oxremark->value === null ) {
00677             $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00678         }
00679 
00680         // currency
00681         $oCur = $myConfig->getActShopCurrencyObject();
00682         $this->oxorder__oxcurrency = new oxField($oCur->name);
00683         $this->oxorder__oxcurrate  = new oxField($oCur->rate, oxField::T_RAW);
00684 
00685         // store voucher discount
00686         if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00687             $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00688         }
00689 
00690         // general discount
00691         if ( $this->_blReloadDiscount ) {
00692             $dDiscount = 0;
00693             $aDiscounts = $oBasket->getDiscounts();
00694             if ( count($aDiscounts) > 0 ) {
00695                 foreach ($aDiscounts as $oDiscount) {
00696                     $dDiscount += $oDiscount->dDiscount;
00697                 }
00698             }
00699             $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00700         }
00701 
00702         //order language
00703         $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00704 
00705 
00706         // initial status - 'ERROR'
00707         $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00708 
00709         // copies basket product info ...
00710         $this->_setOrderArticles( $oBasket->getContents() );
00711 
00712         // copies wrapping info
00713         $this->_setWrapping( $oBasket );
00714 
00715         // copies TS protection info
00716         $this->_setTsProtection( $oBasket );
00717     }
00718 
00725     public function getOrderLanguage()
00726     {
00727         if ( $this->_iOrderLang === null ) {
00728             if ( isset( $this->oxorder__oxlang->value ) ) {
00729                 $this->_iOrderLang = oxRegistry::getLang()->validateLanguage( $this->oxorder__oxlang->value );
00730             } else {
00731                 $this->_iOrderLang = oxRegistry::getLang()->getBaseLanguage();
00732             }
00733         }
00734         return $this->_iOrderLang;
00735     }
00736 
00744     protected function _setUser( $oUser )
00745     {
00746 
00747         $this->oxorder__oxuserid        = new oxField($oUser->getId());
00748 
00749         // bill address
00750         $this->oxorder__oxbillcompany     = clone $oUser->oxuser__oxcompany;
00751         $this->oxorder__oxbillemail       = clone $oUser->oxuser__oxusername;
00752         $this->oxorder__oxbillfname       = clone $oUser->oxuser__oxfname;
00753         $this->oxorder__oxbilllname       = clone $oUser->oxuser__oxlname;
00754         $this->oxorder__oxbillstreet      = clone $oUser->oxuser__oxstreet;
00755         $this->oxorder__oxbillstreetnr    = clone $oUser->oxuser__oxstreetnr;
00756         $this->oxorder__oxbilladdinfo     = clone $oUser->oxuser__oxaddinfo;
00757         $this->oxorder__oxbillustid       = clone $oUser->oxuser__oxustid;
00758         $this->oxorder__oxbillcity        = clone $oUser->oxuser__oxcity;
00759         $this->oxorder__oxbillcountryid   = clone $oUser->oxuser__oxcountryid;
00760         $this->oxorder__oxbillstateid     = clone $oUser->oxuser__oxstateid;
00761         $this->oxorder__oxbillzip         = clone $oUser->oxuser__oxzip;
00762         $this->oxorder__oxbillfon         = clone $oUser->oxuser__oxfon;
00763         $this->oxorder__oxbillfax         = clone $oUser->oxuser__oxfax;
00764         $this->oxorder__oxbillsal         = clone $oUser->oxuser__oxsal;
00765 
00766 
00767         // delivery address
00768         if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00769             // set delivery address
00770             $this->oxorder__oxdelcompany   = clone $oDelAdress->oxaddress__oxcompany;
00771             $this->oxorder__oxdelfname     = clone $oDelAdress->oxaddress__oxfname;
00772             $this->oxorder__oxdellname     = clone $oDelAdress->oxaddress__oxlname;
00773             $this->oxorder__oxdelstreet    = clone $oDelAdress->oxaddress__oxstreet;
00774             $this->oxorder__oxdelstreetnr  = clone $oDelAdress->oxaddress__oxstreetnr;
00775             $this->oxorder__oxdeladdinfo   = clone $oDelAdress->oxaddress__oxaddinfo;
00776             $this->oxorder__oxdelcity      = clone $oDelAdress->oxaddress__oxcity;
00777             $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00778             $this->oxorder__oxdelstateid   = clone $oDelAdress->oxaddress__oxstateid;
00779             $this->oxorder__oxdelzip       = clone $oDelAdress->oxaddress__oxzip;
00780             $this->oxorder__oxdelfon       = clone $oDelAdress->oxaddress__oxfon;
00781             $this->oxorder__oxdelfax       = clone $oDelAdress->oxaddress__oxfax;
00782             $this->oxorder__oxdelsal       = clone $oDelAdress->oxaddress__oxsal;
00783         }
00784     }
00785 
00793     protected function _setWrapping( oxBasket $oBasket )
00794     {
00795         $myConfig = $this->getConfig();
00796 
00797         // wrapping price
00798         if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00799             $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00800             // wrapping VAT will be always calculated (#3757)
00801             $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00802         }
00803 
00804         if ( ( $oGiftCardCost = $oBasket->getCosts( 'oxgiftcard' ) ) ) {
00805             $this->oxorder__oxgiftcardcost = new oxField( $oGiftCardCost->getBruttoPrice(), oxField::T_RAW);
00806             $this->oxorder__oxgiftcardvat  = new oxField( $oGiftCardCost->getVAT(), oxField::T_RAW);
00807         }
00808 
00809         // greetings card
00810         $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00811 
00812         // card text will be stored in database
00813         $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00814     }
00815 
00824     protected function _setOrderArticles( $aArticleList )
00825     {
00826         // reset articles list
00827         $this->_oArticles = oxNew( 'oxlist' );
00828         $iCurrLang = $this->getOrderLanguage();
00829 
00830         // add all the products we have on basket to the order
00831         foreach ( $aArticleList as $oContent ) {
00832 
00833             //$oContent->oProduct = $oContent->getArticle();
00834             // #M773 Do not use article lazy loading on order save
00835             $oProduct = $oContent->getArticle( true, null, true);
00836 
00837             // copy only if object is oxarticle type
00838             if ( $oProduct->isOrderArticle() ) {
00839                 $oOrderArticle = $oProduct;
00840             } else {
00841 
00842                 // if order language doe not match product language - article must be reloaded in order language
00843                 if ( $iCurrLang != $oProduct->getLanguage() ) {
00844                     $oProduct->loadInLang( $iCurrLang, $oProduct->getProductId() );
00845                 }
00846 
00847                 // set chosen select list
00848                 $sSelList = '';
00849                 if ( count( $aChosenSelList = $oContent->getChosenSelList() ) ) {
00850                     foreach ( $aChosenSelList as $oItem ) {
00851                         if ( $sSelList ) {
00852                             $sSelList .= ", ";
00853                         }
00854                         $sSelList .= "{$oItem->name} : {$oItem->value}";
00855                     }
00856                 }
00857 
00858                 $oOrderArticle = oxNew( 'oxorderarticle' );
00859                 $oOrderArticle->setIsNewOrderItem( true );
00860                 $oOrderArticle->copyThis( $oProduct );
00861                 $oOrderArticle->setId();
00862 
00863                 $oOrderArticle->oxorderarticles__oxartnum     = clone $oProduct->oxarticles__oxartnum;
00864                 $oOrderArticle->oxorderarticles__oxselvariant = new oxField( trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->getRawValue() ), oxField::T_RAW );
00865                 $oOrderArticle->oxorderarticles__oxshortdesc  = new oxField( $oProduct->oxarticles__oxshortdesc->getRawValue(), oxField::T_RAW );
00866                 // #M974: duplicated entries for the name of variants in orders
00867                 $oOrderArticle->oxorderarticles__oxtitle      = new oxField( trim( $oProduct->oxarticles__oxtitle->getRawValue() ), oxField::T_RAW );
00868 
00869                 // copying persistent parameters ...
00870                 if ( !is_array( $aPersParams = $oProduct->getPersParams() ) ) {
00871                     $aPersParams = $oContent->getPersParams();
00872                 }
00873                 if ( is_array( $aPersParams ) && count( $aPersParams )) {
00874                     $oOrderArticle->oxorderarticles__oxpersparam = new oxField( serialize( $aPersParams ), oxField::T_RAW );
00875                 }
00876             }
00877 
00878             // ids, titles, numbers ...
00879             $oOrderArticle->oxorderarticles__oxorderid = new oxField( $this->getId() );
00880             $oOrderArticle->oxorderarticles__oxartid   = new oxField( $oContent->getProductId() );
00881             $oOrderArticle->oxorderarticles__oxamount  = new oxField( $oContent->getAmount() );
00882 
00883             // prices
00884             $oPrice = $oContent->getPrice();
00885             $oOrderArticle->oxorderarticles__oxnetprice  = new oxField( $oPrice->getNettoPrice(), oxField::T_RAW );
00886             $oOrderArticle->oxorderarticles__oxvatprice  = new oxField( $oPrice->getVatValue(), oxField::T_RAW );
00887             $oOrderArticle->oxorderarticles__oxbrutprice = new oxField( $oPrice->getBruttoPrice(), oxField::T_RAW );
00888             $oOrderArticle->oxorderarticles__oxvat       = new oxField( $oPrice->getVat(), oxField::T_RAW );
00889 
00890             $oUnitPtice = $oContent->getUnitPrice();
00891             $oOrderArticle->oxorderarticles__oxnprice = new oxField( $oUnitPtice->getNettoPrice(), oxField::T_RAW );
00892             $oOrderArticle->oxorderarticles__oxbprice = new oxField( $oUnitPtice->getBruttoPrice(), oxField::T_RAW );
00893 
00894             // wrap id
00895             $oOrderArticle->oxorderarticles__oxwrapid = new oxField( $oContent->getWrappingId(), oxField::T_RAW );
00896 
00897             // items shop id
00898             $oOrderArticle->oxorderarticles__oxordershopid = new oxField( $oContent->getShopId(), oxField::T_RAW );
00899 
00900             // bundle?
00901             $oOrderArticle->oxorderarticles__oxisbundle = new oxField( $oContent->isBundle() );
00902 
00903             // add information for eMail
00904             //P
00905             //TODO: check if this assign is needed at all
00906             $oOrderArticle->oProduct = $oProduct;
00907 
00908             $oOrderArticle->setArticle( $oProduct );
00909 
00910             // simulation order article list
00911             $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
00912         }
00913     }
00914 
00926     protected function _executePayment( oxBasket $oBasket, $oUserpayment )
00927     {
00928         $oPayTransaction = $this->_getGateway();
00929         $oPayTransaction->setPaymentParams( $oUserpayment );
00930 
00931         if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
00932             $this->delete();
00933 
00934             // checking for error messages
00935             if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
00936                 if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
00937                     return $sLastError;
00938                 }
00939             }
00940 
00941             // checking for error codes
00942             if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
00943                 if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
00944                     return $iLastErrorNo;
00945                 }
00946             }
00947 
00948             return self::ORDER_STATE_PAYMENTERROR; // means no authentication
00949         }
00950         return true; // everything fine
00951     }
00952 
00959     protected function _getGateway()
00960     {
00961         return oxNew( 'oxPaymentGateway' );
00962     }
00963 
00971     protected function _setPayment( $sPaymentid )
00972     {
00973         // copying payment info fields
00974         $aDynvalue = oxSession::getVar( 'dynvalue' );
00975         $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
00976 
00977         // loading payment object
00978         $oPayment = oxNew( 'oxpayment' );
00979 
00980         if (!$oPayment->load( $sPaymentid )) {
00981             return null;
00982         }
00983 
00984         // #756M Preserve already stored payment information
00985         if ( !$aDynvalue && ( $oUserpayment = $this->getPaymentType() ) ) {
00986             if ( is_array( $aStoredDynvalue = $oUserpayment->getDynValues() ) ) {
00987                 foreach ( $aStoredDynvalue as $oVal ) {
00988                     $aDynvalue[$oVal->name] = $oVal->value;
00989                 }
00990             }
00991         }
00992 
00993         $oPayment->setDynValues( oxRegistry::getUtils()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
00994 
00995         // collecting dynamic values
00996         $aDynVal = array();
00997 
00998         if ( is_array( $aPaymentDynValues = $oPayment->getDynValues() ) ) {
00999             foreach ( $aPaymentDynValues  as $key => $oVal ) {
01000                 if ( isset( $aDynvalue[$oVal->name] ) ) {
01001                     $oVal->value = $aDynvalue[$oVal->name];
01002                 }
01003 
01004                 //$oPayment->setDynValue($key, $oVal);
01005                 $aPaymentDynValues[$key] = $oVal;
01006                 $aDynVal[$oVal->name] = $oVal->value;
01007             }
01008         }
01009 
01010         // Store this payment information, we might allow users later to
01011         // reactivate already give payment information
01012 
01013         $oUserpayment = oxNew( 'oxuserpayment' );
01014         $oUserpayment->oxuserpayments__oxuserid     = clone $this->oxorder__oxuserid;
01015         $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
01016         $oUserpayment->oxuserpayments__oxvalue      = new oxField(oxRegistry::getUtils()->assignValuesToText( $aDynVal ), oxField::T_RAW);
01017         $oUserpayment->oxpayments__oxdesc           = clone $oPayment->oxpayments__oxdesc;
01018         $oUserpayment->oxpayments__oxlongdesc       = clone $oPayment->oxpayments__oxlongdesc;
01019         $oUserpayment->setDynValues( $aPaymentDynValues );
01020         $oUserpayment->save();
01021 
01022         // storing payment information to order
01023         $this->oxorder__oxpaymentid   = new oxField($oUserpayment->getId(), oxField::T_RAW);
01024         $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
01025 
01026         // returning user payment object which will be used later in code ...
01027         return $oUserpayment;
01028     }
01029 
01035     protected function _setFolder()
01036     {
01037         $myConfig = $this->getConfig();
01038         $this->oxorder__oxfolder    = new oxField(key( $myConfig->getShopConfVar(  'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
01039     }
01040 
01050     protected function _updateWishlist( $aArticleList, $oUser )
01051     {
01052 
01053         foreach ( $aArticleList as $oContent) {
01054             if ( ( $sWishId = $oContent->getWishId() ) ) {
01055 
01056                 // checking which wishlist user uses ..
01057                 if ( $sWishId == $oUser->getId() ) {
01058                     $oUserBasket = $oUser->getBasket( 'wishlist' );
01059                 } else {
01060                     $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
01061                     $oUserBasket = oxNew( 'oxuserbasket' );
01062                     $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
01063                 }
01064 
01065                 // updating users wish list
01066                 if ( $oUserBasket ) {
01067                     if ( !($sProdId = $oContent->getWishArticleId() )) {
01068                         $sProdId = $oContent->getProductId();
01069                     }
01070                     $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
01071                     $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
01072                     if ( $dNewAmount < 0) {
01073                         $dNewAmount = 0;
01074                     }
01075                     $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
01076                 }
01077             }
01078         }
01079     }
01080 
01090     protected function _updateNoticeList( $aArticleList, $oUser )
01091     {
01092         // loading users notice list ..
01093         if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
01094             // only if wishlist is enabled
01095             foreach ( $aArticleList as $oContent) {
01096                 $sProdId = $oContent->getProductId();
01097 
01098                 // updating users notice list
01099                 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList(), $oContent->getPersParams() );
01100                 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
01101                 if ( $dNewAmount < 0) {
01102                     $dNewAmount = 0;
01103                 }
01104                 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true, $oContent->getPersParams() );
01105             }
01106         }
01107     }
01108 
01114     protected function _updateOrderDate()
01115     {
01116         $oDb = oxDb::getDb();
01117         $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
01118         $sQ = 'update oxorder set oxorderdate=' . $oDb->quote( $sDate ) . ' where oxid=' . $oDb->quote( $this->getId() );
01119         $this->oxorder__oxorderdate = new oxField( $sDate, oxField::T_RAW );
01120         $oDb->execute( $sQ );
01121     }
01122 
01132     protected function _markVouchers( $oBasket, $oUser )
01133     {
01134         $this->_aVoucherList = $oBasket->getVouchers();
01135 
01136         if ( is_array( $this->_aVoucherList ) ) {
01137             foreach ( $this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
01138                 $oVoucher = oxNew( 'oxvoucher' );
01139                 $oVoucher->load( $sVoucherId );
01140                 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->dVoucherdiscount );
01141 
01142                 $this->_aVoucherList[$sVoucherId] = $oVoucher;
01143             }
01144         }
01145     }
01146 
01152     public function save()
01153     {
01154         if ( ( $blSave = parent::save() ) ) {
01155 
01156             // saving order articles
01157             $oOrderArticles = $this->getOrderArticles();
01158             if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
01159                 foreach ( $oOrderArticles as $oOrderArticle ) {
01160                     $oOrderArticle->save();
01161                 }
01162             }
01163         }
01164 
01165         return $blSave;
01166     }
01167 
01174     public function getDelAddressInfo()
01175     {
01176         $oDelAdress = null;
01177         if (! ($soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
01178             $soxAddressId = oxSession::getVar( 'deladrid' );
01179         }
01180         if ( $soxAddressId ) {
01181             $oDelAdress = oxNew( 'oxaddress' );
01182             $oDelAdress->load( $soxAddressId );
01183 
01184             //get delivery country name from delivery country id
01185             if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
01186                  $oCountry = oxNew( 'oxcountry' );
01187                  $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
01188                  $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
01189             }
01190         }
01191         return $oDelAdress;
01192     }
01193 
01204     public function validateStock( $oBasket )
01205     {
01206         foreach ( $oBasket->getContents() as $key => $oContent ) {
01207             try {
01208                 $oProd = $oContent->getArticle( true );
01209             } catch ( oxNoArticleException $oEx ) {
01210                 $oBasket->removeItem( $key );
01211                 throw $oEx;
01212             } catch ( oxArticleInputException $oEx ) {
01213                 $oBasket->removeItem( $key );
01214                 throw $oEx;
01215             }
01216 
01217             // check if its still available
01218             $dArtStockAmount = $oBasket->getArtStockInBasket( $oProd->getId(), $key );
01219             $iOnStock = $oProd->checkForStock( $oContent->getAmount(), $dArtStockAmount );
01220             if ( $iOnStock !== true ) {
01221                 $oEx = oxNew( 'oxOutOfStockException' );
01222                 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
01223                 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
01224                 $oEx->setProductId( $oProd->getId() );
01225 
01226                 if (!is_numeric($iOnStock)) {
01227                     $iOnStock = 0;
01228                 }
01229                 $oEx->setRemainingAmount( $iOnStock );
01230                 throw $oEx;
01231             }
01232         }
01233     }
01234 
01240     protected function _insert()
01241     {
01242         $myConfig = $this->getConfig();
01243         $oUtilsDate = oxRegistry::get("oxUtilsDate");
01244 
01245         //V #M525 orderdate must be the same as it was
01246         if ( !$this->oxorder__oxorderdate->value ) {
01247             $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
01248         } else {
01249             $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
01250         }
01251 
01252         $this->oxorder__oxshopid    = new oxField($myConfig->getShopId(), oxField::T_RAW);
01253         $this->oxorder__oxsenddate  = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01254 
01255         $blInsert = parent::_insert();
01256 
01257         return $blInsert;
01258     }
01259 
01265     protected function _getCounterIdent()
01266     {
01267         $sCounterIdent = ( $this->_blSeparateNumbering ) ? 'oxOrder_' . $this->getConfig()->getShopId() : 'oxOrder';
01268         return $sCounterIdent;
01269     }
01270 
01271 
01277     protected function _setNumber()
01278     {
01279         $oDb = oxDb::getDb();
01280 
01281         $iCnt = oxNew( 'oxCounter' )->getNext( $this->_getCounterIdent() );
01282         $sQ = "update oxorder set oxordernr = ? where oxid = ?";
01283         $blUpdate = ( bool ) $oDb->execute( $sQ, array($iCnt, $this->getId() ) );
01284 
01285         if ( $blUpdate ) {
01286             $this->oxorder__oxordernr = new oxField( $iCnt );
01287         }
01288 
01289         return $blUpdate;
01290     }
01291 
01297     protected function _update()
01298     {
01299         $this->_aSkipSaveFields = array( 'oxtimestamp', 'oxorderdate' );
01300         $this->oxorder__oxsenddate = new oxField(oxRegistry::get("oxUtilsDate")->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01301         return parent::_update();
01302     }
01303 
01312     public function delete( $sOxId = null )
01313     {
01314         if ( $sOxId ) {
01315             if ( !$this->load( $sOxId ) ) {
01316                 // such order does not exist
01317                 return false;
01318             }
01319         } elseif ( !$sOxId ) {
01320             $sOxId = $this->getId();
01321         }
01322 
01323         // no order id is passed
01324         if ( !$sOxId ) {
01325             return false;
01326         }
01327 
01328 
01329         // delete order articles
01330         $oOrderArticles = $this->getOrderArticles( false );
01331         foreach ( $oOrderArticles as $oOrderArticle ) {
01332             $oOrderArticle->delete();
01333         }
01334 
01335         // #440 - deleting user payment info
01336         if ( $oPaymentType = $this->getPaymentType() ) {
01337             $oPaymentType->delete();
01338         }
01339 
01340         return parent::delete( $sOxId );
01341     }
01342 
01352     public function recalculateOrder( $aNewArticles = array() )
01353     {
01354         oxDb::getDb()->startTransaction();
01355 
01356         try {
01357             $oBasket = $this->_getOrderBasket();
01358 
01359             // add this order articles to virtual basket and recalculates basket
01360             $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles( true ) );
01361 
01362             // adding new articles to existing order
01363             $this->_addArticlesToBasket( $oBasket, $aNewArticles );
01364 
01365             // recalculating basket
01366             $oBasket->calculateBasket( true );
01367 
01368             //finalizing order (skipping payment execution, vouchers marking and mail sending)
01369             $iRet = $this->finalizeOrder( $oBasket, $this->getOrderUser(), true );
01370 
01371             //if finalizing order failed, rollback transaction
01372             if ( $iRet !== 1 ) {
01373                 oxDb::getDb()->rollbackTransaction();
01374             } else {
01375                 oxDb::getDb()->commitTransaction();
01376             }
01377 
01378         } catch( Exception $oE ) {
01379             // if exception, rollBack everything
01380             oxDb::getDb()->rollbackTransaction();
01381 
01382             if ( defined( 'OXID_PHP_UNIT' ) ) {
01383                 throw $oE;
01384             }
01385         }
01386     }
01387 
01388     protected $_oOrderBasket = null;
01396     protected function _getOrderBasket( $blStockCheck = true )
01397     {
01398         $this->_oOrderBasket = oxNew( "oxbasket" );
01399 
01400         //setting recalculation mode
01401         $this->_oOrderBasket->setCalculationModeNetto( $this->isNettoMode() );
01402 
01403         // setting stock check mode
01404         $this->_oOrderBasket->setStockCheckMode( $blStockCheck );
01405 
01406         // setting virtual basket user
01407         $this->_oOrderBasket->setBasketUser( $this->getOrderUser() );
01408 
01409         // transferring order id
01410         $this->_oOrderBasket->setOrderId( $this->getId() );
01411 
01412         // setting basket currency order uses
01413         $aCurrencies = $this->getConfig()->getCurrencyArray();
01414         foreach ( $aCurrencies as $oCur ) {
01415             if ($oCur->name == $this->oxorder__oxcurrency->value) {
01416                 $oBasketCur = $oCur;
01417                 break;
01418             }
01419         }
01420 
01421         // setting currency
01422         $this->_oOrderBasket->setBasketCurrency( $oBasketCur );
01423 
01424         // set basket card id and message
01425         $this->_oOrderBasket->setCardId( $this->oxorder__oxcardid->value );
01426         $this->_oOrderBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01427 
01428         if ( $this->_blReloadDiscount ) {
01429             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01430             // disabling availability check
01431             $this->_oOrderBasket->setSkipVouchersChecking( true );
01432 
01433             // add previously used vouchers
01434             $sQ = 'select oxid from oxvouchers where oxorderid = '.$oDb->quote( $this->getId() );
01435             $aVouchers = $oDb->getAll( $sQ );
01436             foreach ( $aVouchers as $aVoucher ) {
01437                 $this->_oOrderBasket->addVoucher( $aVoucher['oxid'] );
01438             }
01439         } else {
01440             $this->_oOrderBasket->setDiscountCalcMode( false );
01441             $this->_oOrderBasket->setVoucherDiscount( $this->oxorder__oxvoucherdiscount->value );
01442             $this->_oOrderBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
01443         }
01444 
01445         // must be kept old delivery?
01446         if ( !$this->_blReloadDelivery ) {
01447             $this->_oOrderBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01448         } else {
01449             //  set shipping
01450             $this->_oOrderBasket->setShipping( $this->oxorder__oxdeltype->value );
01451             $this->_oOrderBasket->setDeliveryPrice( null );
01452         }
01453 
01454         //set basket payment
01455         $this->_oOrderBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01456 
01457         return $this->_oOrderBasket;
01458     }
01459 
01468     public function setDelivery( $sDeliveryId )
01469     {
01470         $this->reloadDelivery( true );
01471         $this->oxorder__oxdeltype = new oxField( $sDeliveryId );
01472     }
01473 
01479     public function getOrderUser()
01480     {
01481         if ($this->_oUser === null ) {
01482             $this->_oUser = oxNew( "oxuser" );
01483             $this->_oUser->load( $this->oxorder__oxuserid->value );
01484 
01485             // if object is loaded then reusing its order info
01486             if ( $this->_isLoaded ) {
01487                 // bill address
01488                 $this->_oUser->oxuser__oxcompany  = clone $this->oxorder__oxbillcompany;
01489                 $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
01490                 $this->_oUser->oxuser__oxfname    = clone $this->oxorder__oxbillfname;
01491                 $this->_oUser->oxuser__oxlname    = clone $this->oxorder__oxbilllname;
01492                 $this->_oUser->oxuser__oxstreet   = clone $this->oxorder__oxbillstreet;
01493                 $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
01494                 $this->_oUser->oxuser__oxaddinfo  = clone $this->oxorder__oxbilladdinfo;
01495                 $this->_oUser->oxuser__oxustid    = clone $this->oxorder__oxbillustid;
01496 
01497 
01498                 $this->_oUser->oxuser__oxcity      = clone $this->oxorder__oxbillcity;
01499                 $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
01500                 $this->_oUser->oxuser__oxstateid   = clone $this->oxorder__oxbillstateid;
01501                 $this->_oUser->oxuser__oxzip       = clone $this->oxorder__oxbillzip;
01502                 $this->_oUser->oxuser__oxfon       = clone $this->oxorder__oxbillfon;
01503                 $this->_oUser->oxuser__oxfax       = clone $this->oxorder__oxbillfax;
01504                 $this->_oUser->oxuser__oxsal       = clone $this->oxorder__oxbillsal;
01505             }
01506         }
01507 
01508         return $this->_oUser;
01509     }
01510 
01518     public function pdfFooter( $oPdf )
01519     {
01520     }
01521 
01529     public function pdfHeaderplus( $oPdf )
01530     {
01531     }
01532 
01540     public function pdfHeader( $oPdf )
01541     {
01542     }
01543 
01552     public function genPdf( $sFilename, $iSelLang = 0 )
01553     {
01554     }
01555 
01561     public function getInvoiceNum()
01562     {
01563         $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01564         return ( ( int ) oxDb::getDb()->getOne( $sQ, false ) + 1 );
01565     }
01566 
01572     public function getNextBillNum()
01573     {
01574         $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01575         return ( ( int ) oxDb::getDb()->getOne( $sQ, false ) + 1 );
01576     }
01577 
01583     public function getShippingSetList()
01584     {
01585         // in which country we deliver
01586         if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
01587             $sShipId = $this->oxorder__oxbillcountryid->value;
01588         }
01589 
01590         $oBasket = $this->_getOrderBasket( false );
01591 
01592         // unsetting bundles
01593         $oOrderArticles = $this->getOrderArticles();
01594         foreach ( $oOrderArticles as $sItemId => $oItem ) {
01595             if ( $oItem->isBundle() ) {
01596                 $oOrderArticles->offsetUnset( $sItemId );
01597             }
01598         }
01599 
01600         // add this order articles to basket and recalculate basket
01601         $this->_addOrderArticlesToBasket( $oBasket, $oOrderArticles );
01602 
01603         // recalculating basket
01604         $oBasket->calculateBasket( true );
01605 
01606         // load fitting deliveries list
01607         $oDeliveryList = oxNew( "oxDeliveryList", "core" );
01608         $oDeliveryList->setCollectFittingDeliveriesSets( true );
01609 
01610         return $oDeliveryList->getDeliveryList( $oBasket, $this->getOrderUser(), $sShipId );
01611     }
01612 
01618     public function getVoucherNrList()
01619     {
01620         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01621         $aVouchers = array();
01622         $sSelect = "select oxvouchernr from oxvouchers where oxorderid = ".$oDb->quote( $this->oxorder__oxid->value );
01623         $rs = $oDb->select( $sSelect );
01624         if ($rs != false && $rs->recordCount() > 0) {
01625             while (!$rs->EOF) {
01626                 $aVouchers[] = $rs->fields['oxvouchernr'];
01627                 $rs->moveNext();
01628             }
01629         }
01630         return $aVouchers;
01631     }
01632 
01640     public function getOrderSum( $blToday = false )
01641     {
01642         $sSelect  = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01643         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01644 
01645         if ( $blToday ) {
01646             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01647         }
01648 
01649         return ( double ) oxDb::getDb()->getOne( $sSelect, false, false );
01650     }
01651 
01659     public function getOrderCnt( $blToday = false )
01660     {
01661         $sSelect  = 'select count(*) from oxorder where ';
01662         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'"  and oxorder.oxstorno != "1" ';
01663 
01664         if ( $blToday ) {
01665             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01666         }
01667 
01668         return ( int ) oxDb::getDb()->getOne( $sSelect, false, false );
01669     }
01670 
01671 
01679     protected function _checkOrderExist( $sOxId = null )
01680     {
01681         if ( !$sOxId) {
01682             return false;
01683         }
01684 
01685         $oDb = oxDb::getDb();
01686         if ( $oDb->getOne( 'select oxid from oxorder where oxid = '.$oDb->quote( $sOxId ), false, false ) ) {
01687             return true;
01688         }
01689 
01690         return false;
01691     }
01692 
01702     protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01703     {
01704         $iRet = self::ORDER_STATE_MAILINGERROR;
01705 
01706         // add user, basket and payment to order
01707         $this->_oUser    = $oUser;
01708         $this->_oBasket  = $oBasket;
01709         $this->_oPayment = $oPayment;
01710 
01711         $oxEmail = oxNew( 'oxemail' );
01712 
01713         // send order email to user
01714         if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01715             // mail to user was successfully sent
01716             $iRet = self::ORDER_STATE_OK;
01717         }
01718 
01719         // send order email to shop owner
01720         $oxEmail->sendOrderEMailToOwner( $this );
01721 
01722         return $iRet;
01723     }
01724 
01730     public function getBasket()
01731     {
01732         return $this->_oBasket;
01733     }
01734 
01740     public function getPayment()
01741     {
01742         return $this->_oPayment;
01743     }
01744 
01750     public function getVoucherList()
01751     {
01752         return $this->_aVoucherList;
01753     }
01754 
01760     public function getDelSet()
01761     {
01762         if ( $this->_oDelSet == null ) {
01763             // load deliveryset info
01764             $this->_oDelSet = oxNew( 'oxdeliveryset' );
01765             $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01766         }
01767 
01768         return $this->_oDelSet;
01769     }
01770 
01776     public function getPaymentType()
01777     {
01778         if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType === null ) {
01779             $this->_oPaymentType = false;
01780             $oPaymentType = oxNew( 'oxuserpayment' );
01781             if ( $oPaymentType->load( $this->oxorder__oxpaymentid->value ) ) {
01782                 $this->_oPaymentType = $oPaymentType;
01783             }
01784         }
01785 
01786         return $this->_oPaymentType;
01787     }
01788 
01794     public function getGiftCard()
01795     {
01796         if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01797             $this->_oGiftCard = oxNew( 'oxwrapping' );
01798             $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01799         }
01800 
01801         return $this->_oGiftCard;
01802     }
01803 
01811     public function setSeparateNumbering( $blSeparateNumbering = null )
01812     {
01813         $this->_blSeparateNumbering = $blSeparateNumbering;
01814     }
01815 
01823     public function getLastUserPaymentType( $sUserId)
01824     {
01825         $oDb = oxDb::getDb();
01826         $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid='.$oDb->quote( $sUserId ).' order by oxorder.oxorderdate desc ';
01827         $sLastPaymentId = $oDb->getOne( $sQ, false, false );
01828         return $sLastPaymentId;
01829     }
01830 
01839     protected function _addOrderArticlesToBasket( $oBasket, $aOrderArticles )
01840     {
01841         // if no order articles, return empty basket
01842         if ( count( $aOrderArticles ) > 0 ) {
01843 
01844             //adding order articles to basket
01845             foreach ( $aOrderArticles as $oOrderArticle ) {
01846                 $oBasket->addOrderArticleToBasket( $oOrderArticle );
01847             }
01848         }
01849     }
01850 
01859     protected function _addArticlesToBasket( $oBasket, $aArticles )
01860     {
01861         // if no order articles
01862         if ( count($aArticles ) > 0 ) {
01863 
01864             //adding order articles to basket
01865             foreach ( $aArticles as $oArticle ) {
01866                 $aSel = isset( $oArticle->oxorderarticles__oxselvariant ) ? $oArticle->oxorderarticles__oxselvariant->value : null;
01867                 $aPersParam = isset( $oArticle->oxorderarticles__oxpersparam ) ? $oArticle->getPersParams() : null;
01868                 $oBasket->addToBasket( $oArticle->oxorderarticles__oxartid->value,
01869                                        $oArticle->oxorderarticles__oxamount->value,
01870                                        $aSel, $aPersParam );
01871             }
01872         }
01873     }
01874 
01880     public function getTotalOrderSum()
01881     {
01882         $oCur = $this->getConfig()->getActShopCurrencyObject();
01883         return number_format( (double)$this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01884     }
01885 
01893     public function getProductVats( $blFormatCurrency = true )
01894     {
01895         $aVats = array();
01896         if ($this->oxorder__oxartvat1->value) {
01897             $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
01898         }
01899         if ($this->oxorder__oxartvat2->value) {
01900             $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
01901         }
01902 
01903         if ( $blFormatCurrency ) {
01904             $oLang = oxRegistry::getLang();
01905             $oCur = $this->getConfig()->getActShopCurrencyObject();
01906             foreach ( $aVats as $sKey => $dVat ) {
01907                 $aVats[$sKey] = $oLang->formatCurrency( $dVat, $oCur );
01908             }
01909         }
01910         return $aVats;
01911     }
01912 
01918     public function getBillCountry()
01919     {
01920         if ( !$this->oxorder__oxbillcountry->value ) {
01921             $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
01922         }
01923         return $this->oxorder__oxbillcountry;
01924     }
01925 
01931     public function getDelCountry()
01932     {
01933         if ( !$this->oxorder__oxdelcountry->value ) {
01934             $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
01935         }
01936         return $this->oxorder__oxdelcountry;
01937     }
01945     public function reloadDelivery( $blReload )
01946     {
01947         $this->_blReloadDelivery = $blReload;
01948     }
01949 
01957     public function reloadDiscount( $blReload )
01958     {
01959         $this->_blReloadDiscount = $blReload;
01960     }
01961 
01967     public function cancelOrder()
01968     {
01969         $this->oxorder__oxstorno = new oxField( 1 );
01970         if ( $this->save() ) {
01971 
01972 
01973             // canceling ordered products
01974             foreach ( $this->getOrderArticles() as $oOrderArticle ) {
01975 
01976 
01977                 $oOrderArticle->cancelOrderArticle();
01978             }
01979         }
01980     }
01981 
01988     public function getOrderCurrency()
01989     {
01990         if ( $this->_oOrderCurrency === null ) {
01991 
01992             // setting default in case unrecognized currency was set during order
01993             $aCurrencies = $this->getConfig()->getCurrencyArray();
01994             $this->_oOrderCurrency = current( $aCurrencies );
01995 
01996             foreach ( $aCurrencies as $oCurr ) {
01997                 if ( $oCurr->name == $this->oxorder__oxcurrency->value ) {
01998                     $this->_oOrderCurrency = $oCurr;
01999                     break;
02000                 }
02001             }
02002         }
02003         return $this->_oOrderCurrency;
02004     }
02005 
02015     public function validateOrder( $oBasket, $oUser )
02016     {
02017         // validating stock
02018         $iValidState = $this->validateStock( $oBasket );
02019 
02020         if ( !$iValidState ) {
02021             // validating delivery
02022             $iValidState = $this->validateDelivery( $oBasket );
02023         }
02024 
02025         if ( !$iValidState ) {
02026             // validating payment
02027             $iValidState = $this->validatePayment( $oBasket );
02028         }
02029 
02030         if ( !$iValidState ) {
02031             //0003110 validating delivewry address, it is not be changed during checkout process
02032             $iValidState = $this->validateDeliveryAddress( $oUser );
02033         }
02034 
02035         if ( !$iValidState ) {
02036             // validatign minimum price
02037             $iValidState = $this->validateBasket( $oBasket );
02038         }
02039         return $iValidState;
02040     }
02041 
02049     public function validateBasket( $oBasket )
02050     {
02051         return $oBasket->isBelowMinOrderPrice() ? self::ORDER_STATE_BELOWMINPRICE : null;
02052     }
02053 
02062     public function validateDeliveryAddress( $oUser )
02063     {
02064         $sDelAddressMD5 =  oxConfig::getParameter( 'sDeliveryAddressMD5' );
02065 
02066         $sDelAddress = $oUser->getEncodedDeliveryAddress();
02067 
02068         // if delivery address is set, add it to delivery address check
02069         if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
02070             $sDelAddress .= $oDelAdress->getEncodedDeliveryAddress();
02071         }
02072 
02073         if ( $sDelAddressMD5 != $sDelAddress ) {
02074             return self::ORDER_STATE_INVALIDDElADDRESSCHANGED;
02075         }
02076 
02077         return;
02078     }
02079 
02080 
02081 
02090     public function validateDelivery( $oBasket )
02091     {
02092         // proceed with no delivery
02093         // used for other countries
02094         if ( $oBasket->getPaymentId() == 'oxempty') {
02095             return;
02096         }
02097         $oDb = oxDb::getDb();
02098 
02099         $oDelSet = oxNew( "oxdeliveryset" );
02100         $sTable = $oDelSet->getViewName();
02101 
02102         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
02103         $oDb->quote( $oBasket->getShippingId() )." and ".$oDelSet->getSqlActiveSnippet();
02104 
02105         if ( !$oDb->getOne( $sQ, false, false ) ) {
02106             // throwing exception
02107             return self::ORDER_STATE_INVALIDDELIVERY;
02108         }
02109     }
02110 
02119     public function validatePayment( $oBasket )
02120     {
02121         $oDb = oxDb::getDb();
02122 
02123         $oPayment = oxNew( "oxpayment" );
02124         $sTable = $oPayment->getViewName();
02125 
02126         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
02127         $oDb->quote( $oBasket->getPaymentId() )." and ".$oPayment->getSqlActiveSnippet();
02128 
02129         if ( !$oDb->getOne( $sQ, false, false ) ) {
02130             return self::ORDER_STATE_INVALIDPAYMENT;
02131         }
02132     }
02133 
02141     protected function _setTsProtection( oxBasket $oBasket )
02142     {
02143         // protection price
02144         if ( ( $oTsProtectionCost = $oBasket->getCosts( 'oxtsprotection' ) ) ) {
02145             $this->oxorder__oxtsprotectcosts = new oxField($oTsProtectionCost->getBruttoPrice(), oxField::T_RAW);
02146         }
02147 
02148         // protection protduct id
02149         $this->oxorder__oxtsprotectid = new oxField($oBasket->getTsProductId(), oxField::T_RAW);
02150     }
02151 
02160     protected function _executeTsProtection( oxBasket $oBasket )
02161     {
02162         $aValues['tsProductId'] = $this->oxorder__oxtsprotectid->value;
02163         $aValues['amount'] = $oBasket->getPrice()->getBruttoPrice();
02164         $oCur = $this->getConfig()->getActShopCurrencyObject();
02165         $aValues['currency'] = $oCur->name;
02166         $aValues['buyerEmail'] = $this->oxorder__oxbillemail->value;
02167         $aValues['shopCustomerID'] = $this->oxorder__oxuserid->value;
02168         $aValues['shopOrderID'] = $this->oxorder__oxordernr->value;
02169         $aValues['orderDate'] = $this->oxorder__oxorderdate->value;
02170         $sPaymentId = $oBasket->getPaymentId();
02171         $oTsProtection = oxNew('oxtsprotection');
02172         $blRes = $oTsProtection->requestForTsProtection( $aValues, $sPaymentId );
02173         /*if ( !$blRes ) {
02174             $this->delete();
02175             return self::ORDER_STATE_INVALIDTSPROTECTION;
02176         }*/
02177         return true; // everything fine
02178     }
02179 
02185     public function getFormattedTotalNetSum()
02186     {
02187         return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxtotalnetsum->value, $this->getOrderCurrency() );
02188     }
02189 
02195     public function getFormattedTotalBrutSum()
02196     {
02197         return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $this->getOrderCurrency() );
02198     }
02199 
02205     public function getFormattedeliveryCost()
02206     {
02207         return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxdelcost->value, $this->getOrderCurrency() );
02208     }
02209 
02215     public function getFormattedPayCost()
02216     {
02217        return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxpaycost->value, $this->getOrderCurrency() );
02218     }
02219 
02225     public function getFormattedWrapCost()
02226     {
02227         return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxwrapcost->value, $this->getOrderCurrency() );
02228     }
02229 
02235     public function getFormattedGiftCardCost()
02236     {
02237         return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxgiftcardcost->value, $this->getOrderCurrency() );
02238     }
02239 
02245     public function getFormattedTotalVouchers()
02246     {
02247         return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $this->getOrderCurrency() );
02248     }
02249 
02255     public function getFormattedDiscount()
02256     {
02257         return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxdiscount->value, $this->getOrderCurrency() );
02258     }
02259 
02265     public function getFormattedTotalOrderSum()
02266     {
02267         return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxtotalordersum->value, $this->getOrderCurrency() );
02268     }
02269 
02275     public function getShipmentTrackingUrl()
02276     {
02277         if ( $this->_sShipTrackUrl === null && $this->oxorder__oxtrackcode->value ) {
02278             $this->_sShipTrackUrl = "http://www.dpd.de/cgi-bin/delistrack?typ=1&amp;lang=de&amp;pknr=".$this->oxorder__oxtrackcode->value;
02279         }
02280 
02281         return $this->_sShipTrackUrl;
02282     }
02283 
02284 }