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         //settting deprecated template variables
00239         $this->_setDeprecatedValues();
00240     }
00241 
00249     protected function _getCountryTitle( $sCountryId )
00250     {
00251         $sTitle = null;
00252         if ( $sCountryId && $sCountryId != '-1' ) {
00253             $oCountry = oxNew( 'oxcountry' );
00254             $oCountry->load( $sCountryId );
00255             $sTitle = $oCountry->oxcountry__oxtitle->value;
00256         }
00257 
00258         return $sTitle;
00259     }
00260 
00268     public function getOrderArticles( $blExcludeCanceled = false )
00269     {
00270         // checking set value
00271         if ( $this->_oArticles === null  ) {
00272             $sTable = getViewName( "oxorderarticles" );
00273             $sSelect = "select {$sTable}.* from {$sTable}
00274                         where {$sTable}.oxorderid = '".$this->getId() . "'" .
00275                         ( $blExcludeCanceled ? " and {$sTable}.oxstorno != 1 ": " " ) ."
00276                         order by {$sTable}.oxartid";
00277 
00278             // order articles
00279             $oArticles = oxNew( 'oxlist' );
00280             $oArticles->init( 'oxorderarticle' );
00281             $oArticles->selectString( $sSelect );
00282 
00283             // is value was not set, just returning it
00284             return $oArticles;
00285         }
00286         return $this->_oArticles;
00287     }
00288 
00296     public function setOrderArticleList( $aOrderArticleList )
00297     {
00298         $this->_oArticles = $aOrderArticleList;
00299     }
00300 
00306     public function getOrderDeliveryPrice()
00307     {
00308         if ( $this->_oDelPrice != null ) {
00309             return $this->_oDelPrice;
00310         }
00311 
00312         $this->_oDelPrice = oxNew( 'oxprice' );
00313         $this->_oDelPrice->setBruttoPriceMode();
00314         $this->_oDelPrice->setPrice( $this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value );
00315         return $this->_oDelPrice;
00316     }
00317 
00323     public function getOrderWrappingPrice()
00324     {
00325         if ( $this->_oWrappingPrice != null ) {
00326             return $this->_oWrappingPrice;
00327         }
00328 
00329         $this->_oWrappingPrice = oxNew( 'oxprice' );
00330         $this->_oWrappingPrice->setBruttoPriceMode();
00331         $this->_oWrappingPrice->setPrice( $this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value );
00332         return $this->_oWrappingPrice;
00333     }
00334 
00340     public function getOrderPaymentPrice()
00341     {
00342         if ( $this->_oPaymentPrice != null ) {
00343             return $this->_oPaymentPrice;
00344         }
00345 
00346         $this->_oPaymentPrice = oxNew( 'oxprice' );
00347         $this->_oPaymentPrice->setBruttoPriceMode();
00348         $this->_oPaymentPrice->setPrice( $this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value );
00349         return $this->_oPaymentPrice;
00350     }
00351 
00357     public function getOrderTsProtectionPrice()
00358     {
00359         if ( $this->_oTsProtectionPrice != null ) {
00360             return $this->_oTsProtectionPrice;
00361         }
00362 
00363         $this->_oTsProtectionPrice = oxNew( 'oxprice' );
00364         $this->_oTsProtectionPrice->setBruttoPriceMode();
00365         $this->_oTsProtectionPrice->setPrice( $this->oxorder__oxtsprotectcosts->value, $this->getConfig()->getConfigParam( 'dDefaultVAT' ) );
00366         return $this->_oTsProtectionPrice;
00367     }
00368 
00375     public function getOrderNetSum()
00376     {
00377         $dTotalNetSum = 0;
00378 
00379         $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
00380         $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
00381         $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
00382         $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
00383 
00384         return $dTotalNetSum;
00385     }
00386 
00407     public function finalizeOrder( oxBasket $oBasket, $oUser, $blRecalculatingOrder = false )
00408     {
00409         // check if this order is already stored
00410         $sGetChallenge = oxSession::getVar( 'sess_challenge' );
00411         if ( $this->_checkOrderExist( $sGetChallenge ) ) {
00412             oxUtils::getInstance()->logger( 'BLOCKER' );
00413             // we might use this later, this means that somebody klicked like mad on order button
00414             return self::ORDER_STATE_ORDEREXISTS;
00415         }
00416 
00417         // if not recalculating order, use sess_challenge id, else leave old order id
00418         if ( !$blRecalculatingOrder ) {
00419             // use this ID
00420             $this->setId( $sGetChallenge );
00421 
00422             // validating various order/basket parameters before finalizing
00423             if ( $iOrderState = $this->validateOrder( $oBasket, $oUser ) ) {
00424                 return $iOrderState;
00425             }
00426         }
00427 
00428         // copies user info
00429         $this->_setUser( $oUser );
00430 
00431         // copies basket info
00432         $this->_loadFromBasket( $oBasket );
00433 
00434         // payment information
00435         $oUserPayment = $this->_setPayment( $oBasket->getPaymentId() );
00436 
00437         // set folder information, if order is new
00438         // #M575 in recalcualting order case folder must be the same as it was
00439         if ( !$blRecalculatingOrder ) {
00440             $this->_setFolder();
00441         }
00442 
00443         //saving all order data to DB
00444         $this->save();
00445 
00446         // executing payment (on failure deletes order and returns error code)
00447         // in case when recalcualting order, payment execution is skipped
00448         if ( !$blRecalculatingOrder ) {
00449             $blRet = $this->_executePayment( $oBasket, $oUserPayment );
00450             if ( $blRet !== true ) {
00451                 return $blRet;
00452             }
00453         }
00454 
00455         // executing TS protection
00456         if ( !$blRecalculatingOrder && $oBasket->getTsProductId()) {
00457             $blRet = $this->_executeTsProtection( $oBasket );
00458             if ( $blRet !== true ) {
00459                 return $blRet;
00460             }
00461         }
00462 
00463         // deleting remark info only when order is finished
00464         oxSession::deleteVar( 'ordrem' );
00465         oxSession::deleteVar( 'stsprotection' );
00466 
00467         // updating order trans status (success status)
00468         $this->_setOrderStatus( 'OK' );
00469 
00470         // store orderid
00471         $oBasket->setOrderId( $this->getId() );
00472 
00473         // updating wish lists
00474         $this->_updateWishlist( $oBasket->getContents(), $oUser );
00475 
00476         // updating users notice list
00477         $this->_updateNoticeList( $oBasket->getContents(), $oUser );
00478 
00479         // marking vouchers as used and sets them to $this->_aVoucherList (will be used in order email)
00480         // skipping this action in case of order recalculation
00481         if ( !$blRecalculatingOrder ) {
00482             $this->_markVouchers( $oBasket, $oUser );
00483         }
00484 
00485         // send order by email to shop owner and current user
00486         // skipping this action in case of order recalculation
00487         if ( !$blRecalculatingOrder ) {
00488             $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
00489         } else {
00490             $iRet = self::ORDER_STATE_OK;
00491         }
00492 
00493         return $iRet;
00494     }
00495 
00503     protected function _setOrderStatus( $sStatus )
00504     {
00505         $oDb = oxDb::getDb();
00506         $sQ = 'update oxorder set oxtransstatus='.$oDb->quote( $sStatus ).' where oxid="'.$this->getId().'" ';
00507         $oDb->execute( $sQ );
00508 
00509         //updating order object
00510         $this->oxorder__oxtransstatus = new oxField( $sStatus, oxField::T_RAW );
00511     }
00512 
00523     protected function _loadFromBasket( oxBasket $oBasket )
00524     {
00525         $myConfig = $this->getConfig();
00526 
00527         // store IP Adress - default must be FALSE as it is illegal to store
00528         if ( $myConfig->getConfigParam( 'blStoreIPs' ) &&  $this->oxorder__oxip->value === null ) {
00529             $this->oxorder__oxip = new oxField(oxUtilsServer::getInstance()->getRemoteAddress(), oxField::T_RAW);
00530         }
00531 
00532         // copying main price info
00533         $this->oxorder__oxtotalnetsum   = new oxField(oxUtils::getInstance()->fRound($oBasket->getDiscountedNettoPrice()), oxField::T_RAW);
00534         $this->oxorder__oxtotalbrutsum  = new oxField($oBasket->getProductsPrice()->getBruttoSum(), oxField::T_RAW);
00535         $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00536 
00537         // copying discounted VAT info
00538         $iVatIndex = 1;
00539         foreach ( $oBasket->getProductVats(false) as $iVat => $dPrice ) {
00540             $this->{"oxorder__oxartvat$iVatIndex"}      = new oxField($iVat, oxField::T_RAW);
00541             $this->{"oxorder__oxartvatprice$iVatIndex"} = new oxField($dPrice, oxField::T_RAW);
00542             $iVatIndex ++;
00543         }
00544 
00545         // payment costs if available
00546         if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00547             $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00548             $this->oxorder__oxpayvat  = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00549         }
00550 
00551         // delivery info
00552         if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00553             $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00554             //V #M382: Save VAT, not VAT value for delivery costs
00555             $this->oxorder__oxdelvat  = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW); //V #M382
00556             $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00557         }
00558 
00559         // user remark
00560         if ( $this->oxorder__oxremark->value === null ) {
00561             $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00562         }
00563 
00564         // currency
00565         $oCur = $myConfig->getActShopCurrencyObject();
00566         $this->oxorder__oxcurrency = new oxField($oCur->name);
00567         $this->oxorder__oxcurrate  = new oxField($oCur->rate, oxField::T_RAW);
00568 
00569         // store voucherdiscount
00570         if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00571             $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00572         }
00573 
00574         // general discount
00575         if ( $this->_blReloadDiscount ) {
00576             $dDiscount = 0;
00577             $aDiscounts = $oBasket->getDiscounts();
00578             if ( count($aDiscounts) > 0 ) {
00579                 foreach ($aDiscounts as $oDiscount) {
00580                     $dDiscount += $oDiscount->dDiscount;
00581                 }
00582             }
00583             $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00584         }
00585 
00586         //order language
00587         $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00588 
00589 
00590         // initial status - 'ERROR'
00591         $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00592 
00593         // copies basket product info ...
00594         $this->_setOrderArticles( $oBasket->getContents() );
00595 
00596         // copies wrapping info
00597         $this->_setWrapping( $oBasket );
00598 
00599         // copies TS protection info
00600         $this->_setTsProtection( $oBasket );
00601     }
00602 
00609     public function getOrderLanguage()
00610     {
00611         if ( $this->_iOrderLang === null ) {
00612             if ( isset( $this->oxorder__oxlang->value ) ) {
00613                 $this->_iOrderLang = oxLang::getInstance()->validateLanguage( $this->oxorder__oxlang->value );
00614             } else {
00615                 $this->_iOrderLang = oxLang::getInstance()->getBaseLanguage();
00616             }
00617         }
00618         return $this->_iOrderLang;
00619     }
00620 
00628     protected function _setUser( $oUser )
00629     {
00630 
00631         $this->oxorder__oxuserid        = new oxField($oUser->getId());
00632 
00633         // bill address
00634         $this->oxorder__oxbillcompany     = clone $oUser->oxuser__oxcompany;
00635         $this->oxorder__oxbillemail       = clone $oUser->oxuser__oxusername;
00636         $this->oxorder__oxbillfname       = clone $oUser->oxuser__oxfname;
00637         $this->oxorder__oxbilllname       = clone $oUser->oxuser__oxlname;
00638         $this->oxorder__oxbillstreet      = clone $oUser->oxuser__oxstreet;
00639         $this->oxorder__oxbillstreetnr    = clone $oUser->oxuser__oxstreetnr;
00640         $this->oxorder__oxbilladdinfo     = clone $oUser->oxuser__oxaddinfo;
00641         $this->oxorder__oxbillustid       = clone $oUser->oxuser__oxustid;
00642         $this->oxorder__oxbillcity        = clone $oUser->oxuser__oxcity;
00643         $this->oxorder__oxbillcountryid   = clone $oUser->oxuser__oxcountryid;
00644         $this->oxorder__oxbillstateid     = clone $oUser->oxuser__oxstateid;
00645         $this->oxorder__oxbillzip         = clone $oUser->oxuser__oxzip;
00646         $this->oxorder__oxbillfon         = clone $oUser->oxuser__oxfon;
00647         $this->oxorder__oxbillfax         = clone $oUser->oxuser__oxfax;
00648         $this->oxorder__oxbillsal         = clone $oUser->oxuser__oxsal;
00649 
00650 
00651         // delivery address
00652         if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00653             // set delivery address
00654             $this->oxorder__oxdelcompany   = clone $oDelAdress->oxaddress__oxcompany;
00655             $this->oxorder__oxdelfname     = clone $oDelAdress->oxaddress__oxfname;
00656             $this->oxorder__oxdellname     = clone $oDelAdress->oxaddress__oxlname;
00657             $this->oxorder__oxdelstreet    = clone $oDelAdress->oxaddress__oxstreet;
00658             $this->oxorder__oxdelstreetnr  = clone $oDelAdress->oxaddress__oxstreetnr;
00659             $this->oxorder__oxdeladdinfo   = clone $oDelAdress->oxaddress__oxaddinfo;
00660             $this->oxorder__oxdelcity      = clone $oDelAdress->oxaddress__oxcity;
00661             $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00662             $this->oxorder__oxdelstateid   = clone $oDelAdress->oxaddress__oxstateid;
00663             $this->oxorder__oxdelzip       = clone $oDelAdress->oxaddress__oxzip;
00664             $this->oxorder__oxdelfon       = clone $oDelAdress->oxaddress__oxfon;
00665             $this->oxorder__oxdelfax       = clone $oDelAdress->oxaddress__oxfax;
00666             $this->oxorder__oxdelsal       = clone $oDelAdress->oxaddress__oxsal;
00667         }
00668     }
00669 
00677     protected function _setWrapping( oxBasket $oBasket )
00678     {
00679         $myConfig = $this->getConfig();
00680 
00681         // wrapping price
00682         if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00683             $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00684 
00685             // wrapping VAT
00686             if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
00687                 $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00688             }
00689         }
00690 
00691         // greetings card
00692         $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00693 
00694         // card text will be stored in database
00695         $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00696     }
00697 
00706     protected function _setOrderArticles( $aArticleList )
00707     {
00708         // reset articles list
00709         $this->_oArticles = oxNew( 'oxlist' );
00710         $iCurrLang = $this->getOrderLanguage();
00711 
00712         // add all the products we have on basket to the order
00713         foreach ( $aArticleList as $oContent ) {
00714 
00715             //$oContent->oProduct = $oContent->getArticle();
00716             // #M773 Do not use article lazy loading on order save
00717             $oProduct = $oContent->getArticle( true, null, true);
00718 
00719             // copy only if object is oxarticle type
00720             if ( $oProduct->isOrderArticle() ) {
00721                 $oOrderArticle = $oProduct;
00722             } else {
00723 
00724                 // if order language doe not match product language - article must be reloaded in order language
00725                 if ( $iCurrLang != $oProduct->getLanguage() ) {
00726                     $oProduct->loadInLang( $iCurrLang, $oProduct->getProductId() );
00727                 }
00728 
00729                 // set chosen selectlist
00730                 $sSelList = '';
00731                 if ( count( $aChosenSelList = $oContent->getChosenSelList() ) ) {
00732                     foreach ( $aChosenSelList as $oItem ) {
00733                         if ( $sSelList ) {
00734                             $sSelList .= ", ";
00735                         }
00736                         $sSelList .= "{$oItem->name} : {$oItem->value}";
00737                     }
00738                 }
00739 
00740                 $oOrderArticle = oxNew( 'oxorderarticle' );
00741                 $oOrderArticle->setIsNewOrderItem( true );
00742                 $oOrderArticle->copyThis( $oProduct );
00743                 $oOrderArticle->setId();
00744 
00745                 $oOrderArticle->oxorderarticles__oxartnum     = clone $oProduct->oxarticles__oxartnum;
00746                 $oOrderArticle->oxorderarticles__oxselvariant = new oxField( trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->getRawValue() ), oxField::T_RAW );
00747                 $oOrderArticle->oxorderarticles__oxshortdesc  = new oxField( $oProduct->oxarticles__oxshortdesc->getRawValue(), oxField::T_RAW );
00748                 // #M974: duplicated entries for the name of variants in orders
00749                 $oOrderArticle->oxorderarticles__oxtitle      = new oxField( trim( $oProduct->oxarticles__oxtitle->getRawValue() ), oxField::T_RAW );
00750 
00751                 // copying persistent parameters ...
00752                 if ( !is_array( $aPersParams = $oProduct->getPersParams() ) ) {
00753                     $aPersParams = $oContent->getPersParams();
00754                 }
00755                 if ( is_array( $aPersParams ) && count( $aPersParams )) {
00756                     $oOrderArticle->oxorderarticles__oxpersparam = new oxField( serialize( $aPersParams ), oxField::T_RAW );
00757                 }
00758             }
00759 
00760             // ids, titles, numbers ...
00761             $oOrderArticle->oxorderarticles__oxorderid = new oxField( $this->getId() );
00762             $oOrderArticle->oxorderarticles__oxartid   = new oxField( $oContent->getProductId() );
00763             $oOrderArticle->oxorderarticles__oxamount  = new oxField( $oContent->getAmount() );
00764 
00765             // prices
00766             $oPrice = $oContent->getPrice();
00767             $oOrderArticle->oxorderarticles__oxnetprice  = new oxField( $oPrice->getNettoPrice(), oxField::T_RAW );
00768             $oOrderArticle->oxorderarticles__oxvatprice  = new oxField( $oPrice->getVatValue(), oxField::T_RAW );
00769             $oOrderArticle->oxorderarticles__oxbrutprice = new oxField( $oPrice->getBruttoPrice(), oxField::T_RAW );
00770             $oOrderArticle->oxorderarticles__oxvat       = new oxField( $oPrice->getVat(), oxField::T_RAW );
00771 
00772             $oUnitPtice = $oContent->getUnitPrice();
00773             $oOrderArticle->oxorderarticles__oxnprice = new oxField( $oUnitPtice->getNettoPrice(), oxField::T_RAW );
00774             $oOrderArticle->oxorderarticles__oxbprice = new oxField( $oUnitPtice->getBruttoPrice(), oxField::T_RAW );
00775 
00776             // wrap id
00777             $oOrderArticle->oxorderarticles__oxwrapid = new oxField( $oContent->getWrappingId(), oxField::T_RAW );
00778 
00779             // items shop id
00780             $oOrderArticle->oxorderarticles__oxordershopid = new oxField( $oContent->getShopId(), oxField::T_RAW );
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 
00997     protected function _markVouchers( $oBasket, $oUser )
00998     {
00999         $this->_aVoucherList = $oBasket->getVouchers();
01000 
01001         if ( is_array( $this->_aVoucherList ) ) {
01002             foreach ( $this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
01003                 $oVoucher = oxNew( 'oxvoucher' );
01004                 $oVoucher->load( $sVoucherId );
01005                 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->dVoucherdiscount );
01006 
01007                 $this->_aVoucherList[$sVoucherId] = $oVoucher;
01008             }
01009         }
01010     }
01011 
01017     public function save()
01018     {
01019         if ( ( $blSave = parent::save() ) ) {
01020 
01021             // saving order articles
01022             $oOrderArticles = $this->getOrderArticles();
01023             if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
01024                 foreach ( $oOrderArticles as $oOrderArticle ) {
01025                     $oOrderArticle->save();
01026                 }
01027             }
01028         }
01029 
01030         return $blSave;
01031     }
01032 
01039     public function getDelAddressInfo()
01040     {
01041         $oDelAdress = null;
01042         if ( ( $soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
01043             $oDelAdress = oxNew( 'oxaddress' );
01044             $oDelAdress->load( $soxAddressId );
01045 
01046             //get delivery country name from delivery country id
01047             if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
01048                  $oCountry = oxNew( 'oxcountry' );
01049                  $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
01050                  $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
01051             }
01052         }
01053         return $oDelAdress;
01054     }
01055 
01066     public function validateStock( $oBasket )
01067     {
01068         foreach ( $oBasket->getContents() as $key => $oContent ) {
01069             try {
01070                 $oProd = $oContent->getArticle();
01071             } catch ( oxNoArticleException $oEx ) {
01072                 $oBasket->removeItem( $key );
01073                 throw $oEx;
01074             } catch ( oxArticleInputException $oEx ) {
01075                 $oBasket->removeItem( $key );
01076                 throw $oEx;
01077             }
01078 
01079             // check if its still available
01080             $dArtStockAmount = $oBasket->getArtStockInBasket( $oProd->getId(), $key );
01081             $iOnStock = $oProd->checkForStock( $oContent->getAmount(), $dArtStockAmount );
01082             if ( $iOnStock !== true ) {
01083                 $oEx = oxNew( 'oxOutOfStockException' );
01084                 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
01085                 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
01086                 $oEx->setProductId( $oProd->getId() );
01087                 $oEx->setRemainingAmount( $oProd->oxarticles__oxstock->value );
01088                 throw $oEx;
01089             }
01090         }
01091     }
01092 
01098     protected function _insert()
01099     {
01100         $myConfig = $this->getConfig();
01101         $oUtilsDate = oxUtilsDate::getInstance();
01102 
01103         //V #M525 orderdate must be the same as it was
01104         if ( !$this->oxorder__oxorderdate->value ) {
01105             $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
01106         } else {
01107             $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
01108         }
01109         $this->oxorder__oxshopid    = new oxField($myConfig->getShopId(), oxField::T_RAW);
01110 
01111         $this->oxorder__oxsenddate  = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01112 
01113         if ( ( $blInsert = parent::_insert() ) ) {
01114             // setting order number
01115             if ( !$this->oxorder__oxordernr->value ) {
01116                 $aWhere = '';
01117                 // separate order numbers for shops ...
01118                 if ( $this->_blSeparateNumbering ) {
01119                     $aWhere = array( 'oxshopid = "'.$myConfig->getShopId().'"' );
01120                 }
01121                 $this->_setRecordNumber( 'oxordernr', $aWhere );
01122             }
01123         }
01124         return $blInsert;
01125     }
01126 
01132     protected function _update()
01133     {
01134         $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01135         return parent::_update();
01136     }
01137 
01146     public function delete( $sOxId = null )
01147     {
01148         if ( $sOxId ) {
01149             if ( !$this->load( $sOxId ) ) {
01150                 // such order does not exist
01151                 return false;
01152             }
01153         } elseif ( !$sOxId ) {
01154             $sOxId = $this->getId();
01155         }
01156 
01157         // no order id is passed
01158         if ( !$sOxId ) {
01159             return false;
01160         }
01161 
01162 
01163         // delete order articles
01164         $oOrderArticles = $this->getOrderArticles( false );
01165         foreach ( $oOrderArticles as $oOrderArticle ) {
01166             $oOrderArticle->delete();
01167         }
01168 
01169         // #440 - deleting user payment info
01170         if ( $oPaymentType = $this->getPaymentType() ) {
01171             $oPaymentType->delete();
01172         }
01173 
01174         return parent::delete( $sOxId );
01175     }
01176 
01186     public function recalculateOrder( $aNewArticles = array() )
01187     {
01188         oxDb::startTransaction();
01189 
01190         try {
01191             $oBasket = $this->_getOrderBasket();
01192 
01193             // add this order articles to virtual basket and recalculates basket
01194             $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles( true ) );
01195 
01196             // adding new articles to existing order
01197             $this->_addArticlesToBasket( $oBasket, $aNewArticles );
01198 
01199             // recalculating basket
01200             $oBasket->calculateBasket( true );
01201 
01202             //finalizing order (skipping payment execution, vouchers marking and mail sending)
01203             $iRet = $this->finalizeOrder( $oBasket, $this->getOrderUser(), true );
01204 
01205             //if finalizing order failed, rollback transaction
01206             if ( $iRet !== 1 ) {
01207                 oxDb::rollbackTransaction();
01208             } else {
01209                 oxDb::commitTransaction();
01210             }
01211 
01212         } catch( Exception $oE ) {
01213             // if exception, rollBack everything
01214             oxDb::rollbackTransaction();
01215 
01216             if ( defined( 'OXID_PHP_UNIT' ) ) {
01217                 throw $oE;
01218             }
01219         }
01220     }
01221 
01222     protected $_oOrderBasket = null;
01230     protected function _getOrderBasket( $blStockCheck = true )
01231     {
01232         $this->_oOrderBasket = oxNew( "oxbasket" );
01233 
01234         // setting stock check mode
01235         $this->_oOrderBasket->setStockCheckMode( $blStockCheck );
01236 
01237         // setting virtual basket user
01238         $this->_oOrderBasket->setBasketUser( $this->getOrderUser() );
01239 
01240         // transferring order id
01241         $this->_oOrderBasket->setOrderId( $this->getId() );
01242 
01243         // setting basket currency order uses
01244         $aCurrencies = $this->getConfig()->getCurrencyArray();
01245         foreach ( $aCurrencies as $oCur ) {
01246             if ($oCur->name == $this->oxorder__oxcurrency->value) {
01247                 $oBasketCur = $oCur;
01248                 break;
01249             }
01250         }
01251 
01252         // setting currency
01253         $this->_oOrderBasket->setBasketCurrency( $oBasketCur );
01254 
01255         // set basket card id and message
01256         $this->_oOrderBasket->setCardId( $this->oxorder__oxcardid->value );
01257         $this->_oOrderBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01258 
01259         if ( $this->_blReloadDiscount ) {
01260             // disabling availability check
01261             $this->_oOrderBasket->setSkipVouchersChecking( true );
01262 
01263             // add previously used vouchers
01264             $sQ = 'select oxid from oxvouchers where oxorderid = "'.$this->getId().'"';
01265             $aVouchers = oxDb::getDb( true )->getAll( $sQ );
01266             foreach ( $aVouchers as $aVoucher ) {
01267                 $this->_oOrderBasket->addVoucher( $aVoucher['oxid'] );
01268             }
01269         } else {
01270             $this->_oOrderBasket->setDiscountCalcMode( false );
01271             $this->_oOrderBasket->setVoucherDiscount( $this->oxorder__oxvoucherdiscount->value );
01272             $this->_oOrderBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
01273         }
01274 
01275         // must be kept old delivery?
01276         if ( !$this->_blReloadDelivery ) {
01277             $this->_oOrderBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01278         } else {
01279             //  set shipping
01280             $this->_oOrderBasket->setShipping( $this->oxorder__oxdeltype->value );
01281             $this->_oOrderBasket->setDeliveryPrice( null );
01282         }
01283 
01284         //set basket payment
01285         $this->_oOrderBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01286 
01287         return $this->_oOrderBasket;
01288     }
01289 
01298     public function setDelivery( $sDeliveryId )
01299     {
01300         $this->reloadDelivery( true );
01301         $this->oxorder__oxdeltype = new oxField( $sDeliveryId );
01302     }
01303 
01309     public function getOrderUser()
01310     {
01311         if ($this->_oUser === null ) {
01312             $this->_oUser = oxNew( "oxuser" );
01313             $this->_oUser->load( $this->oxorder__oxuserid->value );
01314 
01315             // if object is loaded then reusing its order info
01316             if ( $this->_isLoaded ) {
01317                 // bill address
01318                 $this->_oUser->oxuser__oxcompany  = clone $this->oxorder__oxbillcompany;
01319                 $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
01320                 $this->_oUser->oxuser__oxfname    = clone $this->oxorder__oxbillfname;
01321                 $this->_oUser->oxuser__oxlname    = clone $this->oxorder__oxbilllname;
01322                 $this->_oUser->oxuser__oxstreet   = clone $this->oxorder__oxbillstreet;
01323                 $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
01324                 $this->_oUser->oxuser__oxaddinfo  = clone $this->oxorder__oxbilladdinfo;
01325                 $this->_oUser->oxuser__oxustid    = clone $this->oxorder__oxbillustid;
01326 
01327 
01328                 $this->_oUser->oxuser__oxcity      = clone $this->oxorder__oxbillcity;
01329                 $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
01330                 $this->_oUser->oxuser__oxstateid   = clone $this->oxorder__oxbillstateid;
01331                 $this->_oUser->oxuser__oxzip       = clone $this->oxorder__oxbillzip;
01332                 $this->_oUser->oxuser__oxfon       = clone $this->oxorder__oxbillfon;
01333                 $this->_oUser->oxuser__oxfax       = clone $this->oxorder__oxbillfax;
01334                 $this->_oUser->oxuser__oxsal       = clone $this->oxorder__oxbillsal;
01335             }
01336         }
01337 
01338         return $this->_oUser;
01339     }
01340 
01348     public function pdfFooter( $oPdf )
01349     {
01350     }
01351 
01359     public function pdfHeaderplus( $oPdf )
01360     {
01361     }
01362 
01370     public function pdfHeader( $oPdf )
01371     {
01372     }
01373 
01382     public function genPdf( $sFilename, $iSelLang = 0 )
01383     {
01384     }
01385 
01391     public function getInvoiceNum()
01392     {
01393         $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01394         return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01395     }
01396 
01402     public function getNextBillNum()
01403     {
01404         $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01405         return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01406     }
01407 
01413     public function getShippingSetList()
01414     {
01415         // in which country we deliver
01416         if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
01417             $sShipId = $this->oxorder__oxbillcountryid->value;
01418         }
01419 
01420         $oBasket = $this->_getOrderBasket( false );
01421 
01422         // add this order articles to basket and recalculate basket
01423         $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles() );
01424 
01425         // recalculating basket
01426         $oBasket->calculateBasket( true );
01427 
01428         // load fitting deliveries list
01429         $oDeliveryList = oxNew( "oxDeliveryList", "core" );
01430         $oDeliveryList->setCollectFittingDeliveriesSets( true );
01431 
01432         return $oDeliveryList->getDeliveryList( $oBasket, $this->getOrderUser(), $sShipId );
01433     }
01434 
01440     public function getVoucherNrList()
01441     {
01442         $oDB = oxDb::getDb( true );
01443         $aVouchers = array();
01444         $sSelect = "select oxvouchernr from oxvouchers where oxorderid = ".$oDB->quote( $this->oxorder__oxid->value );
01445         $rs = $oDB->execute( $sSelect);
01446         if ($rs != false && $rs->recordCount() > 0) {
01447             while (!$rs->EOF) {
01448                 $aVouchers[] = $rs->fields['oxvouchernr'];
01449                 $rs->moveNext();
01450             }
01451         }
01452         return $aVouchers;
01453     }
01454 
01462     public function getOrderSum( $blToday = false )
01463     {
01464         $sSelect  = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01465         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01466 
01467         if ( $blToday ) {
01468             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01469         }
01470 
01471         return ( double ) oxDb::getDb()->getOne( $sSelect );
01472     }
01473 
01481     public function getOrderCnt( $blToday = false )
01482     {
01483         $sSelect  = 'select count(*) from oxorder where ';
01484         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'"  and oxorder.oxstorno != "1" ';
01485 
01486         if ( $blToday ) {
01487             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01488         }
01489 
01490         return ( int ) oxDb::getDb()->getOne( $sSelect );
01491     }
01492 
01493 
01501     protected function _checkOrderExist( $sOxId = null )
01502     {
01503         if ( !$sOxId) {
01504             return false;
01505         }
01506 
01507         $oDb = oxDb::getDb();
01508         if ( $oDb->getOne( 'select oxid from oxorder where oxid = '.$oDb->quote( $sOxId ) ) ) {
01509             return true;
01510         }
01511 
01512         return false;
01513     }
01514 
01524     protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01525     {
01526         $iRet = self::ORDER_STATE_MAILINGERROR;
01527 
01528         // add user, basket and payment to order
01529         $this->_oUser    = $oUser;
01530         $this->_oBasket  = $oBasket;
01531         $this->_oPayment = $oPayment;
01532 
01533         $oxEmail = oxNew( 'oxemail' );
01534 
01535         // send order email to user
01536         if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01537             // mail to user was successfully sent
01538             $iRet = self::ORDER_STATE_OK;
01539         }
01540 
01541         // send order email to shop owner
01542         $oxEmail->sendOrderEMailToOwner( $this );
01543 
01544         return $iRet;
01545     }
01546 
01552     public function getBasket()
01553     {
01554         return $this->_oBasket;
01555     }
01556 
01562     public function getPayment()
01563     {
01564         return $this->_oPayment;
01565     }
01566 
01572     public function getVoucherList()
01573     {
01574         return $this->_aVoucherList;
01575     }
01576 
01582     public function getDelSet()
01583     {
01584         if ( $this->_oDelSet == null ) {
01585             // load deliveryset info
01586             $this->_oDelSet = oxNew( 'oxdeliveryset' );
01587             $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01588         }
01589 
01590         return $this->_oDelSet;
01591     }
01592 
01598     public function getPaymentType()
01599     {
01600         if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType === null ) {
01601             $this->_oPaymentType = false;
01602             $oPaymentType = oxNew( 'oxuserpayment' );
01603             if ( $oPaymentType->load( $this->oxorder__oxpaymentid->value ) ) {
01604                 $this->_oPaymentType = $oPaymentType;
01605             }
01606         }
01607 
01608         return $this->_oPaymentType;
01609     }
01610 
01616     public function getGiftCard()
01617     {
01618         if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01619             $this->_oGiftCard = oxNew( 'oxwrapping' );
01620             $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01621         }
01622 
01623         return $this->_oGiftCard;
01624     }
01625 
01633     public function setSeparateNumbering( $blSeparateNumbering = null )
01634     {
01635         $this->_blSeparateNumbering = $blSeparateNumbering;
01636     }
01637 
01645     public function getLastUserPaymentType( $sUserId)
01646     {
01647         $oDb = oxDb::getDb();
01648         $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid='.$oDb->quote( $sUserId ).' order by oxorder.oxorderdate desc ';
01649         $sLastPaymentId = $oDb->getOne( $sQ );
01650         return $sLastPaymentId;
01651     }
01652 
01665     protected function _makeSelListArray( $sArtId = null, $sOrderArtSelList = null )
01666     {
01667         $oOrder = oxNew( 'oxorderArticle' );
01668         return $oOrder->getOrderArticleSelectList( $sArtId, $sOrderArtSelList );
01669     }
01670 
01679     protected function _addOrderArticlesToBasket( $oBasket, $aOrderArticles )
01680     {
01681         // if no order articles, return empty basket
01682         if ( count( $aOrderArticles ) > 0 ) {
01683 
01684             //adding order articles to basket
01685             foreach ( $aOrderArticles as $oOrderArticle ) {
01686                 $oBasket->addOrderArticleToBasket( $oOrderArticle );
01687             }
01688         }
01689     }
01690 
01699     protected function _addArticlesToBasket( $oBasket, $aArticles )
01700     {
01701         // if no order articles
01702         if ( count($aArticles ) > 0 ) {
01703 
01704             //adding order articles to basket
01705             foreach ( $aArticles as $oArticle ) {
01706                 $aSel = isset( $oArticle->oxorderarticles__oxselvariant ) ? $oArticle->oxorderarticles__oxselvariant->value : null;
01707                 $aPersParam = isset( $oArticle->oxorderarticles__oxpersparam ) ? $oArticle->getPersParams() : null;
01708                 $oBasket->addToBasket( $oArticle->oxorderarticles__oxartid->value,
01709                                        $oArticle->oxorderarticles__oxamount->value,
01710                                        $aSel, $aPersParam );
01711             }
01712         }
01713     }
01714 
01722     protected function _setDeprecatedValues()
01723     {
01724         if ( $this->oxorder__oxstorno->value != 1 ) {
01725             $oCur = $this->getConfig()->getActShopCurrencyObject();
01726             $oLang = oxLang::getInstance();
01727 
01728             $this->totalnetsum   = $this->oxorder__oxtotalnetsum->value;
01729             $this->totalbrutsum  = $this->oxorder__oxtotalbrutsum->value;
01730             $this->totalorder    = $this->oxorder__oxtotalordersum->value;
01731             $this->ftotalnetsum  = $oLang->formatCurrency( $this->oxorder__oxtotalnetsum->value, $oCur );
01732             $this->ftotalbrutsum = $oLang->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $oCur );
01733             $this->fdelcost      = $oLang->formatCurrency( $this->oxorder__oxdelcost->value, $oCur );
01734             $this->fpaycost      = $oLang->formatCurrency( $this->oxorder__oxpaycost->value, $oCur );
01735             $this->fwrapcost     = $oLang->formatCurrency( $this->oxorder__oxwrapcost->value, $oCur );
01736             $this->ftotalorder   = $this->getTotalOrderSum();
01737             $this->totalvouchers = 0;
01738 
01739             if ( $this->oxorder__oxvoucherdiscount->value ) {
01740                 $this->totalvouchers  = $oLang->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $oCur );
01741             }
01742 
01743             if ( $this->oxorder__oxdiscount->value ) {
01744                 $this->discount  = $this->oxorder__oxdiscount->value;
01745                 $this->fdiscount = $oLang->formatCurrency( $this->oxorder__oxdiscount->value, $oCur );
01746             }
01747         }
01748     }
01749 
01755     public function getTotalOrderSum()
01756     {
01757         $oCur = $this->getConfig()->getActShopCurrencyObject();
01758         return number_format( (double)$this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01759     }
01760 
01768     public function getProductVats( $blFormatCurrency = true )
01769     {
01770         $aVats = array();
01771         if ($this->oxorder__oxartvat1->value) {
01772             $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
01773         }
01774         if ($this->oxorder__oxartvat2->value) {
01775             $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
01776         }
01777 
01778         if ( $blFormatCurrency ) {
01779             $oLang = oxLang::getInstance();
01780             $oCur = $this->getConfig()->getActShopCurrencyObject();
01781             foreach ( $aVats as $sKey => $dVat ) {
01782                 $aVats[$sKey] = $oLang->formatCurrency( $dVat, $oCur );
01783             }
01784         }
01785         return $aVats;
01786     }
01787 
01793     public function getBillCountry()
01794     {
01795         if ( !$this->oxorder__oxbillcountry->value ) {
01796             $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
01797         }
01798         return $this->oxorder__oxbillcountry;
01799     }
01800 
01806     public function getDelCountry()
01807     {
01808         if ( !$this->oxorder__oxdelcountry->value ) {
01809             $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
01810         }
01811         return $this->oxorder__oxdelcountry;
01812     }
01820     public function reloadDelivery( $blReload )
01821     {
01822         $this->_blReloadDelivery = $blReload;
01823     }
01824 
01832     public function reloadDiscount( $blReload )
01833     {
01834         $this->_blReloadDiscount = $blReload;
01835     }
01836 
01842     public function cancelOrder()
01843     {
01844         $this->oxorder__oxstorno = new oxField( 1 );
01845         if ( $this->save() ) {
01846             // canceling ordered products
01847             foreach ( $this->getOrderArticles() as $oOrderArticle ) {
01848                 $oOrderArticle->cancelOrderArticle();
01849             }
01850         }
01851     }
01852 
01859     public function getOrderCurrency()
01860     {
01861         if ( $this->_oOrderCurrency === null ) {
01862 
01863             // setting default in case unrecognized currency was set during order
01864             $aCurrencies = $this->getConfig()->getCurrencyArray();
01865             $this->_oOrderCurrency = current( $aCurrencies );
01866 
01867             foreach ( $aCurrencies as $oCurr ) {
01868                 if ( $oCurr->name == $this->oxorder__oxcurrency->value ) {
01869                     $this->_oOrderCurrency = $oCurr;
01870                     break;
01871                 }
01872             }
01873         }
01874         return $this->_oOrderCurrency;
01875     }
01876 
01886     public function validateOrder( $oBasket, $oUser )
01887     {
01888         // validating stock
01889         $iValidState = $this->validateStock( $oBasket );
01890 
01891         if ( !$iValidState ) {
01892             // validating delivery
01893             $iValidState = $this->validateDelivery( $oBasket );
01894         }
01895 
01896         if ( !$iValidState ) {
01897             // validating payment
01898             $iValidState = $this->validatePayment( $oBasket );
01899         }
01900 
01901         return $iValidState;
01902     }
01903 
01912     public function validateDelivery( $oBasket )
01913     {
01914         // proceed with no delivery
01915         // used for other countries
01916         if ( $oBasket->getPaymentId() == 'oxempty') {
01917             return;
01918         }
01919         $oDb = oxDb::getDb();
01920 
01921         $oDelSet = oxNew( "oxdeliveryset" );
01922         $sTable = $oDelSet->getViewName();
01923 
01924         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
01925               $oDb->quote( $oBasket->getShippingId() )." and ".$oDelSet->getSqlActiveSnippet();
01926 
01927         if ( !$oDb->getOne( $sQ ) ) {
01928             // throwing exception
01929             return self::ORDER_STATE_INVALIDDELIVERY;
01930         }
01931     }
01932 
01941     public function validatePayment( $oBasket )
01942     {
01943         $oDb = oxDb::getDb();
01944 
01945         $oPayment = oxNew( "oxpayment" );
01946         $sTable = $oPayment->getViewName();
01947 
01948         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
01949               $oDb->quote( $oBasket->getPaymentId() )." and ".$oPayment->getSqlActiveSnippet();
01950 
01951         if ( !$oDb->getOne( $sQ ) ) {
01952             return self::ORDER_STATE_INVALIDPAYMENT;
01953         }
01954     }
01955 
01963     protected function _setTsProtection( oxBasket $oBasket )
01964     {
01965         // protection price
01966         if ( ( $oTsProtectionCost = $oBasket->getCosts( 'oxtsprotection' ) ) ) {
01967             $this->oxorder__oxtsprotectcosts = new oxField($oTsProtectionCost->getBruttoPrice(), oxField::T_RAW);
01968         }
01969 
01970         // protection protduct id
01971         $this->oxorder__oxtsprotectid = new oxField($oBasket->getTsProductId(), oxField::T_RAW);
01972     }
01973 
01982     protected function _executeTsProtection( oxBasket $oBasket )
01983     {
01984         $aValues['tsProductId'] = $this->oxorder__oxtsprotectid->value;
01985         $aValues['amount'] = $oBasket->getPrice()->getBruttoPrice();
01986         $oCur = $this->getConfig()->getActShopCurrencyObject();
01987         $aValues['currency'] = $oCur->name;
01988         $aValues['buyerEmail'] = $this->oxorder__oxbillemail->value;
01989         $aValues['shopCustomerID'] = $this->oxorder__oxuserid->value;
01990         $aValues['shopOrderID'] = $this->oxorder__oxordernr->value;
01991         $aValues['orderDate'] = $this->oxorder__oxorderdate->value;
01992         $sPaymentId = $oBasket->getPaymentId();
01993         $oTsProtection = oxNew('oxtsprotection');
01994         $blRes = $oTsProtection->requestForTsProtection( $aValues, $sPaymentId );
01995         if ( !$blRes ) {
01996             $this->delete();
01997             return self::ORDER_STATE_INVALIDTSPROTECTION;
01998         }
01999         return true; // everything fine
02000     }
02001 
02002 }