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->load( $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="'.$this->getId().'" ';
00504         $oDb->execute( $sQ );
00505 
00506         //updating order object
00507         $this->oxorder__oxtransstatus = new oxField( $sStatus, oxField::T_RAW );
00508     }
00509 
00520     protected function _loadFromBasket( oxBasket $oBasket )
00521     {
00522         $myConfig = $this->getConfig();
00523 
00524         // store IP Adress - default must be FALSE as it is illegal to store
00525         if ( $myConfig->getConfigParam( 'blStoreIPs' ) &&  $this->oxorder__oxip->value === null ) {
00526             $this->oxorder__oxip = new oxField(oxUtilsServer::getInstance()->getRemoteAddress(), oxField::T_RAW);
00527         }
00528 
00529         // copying main price info
00530         $this->oxorder__oxtotalnetsum   = new oxField(oxUtils::getInstance()->fRound($oBasket->getDiscountedNettoPrice()), oxField::T_RAW);
00531         $this->oxorder__oxtotalbrutsum  = new oxField($oBasket->getProductsPrice()->getBruttoSum(), oxField::T_RAW);
00532         $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00533 
00534         // copying discounted VAT info
00535         $iVatIndex = 1;
00536         foreach ( $oBasket->getProductVats(false) as $iVat => $dPrice ) {
00537             $this->{"oxorder__oxartvat$iVatIndex"}      = new oxField($iVat, oxField::T_RAW);
00538             $this->{"oxorder__oxartvatprice$iVatIndex"} = new oxField($dPrice, oxField::T_RAW);
00539             $iVatIndex ++;
00540         }
00541 
00542         // payment costs if available
00543         if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00544             $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00545             $this->oxorder__oxpayvat  = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00546         }
00547 
00548         // delivery info
00549         if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00550             $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00551             //V #M382: Save VAT, not VAT value for delivery costs
00552             $this->oxorder__oxdelvat  = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW); //V #M382
00553             $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00554         }
00555 
00556         // user remark
00557         if ( $this->oxorder__oxremark->value === null ) {
00558             $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00559         }
00560 
00561         // currency
00562         $oCur = $myConfig->getActShopCurrencyObject();
00563         $this->oxorder__oxcurrency = new oxField($oCur->name);
00564         $this->oxorder__oxcurrate  = new oxField($oCur->rate, oxField::T_RAW);
00565 
00566         // store voucherdiscount
00567         if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00568             $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00569         }
00570 
00571         // general discount
00572         if ( $this->_blReloadDiscount ) {
00573             $dDiscount = 0;
00574             $aDiscounts = $oBasket->getDiscounts();
00575             if ( count($aDiscounts) > 0 ) {
00576                 foreach ($aDiscounts as $oDiscount) {
00577                     $dDiscount += $oDiscount->dDiscount;
00578                 }
00579             }
00580             $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00581         }
00582 
00583         //order language
00584         $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00585 
00586 
00587         // initial status - 'ERROR'
00588         $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00589 
00590         // copies basket product info ...
00591         $this->_setOrderArticles( $oBasket->getContents() );
00592 
00593         // copies wrapping info
00594         $this->_setWrapping( $oBasket );
00595 
00596         // copies TS protection info
00597         $this->_setTsProtection( $oBasket );
00598     }
00599 
00606     public function getOrderLanguage()
00607     {
00608         if ( $this->_iOrderLang === null ) {
00609             if ( isset( $this->oxorder__oxlang->value ) ) {
00610                 $this->_iOrderLang = oxLang::getInstance()->validateLanguage( $this->oxorder__oxlang->value );
00611             } else {
00612                 $this->_iOrderLang = oxLang::getInstance()->getBaseLanguage();
00613             }
00614         }
00615         return $this->_iOrderLang;
00616     }
00617 
00625     protected function _setUser( $oUser )
00626     {
00627 
00628         $this->oxorder__oxuserid        = new oxField($oUser->getId());
00629 
00630         // bill address
00631         $this->oxorder__oxbillcompany     = clone $oUser->oxuser__oxcompany;
00632         $this->oxorder__oxbillemail       = clone $oUser->oxuser__oxusername;
00633         $this->oxorder__oxbillfname       = clone $oUser->oxuser__oxfname;
00634         $this->oxorder__oxbilllname       = clone $oUser->oxuser__oxlname;
00635         $this->oxorder__oxbillstreet      = clone $oUser->oxuser__oxstreet;
00636         $this->oxorder__oxbillstreetnr    = clone $oUser->oxuser__oxstreetnr;
00637         $this->oxorder__oxbilladdinfo     = clone $oUser->oxuser__oxaddinfo;
00638         $this->oxorder__oxbillustid       = clone $oUser->oxuser__oxustid;
00639         $this->oxorder__oxbillcity        = clone $oUser->oxuser__oxcity;
00640         $this->oxorder__oxbillcountryid   = clone $oUser->oxuser__oxcountryid;
00641         $this->oxorder__oxbillstateid     = clone $oUser->oxuser__oxstateid;
00642         $this->oxorder__oxbillzip         = clone $oUser->oxuser__oxzip;
00643         $this->oxorder__oxbillfon         = clone $oUser->oxuser__oxfon;
00644         $this->oxorder__oxbillfax         = clone $oUser->oxuser__oxfax;
00645         $this->oxorder__oxbillsal         = clone $oUser->oxuser__oxsal;
00646 
00647 
00648         // delivery address
00649         if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00650             // set delivery address
00651             $this->oxorder__oxdelcompany   = clone $oDelAdress->oxaddress__oxcompany;
00652             $this->oxorder__oxdelfname     = clone $oDelAdress->oxaddress__oxfname;
00653             $this->oxorder__oxdellname     = clone $oDelAdress->oxaddress__oxlname;
00654             $this->oxorder__oxdelstreet    = clone $oDelAdress->oxaddress__oxstreet;
00655             $this->oxorder__oxdelstreetnr  = clone $oDelAdress->oxaddress__oxstreetnr;
00656             $this->oxorder__oxdeladdinfo   = clone $oDelAdress->oxaddress__oxaddinfo;
00657             $this->oxorder__oxdelcity      = clone $oDelAdress->oxaddress__oxcity;
00658             $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00659             $this->oxorder__oxdelstateid   = clone $oDelAdress->oxaddress__oxstateid;
00660             $this->oxorder__oxdelzip       = clone $oDelAdress->oxaddress__oxzip;
00661             $this->oxorder__oxdelfon       = clone $oDelAdress->oxaddress__oxfon;
00662             $this->oxorder__oxdelfax       = clone $oDelAdress->oxaddress__oxfax;
00663             $this->oxorder__oxdelsal       = clone $oDelAdress->oxaddress__oxsal;
00664         }
00665     }
00666 
00674     protected function _setWrapping( oxBasket $oBasket )
00675     {
00676         $myConfig = $this->getConfig();
00677 
00678         // wrapping price
00679         if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00680             $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00681 
00682             // wrapping VAT
00683             if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
00684                 $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00685             }
00686         }
00687 
00688         // greetings card
00689         $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00690 
00691         // card text will be stored in database
00692         $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00693     }
00694 
00703     protected function _setOrderArticles( $aArticleList )
00704     {
00705         // reset articles list
00706         $this->_oArticles = oxNew( 'oxlist' );
00707         $iCurrLang = $this->getOrderLanguage();
00708 
00709         // add all the products we have on basket to the order
00710         foreach ( $aArticleList as $oContent ) {
00711 
00712             //$oContent->oProduct = $oContent->getArticle();
00713             // #M773 Do not use article lazy loading on order save
00714             $oProduct = $oContent->getArticle( true, null, true);
00715 
00716             // copy only if object is oxarticle type
00717             if ( $oProduct->isOrderArticle() ) {
00718                 $oOrderArticle = $oProduct;
00719             } else {
00720 
00721                 // if order language doe not match product language - article must be reloaded in order language
00722                 if ( $iCurrLang != $oProduct->getLanguage() ) {
00723                     $oProduct->loadInLang( $iCurrLang, $oProduct->getProductId() );
00724                 }
00725 
00726                 // set chosen selectlist
00727                 $sSelList = '';
00728                 if ( count( $aChosenSelList = $oContent->getChosenSelList() ) ) {
00729                     foreach ( $aChosenSelList as $oItem ) {
00730                         if ( $sSelList ) {
00731                             $sSelList .= ", ";
00732                         }
00733                         $sSelList .= "{$oItem->name} : {$oItem->value}";
00734                     }
00735                 }
00736 
00737                 $oOrderArticle = oxNew( 'oxorderarticle' );
00738                 $oOrderArticle->setIsNewOrderItem( true );
00739                 $oOrderArticle->copyThis( $oProduct );
00740                 $oOrderArticle->setId();
00741 
00742                 $oOrderArticle->oxorderarticles__oxartnum     = clone $oProduct->oxarticles__oxartnum;
00743                 $oOrderArticle->oxorderarticles__oxselvariant = new oxField( trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->getRawValue() ), oxField::T_RAW );
00744                 $oOrderArticle->oxorderarticles__oxshortdesc  = new oxField( $oProduct->oxarticles__oxshortdesc->getRawValue(), oxField::T_RAW );
00745                 // #M974: duplicated entries for the name of variants in orders
00746                 $oOrderArticle->oxorderarticles__oxtitle      = new oxField( trim( $oProduct->oxarticles__oxtitle->getRawValue() ), oxField::T_RAW );
00747 
00748                 // copying persistent parameters ...
00749                 if ( !is_array( $aPersParams = $oProduct->getPersParams() ) ) {
00750                     $aPersParams = $oContent->getPersParams();
00751                 }
00752                 if ( is_array( $aPersParams ) && count( $aPersParams )) {
00753                     $oOrderArticle->oxorderarticles__oxpersparam = new oxField( serialize( $aPersParams ), oxField::T_RAW );
00754                 }
00755             }
00756 
00757             // ids, titles, numbers ...
00758             $oOrderArticle->oxorderarticles__oxorderid = new oxField( $this->getId() );
00759             $oOrderArticle->oxorderarticles__oxartid   = new oxField( $oContent->getProductId() );
00760             $oOrderArticle->oxorderarticles__oxamount  = new oxField( $oContent->getAmount() );
00761 
00762             // prices
00763             $oPrice = $oContent->getPrice();
00764             $oOrderArticle->oxorderarticles__oxnetprice  = new oxField( $oPrice->getNettoPrice(), oxField::T_RAW );
00765             $oOrderArticle->oxorderarticles__oxvatprice  = new oxField( $oPrice->getVatValue(), oxField::T_RAW );
00766             $oOrderArticle->oxorderarticles__oxbrutprice = new oxField( $oPrice->getBruttoPrice(), oxField::T_RAW );
00767             $oOrderArticle->oxorderarticles__oxvat       = new oxField( $oPrice->getVat(), oxField::T_RAW );
00768 
00769             $oUnitPtice = $oContent->getUnitPrice();
00770             $oOrderArticle->oxorderarticles__oxnprice = new oxField( $oUnitPtice->getNettoPrice(), oxField::T_RAW );
00771             $oOrderArticle->oxorderarticles__oxbprice = new oxField( $oUnitPtice->getBruttoPrice(), oxField::T_RAW );
00772 
00773             // wrap id
00774             $oOrderArticle->oxorderarticles__oxwrapid = new oxField( $oContent->getWrappingId(), oxField::T_RAW );
00775 
00776             // items shop id
00777             $oOrderArticle->oxorderarticles__oxordershopid = new oxField( $oContent->getShopId(), oxField::T_RAW );
00778 
00779             // bundle?
00780             $oOrderArticle->oxorderarticles__oxisbundle = new oxField( $oContent->isBundle() );
00781 
00782             // add information for eMail
00783             //P
00784             //TODO: check if this assign is needed at all
00785             $oOrderArticle->oProduct = $oProduct;
00786 
00787             // simulatin order article list
00788             $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
00789         }
00790     }
00791 
00803     protected function _executePayment( oxBasket $oBasket, $oUserpayment )
00804     {
00805         $oPayTransaction = $this->_getGateway();
00806         $oPayTransaction->setPaymentParams( $oUserpayment );
00807 
00808         if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
00809             $this->delete();
00810 
00811             // checking for error messages
00812             if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
00813                 if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
00814                     return $sLastError;
00815                 }
00816             }
00817 
00818             // checking for error codes
00819             if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
00820                 if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
00821                     return $iLastErrorNo;
00822                 }
00823             }
00824 
00825             return self::ORDER_STATE_PAYMENTERROR; // means no authentication
00826         }
00827         return true; // everything fine
00828     }
00829 
00836     protected function _getGateway()
00837     {
00838         return oxNew( 'oxPaymentGateway' );
00839     }
00840 
00848     protected function _setPayment( $sPaymentid )
00849     {
00850         // copying payment info fields
00851         $aDynvalue = oxSession::getVar( 'dynvalue' );
00852         $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
00853 
00854         // loading payment object
00855         $oPayment = oxNew( 'oxpayment' );
00856 
00857         if (!$oPayment->load( $sPaymentid )) {
00858             return null;
00859         }
00860 
00861         // #756M Preserve already stored payment information
00862         if ( !$aDynvalue && ( $oUserpayment = $this->getPaymentType() ) ) {
00863             if ( is_array( $aStoredDynvalue = $oUserpayment->getDynValues() ) ) {
00864                 foreach ( $aStoredDynvalue as $oVal ) {
00865                     $aDynvalue[$oVal->name] = $oVal->value;
00866                 }
00867             }
00868         }
00869 
00870         $oPayment->setDynValues( oxUtils::getInstance()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
00871 
00872         // collecting dynamic values
00873         $aDynVal = array();
00874 
00875         if ( is_array( $aPaymentDynValues = $oPayment->getDynValues() ) ) {
00876             foreach ( $aPaymentDynValues  as $key => $oVal ) {
00877                 if ( isset( $aDynvalue[$oVal->name] ) ) {
00878                     $oVal->value = $aDynvalue[$oVal->name];
00879                 }
00880 
00881                 //$oPayment->setDynValue($key, $oVal);
00882                 $aPaymentDynValues[$key] = $oVal;
00883                 $aDynVal[$oVal->name] = $oVal->value;
00884             }
00885         }
00886 
00887         // Store this payment information, we might allow users later to
00888         // reactivate already give payment informations
00889 
00890         $oUserpayment = oxNew( 'oxuserpayment' );
00891         $oUserpayment->oxuserpayments__oxuserid     = clone $this->oxorder__oxuserid;
00892         $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
00893         $oUserpayment->oxuserpayments__oxvalue      = new oxField(oxUtils::getInstance()->assignValuesToText( $aDynVal ), oxField::T_RAW);
00894         $oUserpayment->oxpayments__oxdesc           = clone $oPayment->oxpayments__oxdesc;
00895         $oUserpayment->oxpayments__oxlongdesc       = clone $oPayment->oxpayments__oxlongdesc;
00896         $oUserpayment->setDynValues( $aPaymentDynValues );
00897         $oUserpayment->save();
00898 
00899         // storing payment information to order
00900         $this->oxorder__oxpaymentid   = new oxField($oUserpayment->getId(), oxField::T_RAW);
00901         $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
00902 
00903         // returning user payment object which will be used later in code ...
00904         return $oUserpayment;
00905     }
00906 
00912     protected function _setFolder()
00913     {
00914         $myConfig = $this->getConfig();
00915         $this->oxorder__oxfolder    = new oxField(key( $myConfig->getShopConfVar(  'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
00916     }
00917 
00927     protected function _updateWishlist( $aArticleList, $oUser )
00928     {
00929 
00930         foreach ( $aArticleList as $oContent) {
00931             if ( ( $sWishId = $oContent->getWishId() ) ) {
00932 
00933                 // checking which wishlist user uses ..
00934                 if ( $sWishId == $oUser->getId() ) {
00935                     $oUserBasket = $oUser->getBasket( 'wishlist' );
00936                 } else {
00937                     $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
00938                     $oUserBasket = oxNew( 'oxuserbasket' );
00939                     $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
00940                 }
00941 
00942                 // updating users wish list
00943                 if ( $oUserBasket ) {
00944                     if ( !($sProdId = $oContent->getWishArticleId() )) {
00945                         $sProdId = $oContent->getProductId();
00946                     }
00947                     $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00948                     $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00949                     if ( $dNewAmount < 0) {
00950                         $dNewAmount = 0;
00951                     }
00952                     $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00953                 }
00954             }
00955         }
00956     }
00957 
00967     protected function _updateNoticeList( $aArticleList, $oUser )
00968     {
00969         // loading users notice list ..
00970         if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
00971             // only if wishlist is enabled
00972             foreach ( $aArticleList as $oContent) {
00973                 $sProdId = $oContent->getProductId();
00974 
00975                 // updating users notice list
00976                 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList(), $oContent->getPersParams() );
00977                 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00978                 if ( $dNewAmount < 0) {
00979                     $dNewAmount = 0;
00980                 }
00981                 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true, $oContent->getPersParams() );
00982             }
00983         }
00984     }
00985 
00995     protected function _markVouchers( $oBasket, $oUser )
00996     {
00997         $this->_aVoucherList = $oBasket->getVouchers();
00998 
00999         if ( is_array( $this->_aVoucherList ) ) {
01000             foreach ( $this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
01001                 $oVoucher = oxNew( 'oxvoucher' );
01002                 $oVoucher->load( $sVoucherId );
01003                 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->dVoucherdiscount );
01004 
01005                 $this->_aVoucherList[$sVoucherId] = $oVoucher;
01006             }
01007         }
01008     }
01009 
01015     public function save()
01016     {
01017         if ( ( $blSave = parent::save() ) ) {
01018 
01019             // saving order articles
01020             $oOrderArticles = $this->getOrderArticles();
01021             if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
01022                 foreach ( $oOrderArticles as $oOrderArticle ) {
01023                     $oOrderArticle->save();
01024                 }
01025             }
01026         }
01027 
01028         return $blSave;
01029     }
01030 
01037     public function getDelAddressInfo()
01038     {
01039         $oDelAdress = null;
01040         if (! ($soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
01041             $soxAddressId = oxSession::getVar( 'deladrid' );
01042         }
01043         if ( $soxAddressId ) {
01044             $oDelAdress = oxNew( 'oxaddress' );
01045             $oDelAdress->load( $soxAddressId );
01046 
01047             //get delivery country name from delivery country id
01048             if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
01049                  $oCountry = oxNew( 'oxcountry' );
01050                  $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
01051                  $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
01052             }
01053         }
01054         return $oDelAdress;
01055     }
01056 
01067     public function validateStock( $oBasket )
01068     {
01069         foreach ( $oBasket->getContents() as $key => $oContent ) {
01070             try {
01071                 $oProd = $oContent->getArticle();
01072             } catch ( oxNoArticleException $oEx ) {
01073                 $oBasket->removeItem( $key );
01074                 throw $oEx;
01075             } catch ( oxArticleInputException $oEx ) {
01076                 $oBasket->removeItem( $key );
01077                 throw $oEx;
01078             }
01079 
01080             // check if its still available
01081             $dArtStockAmount = $oBasket->getArtStockInBasket( $oProd->getId(), $key );
01082             $iOnStock = $oProd->checkForStock( $oContent->getAmount(), $dArtStockAmount );
01083             if ( $iOnStock !== true ) {
01084                 $oEx = oxNew( 'oxOutOfStockException' );
01085                 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
01086                 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
01087                 $oEx->setProductId( $oProd->getId() );
01088                 $oEx->setRemainingAmount( $oProd->oxarticles__oxstock->value );
01089                 throw $oEx;
01090             }
01091         }
01092     }
01093 
01099     protected function _insert()
01100     {
01101         $myConfig = $this->getConfig();
01102         $oUtilsDate = oxUtilsDate::getInstance();
01103 
01104         //V #M525 orderdate must be the same as it was
01105         if ( !$this->oxorder__oxorderdate->value ) {
01106             $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
01107         } else {
01108             $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
01109         }
01110         $this->oxorder__oxshopid    = new oxField($myConfig->getShopId(), oxField::T_RAW);
01111 
01112         $this->oxorder__oxsenddate  = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01113 
01114         if ( ( $blInsert = parent::_insert() ) ) {
01115             // setting order number
01116             if ( !$this->oxorder__oxordernr->value ) {
01117                 $aWhere = '';
01118                 // separate order numbers for shops ...
01119                 if ( $this->_blSeparateNumbering ) {
01120                     $aWhere = array( 'oxshopid = "'.$myConfig->getShopId().'"' );
01121                 }
01122                 $this->_setRecordNumber( 'oxordernr', $aWhere );
01123             }
01124         }
01125         return $blInsert;
01126     }
01127 
01133     protected function _update()
01134     {
01135         $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01136         return parent::_update();
01137     }
01138 
01147     public function delete( $sOxId = null )
01148     {
01149         if ( $sOxId ) {
01150             if ( !$this->load( $sOxId ) ) {
01151                 // such order does not exist
01152                 return false;
01153             }
01154         } elseif ( !$sOxId ) {
01155             $sOxId = $this->getId();
01156         }
01157 
01158         // no order id is passed
01159         if ( !$sOxId ) {
01160             return false;
01161         }
01162 
01163 
01164         // delete order articles
01165         $oOrderArticles = $this->getOrderArticles( false );
01166         foreach ( $oOrderArticles as $oOrderArticle ) {
01167             $oOrderArticle->delete();
01168         }
01169 
01170         // #440 - deleting user payment info
01171         if ( $oPaymentType = $this->getPaymentType() ) {
01172             $oPaymentType->delete();
01173         }
01174 
01175         return parent::delete( $sOxId );
01176     }
01177 
01187     public function recalculateOrder( $aNewArticles = array() )
01188     {
01189         oxDb::startTransaction();
01190 
01191         try {
01192             $oBasket = $this->_getOrderBasket();
01193 
01194             // add this order articles to virtual basket and recalculates basket
01195             $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles( true ) );
01196 
01197             // adding new articles to existing order
01198             $this->_addArticlesToBasket( $oBasket, $aNewArticles );
01199 
01200             // recalculating basket
01201             $oBasket->calculateBasket( true );
01202 
01203             //finalizing order (skipping payment execution, vouchers marking and mail sending)
01204             $iRet = $this->finalizeOrder( $oBasket, $this->getOrderUser(), true );
01205 
01206             //if finalizing order failed, rollback transaction
01207             if ( $iRet !== 1 ) {
01208                 oxDb::rollbackTransaction();
01209             } else {
01210                 oxDb::commitTransaction();
01211             }
01212 
01213         } catch( Exception $oE ) {
01214             // if exception, rollBack everything
01215             oxDb::rollbackTransaction();
01216 
01217             if ( defined( 'OXID_PHP_UNIT' ) ) {
01218                 throw $oE;
01219             }
01220         }
01221     }
01222 
01223     protected $_oOrderBasket = null;
01231     protected function _getOrderBasket( $blStockCheck = true )
01232     {
01233         $this->_oOrderBasket = oxNew( "oxbasket" );
01234 
01235         // setting stock check mode
01236         $this->_oOrderBasket->setStockCheckMode( $blStockCheck );
01237 
01238         // setting virtual basket user
01239         $this->_oOrderBasket->setBasketUser( $this->getOrderUser() );
01240 
01241         // transferring order id
01242         $this->_oOrderBasket->setOrderId( $this->getId() );
01243 
01244         // setting basket currency order uses
01245         $aCurrencies = $this->getConfig()->getCurrencyArray();
01246         foreach ( $aCurrencies as $oCur ) {
01247             if ($oCur->name == $this->oxorder__oxcurrency->value) {
01248                 $oBasketCur = $oCur;
01249                 break;
01250             }
01251         }
01252 
01253         // setting currency
01254         $this->_oOrderBasket->setBasketCurrency( $oBasketCur );
01255 
01256         // set basket card id and message
01257         $this->_oOrderBasket->setCardId( $this->oxorder__oxcardid->value );
01258         $this->_oOrderBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01259 
01260         if ( $this->_blReloadDiscount ) {
01261             // disabling availability check
01262             $this->_oOrderBasket->setSkipVouchersChecking( true );
01263 
01264             // add previously used vouchers
01265             $sQ = 'select oxid from oxvouchers where oxorderid = "'.$this->getId().'"';
01266             $aVouchers = oxDb::getDb( true )->getAll( $sQ );
01267             foreach ( $aVouchers as $aVoucher ) {
01268                 $this->_oOrderBasket->addVoucher( $aVoucher['oxid'] );
01269             }
01270         } else {
01271             $this->_oOrderBasket->setDiscountCalcMode( false );
01272             $this->_oOrderBasket->setVoucherDiscount( $this->oxorder__oxvoucherdiscount->value );
01273             $this->_oOrderBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
01274         }
01275 
01276         // must be kept old delivery?
01277         if ( !$this->_blReloadDelivery ) {
01278             $this->_oOrderBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01279         } else {
01280             //  set shipping
01281             $this->_oOrderBasket->setShipping( $this->oxorder__oxdeltype->value );
01282             $this->_oOrderBasket->setDeliveryPrice( null );
01283         }
01284 
01285         //set basket payment
01286         $this->_oOrderBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01287 
01288         return $this->_oOrderBasket;
01289     }
01290 
01299     public function setDelivery( $sDeliveryId )
01300     {
01301         $this->reloadDelivery( true );
01302         $this->oxorder__oxdeltype = new oxField( $sDeliveryId );
01303     }
01304 
01310     public function getOrderUser()
01311     {
01312         if ($this->_oUser === null ) {
01313             $this->_oUser = oxNew( "oxuser" );
01314             $this->_oUser->load( $this->oxorder__oxuserid->value );
01315 
01316             // if object is loaded then reusing its order info
01317             if ( $this->_isLoaded ) {
01318                 // bill address
01319                 $this->_oUser->oxuser__oxcompany  = clone $this->oxorder__oxbillcompany;
01320                 $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
01321                 $this->_oUser->oxuser__oxfname    = clone $this->oxorder__oxbillfname;
01322                 $this->_oUser->oxuser__oxlname    = clone $this->oxorder__oxbilllname;
01323                 $this->_oUser->oxuser__oxstreet   = clone $this->oxorder__oxbillstreet;
01324                 $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
01325                 $this->_oUser->oxuser__oxaddinfo  = clone $this->oxorder__oxbilladdinfo;
01326                 $this->_oUser->oxuser__oxustid    = clone $this->oxorder__oxbillustid;
01327 
01328 
01329                 $this->_oUser->oxuser__oxcity      = clone $this->oxorder__oxbillcity;
01330                 $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
01331                 $this->_oUser->oxuser__oxstateid   = clone $this->oxorder__oxbillstateid;
01332                 $this->_oUser->oxuser__oxzip       = clone $this->oxorder__oxbillzip;
01333                 $this->_oUser->oxuser__oxfon       = clone $this->oxorder__oxbillfon;
01334                 $this->_oUser->oxuser__oxfax       = clone $this->oxorder__oxbillfax;
01335                 $this->_oUser->oxuser__oxsal       = clone $this->oxorder__oxbillsal;
01336             }
01337         }
01338 
01339         return $this->_oUser;
01340     }
01341 
01349     public function pdfFooter( $oPdf )
01350     {
01351     }
01352 
01360     public function pdfHeaderplus( $oPdf )
01361     {
01362     }
01363 
01371     public function pdfHeader( $oPdf )
01372     {
01373     }
01374 
01383     public function genPdf( $sFilename, $iSelLang = 0 )
01384     {
01385     }
01386 
01392     public function getInvoiceNum()
01393     {
01394         $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01395         return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01396     }
01397 
01403     public function getNextBillNum()
01404     {
01405         $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01406         return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01407     }
01408 
01414     public function getShippingSetList()
01415     {
01416         // in which country we deliver
01417         if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
01418             $sShipId = $this->oxorder__oxbillcountryid->value;
01419         }
01420 
01421         $oBasket = $this->_getOrderBasket( false );
01422 
01423         // add this order articles to basket and recalculate basket
01424         $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles() );
01425 
01426         // recalculating basket
01427         $oBasket->calculateBasket( true );
01428 
01429         // load fitting deliveries list
01430         $oDeliveryList = oxNew( "oxDeliveryList", "core" );
01431         $oDeliveryList->setCollectFittingDeliveriesSets( true );
01432 
01433         return $oDeliveryList->getDeliveryList( $oBasket, $this->getOrderUser(), $sShipId );
01434     }
01435 
01441     public function getVoucherNrList()
01442     {
01443         $oDB = oxDb::getDb( true );
01444         $aVouchers = array();
01445         $sSelect = "select oxvouchernr from oxvouchers where oxorderid = ".$oDB->quote( $this->oxorder__oxid->value );
01446         $rs = $oDB->execute( $sSelect);
01447         if ($rs != false && $rs->recordCount() > 0) {
01448             while (!$rs->EOF) {
01449                 $aVouchers[] = $rs->fields['oxvouchernr'];
01450                 $rs->moveNext();
01451             }
01452         }
01453         return $aVouchers;
01454     }
01455 
01463     public function getOrderSum( $blToday = false )
01464     {
01465         $sSelect  = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01466         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01467 
01468         if ( $blToday ) {
01469             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01470         }
01471 
01472         return ( double ) oxDb::getDb()->getOne( $sSelect );
01473     }
01474 
01482     public function getOrderCnt( $blToday = false )
01483     {
01484         $sSelect  = 'select count(*) from oxorder where ';
01485         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'"  and oxorder.oxstorno != "1" ';
01486 
01487         if ( $blToday ) {
01488             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01489         }
01490 
01491         return ( int ) oxDb::getDb()->getOne( $sSelect );
01492     }
01493 
01494 
01502     protected function _checkOrderExist( $sOxId = null )
01503     {
01504         if ( !$sOxId) {
01505             return false;
01506         }
01507 
01508         $oDb = oxDb::getDb();
01509         if ( $oDb->getOne( 'select oxid from oxorder where oxid = '.$oDb->quote( $sOxId ) ) ) {
01510             return true;
01511         }
01512 
01513         return false;
01514     }
01515 
01525     protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01526     {
01527         $iRet = self::ORDER_STATE_MAILINGERROR;
01528 
01529         // add user, basket and payment to order
01530         $this->_oUser    = $oUser;
01531         $this->_oBasket  = $oBasket;
01532         $this->_oPayment = $oPayment;
01533 
01534         $oxEmail = oxNew( 'oxemail' );
01535 
01536         // send order email to user
01537         if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01538             // mail to user was successfully sent
01539             $iRet = self::ORDER_STATE_OK;
01540         }
01541 
01542         // send order email to shop owner
01543         $oxEmail->sendOrderEMailToOwner( $this );
01544 
01545         return $iRet;
01546     }
01547 
01553     public function getBasket()
01554     {
01555         return $this->_oBasket;
01556     }
01557 
01563     public function getPayment()
01564     {
01565         return $this->_oPayment;
01566     }
01567 
01573     public function getVoucherList()
01574     {
01575         return $this->_aVoucherList;
01576     }
01577 
01583     public function getDelSet()
01584     {
01585         if ( $this->_oDelSet == null ) {
01586             // load deliveryset info
01587             $this->_oDelSet = oxNew( 'oxdeliveryset' );
01588             $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01589         }
01590 
01591         return $this->_oDelSet;
01592     }
01593 
01599     public function getPaymentType()
01600     {
01601         if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType === null ) {
01602             $this->_oPaymentType = false;
01603             $oPaymentType = oxNew( 'oxuserpayment' );
01604             if ( $oPaymentType->load( $this->oxorder__oxpaymentid->value ) ) {
01605                 $this->_oPaymentType = $oPaymentType;
01606             }
01607         }
01608 
01609         return $this->_oPaymentType;
01610     }
01611 
01617     public function getGiftCard()
01618     {
01619         if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01620             $this->_oGiftCard = oxNew( 'oxwrapping' );
01621             $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01622         }
01623 
01624         return $this->_oGiftCard;
01625     }
01626 
01634     public function setSeparateNumbering( $blSeparateNumbering = null )
01635     {
01636         $this->_blSeparateNumbering = $blSeparateNumbering;
01637     }
01638 
01646     public function getLastUserPaymentType( $sUserId)
01647     {
01648         $oDb = oxDb::getDb();
01649         $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid='.$oDb->quote( $sUserId ).' order by oxorder.oxorderdate desc ';
01650         $sLastPaymentId = $oDb->getOne( $sQ );
01651         return $sLastPaymentId;
01652     }
01653 
01662     protected function _addOrderArticlesToBasket( $oBasket, $aOrderArticles )
01663     {
01664         // if no order articles, return empty basket
01665         if ( count( $aOrderArticles ) > 0 ) {
01666 
01667             //adding order articles to basket
01668             foreach ( $aOrderArticles as $oOrderArticle ) {
01669                 $oBasket->addOrderArticleToBasket( $oOrderArticle );
01670             }
01671         }
01672     }
01673 
01682     protected function _addArticlesToBasket( $oBasket, $aArticles )
01683     {
01684         // if no order articles
01685         if ( count($aArticles ) > 0 ) {
01686 
01687             //adding order articles to basket
01688             foreach ( $aArticles as $oArticle ) {
01689                 $aSel = isset( $oArticle->oxorderarticles__oxselvariant ) ? $oArticle->oxorderarticles__oxselvariant->value : null;
01690                 $aPersParam = isset( $oArticle->oxorderarticles__oxpersparam ) ? $oArticle->getPersParams() : null;
01691                 $oBasket->addToBasket( $oArticle->oxorderarticles__oxartid->value,
01692                                        $oArticle->oxorderarticles__oxamount->value,
01693                                        $aSel, $aPersParam );
01694             }
01695         }
01696     }
01697 
01703     public function getTotalOrderSum()
01704     {
01705         $oCur = $this->getConfig()->getActShopCurrencyObject();
01706         return number_format( (double)$this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01707     }
01708 
01716     public function getProductVats( $blFormatCurrency = true )
01717     {
01718         $aVats = array();
01719         if ($this->oxorder__oxartvat1->value) {
01720             $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
01721         }
01722         if ($this->oxorder__oxartvat2->value) {
01723             $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
01724         }
01725 
01726         if ( $blFormatCurrency ) {
01727             $oLang = oxLang::getInstance();
01728             $oCur = $this->getConfig()->getActShopCurrencyObject();
01729             foreach ( $aVats as $sKey => $dVat ) {
01730                 $aVats[$sKey] = $oLang->formatCurrency( $dVat, $oCur );
01731             }
01732         }
01733         return $aVats;
01734     }
01735 
01741     public function getBillCountry()
01742     {
01743         if ( !$this->oxorder__oxbillcountry->value ) {
01744             $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
01745         }
01746         return $this->oxorder__oxbillcountry;
01747     }
01748 
01754     public function getDelCountry()
01755     {
01756         if ( !$this->oxorder__oxdelcountry->value ) {
01757             $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
01758         }
01759         return $this->oxorder__oxdelcountry;
01760     }
01768     public function reloadDelivery( $blReload )
01769     {
01770         $this->_blReloadDelivery = $blReload;
01771     }
01772 
01780     public function reloadDiscount( $blReload )
01781     {
01782         $this->_blReloadDiscount = $blReload;
01783     }
01784 
01790     public function cancelOrder()
01791     {
01792         $this->oxorder__oxstorno = new oxField( 1 );
01793         if ( $this->save() ) {
01794             // canceling ordered products
01795             foreach ( $this->getOrderArticles() as $oOrderArticle ) {
01796                 $oOrderArticle->cancelOrderArticle();
01797             }
01798         }
01799     }
01800 
01807     public function getOrderCurrency()
01808     {
01809         if ( $this->_oOrderCurrency === null ) {
01810 
01811             // setting default in case unrecognized currency was set during order
01812             $aCurrencies = $this->getConfig()->getCurrencyArray();
01813             $this->_oOrderCurrency = current( $aCurrencies );
01814 
01815             foreach ( $aCurrencies as $oCurr ) {
01816                 if ( $oCurr->name == $this->oxorder__oxcurrency->value ) {
01817                     $this->_oOrderCurrency = $oCurr;
01818                     break;
01819                 }
01820             }
01821         }
01822         return $this->_oOrderCurrency;
01823     }
01824 
01834     public function validateOrder( $oBasket, $oUser )
01835     {
01836         // validating stock
01837         $iValidState = $this->validateStock( $oBasket );
01838 
01839         if ( !$iValidState ) {
01840             // validating delivery
01841             $iValidState = $this->validateDelivery( $oBasket );
01842         }
01843 
01844         if ( !$iValidState ) {
01845             // validating payment
01846             $iValidState = $this->validatePayment( $oBasket );
01847         }
01848 
01849         return $iValidState;
01850     }
01851 
01860     public function validateDelivery( $oBasket )
01861     {
01862         // proceed with no delivery
01863         // used for other countries
01864         if ( $oBasket->getPaymentId() == 'oxempty') {
01865             return;
01866         }
01867         $oDb = oxDb::getDb();
01868 
01869         $oDelSet = oxNew( "oxdeliveryset" );
01870         $sTable = $oDelSet->getViewName();
01871 
01872         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
01873               $oDb->quote( $oBasket->getShippingId() )." and ".$oDelSet->getSqlActiveSnippet();
01874 
01875         if ( !$oDb->getOne( $sQ ) ) {
01876             // throwing exception
01877             return self::ORDER_STATE_INVALIDDELIVERY;
01878         }
01879     }
01880 
01889     public function validatePayment( $oBasket )
01890     {
01891         $oDb = oxDb::getDb();
01892 
01893         $oPayment = oxNew( "oxpayment" );
01894         $sTable = $oPayment->getViewName();
01895 
01896         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
01897               $oDb->quote( $oBasket->getPaymentId() )." and ".$oPayment->getSqlActiveSnippet();
01898 
01899         if ( !$oDb->getOne( $sQ ) ) {
01900             return self::ORDER_STATE_INVALIDPAYMENT;
01901         }
01902     }
01903 
01911     protected function _setTsProtection( oxBasket $oBasket )
01912     {
01913         // protection price
01914         if ( ( $oTsProtectionCost = $oBasket->getCosts( 'oxtsprotection' ) ) ) {
01915             $this->oxorder__oxtsprotectcosts = new oxField($oTsProtectionCost->getBruttoPrice(), oxField::T_RAW);
01916         }
01917 
01918         // protection protduct id
01919         $this->oxorder__oxtsprotectid = new oxField($oBasket->getTsProductId(), oxField::T_RAW);
01920     }
01921 
01930     protected function _executeTsProtection( oxBasket $oBasket )
01931     {
01932         $aValues['tsProductId'] = $this->oxorder__oxtsprotectid->value;
01933         $aValues['amount'] = $oBasket->getPrice()->getBruttoPrice();
01934         $oCur = $this->getConfig()->getActShopCurrencyObject();
01935         $aValues['currency'] = $oCur->name;
01936         $aValues['buyerEmail'] = $this->oxorder__oxbillemail->value;
01937         $aValues['shopCustomerID'] = $this->oxorder__oxuserid->value;
01938         $aValues['shopOrderID'] = $this->oxorder__oxordernr->value;
01939         $aValues['orderDate'] = $this->oxorder__oxorderdate->value;
01940         $sPaymentId = $oBasket->getPaymentId();
01941         $oTsProtection = oxNew('oxtsprotection');
01942         $blRes = $oTsProtection->requestForTsProtection( $aValues, $sPaymentId );
01943         if ( !$blRes ) {
01944             $this->delete();
01945             return self::ORDER_STATE_INVALIDTSPROTECTION;
01946         }
01947         return true; // everything fine
01948     }
01949 
01955     public function getFormattedTotalNetSum()
01956     {
01957         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalnetsum->value, $this->getOrderCurrency() );
01958     }
01959 
01965     public function getFormattedTotalBrutSum()
01966     {
01967         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $this->getOrderCurrency() );
01968     }
01969 
01975     public function getFormattedeliveryCost()
01976     {
01977         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxdelcost->value, $this->getOrderCurrency() );
01978     }
01979 
01985     public function getFormattedPayCost()
01986     {
01987        return oxLang::getInstance()->formatCurrency( $this->oxorder__oxpaycost->value, $this->getOrderCurrency() );
01988     }
01989 
01995     public function getFormattedWrapCost()
01996     {
01997         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxwrapcost->value, $this->getOrderCurrency() );
01998     }
01999 
02005     public function getFormattedTotalVouchers()
02006     {
02007         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $this->getOrderCurrency() );
02008     }
02009 
02015     public function getFormattedDiscount()
02016     {
02017         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxdiscount->value, $this->getOrderCurrency() );
02018     }
02019 
02025     public function getFormattedTotalOrderSum()
02026     {
02027         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalordersum->value, $this->getOrderCurrency() );
02028     }
02029 
02035     public function getShipmentTrackingUrl()
02036     {
02037         if ( $this->_sShipTrackUrl === null && $this->oxorder__oxtrackcode->value ) {
02038             $this->_sShipTrackUrl = "http://www.dpd.de/cgi-bin/delistrack?typ=1&amp;lang=de&amp;pknr=".$this->oxorder__oxtrackcode->value;
02039         }
02040 
02041         return $this->_sShipTrackUrl;
02042     }
02043 }