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         $myConfig = $this->getConfig();
00502         // order number setter in finalize if cfq opt true
00503         if ( !$this->oxorder__oxordernr->value ) {
00504             if ( $myConfig->getConfigParam( 'blStoreOrderNrInFinalize' ) ) {
00505                 $this->_setNumber();
00506             }
00507         } else {
00508             oxNew( 'oxCounter' )->update( $this->_getCounterIdent(), $this->oxorder__oxordernr->value );
00509         }
00510         
00511         //#4005: Order creation time is not updated when order processing is complete
00512         if ( !$blRecalculatingOrder ) {
00513            $this-> _updateOrderDate();
00514         }
00515 
00516         // updating order trans status (success status)
00517         $this->_setOrderStatus( 'OK' );
00518 
00519         // store orderid
00520         $oBasket->setOrderId( $this->getId() );
00521 
00522         // updating wish lists
00523         $this->_updateWishlist( $oBasket->getContents(), $oUser );
00524 
00525         // updating users notice list
00526         $this->_updateNoticeList( $oBasket->getContents(), $oUser );
00527 
00528         // marking vouchers as used and sets them to $this->_aVoucherList (will be used in order email)
00529         // skipping this action in case of order recalculation
00530         if ( !$blRecalculatingOrder ) {
00531             $this->_markVouchers( $oBasket, $oUser );
00532         }
00533 
00534         // send order by email to shop owner and current user
00535         // skipping this action in case of order recalculation
00536         if ( !$blRecalculatingOrder ) {
00537             $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
00538         } else {
00539             $iRet = self::ORDER_STATE_OK;
00540         }
00541 
00542         return $iRet;
00543     }
00544 
00552     protected function _setOrderStatus( $sStatus )
00553     {
00554         $oDb = oxDb::getDb();
00555         $sQ = 'update oxorder set oxtransstatus='.$oDb->quote( $sStatus ).' where oxid='.$oDb->quote( $this->getId() );
00556         $oDb->execute( $sQ );
00557 
00558         //updating order object
00559         $this->oxorder__oxtransstatus = new oxField( $sStatus, oxField::T_RAW );
00560     }
00561 
00569     protected function _convertVat( $sVat )
00570     {
00571         if ( strpos( $sVat, '.' ) < strpos( $sVat, ',' ) ) {
00572             $sVat = str_replace( array( '.', ',' ), array( '', '.' ), $sVat );
00573         } else {
00574             $sVat = str_replace( ',', '', $sVat );
00575         }
00576         return (float) $sVat;
00577     }
00578 
00589     protected function _loadFromBasket( oxBasket $oBasket )
00590     {
00591         $myConfig = $this->getConfig();
00592 
00593         // store IP Adress - default must be FALSE as it is illegal to store
00594         if ( $myConfig->getConfigParam( 'blStoreIPs' ) &&  $this->oxorder__oxip->value === null ) {
00595             $this->oxorder__oxip = new oxField(oxUtilsServer::getInstance()->getRemoteAddress(), oxField::T_RAW);
00596         }
00597 
00598         // copying main price info
00599         $this->oxorder__oxtotalnetsum   = new oxField(oxUtils::getInstance()->fRound($oBasket->getDiscountedNettoPrice()), oxField::T_RAW);
00600         $this->oxorder__oxtotalbrutsum  = new oxField($oBasket->getProductsPrice()->getBruttoSum(), oxField::T_RAW);
00601         $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00602 
00603         // copying discounted VAT info
00604         $iVatIndex = 1;
00605         foreach ( $oBasket->getProductVats(false) as $iVat => $dPrice ) {
00606             $this->{"oxorder__oxartvat$iVatIndex"}      = new oxField( $this->_convertVat( $iVat ), oxField::T_RAW);
00607             $this->{"oxorder__oxartvatprice$iVatIndex"} = new oxField($dPrice, oxField::T_RAW);
00608             $iVatIndex ++;
00609         }
00610 
00611         // payment costs if available
00612         if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00613             $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00614             $this->oxorder__oxpayvat  = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00615         }
00616 
00617         // delivery info
00618         if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00619             $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00620             //V #M382: Save VAT, not VAT value for delivery costs
00621             $this->oxorder__oxdelvat  = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW); //V #M382
00622             $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00623         }
00624 
00625         // user remark
00626         if ( !isset( $this->oxorder__oxremark ) || $this->oxorder__oxremark->value === null ) {
00627             $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00628         }
00629 
00630         // currency
00631         $oCur = $myConfig->getActShopCurrencyObject();
00632         $this->oxorder__oxcurrency = new oxField($oCur->name);
00633         $this->oxorder__oxcurrate  = new oxField($oCur->rate, oxField::T_RAW);
00634 
00635         // store voucherdiscount
00636         if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00637             $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00638         }
00639 
00640         // general discount
00641         if ( $this->_blReloadDiscount ) {
00642             $dDiscount = 0;
00643             $aDiscounts = $oBasket->getDiscounts();
00644             if ( count($aDiscounts) > 0 ) {
00645                 foreach ($aDiscounts as $oDiscount) {
00646                     $dDiscount += $oDiscount->dDiscount;
00647                 }
00648             }
00649             $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00650         }
00651 
00652         //order language
00653         $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00654 
00655 
00656         // initial status - 'ERROR'
00657         $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00658 
00659         // copies basket product info ...
00660         $this->_setOrderArticles( $oBasket->getContents() );
00661 
00662         // copies wrapping info
00663         $this->_setWrapping( $oBasket );
00664 
00665         // copies TS protection info
00666         $this->_setTsProtection( $oBasket );
00667     }
00668 
00675     public function getOrderLanguage()
00676     {
00677         if ( $this->_iOrderLang === null ) {
00678             if ( isset( $this->oxorder__oxlang->value ) ) {
00679                 $this->_iOrderLang = oxLang::getInstance()->validateLanguage( $this->oxorder__oxlang->value );
00680             } else {
00681                 $this->_iOrderLang = oxLang::getInstance()->getBaseLanguage();
00682             }
00683         }
00684         return $this->_iOrderLang;
00685     }
00686 
00694     protected function _setUser( $oUser )
00695     {
00696 
00697         $this->oxorder__oxuserid        = new oxField($oUser->getId());
00698 
00699         // bill address
00700         $this->oxorder__oxbillcompany     = clone $oUser->oxuser__oxcompany;
00701         $this->oxorder__oxbillemail       = clone $oUser->oxuser__oxusername;
00702         $this->oxorder__oxbillfname       = clone $oUser->oxuser__oxfname;
00703         $this->oxorder__oxbilllname       = clone $oUser->oxuser__oxlname;
00704         $this->oxorder__oxbillstreet      = clone $oUser->oxuser__oxstreet;
00705         $this->oxorder__oxbillstreetnr    = clone $oUser->oxuser__oxstreetnr;
00706         $this->oxorder__oxbilladdinfo     = clone $oUser->oxuser__oxaddinfo;
00707         $this->oxorder__oxbillustid       = clone $oUser->oxuser__oxustid;
00708         $this->oxorder__oxbillcity        = clone $oUser->oxuser__oxcity;
00709         $this->oxorder__oxbillcountryid   = clone $oUser->oxuser__oxcountryid;
00710         $this->oxorder__oxbillstateid     = clone $oUser->oxuser__oxstateid;
00711         $this->oxorder__oxbillzip         = clone $oUser->oxuser__oxzip;
00712         $this->oxorder__oxbillfon         = clone $oUser->oxuser__oxfon;
00713         $this->oxorder__oxbillfax         = clone $oUser->oxuser__oxfax;
00714         $this->oxorder__oxbillsal         = clone $oUser->oxuser__oxsal;
00715 
00716 
00717         // delivery address
00718         if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00719             // set delivery address
00720             $this->oxorder__oxdelcompany   = clone $oDelAdress->oxaddress__oxcompany;
00721             $this->oxorder__oxdelfname     = clone $oDelAdress->oxaddress__oxfname;
00722             $this->oxorder__oxdellname     = clone $oDelAdress->oxaddress__oxlname;
00723             $this->oxorder__oxdelstreet    = clone $oDelAdress->oxaddress__oxstreet;
00724             $this->oxorder__oxdelstreetnr  = clone $oDelAdress->oxaddress__oxstreetnr;
00725             $this->oxorder__oxdeladdinfo   = clone $oDelAdress->oxaddress__oxaddinfo;
00726             $this->oxorder__oxdelcity      = clone $oDelAdress->oxaddress__oxcity;
00727             $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00728             $this->oxorder__oxdelstateid   = clone $oDelAdress->oxaddress__oxstateid;
00729             $this->oxorder__oxdelzip       = clone $oDelAdress->oxaddress__oxzip;
00730             $this->oxorder__oxdelfon       = clone $oDelAdress->oxaddress__oxfon;
00731             $this->oxorder__oxdelfax       = clone $oDelAdress->oxaddress__oxfax;
00732             $this->oxorder__oxdelsal       = clone $oDelAdress->oxaddress__oxsal;
00733         }
00734     }
00735 
00743     protected function _setWrapping( oxBasket $oBasket )
00744     {
00745         $myConfig = $this->getConfig();
00746 
00747         // wrapping price
00748         if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00749             $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00750 
00751             // wrapping VAT will be always calculated (#3757)
00752             $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00753         }
00754 
00755         // greetings card
00756         $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00757 
00758         // card text will be stored in database
00759         $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00760     }
00761 
00770     protected function _setOrderArticles( $aArticleList )
00771     {
00772         // reset articles list
00773         $this->_oArticles = oxNew( 'oxlist' );
00774         $iCurrLang = $this->getOrderLanguage();
00775 
00776         // add all the products we have on basket to the order
00777         foreach ( $aArticleList as $oContent ) {
00778 
00779             //$oContent->oProduct = $oContent->getArticle();
00780             // #M773 Do not use article lazy loading on order save
00781             $oProduct = $oContent->getArticle( true, null, true);
00782 
00783             // copy only if object is oxarticle type
00784             if ( $oProduct->isOrderArticle() ) {
00785                 $oOrderArticle = $oProduct;
00786             } else {
00787 
00788                 // if order language doe not match product language - article must be reloaded in order language
00789                 if ( $iCurrLang != $oProduct->getLanguage() ) {
00790                     $oProduct->loadInLang( $iCurrLang, $oProduct->getProductId() );
00791                 }
00792 
00793                 // set chosen selectlist
00794                 $sSelList = '';
00795                 if ( count( $aChosenSelList = $oContent->getChosenSelList() ) ) {
00796                     foreach ( $aChosenSelList as $oItem ) {
00797                         if ( $sSelList ) {
00798                             $sSelList .= ", ";
00799                         }
00800                         $sSelList .= "{$oItem->name} : {$oItem->value}";
00801                     }
00802                 }
00803 
00804                 $oOrderArticle = oxNew( 'oxorderarticle' );
00805                 $oOrderArticle->setIsNewOrderItem( true );
00806                 $oOrderArticle->copyThis( $oProduct );
00807                 $oOrderArticle->setId();
00808 
00809                 $oOrderArticle->oxorderarticles__oxartnum     = clone $oProduct->oxarticles__oxartnum;
00810                 $oOrderArticle->oxorderarticles__oxselvariant = new oxField( trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->getRawValue() ), oxField::T_RAW );
00811                 $oOrderArticle->oxorderarticles__oxshortdesc  = new oxField( $oProduct->oxarticles__oxshortdesc->getRawValue(), oxField::T_RAW );
00812                 // #M974: duplicated entries for the name of variants in orders
00813                 $oOrderArticle->oxorderarticles__oxtitle      = new oxField( trim( $oProduct->oxarticles__oxtitle->getRawValue() ), oxField::T_RAW );
00814 
00815                 // copying persistent parameters ...
00816                 if ( !is_array( $aPersParams = $oProduct->getPersParams() ) ) {
00817                     $aPersParams = $oContent->getPersParams();
00818                 }
00819                 if ( is_array( $aPersParams ) && count( $aPersParams )) {
00820                     $oOrderArticle->oxorderarticles__oxpersparam = new oxField( serialize( $aPersParams ), oxField::T_RAW );
00821                 }
00822             }
00823 
00824             // ids, titles, numbers ...
00825             $oOrderArticle->oxorderarticles__oxorderid = new oxField( $this->getId() );
00826             $oOrderArticle->oxorderarticles__oxartid   = new oxField( $oContent->getProductId() );
00827             $oOrderArticle->oxorderarticles__oxamount  = new oxField( $oContent->getAmount() );
00828 
00829             // prices
00830             $oPrice = $oContent->getPrice();
00831             $oOrderArticle->oxorderarticles__oxnetprice  = new oxField( $oPrice->getNettoPrice(), oxField::T_RAW );
00832             $oOrderArticle->oxorderarticles__oxvatprice  = new oxField( $oPrice->getVatValue(), oxField::T_RAW );
00833             $oOrderArticle->oxorderarticles__oxbrutprice = new oxField( $oPrice->getBruttoPrice(), oxField::T_RAW );
00834             $oOrderArticle->oxorderarticles__oxvat       = new oxField( $oPrice->getVat(), oxField::T_RAW );
00835 
00836             $oUnitPtice = $oContent->getUnitPrice();
00837             $oOrderArticle->oxorderarticles__oxnprice = new oxField( $oUnitPtice->getNettoPrice(), oxField::T_RAW );
00838             $oOrderArticle->oxorderarticles__oxbprice = new oxField( $oUnitPtice->getBruttoPrice(), oxField::T_RAW );
00839 
00840             // wrap id
00841             $oOrderArticle->oxorderarticles__oxwrapid = new oxField( $oContent->getWrappingId(), oxField::T_RAW );
00842 
00843             // items shop id
00844             $oOrderArticle->oxorderarticles__oxordershopid = new oxField( $oContent->getShopId(), oxField::T_RAW );
00845 
00846             // bundle?
00847             $oOrderArticle->oxorderarticles__oxisbundle = new oxField( $oContent->isBundle() );
00848 
00849             // add information for eMail
00850             //P
00851             //TODO: check if this assign is needed at all
00852             $oOrderArticle->oProduct = $oProduct;
00853 
00854             $oOrderArticle->setArticle( $oProduct );
00855 
00856             // simulatin order article list
00857             $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
00858         }
00859     }
00860 
00872     protected function _executePayment( oxBasket $oBasket, $oUserpayment )
00873     {
00874         $oPayTransaction = $this->_getGateway();
00875         $oPayTransaction->setPaymentParams( $oUserpayment );
00876 
00877         if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
00878             $this->delete();
00879 
00880             // checking for error messages
00881             if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
00882                 if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
00883                     return $sLastError;
00884                 }
00885             }
00886 
00887             // checking for error codes
00888             if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
00889                 if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
00890                     return $iLastErrorNo;
00891                 }
00892             }
00893 
00894             return self::ORDER_STATE_PAYMENTERROR; // means no authentication
00895         }
00896         return true; // everything fine
00897     }
00898 
00905     protected function _getGateway()
00906     {
00907         return oxNew( 'oxPaymentGateway' );
00908     }
00909 
00917     protected function _setPayment( $sPaymentid )
00918     {
00919         // copying payment info fields
00920         $aDynvalue = oxSession::getVar( 'dynvalue' );
00921         $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
00922 
00923         // loading payment object
00924         $oPayment = oxNew( 'oxpayment' );
00925 
00926         if (!$oPayment->load( $sPaymentid )) {
00927             return null;
00928         }
00929 
00930         // #756M Preserve already stored payment information
00931         if ( !$aDynvalue && ( $oUserpayment = $this->getPaymentType() ) ) {
00932             if ( is_array( $aStoredDynvalue = $oUserpayment->getDynValues() ) ) {
00933                 foreach ( $aStoredDynvalue as $oVal ) {
00934                     $aDynvalue[$oVal->name] = $oVal->value;
00935                 }
00936             }
00937         }
00938 
00939         $oPayment->setDynValues( oxUtils::getInstance()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
00940 
00941         // collecting dynamic values
00942         $aDynVal = array();
00943 
00944         if ( is_array( $aPaymentDynValues = $oPayment->getDynValues() ) ) {
00945             foreach ( $aPaymentDynValues  as $key => $oVal ) {
00946                 if ( isset( $aDynvalue[$oVal->name] ) ) {
00947                     $oVal->value = $aDynvalue[$oVal->name];
00948                 }
00949 
00950                 //$oPayment->setDynValue($key, $oVal);
00951                 $aPaymentDynValues[$key] = $oVal;
00952                 $aDynVal[$oVal->name] = $oVal->value;
00953             }
00954         }
00955 
00956         // Store this payment information, we might allow users later to
00957         // reactivate already give payment informations
00958 
00959         $oUserpayment = oxNew( 'oxuserpayment' );
00960         $oUserpayment->oxuserpayments__oxuserid     = clone $this->oxorder__oxuserid;
00961         $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
00962         $oUserpayment->oxuserpayments__oxvalue      = new oxField(oxUtils::getInstance()->assignValuesToText( $aDynVal ), oxField::T_RAW);
00963         $oUserpayment->oxpayments__oxdesc           = clone $oPayment->oxpayments__oxdesc;
00964         $oUserpayment->oxpayments__oxlongdesc       = clone $oPayment->oxpayments__oxlongdesc;
00965         $oUserpayment->setDynValues( $aPaymentDynValues );
00966         $oUserpayment->save();
00967 
00968         // storing payment information to order
00969         $this->oxorder__oxpaymentid   = new oxField($oUserpayment->getId(), oxField::T_RAW);
00970         $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
00971 
00972         // returning user payment object which will be used later in code ...
00973         return $oUserpayment;
00974     }
00975 
00981     protected function _setFolder()
00982     {
00983         $myConfig = $this->getConfig();
00984         $this->oxorder__oxfolder    = new oxField(key( $myConfig->getShopConfVar(  'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
00985     }
00986 
00996     protected function _updateWishlist( $aArticleList, $oUser )
00997     {
00998 
00999         foreach ( $aArticleList as $oContent) {
01000             if ( ( $sWishId = $oContent->getWishId() ) ) {
01001 
01002                 // checking which wishlist user uses ..
01003                 if ( $sWishId == $oUser->getId() ) {
01004                     $oUserBasket = $oUser->getBasket( 'wishlist' );
01005                 } else {
01006                     $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
01007                     $oUserBasket = oxNew( 'oxuserbasket' );
01008                     $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
01009                 }
01010 
01011                 // updating users wish list
01012                 if ( $oUserBasket ) {
01013                     if ( !($sProdId = $oContent->getWishArticleId() )) {
01014                         $sProdId = $oContent->getProductId();
01015                     }
01016                     $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
01017                     $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
01018                     if ( $dNewAmount < 0) {
01019                         $dNewAmount = 0;
01020                     }
01021                     $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
01022                 }
01023             }
01024         }
01025     }
01026 
01036     protected function _updateNoticeList( $aArticleList, $oUser )
01037     {
01038         // loading users notice list ..
01039         if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
01040             // only if wishlist is enabled
01041             foreach ( $aArticleList as $oContent) {
01042                 $sProdId = $oContent->getProductId();
01043 
01044                 // updating users notice list
01045                 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList(), $oContent->getPersParams() );
01046                 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
01047                 if ( $dNewAmount < 0) {
01048                     $dNewAmount = 0;
01049                 }
01050                 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true, $oContent->getPersParams() );
01051             }
01052         }
01053     }
01054     
01060     protected function _updateOrderDate()
01061     {
01062         $oDb = oxDb::getDb();    
01063         $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );        
01064         $sQ = 'update oxorder set oxorderdate=\''.$sDate.'\' where oxid='.$oDb->quote( $this->getId() );
01065         $this->oxorder__oxorderdate = new oxField( $sDate, oxField::T_RAW );
01066         $oDb->execute( $sQ );
01067     }
01068 
01078     protected function _markVouchers( $oBasket, $oUser )
01079     {
01080         $this->_aVoucherList = $oBasket->getVouchers();
01081 
01082         if ( is_array( $this->_aVoucherList ) ) {
01083             foreach ( $this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
01084                 $oVoucher = oxNew( 'oxvoucher' );
01085                 $oVoucher->load( $sVoucherId );
01086                 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->dVoucherdiscount );
01087 
01088                 $this->_aVoucherList[$sVoucherId] = $oVoucher;
01089             }
01090         }
01091     }
01092 
01098     public function save()
01099     {
01100         if ( ( $blSave = parent::save() ) ) {
01101 
01102             // saving order articles
01103             $oOrderArticles = $this->getOrderArticles();
01104             if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
01105                 foreach ( $oOrderArticles as $oOrderArticle ) {
01106                     $oOrderArticle->save();
01107                 }
01108             }
01109         }
01110 
01111         return $blSave;
01112     }
01113 
01120     public function getDelAddressInfo()
01121     {
01122         $oDelAdress = null;
01123         if (! ($soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
01124             $soxAddressId = oxSession::getVar( 'deladrid' );
01125         }
01126         if ( $soxAddressId ) {
01127             $oDelAdress = oxNew( 'oxaddress' );
01128             $oDelAdress->load( $soxAddressId );
01129 
01130             //get delivery country name from delivery country id
01131             if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
01132                  $oCountry = oxNew( 'oxcountry' );
01133                  $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
01134                  $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
01135             }
01136         }
01137         return $oDelAdress;
01138     }
01139 
01150     public function validateStock( $oBasket )
01151     {
01152         foreach ( $oBasket->getContents() as $key => $oContent ) {
01153             try {
01154                 $oProd = $oContent->getArticle( true );
01155             } catch ( oxNoArticleException $oEx ) {
01156                 $oBasket->removeItem( $key );
01157                 throw $oEx;
01158             } catch ( oxArticleInputException $oEx ) {
01159                 $oBasket->removeItem( $key );
01160                 throw $oEx;
01161             }
01162 
01163             // check if its still available
01164             $dArtStockAmount = $oBasket->getArtStockInBasket( $oProd->getId(), $key );
01165             $iOnStock = $oProd->checkForStock( $oContent->getAmount(), $dArtStockAmount );
01166             if ( $iOnStock !== true ) {
01167                 $oEx = oxNew( 'oxOutOfStockException' );
01168                 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
01169                 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
01170                 $oEx->setProductId( $oProd->getId() );
01171 
01172                 if (!is_numeric($iOnStock)) {
01173                     $iOnStock = 0;
01174                 }
01175                 $oEx->setRemainingAmount( $iOnStock );
01176                 throw $oEx;
01177             }
01178         }
01179     }
01180 
01186     protected function _insert()
01187     {
01188         $myConfig = $this->getConfig();
01189         $oUtilsDate = oxUtilsDate::getInstance();
01190 
01191         //V #M525 orderdate must be the same as it was
01192         if ( !$this->oxorder__oxorderdate->value ) {
01193             $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
01194         } else {
01195             $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
01196         }
01197 
01198         $this->oxorder__oxshopid    = new oxField($myConfig->getShopId(), oxField::T_RAW);
01199         $this->oxorder__oxsenddate  = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01200 
01201         if ( ( $blInsert = parent::_insert() ) ) {
01202             // setting order number
01203             if ( !$this->oxorder__oxordernr->value ) {
01204                 if ( !$myConfig->getConfigParam( 'blStoreOrderNrInFinalize' ) ) {
01205                     $blInsert = $this->_setNumber();
01206                 }
01207             } else {
01208                 oxNew( 'oxCounter' )->update( $this->_getCounterIdent(), $this->oxorder__oxordernr->value );
01209             }
01210         }
01211         return $blInsert;
01212     }
01213 
01219     protected function _getCounterIdent()
01220     {
01221         $sCounterIdent = ( $this->_blSeparateNumbering ) ? 'oxOrder_' . $this->getConfig()->getShopId() : 'oxOrder';
01222         return $sCounterIdent;
01223     }
01224 
01225 
01231     protected function _setNumber()
01232     {
01233         $oDb = oxDb::getDb();
01234 
01235         $iCnt = oxNew( 'oxCounter' )->getNext( $this->_getCounterIdent() );
01236         $sQ = "update oxorder set oxordernr = $iCnt where oxid = ?";
01237         $blUpdate = ( bool ) $oDb->execute( $sQ, array( $this->getId() ) );
01238 
01239         if ( $blUpdate ) {
01240             $this->oxorder__oxordernr = new oxField( $iCnt );
01241         }
01242 
01243         return $blUpdate;
01244     }
01245 
01251     protected function _update()
01252     {
01253         $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01254         return parent::_update();
01255     }
01256 
01265     public function delete( $sOxId = null )
01266     {
01267         if ( $sOxId ) {
01268             if ( !$this->load( $sOxId ) ) {
01269                 // such order does not exist
01270                 return false;
01271             }
01272         } elseif ( !$sOxId ) {
01273             $sOxId = $this->getId();
01274         }
01275 
01276         // no order id is passed
01277         if ( !$sOxId ) {
01278             return false;
01279         }
01280 
01281 
01282         // delete order articles
01283         $oOrderArticles = $this->getOrderArticles( false );
01284         foreach ( $oOrderArticles as $oOrderArticle ) {
01285             $oOrderArticle->delete();
01286         }
01287 
01288         // #440 - deleting user payment info
01289         if ( $oPaymentType = $this->getPaymentType() ) {
01290             $oPaymentType->delete();
01291         }
01292 
01293         return parent::delete( $sOxId );
01294     }
01295 
01305     public function recalculateOrder( $aNewArticles = array() )
01306     {
01307         oxDb::startTransaction();
01308 
01309         try {
01310             $oBasket = $this->_getOrderBasket();
01311 
01312             // add this order articles to virtual basket and recalculates basket
01313             $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles( true ) );
01314 
01315             // adding new articles to existing order
01316             $this->_addArticlesToBasket( $oBasket, $aNewArticles );
01317 
01318             // recalculating basket
01319             $oBasket->calculateBasket( true );
01320 
01321             //finalizing order (skipping payment execution, vouchers marking and mail sending)
01322             $iRet = $this->finalizeOrder( $oBasket, $this->getOrderUser(), true );
01323 
01324             //if finalizing order failed, rollback transaction
01325             if ( $iRet !== 1 ) {
01326                 oxDb::rollbackTransaction();
01327             } else {
01328                 oxDb::commitTransaction();
01329             }
01330 
01331         } catch( Exception $oE ) {
01332             // if exception, rollBack everything
01333             oxDb::rollbackTransaction();
01334 
01335             if ( defined( 'OXID_PHP_UNIT' ) ) {
01336                 throw $oE;
01337             }
01338         }
01339     }
01340 
01341     protected $_oOrderBasket = null;
01349     protected function _getOrderBasket( $blStockCheck = true )
01350     {
01351         $this->_oOrderBasket = oxNew( "oxbasket" );
01352 
01353         // setting stock check mode
01354         $this->_oOrderBasket->setStockCheckMode( $blStockCheck );
01355 
01356         // setting virtual basket user
01357         $this->_oOrderBasket->setBasketUser( $this->getOrderUser() );
01358 
01359         // transferring order id
01360         $this->_oOrderBasket->setOrderId( $this->getId() );
01361 
01362         // setting basket currency order uses
01363         $aCurrencies = $this->getConfig()->getCurrencyArray();
01364         foreach ( $aCurrencies as $oCur ) {
01365             if ($oCur->name == $this->oxorder__oxcurrency->value) {
01366                 $oBasketCur = $oCur;
01367                 break;
01368             }
01369         }
01370 
01371         // setting currency
01372         $this->_oOrderBasket->setBasketCurrency( $oBasketCur );
01373 
01374         // set basket card id and message
01375         $this->_oOrderBasket->setCardId( $this->oxorder__oxcardid->value );
01376         $this->_oOrderBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01377 
01378         if ( $this->_blReloadDiscount ) {
01379             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01380             // disabling availability check
01381             $this->_oOrderBasket->setSkipVouchersChecking( true );
01382 
01383             // add previously used vouchers
01384             $sQ = 'select oxid from oxvouchers where oxorderid = '.$oDb->quote( $this->getId() );
01385             $aVouchers = $oDb->getAll( $sQ );
01386             foreach ( $aVouchers as $aVoucher ) {
01387                 $this->_oOrderBasket->addVoucher( $aVoucher['oxid'] );
01388             }
01389         } else {
01390             $this->_oOrderBasket->setDiscountCalcMode( false );
01391             $this->_oOrderBasket->setVoucherDiscount( $this->oxorder__oxvoucherdiscount->value );
01392             $this->_oOrderBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
01393         }
01394 
01395         // must be kept old delivery?
01396         if ( !$this->_blReloadDelivery ) {
01397             $this->_oOrderBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01398         } else {
01399             //  set shipping
01400             $this->_oOrderBasket->setShipping( $this->oxorder__oxdeltype->value );
01401             $this->_oOrderBasket->setDeliveryPrice( null );
01402         }
01403 
01404         //set basket payment
01405         $this->_oOrderBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01406 
01407         return $this->_oOrderBasket;
01408     }
01409 
01418     public function setDelivery( $sDeliveryId )
01419     {
01420         $this->reloadDelivery( true );
01421         $this->oxorder__oxdeltype = new oxField( $sDeliveryId );
01422     }
01423 
01429     public function getOrderUser()
01430     {
01431         if ($this->_oUser === null ) {
01432             $this->_oUser = oxNew( "oxuser" );
01433             $this->_oUser->load( $this->oxorder__oxuserid->value );
01434 
01435             // if object is loaded then reusing its order info
01436             if ( $this->_isLoaded ) {
01437                 // bill address
01438                 $this->_oUser->oxuser__oxcompany  = clone $this->oxorder__oxbillcompany;
01439                 $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
01440                 $this->_oUser->oxuser__oxfname    = clone $this->oxorder__oxbillfname;
01441                 $this->_oUser->oxuser__oxlname    = clone $this->oxorder__oxbilllname;
01442                 $this->_oUser->oxuser__oxstreet   = clone $this->oxorder__oxbillstreet;
01443                 $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
01444                 $this->_oUser->oxuser__oxaddinfo  = clone $this->oxorder__oxbilladdinfo;
01445                 $this->_oUser->oxuser__oxustid    = clone $this->oxorder__oxbillustid;
01446 
01447 
01448                 $this->_oUser->oxuser__oxcity      = clone $this->oxorder__oxbillcity;
01449                 $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
01450                 $this->_oUser->oxuser__oxstateid   = clone $this->oxorder__oxbillstateid;
01451                 $this->_oUser->oxuser__oxzip       = clone $this->oxorder__oxbillzip;
01452                 $this->_oUser->oxuser__oxfon       = clone $this->oxorder__oxbillfon;
01453                 $this->_oUser->oxuser__oxfax       = clone $this->oxorder__oxbillfax;
01454                 $this->_oUser->oxuser__oxsal       = clone $this->oxorder__oxbillsal;
01455             }
01456         }
01457 
01458         return $this->_oUser;
01459     }
01460 
01468     public function pdfFooter( $oPdf )
01469     {
01470     }
01471 
01479     public function pdfHeaderplus( $oPdf )
01480     {
01481     }
01482 
01490     public function pdfHeader( $oPdf )
01491     {
01492     }
01493 
01502     public function genPdf( $sFilename, $iSelLang = 0 )
01503     {
01504     }
01505 
01511     public function getInvoiceNum()
01512     {
01513         $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01514         return ( ( int ) oxDb::getDb()->getOne( $sQ, false ) + 1 );
01515     }
01516 
01522     public function getNextBillNum()
01523     {
01524         $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01525         return ( ( int ) oxDb::getDb()->getOne( $sQ, false ) + 1 );
01526     }
01527 
01533     public function getShippingSetList()
01534     {
01535         // in which country we deliver
01536         if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
01537             $sShipId = $this->oxorder__oxbillcountryid->value;
01538         }
01539 
01540         $oBasket = $this->_getOrderBasket( false );
01541 
01542         // unsetting bundles
01543         $oOrderArticles = $this->getOrderArticles();
01544         foreach ( $oOrderArticles as $sItemId => $oItem ) {
01545             if ( $oItem->isBundle() ) {
01546                 $oOrderArticles->offsetUnset( $sItemId );
01547             }
01548         }
01549 
01550         // add this order articles to basket and recalculate basket
01551         $this->_addOrderArticlesToBasket( $oBasket, $oOrderArticles );
01552 
01553         // recalculating basket
01554         $oBasket->calculateBasket( true );
01555 
01556         // load fitting deliveries list
01557         $oDeliveryList = oxNew( "oxDeliveryList", "core" );
01558         $oDeliveryList->setCollectFittingDeliveriesSets( true );
01559 
01560         return $oDeliveryList->getDeliveryList( $oBasket, $this->getOrderUser(), $sShipId );
01561     }
01562 
01568     public function getVoucherNrList()
01569     {
01570         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01571         $aVouchers = array();
01572         $sSelect = "select oxvouchernr from oxvouchers where oxorderid = ".$oDb->quote( $this->oxorder__oxid->value );
01573         $rs = $oDb->select( $sSelect );
01574         if ($rs != false && $rs->recordCount() > 0) {
01575             while (!$rs->EOF) {
01576                 $aVouchers[] = $rs->fields['oxvouchernr'];
01577                 $rs->moveNext();
01578             }
01579         }
01580         return $aVouchers;
01581     }
01582 
01590     public function getOrderSum( $blToday = false )
01591     {
01592         $sSelect  = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01593         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01594 
01595         if ( $blToday ) {
01596             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01597         }
01598 
01599         return ( double ) oxDb::getDb()->getOne( $sSelect, false, false );
01600     }
01601 
01609     public function getOrderCnt( $blToday = false )
01610     {
01611         $sSelect  = 'select count(*) from oxorder where ';
01612         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'"  and oxorder.oxstorno != "1" ';
01613 
01614         if ( $blToday ) {
01615             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01616         }
01617 
01618         return ( int ) oxDb::getDb()->getOne( $sSelect, false, false );
01619     }
01620 
01621 
01629     protected function _checkOrderExist( $sOxId = null )
01630     {
01631         if ( !$sOxId) {
01632             return false;
01633         }
01634 
01635         $oDb = oxDb::getDb();
01636         if ( $oDb->getOne( 'select oxid from oxorder where oxid = '.$oDb->quote( $sOxId ), false, false ) ) {
01637             return true;
01638         }
01639 
01640         return false;
01641     }
01642 
01652     protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01653     {
01654         $iRet = self::ORDER_STATE_MAILINGERROR;
01655 
01656         // add user, basket and payment to order
01657         $this->_oUser    = $oUser;
01658         $this->_oBasket  = $oBasket;
01659         $this->_oPayment = $oPayment;
01660 
01661         $oxEmail = oxNew( 'oxemail' );
01662 
01663         // send order email to user
01664         if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01665             // mail to user was successfully sent
01666             $iRet = self::ORDER_STATE_OK;
01667         }
01668 
01669         // send order email to shop owner
01670         $oxEmail->sendOrderEMailToOwner( $this );
01671 
01672         return $iRet;
01673     }
01674 
01680     public function getBasket()
01681     {
01682         return $this->_oBasket;
01683     }
01684 
01690     public function getPayment()
01691     {
01692         return $this->_oPayment;
01693     }
01694 
01700     public function getVoucherList()
01701     {
01702         return $this->_aVoucherList;
01703     }
01704 
01710     public function getDelSet()
01711     {
01712         if ( $this->_oDelSet == null ) {
01713             // load deliveryset info
01714             $this->_oDelSet = oxNew( 'oxdeliveryset' );
01715             $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01716         }
01717 
01718         return $this->_oDelSet;
01719     }
01720 
01726     public function getPaymentType()
01727     {
01728         if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType === null ) {
01729             $this->_oPaymentType = false;
01730             $oPaymentType = oxNew( 'oxuserpayment' );
01731             if ( $oPaymentType->load( $this->oxorder__oxpaymentid->value ) ) {
01732                 $this->_oPaymentType = $oPaymentType;
01733             }
01734         }
01735 
01736         return $this->_oPaymentType;
01737     }
01738 
01744     public function getGiftCard()
01745     {
01746         if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01747             $this->_oGiftCard = oxNew( 'oxwrapping' );
01748             $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01749         }
01750 
01751         return $this->_oGiftCard;
01752     }
01753 
01761     public function setSeparateNumbering( $blSeparateNumbering = null )
01762     {
01763         $this->_blSeparateNumbering = $blSeparateNumbering;
01764     }
01765 
01773     public function getLastUserPaymentType( $sUserId)
01774     {
01775         $oDb = oxDb::getDb();
01776         $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid='.$oDb->quote( $sUserId ).' order by oxorder.oxorderdate desc ';
01777         $sLastPaymentId = $oDb->getOne( $sQ, false, false );
01778         return $sLastPaymentId;
01779     }
01780 
01789     protected function _addOrderArticlesToBasket( $oBasket, $aOrderArticles )
01790     {
01791         // if no order articles, return empty basket
01792         if ( count( $aOrderArticles ) > 0 ) {
01793 
01794             //adding order articles to basket
01795             foreach ( $aOrderArticles as $oOrderArticle ) {
01796                 $oBasket->addOrderArticleToBasket( $oOrderArticle );
01797             }
01798         }
01799     }
01800 
01809     protected function _addArticlesToBasket( $oBasket, $aArticles )
01810     {
01811         // if no order articles
01812         if ( count($aArticles ) > 0 ) {
01813 
01814             //adding order articles to basket
01815             foreach ( $aArticles as $oArticle ) {
01816                 $aSel = isset( $oArticle->oxorderarticles__oxselvariant ) ? $oArticle->oxorderarticles__oxselvariant->value : null;
01817                 $aPersParam = isset( $oArticle->oxorderarticles__oxpersparam ) ? $oArticle->getPersParams() : null;
01818                 $oBasket->addToBasket( $oArticle->oxorderarticles__oxartid->value,
01819                                        $oArticle->oxorderarticles__oxamount->value,
01820                                        $aSel, $aPersParam );
01821             }
01822         }
01823     }
01824 
01830     public function getTotalOrderSum()
01831     {
01832         $oCur = $this->getConfig()->getActShopCurrencyObject();
01833         return number_format( (double)$this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01834     }
01835 
01843     public function getProductVats( $blFormatCurrency = true )
01844     {
01845         $aVats = array();
01846         if ($this->oxorder__oxartvat1->value) {
01847             $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
01848         }
01849         if ($this->oxorder__oxartvat2->value) {
01850             $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
01851         }
01852 
01853         if ( $blFormatCurrency ) {
01854             $oLang = oxLang::getInstance();
01855             $oCur = $this->getConfig()->getActShopCurrencyObject();
01856             foreach ( $aVats as $sKey => $dVat ) {
01857                 $aVats[$sKey] = $oLang->formatCurrency( $dVat, $oCur );
01858             }
01859         }
01860         return $aVats;
01861     }
01862 
01868     public function getBillCountry()
01869     {
01870         if ( !$this->oxorder__oxbillcountry->value ) {
01871             $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
01872         }
01873         return $this->oxorder__oxbillcountry;
01874     }
01875 
01881     public function getDelCountry()
01882     {
01883         if ( !$this->oxorder__oxdelcountry->value ) {
01884             $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
01885         }
01886         return $this->oxorder__oxdelcountry;
01887     }
01895     public function reloadDelivery( $blReload )
01896     {
01897         $this->_blReloadDelivery = $blReload;
01898     }
01899 
01907     public function reloadDiscount( $blReload )
01908     {
01909         $this->_blReloadDiscount = $blReload;
01910     }
01911 
01917     public function cancelOrder()
01918     {
01919         $this->oxorder__oxstorno = new oxField( 1 );
01920         if ( $this->save() ) {
01921 
01922 
01923             // canceling ordered products
01924             foreach ( $this->getOrderArticles() as $oOrderArticle ) {
01925 
01926 
01927                 $oOrderArticle->cancelOrderArticle();
01928             }
01929         }
01930     }
01931 
01938     public function getOrderCurrency()
01939     {
01940         if ( $this->_oOrderCurrency === null ) {
01941 
01942             // setting default in case unrecognized currency was set during order
01943             $aCurrencies = $this->getConfig()->getCurrencyArray();
01944             $this->_oOrderCurrency = current( $aCurrencies );
01945 
01946             foreach ( $aCurrencies as $oCurr ) {
01947                 if ( $oCurr->name == $this->oxorder__oxcurrency->value ) {
01948                     $this->_oOrderCurrency = $oCurr;
01949                     break;
01950                 }
01951             }
01952         }
01953         return $this->_oOrderCurrency;
01954     }
01955 
01965     public function validateOrder( $oBasket, $oUser )
01966     {
01967         // validating stock
01968         $iValidState = $this->validateStock( $oBasket );
01969 
01970         if ( !$iValidState ) {
01971             // validating delivery
01972             $iValidState = $this->validateDelivery( $oBasket );
01973         }
01974 
01975         if ( !$iValidState ) {
01976             // validating payment
01977             $iValidState = $this->validatePayment( $oBasket );
01978         }
01979 
01980         if ( !$iValidState ) {
01981             //0003110 validating delivewry address, it is not be changed during checkout process
01982             $iValidState = $this->validateDeliveryAddress( $oUser );
01983         }
01984 
01985         if ( !$iValidState ) {
01986             // validatign minimum price
01987             $iValidState = $this->validateBasket( $oBasket );
01988         }
01989         return $iValidState;
01990     }
01991 
01999     public function validateBasket( $oBasket )
02000     {
02001         return $oBasket->isBelowMinOrderPrice() ? self::ORDER_STATE_BELOWMINPRICE : null;
02002     }
02003 
02012     public function validateDeliveryAddress( $oUser )
02013     {
02014         $sDelAddressMD5 =  oxConfig::getParameter( 'sDeliveryAddressMD5' );
02015 
02016         // bill address
02017         $sDelAddress = '';
02018         $sDelAddress .= $oUser->oxuser__oxcompany;
02019         $sDelAddress .= $oUser->oxuser__oxusername;
02020         $sDelAddress .= $oUser->oxuser__oxfname;
02021         $sDelAddress .= $oUser->oxuser__oxlname;
02022         $sDelAddress .= $oUser->oxuser__oxstreet;
02023         $sDelAddress .= $oUser->oxuser__oxstreetnr;
02024         $sDelAddress .= $oUser->oxuser__oxaddinfo;
02025         $sDelAddress .= $oUser->oxuser__oxustid;
02026         $sDelAddress .= $oUser->oxuser__oxcity;
02027         $sDelAddress .= $oUser->oxuser__oxcountryid;
02028         $sDelAddress .= $oUser->oxuser__oxstateid;
02029         $sDelAddress .= $oUser->oxuser__oxzip;
02030         $sDelAddress .= $oUser->oxuser__oxfon;
02031         $sDelAddress .= $oUser->oxuser__oxfax;
02032         $sDelAddress .= $oUser->oxuser__oxsal;
02033 
02034         // delivery address
02035         if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
02036             // set delivery address
02037             $sDelAddress .= $oDelAdress->oxaddress__oxcompany;
02038             $sDelAddress .= $oDelAdress->oxaddress__oxfname;
02039             $sDelAddress .= $oDelAdress->oxaddress__oxlname;
02040             $sDelAddress .= $oDelAdress->oxaddress__oxstreet;
02041             $sDelAddress .= $oDelAdress->oxaddress__oxstreetnr;
02042             $sDelAddress .= $oDelAdress->oxaddress__oxaddinfo;
02043             $sDelAddress .= $oDelAdress->oxaddress__oxcity;
02044             $sDelAddress .= $oDelAdress->oxaddress__oxcountryid;
02045             $sDelAddress .= $oDelAdress->oxaddress__oxstateid;
02046             $sDelAddress .= $oDelAdress->oxaddress__oxzip;
02047             $sDelAddress .= $oDelAdress->oxaddress__oxfon;
02048             $sDelAddress .= $oDelAdress->oxaddress__oxfax;
02049             $sDelAddress .= $oDelAdress->oxaddress__oxsal;
02050         }
02051 
02052         if ($sDelAddressMD5 != md5($sDelAddress)) {
02053             return self::ORDER_STATE_INVALIDDElADDRESSCHANGED;
02054         }
02055 
02056         return;
02057     }
02058 
02059 
02060 
02069     public function validateDelivery( $oBasket )
02070     {
02071         // proceed with no delivery
02072         // used for other countries
02073         if ( $oBasket->getPaymentId() == 'oxempty') {
02074             return;
02075         }
02076         $oDb = oxDb::getDb();
02077 
02078         $oDelSet = oxNew( "oxdeliveryset" );
02079         $sTable = $oDelSet->getViewName();
02080 
02081         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
02082         $oDb->quote( $oBasket->getShippingId() )." and ".$oDelSet->getSqlActiveSnippet();
02083 
02084         if ( !$oDb->getOne( $sQ, false, false ) ) {
02085             // throwing exception
02086             return self::ORDER_STATE_INVALIDDELIVERY;
02087         }
02088     }
02089 
02098     public function validatePayment( $oBasket )
02099     {
02100         $oDb = oxDb::getDb();
02101 
02102         $oPayment = oxNew( "oxpayment" );
02103         $sTable = $oPayment->getViewName();
02104 
02105         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
02106         $oDb->quote( $oBasket->getPaymentId() )." and ".$oPayment->getSqlActiveSnippet();
02107 
02108         if ( !$oDb->getOne( $sQ, false, false ) ) {
02109             return self::ORDER_STATE_INVALIDPAYMENT;
02110         }
02111     }
02112 
02120     protected function _setTsProtection( oxBasket $oBasket )
02121     {
02122         // protection price
02123         if ( ( $oTsProtectionCost = $oBasket->getCosts( 'oxtsprotection' ) ) ) {
02124             $this->oxorder__oxtsprotectcosts = new oxField($oTsProtectionCost->getBruttoPrice(), oxField::T_RAW);
02125         }
02126 
02127         // protection protduct id
02128         $this->oxorder__oxtsprotectid = new oxField($oBasket->getTsProductId(), oxField::T_RAW);
02129     }
02130 
02139     protected function _executeTsProtection( oxBasket $oBasket )
02140     {
02141         $aValues['tsProductId'] = $this->oxorder__oxtsprotectid->value;
02142         $aValues['amount'] = $oBasket->getPrice()->getBruttoPrice();
02143         $oCur = $this->getConfig()->getActShopCurrencyObject();
02144         $aValues['currency'] = $oCur->name;
02145         $aValues['buyerEmail'] = $this->oxorder__oxbillemail->value;
02146         $aValues['shopCustomerID'] = $this->oxorder__oxuserid->value;
02147         $aValues['shopOrderID'] = $this->oxorder__oxordernr->value;
02148         $aValues['orderDate'] = $this->oxorder__oxorderdate->value;
02149         $sPaymentId = $oBasket->getPaymentId();
02150         $oTsProtection = oxNew('oxtsprotection');
02151         $blRes = $oTsProtection->requestForTsProtection( $aValues, $sPaymentId );
02152         /*if ( !$blRes ) {
02153             $this->delete();
02154             return self::ORDER_STATE_INVALIDTSPROTECTION;
02155         }*/
02156         return true; // everything fine
02157     }
02158 
02164     public function getFormattedTotalNetSum()
02165     {
02166         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalnetsum->value, $this->getOrderCurrency() );
02167     }
02168 
02174     public function getFormattedTotalBrutSum()
02175     {
02176         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $this->getOrderCurrency() );
02177     }
02178 
02184     public function getFormattedeliveryCost()
02185     {
02186         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxdelcost->value, $this->getOrderCurrency() );
02187     }
02188 
02194     public function getFormattedPayCost()
02195     {
02196        return oxLang::getInstance()->formatCurrency( $this->oxorder__oxpaycost->value, $this->getOrderCurrency() );
02197     }
02198 
02204     public function getFormattedWrapCost()
02205     {
02206         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxwrapcost->value, $this->getOrderCurrency() );
02207     }
02208 
02214     public function getFormattedTotalVouchers()
02215     {
02216         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $this->getOrderCurrency() );
02217     }
02218 
02224     public function getFormattedDiscount()
02225     {
02226         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxdiscount->value, $this->getOrderCurrency() );
02227     }
02228 
02234     public function getFormattedTotalOrderSum()
02235     {
02236         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalordersum->value, $this->getOrderCurrency() );
02237     }
02238 
02244     public function getShipmentTrackingUrl()
02245     {
02246         if ( $this->_sShipTrackUrl === null && $this->oxorder__oxtrackcode->value ) {
02247             $this->_sShipTrackUrl = "http://www.dpd.de/cgi-bin/delistrack?typ=1&amp;lang=de&amp;pknr=".$this->oxorder__oxtrackcode->value;
02248         }
02249 
02250         return $this->_sShipTrackUrl;
02251     }
02252 
02253 }