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 
00051     protected $_aSkipSaveFields = array( 'oxorderdate' );
00052 
00058     protected $_oArticles = null;
00059 
00065     protected $_oDelSet   = null;
00066 
00072     protected $_oGiftCard = null;
00073 
00079     protected $_oPaymentType = null;
00080 
00086     protected $_oPayment = null;
00087 
00093     protected $_aVoucherList = null;
00094 
00100     protected $_oDelPrice = null;
00101 
00107     protected $_oUser = null;
00108 
00114     protected $_oBasket = null;
00115 
00121     protected $_oWrappingPrice = null;
00122 
00128     protected $_oPaymentPrice = null;
00129 
00135     protected $_sClassName = 'oxorder';
00136 
00142     protected $_blSeparateNumbering = null;
00143 
00149     protected $_iOrderLang = null;
00150 
00156     protected $_blReloadDelivery = true;
00157 
00163     protected $_blReloadDiscount = true;
00164 
00170     protected $_oOrderCurrency = null;
00171 
00175     public function __construct()
00176     {
00177         parent::__construct();
00178         $this->init( 'oxorder' );
00179 
00180         // set usage of seperate orders numbering for different shops
00181         $this->setSeparateNumbering( $this->getConfig()->getConfigParam( 'blSeparateNumbering') );
00182 
00183     }
00184 
00192     public function __get( $sName )
00193     {
00194         if ( $sName == 'oDelSet' ) {
00195             return $this->getDelSet();
00196         }
00197 
00198         if ( $sName == 'oxorder__oxbillcountry' ) {
00199             return $this->getBillCountry();
00200         }
00201 
00202         if ( $sName == 'oxorder__oxdelcountry' ) {
00203             return $this->getDelCountry();
00204         }
00205     }
00206 
00214     public function assign( $dbRecord )
00215     {
00216 
00217         parent::assign( $dbRecord );
00218 
00219         $oUtilsDate = oxUtilsDate::getInstance();
00220 
00221         // convert date's to international format
00222         $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value));
00223         $this->oxorder__oxsenddate  = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value));
00224 
00225         //settting deprecated template variables
00226         $this->_setDeprecatedValues();
00227     }
00228 
00236     protected function _getCountryTitle( $sCountryId )
00237     {
00238         $sTitle = null;
00239         if ( $sCountryId && $sCountryId != '-1' ) {
00240             $oCountry = oxNew( 'oxcountry' );
00241             $oCountry->load( $sCountryId );
00242             $sTitle = $oCountry->oxcountry__oxtitle->value;
00243         }
00244 
00245         return $sTitle;
00246     }
00247 
00255     public function getOrderArticles( $blExcludeCanceled = false )
00256     {
00257         // checking set value
00258         if ( $this->_oArticles === null  ) {
00259             $sTable = getViewName( "oxorderarticles" );
00260             $sSelect = "select {$sTable}.* from {$sTable}
00261                         where {$sTable}.oxorderid = '".$this->getId() . "'" .
00262                         ( $blExcludeCanceled ? " and {$sTable}.oxstorno != 1 ": " " ) ."
00263                         order by {$sTable}.oxartid";
00264 
00265             // order articles
00266             $oArticles = oxNew( 'oxlist' );
00267             $oArticles->init( 'oxorderarticle' );
00268             $oArticles->selectString( $sSelect );
00269 
00270             // is value was not set, just returning it
00271             return $oArticles;
00272         }
00273         return $this->_oArticles;
00274     }
00275 
00283     public function setOrderArticleList( $aOrderArticleList )
00284     {
00285         $this->_oArticles = $aOrderArticleList;
00286     }
00287 
00293     public function getOrderDeliveryPrice()
00294     {
00295         if ( $this->_oDelPrice != null ) {
00296             return $this->_oDelPrice;
00297         }
00298 
00299         $this->_oDelPrice = oxNew( 'oxprice' );
00300         $this->_oDelPrice->setBruttoPriceMode();
00301         $this->_oDelPrice->setPrice( $this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value );
00302         return $this->_oDelPrice;
00303     }
00304 
00310     public function getOrderWrappingPrice()
00311     {
00312         if ( $this->_oWrappingPrice != null ) {
00313             return $this->_oWrappingPrice;
00314         }
00315 
00316         $this->_oWrappingPrice = oxNew( 'oxprice' );
00317         $this->_oWrappingPrice->setBruttoPriceMode();
00318         $this->_oWrappingPrice->setPrice( $this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value );
00319         return $this->_oWrappingPrice;
00320     }
00321 
00327     public function getOrderPaymentPrice()
00328     {
00329         if ( $this->_oPaymentPrice != null ) {
00330             return $this->_oPaymentPrice;
00331         }
00332 
00333         $this->_oPaymentPrice = oxNew( 'oxprice' );
00334         $this->_oPaymentPrice->setBruttoPriceMode();
00335         $this->_oPaymentPrice->setPrice( $this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value );
00336         return $this->_oPaymentPrice;
00337     }
00338 
00345     public function getOrderNetSum()
00346     {
00347         $dTotalNetSum = 0;
00348 
00349         $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
00350         $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
00351         $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
00352         $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
00353 
00354         return $dTotalNetSum;
00355     }
00356 
00377     public function finalizeOrder( oxBasket $oBasket, $oUser, $blRecalculatingOrder = false )
00378     {
00379         // check if this order is already stored
00380         $sGetChallenge = oxSession::getVar( 'sess_challenge' );
00381         if ( $this->_checkOrderExist( $sGetChallenge ) ) {
00382             oxUtils::getInstance()->logger( 'BLOCKER' );
00383             // we might use this later, this means that somebody klicked like mad on order button
00384             return self::ORDER_STATE_ORDEREXISTS;
00385         }
00386 
00387         // if not recalculating order, use sess_challenge id, else leave old order id
00388         if ( !$blRecalculatingOrder ) {
00389             // use this ID
00390             $this->setId( $sGetChallenge );
00391 
00392             // validating various order/basket parameters before finalizing
00393             if ( $iOrderState = $this->validateOrder( $oBasket, $oUser ) ) {
00394                 return $iOrderState;
00395             }
00396         }
00397 
00398         // copies user info
00399         $this->_setUser( $oUser );
00400 
00401         // copies basket info
00402         $this->_loadFromBasket( $oBasket );
00403 
00404         // payment information
00405         $oUserPayment = $this->_setPayment( $oBasket->getPaymentId() );
00406 
00407         // set folder information, if order is new
00408         // #M575 in recalcualting order case folder must be the same as it was
00409         if ( !$blRecalculatingOrder ) {
00410             $this->_setFolder();
00411         }
00412 
00413         //saving all order data to DB
00414         $this->save();
00415 
00416         // executing payment (on failure deletes order and returns error code)
00417         // in case when recalcualting order, payment execution is skipped
00418         if ( !$blRecalculatingOrder ) {
00419             $blRet = $this->_executePayment( $oBasket, $oUserPayment );
00420             if ( $blRet !== true ) {
00421                 return $blRet;
00422             }
00423         }
00424 
00425         // deleting remark info only when order is finished
00426         oxSession::deleteVar( 'ordrem' );
00427 
00428         // updating order trans status (success status)
00429         $this->_setOrderStatus( 'OK' );
00430 
00431         // store orderid
00432         $oBasket->setOrderId( $this->getId() );
00433 
00434         // updating wish lists
00435         $this->_updateWishlist( $oBasket->getContents(), $oUser );
00436 
00437         // updating users notice list
00438         $this->_updateNoticeList( $oBasket->getContents(), $oUser );
00439 
00440         // marking vouchers as used and sets them to $this->_aVoucherList (will be used in order email)
00441         // skipping this action in case of order recalculation
00442         if ( !$blRecalculatingOrder ) {
00443             $this->_markVouchers( $oBasket, $oUser );
00444         }
00445 
00446         // send order by email to shop owner and current user
00447         // skipping this action in case of order recalculation
00448         if ( !$blRecalculatingOrder ) {
00449             $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
00450         } else {
00451             $iRet = self::ORDER_STATE_OK;
00452         }
00453 
00454         return $iRet;
00455     }
00456 
00464     protected function _setOrderStatus( $sStatus )
00465     {
00466         $oDb = oxDb::getDb();
00467         $sQ = 'update oxorder set oxtransstatus='.$oDb->quote( $sStatus ).' where oxid="'.$this->getId().'" ';
00468         $oDb->execute( $sQ );
00469 
00470         //updating order object
00471         $this->oxorder__oxtransstatus = new oxField( $sStatus, oxField::T_RAW );
00472     }
00473 
00484     protected function _loadFromBasket( oxBasket $oBasket )
00485     {
00486         $myConfig = $this->getConfig();
00487 
00488         // store IP Adress - default must be FALSE as it is illegal to store
00489         if ( $myConfig->getConfigParam( 'blStoreIPs' ) &&  $this->oxorder__oxip->value === null ) {
00490             $this->oxorder__oxip = new oxField(oxUtilsServer::getInstance()->getRemoteAddress(), oxField::T_RAW);
00491         }
00492 
00493         // copying main price info
00494         $this->oxorder__oxtotalnetsum   = new oxField(oxUtils::getInstance()->fRound($oBasket->getDiscountedNettoPrice()), oxField::T_RAW);
00495         $this->oxorder__oxtotalbrutsum  = new oxField($oBasket->getProductsPrice()->getBruttoSum(), oxField::T_RAW);
00496         $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00497 
00498         // copying discounted VAT info
00499         $iVatIndex = 1;
00500         foreach ( $oBasket->getProductVats(false) as $iVat => $dPrice ) {
00501             $this->{"oxorder__oxartvat$iVatIndex"}      = new oxField($iVat, oxField::T_RAW);
00502             $this->{"oxorder__oxartvatprice$iVatIndex"} = new oxField($dPrice, oxField::T_RAW);
00503             $iVatIndex ++;
00504         }
00505 
00506         // payment costs if available
00507         if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00508             $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00509             $this->oxorder__oxpayvat  = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00510         }
00511 
00512         // delivery info
00513         if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00514             $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00515             //V #M382: Save VAT, not VAT value for delivery costs
00516             $this->oxorder__oxdelvat  = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW); //V #M382
00517             $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00518         }
00519 
00520         // user remark
00521         if ( $this->oxorder__oxremark->value === null ) {
00522             $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00523         }
00524 
00525         // currency
00526         $oCur = $myConfig->getActShopCurrencyObject();
00527         $this->oxorder__oxcurrency = new oxField($oCur->name);
00528         $this->oxorder__oxcurrate  = new oxField($oCur->rate, oxField::T_RAW);
00529 
00530         // store voucherdiscount
00531         if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00532             $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00533         }
00534 
00535         // general discount
00536         if ( $this->_blReloadDiscount ) {
00537             $dDiscount = 0;
00538             $aDiscounts = $oBasket->getDiscounts();
00539             if ( count($aDiscounts) > 0 ) {
00540                 foreach ($aDiscounts as $oDiscount) {
00541                     $dDiscount += $oDiscount->dDiscount;
00542                 }
00543             }
00544             $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00545         }
00546 
00547         //order language
00548         $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00549 
00550 
00551         // initial status - 'ERROR'
00552         $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00553 
00554         // copies basket product info ...
00555         $this->_setOrderArticles( $oBasket->getContents() );
00556 
00557         // copies wrapping info
00558         $this->_setWrapping( $oBasket );
00559     }
00560 
00567     public function getOrderLanguage()
00568     {
00569         if ( $this->_iOrderLang === null ) {
00570             if ( isset( $this->oxorder__oxlang->value ) ) {
00571                 $this->_iOrderLang = oxLang::getInstance()->validateLanguage( $this->oxorder__oxlang->value );
00572             } else {
00573                 $this->_iOrderLang = oxLang::getInstance()->getBaseLanguage();
00574             }
00575         }
00576         return $this->_iOrderLang;
00577     }
00578 
00586     protected function _setUser( $oUser )
00587     {
00588 
00589         $this->oxorder__oxuserid        = new oxField($oUser->getId());
00590 
00591         // bill address
00592         $this->oxorder__oxbillcompany     = clone $oUser->oxuser__oxcompany;
00593         $this->oxorder__oxbillemail       = clone $oUser->oxuser__oxusername;
00594         $this->oxorder__oxbillfname       = clone $oUser->oxuser__oxfname;
00595         $this->oxorder__oxbilllname       = clone $oUser->oxuser__oxlname;
00596         $this->oxorder__oxbillstreet      = clone $oUser->oxuser__oxstreet;
00597         $this->oxorder__oxbillstreetnr    = clone $oUser->oxuser__oxstreetnr;
00598         $this->oxorder__oxbilladdinfo     = clone $oUser->oxuser__oxaddinfo;
00599         $this->oxorder__oxbillustid       = clone $oUser->oxuser__oxustid;
00600         $this->oxorder__oxbillcity        = clone $oUser->oxuser__oxcity;
00601         $this->oxorder__oxbillcountryid   = clone $oUser->oxuser__oxcountryid;
00602         $this->oxorder__oxbillstateid     = clone $oUser->oxuser__oxstateid;
00603         $this->oxorder__oxbillzip         = clone $oUser->oxuser__oxzip;
00604         $this->oxorder__oxbillfon         = clone $oUser->oxuser__oxfon;
00605         $this->oxorder__oxbillfax         = clone $oUser->oxuser__oxfax;
00606         $this->oxorder__oxbillsal         = clone $oUser->oxuser__oxsal;
00607 
00608 
00609         // delivery address
00610         if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00611             // set delivery address
00612             $this->oxorder__oxdelcompany   = clone $oDelAdress->oxaddress__oxcompany;
00613             $this->oxorder__oxdelfname     = clone $oDelAdress->oxaddress__oxfname;
00614             $this->oxorder__oxdellname     = clone $oDelAdress->oxaddress__oxlname;
00615             $this->oxorder__oxdelstreet    = clone $oDelAdress->oxaddress__oxstreet;
00616             $this->oxorder__oxdelstreetnr  = clone $oDelAdress->oxaddress__oxstreetnr;
00617             $this->oxorder__oxdeladdinfo   = clone $oDelAdress->oxaddress__oxaddinfo;
00618             $this->oxorder__oxdelcity      = clone $oDelAdress->oxaddress__oxcity;
00619             $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00620             $this->oxorder__oxdelstateid   = clone $oDelAdress->oxaddress__oxstateid;
00621             $this->oxorder__oxdelzip       = clone $oDelAdress->oxaddress__oxzip;
00622             $this->oxorder__oxdelfon       = clone $oDelAdress->oxaddress__oxfon;
00623             $this->oxorder__oxdelfax       = clone $oDelAdress->oxaddress__oxfax;
00624             $this->oxorder__oxdelsal       = clone $oDelAdress->oxaddress__oxsal;
00625         }
00626     }
00627 
00635     protected function _setWrapping( oxBasket $oBasket )
00636     {
00637         $myConfig = $this->getConfig();
00638 
00639         // wrapping price
00640         if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00641             $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00642 
00643             // wrapping VAT
00644             if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
00645                 $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00646             }
00647         }
00648 
00649         // greetings card
00650         $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00651 
00652         // card text will be stored in database
00653         $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00654     }
00655 
00664     protected function _setOrderArticles( $aArticleList )
00665     {
00666         // reset articles list
00667         $this->_oArticles = oxNew( 'oxlist' );
00668         $iCurrLang = $this->getOrderLanguage();
00669 
00670         // add all the products we have on basket to the order
00671         foreach ( $aArticleList as $oContent ) {
00672 
00673             //$oContent->oProduct = $oContent->getArticle();
00674             // #M773 Do not use article lazy loading on order save
00675             $oProduct = $oContent->getArticle( true, null, true);
00676 
00677             // copy only if object is oxarticle type
00678             if ( $oProduct->isOrderArticle() ) {
00679                 $oOrderArticle = $oProduct;
00680             } else {
00681 
00682                 // if order language doe not match product language - article must be reloaded in order language
00683                 if ( $iCurrLang != $oProduct->getLanguage() ) {
00684                     $oProduct->loadInLang( $iCurrLang, $oProduct->getProductId() );
00685                 }
00686 
00687                 // set chosen selectlist
00688                 $sSelList = '';
00689                 if ( count( $aChosenSelList = $oContent->getChosenSelList() ) ) {
00690                     foreach ( $aChosenSelList as $oItem ) {
00691                         if ( $sSelList ) {
00692                             $sSelList .= ", ";
00693                         }
00694                         $sSelList .= "{$oItem->name} : {$oItem->value}";
00695                     }
00696                 }
00697 
00698                 $oOrderArticle = oxNew( 'oxorderarticle' );
00699                 $oOrderArticle->setIsNewOrderItem( true );
00700                 $oOrderArticle->copyThis( $oProduct );
00701                 $oOrderArticle->setId();
00702 
00703                 $oOrderArticle->oxorderarticles__oxartnum     = clone $oProduct->oxarticles__oxartnum;
00704                 $oOrderArticle->oxorderarticles__oxselvariant = new oxField( trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->value ), oxField::T_RAW );
00705                 $oOrderArticle->oxorderarticles__oxshortdesc  = new oxField( $oProduct->oxarticles__oxshortdesc->value, oxField::T_RAW );
00706                 // #M974: duplicated entries for the name of variants in orders
00707                 $oOrderArticle->oxorderarticles__oxtitle      = new oxField( trim( $oProduct->oxarticles__oxtitle->value ), oxField::T_RAW );
00708 
00709                 // copying persistent parameters ...
00710                 if ( !is_array( $aPersParams = $oProduct->getPersParams() ) ) {
00711                     $aPersParams = $oContent->getPersParams();
00712                 }
00713                 if ( is_array( $aPersParams ) && count( $aPersParams )) {
00714                     $oOrderArticle->oxorderarticles__oxpersparam = new oxField( serialize( $aPersParams ), oxField::T_RAW );
00715                 }
00716             }
00717 
00718             // ids, titles, numbers ...
00719             $oOrderArticle->oxorderarticles__oxorderid = new oxField( $this->getId() );
00720             $oOrderArticle->oxorderarticles__oxartid   = new oxField( $oContent->getProductId() );
00721             $oOrderArticle->oxorderarticles__oxamount  = new oxField( $oContent->getAmount() );
00722 
00723             // prices
00724             $oPrice = $oContent->getPrice();
00725             $oOrderArticle->oxorderarticles__oxnetprice  = new oxField( $oPrice->getNettoPrice(), oxField::T_RAW );
00726             $oOrderArticle->oxorderarticles__oxvatprice  = new oxField( $oPrice->getVatValue(), oxField::T_RAW );
00727             $oOrderArticle->oxorderarticles__oxbrutprice = new oxField( $oPrice->getBruttoPrice(), oxField::T_RAW );
00728             $oOrderArticle->oxorderarticles__oxvat       = new oxField( $oPrice->getVat(), oxField::T_RAW );
00729 
00730             $oUnitPtice = $oContent->getUnitPrice();
00731             $oOrderArticle->oxorderarticles__oxnprice = new oxField( $oUnitPtice->getNettoPrice(), oxField::T_RAW );
00732             $oOrderArticle->oxorderarticles__oxbprice = new oxField( $oUnitPtice->getBruttoPrice(), oxField::T_RAW );
00733 
00734             // wrap id
00735             $oOrderArticle->oxorderarticles__oxwrapid = new oxField( $oContent->getWrappingId(), oxField::T_RAW );
00736 
00737             // items shop id
00738             $oOrderArticle->oxorderarticles__oxordershopid = new oxField( $oContent->getShopId(), oxField::T_RAW );
00739 
00740             // add information for eMail
00741             //P
00742             //TODO: check if this assign is needed at all
00743             $oOrderArticle->oProduct = $oProduct;
00744 
00745             // simulatin order article list
00746             $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
00747         }
00748     }
00749 
00761     protected function _executePayment( oxBasket $oBasket, $oUserpayment )
00762     {
00763         $oPayTransaction = $this->_getGateway();
00764         $oPayTransaction->setPaymentParams( $oUserpayment );
00765 
00766         if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
00767             $this->delete();
00768 
00769             // checking for error messages
00770             if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
00771                 if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
00772                     return $sLastError;
00773                 }
00774             }
00775 
00776             // checking for error codes
00777             if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
00778                 if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
00779                     return $iLastErrorNo;
00780                 }
00781             }
00782 
00783             return self::ORDER_STATE_PAYMENTERROR; // means no authentication
00784         }
00785         return true; // everything fine
00786     }
00787 
00794     protected function _getGateway()
00795     {
00796         return oxNew( 'oxPaymentGateway' );
00797     }
00798 
00806     protected function _setPayment( $sPaymentid )
00807     {
00808         // copying payment info fields
00809         $aDynvalue = oxSession::getVar( 'dynvalue' );
00810         $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
00811 
00812         // loading payment object
00813         $oPayment = oxNew( 'oxpayment' );
00814 
00815         if (!$oPayment->load( $sPaymentid )) {
00816             return null;
00817         }
00818 
00819         // #756M Preserve already stored payment information
00820         if ( !$aDynvalue && ( $oUserpayment = $this->getPaymentType() ) ) {
00821             if ( is_array( $aStoredDynvalue = $oUserpayment->getDynValues() ) ) {
00822                 foreach ( $aStoredDynvalue as $oVal ) {
00823                     $aDynvalue[$oVal->name] = $oVal->value;
00824                 }
00825             }
00826         }
00827 
00828         $oPayment->setDynValues( oxUtils::getInstance()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
00829 
00830         // collecting dynamic values
00831         $aDynVal = array();
00832 
00833         if ( is_array( $aPaymentDynValues = $oPayment->getDynValues() ) ) {
00834             foreach ( $aPaymentDynValues  as $key => $oVal ) {
00835                 if ( isset( $aDynvalue[$oVal->name] ) ) {
00836                     $oVal->value = $aDynvalue[$oVal->name];
00837                 }
00838 
00839                 //$oPayment->setDynValue($key, $oVal);
00840                 $aPaymentDynValues[$key] = $oVal;
00841                 $aDynVal[$oVal->name] = $oVal->value;
00842             }
00843         }
00844 
00845         // Store this payment information, we might allow users later to
00846         // reactivate already give payment informations
00847 
00848         $oUserpayment = oxNew( 'oxuserpayment' );
00849         $oUserpayment->oxuserpayments__oxuserid     = clone $this->oxorder__oxuserid;
00850         $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
00851         $oUserpayment->oxuserpayments__oxvalue      = new oxField(oxUtils::getInstance()->assignValuesToText( $aDynVal ), oxField::T_RAW);
00852         $oUserpayment->oxpayments__oxdesc           = clone $oPayment->oxpayments__oxdesc;
00853         $oUserpayment->setDynValues( $aPaymentDynValues );
00854         $oUserpayment->save();
00855 
00856         // storing payment information to order
00857         $this->oxorder__oxpaymentid   = new oxField($oUserpayment->getId(), oxField::T_RAW);
00858         $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
00859 
00860         // returning user payment object which will be used later in code ...
00861         return $oUserpayment;
00862     }
00863 
00869     protected function _setFolder()
00870     {
00871         $myConfig = $this->getConfig();
00872         $this->oxorder__oxfolder    = new oxField(key( $myConfig->getShopConfVar(  'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
00873     }
00874 
00884     protected function _updateWishlist( $aArticleList, $oUser )
00885     {
00886 
00887         foreach ( $aArticleList as $oContent) {
00888             if ( ( $sWishId = $oContent->getWishId() ) ) {
00889 
00890                 // checking which wishlist user uses ..
00891                 if ( $sWishId == $oUser->getId() ) {
00892                     $oUserBasket = $oUser->getBasket( 'wishlist' );
00893                 } else {
00894                     $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
00895                     $oUserBasket = oxNew( 'oxuserbasket' );
00896                     $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
00897                 }
00898 
00899                 // updating users wish list
00900                 if ( $oUserBasket ) {
00901                     if ( !($sProdId = $oContent->getWishArticleId() )) {
00902                         $sProdId = $oContent->getProductId();
00903                     }
00904                     $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00905                     $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00906                     if ( $dNewAmount < 0) {
00907                         $dNewAmount = 0;
00908                     }
00909                     $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00910                 }
00911             }
00912         }
00913     }
00914 
00924     protected function _updateNoticeList( $aArticleList, $oUser )
00925     {
00926         // loading users notice list ..
00927         if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
00928             // only if wishlist is enabled
00929             foreach ( $aArticleList as $oContent) {
00930                 $sProdId = $oContent->getProductId();
00931 
00932                 // updating users notice list
00933                 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00934                 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00935                 if ( $dNewAmount < 0) {
00936                     $dNewAmount = 0;
00937                 }
00938                 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00939             }
00940         }
00941     }
00942 
00948     protected function _updateStock()
00949     {
00950         $myConfig = $this->getConfig();
00951 
00952         // ordered articles
00953         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00954             $oOrderArticles = $this->getOrderArticles();
00955             foreach ( $oOrderArticles as $oOrderArticle ) {
00956                 if ( $oOrderArticle->isNewOrderItem() ) {
00957                     $oOrderArticle->updateArticleStock( $oOrderArticle->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
00958                 }
00959             }
00960         }
00961     }
00962 
00974     protected function _markVouchers( $oBasket, $oUser )
00975     {
00976         $this->_aVoucherList = $oBasket->getVouchers();
00977 
00978         if ( is_array( $this->_aVoucherList ) ) {
00979             foreach ( $this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
00980                 $oVoucher = oxNew( 'oxvoucher' );
00981                 $oVoucher->load( $sVoucherId );
00982                 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->fVoucherdiscount );
00983 
00984                 $this->_aVoucherList[$sVoucherId] = $oVoucher;
00985             }
00986         }
00987     }
00988 
00994     public function save()
00995     {
00996         if ( ( $blSave = parent::save() ) ) {
00997 
00998             // saving order articles
00999             $oOrderArticles = $this->getOrderArticles();
01000             if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
01001                 foreach ( $oOrderArticles as $oOrderArticle ) {
01002                     $oOrderArticle->save();
01003                 }
01004             }
01005         }
01006 
01007         return $blSave;
01008     }
01009 
01016     public function getDelAddressInfo()
01017     {
01018         $oDelAdress = null;
01019         if ( ( $soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
01020             $oDelAdress = oxNew( 'oxaddress' );
01021             $oDelAdress->load( $soxAddressId );
01022 
01023             //get delivery country name from delivery country id
01024             if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
01025                  $oCountry = oxNew( 'oxcountry' );
01026                  $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
01027                  $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
01028             }
01029         }
01030         return $oDelAdress;
01031     }
01032 
01043     public function validateStock( $oBasket )
01044     {
01045         foreach ( $oBasket->getContents() as $key => $oContent ) {
01046             try {
01047                 $oProd = $oContent->getArticle();
01048             } catch ( oxNoArticleException $oEx ) {
01049                 $oBasket->removeItem( $key );
01050                 throw $oEx;
01051             } catch ( oxArticleInputException $oEx ) {
01052                 $oBasket->removeItem( $key );
01053                 throw $oEx;
01054             }
01055 
01056             // check if its still available
01057             $dArtStockAmount = $oBasket->getArtStockInBasket( $oProd->getId(), $key );
01058             $iOnStock = $oProd->checkForStock( $oContent->getAmount(), $dArtStockAmount );
01059             if ( $iOnStock !== true ) {
01060                 $oEx = oxNew( 'oxOutOfStockException' );
01061                 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
01062                 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
01063                 $oEx->setProductId( $oProd->getId() );
01064                 $oEx->setRemainingAmount( $oProd->oxarticles__oxstock->value );
01065                 throw $oEx;
01066             }
01067         }
01068     }
01069 
01075     protected function _insert()
01076     {
01077         $myConfig = $this->getConfig();
01078         $oUtilsDate = oxUtilsDate::getInstance();
01079 
01080         //V #M525 orderdate must be the same as it was
01081         if ( !$this->oxorder__oxorderdate->value ) {
01082             $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
01083         } else {
01084             $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
01085         }
01086         $this->oxorder__oxshopid    = new oxField($myConfig->getShopId(), oxField::T_RAW);
01087 
01088         $this->oxorder__oxsenddate  = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01089 
01090         if ( ( $blInsert = parent::_insert() ) ) {
01091             // setting order number
01092             if ( !$this->oxorder__oxordernr->value ) {
01093                 $aWhere = '';
01094                 // separate order numbers for shops ...
01095                 if ( $this->_blSeparateNumbering ) {
01096                     $aWhere = array( 'oxshopid = "'.$myConfig->getShopId().'"' );
01097                 }
01098                 $this->_setRecordNumber( 'oxordernr', $aWhere );
01099             }
01100         }
01101         return $blInsert;
01102     }
01103 
01109     protected function _update()
01110     {
01111         $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01112         return parent::_update();
01113     }
01114 
01123     public function delete( $sOxId = null )
01124     {
01125         if ( $sOxId ) {
01126             if ( !$this->load( $sOxId ) ) {
01127                 // such order does not exist
01128                 return false;
01129             }
01130         } elseif ( !$sOxId ) {
01131             $sOxId = $this->getId();
01132         }
01133 
01134         // no order id is passed
01135         if ( !$sOxId ) {
01136             return false;
01137         }
01138 
01139 
01140         // delete order articles
01141         $oOrderArticles = $this->getOrderArticles( false );
01142         foreach ( $oOrderArticles as $oOrderArticle ) {
01143             $oOrderArticle->delete();
01144         }
01145 
01146         // #440 - deleting user payment info
01147         if ( $oPaymentType = $this->getPaymentType() ) {
01148             $oPaymentType->delete();
01149         }
01150 
01151         return parent::delete( $sOxId );
01152     }
01153 
01163     public function recalculateOrder( $aNewArticles = array() )
01164     {
01165         oxDb::startTransaction();
01166 
01167         try {
01168             $oBasket = $this->_getOrderBasket();
01169 
01170             // add this order articles to virtual basket and recalculates basket
01171             $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles( true ) );
01172 
01173             // adding new articles to existing order
01174             $this->_addArticlesToBasket( $oBasket, $aNewArticles );
01175 
01176             // recalculating basket
01177             $oBasket->calculateBasket( true );
01178 
01179             //finalizing order (skipping payment execution, vouchers marking and mail sending)
01180             $iRet = $this->finalizeOrder( $oBasket, $this->getOrderUser(), true );
01181 
01182             //if finalizing order failed, rollback transaction
01183             if ( $iRet !== 1 ) {
01184                 oxDb::rollbackTransaction();
01185             } else {
01186                 oxDb::commitTransaction();
01187             }
01188 
01189         } catch( Exception $oE ) {
01190             // if exception, rollBack everything
01191             oxDb::rollbackTransaction();
01192 
01193             if ( defined( 'OXID_PHP_UNIT' ) ) {
01194                 throw $oE;
01195             }
01196         }
01197     }
01198 
01199     protected $_oOrderBasket = null;
01207     protected function _getOrderBasket( $blStockCheck = true )
01208     {
01209         $this->_oOrderBasket = oxNew( "oxbasket" );
01210 
01211         // setting stock check mode
01212         $this->_oOrderBasket->setStockCheckMode( $blStockCheck );
01213 
01214         // setting virtual basket user
01215         $this->_oOrderBasket->setBasketUser( $this->getOrderUser() );
01216 
01217         // transferring order id
01218         $this->_oOrderBasket->setOrderId( $this->getId() );
01219 
01220         // setting basket currency order uses
01221         $aCurrencies = $this->getConfig()->getCurrencyArray();
01222         foreach ( $aCurrencies as $oCur ) {
01223             if ($oCur->name == $this->oxorder__oxcurrency->value) {
01224                 $oBasketCur = $oCur;
01225                 break;
01226             }
01227         }
01228 
01229         // setting currency
01230         $this->_oOrderBasket->setBasketCurrency( $oBasketCur );
01231 
01232         // set basket card id and message
01233         $this->_oOrderBasket->setCardId( $this->oxorder__oxcardid->value );
01234         $this->_oOrderBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01235 
01236         if ( $this->_blReloadDiscount ) {
01237             // disabling availability check
01238             $this->_oOrderBasket->setSkipVouchersChecking( true );
01239 
01240             // add previously used vouchers
01241             $sQ = 'select oxid from oxvouchers where oxorderid = "'.$this->getId().'"';
01242             $aVouchers = oxDb::getDb( true )->getAll( $sQ );
01243             foreach ( $aVouchers as $aVoucher ) {
01244                 $this->_oOrderBasket->addVoucher( $aVoucher['oxid'] );
01245             }
01246         } else {
01247             $this->_oOrderBasket->setDiscountCalcMode( false );
01248             $this->_oOrderBasket->setVoucherDiscount( $this->oxorder__oxvoucherdiscount->value );
01249             $this->_oOrderBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
01250         }
01251 
01252         // must be kept old delivery?
01253         if ( !$this->_blReloadDelivery ) {
01254             $this->_oOrderBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01255         } else {
01256             //  set shipping
01257             $this->_oOrderBasket->setShipping( $this->oxorder__oxdeltype->value );
01258             $this->_oOrderBasket->setDeliveryPrice( null );
01259         }
01260 
01261         //set basket payment
01262         $this->_oOrderBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01263 
01264         return $this->_oOrderBasket;
01265     }
01266 
01275     public function setDelivery( $sDeliveryId )
01276     {
01277         $this->reloadDelivery( true );
01278         $this->oxorder__oxdeltype = new oxField( $sDeliveryId );
01279     }
01280 
01286     public function getOrderUser()
01287     {
01288         if ($this->_oUser === null ) {
01289             $this->_oUser = oxNew( "oxuser" );
01290             $this->_oUser->load( $this->oxorder__oxuserid->value );
01291 
01292             // if object is loaded then reusing its order info
01293             if ( $this->_isLoaded ) {
01294                 // bill address
01295                 $this->_oUser->oxuser__oxcompany  = clone $this->oxorder__oxbillcompany;
01296                 $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
01297                 $this->_oUser->oxuser__oxfname    = clone $this->oxorder__oxbillfname;
01298                 $this->_oUser->oxuser__oxlname    = clone $this->oxorder__oxbilllname;
01299                 $this->_oUser->oxuser__oxstreet   = clone $this->oxorder__oxbillstreet;
01300                 $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
01301                 $this->_oUser->oxuser__oxaddinfo  = clone $this->oxorder__oxbilladdinfo;
01302                 $this->_oUser->oxuser__oxustid    = clone $this->oxorder__oxbillustid;
01303 
01304 
01305                 $this->_oUser->oxuser__oxcity      = clone $this->oxorder__oxbillcity;
01306                 $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
01307                 $this->_oUser->oxuser__oxstateid   = clone $this->oxorder__oxbillstateid;
01308                 $this->_oUser->oxuser__oxzip       = clone $this->oxorder__oxbillzip;
01309                 $this->_oUser->oxuser__oxfon       = clone $this->oxorder__oxbillfon;
01310                 $this->_oUser->oxuser__oxfax       = clone $this->oxorder__oxbillfax;
01311                 $this->_oUser->oxuser__oxsal       = clone $this->oxorder__oxbillsal;
01312             }
01313         }
01314 
01315         return $this->_oUser;
01316     }
01317 
01325     public function pdfFooter( $oPdf )
01326     {
01327     }
01328 
01336     public function pdfHeaderplus( $oPdf )
01337     {
01338     }
01339 
01347     public function pdfHeader( $oPdf )
01348     {
01349     }
01350 
01359     public function genPdf( $sFilename, $iSelLang = 0 )
01360     {
01361     }
01362 
01368     public function getInvoiceNum()
01369     {
01370         $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01371         return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01372     }
01373 
01379     public function getNextBillNum()
01380     {
01381         $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01382         return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01383     }
01384 
01390     public function getShippingSetList()
01391     {
01392         // in which country we deliver
01393         if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
01394             $sShipId = $this->oxorder__oxbillcountryid->value;
01395         }
01396 
01397         $oBasket = $this->_getOrderBasket( false );
01398 
01399         // add this order articles to basket and recalculate basket
01400         $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles() );
01401 
01402         // recalculating basket
01403         $oBasket->calculateBasket( true );
01404 
01405         // load fitting deliveries list
01406         $oDeliveryList = oxNew( "oxDeliveryList", "core" );
01407         $oDeliveryList->setCollectFittingDeliveriesSets( true );
01408 
01409         return $oDeliveryList->getDeliveryList( $oBasket, $this->getOrderUser(), $sShipId );
01410     }
01411 
01417     public function getVoucherNrList()
01418     {
01419         $oDB = oxDb::getDb( true );
01420         $aVouchers = array();
01421         $sSelect = "select oxvouchernr from oxvouchers where oxorderid = ".$oDB->quote( $this->oxorder__oxid->value );
01422         $rs = $oDB->execute( $sSelect);
01423         if ($rs != false && $rs->recordCount() > 0) {
01424             while (!$rs->EOF) {
01425                 $aVouchers[] = $rs->fields['oxvouchernr'];
01426                 $rs->moveNext();
01427             }
01428         }
01429         return $aVouchers;
01430     }
01431 
01439     public function getOrderSum( $blToday = false )
01440     {
01441         $sSelect  = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01442         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01443 
01444         if ( $blToday ) {
01445             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01446         }
01447 
01448         return ( double ) oxDb::getDb()->getOne( $sSelect );
01449     }
01450 
01458     public function getOrderCnt( $blToday = false )
01459     {
01460         $sSelect  = 'select count(*) from oxorder where ';
01461         $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'"  and oxorder.oxstorno != "1" ';
01462 
01463         if ( $blToday ) {
01464             $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01465         }
01466 
01467         return ( int ) oxDb::getDb()->getOne( $sSelect );
01468     }
01469 
01470 
01478     protected function _checkOrderExist( $sOxId = null )
01479     {
01480         if ( !$sOxId) {
01481             return false;
01482         }
01483 
01484         $oDb = oxDb::getDb();
01485         if ( $oDb->getOne( 'select oxid from oxorder where oxid = '.$oDb->quote( $sOxId ) ) ) {
01486             return true;
01487         }
01488 
01489         return false;
01490     }
01491 
01501     protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01502     {
01503         $iRet = self::ORDER_STATE_MAILINGERROR;
01504 
01505         // add user, basket and payment to order
01506         $this->_oUser    = $oUser;
01507         $this->_oBasket  = $oBasket;
01508         $this->_oPayment = $oPayment;
01509 
01510         $oxEmail = oxNew( 'oxemail' );
01511 
01512         // send order email to user
01513         if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01514             // mail to user was successfully sent
01515             $iRet = self::ORDER_STATE_OK;
01516         }
01517 
01518         // send order email to shop owner
01519         $oxEmail->sendOrderEMailToOwner( $this );
01520 
01521         return $iRet;
01522     }
01523 
01529     public function getBasket()
01530     {
01531         return $this->_oBasket;
01532     }
01533 
01539     public function getPayment()
01540     {
01541         return $this->_oPayment;
01542     }
01543 
01549     public function getVoucherList()
01550     {
01551         return $this->_aVoucherList;
01552     }
01553 
01559     public function getDelSet()
01560     {
01561         if ( $this->_oDelSet == null ) {
01562             // load deliveryset info
01563             $this->_oDelSet = oxNew( 'oxdeliveryset' );
01564             $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01565         }
01566 
01567         return $this->_oDelSet;
01568     }
01569 
01575     public function getPaymentType()
01576     {
01577         if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType === null ) {
01578             $this->_oPaymentType = false;
01579             $oPaymentType = oxNew( 'oxuserpayment' );
01580             if ( $oPaymentType->load( $this->oxorder__oxpaymentid->value ) ) {
01581                 $this->_oPaymentType = $oPaymentType;
01582             }
01583         }
01584 
01585         return $this->_oPaymentType;
01586     }
01587 
01593     public function getGiftCard()
01594     {
01595         if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01596             $this->_oGiftCard = oxNew( 'oxwrapping' );
01597             $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01598         }
01599 
01600         return $this->_oGiftCard;
01601     }
01602 
01610     public function setSeparateNumbering( $blSeparateNumbering = null )
01611     {
01612         $this->_blSeparateNumbering = $blSeparateNumbering;
01613     }
01614 
01622     public function getLastUserPaymentType( $sUserId)
01623     {
01624         $oDb = oxDb::getDb();
01625         $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid='.$oDb->quote( $sUserId ).' order by oxorder.oxorderdate desc ';
01626         $sLastPaymentId = $oDb->getOne( $sQ );
01627         return $sLastPaymentId;
01628     }
01629 
01642     protected function _makeSelListArray( $sArtId = null, $sOrderArtSelList = null )
01643     {
01644         $oOrder = oxNew( 'oxorderArticle' );
01645         return $oOrder->getOrderArticleSelectList( $sArtId, $sOrderArtSelList );
01646     }
01647 
01656     protected function _addOrderArticlesToBasket( $oBasket, $aOrderArticles )
01657     {
01658         // if no order articles, return empty basket
01659         if ( count( $aOrderArticles ) > 0 ) {
01660 
01661             //adding order articles to basket
01662             foreach ( $aOrderArticles as $oOrderArticle ) {
01663                 $oBasket->addOrderArticleToBasket( $oOrderArticle );
01664             }
01665         }
01666     }
01667 
01676     protected function _addArticlesToBasket( $oBasket, $aArticles )
01677     {
01678         // if no order articles
01679         if ( count($aArticles ) > 0 ) {
01680 
01681             //adding order articles to basket
01682             foreach ( $aArticles as $oArticle ) {
01683                 $aSel = isset( $oArticle->oxorderarticles__oxselvariant ) ? $oArticle->oxorderarticles__oxselvariant->value : null;
01684                 $aPersParam = isset( $oArticle->oxorderarticles__oxpersparam ) ? $oArticle->getPersParams() : null;
01685                 $oBasket->addToBasket( $oArticle->oxorderarticles__oxartid->value,
01686                                        $oArticle->oxorderarticles__oxamount->value,
01687                                        $aSel, $aPersParam );
01688             }
01689         }
01690     }
01691 
01699     protected function _setDeprecatedValues()
01700     {
01701         if ( $this->oxorder__oxstorno->value != 1 ) {
01702             $oCur = $this->getConfig()->getActShopCurrencyObject();
01703             $oLang = oxLang::getInstance();
01704 
01705             $this->totalnetsum   = $this->oxorder__oxtotalnetsum->value;
01706             $this->totalbrutsum  = $this->oxorder__oxtotalbrutsum->value;
01707             $this->totalorder    = $this->oxorder__oxtotalordersum->value;
01708             $this->ftotalnetsum  = $oLang->formatCurrency( $this->oxorder__oxtotalnetsum->value, $oCur );
01709             $this->ftotalbrutsum = $oLang->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $oCur );
01710             $this->fdelcost      = $oLang->formatCurrency( $this->oxorder__oxdelcost->value, $oCur );
01711             $this->fpaycost      = $oLang->formatCurrency( $this->oxorder__oxpaycost->value, $oCur );
01712             $this->fwrapcost     = $oLang->formatCurrency( $this->oxorder__oxwrapcost->value, $oCur );
01713             $this->ftotalorder   = $this->getTotalOrderSum();
01714             $this->totalvouchers = 0;
01715 
01716             if ( $this->oxorder__oxvoucherdiscount->value ) {
01717                 $this->totalvouchers  = $oLang->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $oCur );
01718             }
01719 
01720             if ( $this->oxorder__oxdiscount->value ) {
01721                 $this->discount  = $this->oxorder__oxdiscount->value;
01722                 $this->fdiscount = $oLang->formatCurrency( $this->oxorder__oxdiscount->value, $oCur );
01723             }
01724         }
01725     }
01726 
01732     public function getTotalOrderSum()
01733     {
01734         $oCur = $this->getConfig()->getActShopCurrencyObject();
01735         return number_format( $this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01736     }
01737 
01745     public function getProductVats( $blFormatCurrency = true )
01746     {
01747         $aVats = array();
01748         if ($this->oxorder__oxartvat1->value) {
01749             $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
01750         }
01751         if ($this->oxorder__oxartvat2->value) {
01752             $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
01753         }
01754 
01755         if ( $blFormatCurrency ) {
01756             $oLang = oxLang::getInstance();
01757             $oCur = $this->getConfig()->getActShopCurrencyObject();
01758             foreach ( $aVats as $sKey => $dVat ) {
01759                 $aVats[$sKey] = $oLang->formatCurrency( $dVat, $oCur );
01760             }
01761         }
01762         return $aVats;
01763     }
01764 
01770     public function getBillCountry()
01771     {
01772         if ( !$this->oxorder__oxbillcountry->value ) {
01773             $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
01774         }
01775         return $this->oxorder__oxbillcountry;
01776     }
01777 
01783     public function getDelCountry()
01784     {
01785         if ( !$this->oxorder__oxdelcountry->value ) {
01786             $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
01787         }
01788         return $this->oxorder__oxdelcountry;
01789     }
01797     public function reloadDelivery( $blReload )
01798     {
01799         $this->_blReloadDelivery = $blReload;
01800     }
01801 
01809     public function reloadDiscount( $blReload )
01810     {
01811         $this->_blReloadDiscount = $blReload;
01812     }
01813 
01819     public function cancelOrder()
01820     {
01821         $this->oxorder__oxstorno = new oxField( 1 );
01822         if ( $this->save() ) {
01823             // canceling ordered products
01824             foreach ( $this->getOrderArticles() as $oOrderArticle ) {
01825                 $oOrderArticle->cancelOrderArticle();
01826             }
01827         }
01828     }
01829 
01836     public function getOrderCurrency()
01837     {
01838         if ( $this->_oOrderCurrency === null ) {
01839 
01840             // setting default in case unrecognized currency was set during order
01841             $aCurrencies = $this->getConfig()->getCurrencyArray();
01842             $this->_oOrderCurrency = current( $aCurrencies );
01843 
01844             foreach ( $aCurrencies as $oCurr ) {
01845                 if ( $oCurr->name == $this->oxorder__oxcurrency->value ) {
01846                     $this->_oOrderCurrency = $oCurr;
01847                     break;
01848                 }
01849             }
01850         }
01851         return $this->_oOrderCurrency;
01852     }
01853 
01863     public function validateOrder( $oBasket, $oUser )
01864     {
01865         // validating stock
01866         $iValidState = $this->validateStock( $oBasket );
01867 
01868         if ( !$iValidState ) {
01869             // validating delivery
01870             $iValidState = $this->validateDelivery( $oBasket );
01871         }
01872 
01873         if ( !$iValidState ) {
01874             // validating payment
01875             $iValidState = $this->validatePayment( $oBasket );
01876         }
01877 
01878         return $iValidState;
01879     }
01880 
01889     public function validateDelivery( $oBasket )
01890     {
01891         // proceed with no delivery
01892         // used for other countries
01893         if ( $oBasket->getPaymentId() == 'oxempty') {
01894             return;
01895         }
01896         $oDb = oxDb::getDb();
01897 
01898         $oDelSet = oxNew( "oxdeliveryset" );
01899         $sTable = $oDelSet->getViewName();
01900 
01901         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
01902               $oDb->quote( $oBasket->getShippingId() )." and ".$oDelSet->getSqlActiveSnippet();
01903 
01904         if ( !$oDb->getOne( $sQ ) ) {
01905             // throwing exception
01906             return self::ORDER_STATE_INVALIDDELIVERY;
01907         }
01908     }
01909 
01918     public function validatePayment( $oBasket )
01919     {
01920         $oDb = oxDb::getDb();
01921 
01922         $oPayment = oxNew( "oxpayment" );
01923         $sTable = $oPayment->getViewName();
01924 
01925         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
01926               $oDb->quote( $oBasket->getPaymentId() )." and ".$oPayment->getSqlActiveSnippet();
01927 
01928         if ( !$oDb->getOne( $sQ ) ) {
01929             return self::ORDER_STATE_INVALIDPAYMENT;
01930         }
01931     }
01932 }

Generated by  doxygen 1.6.2