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     protected function _getArticles( $blExcludeCanceled = false )
00266     {
00267         $sSelect = "SELECT `oxorderarticles`.* FROM `oxorderarticles`
00268                         WHERE `oxorderarticles`.`oxorderid` = '".$this->getId() . "'" .
00269                         ( $blExcludeCanceled ? " AND `oxorderarticles`.`oxstorno` != 1 ": " " ) ."
00270                         ORDER BY `oxorderarticles`.`oxartid`";
00271 
00272             // order articles
00273         $oArticles = oxNew( 'oxlist' );
00274         $oArticles->init( 'oxorderarticle' );
00275         $oArticles->selectString( $sSelect );
00276 
00277         return $oArticles;
00278     }
00279 
00287     public function getOrderArticles( $blExcludeCanceled = false )
00288     {
00289         // checking set value
00290         if ( $blExcludeCanceled ) {
00291 
00292             return $this->_getArticles( true );
00293 
00294         } elseif ( $this->_oArticles === null  ) {
00295                 $this->_oArticles = $this->_getArticles();
00296         }
00297 
00298         return $this->_oArticles;
00299     }
00300 
00308     public function setOrderArticleList( $aOrderArticleList )
00309     {
00310         $this->_oArticles = $aOrderArticleList;
00311     }
00312 
00318     public function getOrderDeliveryPrice()
00319     {
00320         if ( $this->_oDelPrice != null ) {
00321             return $this->_oDelPrice;
00322         }
00323 
00324         $this->_oDelPrice = oxNew( 'oxprice' );
00325         $this->_oDelPrice->setBruttoPriceMode();
00326         $this->_oDelPrice->setPrice( $this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value );
00327         return $this->_oDelPrice;
00328     }
00329 
00335     public function getOrderWrappingPrice()
00336     {
00337         if ( $this->_oWrappingPrice != null ) {
00338             return $this->_oWrappingPrice;
00339         }
00340 
00341         $this->_oWrappingPrice = oxNew( 'oxprice' );
00342         $this->_oWrappingPrice->setBruttoPriceMode();
00343         $this->_oWrappingPrice->setPrice( $this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value );
00344         return $this->_oWrappingPrice;
00345     }
00346 
00352     public function getOrderPaymentPrice()
00353     {
00354         if ( $this->_oPaymentPrice != null ) {
00355             return $this->_oPaymentPrice;
00356         }
00357 
00358         $this->_oPaymentPrice = oxNew( 'oxprice' );
00359         $this->_oPaymentPrice->setBruttoPriceMode();
00360         $this->_oPaymentPrice->setPrice( $this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value );
00361         return $this->_oPaymentPrice;
00362     }
00363 
00369     public function getOrderTsProtectionPrice()
00370     {
00371         if ( $this->_oTsProtectionPrice != null ) {
00372             return $this->_oTsProtectionPrice;
00373         }
00374 
00375         $this->_oTsProtectionPrice = oxNew( 'oxprice' );
00376         $this->_oTsProtectionPrice->setBruttoPriceMode();
00377         $this->_oTsProtectionPrice->setPrice( $this->oxorder__oxtsprotectcosts->value, $this->getConfig()->getConfigParam( 'dDefaultVAT' ) );
00378         return $this->_oTsProtectionPrice;
00379     }
00380 
00387     public function getOrderNetSum()
00388     {
00389         $dTotalNetSum = 0;
00390 
00391         $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
00392         $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
00393         $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
00394         $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
00395 
00396         return $dTotalNetSum;
00397     }
00398 
00419     public function finalizeOrder( oxBasket $oBasket, $oUser, $blRecalculatingOrder = false )
00420     {
00421         // check if this order is already stored
00422         $sGetChallenge = oxSession::getVar( 'sess_challenge' );
00423         if ( $this->_checkOrderExist( $sGetChallenge ) ) {
00424             oxUtils::getInstance()->logger( 'BLOCKER' );
00425             // we might use this later, this means that somebody klicked like mad on order button
00426             return self::ORDER_STATE_ORDEREXISTS;
00427         }
00428 
00429         // if not recalculating order, use sess_challenge id, else leave old order id
00430         if ( !$blRecalculatingOrder ) {
00431             // use this ID
00432             $this->setId( $sGetChallenge );
00433 
00434             // validating various order/basket parameters before finalizing
00435             if ( $iOrderState = $this->validateOrder( $oBasket, $oUser ) ) {
00436                 return $iOrderState;
00437             }
00438         }
00439 
00440         // copies user info
00441         $this->_setUser( $oUser );
00442 
00443         // copies basket info
00444         $this->_loadFromBasket( $oBasket );
00445 
00446         // payment information
00447         $oUserPayment = $this->_setPayment( $oBasket->getPaymentId() );
00448 
00449         // set folder information, if order is new
00450         // #M575 in recalcualting order case folder must be the same as it was
00451         if ( !$blRecalculatingOrder ) {
00452             $this->_setFolder();
00453         }
00454 
00455         //saving all order data to DB
00456         $this->save();
00457 
00458         // executing payment (on failure deletes order and returns error code)
00459         // in case when recalcualting order, payment execution is skipped
00460         if ( !$blRecalculatingOrder ) {
00461             $blRet = $this->_executePayment( $oBasket, $oUserPayment );
00462             if ( $blRet !== true ) {
00463                 return $blRet;
00464             }
00465         }
00466 
00467         // executing TS protection
00468         if ( !$blRecalculatingOrder && $oBasket->getTsProductId()) {
00469             $blRet = $this->_executeTsProtection( $oBasket );
00470             if ( $blRet !== true ) {
00471                 return $blRet;
00472             }
00473         }
00474 
00475         // deleting remark info only when order is finished
00476         oxSession::deleteVar( 'ordrem' );
00477         oxSession::deleteVar( 'stsprotection' );
00478 
00479         // updating order trans status (success status)
00480         $this->_setOrderStatus( 'OK' );
00481 
00482         // store orderid
00483         $oBasket->setOrderId( $this->getId() );
00484 
00485         // updating wish lists
00486         $this->_updateWishlist( $oBasket->getContents(), $oUser );
00487 
00488         // updating users notice list
00489         $this->_updateNoticeList( $oBasket->getContents(), $oUser );
00490 
00491         // marking vouchers as used and sets them to $this->_aVoucherList (will be used in order email)
00492         // skipping this action in case of order recalculation
00493         if ( !$blRecalculatingOrder ) {
00494             $this->_markVouchers( $oBasket, $oUser );
00495         }
00496 
00497         // send order by email to shop owner and current user
00498         // skipping this action in case of order recalculation
00499         if ( !$blRecalculatingOrder ) {
00500             $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
00501         } else {
00502             $iRet = self::ORDER_STATE_OK;
00503         }
00504 
00505         return $iRet;
00506     }
00507 
00515     protected function _setOrderStatus( $sStatus )
00516     {
00517         $oDb = oxDb::getDb();
00518         $sQ = 'update oxorder set oxtransstatus='.$oDb->quote( $sStatus ).' where oxid='.$oDb->quote( $this->getId() );
00519         $oDb->execute( $sQ );
00520 
00521         //updating order object
00522         $this->oxorder__oxtransstatus = new oxField( $sStatus, oxField::T_RAW );
00523     }
00524 
00532     protected function _convertVat( $sVat )
00533     {
00534         if ( strpos( $sVat, '.' ) < strpos( $sVat, ',' ) ) {
00535             $sVat = str_replace( array( '.', ',' ), array( '', '.' ), $sVat );
00536         } else {
00537             $sVat = str_replace( ',', '', $sVat );
00538         }
00539         return (float) $sVat;
00540     }
00541 
00552     protected function _loadFromBasket( oxBasket $oBasket )
00553     {
00554         $myConfig = $this->getConfig();
00555 
00556         // store IP Adress - default must be FALSE as it is illegal to store
00557         if ( $myConfig->getConfigParam( 'blStoreIPs' ) &&  $this->oxorder__oxip->value === null ) {
00558             $this->oxorder__oxip = new oxField(oxUtilsServer::getInstance()->getRemoteAddress(), oxField::T_RAW);
00559         }
00560 
00561         // copying main price info
00562         $this->oxorder__oxtotalnetsum   = new oxField(oxUtils::getInstance()->fRound($oBasket->getDiscountedNettoPrice()), oxField::T_RAW);
00563         $this->oxorder__oxtotalbrutsum  = new oxField($oBasket->getProductsPrice()->getBruttoSum(), oxField::T_RAW);
00564         $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00565 
00566         // copying discounted VAT info
00567         $iVatIndex = 1;
00568         foreach ( $oBasket->getProductVats(false) as $iVat => $dPrice ) {
00569             $this->{"oxorder__oxartvat$iVatIndex"}      = new oxField( $this->_convertVat( $iVat ), oxField::T_RAW);
00570             $this->{"oxorder__oxartvatprice$iVatIndex"} = new oxField($dPrice, oxField::T_RAW);
00571             $iVatIndex ++;
00572         }
00573 
00574         // payment costs if available
00575         if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00576             $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00577             $this->oxorder__oxpayvat  = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00578         }
00579 
00580         // delivery info
00581         if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00582             $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00583             //V #M382: Save VAT, not VAT value for delivery costs
00584             $this->oxorder__oxdelvat  = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW); //V #M382
00585             $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00586         }
00587 
00588         // user remark
00589         if ( !isset( $this->oxorder__oxremark ) || $this->oxorder__oxremark->value === null ) {
00590             $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00591         }
00592 
00593         // currency
00594         $oCur = $myConfig->getActShopCurrencyObject();
00595         $this->oxorder__oxcurrency = new oxField($oCur->name);
00596         $this->oxorder__oxcurrate  = new oxField($oCur->rate, oxField::T_RAW);
00597 
00598         // store voucherdiscount
00599         if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00600             $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00601         }
00602 
00603         // general discount
00604         if ( $this->_blReloadDiscount ) {
00605             $dDiscount = 0;
00606             $aDiscounts = $oBasket->getDiscounts();
00607             if ( count($aDiscounts) > 0 ) {
00608                 foreach ($aDiscounts as $oDiscount) {
00609                     $dDiscount += $oDiscount->dDiscount;
00610                 }
00611             }
00612             $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00613         }
00614 
00615         //order language
00616         $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00617 
00618 
00619         // initial status - 'ERROR'
00620         $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00621 
00622         // copies basket product info ...
00623         $this->_setOrderArticles( $oBasket->getContents() );
00624 
00625         // copies wrapping info
00626         $this->_setWrapping( $oBasket );
00627 
00628         // copies TS protection info
00629         $this->_setTsProtection( $oBasket );
00630     }
00631 
00638     public function getOrderLanguage()
00639     {
00640         if ( $this->_iOrderLang === null ) {
00641             if ( isset( $this->oxorder__oxlang->value ) ) {
00642                 $this->_iOrderLang = oxLang::getInstance()->validateLanguage( $this->oxorder__oxlang->value );
00643             } else {
00644                 $this->_iOrderLang = oxLang::getInstance()->getBaseLanguage();
00645             }
00646         }
00647         return $this->_iOrderLang;
00648     }
00649 
00657     protected function _setUser( $oUser )
00658     {
00659 
00660         $this->oxorder__oxuserid        = new oxField($oUser->getId());
00661 
00662         // bill address
00663         $this->oxorder__oxbillcompany     = clone $oUser->oxuser__oxcompany;
00664         $this->oxorder__oxbillemail       = clone $oUser->oxuser__oxusername;
00665         $this->oxorder__oxbillfname       = clone $oUser->oxuser__oxfname;
00666         $this->oxorder__oxbilllname       = clone $oUser->oxuser__oxlname;
00667         $this->oxorder__oxbillstreet      = clone $oUser->oxuser__oxstreet;
00668         $this->oxorder__oxbillstreetnr    = clone $oUser->oxuser__oxstreetnr;
00669         $this->oxorder__oxbilladdinfo     = clone $oUser->oxuser__oxaddinfo;
00670         $this->oxorder__oxbillustid       = clone $oUser->oxuser__oxustid;
00671         $this->oxorder__oxbillcity        = clone $oUser->oxuser__oxcity;
00672         $this->oxorder__oxbillcountryid   = clone $oUser->oxuser__oxcountryid;
00673         $this->oxorder__oxbillstateid     = clone $oUser->oxuser__oxstateid;
00674         $this->oxorder__oxbillzip         = clone $oUser->oxuser__oxzip;
00675         $this->oxorder__oxbillfon         = clone $oUser->oxuser__oxfon;
00676         $this->oxorder__oxbillfax         = clone $oUser->oxuser__oxfax;
00677         $this->oxorder__oxbillsal         = clone $oUser->oxuser__oxsal;
00678 
00679 
00680         // delivery address
00681         if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00682             // set delivery address
00683             $this->oxorder__oxdelcompany   = clone $oDelAdress->oxaddress__oxcompany;
00684             $this->oxorder__oxdelfname     = clone $oDelAdress->oxaddress__oxfname;
00685             $this->oxorder__oxdellname     = clone $oDelAdress->oxaddress__oxlname;
00686             $this->oxorder__oxdelstreet    = clone $oDelAdress->oxaddress__oxstreet;
00687             $this->oxorder__oxdelstreetnr  = clone $oDelAdress->oxaddress__oxstreetnr;
00688             $this->oxorder__oxdeladdinfo   = clone $oDelAdress->oxaddress__oxaddinfo;
00689             $this->oxorder__oxdelcity      = clone $oDelAdress->oxaddress__oxcity;
00690             $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00691             $this->oxorder__oxdelstateid   = clone $oDelAdress->oxaddress__oxstateid;
00692             $this->oxorder__oxdelzip       = clone $oDelAdress->oxaddress__oxzip;
00693             $this->oxorder__oxdelfon       = clone $oDelAdress->oxaddress__oxfon;
00694             $this->oxorder__oxdelfax       = clone $oDelAdress->oxaddress__oxfax;
00695             $this->oxorder__oxdelsal       = clone $oDelAdress->oxaddress__oxsal;
00696         }
00697     }
00698 
00706     protected function _setWrapping( oxBasket $oBasket )
00707     {
00708         $myConfig = $this->getConfig();
00709 
00710         // wrapping price
00711         if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00712             $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00713 
00714             // wrapping VAT
00715             if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
00716                 $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00717             }
00718         }
00719 
00720         // greetings card
00721         $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00722 
00723         // card text will be stored in database
00724         $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00725     }
00726 
00735     protected function _setOrderArticles( $aArticleList )
00736     {
00737         // reset articles list
00738         $this->_oArticles = oxNew( 'oxlist' );
00739         $iCurrLang = $this->getOrderLanguage();
00740 
00741         // add all the products we have on basket to the order
00742         foreach ( $aArticleList as $oContent ) {
00743 
00744             //$oContent->oProduct = $oContent->getArticle();
00745             // #M773 Do not use article lazy loading on order save
00746             $oProduct = $oContent->getArticle( true, null, true);
00747 
00748             // copy only if object is oxarticle type
00749             if ( $oProduct->isOrderArticle() ) {
00750                 $oOrderArticle = $oProduct;
00751             } else {
00752 
00753                 // if order language doe not match product language - article must be reloaded in order language
00754                 if ( $iCurrLang != $oProduct->getLanguage() ) {
00755                     $oProduct->loadInLang( $iCurrLang, $oProduct->getProductId() );
00756                 }
00757 
00758                 // set chosen selectlist
00759                 $sSelList = '';
00760                 if ( count( $aChosenSelList = $oContent->getChosenSelList() ) ) {
00761                     foreach ( $aChosenSelList as $oItem ) {
00762                         if ( $sSelList ) {
00763                             $sSelList .= ", ";
00764                         }
00765                         $sSelList .= "{$oItem->name} : {$oItem->value}";
00766                     }
00767                 }
00768 
00769                 $oOrderArticle = oxNew( 'oxorderarticle' );
00770                 $oOrderArticle->setIsNewOrderItem( true );
00771                 $oOrderArticle->copyThis( $oProduct );
00772                 $oOrderArticle->setId();
00773 
00774                 $oOrderArticle->oxorderarticles__oxartnum     = clone $oProduct->oxarticles__oxartnum;
00775                 $oOrderArticle->oxorderarticles__oxselvariant = new oxField( trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->getRawValue() ), oxField::T_RAW );
00776                 $oOrderArticle->oxorderarticles__oxshortdesc  = new oxField( $oProduct->oxarticles__oxshortdesc->getRawValue(), oxField::T_RAW );
00777                 // #M974: duplicated entries for the name of variants in orders
00778                 $oOrderArticle->oxorderarticles__oxtitle      = new oxField( trim( $oProduct->oxarticles__oxtitle->getRawValue() ), oxField::T_RAW );
00779 
00780                 // copying persistent parameters ...
00781                 if ( !is_array( $aPersParams = $oProduct->getPersParams() ) ) {
00782                     $aPersParams = $oContent->getPersParams();
00783                 }
00784                 if ( is_array( $aPersParams ) && count( $aPersParams )) {
00785                     $oOrderArticle->oxorderarticles__oxpersparam = new oxField( serialize( $aPersParams ), oxField::T_RAW );
00786                 }
00787             }
00788 
00789             // ids, titles, numbers ...
00790             $oOrderArticle->oxorderarticles__oxorderid = new oxField( $this->getId() );
00791             $oOrderArticle->oxorderarticles__oxartid   = new oxField( $oContent->getProductId() );
00792             $oOrderArticle->oxorderarticles__oxamount  = new oxField( $oContent->getAmount() );
00793 
00794             // prices
00795             $oPrice = $oContent->getPrice();
00796             $oOrderArticle->oxorderarticles__oxnetprice  = new oxField( $oPrice->getNettoPrice(), oxField::T_RAW );
00797             $oOrderArticle->oxorderarticles__oxvatprice  = new oxField( $oPrice->getVatValue(), oxField::T_RAW );
00798             $oOrderArticle->oxorderarticles__oxbrutprice = new oxField( $oPrice->getBruttoPrice(), oxField::T_RAW );
00799             $oOrderArticle->oxorderarticles__oxvat       = new oxField( $oPrice->getVat(), oxField::T_RAW );
00800 
00801             $oUnitPtice = $oContent->getUnitPrice();
00802             $oOrderArticle->oxorderarticles__oxnprice = new oxField( $oUnitPtice->getNettoPrice(), oxField::T_RAW );
00803             $oOrderArticle->oxorderarticles__oxbprice = new oxField( $oUnitPtice->getBruttoPrice(), oxField::T_RAW );
00804 
00805             // wrap id
00806             $oOrderArticle->oxorderarticles__oxwrapid = new oxField( $oContent->getWrappingId(), oxField::T_RAW );
00807 
00808             // items shop id
00809             $oOrderArticle->oxorderarticles__oxordershopid = new oxField( $oContent->getShopId(), oxField::T_RAW );
00810 
00811             // bundle?
00812             $oOrderArticle->oxorderarticles__oxisbundle = new oxField( $oContent->isBundle() );
00813 
00814             // add information for eMail
00815             //P
00816             //TODO: check if this assign is needed at all
00817             $oOrderArticle->oProduct = $oProduct;
00818 
00819             // simulatin order article list
00820             $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
00821         }
00822     }
00823 
00835     protected function _executePayment( oxBasket $oBasket, $oUserpayment )
00836     {
00837         $oPayTransaction = $this->_getGateway();
00838         $oPayTransaction->setPaymentParams( $oUserpayment );
00839 
00840         if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
00841             $this->delete();
00842 
00843             // checking for error messages
00844             if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
00845                 if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
00846                     return $sLastError;
00847                 }
00848             }
00849 
00850             // checking for error codes
00851             if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
00852                 if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
00853                     return $iLastErrorNo;
00854                 }
00855             }
00856 
00857             return self::ORDER_STATE_PAYMENTERROR; // means no authentication
00858         }
00859         return true; // everything fine
00860     }
00861 
00868     protected function _getGateway()
00869     {
00870         return oxNew( 'oxPaymentGateway' );
00871     }
00872 
00880     protected function _setPayment( $sPaymentid )
00881     {
00882         // copying payment info fields
00883         $aDynvalue = oxSession::getVar( 'dynvalue' );
00884         $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
00885 
00886         // loading payment object
00887         $oPayment = oxNew( 'oxpayment' );
00888 
00889         if (!$oPayment->load( $sPaymentid )) {
00890             return null;
00891         }
00892 
00893         // #756M Preserve already stored payment information
00894         if ( !$aDynvalue && ( $oUserpayment = $this->getPaymentType() ) ) {
00895             if ( is_array( $aStoredDynvalue = $oUserpayment->getDynValues() ) ) {
00896                 foreach ( $aStoredDynvalue as $oVal ) {
00897                     $aDynvalue[$oVal->name] = $oVal->value;
00898                 }
00899             }
00900         }
00901 
00902         $oPayment->setDynValues( oxUtils::getInstance()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
00903 
00904         // collecting dynamic values
00905         $aDynVal = array();
00906 
00907         if ( is_array( $aPaymentDynValues = $oPayment->getDynValues() ) ) {
00908             foreach ( $aPaymentDynValues  as $key => $oVal ) {
00909                 if ( isset( $aDynvalue[$oVal->name] ) ) {
00910                     $oVal->value = $aDynvalue[$oVal->name];
00911                 }
00912 
00913                 //$oPayment->setDynValue($key, $oVal);
00914                 $aPaymentDynValues[$key] = $oVal;
00915                 $aDynVal[$oVal->name] = $oVal->value;
00916             }
00917         }
00918 
00919         // Store this payment information, we might allow users later to
00920         // reactivate already give payment informations
00921 
00922         $oUserpayment = oxNew( 'oxuserpayment' );
00923         $oUserpayment->oxuserpayments__oxuserid     = clone $this->oxorder__oxuserid;
00924         $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
00925         $oUserpayment->oxuserpayments__oxvalue      = new oxField(oxUtils::getInstance()->assignValuesToText( $aDynVal ), oxField::T_RAW);
00926         $oUserpayment->oxpayments__oxdesc           = clone $oPayment->oxpayments__oxdesc;
00927         $oUserpayment->oxpayments__oxlongdesc       = clone $oPayment->oxpayments__oxlongdesc;
00928         $oUserpayment->setDynValues( $aPaymentDynValues );
00929         $oUserpayment->save();
00930 
00931         // storing payment information to order
00932         $this->oxorder__oxpaymentid   = new oxField($oUserpayment->getId(), oxField::T_RAW);
00933         $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
00934 
00935         // returning user payment object which will be used later in code ...
00936         return $oUserpayment;
00937     }
00938 
00944     protected function _setFolder()
00945     {
00946         $myConfig = $this->getConfig();
00947         $this->oxorder__oxfolder    = new oxField(key( $myConfig->getShopConfVar(  'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
00948     }
00949 
00959     protected function _updateWishlist( $aArticleList, $oUser )
00960     {
00961 
00962         foreach ( $aArticleList as $oContent) {
00963             if ( ( $sWishId = $oContent->getWishId() ) ) {
00964 
00965                 // checking which wishlist user uses ..
00966                 if ( $sWishId == $oUser->getId() ) {
00967                     $oUserBasket = $oUser->getBasket( 'wishlist' );
00968                 } else {
00969                     $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
00970                     $oUserBasket = oxNew( 'oxuserbasket' );
00971                     $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
00972                 }
00973 
00974                 // updating users wish list
00975                 if ( $oUserBasket ) {
00976                     if ( !($sProdId = $oContent->getWishArticleId() )) {
00977                         $sProdId = $oContent->getProductId();
00978                     }
00979                     $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00980                     $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00981                     if ( $dNewAmount < 0) {
00982                         $dNewAmount = 0;
00983                     }
00984                     $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00985                 }
00986             }
00987         }
00988     }
00989 
00999     protected function _updateNoticeList( $aArticleList, $oUser )
01000     {
01001         // loading users notice list ..
01002         if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
01003             // only if wishlist is enabled
01004             foreach ( $aArticleList as $oContent) {
01005                 $sProdId = $oContent->getProductId();
01006 
01007                 // updating users notice list
01008                 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList(), $oContent->getPersParams() );
01009                 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
01010                 if ( $dNewAmount < 0) {
01011                     $dNewAmount = 0;
01012                 }
01013                 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true, $oContent->getPersParams() );
01014             }
01015         }
01016     }
01017 
01027     protected function _markVouchers( $oBasket, $oUser )
01028     {
01029         $this->_aVoucherList = $oBasket->getVouchers();
01030 
01031         if ( is_array( $this->_aVoucherList ) ) {
01032             foreach ( $this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
01033                 $oVoucher = oxNew( 'oxvoucher' );
01034                 $oVoucher->load( $sVoucherId );
01035                 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->dVoucherdiscount );
01036 
01037                 $this->_aVoucherList[$sVoucherId] = $oVoucher;
01038             }
01039         }
01040     }
01041 
01047     public function save()
01048     {
01049         if ( ( $blSave = parent::save() ) ) {
01050 
01051             // saving order articles
01052             $oOrderArticles = $this->getOrderArticles();
01053             if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
01054                 foreach ( $oOrderArticles as $oOrderArticle ) {
01055                     $oOrderArticle->save();
01056                 }
01057             }
01058         }
01059 
01060         return $blSave;
01061     }
01062 
01069     public function getDelAddressInfo()
01070     {
01071         $oDelAdress = null;
01072         if (! ($soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
01073             $soxAddressId = oxSession::getVar( 'deladrid' );
01074         }
01075         if ( $soxAddressId ) {
01076             $oDelAdress = oxNew( 'oxaddress' );
01077             $oDelAdress->load( $soxAddressId );
01078 
01079             //get delivery country name from delivery country id
01080             if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
01081                  $oCountry = oxNew( 'oxcountry' );
01082                  $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
01083                  $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
01084             }
01085         }
01086         return $oDelAdress;
01087     }
01088 
01099     public function validateStock( $oBasket )
01100     {
01101         foreach ( $oBasket->getContents() as $key => $oContent ) {
01102             try {
01103                 $oProd = $oContent->getArticle();
01104             } catch ( oxNoArticleException $oEx ) {
01105                 $oBasket->removeItem( $key );
01106                 throw $oEx;
01107             } catch ( oxArticleInputException $oEx ) {
01108                 $oBasket->removeItem( $key );
01109                 throw $oEx;
01110             }
01111 
01112             // check if its still available
01113             $dArtStockAmount = $oBasket->getArtStockInBasket( $oProd->getId(), $key );
01114             $iOnStock = $oProd->checkForStock( $oContent->getAmount(), $dArtStockAmount );
01115             if ( $iOnStock !== true ) {
01116                 $oEx = oxNew( 'oxOutOfStockException' );
01117                 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
01118                 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
01119                 $oEx->setProductId( $oProd->getId() );
01120 
01121                 if (!is_numeric($iOnStock)) {
01122                     $iOnStock = 0;
01123                 }
01124                 $oEx->setRemainingAmount( $iOnStock );
01125                 throw $oEx;
01126             }
01127         }
01128     }
01129 
01135     protected function _insert()
01136     {
01137         $myConfig = $this->getConfig();
01138         $oUtilsDate = oxUtilsDate::getInstance();
01139 
01140         //V #M525 orderdate must be the same as it was
01141         if ( !$this->oxorder__oxorderdate->value ) {
01142             $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
01143         } else {
01144             $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
01145         }
01146         $this->oxorder__oxshopid    = new oxField($myConfig->getShopId(), oxField::T_RAW);
01147 
01148         $this->oxorder__oxsenddate  = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01149 
01150         if ( ( $blInsert = parent::_insert() ) ) {
01151             // setting order number
01152             if ( !$this->oxorder__oxordernr->value ) {
01153                 $aWhere = '';
01154                 // separate order numbers for shops ...
01155                 if ( $this->_blSeparateNumbering ) {
01156                     $aWhere = array( 'oxshopid = "'.$myConfig->getShopId().'"' );
01157                 }
01158                 $this->_setRecordNumber( 'oxordernr', $aWhere );
01159             }
01160         }
01161         return $blInsert;
01162     }
01163 
01169     protected function _update()
01170     {
01171         $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01172         return parent::_update();
01173     }
01174 
01183     public function delete( $sOxId = null )
01184     {
01185         if ( $sOxId ) {
01186             if ( !$this->load( $sOxId ) ) {
01187                 // such order does not exist
01188                 return false;
01189             }
01190         } elseif ( !$sOxId ) {
01191             $sOxId = $this->getId();
01192         }
01193 
01194         // no order id is passed
01195         if ( !$sOxId ) {
01196             return false;
01197         }
01198 
01199 
01200         // delete order articles
01201         $oOrderArticles = $this->getOrderArticles( false );
01202         foreach ( $oOrderArticles as $oOrderArticle ) {
01203             $oOrderArticle->delete();
01204         }
01205 
01206         // #440 - deleting user payment info
01207         if ( $oPaymentType = $this->getPaymentType() ) {
01208             $oPaymentType->delete();
01209         }
01210 
01211         return parent::delete( $sOxId );
01212     }
01213 
01223     public function recalculateOrder( $aNewArticles = array() )
01224     {
01225         oxDb::startTransaction();
01226 
01227         try {
01228             $oBasket = $this->_getOrderBasket();
01229 
01230             // add this order articles to virtual basket and recalculates basket
01231             $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles( true ) );
01232 
01233             // adding new articles to existing order
01234             $this->_addArticlesToBasket( $oBasket, $aNewArticles );
01235 
01236             // recalculating basket
01237             $oBasket->calculateBasket( true );
01238 
01239             //finalizing order (skipping payment execution, vouchers marking and mail sending)
01240             $iRet = $this->finalizeOrder( $oBasket, $this->getOrderUser(), true );
01241 
01242             //if finalizing order failed, rollback transaction
01243             if ( $iRet !== 1 ) {
01244                 oxDb::rollbackTransaction();
01245             } else {
01246                 oxDb::commitTransaction();
01247             }
01248 
01249         } catch( Exception $oE ) {
01250             // if exception, rollBack everything
01251             oxDb::rollbackTransaction();
01252 
01253             if ( defined( 'OXID_PHP_UNIT' ) ) {
01254                 throw $oE;
01255             }
01256         }
01257     }
01258 
01259     protected $_oOrderBasket = null;
01267     protected function _getOrderBasket( $blStockCheck = true )
01268     {
01269         $this->_oOrderBasket = oxNew( "oxbasket" );
01270 
01271         // setting stock check mode
01272         $this->_oOrderBasket->setStockCheckMode( $blStockCheck );
01273 
01274         // setting virtual basket user
01275         $this->_oOrderBasket->setBasketUser( $this->getOrderUser() );
01276 
01277         // transferring order id
01278         $this->_oOrderBasket->setOrderId( $this->getId() );
01279 
01280         // setting basket currency order uses
01281         $aCurrencies = $this->getConfig()->getCurrencyArray();
01282         foreach ( $aCurrencies as $oCur ) {
01283             if ($oCur->name == $this->oxorder__oxcurrency->value) {
01284                 $oBasketCur = $oCur;
01285                 break;
01286             }
01287         }
01288 
01289         // setting currency
01290         $this->_oOrderBasket->setBasketCurrency( $oBasketCur );
01291 
01292         // set basket card id and message
01293         $this->_oOrderBasket->setCardId( $this->oxorder__oxcardid->value );
01294         $this->_oOrderBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01295 
01296         if ( $this->_blReloadDiscount ) {
01297             $oDb = oxDb::getDb( true );
01298             // disabling availability check
01299             $this->_oOrderBasket->setSkipVouchersChecking( true );
01300 
01301             // add previously used vouchers
01302             $sQ = 'select oxid from oxvouchers where oxorderid = '.$oDb->quote( $this->getId() );
01303             $aVouchers = $oDb->getAll( $sQ );
01304             foreach ( $aVouchers as $aVoucher ) {
01305                 $this->_oOrderBasket->addVoucher( $aVoucher['oxid'] );
01306             }
01307         } else {
01308             $this->_oOrderBasket->setDiscountCalcMode( false );
01309             $this->_oOrderBasket->setVoucherDiscount( $this->oxorder__oxvoucherdiscount->value );
01310             $this->_oOrderBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
01311         }
01312 
01313         // must be kept old delivery?
01314         if ( !$this->_blReloadDelivery ) {
01315             $this->_oOrderBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01316         } else {
01317             //  set shipping
01318             $this->_oOrderBasket->setShipping( $this->oxorder__oxdeltype->value );
01319             $this->_oOrderBasket->setDeliveryPrice( null );
01320         }
01321 
01322         //set basket payment
01323         $this->_oOrderBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01324 
01325         return $this->_oOrderBasket;
01326     }
01327 
01336     public function setDelivery( $sDeliveryId )
01337     {
01338         $this->reloadDelivery( true );
01339         $this->oxorder__oxdeltype = new oxField( $sDeliveryId );
01340     }
01341 
01347     public function getOrderUser()
01348     {
01349         if ($this->_oUser === null ) {
01350             $this->_oUser = oxNew( "oxuser" );
01351             $this->_oUser->load( $this->oxorder__oxuserid->value );
01352 
01353             // if object is loaded then reusing its order info
01354             if ( $this->_isLoaded ) {
01355                 // bill address
01356                 $this->_oUser->oxuser__oxcompany  = clone $this->oxorder__oxbillcompany;
01357                 $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
01358                 $this->_oUser->oxuser__oxfname    = clone $this->oxorder__oxbillfname;
01359                 $this->_oUser->oxuser__oxlname    = clone $this->oxorder__oxbilllname;
01360                 $this->_oUser->oxuser__oxstreet   = clone $this->oxorder__oxbillstreet;
01361                 $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
01362                 $this->_oUser->oxuser__oxaddinfo  = clone $this->oxorder__oxbilladdinfo;
01363                 $this->_oUser->oxuser__oxustid    = clone $this->oxorder__oxbillustid;
01364 
01365 
01366                 $this->_oUser->oxuser__oxcity      = clone $this->oxorder__oxbillcity;
01367                 $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
01368                 $this->_oUser->oxuser__oxstateid   = clone $this->oxorder__oxbillstateid;
01369                 $this->_oUser->oxuser__oxzip       = clone $this->oxorder__oxbillzip;
01370                 $this->_oUser->oxuser__oxfon       = clone $this->oxorder__oxbillfon;
01371                 $this->_oUser->oxuser__oxfax       = clone $this->oxorder__oxbillfax;
01372                 $this->_oUser->oxuser__oxsal       = clone $this->oxorder__oxbillsal;
01373             }
01374         }
01375 
01376         return $this->_oUser;
01377     }
01378 
01386     public function pdfFooter( $oPdf )
01387     {
01388     }
01389 
01397     public function pdfHeaderplus( $oPdf )
01398     {
01399     }
01400 
01408     public function pdfHeader( $oPdf )
01409     {
01410     }
01411 
01420     public function genPdf( $sFilename, $iSelLang = 0 )
01421     {
01422     }
01423 
01429     public function getInvoiceNum()
01430     {
01431         $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01432         return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01433     }
01434 
01440     public function getNextBillNum()
01441     {
01442         $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01443         return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01444     }
01445 
01451     public function getShippingSetList()
01452     {
01453         // in which country we deliver
01454         if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
01455             $sShipId = $this->oxorder__oxbillcountryid->value;
01456         }
01457 
01458         $oBasket = $this->_getOrderBasket( false );
01459 
01460         // unsetting bundles
01461         $oOrderArticles = $this->getOrderArticles();
01462         foreach ( $oOrderArticles as $sItemId => $oItem ) {
01463             if ( $oItem->isBundle() ) {
01464                 $oOrderArticles->offsetUnset( $sItemId );
01465             }
01466         }
01467 
01468         // add this order articles to basket and recalculate basket
01469         $this->_addOrderArticlesToBasket( $oBasket, $oOrderArticles );
01470 
01471         // recalculating basket
01472         $oBasket->calculateBasket( true );
01473 
01474         // load fitting deliveries list
01475         $oDeliveryList = oxNew( "oxDeliveryList", "core" );
01476         $oDeliveryList->setCollectFittingDeliveriesSets( true );
01477 
01478         return $oDeliveryList->getDeliveryList( $oBasket, $this->getOrderUser(), $sShipId );
01479     }
01480 
01486     public function getVoucherNrList()
01487     {
01488         $oDB = oxDb::getDb( true );
01489         $aVouchers = array();
01490         $sSelect = "select oxvouchernr from oxvouchers where oxorderid = ".$oDB->quote( $this->oxorder__oxid->value );
01491         $rs = $oDB->execute( $sSelect);
01492         if ($rs != false && $rs->recordCount() > 0) {
01493             while (!$rs->EOF) {
01494                 $aVouchers[] = $rs->fields['oxvouchernr'];
01495                 $rs->moveNext();
01496             }
01497         }
01498         return $aVouchers;
01499     }
01500 
01508     public function getOrderSum( $blToday = false )
01509     {
01510         $sSelect  = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01511         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01512 
01513         if ( $blToday ) {
01514             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01515         }
01516 
01517         return ( double ) oxDb::getDb()->getOne( $sSelect );
01518     }
01519 
01527     public function getOrderCnt( $blToday = false )
01528     {
01529         $sSelect  = 'select count(*) from oxorder where ';
01530         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'"  and oxorder.oxstorno != "1" ';
01531 
01532         if ( $blToday ) {
01533             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01534         }
01535 
01536         return ( int ) oxDb::getDb()->getOne( $sSelect );
01537     }
01538 
01539 
01547     protected function _checkOrderExist( $sOxId = null )
01548     {
01549         if ( !$sOxId) {
01550             return false;
01551         }
01552 
01553         $oDb = oxDb::getDb();
01554         if ( $oDb->getOne( 'select oxid from oxorder where oxid = '.$oDb->quote( $sOxId ) ) ) {
01555             return true;
01556         }
01557 
01558         return false;
01559     }
01560 
01570     protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01571     {
01572         $iRet = self::ORDER_STATE_MAILINGERROR;
01573 
01574         // add user, basket and payment to order
01575         $this->_oUser    = $oUser;
01576         $this->_oBasket  = $oBasket;
01577         $this->_oPayment = $oPayment;
01578 
01579         $oxEmail = oxNew( 'oxemail' );
01580 
01581         // send order email to user
01582         if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01583             // mail to user was successfully sent
01584             $iRet = self::ORDER_STATE_OK;
01585         }
01586 
01587         // send order email to shop owner
01588         $oxEmail->sendOrderEMailToOwner( $this );
01589 
01590         return $iRet;
01591     }
01592 
01598     public function getBasket()
01599     {
01600         return $this->_oBasket;
01601     }
01602 
01608     public function getPayment()
01609     {
01610         return $this->_oPayment;
01611     }
01612 
01618     public function getVoucherList()
01619     {
01620         return $this->_aVoucherList;
01621     }
01622 
01628     public function getDelSet()
01629     {
01630         if ( $this->_oDelSet == null ) {
01631             // load deliveryset info
01632             $this->_oDelSet = oxNew( 'oxdeliveryset' );
01633             $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01634         }
01635 
01636         return $this->_oDelSet;
01637     }
01638 
01644     public function getPaymentType()
01645     {
01646         if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType === null ) {
01647             $this->_oPaymentType = false;
01648             $oPaymentType = oxNew( 'oxuserpayment' );
01649             if ( $oPaymentType->load( $this->oxorder__oxpaymentid->value ) ) {
01650                 $this->_oPaymentType = $oPaymentType;
01651             }
01652         }
01653 
01654         return $this->_oPaymentType;
01655     }
01656 
01662     public function getGiftCard()
01663     {
01664         if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01665             $this->_oGiftCard = oxNew( 'oxwrapping' );
01666             $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01667         }
01668 
01669         return $this->_oGiftCard;
01670     }
01671 
01679     public function setSeparateNumbering( $blSeparateNumbering = null )
01680     {
01681         $this->_blSeparateNumbering = $blSeparateNumbering;
01682     }
01683 
01691     public function getLastUserPaymentType( $sUserId)
01692     {
01693         $oDb = oxDb::getDb();
01694         $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid='.$oDb->quote( $sUserId ).' order by oxorder.oxorderdate desc ';
01695         $sLastPaymentId = $oDb->getOne( $sQ );
01696         return $sLastPaymentId;
01697     }
01698 
01707     protected function _addOrderArticlesToBasket( $oBasket, $aOrderArticles )
01708     {
01709         // if no order articles, return empty basket
01710         if ( count( $aOrderArticles ) > 0 ) {
01711 
01712             //adding order articles to basket
01713             foreach ( $aOrderArticles as $oOrderArticle ) {
01714                 $oBasket->addOrderArticleToBasket( $oOrderArticle );
01715             }
01716         }
01717     }
01718 
01727     protected function _addArticlesToBasket( $oBasket, $aArticles )
01728     {
01729         // if no order articles
01730         if ( count($aArticles ) > 0 ) {
01731 
01732             //adding order articles to basket
01733             foreach ( $aArticles as $oArticle ) {
01734                 $aSel = isset( $oArticle->oxorderarticles__oxselvariant ) ? $oArticle->oxorderarticles__oxselvariant->value : null;
01735                 $aPersParam = isset( $oArticle->oxorderarticles__oxpersparam ) ? $oArticle->getPersParams() : null;
01736                 $oBasket->addToBasket( $oArticle->oxorderarticles__oxartid->value,
01737                                        $oArticle->oxorderarticles__oxamount->value,
01738                                        $aSel, $aPersParam );
01739             }
01740         }
01741     }
01742 
01748     public function getTotalOrderSum()
01749     {
01750         $oCur = $this->getConfig()->getActShopCurrencyObject();
01751         return number_format( (double)$this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01752     }
01753 
01761     public function getProductVats( $blFormatCurrency = true )
01762     {
01763         $aVats = array();
01764         if ($this->oxorder__oxartvat1->value) {
01765             $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
01766         }
01767         if ($this->oxorder__oxartvat2->value) {
01768             $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
01769         }
01770 
01771         if ( $blFormatCurrency ) {
01772             $oLang = oxLang::getInstance();
01773             $oCur = $this->getConfig()->getActShopCurrencyObject();
01774             foreach ( $aVats as $sKey => $dVat ) {
01775                 $aVats[$sKey] = $oLang->formatCurrency( $dVat, $oCur );
01776             }
01777         }
01778         return $aVats;
01779     }
01780 
01786     public function getBillCountry()
01787     {
01788         if ( !$this->oxorder__oxbillcountry->value ) {
01789             $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
01790         }
01791         return $this->oxorder__oxbillcountry;
01792     }
01793 
01799     public function getDelCountry()
01800     {
01801         if ( !$this->oxorder__oxdelcountry->value ) {
01802             $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
01803         }
01804         return $this->oxorder__oxdelcountry;
01805     }
01813     public function reloadDelivery( $blReload )
01814     {
01815         $this->_blReloadDelivery = $blReload;
01816     }
01817 
01825     public function reloadDiscount( $blReload )
01826     {
01827         $this->_blReloadDiscount = $blReload;
01828     }
01829 
01835     public function cancelOrder()
01836     {
01837         $this->oxorder__oxstorno = new oxField( 1 );
01838         if ( $this->save() ) {
01839 
01840 
01841             // canceling ordered products
01842             foreach ( $this->getOrderArticles() as $oOrderArticle ) {
01843 
01844 
01845                 $oOrderArticle->cancelOrderArticle();
01846             }
01847         }
01848     }
01849 
01856     public function getOrderCurrency()
01857     {
01858         if ( $this->_oOrderCurrency === null ) {
01859 
01860             // setting default in case unrecognized currency was set during order
01861             $aCurrencies = $this->getConfig()->getCurrencyArray();
01862             $this->_oOrderCurrency = current( $aCurrencies );
01863 
01864             foreach ( $aCurrencies as $oCurr ) {
01865                 if ( $oCurr->name == $this->oxorder__oxcurrency->value ) {
01866                     $this->_oOrderCurrency = $oCurr;
01867                     break;
01868                 }
01869             }
01870         }
01871         return $this->_oOrderCurrency;
01872     }
01873 
01883     public function validateOrder( $oBasket, $oUser )
01884     {
01885         // validating stock
01886         $iValidState = $this->validateStock( $oBasket );
01887 
01888         if ( !$iValidState ) {
01889             // validating delivery
01890             $iValidState = $this->validateDelivery( $oBasket );
01891         }
01892 
01893         if ( !$iValidState ) {
01894             // validating payment
01895             $iValidState = $this->validatePayment( $oBasket );
01896         }
01897 
01898         return $iValidState;
01899     }
01900 
01909     public function validateDelivery( $oBasket )
01910     {
01911         // proceed with no delivery
01912         // used for other countries
01913         if ( $oBasket->getPaymentId() == 'oxempty') {
01914             return;
01915         }
01916         $oDb = oxDb::getDb();
01917 
01918         $oDelSet = oxNew( "oxdeliveryset" );
01919         $sTable = $oDelSet->getViewName();
01920 
01921         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
01922          $oDb->quote( $oBasket->getShippingId() )." and ".$oDelSet->getSqlActiveSnippet();
01923 
01924         if ( !$oDb->getOne( $sQ ) ) {
01925             // throwing exception
01926             return self::ORDER_STATE_INVALIDDELIVERY;
01927         }
01928     }
01929 
01938     public function validatePayment( $oBasket )
01939     {
01940         $oDb = oxDb::getDb();
01941 
01942         $oPayment = oxNew( "oxpayment" );
01943         $sTable = $oPayment->getViewName();
01944 
01945         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
01946         $oDb->quote( $oBasket->getPaymentId() )." and ".$oPayment->getSqlActiveSnippet();
01947 
01948         if ( !$oDb->getOne( $sQ ) ) {
01949             return self::ORDER_STATE_INVALIDPAYMENT;
01950         }
01951     }
01952 
01960     protected function _setTsProtection( oxBasket $oBasket )
01961     {
01962         // protection price
01963         if ( ( $oTsProtectionCost = $oBasket->getCosts( 'oxtsprotection' ) ) ) {
01964             $this->oxorder__oxtsprotectcosts = new oxField($oTsProtectionCost->getBruttoPrice(), oxField::T_RAW);
01965         }
01966 
01967         // protection protduct id
01968         $this->oxorder__oxtsprotectid = new oxField($oBasket->getTsProductId(), oxField::T_RAW);
01969     }
01970 
01979     protected function _executeTsProtection( oxBasket $oBasket )
01980     {
01981         $aValues['tsProductId'] = $this->oxorder__oxtsprotectid->value;
01982         $aValues['amount'] = $oBasket->getPrice()->getBruttoPrice();
01983         $oCur = $this->getConfig()->getActShopCurrencyObject();
01984         $aValues['currency'] = $oCur->name;
01985         $aValues['buyerEmail'] = $this->oxorder__oxbillemail->value;
01986         $aValues['shopCustomerID'] = $this->oxorder__oxuserid->value;
01987         $aValues['shopOrderID'] = $this->oxorder__oxordernr->value;
01988         $aValues['orderDate'] = $this->oxorder__oxorderdate->value;
01989         $sPaymentId = $oBasket->getPaymentId();
01990         $oTsProtection = oxNew('oxtsprotection');
01991         $blRes = $oTsProtection->requestForTsProtection( $aValues, $sPaymentId );
01992         /*if ( !$blRes ) {
01993             $this->delete();
01994             return self::ORDER_STATE_INVALIDTSPROTECTION;
01995         }*/
01996         return true; // everything fine
01997     }
01998 
02004     public function getFormattedTotalNetSum()
02005     {
02006         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalnetsum->value, $this->getOrderCurrency() );
02007     }
02008 
02014     public function getFormattedTotalBrutSum()
02015     {
02016         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $this->getOrderCurrency() );
02017     }
02018 
02024     public function getFormattedeliveryCost()
02025     {
02026         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxdelcost->value, $this->getOrderCurrency() );
02027     }
02028 
02034     public function getFormattedPayCost()
02035     {
02036        return oxLang::getInstance()->formatCurrency( $this->oxorder__oxpaycost->value, $this->getOrderCurrency() );
02037     }
02038 
02044     public function getFormattedWrapCost()
02045     {
02046         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxwrapcost->value, $this->getOrderCurrency() );
02047     }
02048 
02054     public function getFormattedTotalVouchers()
02055     {
02056         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $this->getOrderCurrency() );
02057     }
02058 
02064     public function getFormattedDiscount()
02065     {
02066         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxdiscount->value, $this->getOrderCurrency() );
02067     }
02068 
02074     public function getFormattedTotalOrderSum()
02075     {
02076         return oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalordersum->value, $this->getOrderCurrency() );
02077     }
02078 
02084     public function getShipmentTrackingUrl()
02085     {
02086         if ( $this->_sShipTrackUrl === null && $this->oxorder__oxtrackcode->value ) {
02087             $this->_sShipTrackUrl = "http://www.dpd.de/cgi-bin/delistrack?typ=1&amp;lang=de&amp;pknr=".$this->oxorder__oxtrackcode->value;
02088         }
02089 
02090         return $this->_sShipTrackUrl;
02091     }
02092 }