oxorder.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxOrder extends oxBase
00008 {
00009     // defining order state constants
00014     const ORDER_STATE_MAILINGERROR = 0;
00015 
00020     const ORDER_STATE_OK = 1;
00021 
00026     const ORDER_STATE_PAYMENTERROR = 2;
00027 
00032     const ORDER_STATE_ORDEREXISTS = 3;
00033 
00038     const ORDER_STATE_INVALIDDELIVERY = 4;
00039 
00044     const ORDER_STATE_INVALIDPAYMENT = 5;
00045 
00050     const ORDER_STATE_INVALIDTSPROTECTION = 6;
00051 
00056     const ORDER_STATE_INVALIDDElADDRESSCHANGED = 7;
00057 
00062     const ORDER_STATE_BELOWMINPRICE = 8;
00063 
00069     protected $_aSkipSaveFields = array( 'oxorderdate' );
00070 
00076     protected $_oArticles = null;
00077 
00083     protected $_oDelSet   = null;
00084 
00090     protected $_oGiftCard = null;
00091 
00097     protected $_oPaymentType = null;
00098 
00104     protected $_oPayment = null;
00105 
00111     protected $_aVoucherList = null;
00112 
00118     protected $_oDelPrice = null;
00119 
00125     protected $_oUser = null;
00126 
00132     protected $_oBasket = null;
00133 
00139     protected $_oWrappingPrice = null;
00140 
00146     protected $_oPaymentPrice = null;
00147 
00153     protected $_oTsProtectionPrice = null;
00154 
00160     protected $_sClassName = 'oxorder';
00161 
00167     protected $_blSeparateNumbering = null;
00168 
00174     protected $_iOrderLang = null;
00175 
00181     protected $_blReloadDelivery = true;
00182 
00188     protected $_blReloadDiscount = true;
00189 
00195     protected $_oOrderCurrency = null;
00196 
00202     protected $_oOrderFiles = null;
00203 
00207     public function __construct()
00208     {
00209         parent::__construct();
00210         $this->init( 'oxorder' );
00211 
00212         // set usage of seperate orders numbering for different shops
00213         $this->setSeparateNumbering( $this->getConfig()->getConfigParam( 'blSeparateNumbering') );
00214 
00215     }
00216 
00224     public function __get( $sName )
00225     {
00226         if ( $sName == 'oDelSet' ) {
00227             return $this->getDelSet();
00228         }
00229 
00230         if ( $sName == 'oxorder__oxbillcountry' ) {
00231             return $this->getBillCountry();
00232         }
00233 
00234         if ( $sName == 'oxorder__oxdelcountry' ) {
00235             return $this->getDelCountry();
00236         }
00237     }
00238 
00246     public function assign( $dbRecord )
00247     {
00248 
00249         parent::assign( $dbRecord );
00250 
00251         $oUtilsDate = oxUtilsDate::getInstance();
00252 
00253         // convert date's to international format
00254         $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value));
00255         $this->oxorder__oxsenddate  = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value));
00256     }
00257 
00265     protected function _getCountryTitle( $sCountryId )
00266     {
00267         $sTitle = null;
00268         if ( $sCountryId && $sCountryId != '-1' ) {
00269             $oCountry = oxNew( 'oxcountry' );
00270             $oCountry->loadInLang( $this->getOrderLanguage(), $sCountryId );
00271             $sTitle = $oCountry->oxcountry__oxtitle->value;
00272         }
00273 
00274         return $sTitle;
00275     }
00276 
00284     protected function _getArticles( $blExcludeCanceled = false )
00285     {
00286         $sSelect = "SELECT `oxorderarticles`.* FROM `oxorderarticles`
00287                         WHERE `oxorderarticles`.`oxorderid` = '".$this->getId() . "'" .
00288                         ( $blExcludeCanceled ? " AND `oxorderarticles`.`oxstorno` != 1 ": " " ) ."
00289                         ORDER BY `oxorderarticles`.`oxartid`";
00290 
00291             // order articles
00292         $oArticles = oxNew( 'oxlist' );
00293         $oArticles->init( 'oxorderarticle' );
00294         $oArticles->selectString( $sSelect );
00295 
00296         return $oArticles;
00297     }
00298 
00306     public function getOrderArticles( $blExcludeCanceled = false )
00307     {
00308         // checking set value
00309         if ( $blExcludeCanceled ) {
00310 
00311             return $this->_getArticles( true );
00312 
00313         } elseif ( $this->_oArticles === null  ) {
00314                 $this->_oArticles = $this->_getArticles();
00315         }
00316 
00317         return $this->_oArticles;
00318     }
00319 
00327     public function setOrderArticleList( $aOrderArticleList )
00328     {
00329         $this->_oArticles = $aOrderArticleList;
00330     }
00331 
00337     public function getOrderDeliveryPrice()
00338     {
00339         if ( $this->_oDelPrice != null ) {
00340             return $this->_oDelPrice;
00341         }
00342 
00343         $this->_oDelPrice = oxNew( 'oxprice' );
00344         $this->_oDelPrice->setBruttoPriceMode();
00345         $this->_oDelPrice->setPrice( $this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value );
00346         return $this->_oDelPrice;
00347     }
00348 
00354     public function getOrderWrappingPrice()
00355     {
00356         if ( $this->_oWrappingPrice != null ) {
00357             return $this->_oWrappingPrice;
00358         }
00359 
00360         $this->_oWrappingPrice = oxNew( 'oxprice' );
00361         $this->_oWrappingPrice->setBruttoPriceMode();
00362         $this->_oWrappingPrice->setPrice( $this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value );
00363         return $this->_oWrappingPrice;
00364     }
00365 
00371     public function getOrderPaymentPrice()
00372     {
00373         if ( $this->_oPaymentPrice != null ) {
00374             return $this->_oPaymentPrice;
00375         }
00376 
00377         $this->_oPaymentPrice = oxNew( 'oxprice' );
00378         $this->_oPaymentPrice->setBruttoPriceMode();
00379         $this->_oPaymentPrice->setPrice( $this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value );
00380         return $this->_oPaymentPrice;
00381     }
00382 
00388     public function getOrderTsProtectionPrice()
00389     {
00390         if ( $this->_oTsProtectionPrice != null ) {
00391             return $this->_oTsProtectionPrice;
00392         }
00393 
00394         $this->_oTsProtectionPrice = oxNew( 'oxprice' );
00395         $this->_oTsProtectionPrice->setBruttoPriceMode();
00396         $this->_oTsProtectionPrice->setPrice( $this->oxorder__oxtsprotectcosts->value, $this->getConfig()->getConfigParam( 'dDefaultVAT' ) );
00397         return $this->_oTsProtectionPrice;
00398     }
00399 
00406     public function getOrderNetSum()
00407     {
00408         $dTotalNetSum = 0;
00409 
00410         $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
00411         $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
00412         $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
00413         $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
00414 
00415         return $dTotalNetSum;
00416     }
00417 
00438     public function finalizeOrder( oxBasket $oBasket, $oUser, $blRecalculatingOrder = false )
00439     {
00440         // check if this order is already stored
00441         $sGetChallenge = oxSession::getVar( 'sess_challenge' );
00442         if ( $this->_checkOrderExist( $sGetChallenge ) ) {
00443             oxUtils::getInstance()->logger( 'BLOCKER' );
00444             // we might use this later, this means that somebody klicked like mad on order button
00445             return self::ORDER_STATE_ORDEREXISTS;
00446         }
00447 
00448         // if not recalculating order, use sess_challenge id, else leave old order id
00449         if ( !$blRecalculatingOrder ) {
00450             // use this ID
00451             $this->setId( $sGetChallenge );
00452 
00453             // validating various order/basket parameters before finalizing
00454             if ( $iOrderState = $this->validateOrder( $oBasket, $oUser ) ) {
00455                 return $iOrderState;
00456             }
00457         }
00458 
00459         // copies user info
00460         $this->_setUser( $oUser );
00461 
00462         // copies basket info
00463         $this->_loadFromBasket( $oBasket );
00464 
00465         // payment information
00466         $oUserPayment = $this->_setPayment( $oBasket->getPaymentId() );
00467 
00468         // set folder information, if order is new
00469         // #M575 in recalcualting order case folder must be the same as it was
00470         if ( !$blRecalculatingOrder ) {
00471             $this->_setFolder();
00472         }
00473 
00474         // marking as not finished
00475         $this->_setOrderStatus( 'NOT_FINISHED' );
00476 
00477         //saving all order data to DB
00478         $this->save();
00479 
00480         // executing payment (on failure deletes order and returns error code)
00481         // in case when recalcualting order, payment execution is skipped
00482         if ( !$blRecalculatingOrder ) {
00483             $blRet = $this->_executePayment( $oBasket, $oUserPayment );
00484             if ( $blRet !== true ) {
00485                 return $blRet;
00486             }
00487         }
00488 
00489         // executing TS protection
00490         if ( !$blRecalculatingOrder && $oBasket->getTsProductId()) {
00491             $blRet = $this->_executeTsProtection( $oBasket );
00492             if ( $blRet !== true ) {
00493                 return $blRet;
00494             }
00495         }
00496 
00497         // deleting remark info only when order is finished
00498         oxSession::deleteVar( 'ordrem' );
00499         oxSession::deleteVar( 'stsprotection' );
00500 
00501         // updating order trans status (success status)
00502         $this->_setOrderStatus( 'OK' );
00503 
00504         // store orderid
00505         $oBasket->setOrderId( $this->getId() );
00506 
00507         // updating wish lists
00508         $this->_updateWishlist( $oBasket->getContents(), $oUser );
00509 
00510         // updating users notice list
00511         $this->_updateNoticeList( $oBasket->getContents(), $oUser );
00512 
00513         // marking vouchers as used and sets them to $this->_aVoucherList (will be used in order email)
00514         // skipping this action in case of order recalculation
00515         if ( !$blRecalculatingOrder ) {
00516             $this->_markVouchers( $oBasket, $oUser );
00517         }
00518 
00519         // send order by email to shop owner and current user
00520         // skipping this action in case of order recalculation
00521         if ( !$blRecalculatingOrder ) {
00522             $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
00523         } else {
00524             $iRet = self::ORDER_STATE_OK;
00525         }
00526 
00527         return $iRet;
00528     }
00529 
00537     protected function _setOrderStatus( $sStatus )
00538     {
00539         $oDb = oxDb::getDb();
00540         $sQ = 'update oxorder set oxtransstatus='.$oDb->quote( $sStatus ).' where oxid='.$oDb->quote( $this->getId() );
00541         $oDb->execute( $sQ );
00542 
00543         //updating order object
00544         $this->oxorder__oxtransstatus = new oxField( $sStatus, oxField::T_RAW );
00545     }
00546 
00554     protected function _convertVat( $sVat )
00555     {
00556         if ( strpos( $sVat, '.' ) < strpos( $sVat, ',' ) ) {
00557             $sVat = str_replace( array( '.', ',' ), array( '', '.' ), $sVat );
00558         } else {
00559             $sVat = str_replace( ',', '', $sVat );
00560         }
00561         return (float) $sVat;
00562     }
00563 
00574     protected function _loadFromBasket( oxBasket $oBasket )
00575     {
00576         $myConfig = $this->getConfig();
00577 
00578         // store IP Adress - default must be FALSE as it is illegal to store
00579         if ( $myConfig->getConfigParam( 'blStoreIPs' ) &&  $this->oxorder__oxip->value === null ) {
00580             $this->oxorder__oxip = new oxField(oxUtilsServer::getInstance()->getRemoteAddress(), oxField::T_RAW);
00581         }
00582 
00583         // copying main price info
00584         $this->oxorder__oxtotalnetsum   = new oxField(oxUtils::getInstance()->fRound($oBasket->getDiscountedNettoPrice()), oxField::T_RAW);
00585         $this->oxorder__oxtotalbrutsum  = new oxField($oBasket->getProductsPrice()->getBruttoSum(), oxField::T_RAW);
00586         $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00587 
00588         // copying discounted VAT info
00589         $iVatIndex = 1;
00590         foreach ( $oBasket->getProductVats(false) as $iVat => $dPrice ) {
00591             $this->{"oxorder__oxartvat$iVatIndex"}      = new oxField( $this->_convertVat( $iVat ), oxField::T_RAW);
00592             $this->{"oxorder__oxartvatprice$iVatIndex"} = new oxField($dPrice, oxField::T_RAW);
00593             $iVatIndex ++;
00594         }
00595 
00596         // payment costs if available
00597         if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00598             $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00599             $this->oxorder__oxpayvat  = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00600         }
00601 
00602         // delivery info
00603         if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00604             $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00605             //V #M382: Save VAT, not VAT value for delivery costs
00606             $this->oxorder__oxdelvat  = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW); //V #M382
00607             $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00608         }
00609 
00610         // user remark
00611         if ( !isset( $this->oxorder__oxremark ) || $this->oxorder__oxremark->value === null ) {
00612             $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00613         }
00614 
00615         // currency
00616         $oCur = $myConfig->getActShopCurrencyObject();
00617         $this->oxorder__oxcurrency = new oxField($oCur->name);
00618         $this->oxorder__oxcurrate  = new oxField($oCur->rate, oxField::T_RAW);
00619 
00620         // store voucherdiscount
00621         if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00622             $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00623         }
00624 
00625         // general discount
00626         if ( $this->_blReloadDiscount ) {
00627             $dDiscount = 0;
00628             $aDiscounts = $oBasket->getDiscounts();
00629             if ( count($aDiscounts) > 0 ) {
00630                 foreach ($aDiscounts as $oDiscount) {
00631                     $dDiscount += $oDiscount->dDiscount;
00632                 }
00633             }
00634             $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00635         }
00636 
00637         //order language
00638         $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00639 
00640 
00641         // initial status - 'ERROR'
00642         $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00643 
00644         // copies basket product info ...
00645         $this->_setOrderArticles( $oBasket->getContents() );
00646 
00647         // copies wrapping info
00648         $this->_setWrapping( $oBasket );
00649 
00650         // copies TS protection info
00651         $this->_setTsProtection( $oBasket );
00652     }
00653 
00660     public function getOrderLanguage()
00661     {
00662         if ( $this->_iOrderLang === null ) {
00663             if ( isset( $this->oxorder__oxlang->value ) ) {
00664                 $this->_iOrderLang = oxLang::getInstance()->validateLanguage( $this->oxorder__oxlang->value );
00665             } else {
00666                 $this->_iOrderLang = oxLang::getInstance()->getBaseLanguage();
00667             }
00668         }
00669         return $this->_iOrderLang;
00670     }
00671 
00679     protected function _setUser( $oUser )
00680     {
00681 
00682         $this->oxorder__oxuserid        = new oxField($oUser->getId());
00683 
00684         // bill address
00685         $this->oxorder__oxbillcompany     = clone $oUser->oxuser__oxcompany;
00686         $this->oxorder__oxbillemail       = clone $oUser->oxuser__oxusername;
00687         $this->oxorder__oxbillfname       = clone $oUser->oxuser__oxfname;
00688         $this->oxorder__oxbilllname       = clone $oUser->oxuser__oxlname;
00689         $this->oxorder__oxbillstreet      = clone $oUser->oxuser__oxstreet;
00690         $this->oxorder__oxbillstreetnr    = clone $oUser->oxuser__oxstreetnr;
00691         $this->oxorder__oxbilladdinfo     = clone $oUser->oxuser__oxaddinfo;
00692         $this->oxorder__oxbillustid       = clone $oUser->oxuser__oxustid;
00693         $this->oxorder__oxbillcity        = clone $oUser->oxuser__oxcity;
00694         $this->oxorder__oxbillcountryid   = clone $oUser->oxuser__oxcountryid;
00695         $this->oxorder__oxbillstateid     = clone $oUser->oxuser__oxstateid;
00696         $this->oxorder__oxbillzip         = clone $oUser->oxuser__oxzip;
00697         $this->oxorder__oxbillfon         = clone $oUser->oxuser__oxfon;
00698         $this->oxorder__oxbillfax         = clone $oUser->oxuser__oxfax;
00699         $this->oxorder__oxbillsal         = clone $oUser->oxuser__oxsal;
00700 
00701 
00702         // delivery address
00703         if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00704             // set delivery address
00705             $this->oxorder__oxdelcompany   = clone $oDelAdress->oxaddress__oxcompany;
00706             $this->oxorder__oxdelfname     = clone $oDelAdress->oxaddress__oxfname;
00707             $this->oxorder__oxdellname     = clone $oDelAdress->oxaddress__oxlname;
00708             $this->oxorder__oxdelstreet    = clone $oDelAdress->oxaddress__oxstreet;
00709             $this->oxorder__oxdelstreetnr  = clone $oDelAdress->oxaddress__oxstreetnr;
00710             $this->oxorder__oxdeladdinfo   = clone $oDelAdress->oxaddress__oxaddinfo;
00711             $this->oxorder__oxdelcity      = clone $oDelAdress->oxaddress__oxcity;
00712             $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00713             $this->oxorder__oxdelstateid   = clone $oDelAdress->oxaddress__oxstateid;
00714             $this->oxorder__oxdelzip       = clone $oDelAdress->oxaddress__oxzip;
00715             $this->oxorder__oxdelfon       = clone $oDelAdress->oxaddress__oxfon;
00716             $this->oxorder__oxdelfax       = clone $oDelAdress->oxaddress__oxfax;
00717             $this->oxorder__oxdelsal       = clone $oDelAdress->oxaddress__oxsal;
00718         }
00719     }
00720 
00728     protected function _setWrapping( oxBasket $oBasket )
00729     {
00730         $myConfig = $this->getConfig();
00731 
00732         // wrapping price
00733         if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00734             $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00735 
00736             // wrapping VAT will be always calculated (#3757)
00737             $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00738         }
00739 
00740         // greetings card
00741         $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00742 
00743         // card text will be stored in database
00744         $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00745     }
00746 
00755     protected function _setOrderArticles( $aArticleList )
00756     {
00757         // reset articles list
00758         $this->_oArticles = oxNew( 'oxlist' );
00759         $iCurrLang = $this->getOrderLanguage();
00760 
00761         // add all the products we have on basket to the order
00762         foreach ( $aArticleList as $oContent ) {
00763 
00764             //$oContent->oProduct = $oContent->getArticle();
00765             // #M773 Do not use article lazy loading on order save
00766             $oProduct = $oContent->getArticle( true, null, true);
00767 
00768             // copy only if object is oxarticle type
00769             if ( $oProduct->isOrderArticle() ) {
00770                 $oOrderArticle = $oProduct;
00771             } else {
00772 
00773                 // if order language doe not match product language - article must be reloaded in order language
00774                 if ( $iCurrLang != $oProduct->getLanguage() ) {
00775                     $oProduct->loadInLang( $iCurrLang, $oProduct->getProductId() );
00776                 }
00777 
00778                 // set chosen selectlist
00779                 $sSelList = '';
00780                 if ( count( $aChosenSelList = $oContent->getChosenSelList() ) ) {
00781                     foreach ( $aChosenSelList as $oItem ) {
00782                         if ( $sSelList ) {
00783                             $sSelList .= ", ";
00784                         }
00785                         $sSelList .= "{$oItem->name} : {$oItem->value}";
00786                     }
00787                 }
00788 
00789                 $oOrderArticle = oxNew( 'oxorderarticle' );
00790                 $oOrderArticle->setIsNewOrderItem( true );
00791                 $oOrderArticle->copyThis( $oProduct );
00792                 $oOrderArticle->setId();
00793 
00794                 $oOrderArticle->oxorderarticles__oxartnum     = clone $oProduct->oxarticles__oxartnum;
00795                 $oOrderArticle->oxorderarticles__oxselvariant = new oxField( trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->getRawValue() ), oxField::T_RAW );
00796                 $oOrderArticle->oxorderarticles__oxshortdesc  = new oxField( $oProduct->oxarticles__oxshortdesc->getRawValue(), oxField::T_RAW );
00797                 // #M974: duplicated entries for the name of variants in orders
00798                 $oOrderArticle->oxorderarticles__oxtitle      = new oxField( trim( $oProduct->oxarticles__oxtitle->getRawValue() ), oxField::T_RAW );
00799 
00800                 // copying persistent parameters ...
00801                 if ( !is_array( $aPersParams = $oProduct->getPersParams() ) ) {
00802                     $aPersParams = $oContent->getPersParams();
00803                 }
00804                 if ( is_array( $aPersParams ) && count( $aPersParams )) {
00805                     $oOrderArticle->oxorderarticles__oxpersparam = new oxField( serialize( $aPersParams ), oxField::T_RAW );
00806                 }
00807             }
00808 
00809             // ids, titles, numbers ...
00810             $oOrderArticle->oxorderarticles__oxorderid = new oxField( $this->getId() );
00811             $oOrderArticle->oxorderarticles__oxartid   = new oxField( $oContent->getProductId() );
00812             $oOrderArticle->oxorderarticles__oxamount  = new oxField( $oContent->getAmount() );
00813 
00814             // prices
00815             $oPrice = $oContent->getPrice();
00816             $oOrderArticle->oxorderarticles__oxnetprice  = new oxField( $oPrice->getNettoPrice(), oxField::T_RAW );
00817             $oOrderArticle->oxorderarticles__oxvatprice  = new oxField( $oPrice->getVatValue(), oxField::T_RAW );
00818             $oOrderArticle->oxorderarticles__oxbrutprice = new oxField( $oPrice->getBruttoPrice(), oxField::T_RAW );
00819             $oOrderArticle->oxorderarticles__oxvat       = new oxField( $oPrice->getVat(), oxField::T_RAW );
00820 
00821             $oUnitPtice = $oContent->getUnitPrice();
00822             $oOrderArticle->oxorderarticles__oxnprice = new oxField( $oUnitPtice->getNettoPrice(), oxField::T_RAW );
00823             $oOrderArticle->oxorderarticles__oxbprice = new oxField( $oUnitPtice->getBruttoPrice(), oxField::T_RAW );
00824 
00825             // wrap id
00826             $oOrderArticle->oxorderarticles__oxwrapid = new oxField( $oContent->getWrappingId(), oxField::T_RAW );
00827 
00828             // items shop id
00829             $oOrderArticle->oxorderarticles__oxordershopid = new oxField( $oContent->getShopId(), oxField::T_RAW );
00830 
00831             // bundle?
00832             $oOrderArticle->oxorderarticles__oxisbundle = new oxField( $oContent->isBundle() );
00833 
00834             // add information for eMail
00835             //P
00836             //TODO: check if this assign is needed at all
00837             $oOrderArticle->oProduct = $oProduct;
00838 
00839             $oOrderArticle->setArticle( $oProduct );
00840 
00841             // simulatin order article list
00842             $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
00843         }
00844     }
00845 
00857     protected function _executePayment( oxBasket $oBasket, $oUserpayment )
00858     {
00859         $oPayTransaction = $this->_getGateway();
00860         $oPayTransaction->setPaymentParams( $oUserpayment );
00861 
00862         if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
00863             $this->delete();
00864 
00865             // checking for error messages
00866             if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
00867                 if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
00868                     return $sLastError;
00869                 }
00870             }
00871 
00872             // checking for error codes
00873             if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
00874                 if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
00875                     return $iLastErrorNo;
00876                 }
00877             }
00878 
00879             return self::ORDER_STATE_PAYMENTERROR; // means no authentication
00880         }
00881         return true; // everything fine
00882     }
00883 
00890     protected function _getGateway()
00891     {
00892         return oxNew( 'oxPaymentGateway' );
00893     }
00894 
00902     protected function _setPayment( $sPaymentid )
00903     {
00904         // copying payment info fields
00905         $aDynvalue = oxSession::getVar( 'dynvalue' );
00906         $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
00907 
00908         // loading payment object
00909         $oPayment = oxNew( 'oxpayment' );
00910 
00911         if (!$oPayment->load( $sPaymentid )) {
00912             return null;
00913         }
00914 
00915         // #756M Preserve already stored payment information
00916         if ( !$aDynvalue && ( $oUserpayment = $this->getPaymentType() ) ) {
00917             if ( is_array( $aStoredDynvalue = $oUserpayment->getDynValues() ) ) {
00918                 foreach ( $aStoredDynvalue as $oVal ) {
00919                     $aDynvalue[$oVal->name] = $oVal->value;
00920                 }
00921             }
00922         }
00923 
00924         $oPayment->setDynValues( oxUtils::getInstance()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
00925 
00926         // collecting dynamic values
00927         $aDynVal = array();
00928 
00929         if ( is_array( $aPaymentDynValues = $oPayment->getDynValues() ) ) {
00930             foreach ( $aPaymentDynValues  as $key => $oVal ) {
00931                 if ( isset( $aDynvalue[$oVal->name] ) ) {
00932                     $oVal->value = $aDynvalue[$oVal->name];
00933                 }
00934 
00935                 //$oPayment->setDynValue($key, $oVal);
00936                 $aPaymentDynValues[$key] = $oVal;
00937                 $aDynVal[$oVal->name] = $oVal->value;
00938             }
00939         }
00940 
00941         // Store this payment information, we might allow users later to
00942         // reactivate already give payment informations
00943 
00944         $oUserpayment = oxNew( 'oxuserpayment' );
00945         $oUserpayment->oxuserpayments__oxuserid     = clone $this->oxorder__oxuserid;
00946         $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
00947         $oUserpayment->oxuserpayments__oxvalue      = new oxField(oxUtils::getInstance()->assignValuesToText( $aDynVal ), oxField::T_RAW);
00948         $oUserpayment->oxpayments__oxdesc           = clone $oPayment->oxpayments__oxdesc;
00949         $oUserpayment->oxpayments__oxlongdesc       = clone $oPayment->oxpayments__oxlongdesc;
00950         $oUserpayment->setDynValues( $aPaymentDynValues );
00951         $oUserpayment->save();
00952 
00953         // storing payment information to order
00954         $this->oxorder__oxpaymentid   = new oxField($oUserpayment->getId(), oxField::T_RAW);
00955         $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
00956 
00957         // returning user payment object which will be used later in code ...
00958         return $oUserpayment;
00959     }
00960 
00966     protected function _setFolder()
00967     {
00968         $myConfig = $this->getConfig();
00969         $this->oxorder__oxfolder    = new oxField(key( $myConfig->getShopConfVar(  'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
00970     }
00971 
00981     protected function _updateWishlist( $aArticleList, $oUser )
00982     {
00983 
00984         foreach ( $aArticleList as $oContent) {
00985             if ( ( $sWishId = $oContent->getWishId() ) ) {
00986 
00987                 // checking which wishlist user uses ..
00988                 if ( $sWishId == $oUser->getId() ) {
00989                     $oUserBasket = $oUser->getBasket( 'wishlist' );
00990                 } else {
00991                     $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
00992                     $oUserBasket = oxNew( 'oxuserbasket' );
00993                     $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
00994                 }
00995 
00996                 // updating users wish list
00997                 if ( $oUserBasket ) {
00998                     if ( !($sProdId = $oContent->getWishArticleId() )) {
00999                         $sProdId = $oContent->getProductId();
01000                     }
01001                     $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
01002                     $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
01003                     if ( $dNewAmount < 0) {
01004                         $dNewAmount = 0;
01005                     }
01006                     $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
01007                 }
01008             }
01009         }
01010     }
01011 
01021     protected function _updateNoticeList( $aArticleList, $oUser )
01022     {
01023         // loading users notice list ..
01024         if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
01025             // only if wishlist is enabled
01026             foreach ( $aArticleList as $oContent) {
01027                 $sProdId = $oContent->getProductId();
01028 
01029                 // updating users notice list
01030                 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList(), $oContent->getPersParams() );
01031                 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
01032                 if ( $dNewAmount < 0) {
01033                     $dNewAmount = 0;
01034                 }
01035                 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true, $oContent->getPersParams() );
01036             }
01037         }
01038     }
01039 
01049     protected function _markVouchers( $oBasket, $oUser )
01050     {
01051         $this->_aVoucherList = $oBasket->getVouchers();
01052 
01053         if ( is_array( $this->_aVoucherList ) ) {
01054             foreach ( $this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
01055                 $oVoucher = oxNew( 'oxvoucher' );
01056                 $oVoucher->load( $sVoucherId );
01057                 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->dVoucherdiscount );
01058 
01059                 $this->_aVoucherList[$sVoucherId] = $oVoucher;
01060             }
01061         }
01062     }
01063 
01069     public function save()
01070     {
01071         if ( ( $blSave = parent::save() ) ) {
01072 
01073             // saving order articles
01074             $oOrderArticles = $this->getOrderArticles();
01075             if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
01076                 foreach ( $oOrderArticles as $oOrderArticle ) {
01077                     $oOrderArticle->save();
01078                 }
01079             }
01080         }
01081 
01082         return $blSave;
01083     }
01084 
01091     public function getDelAddressInfo()
01092     {
01093         $oDelAdress = null;
01094         if (! ($soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
01095             $soxAddressId = oxSession::getVar( 'deladrid' );
01096         }
01097         if ( $soxAddressId ) {
01098             $oDelAdress = oxNew( 'oxaddress' );
01099             $oDelAdress->load( $soxAddressId );
01100 
01101             //get delivery country name from delivery country id
01102             if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
01103                  $oCountry = oxNew( 'oxcountry' );
01104                  $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
01105                  $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
01106             }
01107         }
01108         return $oDelAdress;
01109     }
01110 
01121     public function validateStock( $oBasket )
01122     {
01123         foreach ( $oBasket->getContents() as $key => $oContent ) {
01124             try {
01125                 $oProd = $oContent->getArticle();
01126             } catch ( oxNoArticleException $oEx ) {
01127                 $oBasket->removeItem( $key );
01128                 throw $oEx;
01129             } catch ( oxArticleInputException $oEx ) {
01130                 $oBasket->removeItem( $key );
01131                 throw $oEx;
01132             }
01133 
01134             // check if its still available
01135             $dArtStockAmount = $oBasket->getArtStockInBasket( $oProd->getId(), $key );
01136             $iOnStock = $oProd->checkForStock( $oContent->getAmount(), $dArtStockAmount );
01137             if ( $iOnStock !== true ) {
01138                 $oEx = oxNew( 'oxOutOfStockException' );
01139                 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
01140                 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
01141                 $oEx->setProductId( $oProd->getId() );
01142 
01143                 if (!is_numeric($iOnStock)) {
01144                     $iOnStock = 0;
01145                 }
01146                 $oEx->setRemainingAmount( $iOnStock );
01147                 throw $oEx;
01148             }
01149         }
01150     }
01151 
01157     protected function _insert()
01158     {
01159         $myConfig = $this->getConfig();
01160         $oUtilsDate = oxUtilsDate::getInstance();
01161 
01162         //V #M525 orderdate must be the same as it was
01163         if ( !$this->oxorder__oxorderdate->value ) {
01164             $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
01165         } else {
01166             $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
01167         }
01168 
01169         $this->oxorder__oxshopid    = new oxField($myConfig->getShopId(), oxField::T_RAW);
01170         $this->oxorder__oxsenddate  = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01171 
01172         if ( ( $blInsert = parent::_insert() ) ) {
01173             // setting order number
01174             if ( !$this->oxorder__oxordernr->value ) {
01175                $blInsert = $this->_setNumber();
01176             } else {
01177                 oxNew( 'oxCounter' )->update( $this->_getCounterIdent(), $this->oxorder__oxordernr->value );
01178             }
01179         }
01180 
01181         return $blInsert;
01182     }
01183 
01189     protected function _getCounterIdent()
01190     {
01191         $sCounterIdent = ( $this->_blSeparateNumbering ) ? 'oxOrder_' . $this->getConfig()->getShopId() : 'oxOrder';
01192         return $sCounterIdent;
01193     }
01194 
01195 
01201     protected function _setNumber()
01202     {
01203         $oDb = oxDb::getDb();
01204 
01205         $iCnt = oxNew( 'oxCounter' )->getNext( $this->_getCounterIdent() );
01206         $sQ = "update oxorder set oxordernr = $iCnt where oxid = ?";
01207         $blUpdate = ( bool ) $oDb->execute( $sQ, array( $this->getId() ) );
01208 
01209         if ( $blUpdate ) {
01210             $this->oxorder__oxordernr = new oxField( $iCnt );
01211         }
01212 
01213         return $blUpdate;
01214     }
01215 
01221     protected function _update()
01222     {
01223         $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01224         return parent::_update();
01225     }
01226 
01235     public function delete( $sOxId = null )
01236     {
01237         if ( $sOxId ) {
01238             if ( !$this->load( $sOxId ) ) {
01239                 // such order does not exist
01240                 return false;
01241             }
01242         } elseif ( !$sOxId ) {
01243             $sOxId = $this->getId();
01244         }
01245 
01246         // no order id is passed
01247         if ( !$sOxId ) {
01248             return false;
01249         }
01250 
01251 
01252         // delete order articles
01253         $oOrderArticles = $this->getOrderArticles( false );
01254         foreach ( $oOrderArticles as $oOrderArticle ) {
01255             $oOrderArticle->delete();
01256         }
01257 
01258         // #440 - deleting user payment info
01259         if ( $oPaymentType = $this->getPaymentType() ) {
01260             $oPaymentType->delete();
01261         }
01262 
01263         return parent::delete( $sOxId );
01264     }
01265 
01275     public function recalculateOrder( $aNewArticles = array() )
01276     {
01277         oxDb::startTransaction();
01278 
01279         try {
01280             $oBasket = $this->_getOrderBasket();
01281 
01282             // add this order articles to virtual basket and recalculates basket
01283             $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles( true ) );
01284 
01285             // adding new articles to existing order
01286             $this->_addArticlesToBasket( $oBasket, $aNewArticles );
01287 
01288             // recalculating basket
01289             $oBasket->calculateBasket( true );
01290 
01291             //finalizing order (skipping payment execution, vouchers marking and mail sending)
01292             $iRet = $this->finalizeOrder( $oBasket, $this->getOrderUser(), true );
01293 
01294             //if finalizing order failed, rollback transaction
01295             if ( $iRet !== 1 ) {
01296                 oxDb::rollbackTransaction();
01297             } else {
01298                 oxDb::commitTransaction();
01299             }
01300 
01301         } catch( Exception $oE ) {
01302             // if exception, rollBack everything
01303             oxDb::rollbackTransaction();
01304 
01305             if ( defined( 'OXID_PHP_UNIT' ) ) {
01306                 throw $oE;
01307             }
01308         }
01309     }
01310 
01311     protected $_oOrderBasket = null;
01319     protected function _getOrderBasket( $blStockCheck = true )
01320     {
01321         $this->_oOrderBasket = oxNew( "oxbasket" );
01322 
01323         // setting stock check mode
01324         $this->_oOrderBasket->setStockCheckMode( $blStockCheck );
01325 
01326         // setting virtual basket user
01327         $this->_oOrderBasket->setBasketUser( $this->getOrderUser() );
01328 
01329         // transferring order id
01330         $this->_oOrderBasket->setOrderId( $this->getId() );
01331 
01332         // setting basket currency order uses
01333         $aCurrencies = $this->getConfig()->getCurrencyArray();
01334         foreach ( $aCurrencies as $oCur ) {
01335             if ($oCur->name == $this->oxorder__oxcurrency->value) {
01336                 $oBasketCur = $oCur;
01337                 break;
01338             }
01339         }
01340 
01341         // setting currency
01342         $this->_oOrderBasket->setBasketCurrency( $oBasketCur );
01343 
01344         // set basket card id and message
01345         $this->_oOrderBasket->setCardId( $this->oxorder__oxcardid->value );
01346         $this->_oOrderBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01347 
01348         if ( $this->_blReloadDiscount ) {
01349             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01350             // disabling availability check
01351             $this->_oOrderBasket->setSkipVouchersChecking( true );
01352 
01353             // add previously used vouchers
01354             $sQ = 'select oxid from oxvouchers where oxorderid = '.$oDb->quote( $this->getId() );
01355             $aVouchers = $oDb->getAll( $sQ );
01356             foreach ( $aVouchers as $aVoucher ) {
01357                 $this->_oOrderBasket->addVoucher( $aVoucher['oxid'] );
01358             }
01359         } else {
01360             $this->_oOrderBasket->setDiscountCalcMode( false );
01361             $this->_oOrderBasket->setVoucherDiscount( $this->oxorder__oxvoucherdiscount->value );
01362             $this->_oOrderBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
01363         }
01364 
01365         // must be kept old delivery?
01366         if ( !$this->_blReloadDelivery ) {
01367             $this->_oOrderBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01368         } else {
01369             //  set shipping
01370             $this->_oOrderBasket->setShipping( $this->oxorder__oxdeltype->value );
01371             $this->_oOrderBasket->setDeliveryPrice( null );
01372         }
01373 
01374         //set basket payment
01375         $this->_oOrderBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01376 
01377         return $this->_oOrderBasket;
01378     }
01379 
01388     public function setDelivery( $sDeliveryId )
01389     {
01390         $this->reloadDelivery( true );
01391         $this->oxorder__oxdeltype = new oxField( $sDeliveryId );
01392     }
01393 
01399     public function getOrderUser()
01400     {
01401         if ($this->_oUser === null ) {
01402             $this->_oUser = oxNew( "oxuser" );
01403             $this->_oUser->load( $this->oxorder__oxuserid->value );
01404 
01405             // if object is loaded then reusing its order info
01406             if ( $this->_isLoaded ) {
01407                 // bill address
01408                 $this->_oUser->oxuser__oxcompany  = clone $this->oxorder__oxbillcompany;
01409                 $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
01410                 $this->_oUser->oxuser__oxfname    = clone $this->oxorder__oxbillfname;
01411                 $this->_oUser->oxuser__oxlname    = clone $this->oxorder__oxbilllname;
01412                 $this->_oUser->oxuser__oxstreet   = clone $this->oxorder__oxbillstreet;
01413                 $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
01414                 $this->_oUser->oxuser__oxaddinfo  = clone $this->oxorder__oxbilladdinfo;
01415                 $this->_oUser->oxuser__oxustid    = clone $this->oxorder__oxbillustid;
01416 
01417 
01418                 $this->_oUser->oxuser__oxcity      = clone $this->oxorder__oxbillcity;
01419                 $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
01420                 $this->_oUser->oxuser__oxstateid   = clone $this->oxorder__oxbillstateid;
01421                 $this->_oUser->oxuser__oxzip       = clone $this->oxorder__oxbillzip;
01422                 $this->_oUser->oxuser__oxfon       = clone $this->oxorder__oxbillfon;
01423                 $this->_oUser->oxuser__oxfax       = clone $this->oxorder__oxbillfax;
01424                 $this->_oUser->oxuser__oxsal       = clone $this->oxorder__oxbillsal;
01425             }
01426         }
01427 
01428         return $this->_oUser;
01429     }
01430 
01438     public function pdfFooter( $oPdf )
01439     {
01440     }
01441 
01449     public function pdfHeaderplus( $oPdf )
01450     {
01451     }
01452 
01460     public function pdfHeader( $oPdf )
01461     {
01462     }
01463 
01472     public function genPdf( $sFilename, $iSelLang = 0 )
01473     {
01474     }
01475 
01481     public function getInvoiceNum()
01482     {
01483         $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01484         return ( ( int ) oxDb::getDb()->getOne( $sQ, false ) + 1 );
01485     }
01486 
01492     public function getNextBillNum()
01493     {
01494         $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01495         return ( ( int ) oxDb::getDb()->getOne( $sQ, false ) + 1 );
01496     }
01497 
01503     public function getShippingSetList()
01504     {
01505         // in which country we deliver
01506         if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
01507             $sShipId = $this->oxorder__oxbillcountryid->value;
01508         }
01509 
01510         $oBasket = $this->_getOrderBasket( false );
01511 
01512         // unsetting bundles
01513         $oOrderArticles = $this->getOrderArticles();
01514         foreach ( $oOrderArticles as $sItemId => $oItem ) {
01515             if ( $oItem->isBundle() ) {
01516                 $oOrderArticles->offsetUnset( $sItemId );
01517             }
01518         }
01519 
01520         // add this order articles to basket and recalculate basket
01521         $this->_addOrderArticlesToBasket( $oBasket, $oOrderArticles );
01522 
01523         // recalculating basket
01524         $oBasket->calculateBasket( true );
01525 
01526         // load fitting deliveries list
01527         $oDeliveryList = oxNew( "oxDeliveryList", "core" );
01528         $oDeliveryList->setCollectFittingDeliveriesSets( true );
01529 
01530         return $oDeliveryList->getDeliveryList( $oBasket, $this->getOrderUser(), $sShipId );
01531     }
01532 
01538     public function getVoucherNrList()
01539     {
01540         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01541         $aVouchers = array();
01542         $sSelect = "select oxvouchernr from oxvouchers where oxorderid = ".$oDb->quote( $this->oxorder__oxid->value );
01543         $rs = $oDb->select( $sSelect );
01544         if ($rs != false && $rs->recordCount() > 0) {
01545             while (!$rs->EOF) {
01546                 $aVouchers[] = $rs->fields['oxvouchernr'];
01547                 $rs->moveNext();
01548             }
01549         }
01550         return $aVouchers;
01551     }
01552 
01560     public function getOrderSum( $blToday = false )
01561     {
01562         $sSelect  = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01563         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01564 
01565         if ( $blToday ) {
01566             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01567         }
01568 
01569         return ( double ) oxDb::getDb()->getOne( $sSelect, false, false );
01570     }
01571 
01579     public function getOrderCnt( $blToday = false )
01580     {
01581         $sSelect  = 'select count(*) from oxorder where ';
01582         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'"  and oxorder.oxstorno != "1" ';
01583 
01584         if ( $blToday ) {
01585             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01586         }
01587 
01588         return ( int ) oxDb::getDb()->getOne( $sSelect, false, false );
01589     }
01590 
01591 
01599     protected function _checkOrderExist( $sOxId = null )
01600     {
01601         if ( !$sOxId) {
01602             return false;
01603         }
01604 
01605         $oDb = oxDb::getDb();
01606         if ( $oDb->getOne( 'select oxid from oxorder where oxid = '.$oDb->quote( $sOxId ), false, false ) ) {
01607             return true;
01608         }
01609 
01610         return false;
01611     }
01612 
01622     protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01623     {
01624         $iRet = self::ORDER_STATE_MAILINGERROR;
01625 
01626         // add user, basket and payment to order
01627         $this->_oUser    = $oUser;
01628         $this->_oBasket  = $oBasket;
01629         $this->_oPayment = $oPayment;
01630 
01631         $oxEmail = oxNew( 'oxemail' );
01632 
01633         // send order email to user
01634         if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01635             // mail to user was successfully sent
01636             $iRet = self::ORDER_STATE_OK;
01637         }
01638 
01639         // send order email to shop owner
01640         $oxEmail->sendOrderEMailToOwner( $this );
01641 
01642         return $iRet;
01643     }
01644 
01650     public function getBasket()
01651     {
01652         return $this->_oBasket;
01653     }
01654 
01660     public function getPayment()
01661     {
01662         return $this->_oPayment;
01663     }
01664 
01670     public function getVoucherList()
01671     {
01672         return $this->_aVoucherList;
01673     }
01674 
01680     public function getDelSet()
01681     {
01682         if ( $this->_oDelSet == null ) {
01683             // load deliveryset info
01684             $this->_oDelSet = oxNew( 'oxdeliveryset' );
01685             $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01686         }
01687 
01688         return $this->_oDelSet;
01689     }
01690 
01696     public function getPaymentType()
01697     {
01698         if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType === null ) {
01699             $this->_oPaymentType = false;
01700             $oPaymentType = oxNew( 'oxuserpayment' );
01701             if ( $oPaymentType->load( $this->oxorder__oxpaymentid->value ) ) {
01702                 $this->_oPaymentType = $oPaymentType;
01703             }
01704         }
01705 
01706         return $this->_oPaymentType;
01707     }
01708 
01714     public function getGiftCard()
01715     {
01716         if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01717             $this->_oGiftCard = oxNew( 'oxwrapping' );
01718             $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01719         }
01720 
01721         return $this->_oGiftCard;
01722     }
01723 
01731     public function setSeparateNumbering( $blSeparateNumbering = null )
01732     {
01733         $this->_blSeparateNumbering = $blSeparateNumbering;
01734     }
01735 
01743     public function getLastUserPaymentType( $sUserId)
01744     {
01745         $oDb = oxDb::getDb();
01746         $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid='.$oDb->quote( $sUserId ).' order by oxorder.oxorderdate desc ';
01747         $sLastPaymentId = $oDb->getOne( $sQ, false, false );
01748         return $sLastPaymentId;
01749     }
01750 
01759     protected function _addOrderArticlesToBasket( $oBasket, $aOrderArticles )
01760     {
01761         // if no order articles, return empty basket
01762         if ( count( $aOrderArticles ) > 0 ) {
01763 
01764             //adding order articles to basket
01765             foreach ( $aOrderArticles as $oOrderArticle ) {
01766                 $oBasket->addOrderArticleToBasket( $oOrderArticle );
01767             }
01768         }
01769     }
01770 
01779     protected function _addArticlesToBasket( $oBasket, $aArticles )
01780     {
01781         // if no order articles
01782         if ( count($aArticles ) > 0 ) {
01783 
01784             //adding order articles to basket
01785             foreach ( $aArticles as $oArticle ) {
01786                 $aSel = isset( $oArticle->oxorderarticles__oxselvariant ) ? $oArticle->oxorderarticles__oxselvariant->value : null;
01787                 $aPersParam = isset( $oArticle->oxorderarticles__oxpersparam ) ? $oArticle->getPersParams() : null;
01788                 $oBasket->addToBasket( $oArticle->oxorderarticles__oxartid->value,
01789                                        $oArticle->oxorderarticles__oxamount->value,
01790                                        $aSel, $aPersParam );
01791             }
01792         }
01793     }
01794 
01800     public function getTotalOrderSum()
01801     {
01802         $oCur = $this->getConfig()->getActShopCurrencyObject();
01803         return number_format( (double)$this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01804     }
01805 
01813     public function getProductVats( $blFormatCurrency = true )
01814     {
01815         $aVats = array();
01816         if ($this->oxorder__oxartvat1->value) {
01817             $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
01818         }
01819         if ($this->oxorder__oxartvat2->value) {
01820             $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
01821         }
01822 
01823         if ( $blFormatCurrency ) {
01824             $oLang = oxLang::getInstance();
01825             $oCur = $this->getConfig()->getActShopCurrencyObject();
01826             foreach ( $aVats as $sKey => $dVat ) {
01827                 $aVats[$sKey] = $oLang->formatCurrency( $dVat, $oCur );
01828             }
01829         }
01830         return $aVats;
01831     }
01832 
01838     public function getBillCountry()
01839     {
01840         if ( !$this->oxorder__oxbillcountry->value ) {
01841             $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
01842         }
01843         return $this->oxorder__oxbillcountry;
01844     }
01845 
01851     public function getDelCountry()
01852     {
01853         if ( !$this->oxorder__oxdelcountry->value ) {
01854             $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
01855         }
01856         return $this->oxorder__oxdelcountry;
01857     }
01865     public function reloadDelivery( $blReload )
01866     {
01867         $this->_blReloadDelivery = $blReload;
01868     }
01869 
01877     public function reloadDiscount( $blReload )
01878     {
01879         $this->_blReloadDiscount = $blReload;
01880     }
01881 
01887     public function cancelOrder()
01888     {
01889         $this->oxorder__oxstorno = new oxField( 1 );
01890         if ( $this->save() ) {
01891 
01892 
01893             // canceling ordered products
01894             foreach ( $this->getOrderArticles() as $oOrderArticle ) {
01895 
01896 
01897                 $oOrderArticle->cancelOrderArticle();
01898             }
01899         }
01900     }
01901 
01908     public function getOrderCurrency()
01909     {
01910         if ( $this->_oOrderCurrency === null ) {
01911 
01912             // setting default in case unrecognized currency was set during order
01913             $aCurrencies = $this->getConfig()->getCurrencyArray();
01914             $this->_oOrderCurrency = current( $aCurrencies );
01915 
01916             foreach ( $aCurrencies as $oCurr ) {
01917                 if ( $oCurr->name == $this->oxorder__oxcurrency->value ) {
01918                     $this->_oOrderCurrency = $oCurr;
01919                     break;
01920                 }
01921             }
01922         }
01923         return $this->_oOrderCurrency;
01924     }
01925 
01935     public function validateOrder( $oBasket, $oUser )
01936     {
01937         // validating stock
01938         $iValidState = $this->validateStock( $oBasket );
01939 
01940         if ( !$iValidState ) {
01941             // validating delivery
01942             $iValidState = $this->validateDelivery( $oBasket );
01943         }
01944 
01945         if ( !$iValidState ) {
01946             // validating payment
01947             $iValidState = $this->validatePayment( $oBasket );
01948         }
01949 
01950         if ( !$iValidState ) {
01951             //0003110 validating delivewry address, it is not be changed during checkout process
01952             $iValidState = $this->validateDeliveryAddress( $oUser );
01953         }
01954 
01955         if ( !$iValidState ) {
01956             // validatign minimum price
01957             $iValidState = $this->validateBasket( $oBasket );
01958         }
01959         return $iValidState;
01960     }
01961 
01969     public function validateBasket( $oBasket )
01970     {
01971         return $oBasket->isBelowMinOrderPrice() ? self::ORDER_STATE_BELOWMINPRICE : null;
01972     }
01973 
01982     public function validateDeliveryAddress( $oUser )
01983     {
01984         $sDelAddressMD5 =  oxConfig::getParameter( 'sDeliveryAddressMD5' );
01985 
01986         // bill address
01987         $sDelAddress = '';
01988         $sDelAddress .= $oUser->oxuser__oxcompany;
01989         $sDelAddress .= $oUser->oxuser__oxusername;
01990         $sDelAddress .= $oUser->oxuser__oxfname;
01991         $sDelAddress .= $oUser->oxuser__oxlname;
01992         $sDelAddress .= $oUser->oxuser__oxstreet;
01993         $sDelAddress .= $oUser->oxuser__oxstreetnr;
01994         $sDelAddress .= $oUser->oxuser__oxaddinfo;
01995         $sDelAddress .= $oUser->oxuser__oxustid;
01996         $sDelAddress .= $oUser->oxuser__oxcity;
01997         $sDelAddress .= $oUser->oxuser__oxcountryid;
01998         $sDelAddress .= $oUser->oxuser__oxstateid;
01999         $sDelAddress .= $oUser->oxuser__oxzip;
02000         $sDelAddress .= $oUser->oxuser__oxfon;
02001         $sDelAddress .= $oUser->oxuser__oxfax;
02002         $sDelAddress .= $oUser->oxuser__oxsal;
02003 
02004         // delivery address
02005         if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
02006             // set delivery address
02007             $sDelAddress .= $oDelAdress->oxaddress__oxcompany;
02008             $sDelAddress .= $oDelAdress->oxaddress__oxfname;
02009             $sDelAddress .= $oDelAdress->oxaddress__oxlname;
02010             $sDelAddress .= $oDelAdress->oxaddress__oxstreet;
02011             $sDelAddress .= $oDelAdress->oxaddress__oxstreetnr;
02012             $sDelAddress .= $oDelAdress->oxaddress__oxaddinfo;
02013             $sDelAddress .= $oDelAdress->oxaddress__oxcity;
02014             $sDelAddress .= $oDelAdress->oxaddress__oxcountryid;
02015             $sDelAddress .= $oDelAdress->oxaddress__oxstateid;
02016             $sDelAddress .= $oDelAdress->oxaddress__oxzip;
02017             $sDelAddress .= $oDelAdress->oxaddress__oxfon;
02018             $sDelAddress .= $oDelAdress->oxaddress__oxfax;
02019             $sDelAddress .= $oDelAdress->oxaddress__oxsal;
02020         }
02021 
02022         if ($sDelAddressMD5 != md5($sDelAddress)) {
02023             return self::ORDER_STATE_INVALIDDElADDRESSCHANGED;
02024         }
02025 
02026         return;
02027     }
02028 
02029 
02030 
02039     public function validateDelivery( $oBasket )
02040     {
02041         // proceed with no delivery
02042         // used for other countries
02043         if ( $oBasket->getPaymentId() == 'oxempty') {
02044             return;
02045         }
02046         $oDb = oxDb::getDb();
02047 
02048         $oDelSet = oxNew( "oxdeliveryset" );
02049         $sTable = $oDelSet->getViewName();
02050 
02051         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
02052         $oDb->quote( $oBasket->getShippingId() )." and ".$oDelSet->getSqlActiveSnippet();
02053 
02054         if ( !$oDb->getOne( $sQ, false, false ) ) {
02055             // throwing exception
02056             return self::ORDER_STATE_INVALIDDELIVERY;
02057         }
02058     }
02059 
02068     public function validatePayment( $oBasket )
02069     {
02070         $oDb = oxDb::getDb();
02071 
02072         $oPayment = oxNew( "oxpayment" );
02073         $sTable = $oPayment->getViewName();
02074 
02075         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
02076         $oDb->quote( $oBasket->getPaymentId() )." and ".$oPayment->getSqlActiveSnippet();
02077 
02078         if ( !$oDb->getOne( $sQ, false, false ) ) {
02079             return self::ORDER_STATE_INVALIDPAYMENT;
02080         }
02081     }
02082 
02090     protected function _setTsProtection( oxBasket $oBasket )
02091     {
02092         // protection price
02093         if ( ( $oTsProtectionCost = $oBasket->getCosts( 'oxtsprotection' ) ) ) {
02094             $this->oxorder__oxtsprotectcosts = new oxField($oTsProtectionCost->getBruttoPrice(), oxField::T_RAW);
02095         }
02096 
02097         // protection protduct id
02098         $this->oxorder__oxtsprotectid = new oxField($oBasket->getTsProductId(), oxField::T_RAW);
02099     }
02100 
02109     protected function _executeTsProtection( oxBasket $oBasket )
02110     {
02111         $aValues['tsProductId'] = $this->oxorder__oxtsprotectid->value;
02112         $aValues['amount'] = $oBasket->getPrice()->getBruttoPrice();
02113         $oCur = $this->getConfig()->getActShopCurrencyObject();
02114         $aValues['currency'] = $oCur->name;
02115         $aValues['buyerEmail'] = $this->oxorder__oxbillemail->value;
02116         $aValues['shopCustomerID'] = $this->oxorder__oxuserid->value;
02117         $aValues['shopOrderID'] = $this->oxorder__oxordernr->value;
02118         $aValues['orderDate'] = $this->oxorder__oxorderdate->value;
02119         $sPaymentId = $oBasket->getPaymentId();
02120         $oTsProtection = oxNew('oxtsprotection');
02121         $blRes = $oTsProtection->requestForTsProtection( $aValues, $sPaymentId );
02122         /*if ( !$blRes ) {
02123             $this->delete();
02124             return self::ORDER_STATE_INVALIDTSPROTECTION;
02125         }*/
02126         return true; // everything fine
02127     }
02128 
02134     public function getFormattedTotalNetSum()
02135     {
02136         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalnetsum->value, $this->getOrderCurrency() );
02137     }
02138 
02144     public function getFormattedTotalBrutSum()
02145     {
02146         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $this->getOrderCurrency() );
02147     }
02148 
02154     public function getFormattedeliveryCost()
02155     {
02156         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxdelcost->value, $this->getOrderCurrency() );
02157     }
02158 
02164     public function getFormattedPayCost()
02165     {
02166        return oxLang::getInstance()->formatCurrency( $this->oxorder__oxpaycost->value, $this->getOrderCurrency() );
02167     }
02168 
02174     public function getFormattedWrapCost()
02175     {
02176         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxwrapcost->value, $this->getOrderCurrency() );
02177     }
02178 
02184     public function getFormattedTotalVouchers()
02185     {
02186         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $this->getOrderCurrency() );
02187     }
02188 
02194     public function getFormattedDiscount()
02195     {
02196         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxdiscount->value, $this->getOrderCurrency() );
02197     }
02198 
02204     public function getFormattedTotalOrderSum()
02205     {
02206         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalordersum->value, $this->getOrderCurrency() );
02207     }
02208 
02214     public function getShipmentTrackingUrl()
02215     {
02216         if ( $this->_sShipTrackUrl === null && $this->oxorder__oxtrackcode->value ) {
02217             $this->_sShipTrackUrl = "http://www.dpd.de/cgi-bin/delistrack?typ=1&amp;lang=de&amp;pknr=".$this->oxorder__oxtrackcode->value;
02218         }
02219 
02220         return $this->_sShipTrackUrl;
02221     }
02222 
02223 }