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         //#4005: Order creation time is not updated when order processing is complete
00502         if ( !$blRecalculatingOrder ) {
00503            $this-> _updateOrderDate();
00504         }
00505 
00506         // updating order trans status (success status)
00507         $this->_setOrderStatus( 'OK' );
00508 
00509         // store orderid
00510         $oBasket->setOrderId( $this->getId() );
00511 
00512         // updating wish lists
00513         $this->_updateWishlist( $oBasket->getContents(), $oUser );
00514 
00515         // updating users notice list
00516         $this->_updateNoticeList( $oBasket->getContents(), $oUser );
00517 
00518         // marking vouchers as used and sets them to $this->_aVoucherList (will be used in order email)
00519         // skipping this action in case of order recalculation
00520         if ( !$blRecalculatingOrder ) {
00521             $this->_markVouchers( $oBasket, $oUser );
00522         }
00523 
00524         // send order by email to shop owner and current user
00525         // skipping this action in case of order recalculation
00526         if ( !$blRecalculatingOrder ) {
00527             $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
00528         } else {
00529             $iRet = self::ORDER_STATE_OK;
00530         }
00531 
00532         return $iRet;
00533     }
00534 
00542     protected function _setOrderStatus( $sStatus )
00543     {
00544         $oDb = oxDb::getDb();
00545         $sQ = 'update oxorder set oxtransstatus='.$oDb->quote( $sStatus ).' where oxid='.$oDb->quote( $this->getId() );
00546         $oDb->execute( $sQ );
00547 
00548         //updating order object
00549         $this->oxorder__oxtransstatus = new oxField( $sStatus, oxField::T_RAW );
00550     }
00551 
00559     protected function _convertVat( $sVat )
00560     {
00561         if ( strpos( $sVat, '.' ) < strpos( $sVat, ',' ) ) {
00562             $sVat = str_replace( array( '.', ',' ), array( '', '.' ), $sVat );
00563         } else {
00564             $sVat = str_replace( ',', '', $sVat );
00565         }
00566         return (float) $sVat;
00567     }
00568 
00579     protected function _loadFromBasket( oxBasket $oBasket )
00580     {
00581         $myConfig = $this->getConfig();
00582 
00583         // store IP Adress - default must be FALSE as it is illegal to store
00584         if ( $myConfig->getConfigParam( 'blStoreIPs' ) &&  $this->oxorder__oxip->value === null ) {
00585             $this->oxorder__oxip = new oxField(oxUtilsServer::getInstance()->getRemoteAddress(), oxField::T_RAW);
00586         }
00587 
00588         // copying main price info
00589         $this->oxorder__oxtotalnetsum   = new oxField(oxUtils::getInstance()->fRound($oBasket->getDiscountedNettoPrice()), oxField::T_RAW);
00590         $this->oxorder__oxtotalbrutsum  = new oxField($oBasket->getProductsPrice()->getBruttoSum(), oxField::T_RAW);
00591         $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00592 
00593         // copying discounted VAT info
00594         $iVatIndex = 1;
00595         foreach ( $oBasket->getProductVats(false) as $iVat => $dPrice ) {
00596             $this->{"oxorder__oxartvat$iVatIndex"}      = new oxField( $this->_convertVat( $iVat ), oxField::T_RAW);
00597             $this->{"oxorder__oxartvatprice$iVatIndex"} = new oxField($dPrice, oxField::T_RAW);
00598             $iVatIndex ++;
00599         }
00600 
00601         // payment costs if available
00602         if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00603             $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00604             $this->oxorder__oxpayvat  = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00605         }
00606 
00607         // delivery info
00608         if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00609             $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00610             //V #M382: Save VAT, not VAT value for delivery costs
00611             $this->oxorder__oxdelvat  = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW); //V #M382
00612             $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00613         }
00614 
00615         // user remark
00616         if ( !isset( $this->oxorder__oxremark ) || $this->oxorder__oxremark->value === null ) {
00617             $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00618         }
00619 
00620         // currency
00621         $oCur = $myConfig->getActShopCurrencyObject();
00622         $this->oxorder__oxcurrency = new oxField($oCur->name);
00623         $this->oxorder__oxcurrate  = new oxField($oCur->rate, oxField::T_RAW);
00624 
00625         // store voucherdiscount
00626         if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00627             $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00628         }
00629 
00630         // general discount
00631         if ( $this->_blReloadDiscount ) {
00632             $dDiscount = 0;
00633             $aDiscounts = $oBasket->getDiscounts();
00634             if ( count($aDiscounts) > 0 ) {
00635                 foreach ($aDiscounts as $oDiscount) {
00636                     $dDiscount += $oDiscount->dDiscount;
00637                 }
00638             }
00639             $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00640         }
00641 
00642         //order language
00643         $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00644 
00645 
00646         // initial status - 'ERROR'
00647         $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00648 
00649         // copies basket product info ...
00650         $this->_setOrderArticles( $oBasket->getContents() );
00651 
00652         // copies wrapping info
00653         $this->_setWrapping( $oBasket );
00654 
00655         // copies TS protection info
00656         $this->_setTsProtection( $oBasket );
00657     }
00658 
00665     public function getOrderLanguage()
00666     {
00667         if ( $this->_iOrderLang === null ) {
00668             if ( isset( $this->oxorder__oxlang->value ) ) {
00669                 $this->_iOrderLang = oxLang::getInstance()->validateLanguage( $this->oxorder__oxlang->value );
00670             } else {
00671                 $this->_iOrderLang = oxLang::getInstance()->getBaseLanguage();
00672             }
00673         }
00674         return $this->_iOrderLang;
00675     }
00676 
00684     protected function _setUser( $oUser )
00685     {
00686 
00687         $this->oxorder__oxuserid        = new oxField($oUser->getId());
00688 
00689         // bill address
00690         $this->oxorder__oxbillcompany     = clone $oUser->oxuser__oxcompany;
00691         $this->oxorder__oxbillemail       = clone $oUser->oxuser__oxusername;
00692         $this->oxorder__oxbillfname       = clone $oUser->oxuser__oxfname;
00693         $this->oxorder__oxbilllname       = clone $oUser->oxuser__oxlname;
00694         $this->oxorder__oxbillstreet      = clone $oUser->oxuser__oxstreet;
00695         $this->oxorder__oxbillstreetnr    = clone $oUser->oxuser__oxstreetnr;
00696         $this->oxorder__oxbilladdinfo     = clone $oUser->oxuser__oxaddinfo;
00697         $this->oxorder__oxbillustid       = clone $oUser->oxuser__oxustid;
00698         $this->oxorder__oxbillcity        = clone $oUser->oxuser__oxcity;
00699         $this->oxorder__oxbillcountryid   = clone $oUser->oxuser__oxcountryid;
00700         $this->oxorder__oxbillstateid     = clone $oUser->oxuser__oxstateid;
00701         $this->oxorder__oxbillzip         = clone $oUser->oxuser__oxzip;
00702         $this->oxorder__oxbillfon         = clone $oUser->oxuser__oxfon;
00703         $this->oxorder__oxbillfax         = clone $oUser->oxuser__oxfax;
00704         $this->oxorder__oxbillsal         = clone $oUser->oxuser__oxsal;
00705 
00706 
00707         // delivery address
00708         if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00709             // set delivery address
00710             $this->oxorder__oxdelcompany   = clone $oDelAdress->oxaddress__oxcompany;
00711             $this->oxorder__oxdelfname     = clone $oDelAdress->oxaddress__oxfname;
00712             $this->oxorder__oxdellname     = clone $oDelAdress->oxaddress__oxlname;
00713             $this->oxorder__oxdelstreet    = clone $oDelAdress->oxaddress__oxstreet;
00714             $this->oxorder__oxdelstreetnr  = clone $oDelAdress->oxaddress__oxstreetnr;
00715             $this->oxorder__oxdeladdinfo   = clone $oDelAdress->oxaddress__oxaddinfo;
00716             $this->oxorder__oxdelcity      = clone $oDelAdress->oxaddress__oxcity;
00717             $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00718             $this->oxorder__oxdelstateid   = clone $oDelAdress->oxaddress__oxstateid;
00719             $this->oxorder__oxdelzip       = clone $oDelAdress->oxaddress__oxzip;
00720             $this->oxorder__oxdelfon       = clone $oDelAdress->oxaddress__oxfon;
00721             $this->oxorder__oxdelfax       = clone $oDelAdress->oxaddress__oxfax;
00722             $this->oxorder__oxdelsal       = clone $oDelAdress->oxaddress__oxsal;
00723         }
00724     }
00725 
00733     protected function _setWrapping( oxBasket $oBasket )
00734     {
00735         $myConfig = $this->getConfig();
00736 
00737         // wrapping price
00738         if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00739             $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00740 
00741             // wrapping VAT will be always calculated (#3757)
00742             $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00743         }
00744 
00745         // greetings card
00746         $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00747 
00748         // card text will be stored in database
00749         $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00750     }
00751 
00760     protected function _setOrderArticles( $aArticleList )
00761     {
00762         // reset articles list
00763         $this->_oArticles = oxNew( 'oxlist' );
00764         $iCurrLang = $this->getOrderLanguage();
00765 
00766         // add all the products we have on basket to the order
00767         foreach ( $aArticleList as $oContent ) {
00768 
00769             //$oContent->oProduct = $oContent->getArticle();
00770             // #M773 Do not use article lazy loading on order save
00771             $oProduct = $oContent->getArticle( true, null, true);
00772 
00773             // copy only if object is oxarticle type
00774             if ( $oProduct->isOrderArticle() ) {
00775                 $oOrderArticle = $oProduct;
00776             } else {
00777 
00778                 // if order language doe not match product language - article must be reloaded in order language
00779                 if ( $iCurrLang != $oProduct->getLanguage() ) {
00780                     $oProduct->loadInLang( $iCurrLang, $oProduct->getProductId() );
00781                 }
00782 
00783                 // set chosen selectlist
00784                 $sSelList = '';
00785                 if ( count( $aChosenSelList = $oContent->getChosenSelList() ) ) {
00786                     foreach ( $aChosenSelList as $oItem ) {
00787                         if ( $sSelList ) {
00788                             $sSelList .= ", ";
00789                         }
00790                         $sSelList .= "{$oItem->name} : {$oItem->value}";
00791                     }
00792                 }
00793 
00794                 $oOrderArticle = oxNew( 'oxorderarticle' );
00795                 $oOrderArticle->setIsNewOrderItem( true );
00796                 $oOrderArticle->copyThis( $oProduct );
00797                 $oOrderArticle->setId();
00798 
00799                 $oOrderArticle->oxorderarticles__oxartnum     = clone $oProduct->oxarticles__oxartnum;
00800                 $oOrderArticle->oxorderarticles__oxselvariant = new oxField( trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->getRawValue() ), oxField::T_RAW );
00801                 $oOrderArticle->oxorderarticles__oxshortdesc  = new oxField( $oProduct->oxarticles__oxshortdesc->getRawValue(), oxField::T_RAW );
00802                 // #M974: duplicated entries for the name of variants in orders
00803                 $oOrderArticle->oxorderarticles__oxtitle      = new oxField( trim( $oProduct->oxarticles__oxtitle->getRawValue() ), oxField::T_RAW );
00804 
00805                 // copying persistent parameters ...
00806                 if ( !is_array( $aPersParams = $oProduct->getPersParams() ) ) {
00807                     $aPersParams = $oContent->getPersParams();
00808                 }
00809                 if ( is_array( $aPersParams ) && count( $aPersParams )) {
00810                     $oOrderArticle->oxorderarticles__oxpersparam = new oxField( serialize( $aPersParams ), oxField::T_RAW );
00811                 }
00812             }
00813 
00814             // ids, titles, numbers ...
00815             $oOrderArticle->oxorderarticles__oxorderid = new oxField( $this->getId() );
00816             $oOrderArticle->oxorderarticles__oxartid   = new oxField( $oContent->getProductId() );
00817             $oOrderArticle->oxorderarticles__oxamount  = new oxField( $oContent->getAmount() );
00818 
00819             // prices
00820             $oPrice = $oContent->getPrice();
00821             $oOrderArticle->oxorderarticles__oxnetprice  = new oxField( $oPrice->getNettoPrice(), oxField::T_RAW );
00822             $oOrderArticle->oxorderarticles__oxvatprice  = new oxField( $oPrice->getVatValue(), oxField::T_RAW );
00823             $oOrderArticle->oxorderarticles__oxbrutprice = new oxField( $oPrice->getBruttoPrice(), oxField::T_RAW );
00824             $oOrderArticle->oxorderarticles__oxvat       = new oxField( $oPrice->getVat(), oxField::T_RAW );
00825 
00826             $oUnitPtice = $oContent->getUnitPrice();
00827             $oOrderArticle->oxorderarticles__oxnprice = new oxField( $oUnitPtice->getNettoPrice(), oxField::T_RAW );
00828             $oOrderArticle->oxorderarticles__oxbprice = new oxField( $oUnitPtice->getBruttoPrice(), oxField::T_RAW );
00829 
00830             // wrap id
00831             $oOrderArticle->oxorderarticles__oxwrapid = new oxField( $oContent->getWrappingId(), oxField::T_RAW );
00832 
00833             // items shop id
00834             $oOrderArticle->oxorderarticles__oxordershopid = new oxField( $oContent->getShopId(), oxField::T_RAW );
00835 
00836             // bundle?
00837             $oOrderArticle->oxorderarticles__oxisbundle = new oxField( $oContent->isBundle() );
00838 
00839             // add information for eMail
00840             //P
00841             //TODO: check if this assign is needed at all
00842             $oOrderArticle->oProduct = $oProduct;
00843 
00844             $oOrderArticle->setArticle( $oProduct );
00845 
00846             // simulatin order article list
00847             $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
00848         }
00849     }
00850 
00862     protected function _executePayment( oxBasket $oBasket, $oUserpayment )
00863     {
00864         $oPayTransaction = $this->_getGateway();
00865         $oPayTransaction->setPaymentParams( $oUserpayment );
00866 
00867         if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
00868             $this->delete();
00869 
00870             // checking for error messages
00871             if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
00872                 if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
00873                     return $sLastError;
00874                 }
00875             }
00876 
00877             // checking for error codes
00878             if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
00879                 if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
00880                     return $iLastErrorNo;
00881                 }
00882             }
00883 
00884             return self::ORDER_STATE_PAYMENTERROR; // means no authentication
00885         }
00886         return true; // everything fine
00887     }
00888 
00895     protected function _getGateway()
00896     {
00897         return oxNew( 'oxPaymentGateway' );
00898     }
00899 
00907     protected function _setPayment( $sPaymentid )
00908     {
00909         // copying payment info fields
00910         $aDynvalue = oxSession::getVar( 'dynvalue' );
00911         $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
00912 
00913         // loading payment object
00914         $oPayment = oxNew( 'oxpayment' );
00915 
00916         if (!$oPayment->load( $sPaymentid )) {
00917             return null;
00918         }
00919 
00920         // #756M Preserve already stored payment information
00921         if ( !$aDynvalue && ( $oUserpayment = $this->getPaymentType() ) ) {
00922             if ( is_array( $aStoredDynvalue = $oUserpayment->getDynValues() ) ) {
00923                 foreach ( $aStoredDynvalue as $oVal ) {
00924                     $aDynvalue[$oVal->name] = $oVal->value;
00925                 }
00926             }
00927         }
00928 
00929         $oPayment->setDynValues( oxUtils::getInstance()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
00930 
00931         // collecting dynamic values
00932         $aDynVal = array();
00933 
00934         if ( is_array( $aPaymentDynValues = $oPayment->getDynValues() ) ) {
00935             foreach ( $aPaymentDynValues  as $key => $oVal ) {
00936                 if ( isset( $aDynvalue[$oVal->name] ) ) {
00937                     $oVal->value = $aDynvalue[$oVal->name];
00938                 }
00939 
00940                 //$oPayment->setDynValue($key, $oVal);
00941                 $aPaymentDynValues[$key] = $oVal;
00942                 $aDynVal[$oVal->name] = $oVal->value;
00943             }
00944         }
00945 
00946         // Store this payment information, we might allow users later to
00947         // reactivate already give payment informations
00948 
00949         $oUserpayment = oxNew( 'oxuserpayment' );
00950         $oUserpayment->oxuserpayments__oxuserid     = clone $this->oxorder__oxuserid;
00951         $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
00952         $oUserpayment->oxuserpayments__oxvalue      = new oxField(oxUtils::getInstance()->assignValuesToText( $aDynVal ), oxField::T_RAW);
00953         $oUserpayment->oxpayments__oxdesc           = clone $oPayment->oxpayments__oxdesc;
00954         $oUserpayment->oxpayments__oxlongdesc       = clone $oPayment->oxpayments__oxlongdesc;
00955         $oUserpayment->setDynValues( $aPaymentDynValues );
00956         $oUserpayment->save();
00957 
00958         // storing payment information to order
00959         $this->oxorder__oxpaymentid   = new oxField($oUserpayment->getId(), oxField::T_RAW);
00960         $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
00961 
00962         // returning user payment object which will be used later in code ...
00963         return $oUserpayment;
00964     }
00965 
00971     protected function _setFolder()
00972     {
00973         $myConfig = $this->getConfig();
00974         $this->oxorder__oxfolder    = new oxField(key( $myConfig->getShopConfVar(  'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
00975     }
00976 
00986     protected function _updateWishlist( $aArticleList, $oUser )
00987     {
00988 
00989         foreach ( $aArticleList as $oContent) {
00990             if ( ( $sWishId = $oContent->getWishId() ) ) {
00991 
00992                 // checking which wishlist user uses ..
00993                 if ( $sWishId == $oUser->getId() ) {
00994                     $oUserBasket = $oUser->getBasket( 'wishlist' );
00995                 } else {
00996                     $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
00997                     $oUserBasket = oxNew( 'oxuserbasket' );
00998                     $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
00999                 }
01000 
01001                 // updating users wish list
01002                 if ( $oUserBasket ) {
01003                     if ( !($sProdId = $oContent->getWishArticleId() )) {
01004                         $sProdId = $oContent->getProductId();
01005                     }
01006                     $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
01007                     $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
01008                     if ( $dNewAmount < 0) {
01009                         $dNewAmount = 0;
01010                     }
01011                     $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
01012                 }
01013             }
01014         }
01015     }
01016 
01026     protected function _updateNoticeList( $aArticleList, $oUser )
01027     {
01028         // loading users notice list ..
01029         if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
01030             // only if wishlist is enabled
01031             foreach ( $aArticleList as $oContent) {
01032                 $sProdId = $oContent->getProductId();
01033 
01034                 // updating users notice list
01035                 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList(), $oContent->getPersParams() );
01036                 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
01037                 if ( $dNewAmount < 0) {
01038                     $dNewAmount = 0;
01039                 }
01040                 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true, $oContent->getPersParams() );
01041             }
01042         }
01043     }
01044     
01050     protected function _updateOrderDate()
01051     {
01052         $oDb = oxDb::getDb();    
01053         $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );        
01054         $sQ = 'update oxorder set oxorderdate=\''.$sDate.'\' where oxid='.$oDb->quote( $this->getId() );
01055         $this->oxorder__oxorderdate = new oxField( $sDate, oxField::T_RAW );
01056         $oDb->execute( $sQ );
01057     }
01058 
01068     protected function _markVouchers( $oBasket, $oUser )
01069     {
01070         $this->_aVoucherList = $oBasket->getVouchers();
01071 
01072         if ( is_array( $this->_aVoucherList ) ) {
01073             foreach ( $this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
01074                 $oVoucher = oxNew( 'oxvoucher' );
01075                 $oVoucher->load( $sVoucherId );
01076                 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->dVoucherdiscount );
01077 
01078                 $this->_aVoucherList[$sVoucherId] = $oVoucher;
01079             }
01080         }
01081     }
01082 
01088     public function save()
01089     {
01090         if ( ( $blSave = parent::save() ) ) {
01091 
01092             // saving order articles
01093             $oOrderArticles = $this->getOrderArticles();
01094             if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
01095                 foreach ( $oOrderArticles as $oOrderArticle ) {
01096                     $oOrderArticle->save();
01097                 }
01098             }
01099         }
01100 
01101         return $blSave;
01102     }
01103 
01110     public function getDelAddressInfo()
01111     {
01112         $oDelAdress = null;
01113         if (! ($soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
01114             $soxAddressId = oxSession::getVar( 'deladrid' );
01115         }
01116         if ( $soxAddressId ) {
01117             $oDelAdress = oxNew( 'oxaddress' );
01118             $oDelAdress->load( $soxAddressId );
01119 
01120             //get delivery country name from delivery country id
01121             if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
01122                  $oCountry = oxNew( 'oxcountry' );
01123                  $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
01124                  $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
01125             }
01126         }
01127         return $oDelAdress;
01128     }
01129 
01140     public function validateStock( $oBasket )
01141     {
01142         foreach ( $oBasket->getContents() as $key => $oContent ) {
01143             try {
01144                 $oProd = $oContent->getArticle();
01145             } catch ( oxNoArticleException $oEx ) {
01146                 $oBasket->removeItem( $key );
01147                 throw $oEx;
01148             } catch ( oxArticleInputException $oEx ) {
01149                 $oBasket->removeItem( $key );
01150                 throw $oEx;
01151             }
01152 
01153             // check if its still available
01154             $dArtStockAmount = $oBasket->getArtStockInBasket( $oProd->getId(), $key );
01155             $iOnStock = $oProd->checkForStock( $oContent->getAmount(), $dArtStockAmount );
01156             if ( $iOnStock !== true ) {
01157                 $oEx = oxNew( 'oxOutOfStockException' );
01158                 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
01159                 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
01160                 $oEx->setProductId( $oProd->getId() );
01161 
01162                 if (!is_numeric($iOnStock)) {
01163                     $iOnStock = 0;
01164                 }
01165                 $oEx->setRemainingAmount( $iOnStock );
01166                 throw $oEx;
01167             }
01168         }
01169     }
01170 
01176     protected function _insert()
01177     {
01178         $myConfig = $this->getConfig();
01179         $oUtilsDate = oxUtilsDate::getInstance();
01180 
01181         //V #M525 orderdate must be the same as it was
01182         if ( !$this->oxorder__oxorderdate->value ) {
01183             $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
01184         } else {
01185             $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
01186         }
01187 
01188         $this->oxorder__oxshopid    = new oxField($myConfig->getShopId(), oxField::T_RAW);
01189         $this->oxorder__oxsenddate  = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01190 
01191         if ( ( $blInsert = parent::_insert() ) ) {
01192             // setting order number
01193             if ( !$this->oxorder__oxordernr->value ) {
01194                $blInsert = $this->_setNumber();
01195             } else {
01196                 oxNew( 'oxCounter' )->update( $this->_getCounterIdent(), $this->oxorder__oxordernr->value );
01197             }
01198         }
01199 
01200         return $blInsert;
01201     }
01202 
01208     protected function _getCounterIdent()
01209     {
01210         $sCounterIdent = ( $this->_blSeparateNumbering ) ? 'oxOrder_' . $this->getConfig()->getShopId() : 'oxOrder';
01211         return $sCounterIdent;
01212     }
01213 
01214 
01220     protected function _setNumber()
01221     {
01222         $oDb = oxDb::getDb();
01223 
01224         $iCnt = oxNew( 'oxCounter' )->getNext( $this->_getCounterIdent() );
01225         $sQ = "update oxorder set oxordernr = $iCnt where oxid = ?";
01226         $blUpdate = ( bool ) $oDb->execute( $sQ, array( $this->getId() ) );
01227 
01228         if ( $blUpdate ) {
01229             $this->oxorder__oxordernr = new oxField( $iCnt );
01230         }
01231 
01232         return $blUpdate;
01233     }
01234 
01240     protected function _update()
01241     {
01242         $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01243         return parent::_update();
01244     }
01245 
01254     public function delete( $sOxId = null )
01255     {
01256         if ( $sOxId ) {
01257             if ( !$this->load( $sOxId ) ) {
01258                 // such order does not exist
01259                 return false;
01260             }
01261         } elseif ( !$sOxId ) {
01262             $sOxId = $this->getId();
01263         }
01264 
01265         // no order id is passed
01266         if ( !$sOxId ) {
01267             return false;
01268         }
01269 
01270 
01271         // delete order articles
01272         $oOrderArticles = $this->getOrderArticles( false );
01273         foreach ( $oOrderArticles as $oOrderArticle ) {
01274             $oOrderArticle->delete();
01275         }
01276 
01277         // #440 - deleting user payment info
01278         if ( $oPaymentType = $this->getPaymentType() ) {
01279             $oPaymentType->delete();
01280         }
01281 
01282         return parent::delete( $sOxId );
01283     }
01284 
01294     public function recalculateOrder( $aNewArticles = array() )
01295     {
01296         oxDb::startTransaction();
01297 
01298         try {
01299             $oBasket = $this->_getOrderBasket();
01300 
01301             // add this order articles to virtual basket and recalculates basket
01302             $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles( true ) );
01303 
01304             // adding new articles to existing order
01305             $this->_addArticlesToBasket( $oBasket, $aNewArticles );
01306 
01307             // recalculating basket
01308             $oBasket->calculateBasket( true );
01309 
01310             //finalizing order (skipping payment execution, vouchers marking and mail sending)
01311             $iRet = $this->finalizeOrder( $oBasket, $this->getOrderUser(), true );
01312 
01313             //if finalizing order failed, rollback transaction
01314             if ( $iRet !== 1 ) {
01315                 oxDb::rollbackTransaction();
01316             } else {
01317                 oxDb::commitTransaction();
01318             }
01319 
01320         } catch( Exception $oE ) {
01321             // if exception, rollBack everything
01322             oxDb::rollbackTransaction();
01323 
01324             if ( defined( 'OXID_PHP_UNIT' ) ) {
01325                 throw $oE;
01326             }
01327         }
01328     }
01329 
01330     protected $_oOrderBasket = null;
01338     protected function _getOrderBasket( $blStockCheck = true )
01339     {
01340         $this->_oOrderBasket = oxNew( "oxbasket" );
01341 
01342         // setting stock check mode
01343         $this->_oOrderBasket->setStockCheckMode( $blStockCheck );
01344 
01345         // setting virtual basket user
01346         $this->_oOrderBasket->setBasketUser( $this->getOrderUser() );
01347 
01348         // transferring order id
01349         $this->_oOrderBasket->setOrderId( $this->getId() );
01350 
01351         // setting basket currency order uses
01352         $aCurrencies = $this->getConfig()->getCurrencyArray();
01353         foreach ( $aCurrencies as $oCur ) {
01354             if ($oCur->name == $this->oxorder__oxcurrency->value) {
01355                 $oBasketCur = $oCur;
01356                 break;
01357             }
01358         }
01359 
01360         // setting currency
01361         $this->_oOrderBasket->setBasketCurrency( $oBasketCur );
01362 
01363         // set basket card id and message
01364         $this->_oOrderBasket->setCardId( $this->oxorder__oxcardid->value );
01365         $this->_oOrderBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01366 
01367         if ( $this->_blReloadDiscount ) {
01368             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01369             // disabling availability check
01370             $this->_oOrderBasket->setSkipVouchersChecking( true );
01371 
01372             // add previously used vouchers
01373             $sQ = 'select oxid from oxvouchers where oxorderid = '.$oDb->quote( $this->getId() );
01374             $aVouchers = $oDb->getAll( $sQ );
01375             foreach ( $aVouchers as $aVoucher ) {
01376                 $this->_oOrderBasket->addVoucher( $aVoucher['oxid'] );
01377             }
01378         } else {
01379             $this->_oOrderBasket->setDiscountCalcMode( false );
01380             $this->_oOrderBasket->setVoucherDiscount( $this->oxorder__oxvoucherdiscount->value );
01381             $this->_oOrderBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
01382         }
01383 
01384         // must be kept old delivery?
01385         if ( !$this->_blReloadDelivery ) {
01386             $this->_oOrderBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01387         } else {
01388             //  set shipping
01389             $this->_oOrderBasket->setShipping( $this->oxorder__oxdeltype->value );
01390             $this->_oOrderBasket->setDeliveryPrice( null );
01391         }
01392 
01393         //set basket payment
01394         $this->_oOrderBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01395 
01396         return $this->_oOrderBasket;
01397     }
01398 
01407     public function setDelivery( $sDeliveryId )
01408     {
01409         $this->reloadDelivery( true );
01410         $this->oxorder__oxdeltype = new oxField( $sDeliveryId );
01411     }
01412 
01418     public function getOrderUser()
01419     {
01420         if ($this->_oUser === null ) {
01421             $this->_oUser = oxNew( "oxuser" );
01422             $this->_oUser->load( $this->oxorder__oxuserid->value );
01423 
01424             // if object is loaded then reusing its order info
01425             if ( $this->_isLoaded ) {
01426                 // bill address
01427                 $this->_oUser->oxuser__oxcompany  = clone $this->oxorder__oxbillcompany;
01428                 $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
01429                 $this->_oUser->oxuser__oxfname    = clone $this->oxorder__oxbillfname;
01430                 $this->_oUser->oxuser__oxlname    = clone $this->oxorder__oxbilllname;
01431                 $this->_oUser->oxuser__oxstreet   = clone $this->oxorder__oxbillstreet;
01432                 $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
01433                 $this->_oUser->oxuser__oxaddinfo  = clone $this->oxorder__oxbilladdinfo;
01434                 $this->_oUser->oxuser__oxustid    = clone $this->oxorder__oxbillustid;
01435 
01436 
01437                 $this->_oUser->oxuser__oxcity      = clone $this->oxorder__oxbillcity;
01438                 $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
01439                 $this->_oUser->oxuser__oxstateid   = clone $this->oxorder__oxbillstateid;
01440                 $this->_oUser->oxuser__oxzip       = clone $this->oxorder__oxbillzip;
01441                 $this->_oUser->oxuser__oxfon       = clone $this->oxorder__oxbillfon;
01442                 $this->_oUser->oxuser__oxfax       = clone $this->oxorder__oxbillfax;
01443                 $this->_oUser->oxuser__oxsal       = clone $this->oxorder__oxbillsal;
01444             }
01445         }
01446 
01447         return $this->_oUser;
01448     }
01449 
01457     public function pdfFooter( $oPdf )
01458     {
01459     }
01460 
01468     public function pdfHeaderplus( $oPdf )
01469     {
01470     }
01471 
01479     public function pdfHeader( $oPdf )
01480     {
01481     }
01482 
01491     public function genPdf( $sFilename, $iSelLang = 0 )
01492     {
01493     }
01494 
01500     public function getInvoiceNum()
01501     {
01502         $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01503         return ( ( int ) oxDb::getDb()->getOne( $sQ, false ) + 1 );
01504     }
01505 
01511     public function getNextBillNum()
01512     {
01513         $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01514         return ( ( int ) oxDb::getDb()->getOne( $sQ, false ) + 1 );
01515     }
01516 
01522     public function getShippingSetList()
01523     {
01524         // in which country we deliver
01525         if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
01526             $sShipId = $this->oxorder__oxbillcountryid->value;
01527         }
01528 
01529         $oBasket = $this->_getOrderBasket( false );
01530 
01531         // unsetting bundles
01532         $oOrderArticles = $this->getOrderArticles();
01533         foreach ( $oOrderArticles as $sItemId => $oItem ) {
01534             if ( $oItem->isBundle() ) {
01535                 $oOrderArticles->offsetUnset( $sItemId );
01536             }
01537         }
01538 
01539         // add this order articles to basket and recalculate basket
01540         $this->_addOrderArticlesToBasket( $oBasket, $oOrderArticles );
01541 
01542         // recalculating basket
01543         $oBasket->calculateBasket( true );
01544 
01545         // load fitting deliveries list
01546         $oDeliveryList = oxNew( "oxDeliveryList", "core" );
01547         $oDeliveryList->setCollectFittingDeliveriesSets( true );
01548 
01549         return $oDeliveryList->getDeliveryList( $oBasket, $this->getOrderUser(), $sShipId );
01550     }
01551 
01557     public function getVoucherNrList()
01558     {
01559         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01560         $aVouchers = array();
01561         $sSelect = "select oxvouchernr from oxvouchers where oxorderid = ".$oDb->quote( $this->oxorder__oxid->value );
01562         $rs = $oDb->select( $sSelect );
01563         if ($rs != false && $rs->recordCount() > 0) {
01564             while (!$rs->EOF) {
01565                 $aVouchers[] = $rs->fields['oxvouchernr'];
01566                 $rs->moveNext();
01567             }
01568         }
01569         return $aVouchers;
01570     }
01571 
01579     public function getOrderSum( $blToday = false )
01580     {
01581         $sSelect  = 'select sum(oxtotalordersum / oxcurrate) 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 ( double ) oxDb::getDb()->getOne( $sSelect, false, false );
01589     }
01590 
01598     public function getOrderCnt( $blToday = false )
01599     {
01600         $sSelect  = 'select count(*) from oxorder where ';
01601         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'"  and oxorder.oxstorno != "1" ';
01602 
01603         if ( $blToday ) {
01604             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01605         }
01606 
01607         return ( int ) oxDb::getDb()->getOne( $sSelect, false, false );
01608     }
01609 
01610 
01618     protected function _checkOrderExist( $sOxId = null )
01619     {
01620         if ( !$sOxId) {
01621             return false;
01622         }
01623 
01624         $oDb = oxDb::getDb();
01625         if ( $oDb->getOne( 'select oxid from oxorder where oxid = '.$oDb->quote( $sOxId ), false, false ) ) {
01626             return true;
01627         }
01628 
01629         return false;
01630     }
01631 
01641     protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01642     {
01643         $iRet = self::ORDER_STATE_MAILINGERROR;
01644 
01645         // add user, basket and payment to order
01646         $this->_oUser    = $oUser;
01647         $this->_oBasket  = $oBasket;
01648         $this->_oPayment = $oPayment;
01649 
01650         $oxEmail = oxNew( 'oxemail' );
01651 
01652         // send order email to user
01653         if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01654             // mail to user was successfully sent
01655             $iRet = self::ORDER_STATE_OK;
01656         }
01657 
01658         // send order email to shop owner
01659         $oxEmail->sendOrderEMailToOwner( $this );
01660 
01661         return $iRet;
01662     }
01663 
01669     public function getBasket()
01670     {
01671         return $this->_oBasket;
01672     }
01673 
01679     public function getPayment()
01680     {
01681         return $this->_oPayment;
01682     }
01683 
01689     public function getVoucherList()
01690     {
01691         return $this->_aVoucherList;
01692     }
01693 
01699     public function getDelSet()
01700     {
01701         if ( $this->_oDelSet == null ) {
01702             // load deliveryset info
01703             $this->_oDelSet = oxNew( 'oxdeliveryset' );
01704             $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01705         }
01706 
01707         return $this->_oDelSet;
01708     }
01709 
01715     public function getPaymentType()
01716     {
01717         if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType === null ) {
01718             $this->_oPaymentType = false;
01719             $oPaymentType = oxNew( 'oxuserpayment' );
01720             if ( $oPaymentType->load( $this->oxorder__oxpaymentid->value ) ) {
01721                 $this->_oPaymentType = $oPaymentType;
01722             }
01723         }
01724 
01725         return $this->_oPaymentType;
01726     }
01727 
01733     public function getGiftCard()
01734     {
01735         if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01736             $this->_oGiftCard = oxNew( 'oxwrapping' );
01737             $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01738         }
01739 
01740         return $this->_oGiftCard;
01741     }
01742 
01750     public function setSeparateNumbering( $blSeparateNumbering = null )
01751     {
01752         $this->_blSeparateNumbering = $blSeparateNumbering;
01753     }
01754 
01762     public function getLastUserPaymentType( $sUserId)
01763     {
01764         $oDb = oxDb::getDb();
01765         $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid='.$oDb->quote( $sUserId ).' order by oxorder.oxorderdate desc ';
01766         $sLastPaymentId = $oDb->getOne( $sQ, false, false );
01767         return $sLastPaymentId;
01768     }
01769 
01778     protected function _addOrderArticlesToBasket( $oBasket, $aOrderArticles )
01779     {
01780         // if no order articles, return empty basket
01781         if ( count( $aOrderArticles ) > 0 ) {
01782 
01783             //adding order articles to basket
01784             foreach ( $aOrderArticles as $oOrderArticle ) {
01785                 $oBasket->addOrderArticleToBasket( $oOrderArticle );
01786             }
01787         }
01788     }
01789 
01798     protected function _addArticlesToBasket( $oBasket, $aArticles )
01799     {
01800         // if no order articles
01801         if ( count($aArticles ) > 0 ) {
01802 
01803             //adding order articles to basket
01804             foreach ( $aArticles as $oArticle ) {
01805                 $aSel = isset( $oArticle->oxorderarticles__oxselvariant ) ? $oArticle->oxorderarticles__oxselvariant->value : null;
01806                 $aPersParam = isset( $oArticle->oxorderarticles__oxpersparam ) ? $oArticle->getPersParams() : null;
01807                 $oBasket->addToBasket( $oArticle->oxorderarticles__oxartid->value,
01808                                        $oArticle->oxorderarticles__oxamount->value,
01809                                        $aSel, $aPersParam );
01810             }
01811         }
01812     }
01813 
01819     public function getTotalOrderSum()
01820     {
01821         $oCur = $this->getConfig()->getActShopCurrencyObject();
01822         return number_format( (double)$this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01823     }
01824 
01832     public function getProductVats( $blFormatCurrency = true )
01833     {
01834         $aVats = array();
01835         if ($this->oxorder__oxartvat1->value) {
01836             $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
01837         }
01838         if ($this->oxorder__oxartvat2->value) {
01839             $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
01840         }
01841 
01842         if ( $blFormatCurrency ) {
01843             $oLang = oxLang::getInstance();
01844             $oCur = $this->getConfig()->getActShopCurrencyObject();
01845             foreach ( $aVats as $sKey => $dVat ) {
01846                 $aVats[$sKey] = $oLang->formatCurrency( $dVat, $oCur );
01847             }
01848         }
01849         return $aVats;
01850     }
01851 
01857     public function getBillCountry()
01858     {
01859         if ( !$this->oxorder__oxbillcountry->value ) {
01860             $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
01861         }
01862         return $this->oxorder__oxbillcountry;
01863     }
01864 
01870     public function getDelCountry()
01871     {
01872         if ( !$this->oxorder__oxdelcountry->value ) {
01873             $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
01874         }
01875         return $this->oxorder__oxdelcountry;
01876     }
01884     public function reloadDelivery( $blReload )
01885     {
01886         $this->_blReloadDelivery = $blReload;
01887     }
01888 
01896     public function reloadDiscount( $blReload )
01897     {
01898         $this->_blReloadDiscount = $blReload;
01899     }
01900 
01906     public function cancelOrder()
01907     {
01908         $this->oxorder__oxstorno = new oxField( 1 );
01909         if ( $this->save() ) {
01910 
01911 
01912             // canceling ordered products
01913             foreach ( $this->getOrderArticles() as $oOrderArticle ) {
01914 
01915 
01916                 $oOrderArticle->cancelOrderArticle();
01917             }
01918         }
01919     }
01920 
01927     public function getOrderCurrency()
01928     {
01929         if ( $this->_oOrderCurrency === null ) {
01930 
01931             // setting default in case unrecognized currency was set during order
01932             $aCurrencies = $this->getConfig()->getCurrencyArray();
01933             $this->_oOrderCurrency = current( $aCurrencies );
01934 
01935             foreach ( $aCurrencies as $oCurr ) {
01936                 if ( $oCurr->name == $this->oxorder__oxcurrency->value ) {
01937                     $this->_oOrderCurrency = $oCurr;
01938                     break;
01939                 }
01940             }
01941         }
01942         return $this->_oOrderCurrency;
01943     }
01944 
01954     public function validateOrder( $oBasket, $oUser )
01955     {
01956         // validating stock
01957         $iValidState = $this->validateStock( $oBasket );
01958 
01959         if ( !$iValidState ) {
01960             // validating delivery
01961             $iValidState = $this->validateDelivery( $oBasket );
01962         }
01963 
01964         if ( !$iValidState ) {
01965             // validating payment
01966             $iValidState = $this->validatePayment( $oBasket );
01967         }
01968 
01969         if ( !$iValidState ) {
01970             //0003110 validating delivewry address, it is not be changed during checkout process
01971             $iValidState = $this->validateDeliveryAddress( $oUser );
01972         }
01973 
01974         if ( !$iValidState ) {
01975             // validatign minimum price
01976             $iValidState = $this->validateBasket( $oBasket );
01977         }
01978         return $iValidState;
01979     }
01980 
01988     public function validateBasket( $oBasket )
01989     {
01990         return $oBasket->isBelowMinOrderPrice() ? self::ORDER_STATE_BELOWMINPRICE : null;
01991     }
01992 
02001     public function validateDeliveryAddress( $oUser )
02002     {
02003         $sDelAddressMD5 =  oxConfig::getParameter( 'sDeliveryAddressMD5' );
02004 
02005         // bill address
02006         $sDelAddress = '';
02007         $sDelAddress .= $oUser->oxuser__oxcompany;
02008         $sDelAddress .= $oUser->oxuser__oxusername;
02009         $sDelAddress .= $oUser->oxuser__oxfname;
02010         $sDelAddress .= $oUser->oxuser__oxlname;
02011         $sDelAddress .= $oUser->oxuser__oxstreet;
02012         $sDelAddress .= $oUser->oxuser__oxstreetnr;
02013         $sDelAddress .= $oUser->oxuser__oxaddinfo;
02014         $sDelAddress .= $oUser->oxuser__oxustid;
02015         $sDelAddress .= $oUser->oxuser__oxcity;
02016         $sDelAddress .= $oUser->oxuser__oxcountryid;
02017         $sDelAddress .= $oUser->oxuser__oxstateid;
02018         $sDelAddress .= $oUser->oxuser__oxzip;
02019         $sDelAddress .= $oUser->oxuser__oxfon;
02020         $sDelAddress .= $oUser->oxuser__oxfax;
02021         $sDelAddress .= $oUser->oxuser__oxsal;
02022 
02023         // delivery address
02024         if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
02025             // set delivery address
02026             $sDelAddress .= $oDelAdress->oxaddress__oxcompany;
02027             $sDelAddress .= $oDelAdress->oxaddress__oxfname;
02028             $sDelAddress .= $oDelAdress->oxaddress__oxlname;
02029             $sDelAddress .= $oDelAdress->oxaddress__oxstreet;
02030             $sDelAddress .= $oDelAdress->oxaddress__oxstreetnr;
02031             $sDelAddress .= $oDelAdress->oxaddress__oxaddinfo;
02032             $sDelAddress .= $oDelAdress->oxaddress__oxcity;
02033             $sDelAddress .= $oDelAdress->oxaddress__oxcountryid;
02034             $sDelAddress .= $oDelAdress->oxaddress__oxstateid;
02035             $sDelAddress .= $oDelAdress->oxaddress__oxzip;
02036             $sDelAddress .= $oDelAdress->oxaddress__oxfon;
02037             $sDelAddress .= $oDelAdress->oxaddress__oxfax;
02038             $sDelAddress .= $oDelAdress->oxaddress__oxsal;
02039         }
02040 
02041         if ($sDelAddressMD5 != md5($sDelAddress)) {
02042             return self::ORDER_STATE_INVALIDDElADDRESSCHANGED;
02043         }
02044 
02045         return;
02046     }
02047 
02048 
02049 
02058     public function validateDelivery( $oBasket )
02059     {
02060         // proceed with no delivery
02061         // used for other countries
02062         if ( $oBasket->getPaymentId() == 'oxempty') {
02063             return;
02064         }
02065         $oDb = oxDb::getDb();
02066 
02067         $oDelSet = oxNew( "oxdeliveryset" );
02068         $sTable = $oDelSet->getViewName();
02069 
02070         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
02071         $oDb->quote( $oBasket->getShippingId() )." and ".$oDelSet->getSqlActiveSnippet();
02072 
02073         if ( !$oDb->getOne( $sQ, false, false ) ) {
02074             // throwing exception
02075             return self::ORDER_STATE_INVALIDDELIVERY;
02076         }
02077     }
02078 
02087     public function validatePayment( $oBasket )
02088     {
02089         $oDb = oxDb::getDb();
02090 
02091         $oPayment = oxNew( "oxpayment" );
02092         $sTable = $oPayment->getViewName();
02093 
02094         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
02095         $oDb->quote( $oBasket->getPaymentId() )." and ".$oPayment->getSqlActiveSnippet();
02096 
02097         if ( !$oDb->getOne( $sQ, false, false ) ) {
02098             return self::ORDER_STATE_INVALIDPAYMENT;
02099         }
02100     }
02101 
02109     protected function _setTsProtection( oxBasket $oBasket )
02110     {
02111         // protection price
02112         if ( ( $oTsProtectionCost = $oBasket->getCosts( 'oxtsprotection' ) ) ) {
02113             $this->oxorder__oxtsprotectcosts = new oxField($oTsProtectionCost->getBruttoPrice(), oxField::T_RAW);
02114         }
02115 
02116         // protection protduct id
02117         $this->oxorder__oxtsprotectid = new oxField($oBasket->getTsProductId(), oxField::T_RAW);
02118     }
02119 
02128     protected function _executeTsProtection( oxBasket $oBasket )
02129     {
02130         $aValues['tsProductId'] = $this->oxorder__oxtsprotectid->value;
02131         $aValues['amount'] = $oBasket->getPrice()->getBruttoPrice();
02132         $oCur = $this->getConfig()->getActShopCurrencyObject();
02133         $aValues['currency'] = $oCur->name;
02134         $aValues['buyerEmail'] = $this->oxorder__oxbillemail->value;
02135         $aValues['shopCustomerID'] = $this->oxorder__oxuserid->value;
02136         $aValues['shopOrderID'] = $this->oxorder__oxordernr->value;
02137         $aValues['orderDate'] = $this->oxorder__oxorderdate->value;
02138         $sPaymentId = $oBasket->getPaymentId();
02139         $oTsProtection = oxNew('oxtsprotection');
02140         $blRes = $oTsProtection->requestForTsProtection( $aValues, $sPaymentId );
02141         /*if ( !$blRes ) {
02142             $this->delete();
02143             return self::ORDER_STATE_INVALIDTSPROTECTION;
02144         }*/
02145         return true; // everything fine
02146     }
02147 
02153     public function getFormattedTotalNetSum()
02154     {
02155         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalnetsum->value, $this->getOrderCurrency() );
02156     }
02157 
02163     public function getFormattedTotalBrutSum()
02164     {
02165         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $this->getOrderCurrency() );
02166     }
02167 
02173     public function getFormattedeliveryCost()
02174     {
02175         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxdelcost->value, $this->getOrderCurrency() );
02176     }
02177 
02183     public function getFormattedPayCost()
02184     {
02185        return oxLang::getInstance()->formatCurrency( $this->oxorder__oxpaycost->value, $this->getOrderCurrency() );
02186     }
02187 
02193     public function getFormattedWrapCost()
02194     {
02195         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxwrapcost->value, $this->getOrderCurrency() );
02196     }
02197 
02203     public function getFormattedTotalVouchers()
02204     {
02205         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $this->getOrderCurrency() );
02206     }
02207 
02213     public function getFormattedDiscount()
02214     {
02215         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxdiscount->value, $this->getOrderCurrency() );
02216     }
02217 
02223     public function getFormattedTotalOrderSum()
02224     {
02225         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalordersum->value, $this->getOrderCurrency() );
02226     }
02227 
02233     public function getShipmentTrackingUrl()
02234     {
02235         if ( $this->_sShipTrackUrl === null && $this->oxorder__oxtrackcode->value ) {
02236             $this->_sShipTrackUrl = "http://www.dpd.de/cgi-bin/delistrack?typ=1&amp;lang=de&amp;pknr=".$this->oxorder__oxtrackcode->value;
02237         }
02238 
02239         return $this->_sShipTrackUrl;
02240     }
02241 
02242 }