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 
00057     protected $_aSkipSaveFields = array( 'oxorderdate' );
00058 
00064     protected $_oArticles = null;
00065 
00071     protected $_oDelSet   = null;
00072 
00078     protected $_oGiftCard = null;
00079 
00085     protected $_oPaymentType = null;
00086 
00092     protected $_oPayment = null;
00093 
00099     protected $_aVoucherList = null;
00100 
00106     protected $_oDelPrice = null;
00107 
00113     protected $_oUser = null;
00114 
00120     protected $_oBasket = null;
00121 
00127     protected $_oWrappingPrice = null;
00128 
00134     protected $_oPaymentPrice = null;
00135 
00141     protected $_oTsProtectionPrice = null;
00142 
00148     protected $_sClassName = 'oxorder';
00149 
00155     protected $_blSeparateNumbering = null;
00156 
00162     protected $_iOrderLang = null;
00163 
00169     protected $_blReloadDelivery = true;
00170 
00176     protected $_blReloadDiscount = true;
00177 
00183     protected $_oOrderCurrency = null;
00184 
00188     public function __construct()
00189     {
00190         parent::__construct();
00191         $this->init( 'oxorder' );
00192 
00193         // set usage of seperate orders numbering for different shops
00194         $this->setSeparateNumbering( $this->getConfig()->getConfigParam( 'blSeparateNumbering') );
00195 
00196     }
00197 
00205     public function __get( $sName )
00206     {
00207         if ( $sName == 'oDelSet' ) {
00208             return $this->getDelSet();
00209         }
00210 
00211         if ( $sName == 'oxorder__oxbillcountry' ) {
00212             return $this->getBillCountry();
00213         }
00214 
00215         if ( $sName == 'oxorder__oxdelcountry' ) {
00216             return $this->getDelCountry();
00217         }
00218     }
00219 
00227     public function assign( $dbRecord )
00228     {
00229 
00230         parent::assign( $dbRecord );
00231 
00232         $oUtilsDate = oxUtilsDate::getInstance();
00233 
00234         // convert date's to international format
00235         $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value));
00236         $this->oxorder__oxsenddate  = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value));
00237     }
00238 
00246     protected function _getCountryTitle( $sCountryId )
00247     {
00248         $sTitle = null;
00249         if ( $sCountryId && $sCountryId != '-1' ) {
00250             $oCountry = oxNew( 'oxcountry' );
00251             $oCountry->loadInLang( $this->getOrderLanguage(), $sCountryId );
00252             $sTitle = $oCountry->oxcountry__oxtitle->value;
00253         }
00254 
00255         return $sTitle;
00256     }
00257 
00265     public function getOrderArticles( $blExcludeCanceled = false )
00266     {
00267         // checking set value
00268         if ( $this->_oArticles === null  ) {
00269             $sTable = getViewName( "oxorderarticles" );
00270             $sSelect = "select {$sTable}.* from {$sTable}
00271                         where {$sTable}.oxorderid = '".$this->getId() . "'" .
00272                         ( $blExcludeCanceled ? " and {$sTable}.oxstorno != 1 ": " " ) ."
00273                         order by {$sTable}.oxartid";
00274 
00275             // order articles
00276             $oArticles = oxNew( 'oxlist' );
00277             $oArticles->init( 'oxorderarticle' );
00278             $oArticles->selectString( $sSelect );
00279 
00280             // is value was not set, just returning it
00281             return $oArticles;
00282         }
00283         return $this->_oArticles;
00284     }
00285 
00293     public function setOrderArticleList( $aOrderArticleList )
00294     {
00295         $this->_oArticles = $aOrderArticleList;
00296     }
00297 
00303     public function getOrderDeliveryPrice()
00304     {
00305         if ( $this->_oDelPrice != null ) {
00306             return $this->_oDelPrice;
00307         }
00308 
00309         $this->_oDelPrice = oxNew( 'oxprice' );
00310         $this->_oDelPrice->setBruttoPriceMode();
00311         $this->_oDelPrice->setPrice( $this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value );
00312         return $this->_oDelPrice;
00313     }
00314 
00320     public function getOrderWrappingPrice()
00321     {
00322         if ( $this->_oWrappingPrice != null ) {
00323             return $this->_oWrappingPrice;
00324         }
00325 
00326         $this->_oWrappingPrice = oxNew( 'oxprice' );
00327         $this->_oWrappingPrice->setBruttoPriceMode();
00328         $this->_oWrappingPrice->setPrice( $this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value );
00329         return $this->_oWrappingPrice;
00330     }
00331 
00337     public function getOrderPaymentPrice()
00338     {
00339         if ( $this->_oPaymentPrice != null ) {
00340             return $this->_oPaymentPrice;
00341         }
00342 
00343         $this->_oPaymentPrice = oxNew( 'oxprice' );
00344         $this->_oPaymentPrice->setBruttoPriceMode();
00345         $this->_oPaymentPrice->setPrice( $this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value );
00346         return $this->_oPaymentPrice;
00347     }
00348 
00354     public function getOrderTsProtectionPrice()
00355     {
00356         if ( $this->_oTsProtectionPrice != null ) {
00357             return $this->_oTsProtectionPrice;
00358         }
00359 
00360         $this->_oTsProtectionPrice = oxNew( 'oxprice' );
00361         $this->_oTsProtectionPrice->setBruttoPriceMode();
00362         $this->_oTsProtectionPrice->setPrice( $this->oxorder__oxtsprotectcosts->value, $this->getConfig()->getConfigParam( 'dDefaultVAT' ) );
00363         return $this->_oTsProtectionPrice;
00364     }
00365 
00372     public function getOrderNetSum()
00373     {
00374         $dTotalNetSum = 0;
00375 
00376         $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
00377         $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
00378         $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
00379         $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
00380 
00381         return $dTotalNetSum;
00382     }
00383 
00404     public function finalizeOrder( oxBasket $oBasket, $oUser, $blRecalculatingOrder = false )
00405     {
00406         // check if this order is already stored
00407         $sGetChallenge = oxSession::getVar( 'sess_challenge' );
00408         if ( $this->_checkOrderExist( $sGetChallenge ) ) {
00409             oxUtils::getInstance()->logger( 'BLOCKER' );
00410             // we might use this later, this means that somebody klicked like mad on order button
00411             return self::ORDER_STATE_ORDEREXISTS;
00412         }
00413 
00414         // if not recalculating order, use sess_challenge id, else leave old order id
00415         if ( !$blRecalculatingOrder ) {
00416             // use this ID
00417             $this->setId( $sGetChallenge );
00418 
00419             // validating various order/basket parameters before finalizing
00420             if ( $iOrderState = $this->validateOrder( $oBasket, $oUser ) ) {
00421                 return $iOrderState;
00422             }
00423         }
00424 
00425         // copies user info
00426         $this->_setUser( $oUser );
00427 
00428         // copies basket info
00429         $this->_loadFromBasket( $oBasket );
00430 
00431         // payment information
00432         $oUserPayment = $this->_setPayment( $oBasket->getPaymentId() );
00433 
00434         // set folder information, if order is new
00435         // #M575 in recalcualting order case folder must be the same as it was
00436         if ( !$blRecalculatingOrder ) {
00437             $this->_setFolder();
00438         }
00439 
00440         //saving all order data to DB
00441         $this->save();
00442 
00443         // executing payment (on failure deletes order and returns error code)
00444         // in case when recalcualting order, payment execution is skipped
00445         if ( !$blRecalculatingOrder ) {
00446             $blRet = $this->_executePayment( $oBasket, $oUserPayment );
00447             if ( $blRet !== true ) {
00448                 return $blRet;
00449             }
00450         }
00451 
00452         // executing TS protection
00453         if ( !$blRecalculatingOrder && $oBasket->getTsProductId()) {
00454             $blRet = $this->_executeTsProtection( $oBasket );
00455             if ( $blRet !== true ) {
00456                 return $blRet;
00457             }
00458         }
00459 
00460         // deleting remark info only when order is finished
00461         oxSession::deleteVar( 'ordrem' );
00462         oxSession::deleteVar( 'stsprotection' );
00463 
00464         // updating order trans status (success status)
00465         $this->_setOrderStatus( 'OK' );
00466 
00467         // store orderid
00468         $oBasket->setOrderId( $this->getId() );
00469 
00470         // updating wish lists
00471         $this->_updateWishlist( $oBasket->getContents(), $oUser );
00472 
00473         // updating users notice list
00474         $this->_updateNoticeList( $oBasket->getContents(), $oUser );
00475 
00476         // marking vouchers as used and sets them to $this->_aVoucherList (will be used in order email)
00477         // skipping this action in case of order recalculation
00478         if ( !$blRecalculatingOrder ) {
00479             $this->_markVouchers( $oBasket, $oUser );
00480         }
00481 
00482         // send order by email to shop owner and current user
00483         // skipping this action in case of order recalculation
00484         if ( !$blRecalculatingOrder ) {
00485             $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
00486         } else {
00487             $iRet = self::ORDER_STATE_OK;
00488         }
00489 
00490         return $iRet;
00491     }
00492 
00500     protected function _setOrderStatus( $sStatus )
00501     {
00502         $oDb = oxDb::getDb();
00503         $sQ = 'update oxorder set oxtransstatus='.$oDb->quote( $sStatus ).' where oxid='.$oDb->quote( $this->getId() );
00504         $oDb->execute( $sQ );
00505 
00506         //updating order object
00507         $this->oxorder__oxtransstatus = new oxField( $sStatus, oxField::T_RAW );
00508     }
00509 
00517     protected function _convertVat( $sVat )
00518     {
00519         if ( strpos( $sVat, '.' ) < strpos( $sVat, ',' ) ) {
00520             $sVat = str_replace( array( '.', ',' ), array( '', '.' ), $sVat );
00521         } else {
00522             $sVat = str_replace( ',', '', $sVat );
00523         }
00524         return (float) $sVat;
00525     }
00526 
00537     protected function _loadFromBasket( oxBasket $oBasket )
00538     {
00539         $myConfig = $this->getConfig();
00540 
00541         // store IP Adress - default must be FALSE as it is illegal to store
00542         if ( $myConfig->getConfigParam( 'blStoreIPs' ) &&  $this->oxorder__oxip->value === null ) {
00543             $this->oxorder__oxip = new oxField(oxUtilsServer::getInstance()->getRemoteAddress(), oxField::T_RAW);
00544         }
00545 
00546         // copying main price info
00547         $this->oxorder__oxtotalnetsum   = new oxField(oxUtils::getInstance()->fRound($oBasket->getDiscountedNettoPrice()), oxField::T_RAW);
00548         $this->oxorder__oxtotalbrutsum  = new oxField($oBasket->getProductsPrice()->getBruttoSum(), oxField::T_RAW);
00549         $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00550 
00551         // copying discounted VAT info
00552         $iVatIndex = 1;
00553         foreach ( $oBasket->getProductVats(false) as $iVat => $dPrice ) {
00554             $this->{"oxorder__oxartvat$iVatIndex"}      = new oxField( $this->_convertVat( $iVat ), oxField::T_RAW);
00555             $this->{"oxorder__oxartvatprice$iVatIndex"} = new oxField($dPrice, oxField::T_RAW);
00556             $iVatIndex ++;
00557         }
00558 
00559         // payment costs if available
00560         if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00561             $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00562             $this->oxorder__oxpayvat  = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00563         }
00564 
00565         // delivery info
00566         if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00567             $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00568             //V #M382: Save VAT, not VAT value for delivery costs
00569             $this->oxorder__oxdelvat  = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW); //V #M382
00570             $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00571         }
00572 
00573         // user remark
00574         if ( !isset( $this->oxorder__oxremark ) || $this->oxorder__oxremark->value === null ) {
00575             $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00576         }
00577 
00578         // currency
00579         $oCur = $myConfig->getActShopCurrencyObject();
00580         $this->oxorder__oxcurrency = new oxField($oCur->name);
00581         $this->oxorder__oxcurrate  = new oxField($oCur->rate, oxField::T_RAW);
00582 
00583         // store voucherdiscount
00584         if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00585             $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00586         }
00587 
00588         // general discount
00589         if ( $this->_blReloadDiscount ) {
00590             $dDiscount = 0;
00591             $aDiscounts = $oBasket->getDiscounts();
00592             if ( count($aDiscounts) > 0 ) {
00593                 foreach ($aDiscounts as $oDiscount) {
00594                     $dDiscount += $oDiscount->dDiscount;
00595                 }
00596             }
00597             $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00598         }
00599 
00600         //order language
00601         $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00602 
00603 
00604         // initial status - 'ERROR'
00605         $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00606 
00607         // copies basket product info ...
00608         $this->_setOrderArticles( $oBasket->getContents() );
00609 
00610         // copies wrapping info
00611         $this->_setWrapping( $oBasket );
00612 
00613         // copies TS protection info
00614         $this->_setTsProtection( $oBasket );
00615     }
00616 
00623     public function getOrderLanguage()
00624     {
00625         if ( $this->_iOrderLang === null ) {
00626             if ( isset( $this->oxorder__oxlang->value ) ) {
00627                 $this->_iOrderLang = oxLang::getInstance()->validateLanguage( $this->oxorder__oxlang->value );
00628             } else {
00629                 $this->_iOrderLang = oxLang::getInstance()->getBaseLanguage();
00630             }
00631         }
00632         return $this->_iOrderLang;
00633     }
00634 
00642     protected function _setUser( $oUser )
00643     {
00644 
00645         $this->oxorder__oxuserid        = new oxField($oUser->getId());
00646 
00647         // bill address
00648         $this->oxorder__oxbillcompany     = clone $oUser->oxuser__oxcompany;
00649         $this->oxorder__oxbillemail       = clone $oUser->oxuser__oxusername;
00650         $this->oxorder__oxbillfname       = clone $oUser->oxuser__oxfname;
00651         $this->oxorder__oxbilllname       = clone $oUser->oxuser__oxlname;
00652         $this->oxorder__oxbillstreet      = clone $oUser->oxuser__oxstreet;
00653         $this->oxorder__oxbillstreetnr    = clone $oUser->oxuser__oxstreetnr;
00654         $this->oxorder__oxbilladdinfo     = clone $oUser->oxuser__oxaddinfo;
00655         $this->oxorder__oxbillustid       = clone $oUser->oxuser__oxustid;
00656         $this->oxorder__oxbillcity        = clone $oUser->oxuser__oxcity;
00657         $this->oxorder__oxbillcountryid   = clone $oUser->oxuser__oxcountryid;
00658         $this->oxorder__oxbillstateid     = clone $oUser->oxuser__oxstateid;
00659         $this->oxorder__oxbillzip         = clone $oUser->oxuser__oxzip;
00660         $this->oxorder__oxbillfon         = clone $oUser->oxuser__oxfon;
00661         $this->oxorder__oxbillfax         = clone $oUser->oxuser__oxfax;
00662         $this->oxorder__oxbillsal         = clone $oUser->oxuser__oxsal;
00663 
00664 
00665         // delivery address
00666         if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00667             // set delivery address
00668             $this->oxorder__oxdelcompany   = clone $oDelAdress->oxaddress__oxcompany;
00669             $this->oxorder__oxdelfname     = clone $oDelAdress->oxaddress__oxfname;
00670             $this->oxorder__oxdellname     = clone $oDelAdress->oxaddress__oxlname;
00671             $this->oxorder__oxdelstreet    = clone $oDelAdress->oxaddress__oxstreet;
00672             $this->oxorder__oxdelstreetnr  = clone $oDelAdress->oxaddress__oxstreetnr;
00673             $this->oxorder__oxdeladdinfo   = clone $oDelAdress->oxaddress__oxaddinfo;
00674             $this->oxorder__oxdelcity      = clone $oDelAdress->oxaddress__oxcity;
00675             $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00676             $this->oxorder__oxdelstateid   = clone $oDelAdress->oxaddress__oxstateid;
00677             $this->oxorder__oxdelzip       = clone $oDelAdress->oxaddress__oxzip;
00678             $this->oxorder__oxdelfon       = clone $oDelAdress->oxaddress__oxfon;
00679             $this->oxorder__oxdelfax       = clone $oDelAdress->oxaddress__oxfax;
00680             $this->oxorder__oxdelsal       = clone $oDelAdress->oxaddress__oxsal;
00681         }
00682     }
00683 
00691     protected function _setWrapping( oxBasket $oBasket )
00692     {
00693         $myConfig = $this->getConfig();
00694 
00695         // wrapping price
00696         if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00697             $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00698 
00699             // wrapping VAT
00700             if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
00701                 $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00702             }
00703         }
00704 
00705         // greetings card
00706         $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00707 
00708         // card text will be stored in database
00709         $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00710     }
00711 
00720     protected function _setOrderArticles( $aArticleList )
00721     {
00722         // reset articles list
00723         $this->_oArticles = oxNew( 'oxlist' );
00724         $iCurrLang = $this->getOrderLanguage();
00725 
00726         // add all the products we have on basket to the order
00727         foreach ( $aArticleList as $oContent ) {
00728 
00729             //$oContent->oProduct = $oContent->getArticle();
00730             // #M773 Do not use article lazy loading on order save
00731             $oProduct = $oContent->getArticle( true, null, true);
00732 
00733             // copy only if object is oxarticle type
00734             if ( $oProduct->isOrderArticle() ) {
00735                 $oOrderArticle = $oProduct;
00736             } else {
00737 
00738                 // if order language doe not match product language - article must be reloaded in order language
00739                 if ( $iCurrLang != $oProduct->getLanguage() ) {
00740                     $oProduct->loadInLang( $iCurrLang, $oProduct->getProductId() );
00741                 }
00742 
00743                 // set chosen selectlist
00744                 $sSelList = '';
00745                 if ( count( $aChosenSelList = $oContent->getChosenSelList() ) ) {
00746                     foreach ( $aChosenSelList as $oItem ) {
00747                         if ( $sSelList ) {
00748                             $sSelList .= ", ";
00749                         }
00750                         $sSelList .= "{$oItem->name} : {$oItem->value}";
00751                     }
00752                 }
00753 
00754                 $oOrderArticle = oxNew( 'oxorderarticle' );
00755                 $oOrderArticle->setIsNewOrderItem( true );
00756                 $oOrderArticle->copyThis( $oProduct );
00757                 $oOrderArticle->setId();
00758 
00759                 $oOrderArticle->oxorderarticles__oxartnum     = clone $oProduct->oxarticles__oxartnum;
00760                 $oOrderArticle->oxorderarticles__oxselvariant = new oxField( trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->getRawValue() ), oxField::T_RAW );
00761                 $oOrderArticle->oxorderarticles__oxshortdesc  = new oxField( $oProduct->oxarticles__oxshortdesc->getRawValue(), oxField::T_RAW );
00762                 // #M974: duplicated entries for the name of variants in orders
00763                 $oOrderArticle->oxorderarticles__oxtitle      = new oxField( trim( $oProduct->oxarticles__oxtitle->getRawValue() ), oxField::T_RAW );
00764 
00765                 // copying persistent parameters ...
00766                 if ( !is_array( $aPersParams = $oProduct->getPersParams() ) ) {
00767                     $aPersParams = $oContent->getPersParams();
00768                 }
00769                 if ( is_array( $aPersParams ) && count( $aPersParams )) {
00770                     $oOrderArticle->oxorderarticles__oxpersparam = new oxField( serialize( $aPersParams ), oxField::T_RAW );
00771                 }
00772             }
00773 
00774             // ids, titles, numbers ...
00775             $oOrderArticle->oxorderarticles__oxorderid = new oxField( $this->getId() );
00776             $oOrderArticle->oxorderarticles__oxartid   = new oxField( $oContent->getProductId() );
00777             $oOrderArticle->oxorderarticles__oxamount  = new oxField( $oContent->getAmount() );
00778 
00779             // prices
00780             $oPrice = $oContent->getPrice();
00781             $oOrderArticle->oxorderarticles__oxnetprice  = new oxField( $oPrice->getNettoPrice(), oxField::T_RAW );
00782             $oOrderArticle->oxorderarticles__oxvatprice  = new oxField( $oPrice->getVatValue(), oxField::T_RAW );
00783             $oOrderArticle->oxorderarticles__oxbrutprice = new oxField( $oPrice->getBruttoPrice(), oxField::T_RAW );
00784             $oOrderArticle->oxorderarticles__oxvat       = new oxField( $oPrice->getVat(), oxField::T_RAW );
00785 
00786             $oUnitPtice = $oContent->getUnitPrice();
00787             $oOrderArticle->oxorderarticles__oxnprice = new oxField( $oUnitPtice->getNettoPrice(), oxField::T_RAW );
00788             $oOrderArticle->oxorderarticles__oxbprice = new oxField( $oUnitPtice->getBruttoPrice(), oxField::T_RAW );
00789 
00790             // wrap id
00791             $oOrderArticle->oxorderarticles__oxwrapid = new oxField( $oContent->getWrappingId(), oxField::T_RAW );
00792 
00793             // items shop id
00794             $oOrderArticle->oxorderarticles__oxordershopid = new oxField( $oContent->getShopId(), oxField::T_RAW );
00795 
00796             // bundle?
00797             $oOrderArticle->oxorderarticles__oxisbundle = new oxField( $oContent->isBundle() );
00798 
00799             // add information for eMail
00800             //P
00801             //TODO: check if this assign is needed at all
00802             $oOrderArticle->oProduct = $oProduct;
00803 
00804             // simulatin order article list
00805             $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
00806         }
00807     }
00808 
00820     protected function _executePayment( oxBasket $oBasket, $oUserpayment )
00821     {
00822         $oPayTransaction = $this->_getGateway();
00823         $oPayTransaction->setPaymentParams( $oUserpayment );
00824 
00825         if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
00826             $this->delete();
00827 
00828             // checking for error messages
00829             if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
00830                 if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
00831                     return $sLastError;
00832                 }
00833             }
00834 
00835             // checking for error codes
00836             if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
00837                 if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
00838                     return $iLastErrorNo;
00839                 }
00840             }
00841 
00842             return self::ORDER_STATE_PAYMENTERROR; // means no authentication
00843         }
00844         return true; // everything fine
00845     }
00846 
00853     protected function _getGateway()
00854     {
00855         return oxNew( 'oxPaymentGateway' );
00856     }
00857 
00865     protected function _setPayment( $sPaymentid )
00866     {
00867         // copying payment info fields
00868         $aDynvalue = oxSession::getVar( 'dynvalue' );
00869         $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
00870 
00871         // loading payment object
00872         $oPayment = oxNew( 'oxpayment' );
00873 
00874         if (!$oPayment->load( $sPaymentid )) {
00875             return null;
00876         }
00877 
00878         // #756M Preserve already stored payment information
00879         if ( !$aDynvalue && ( $oUserpayment = $this->getPaymentType() ) ) {
00880             if ( is_array( $aStoredDynvalue = $oUserpayment->getDynValues() ) ) {
00881                 foreach ( $aStoredDynvalue as $oVal ) {
00882                     $aDynvalue[$oVal->name] = $oVal->value;
00883                 }
00884             }
00885         }
00886 
00887         $oPayment->setDynValues( oxUtils::getInstance()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
00888 
00889         // collecting dynamic values
00890         $aDynVal = array();
00891 
00892         if ( is_array( $aPaymentDynValues = $oPayment->getDynValues() ) ) {
00893             foreach ( $aPaymentDynValues  as $key => $oVal ) {
00894                 if ( isset( $aDynvalue[$oVal->name] ) ) {
00895                     $oVal->value = $aDynvalue[$oVal->name];
00896                 }
00897 
00898                 //$oPayment->setDynValue($key, $oVal);
00899                 $aPaymentDynValues[$key] = $oVal;
00900                 $aDynVal[$oVal->name] = $oVal->value;
00901             }
00902         }
00903 
00904         // Store this payment information, we might allow users later to
00905         // reactivate already give payment informations
00906 
00907         $oUserpayment = oxNew( 'oxuserpayment' );
00908         $oUserpayment->oxuserpayments__oxuserid     = clone $this->oxorder__oxuserid;
00909         $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
00910         $oUserpayment->oxuserpayments__oxvalue      = new oxField(oxUtils::getInstance()->assignValuesToText( $aDynVal ), oxField::T_RAW);
00911         $oUserpayment->oxpayments__oxdesc           = clone $oPayment->oxpayments__oxdesc;
00912         $oUserpayment->oxpayments__oxlongdesc       = clone $oPayment->oxpayments__oxlongdesc;
00913         $oUserpayment->setDynValues( $aPaymentDynValues );
00914         $oUserpayment->save();
00915 
00916         // storing payment information to order
00917         $this->oxorder__oxpaymentid   = new oxField($oUserpayment->getId(), oxField::T_RAW);
00918         $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
00919 
00920         // returning user payment object which will be used later in code ...
00921         return $oUserpayment;
00922     }
00923 
00929     protected function _setFolder()
00930     {
00931         $myConfig = $this->getConfig();
00932         $this->oxorder__oxfolder    = new oxField(key( $myConfig->getShopConfVar(  'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
00933     }
00934 
00944     protected function _updateWishlist( $aArticleList, $oUser )
00945     {
00946 
00947         foreach ( $aArticleList as $oContent) {
00948             if ( ( $sWishId = $oContent->getWishId() ) ) {
00949 
00950                 // checking which wishlist user uses ..
00951                 if ( $sWishId == $oUser->getId() ) {
00952                     $oUserBasket = $oUser->getBasket( 'wishlist' );
00953                 } else {
00954                     $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
00955                     $oUserBasket = oxNew( 'oxuserbasket' );
00956                     $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
00957                 }
00958 
00959                 // updating users wish list
00960                 if ( $oUserBasket ) {
00961                     if ( !($sProdId = $oContent->getWishArticleId() )) {
00962                         $sProdId = $oContent->getProductId();
00963                     }
00964                     $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00965                     $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00966                     if ( $dNewAmount < 0) {
00967                         $dNewAmount = 0;
00968                     }
00969                     $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00970                 }
00971             }
00972         }
00973     }
00974 
00984     protected function _updateNoticeList( $aArticleList, $oUser )
00985     {
00986         // loading users notice list ..
00987         if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
00988             // only if wishlist is enabled
00989             foreach ( $aArticleList as $oContent) {
00990                 $sProdId = $oContent->getProductId();
00991 
00992                 // updating users notice list
00993                 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList(), $oContent->getPersParams() );
00994                 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00995                 if ( $dNewAmount < 0) {
00996                     $dNewAmount = 0;
00997                 }
00998                 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true, $oContent->getPersParams() );
00999             }
01000         }
01001     }
01002 
01012     protected function _markVouchers( $oBasket, $oUser )
01013     {
01014         $this->_aVoucherList = $oBasket->getVouchers();
01015 
01016         if ( is_array( $this->_aVoucherList ) ) {
01017             foreach ( $this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
01018                 $oVoucher = oxNew( 'oxvoucher' );
01019                 $oVoucher->load( $sVoucherId );
01020                 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->dVoucherdiscount );
01021 
01022                 $this->_aVoucherList[$sVoucherId] = $oVoucher;
01023             }
01024         }
01025     }
01026 
01032     public function save()
01033     {
01034         if ( ( $blSave = parent::save() ) ) {
01035 
01036             // saving order articles
01037             $oOrderArticles = $this->getOrderArticles();
01038             if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
01039                 foreach ( $oOrderArticles as $oOrderArticle ) {
01040                     $oOrderArticle->save();
01041                 }
01042             }
01043         }
01044 
01045         return $blSave;
01046     }
01047 
01054     public function getDelAddressInfo()
01055     {
01056         $oDelAdress = null;
01057         if (! ($soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
01058             $soxAddressId = oxSession::getVar( 'deladrid' );
01059         }
01060         if ( $soxAddressId ) {
01061             $oDelAdress = oxNew( 'oxaddress' );
01062             $oDelAdress->load( $soxAddressId );
01063 
01064             //get delivery country name from delivery country id
01065             if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
01066                  $oCountry = oxNew( 'oxcountry' );
01067                  $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
01068                  $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
01069             }
01070         }
01071         return $oDelAdress;
01072     }
01073 
01084     public function validateStock( $oBasket )
01085     {
01086         foreach ( $oBasket->getContents() as $key => $oContent ) {
01087             try {
01088                 $oProd = $oContent->getArticle();
01089             } catch ( oxNoArticleException $oEx ) {
01090                 $oBasket->removeItem( $key );
01091                 throw $oEx;
01092             } catch ( oxArticleInputException $oEx ) {
01093                 $oBasket->removeItem( $key );
01094                 throw $oEx;
01095             }
01096 
01097             // check if its still available
01098             $dArtStockAmount = $oBasket->getArtStockInBasket( $oProd->getId(), $key );
01099             $iOnStock = $oProd->checkForStock( $oContent->getAmount(), $dArtStockAmount );
01100             if ( $iOnStock !== true ) {
01101                 $oEx = oxNew( 'oxOutOfStockException' );
01102                 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
01103                 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
01104                 $oEx->setProductId( $oProd->getId() );
01105 
01106                 if (!is_numeric($iOnStock)) {
01107                     $iOnStock = 0;
01108                 }
01109                 $oEx->setRemainingAmount( $iOnStock );
01110                 throw $oEx;
01111             }
01112         }
01113     }
01114 
01120     protected function _insert()
01121     {
01122         $myConfig = $this->getConfig();
01123         $oUtilsDate = oxUtilsDate::getInstance();
01124 
01125         //V #M525 orderdate must be the same as it was
01126         if ( !$this->oxorder__oxorderdate->value ) {
01127             $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
01128         } else {
01129             $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
01130         }
01131         $this->oxorder__oxshopid    = new oxField($myConfig->getShopId(), oxField::T_RAW);
01132 
01133         $this->oxorder__oxsenddate  = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01134 
01135         if ( ( $blInsert = parent::_insert() ) ) {
01136             // setting order number
01137             if ( !$this->oxorder__oxordernr->value ) {
01138                 $aWhere = '';
01139                 // separate order numbers for shops ...
01140                 if ( $this->_blSeparateNumbering ) {
01141                     $aWhere = array( 'oxshopid = "'.$myConfig->getShopId().'"' );
01142                 }
01143                 $this->_setRecordNumber( 'oxordernr', $aWhere );
01144             }
01145         }
01146         return $blInsert;
01147     }
01148 
01154     protected function _update()
01155     {
01156         $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01157         return parent::_update();
01158     }
01159 
01168     public function delete( $sOxId = null )
01169     {
01170         if ( $sOxId ) {
01171             if ( !$this->load( $sOxId ) ) {
01172                 // such order does not exist
01173                 return false;
01174             }
01175         } elseif ( !$sOxId ) {
01176             $sOxId = $this->getId();
01177         }
01178 
01179         // no order id is passed
01180         if ( !$sOxId ) {
01181             return false;
01182         }
01183 
01184 
01185         // delete order articles
01186         $oOrderArticles = $this->getOrderArticles( false );
01187         foreach ( $oOrderArticles as $oOrderArticle ) {
01188             $oOrderArticle->delete();
01189         }
01190 
01191         // #440 - deleting user payment info
01192         if ( $oPaymentType = $this->getPaymentType() ) {
01193             $oPaymentType->delete();
01194         }
01195 
01196         return parent::delete( $sOxId );
01197     }
01198 
01208     public function recalculateOrder( $aNewArticles = array() )
01209     {
01210         oxDb::startTransaction();
01211 
01212         try {
01213             $oBasket = $this->_getOrderBasket();
01214 
01215             // add this order articles to virtual basket and recalculates basket
01216             $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles( true ) );
01217 
01218             // adding new articles to existing order
01219             $this->_addArticlesToBasket( $oBasket, $aNewArticles );
01220 
01221             // recalculating basket
01222             $oBasket->calculateBasket( true );
01223 
01224             //finalizing order (skipping payment execution, vouchers marking and mail sending)
01225             $iRet = $this->finalizeOrder( $oBasket, $this->getOrderUser(), true );
01226 
01227             //if finalizing order failed, rollback transaction
01228             if ( $iRet !== 1 ) {
01229                 oxDb::rollbackTransaction();
01230             } else {
01231                 oxDb::commitTransaction();
01232             }
01233 
01234         } catch( Exception $oE ) {
01235             // if exception, rollBack everything
01236             oxDb::rollbackTransaction();
01237 
01238             if ( defined( 'OXID_PHP_UNIT' ) ) {
01239                 throw $oE;
01240             }
01241         }
01242     }
01243 
01244     protected $_oOrderBasket = null;
01252     protected function _getOrderBasket( $blStockCheck = true )
01253     {
01254         $this->_oOrderBasket = oxNew( "oxbasket" );
01255 
01256         // setting stock check mode
01257         $this->_oOrderBasket->setStockCheckMode( $blStockCheck );
01258 
01259         // setting virtual basket user
01260         $this->_oOrderBasket->setBasketUser( $this->getOrderUser() );
01261 
01262         // transferring order id
01263         $this->_oOrderBasket->setOrderId( $this->getId() );
01264 
01265         // setting basket currency order uses
01266         $aCurrencies = $this->getConfig()->getCurrencyArray();
01267         foreach ( $aCurrencies as $oCur ) {
01268             if ($oCur->name == $this->oxorder__oxcurrency->value) {
01269                 $oBasketCur = $oCur;
01270                 break;
01271             }
01272         }
01273 
01274         // setting currency
01275         $this->_oOrderBasket->setBasketCurrency( $oBasketCur );
01276 
01277         // set basket card id and message
01278         $this->_oOrderBasket->setCardId( $this->oxorder__oxcardid->value );
01279         $this->_oOrderBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01280 
01281         if ( $this->_blReloadDiscount ) {
01282             $oDb = oxDb::getDb( true );
01283             // disabling availability check
01284             $this->_oOrderBasket->setSkipVouchersChecking( true );
01285 
01286             // add previously used vouchers
01287             $sQ = 'select oxid from oxvouchers where oxorderid = '.$oDb->quote( $this->getId() );
01288             $aVouchers = $oDb->getAll( $sQ );
01289             foreach ( $aVouchers as $aVoucher ) {
01290                 $this->_oOrderBasket->addVoucher( $aVoucher['oxid'] );
01291             }
01292         } else {
01293             $this->_oOrderBasket->setDiscountCalcMode( false );
01294             $this->_oOrderBasket->setVoucherDiscount( $this->oxorder__oxvoucherdiscount->value );
01295             $this->_oOrderBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
01296         }
01297 
01298         // must be kept old delivery?
01299         if ( !$this->_blReloadDelivery ) {
01300             $this->_oOrderBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01301         } else {
01302             //  set shipping
01303             $this->_oOrderBasket->setShipping( $this->oxorder__oxdeltype->value );
01304             $this->_oOrderBasket->setDeliveryPrice( null );
01305         }
01306 
01307         //set basket payment
01308         $this->_oOrderBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01309 
01310         return $this->_oOrderBasket;
01311     }
01312 
01321     public function setDelivery( $sDeliveryId )
01322     {
01323         $this->reloadDelivery( true );
01324         $this->oxorder__oxdeltype = new oxField( $sDeliveryId );
01325     }
01326 
01332     public function getOrderUser()
01333     {
01334         if ($this->_oUser === null ) {
01335             $this->_oUser = oxNew( "oxuser" );
01336             $this->_oUser->load( $this->oxorder__oxuserid->value );
01337 
01338             // if object is loaded then reusing its order info
01339             if ( $this->_isLoaded ) {
01340                 // bill address
01341                 $this->_oUser->oxuser__oxcompany  = clone $this->oxorder__oxbillcompany;
01342                 $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
01343                 $this->_oUser->oxuser__oxfname    = clone $this->oxorder__oxbillfname;
01344                 $this->_oUser->oxuser__oxlname    = clone $this->oxorder__oxbilllname;
01345                 $this->_oUser->oxuser__oxstreet   = clone $this->oxorder__oxbillstreet;
01346                 $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
01347                 $this->_oUser->oxuser__oxaddinfo  = clone $this->oxorder__oxbilladdinfo;
01348                 $this->_oUser->oxuser__oxustid    = clone $this->oxorder__oxbillustid;
01349 
01350 
01351                 $this->_oUser->oxuser__oxcity      = clone $this->oxorder__oxbillcity;
01352                 $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
01353                 $this->_oUser->oxuser__oxstateid   = clone $this->oxorder__oxbillstateid;
01354                 $this->_oUser->oxuser__oxzip       = clone $this->oxorder__oxbillzip;
01355                 $this->_oUser->oxuser__oxfon       = clone $this->oxorder__oxbillfon;
01356                 $this->_oUser->oxuser__oxfax       = clone $this->oxorder__oxbillfax;
01357                 $this->_oUser->oxuser__oxsal       = clone $this->oxorder__oxbillsal;
01358             }
01359         }
01360 
01361         return $this->_oUser;
01362     }
01363 
01371     public function pdfFooter( $oPdf )
01372     {
01373     }
01374 
01382     public function pdfHeaderplus( $oPdf )
01383     {
01384     }
01385 
01393     public function pdfHeader( $oPdf )
01394     {
01395     }
01396 
01405     public function genPdf( $sFilename, $iSelLang = 0 )
01406     {
01407     }
01408 
01414     public function getInvoiceNum()
01415     {
01416         $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01417         return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01418     }
01419 
01425     public function getNextBillNum()
01426     {
01427         $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01428         return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01429     }
01430 
01436     public function getShippingSetList()
01437     {
01438         // in which country we deliver
01439         if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
01440             $sShipId = $this->oxorder__oxbillcountryid->value;
01441         }
01442 
01443         $oBasket = $this->_getOrderBasket( false );
01444 
01445         // add this order articles to basket and recalculate basket
01446         $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles() );
01447 
01448         // recalculating basket
01449         $oBasket->calculateBasket( true );
01450 
01451         // load fitting deliveries list
01452         $oDeliveryList = oxNew( "oxDeliveryList", "core" );
01453         $oDeliveryList->setCollectFittingDeliveriesSets( true );
01454 
01455         return $oDeliveryList->getDeliveryList( $oBasket, $this->getOrderUser(), $sShipId );
01456     }
01457 
01463     public function getVoucherNrList()
01464     {
01465         $oDB = oxDb::getDb( true );
01466         $aVouchers = array();
01467         $sSelect = "select oxvouchernr from oxvouchers where oxorderid = ".$oDB->quote( $this->oxorder__oxid->value );
01468         $rs = $oDB->execute( $sSelect);
01469         if ($rs != false && $rs->recordCount() > 0) {
01470             while (!$rs->EOF) {
01471                 $aVouchers[] = $rs->fields['oxvouchernr'];
01472                 $rs->moveNext();
01473             }
01474         }
01475         return $aVouchers;
01476     }
01477 
01485     public function getOrderSum( $blToday = false )
01486     {
01487         $sSelect  = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01488         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01489 
01490         if ( $blToday ) {
01491             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01492         }
01493 
01494         return ( double ) oxDb::getDb()->getOne( $sSelect );
01495     }
01496 
01504     public function getOrderCnt( $blToday = false )
01505     {
01506         $sSelect  = 'select count(*) from oxorder where ';
01507         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'"  and oxorder.oxstorno != "1" ';
01508 
01509         if ( $blToday ) {
01510             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01511         }
01512 
01513         return ( int ) oxDb::getDb()->getOne( $sSelect );
01514     }
01515 
01516 
01524     protected function _checkOrderExist( $sOxId = null )
01525     {
01526         if ( !$sOxId) {
01527             return false;
01528         }
01529 
01530         $oDb = oxDb::getDb();
01531         if ( $oDb->getOne( 'select oxid from oxorder where oxid = '.$oDb->quote( $sOxId ) ) ) {
01532             return true;
01533         }
01534 
01535         return false;
01536     }
01537 
01547     protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01548     {
01549         $iRet = self::ORDER_STATE_MAILINGERROR;
01550 
01551         // add user, basket and payment to order
01552         $this->_oUser    = $oUser;
01553         $this->_oBasket  = $oBasket;
01554         $this->_oPayment = $oPayment;
01555 
01556         $oxEmail = oxNew( 'oxemail' );
01557 
01558         // send order email to user
01559         if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01560             // mail to user was successfully sent
01561             $iRet = self::ORDER_STATE_OK;
01562         }
01563 
01564         // send order email to shop owner
01565         $oxEmail->sendOrderEMailToOwner( $this );
01566 
01567         return $iRet;
01568     }
01569 
01575     public function getBasket()
01576     {
01577         return $this->_oBasket;
01578     }
01579 
01585     public function getPayment()
01586     {
01587         return $this->_oPayment;
01588     }
01589 
01595     public function getVoucherList()
01596     {
01597         return $this->_aVoucherList;
01598     }
01599 
01605     public function getDelSet()
01606     {
01607         if ( $this->_oDelSet == null ) {
01608             // load deliveryset info
01609             $this->_oDelSet = oxNew( 'oxdeliveryset' );
01610             $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01611         }
01612 
01613         return $this->_oDelSet;
01614     }
01615 
01621     public function getPaymentType()
01622     {
01623         if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType === null ) {
01624             $this->_oPaymentType = false;
01625             $oPaymentType = oxNew( 'oxuserpayment' );
01626             if ( $oPaymentType->load( $this->oxorder__oxpaymentid->value ) ) {
01627                 $this->_oPaymentType = $oPaymentType;
01628             }
01629         }
01630 
01631         return $this->_oPaymentType;
01632     }
01633 
01639     public function getGiftCard()
01640     {
01641         if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01642             $this->_oGiftCard = oxNew( 'oxwrapping' );
01643             $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01644         }
01645 
01646         return $this->_oGiftCard;
01647     }
01648 
01656     public function setSeparateNumbering( $blSeparateNumbering = null )
01657     {
01658         $this->_blSeparateNumbering = $blSeparateNumbering;
01659     }
01660 
01668     public function getLastUserPaymentType( $sUserId)
01669     {
01670         $oDb = oxDb::getDb();
01671         $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid='.$oDb->quote( $sUserId ).' order by oxorder.oxorderdate desc ';
01672         $sLastPaymentId = $oDb->getOne( $sQ );
01673         return $sLastPaymentId;
01674     }
01675 
01684     protected function _addOrderArticlesToBasket( $oBasket, $aOrderArticles )
01685     {
01686         // if no order articles, return empty basket
01687         if ( count( $aOrderArticles ) > 0 ) {
01688 
01689             //adding order articles to basket
01690             foreach ( $aOrderArticles as $oOrderArticle ) {
01691                 $oBasket->addOrderArticleToBasket( $oOrderArticle );
01692             }
01693         }
01694     }
01695 
01704     protected function _addArticlesToBasket( $oBasket, $aArticles )
01705     {
01706         // if no order articles
01707         if ( count($aArticles ) > 0 ) {
01708 
01709             //adding order articles to basket
01710             foreach ( $aArticles as $oArticle ) {
01711                 $aSel = isset( $oArticle->oxorderarticles__oxselvariant ) ? $oArticle->oxorderarticles__oxselvariant->value : null;
01712                 $aPersParam = isset( $oArticle->oxorderarticles__oxpersparam ) ? $oArticle->getPersParams() : null;
01713                 $oBasket->addToBasket( $oArticle->oxorderarticles__oxartid->value,
01714                                        $oArticle->oxorderarticles__oxamount->value,
01715                                        $aSel, $aPersParam );
01716             }
01717         }
01718     }
01719 
01725     public function getTotalOrderSum()
01726     {
01727         $oCur = $this->getConfig()->getActShopCurrencyObject();
01728         return number_format( (double)$this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01729     }
01730 
01738     public function getProductVats( $blFormatCurrency = true )
01739     {
01740         $aVats = array();
01741         if ($this->oxorder__oxartvat1->value) {
01742             $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
01743         }
01744         if ($this->oxorder__oxartvat2->value) {
01745             $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
01746         }
01747 
01748         if ( $blFormatCurrency ) {
01749             $oLang = oxLang::getInstance();
01750             $oCur = $this->getConfig()->getActShopCurrencyObject();
01751             foreach ( $aVats as $sKey => $dVat ) {
01752                 $aVats[$sKey] = $oLang->formatCurrency( $dVat, $oCur );
01753             }
01754         }
01755         return $aVats;
01756     }
01757 
01763     public function getBillCountry()
01764     {
01765         if ( !$this->oxorder__oxbillcountry->value ) {
01766             $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
01767         }
01768         return $this->oxorder__oxbillcountry;
01769     }
01770 
01776     public function getDelCountry()
01777     {
01778         if ( !$this->oxorder__oxdelcountry->value ) {
01779             $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
01780         }
01781         return $this->oxorder__oxdelcountry;
01782     }
01790     public function reloadDelivery( $blReload )
01791     {
01792         $this->_blReloadDelivery = $blReload;
01793     }
01794 
01802     public function reloadDiscount( $blReload )
01803     {
01804         $this->_blReloadDiscount = $blReload;
01805     }
01806 
01812     public function cancelOrder()
01813     {
01814         $this->oxorder__oxstorno = new oxField( 1 );
01815         if ( $this->save() ) {
01816             // canceling ordered products
01817             foreach ( $this->getOrderArticles() as $oOrderArticle ) {
01818                 $oOrderArticle->cancelOrderArticle();
01819             }
01820         }
01821     }
01822 
01829     public function getOrderCurrency()
01830     {
01831         if ( $this->_oOrderCurrency === null ) {
01832 
01833             // setting default in case unrecognized currency was set during order
01834             $aCurrencies = $this->getConfig()->getCurrencyArray();
01835             $this->_oOrderCurrency = current( $aCurrencies );
01836 
01837             foreach ( $aCurrencies as $oCurr ) {
01838                 if ( $oCurr->name == $this->oxorder__oxcurrency->value ) {
01839                     $this->_oOrderCurrency = $oCurr;
01840                     break;
01841                 }
01842             }
01843         }
01844         return $this->_oOrderCurrency;
01845     }
01846 
01856     public function validateOrder( $oBasket, $oUser )
01857     {
01858         // validating stock
01859         $iValidState = $this->validateStock( $oBasket );
01860 
01861         if ( !$iValidState ) {
01862             // validating delivery
01863             $iValidState = $this->validateDelivery( $oBasket );
01864         }
01865 
01866         if ( !$iValidState ) {
01867             // validating payment
01868             $iValidState = $this->validatePayment( $oBasket );
01869         }
01870 
01871         return $iValidState;
01872     }
01873 
01882     public function validateDelivery( $oBasket )
01883     {
01884         // proceed with no delivery
01885         // used for other countries
01886         if ( $oBasket->getPaymentId() == 'oxempty') {
01887             return;
01888         }
01889         $oDb = oxDb::getDb();
01890 
01891         $oDelSet = oxNew( "oxdeliveryset" );
01892         $sTable = $oDelSet->getViewName();
01893 
01894         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
01895          $oDb->quote( $oBasket->getShippingId() )." and ".$oDelSet->getSqlActiveSnippet();
01896 
01897         if ( !$oDb->getOne( $sQ ) ) {
01898             // throwing exception
01899             return self::ORDER_STATE_INVALIDDELIVERY;
01900         }
01901     }
01902 
01911     public function validatePayment( $oBasket )
01912     {
01913         $oDb = oxDb::getDb();
01914 
01915         $oPayment = oxNew( "oxpayment" );
01916         $sTable = $oPayment->getViewName();
01917 
01918         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
01919         $oDb->quote( $oBasket->getPaymentId() )." and ".$oPayment->getSqlActiveSnippet();
01920 
01921         if ( !$oDb->getOne( $sQ ) ) {
01922             return self::ORDER_STATE_INVALIDPAYMENT;
01923         }
01924     }
01925 
01933     protected function _setTsProtection( oxBasket $oBasket )
01934     {
01935         // protection price
01936         if ( ( $oTsProtectionCost = $oBasket->getCosts( 'oxtsprotection' ) ) ) {
01937             $this->oxorder__oxtsprotectcosts = new oxField($oTsProtectionCost->getBruttoPrice(), oxField::T_RAW);
01938         }
01939 
01940         // protection protduct id
01941         $this->oxorder__oxtsprotectid = new oxField($oBasket->getTsProductId(), oxField::T_RAW);
01942     }
01943 
01952     protected function _executeTsProtection( oxBasket $oBasket )
01953     {
01954         $aValues['tsProductId'] = $this->oxorder__oxtsprotectid->value;
01955         $aValues['amount'] = $oBasket->getPrice()->getBruttoPrice();
01956         $oCur = $this->getConfig()->getActShopCurrencyObject();
01957         $aValues['currency'] = $oCur->name;
01958         $aValues['buyerEmail'] = $this->oxorder__oxbillemail->value;
01959         $aValues['shopCustomerID'] = $this->oxorder__oxuserid->value;
01960         $aValues['shopOrderID'] = $this->oxorder__oxordernr->value;
01961         $aValues['orderDate'] = $this->oxorder__oxorderdate->value;
01962         $sPaymentId = $oBasket->getPaymentId();
01963         $oTsProtection = oxNew('oxtsprotection');
01964         $blRes = $oTsProtection->requestForTsProtection( $aValues, $sPaymentId );
01965         /*if ( !$blRes ) {
01966             $this->delete();
01967             return self::ORDER_STATE_INVALIDTSPROTECTION;
01968         }*/
01969         return true; // everything fine
01970     }
01971 
01977     public function getFormattedTotalNetSum()
01978     {
01979         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalnetsum->value, $this->getOrderCurrency() );
01980     }
01981 
01987     public function getFormattedTotalBrutSum()
01988     {
01989         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $this->getOrderCurrency() );
01990     }
01991 
01997     public function getFormattedeliveryCost()
01998     {
01999         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxdelcost->value, $this->getOrderCurrency() );
02000     }
02001 
02007     public function getFormattedPayCost()
02008     {
02009        return oxLang::getInstance()->formatCurrency( $this->oxorder__oxpaycost->value, $this->getOrderCurrency() );
02010     }
02011 
02017     public function getFormattedWrapCost()
02018     {
02019         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxwrapcost->value, $this->getOrderCurrency() );
02020     }
02021 
02027     public function getFormattedTotalVouchers()
02028     {
02029         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $this->getOrderCurrency() );
02030     }
02031 
02037     public function getFormattedDiscount()
02038     {
02039         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxdiscount->value, $this->getOrderCurrency() );
02040     }
02041 
02047     public function getFormattedTotalOrderSum()
02048     {
02049         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalordersum->value, $this->getOrderCurrency() );
02050     }
02051 
02057     public function getShipmentTrackingUrl()
02058     {
02059         if ( $this->_sShipTrackUrl === null && $this->oxorder__oxtrackcode->value ) {
02060             $this->_sShipTrackUrl = "http://www.dpd.de/cgi-bin/delistrack?typ=1&amp;lang=de&amp;pknr=".$this->oxorder__oxtrackcode->value;
02061         }
02062 
02063         return $this->_sShipTrackUrl;
02064     }
02065 }