oxorder.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxOrder extends oxBase
00009 {
00010 
00011     // defining order state constants
00017     const ORDER_STATE_MAILINGERROR = 0;
00018 
00024     const ORDER_STATE_OK = 1;
00025 
00031     const ORDER_STATE_PAYMENTERROR = 2;
00032 
00038     const ORDER_STATE_ORDEREXISTS = 3;
00039 
00045     const ORDER_STATE_INVALIDDELIVERY = 4;
00046 
00052     const ORDER_STATE_INVALIDPAYMENT = 5;
00053 
00059     const ORDER_STATE_INVALIDTSPROTECTION = 6;
00060 
00066     const ORDER_STATE_INVALIDDElADDRESSCHANGED = 7;
00067 
00073     const ORDER_STATE_BELOWMINPRICE = 8;
00074 
00080     protected $_aSkipSaveFields = array('oxtimestamp');
00081 
00087     protected $_oArticles = null;
00088 
00094     protected $_oDelSet = null;
00095 
00101     protected $_oGiftCard = null;
00102 
00108     protected $_oPaymentType = null;
00109 
00115     protected $_oPayment = null;
00116 
00122     protected $_aVoucherList = null;
00123 
00129     protected $_oDelPrice = null;
00130 
00136     protected $_oUser = null;
00137 
00143     protected $_oBasket = null;
00144 
00150     protected $_oWrappingPrice = null;
00151 
00157     protected $_oGiftCardPrice = null;
00158 
00164     protected $_oPaymentPrice = null;
00165 
00171     protected $_oTsProtectionPrice = null;
00172 
00178     protected $_sClassName = 'oxorder';
00179 
00185     protected $_blSeparateNumbering = null;
00186 
00192     protected $_iOrderLang = null;
00193 
00199     protected $_blReloadDelivery = true;
00200 
00206     protected $_blReloadDiscount = true;
00207 
00213     protected $_oOrderCurrency = null;
00214 
00220     protected $_oOrderFiles = null;
00221 
00227     protected $_sShipTrackUrl = null;
00228 
00232     public function __construct()
00233     {
00234         parent::__construct();
00235         $this->init('oxorder');
00236 
00237         // set usage of separate orders numbering for different shops
00238         $this->setSeparateNumbering($this->getConfig()->getConfigParam('blSeparateNumbering'));
00239 
00240     }
00241 
00249     public function __get($sName)
00250     {
00251         if ($sName == 'oDelSet') {
00252             return $this->getDelSet();
00253         }
00254 
00255         if ($sName == 'oxorder__oxbillcountry') {
00256             return $this->getBillCountry();
00257         }
00258 
00259         if ($sName == 'oxorder__oxdelcountry') {
00260             return $this->getDelCountry();
00261         }
00262     }
00263 
00271     public function assign($dbRecord)
00272     {
00273 
00274         parent::assign($dbRecord);
00275 
00276         $oUtilsDate = oxRegistry::get("oxUtilsDate");
00277 
00278         // convert date's to international format
00279         $this->oxorder__oxorderdate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxorderdate->value));
00280         $this->oxorder__oxsenddate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxsenddate->value));
00281     }
00282 
00290     protected function _getCountryTitle($sCountryId)
00291     {
00292         $sTitle = null;
00293         if ($sCountryId && $sCountryId != '-1') {
00294             $oCountry = oxNew('oxcountry');
00295             $oCountry->loadInLang($this->getOrderLanguage(), $sCountryId);
00296             $sTitle = $oCountry->oxcountry__oxtitle->value;
00297         }
00298 
00299         return $sTitle;
00300     }
00301 
00309     protected function _getArticles($blExcludeCanceled = false)
00310     {
00311         $sSelect = "SELECT `oxorderarticles`.* FROM `oxorderarticles`
00312                         WHERE `oxorderarticles`.`oxorderid` = '" . $this->getId() . "'" .
00313                    ($blExcludeCanceled ? " AND `oxorderarticles`.`oxstorno` != 1 " : " ") . "
00314                         ORDER BY `oxorderarticles`.`oxartid`, `oxorderarticles`.`oxselvariant`, `oxorderarticles`.`oxpersparam` ";
00315 
00316         // order articles
00317         $oArticles = oxNew('oxlist');
00318         $oArticles->init('oxorderarticle');
00319         $oArticles->selectString($sSelect);
00320 
00321         return $oArticles;
00322     }
00323 
00331     public function getOrderArticles($blExcludeCanceled = false)
00332     {
00333         // checking set value
00334         if ($blExcludeCanceled) {
00335 
00336             return $this->_getArticles(true);
00337 
00338         } elseif ($this->_oArticles === null) {
00339             $this->_oArticles = $this->_getArticles();
00340         }
00341 
00342         return $this->_oArticles;
00343     }
00344 
00350     public function setOrderArticleList($oOrderArticleList)
00351     {
00352         $this->_oArticles = $oOrderArticleList;
00353     }
00354 
00360     public function getOrderDeliveryPrice()
00361     {
00362         if ($this->_oDelPrice != null) {
00363             return $this->_oDelPrice;
00364         }
00365 
00366         $this->_oDelPrice = oxNew('oxprice');
00367         $this->_oDelPrice->setBruttoPriceMode();
00368         $this->_oDelPrice->setPrice($this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value);
00369 
00370         return $this->_oDelPrice;
00371     }
00372 
00378     public function getOrderWrappingPrice()
00379     {
00380         if ($this->_oWrappingPrice != null) {
00381             return $this->_oWrappingPrice;
00382         }
00383 
00384         $this->_oWrappingPrice = oxNew('oxprice');
00385         $this->_oWrappingPrice->setBruttoPriceMode();
00386         $this->_oWrappingPrice->setPrice($this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value);
00387 
00388         return $this->_oWrappingPrice;
00389     }
00390 
00396     public function getOrderGiftCardPrice()
00397     {
00398         if ($this->_oGidtCardPrice != null) {
00399             return $this->_oGidtCardPrice;
00400         }
00401 
00402         $this->_oGidtCardPrice = oxNew('oxprice');
00403         $this->_oGidtCardPrice->setBruttoPriceMode();
00404         $this->_oGidtCardPrice->setPrice($this->oxorder__oxgiftcardcost->value, $this->oxorder__oxgiftcardvat->value);
00405 
00406         return $this->_oGidtCardPrice;
00407     }
00408 
00409 
00415     public function getOrderPaymentPrice()
00416     {
00417         if ($this->_oPaymentPrice != null) {
00418             return $this->_oPaymentPrice;
00419         }
00420 
00421         $this->_oPaymentPrice = oxNew('oxprice');
00422         $this->_oPaymentPrice->setBruttoPriceMode();
00423         $this->_oPaymentPrice->setPrice($this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value);
00424 
00425         return $this->_oPaymentPrice;
00426     }
00427 
00433     public function getOrderTsProtectionPrice()
00434     {
00435         if ($this->_oTsProtectionPrice != null) {
00436             return $this->_oTsProtectionPrice;
00437         }
00438 
00439         $this->_oTsProtectionPrice = oxNew('oxprice');
00440         $this->_oTsProtectionPrice->setBruttoPriceMode();
00441         $this->_oTsProtectionPrice->setPrice($this->oxorder__oxtsprotectcosts->value, $this->getConfig()->getConfigParam('dDefaultVAT'));
00442 
00443         return $this->_oTsProtectionPrice;
00444     }
00445 
00451     public function getOrderNetSum()
00452     {
00453         $dTotalNetSum = 0;
00454 
00455         $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
00456         $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
00457         $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
00458         $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
00459 
00460         return $dTotalNetSum;
00461     }
00462 
00483     public function finalizeOrder(oxBasket $oBasket, $oUser, $blRecalculatingOrder = false)
00484     {
00485         // check if this order is already stored
00486         $sGetChallenge = oxRegistry::getSession()->getVariable('sess_challenge');
00487         if ($this->_checkOrderExist($sGetChallenge)) {
00488             oxRegistry::getUtils()->logger('BLOCKER');
00489 
00490             // we might use this later, this means that somebody klicked like mad on order button
00491             return self::ORDER_STATE_ORDEREXISTS;
00492         }
00493 
00494         // if not recalculating order, use sess_challenge id, else leave old order id
00495         if (!$blRecalculatingOrder) {
00496             // use this ID
00497             $this->setId($sGetChallenge);
00498 
00499             // validating various order/basket parameters before finalizing
00500             if ($iOrderState = $this->validateOrder($oBasket, $oUser)) {
00501                 return $iOrderState;
00502             }
00503         }
00504 
00505         // copies user info
00506         $this->_setUser($oUser);
00507 
00508         // copies basket info
00509         $this->_loadFromBasket($oBasket);
00510 
00511         // payment information
00512         $oUserPayment = $this->_setPayment($oBasket->getPaymentId());
00513 
00514         // set folder information, if order is new
00515         // #M575 in recalculating order case folder must be the same as it was
00516         if (!$blRecalculatingOrder) {
00517             $this->_setFolder();
00518         }
00519 
00520         // marking as not finished
00521         $this->_setOrderStatus('NOT_FINISHED');
00522 
00523         //saving all order data to DB
00524         $this->save();
00525 
00526         // executing payment (on failure deletes order and returns error code)
00527         // in case when recalculating order, payment execution is skipped
00528         if (!$blRecalculatingOrder) {
00529             $blRet = $this->_executePayment($oBasket, $oUserPayment);
00530             if ($blRet !== true) {
00531                 return $blRet;
00532             }
00533         }
00534 
00535         // executing TS protection
00536         if (!$blRecalculatingOrder && $oBasket->getTsProductId()) {
00537             $blRet = $this->_executeTsProtection($oBasket);
00538             if ($blRet !== true) {
00539                 return $blRet;
00540             }
00541         }
00542 
00543         // deleting remark info only when order is finished
00544         oxRegistry::getSession()->deleteVariable('ordrem');
00545         oxRegistry::getSession()->deleteVariable('stsprotection');
00546 
00547         if (!$this->oxorder__oxordernr->value) {
00548             $this->_setNumber();
00549         } else {
00550             oxNew('oxCounter')->update($this->_getCounterIdent(), $this->oxorder__oxordernr->value);
00551         }
00552 
00553         //#4005: Order creation time is not updated when order processing is complete
00554         if (!$blRecalculatingOrder) {
00555             $this->_updateOrderDate();
00556         }
00557 
00558         // updating order trans status (success status)
00559         $this->_setOrderStatus('OK');
00560 
00561         // store orderid
00562         $oBasket->setOrderId($this->getId());
00563 
00564         // updating wish lists
00565         $this->_updateWishlist($oBasket->getContents(), $oUser);
00566 
00567         // updating users notice list
00568         $this->_updateNoticeList($oBasket->getContents(), $oUser);
00569 
00570         // marking vouchers as used and sets them to $this->_aVoucherList (will be used in order email)
00571         // skipping this action in case of order recalculation
00572         if (!$blRecalculatingOrder) {
00573             $this->_markVouchers($oBasket, $oUser);
00574         }
00575 
00576         // send order by email to shop owner and current user
00577         // skipping this action in case of order recalculation
00578         if (!$blRecalculatingOrder) {
00579             $iRet = $this->_sendOrderByEmail($oUser, $oBasket, $oUserPayment);
00580         } else {
00581             $iRet = self::ORDER_STATE_OK;
00582         }
00583 
00584         return $iRet;
00585     }
00586 
00592     public function isNettoMode()
00593     {
00594         return (bool) $this->oxorder__oxisnettomode->value;
00595     }
00596 
00597 
00603     protected function _setOrderStatus($sStatus)
00604     {
00605         $oDb = oxDb::getDb();
00606         $sQ = 'update oxorder set oxtransstatus=' . $oDb->quote($sStatus) . ' where oxid=' . $oDb->quote($this->getId());
00607         $oDb->execute($sQ);
00608 
00609         //updating order object
00610         $this->oxorder__oxtransstatus = new oxField($sStatus, oxField::T_RAW);
00611     }
00612 
00620     protected function _convertVat($sVat)
00621     {
00622         if (strpos($sVat, '.') < strpos($sVat, ',')) {
00623             $sVat = str_replace(array('.', ','), array('', '.'), $sVat);
00624         } else {
00625             $sVat = str_replace(',', '', $sVat);
00626         }
00627 
00628         return (float) $sVat;
00629     }
00630 
00634     protected function _resetVats()
00635     {
00636         $this->oxorder__oxartvat1 = new oxField(null);
00637         $this->oxorder__oxartvatprice1 = new oxField(null);
00638         $this->oxorder__oxartvat2 = new oxField(null);
00639         $this->oxorder__oxartvatprice2 = new oxField(null);
00640     }
00641 
00650     protected function _loadFromBasket(oxBasket $oBasket)
00651     {
00652         $myConfig = $this->getConfig();
00653 
00654         // store IP Address - default must be FALSE as it is illegal to store
00655         if ($myConfig->getConfigParam('blStoreIPs') && $this->oxorder__oxip->value === null) {
00656             $this->oxorder__oxip = new oxField(oxRegistry::get("oxUtilsServer")->getRemoteAddress(), oxField::T_RAW);
00657         }
00658 
00659         //setting view mode
00660         $this->oxorder__oxisnettomode = new oxField($oBasket->isCalculationModeNetto());
00661 
00662         // copying main price info
00663         $this->oxorder__oxtotalnetsum = new oxField($oBasket->getNettoSum());
00664         $this->oxorder__oxtotalbrutsum = new oxField($oBasket->getBruttoSum());
00665         $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00666 
00667         // copying discounted VAT info
00668         $this->_resetVats();
00669         $iVatIndex = 1;
00670         foreach ($oBasket->getProductVats(false) as $iVat => $dPrice) {
00671             $this->{"oxorder__oxartvat$iVatIndex"} = new oxField($this->_convertVat($iVat), oxField::T_RAW);
00672             $this->{"oxorder__oxartvatprice$iVatIndex"} = new oxField($dPrice, oxField::T_RAW);
00673             $iVatIndex++;
00674         }
00675 
00676         // payment costs if available
00677         if (($oPaymentCost = $oBasket->getCosts('oxpayment'))) {
00678             $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00679             $this->oxorder__oxpayvat = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00680         }
00681 
00682         // delivery info
00683         if (($oDeliveryCost = $oBasket->getCosts('oxdelivery'))) {
00684             $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00685             //V #M382: Save VAT, not VAT value for delivery costs
00686             $this->oxorder__oxdelvat = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW); //V #M382
00687             $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00688         }
00689 
00690         // user remark
00691         if (!isset($this->oxorder__oxremark) || $this->oxorder__oxremark->value === null) {
00692             $this->oxorder__oxremark = new oxField(oxRegistry::getSession()->getVariable('ordrem'), oxField::T_RAW);
00693         }
00694 
00695         // currency
00696         $oCur = $myConfig->getActShopCurrencyObject();
00697         $this->oxorder__oxcurrency = new oxField($oCur->name);
00698         $this->oxorder__oxcurrate = new oxField($oCur->rate, oxField::T_RAW);
00699 
00700         // store voucher discount
00701         if (($oVoucherDiscount = $oBasket->getVoucherDiscount())) {
00702             $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00703         }
00704 
00705         // general discount
00706         if ($this->_blReloadDiscount) {
00707             $dDiscount = 0;
00708             $aDiscounts = $oBasket->getDiscounts();
00709             if (count($aDiscounts) > 0) {
00710                 foreach ($aDiscounts as $oDiscount) {
00711                     $dDiscount += $oDiscount->dDiscount;
00712                 }
00713             }
00714             $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00715         }
00716 
00717         //order language
00718         $this->oxorder__oxlang = new oxField($this->getOrderLanguage());
00719 
00720 
00721         // initial status - 'ERROR'
00722         $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00723 
00724         // copies basket product info ...
00725         $this->_setOrderArticles($oBasket->getContents());
00726 
00727         // copies wrapping info
00728         $this->_setWrapping($oBasket);
00729 
00730         // copies TS protection info
00731         $this->_setTsProtection($oBasket);
00732     }
00733 
00740     public function getOrderLanguage()
00741     {
00742         if ($this->_iOrderLang === null) {
00743             if (isset($this->oxorder__oxlang->value)) {
00744                 $this->_iOrderLang = oxRegistry::getLang()->validateLanguage($this->oxorder__oxlang->value);
00745             } else {
00746                 $this->_iOrderLang = oxRegistry::getLang()->getBaseLanguage();
00747             }
00748         }
00749 
00750         return $this->_iOrderLang;
00751     }
00752 
00758     protected function _setUser($oUser)
00759     {
00760 
00761         $this->oxorder__oxuserid = new oxField($oUser->getId());
00762 
00763         // bill address
00764         $this->oxorder__oxbillcompany = clone $oUser->oxuser__oxcompany;
00765         $this->oxorder__oxbillemail = clone $oUser->oxuser__oxusername;
00766         $this->oxorder__oxbillfname = clone $oUser->oxuser__oxfname;
00767         $this->oxorder__oxbilllname = clone $oUser->oxuser__oxlname;
00768         $this->oxorder__oxbillstreet = clone $oUser->oxuser__oxstreet;
00769         $this->oxorder__oxbillstreetnr = clone $oUser->oxuser__oxstreetnr;
00770         $this->oxorder__oxbilladdinfo = clone $oUser->oxuser__oxaddinfo;
00771         $this->oxorder__oxbillustid = clone $oUser->oxuser__oxustid;
00772         $this->oxorder__oxbillcity = clone $oUser->oxuser__oxcity;
00773         $this->oxorder__oxbillcountryid = clone $oUser->oxuser__oxcountryid;
00774         $this->oxorder__oxbillstateid = clone $oUser->oxuser__oxstateid;
00775         $this->oxorder__oxbillzip = clone $oUser->oxuser__oxzip;
00776         $this->oxorder__oxbillfon = clone $oUser->oxuser__oxfon;
00777         $this->oxorder__oxbillfax = clone $oUser->oxuser__oxfax;
00778         $this->oxorder__oxbillsal = clone $oUser->oxuser__oxsal;
00779 
00780 
00781         // delivery address
00782         if (($oDelAdress = $this->getDelAddressInfo())) {
00783             // set delivery address
00784             $this->oxorder__oxdelcompany = clone $oDelAdress->oxaddress__oxcompany;
00785             $this->oxorder__oxdelfname = clone $oDelAdress->oxaddress__oxfname;
00786             $this->oxorder__oxdellname = clone $oDelAdress->oxaddress__oxlname;
00787             $this->oxorder__oxdelstreet = clone $oDelAdress->oxaddress__oxstreet;
00788             $this->oxorder__oxdelstreetnr = clone $oDelAdress->oxaddress__oxstreetnr;
00789             $this->oxorder__oxdeladdinfo = clone $oDelAdress->oxaddress__oxaddinfo;
00790             $this->oxorder__oxdelcity = clone $oDelAdress->oxaddress__oxcity;
00791             $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00792             $this->oxorder__oxdelstateid = clone $oDelAdress->oxaddress__oxstateid;
00793             $this->oxorder__oxdelzip = clone $oDelAdress->oxaddress__oxzip;
00794             $this->oxorder__oxdelfon = clone $oDelAdress->oxaddress__oxfon;
00795             $this->oxorder__oxdelfax = clone $oDelAdress->oxaddress__oxfax;
00796             $this->oxorder__oxdelsal = clone $oDelAdress->oxaddress__oxsal;
00797         }
00798     }
00799 
00805     protected function _setWrapping(oxBasket $oBasket)
00806     {
00807         $myConfig = $this->getConfig();
00808 
00809         // wrapping price
00810         if (($oWrappingCost = $oBasket->getCosts('oxwrapping'))) {
00811             $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00812             // wrapping VAT will be always calculated (#3757)
00813             $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00814         }
00815 
00816         if (($oGiftCardCost = $oBasket->getCosts('oxgiftcard'))) {
00817             $this->oxorder__oxgiftcardcost = new oxField($oGiftCardCost->getBruttoPrice(), oxField::T_RAW);
00818             $this->oxorder__oxgiftcardvat = new oxField($oGiftCardCost->getVAT(), oxField::T_RAW);
00819         }
00820 
00821         // greetings card
00822         $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00823 
00824         // card text will be stored in database
00825         $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00826     }
00827 
00834     protected function _setOrderArticles($aArticleList)
00835     {
00836         // reset articles list
00837         $this->_oArticles = oxNew('oxlist');
00838         $iCurrLang = $this->getOrderLanguage();
00839 
00840         // add all the products we have on basket to the order
00841         foreach ($aArticleList as $oContent) {
00842 
00843             //$oContent->oProduct = $oContent->getArticle();
00844             // #M773 Do not use article lazy loading on order save
00845             $oProduct = $oContent->getArticle(true, null, true);
00846 
00847             // copy only if object is oxarticle type
00848             if ($oProduct->isOrderArticle()) {
00849                 $oOrderArticle = $oProduct;
00850             } else {
00851 
00852                 // if order language doe not match product language - article must be reloaded in order language
00853                 if ($iCurrLang != $oProduct->getLanguage()) {
00854                     $oProduct->loadInLang($iCurrLang, $oProduct->getProductId());
00855                 }
00856 
00857                 // set chosen select list
00858                 $sSelList = '';
00859                 if (count($aChosenSelList = $oContent->getChosenSelList())) {
00860                     foreach ($aChosenSelList as $oItem) {
00861                         if ($sSelList) {
00862                             $sSelList .= ", ";
00863                         }
00864                         $sSelList .= "{$oItem->name} : {$oItem->value}";
00865                     }
00866                 }
00867 
00868                 $oOrderArticle = oxNew('oxorderarticle');
00869                 $oOrderArticle->setIsNewOrderItem(true);
00870                 $oOrderArticle->copyThis($oProduct);
00871                 $oOrderArticle->setId();
00872 
00873                 $oOrderArticle->oxorderarticles__oxartnum = clone $oProduct->oxarticles__oxartnum;
00874                 $oOrderArticle->oxorderarticles__oxselvariant = new oxField(trim($sSelList . ' ' . $oProduct->oxarticles__oxvarselect->getRawValue()), oxField::T_RAW);
00875                 $oOrderArticle->oxorderarticles__oxshortdesc = new oxField($oProduct->oxarticles__oxshortdesc->getRawValue(), oxField::T_RAW);
00876                 // #M974: duplicated entries for the name of variants in orders
00877                 $oOrderArticle->oxorderarticles__oxtitle = new oxField(trim($oProduct->oxarticles__oxtitle->getRawValue()), oxField::T_RAW);
00878 
00879                 // copying persistent parameters ...
00880                 if (!is_array($aPersParams = $oProduct->getPersParams())) {
00881                     $aPersParams = $oContent->getPersParams();
00882                 }
00883                 if (is_array($aPersParams) && count($aPersParams)) {
00884                     $oOrderArticle->oxorderarticles__oxpersparam = new oxField(serialize($aPersParams), oxField::T_RAW);
00885                 }
00886             }
00887 
00888             // ids, titles, numbers ...
00889             $oOrderArticle->oxorderarticles__oxorderid = new oxField($this->getId());
00890             $oOrderArticle->oxorderarticles__oxartid = new oxField($oContent->getProductId());
00891             $oOrderArticle->oxorderarticles__oxamount = new oxField($oContent->getAmount());
00892 
00893             // prices
00894             $oPrice = $oContent->getPrice();
00895             $oOrderArticle->oxorderarticles__oxnetprice = new oxField($oPrice->getNettoPrice(), oxField::T_RAW);
00896             $oOrderArticle->oxorderarticles__oxvatprice = new oxField($oPrice->getVatValue(), oxField::T_RAW);
00897             $oOrderArticle->oxorderarticles__oxbrutprice = new oxField($oPrice->getBruttoPrice(), oxField::T_RAW);
00898             $oOrderArticle->oxorderarticles__oxvat = new oxField($oPrice->getVat(), oxField::T_RAW);
00899 
00900             $oUnitPtice = $oContent->getUnitPrice();
00901             $oOrderArticle->oxorderarticles__oxnprice = new oxField($oUnitPtice->getNettoPrice(), oxField::T_RAW);
00902             $oOrderArticle->oxorderarticles__oxbprice = new oxField($oUnitPtice->getBruttoPrice(), oxField::T_RAW);
00903 
00904             // wrap id
00905             $oOrderArticle->oxorderarticles__oxwrapid = new oxField($oContent->getWrappingId(), oxField::T_RAW);
00906 
00907             // items shop id
00908             $oOrderArticle->oxorderarticles__oxordershopid = new oxField($oContent->getShopId(), oxField::T_RAW);
00909 
00910             // bundle?
00911             $oOrderArticle->oxorderarticles__oxisbundle = new oxField($oContent->isBundle());
00912 
00913             // add information for eMail
00914             //P
00915             //TODO: check if this assign is needed at all
00916             $oOrderArticle->oProduct = $oProduct;
00917 
00918             $oOrderArticle->setArticle($oProduct);
00919 
00920             // simulation order article list
00921             $this->_oArticles->offsetSet($oOrderArticle->getId(), $oOrderArticle);
00922         }
00923     }
00924 
00936     protected function _executePayment(oxBasket $oBasket, $oUserpayment)
00937     {
00938         $oPayTransaction = $this->_getGateway();
00939         $oPayTransaction->setPaymentParams($oUserpayment);
00940 
00941         if (!$oPayTransaction->executePayment($oBasket->getPrice()->getBruttoPrice(), $this)) {
00942             $this->delete();
00943 
00944             // checking for error messages
00945             if (method_exists($oPayTransaction, 'getLastError')) {
00946                 if (($sLastError = $oPayTransaction->getLastError())) {
00947                     return $sLastError;
00948                 }
00949             }
00950 
00951             // checking for error codes
00952             if (method_exists($oPayTransaction, 'getLastErrorNo')) {
00953                 if (($iLastErrorNo = $oPayTransaction->getLastErrorNo())) {
00954                     return $iLastErrorNo;
00955                 }
00956             }
00957 
00958             return self::ORDER_STATE_PAYMENTERROR; // means no authentication
00959         }
00960 
00961         return true; // everything fine
00962     }
00963 
00970     protected function _getGateway()
00971     {
00972         return oxNew('oxPaymentGateway');
00973     }
00974 
00982     protected function _setPayment($sPaymentid)
00983     {
00984         // copying payment info fields
00985         $aDynvalue = oxRegistry::getSession()->getVariable('dynvalue');
00986         $aDynvalue = $aDynvalue ? $aDynvalue : oxRegistry::getConfig()->getRequestParameter('dynvalue');
00987 
00988         // loading payment object
00989         $oPayment = oxNew('oxpayment');
00990 
00991         if (!$oPayment->load($sPaymentid)) {
00992             return null;
00993         }
00994 
00995         // #756M Preserve already stored payment information
00996         if (!$aDynvalue && ($oUserpayment = $this->getPaymentType())) {
00997             if (is_array($aStoredDynvalue = $oUserpayment->getDynValues())) {
00998                 foreach ($aStoredDynvalue as $oVal) {
00999                     $aDynvalue[$oVal->name] = $oVal->value;
01000                 }
01001             }
01002         }
01003 
01004         $oPayment->setDynValues(oxRegistry::getUtils()->assignValuesFromText($oPayment->oxpayments__oxvaldesc->value));
01005 
01006         // collecting dynamic values
01007         $aDynVal = array();
01008 
01009         if (is_array($aPaymentDynValues = $oPayment->getDynValues())) {
01010             foreach ($aPaymentDynValues as $key => $oVal) {
01011                 if (isset($aDynvalue[$oVal->name])) {
01012                     $oVal->value = $aDynvalue[$oVal->name];
01013                 }
01014 
01015                 //$oPayment->setDynValue($key, $oVal);
01016                 $aPaymentDynValues[$key] = $oVal;
01017                 $aDynVal[$oVal->name] = $oVal->value;
01018             }
01019         }
01020 
01021         // Store this payment information, we might allow users later to
01022         // reactivate already give payment information
01023 
01024         $oUserpayment = oxNew('oxuserpayment');
01025         $oUserpayment->oxuserpayments__oxuserid = clone $this->oxorder__oxuserid;
01026         $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
01027         $oUserpayment->oxuserpayments__oxvalue = new oxField(oxRegistry::getUtils()->assignValuesToText($aDynVal), oxField::T_RAW);
01028         $oUserpayment->oxpayments__oxdesc = clone $oPayment->oxpayments__oxdesc;
01029         $oUserpayment->oxpayments__oxlongdesc = clone $oPayment->oxpayments__oxlongdesc;
01030         $oUserpayment->setDynValues($aPaymentDynValues);
01031         $oUserpayment->save();
01032 
01033         // storing payment information to order
01034         $this->oxorder__oxpaymentid = new oxField($oUserpayment->getId(), oxField::T_RAW);
01035         $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
01036 
01037         // returning user payment object which will be used later in code ...
01038         return $oUserpayment;
01039     }
01040 
01044     protected function _setFolder()
01045     {
01046         $myConfig = $this->getConfig();
01047         $this->oxorder__oxfolder = new oxField(key($myConfig->getShopConfVar('aOrderfolder', $myConfig->getShopId())), oxField::T_RAW);
01048     }
01049 
01057     protected function _updateWishlist($aArticleList, $oUser)
01058     {
01059 
01060         foreach ($aArticleList as $oContent) {
01061             if (($sWishId = $oContent->getWishId())) {
01062 
01063                 // checking which wishlist user uses ..
01064                 if ($sWishId == $oUser->getId()) {
01065                     $oUserBasket = $oUser->getBasket('wishlist');
01066                 } else {
01067                     $aWhere = array('oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist');
01068                     $oUserBasket = oxNew('oxuserbasket');
01069                     $oUserBasket->assignRecord($oUserBasket->buildSelectString($aWhere));
01070                 }
01071 
01072                 // updating users wish list
01073                 if ($oUserBasket) {
01074                     if (!($sProdId = $oContent->getWishArticleId())) {
01075                         $sProdId = $oContent->getProductId();
01076                     }
01077                     $oUserBasketItem = $oUserBasket->getItem($sProdId, $oContent->getSelList());
01078                     $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
01079                     if ($dNewAmount < 0) {
01080                         $dNewAmount = 0;
01081                     }
01082                     $oUserBasket->addItemToBasket($sProdId, $dNewAmount, $oContent->getSelList(), true);
01083                 }
01084             }
01085         }
01086     }
01087 
01095     protected function _updateNoticeList($aArticleList, $oUser)
01096     {
01097         // loading users notice list ..
01098         if ($oUserBasket = $oUser->getBasket('noticelist')) {
01099             // only if wishlist is enabled
01100             foreach ($aArticleList as $oContent) {
01101                 $sProdId = $oContent->getProductId();
01102 
01103                 // updating users notice list
01104                 $oUserBasketItem = $oUserBasket->getItem($sProdId, $oContent->getSelList(), $oContent->getPersParams());
01105                 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
01106                 if ($dNewAmount < 0) {
01107                     $dNewAmount = 0;
01108                 }
01109                 $oUserBasket->addItemToBasket($sProdId, $dNewAmount, $oContent->getSelList(), true, $oContent->getPersParams());
01110             }
01111         }
01112     }
01113 
01117     protected function _updateOrderDate()
01118     {
01119         $oDb = oxDb::getDb();
01120         $sDate = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
01121         $sQ = 'update oxorder set oxorderdate=' . $oDb->quote($sDate) . ' where oxid=' . $oDb->quote($this->getId());
01122         $this->oxorder__oxorderdate = new oxField($sDate, oxField::T_RAW);
01123         $oDb->execute($sQ);
01124     }
01125 
01133     protected function _markVouchers($oBasket, $oUser)
01134     {
01135         $this->_aVoucherList = $oBasket->getVouchers();
01136 
01137         if (is_array($this->_aVoucherList)) {
01138             foreach ($this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
01139                 $oVoucher = oxNew('oxvoucher');
01140                 $oVoucher->load($sVoucherId);
01141                 $oVoucher->markAsUsed($this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->dVoucherdiscount);
01142 
01143                 $this->_aVoucherList[$sVoucherId] = $oVoucher;
01144             }
01145         }
01146     }
01147 
01153     public function save()
01154     {
01155         if (($blSave = parent::save())) {
01156 
01157             // saving order articles
01158             $oOrderArticles = $this->getOrderArticles();
01159             if ($oOrderArticles && count($oOrderArticles) > 0) {
01160                 foreach ($oOrderArticles as $oOrderArticle) {
01161                     $oOrderArticle->save();
01162                 }
01163             }
01164         }
01165 
01166         return $blSave;
01167     }
01168 
01175     public function getDelAddressInfo()
01176     {
01177         $oDelAdress = null;
01178         if (!($soxAddressId = oxRegistry::getConfig()->getRequestParameter('deladrid'))) {
01179             $soxAddressId = oxRegistry::getSession()->getVariable('deladrid');
01180         }
01181         if ($soxAddressId) {
01182             $oDelAdress = oxNew('oxaddress');
01183             $oDelAdress->load($soxAddressId);
01184 
01185             //get delivery country name from delivery country id
01186             if ($oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1) {
01187                 $oCountry = oxNew('oxcountry');
01188                 $oCountry->load($oDelAdress->oxaddress__oxcountryid->value);
01189                 $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
01190             }
01191         }
01192 
01193         return $oDelAdress;
01194     }
01195 
01204     public function validateStock($oBasket)
01205     {
01206         foreach ($oBasket->getContents() as $key => $oContent) {
01207             try {
01208                 $oProd = $oContent->getArticle(true);
01209             } catch (oxNoArticleException $oEx) {
01210                 $oBasket->removeItem($key);
01211                 throw $oEx;
01212             } catch (oxArticleInputException $oEx) {
01213                 $oBasket->removeItem($key);
01214                 throw $oEx;
01215             }
01216 
01217             // check if its still available
01218             $dArtStockAmount = $oBasket->getArtStockInBasket($oProd->getId(), $key);
01219             $iOnStock = $oProd->checkForStock($oContent->getAmount(), $dArtStockAmount);
01220             if ($iOnStock !== true) {
01222                 $oEx = oxNew('oxOutOfStockException');
01223                 $oEx->setMessage('ERROR_MESSAGE_OUTOFSTOCK_OUTOFSTOCK');
01224                 $oEx->setArticleNr($oProd->oxarticles__oxartnum->value);
01225                 $oEx->setProductId($oProd->getId());
01226 
01227                 if (!is_numeric($iOnStock)) {
01228                     $iOnStock = 0;
01229                 }
01230                 $oEx->setRemainingAmount($iOnStock);
01231                 throw $oEx;
01232             }
01233         }
01234     }
01235 
01241     protected function _insert()
01242     {
01243         $myConfig = $this->getConfig();
01244         $oUtilsDate = oxRegistry::get("oxUtilsDate");
01245 
01246         //V #M525 orderdate must be the same as it was
01247         if (!$this->oxorder__oxorderdate->value) {
01248             $this->oxorder__oxorderdate = new oxField(date('Y-m-d H:i:s', $oUtilsDate->getTime()), oxField::T_RAW);
01249         } else {
01250             $this->oxorder__oxorderdate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxorderdate->value, true));
01251         }
01252 
01253         $this->oxorder__oxshopid = new oxField($myConfig->getShopId(), oxField::T_RAW);
01254         $this->oxorder__oxsenddate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxsenddate->value, true));
01255 
01256         $blInsert = parent::_insert();
01257 
01258         return $blInsert;
01259     }
01260 
01266     protected function _getCounterIdent()
01267     {
01268         $sCounterIdent = ($this->_blSeparateNumbering) ? 'oxOrder_' . $this->getConfig()->getShopId() : 'oxOrder';
01269 
01270         return $sCounterIdent;
01271     }
01272 
01273 
01279     protected function _setNumber()
01280     {
01281         $oDb = oxDb::getDb();
01282 
01283         $iCnt = oxNew('oxCounter')->getNext($this->_getCounterIdent());
01284         $sQ = "update oxorder set oxordernr = ? where oxid = ?";
01285         $blUpdate = ( bool ) $oDb->execute($sQ, array($iCnt, $this->getId()));
01286 
01287         if ($blUpdate) {
01288             $this->oxorder__oxordernr = new oxField($iCnt);
01289         }
01290 
01291         return $blUpdate;
01292     }
01293 
01299     protected function _update()
01300     {
01301         $this->_aSkipSaveFields = array('oxtimestamp', 'oxorderdate');
01302         $this->oxorder__oxsenddate = new oxField(oxRegistry::get("oxUtilsDate")->formatDBDate($this->oxorder__oxsenddate->value, true));
01303 
01304         return parent::_update();
01305     }
01306 
01315     public function delete($sOxId = null)
01316     {
01317         if ($sOxId) {
01318             if (!$this->load($sOxId)) {
01319                 // such order does not exist
01320                 return false;
01321             }
01322         } elseif (!$sOxId) {
01323             $sOxId = $this->getId();
01324         }
01325 
01326         // no order id is passed
01327         if (!$sOxId) {
01328             return false;
01329         }
01330 
01331 
01332         // delete order articles
01333         $oOrderArticles = $this->getOrderArticles(false);
01334         foreach ($oOrderArticles as $oOrderArticle) {
01335             $oOrderArticle->delete();
01336         }
01337 
01338         // #440 - deleting user payment info
01339         if ($oPaymentType = $this->getPaymentType()) {
01340             $oPaymentType->delete();
01341         }
01342 
01343         return parent::delete($sOxId);
01344     }
01345 
01355     public function recalculateOrder($aNewArticles = array())
01356     {
01357         oxDb::getDb()->startTransaction();
01358 
01359         try {
01360             $oBasket = $this->_getOrderBasket();
01361 
01362             // add this order articles to virtual basket and recalculates basket
01363             $this->_addOrderArticlesToBasket($oBasket, $this->getOrderArticles(true));
01364 
01365             // adding new articles to existing order
01366             $this->_addArticlesToBasket($oBasket, $aNewArticles);
01367 
01368             // recalculating basket
01369             $oBasket->calculateBasket(true);
01370 
01371             //finalizing order (skipping payment execution, vouchers marking and mail sending)
01372             $iRet = $this->finalizeOrder($oBasket, $this->getOrderUser(), true);
01373 
01374             //if finalizing order failed, rollback transaction
01375             if ($iRet !== 1) {
01376                 oxDb::getDb()->rollbackTransaction();
01377             } else {
01378                 oxDb::getDb()->commitTransaction();
01379             }
01380 
01381         } catch (Exception $oE) {
01382             // if exception, rollBack everything
01383             oxDb::getDb()->rollbackTransaction();
01384 
01385             if (defined('OXID_PHP_UNIT')) {
01386                 throw $oE;
01387             }
01388         }
01389     }
01390 
01391     protected $_oOrderBasket = null;
01392 
01400     protected function _getOrderBasket($blStockCheck = true)
01401     {
01402         $this->_oOrderBasket = oxNew("oxBasket");
01403         $this->_oOrderBasket->enableSaveToDataBase(false);
01404 
01405         //setting recalculation mode
01406         $this->_oOrderBasket->setCalculationModeNetto($this->isNettoMode());
01407 
01408         // setting stock check mode
01409         $this->_oOrderBasket->setStockCheckMode($blStockCheck);
01410 
01411         // setting virtual basket user
01412         $this->_oOrderBasket->setBasketUser($this->getOrderUser());
01413 
01414         // transferring order id
01415         $this->_oOrderBasket->setOrderId($this->getId());
01416 
01417         // setting basket currency order uses
01418         $aCurrencies = $this->getConfig()->getCurrencyArray();
01419         foreach ($aCurrencies as $oCur) {
01420             if ($oCur->name == $this->oxorder__oxcurrency->value) {
01421                 $oBasketCur = $oCur;
01422                 break;
01423             }
01424         }
01425 
01426         // setting currency
01427         $this->_oOrderBasket->setBasketCurrency($oBasketCur);
01428 
01429         // set basket card id and message
01430         $this->_oOrderBasket->setCardId($this->oxorder__oxcardid->value);
01431         $this->_oOrderBasket->setCardMessage($this->oxorder__oxcardtext->value);
01432 
01433         if ($this->_blReloadDiscount) {
01434             $oDb = oxDb::getDb(oxDb::FETCH_MODE_ASSOC);
01435             // disabling availability check
01436             $this->_oOrderBasket->setSkipVouchersChecking(true);
01437 
01438             // add previously used vouchers
01439             $sQ = 'select oxid from oxvouchers where oxorderid = ' . $oDb->quote($this->getId());
01440             $aVouchers = $oDb->getAll($sQ);
01441             foreach ($aVouchers as $aVoucher) {
01442                 $this->_oOrderBasket->addVoucher($aVoucher['oxid']);
01443             }
01444         } else {
01445             $this->_oOrderBasket->setDiscountCalcMode(false);
01446             $this->_oOrderBasket->setVoucherDiscount($this->oxorder__oxvoucherdiscount->value);
01447             $this->_oOrderBasket->setTotalDiscount($this->oxorder__oxdiscount->value);
01448         }
01449 
01450         // must be kept old delivery?
01451         if (!$this->_blReloadDelivery) {
01452             $this->_oOrderBasket->setDeliveryPrice($this->getOrderDeliveryPrice());
01453         } else {
01454             //  set shipping
01455             $this->_oOrderBasket->setShipping($this->oxorder__oxdeltype->value);
01456             $this->_oOrderBasket->setDeliveryPrice(null);
01457         }
01458 
01459         //set basket payment
01460         $this->_oOrderBasket->setPayment($this->oxorder__oxpaymenttype->value);
01461 
01462         return $this->_oOrderBasket;
01463     }
01464 
01471     public function setDelivery($sDeliveryId)
01472     {
01473         $this->reloadDelivery(true);
01474         $this->oxorder__oxdeltype = new oxField($sDeliveryId);
01475     }
01476 
01482     public function getOrderUser()
01483     {
01484         if ($this->_oUser === null) {
01485             $this->_oUser = oxNew("oxuser");
01486             $this->_oUser->load($this->oxorder__oxuserid->value);
01487 
01488             // if object is loaded then reusing its order info
01489             if ($this->_isLoaded) {
01490                 // bill address
01491                 $this->_oUser->oxuser__oxcompany = clone $this->oxorder__oxbillcompany;
01492                 $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
01493                 $this->_oUser->oxuser__oxfname = clone $this->oxorder__oxbillfname;
01494                 $this->_oUser->oxuser__oxlname = clone $this->oxorder__oxbilllname;
01495                 $this->_oUser->oxuser__oxstreet = clone $this->oxorder__oxbillstreet;
01496                 $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
01497                 $this->_oUser->oxuser__oxaddinfo = clone $this->oxorder__oxbilladdinfo;
01498                 $this->_oUser->oxuser__oxustid = clone $this->oxorder__oxbillustid;
01499 
01500 
01501                 $this->_oUser->oxuser__oxcity = clone $this->oxorder__oxbillcity;
01502                 $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
01503                 $this->_oUser->oxuser__oxstateid = clone $this->oxorder__oxbillstateid;
01504                 $this->_oUser->oxuser__oxzip = clone $this->oxorder__oxbillzip;
01505                 $this->_oUser->oxuser__oxfon = clone $this->oxorder__oxbillfon;
01506                 $this->_oUser->oxuser__oxfax = clone $this->oxorder__oxbillfax;
01507                 $this->_oUser->oxuser__oxsal = clone $this->oxorder__oxbillsal;
01508             }
01509         }
01510 
01511         return $this->_oUser;
01512     }
01513 
01519     public function pdfFooter($oPdf)
01520     {
01521     }
01522 
01528     public function pdfHeaderplus($oPdf)
01529     {
01530     }
01531 
01537     public function pdfHeader($oPdf)
01538     {
01539     }
01540 
01547     public function genPdf($sFilename, $iSelLang = 0)
01548     {
01549     }
01550 
01556     public function getInvoiceNum()
01557     {
01558         $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "' . $this->getConfig()->getShopId() . '" ';
01559 
01560         return (( int ) oxDb::getDb()->getOne($sQ, false) + 1);
01561     }
01562 
01568     public function getNextBillNum()
01569     {
01570         $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "' . $this->getConfig()->getShopId() . '" ';
01571 
01572         return (( int ) oxDb::getDb()->getOne($sQ, false) + 1);
01573     }
01574 
01580     public function getShippingSetList()
01581     {
01582         // in which country we deliver
01583         if (!($sShipId = $this->oxorder__oxdelcountryid->value)) {
01584             $sShipId = $this->oxorder__oxbillcountryid->value;
01585         }
01586 
01587         $oBasket = $this->_getOrderBasket(false);
01588 
01589         // unsetting bundles
01590         $oOrderArticles = $this->getOrderArticles();
01591         foreach ($oOrderArticles as $sItemId => $oItem) {
01592             if ($oItem->isBundle()) {
01593                 $oOrderArticles->offsetUnset($sItemId);
01594             }
01595         }
01596 
01597         // add this order articles to basket and recalculate basket
01598         $this->_addOrderArticlesToBasket($oBasket, $oOrderArticles);
01599 
01600         // recalculating basket
01601         $oBasket->calculateBasket(true);
01602 
01603         // load fitting deliveries list
01604         $oDeliveryList = oxNew("oxDeliveryList", "core");
01605         $oDeliveryList->setCollectFittingDeliveriesSets(true);
01606 
01607         return $oDeliveryList->getDeliveryList($oBasket, $this->getOrderUser(), $sShipId);
01608     }
01609 
01615     public function getVoucherNrList()
01616     {
01617         $oDb = oxDb::getDb(oxDb::FETCH_MODE_ASSOC);
01618         $aVouchers = array();
01619         $sSelect = "select oxvouchernr from oxvouchers where oxorderid = " . $oDb->quote($this->oxorder__oxid->value);
01620         $rs = $oDb->select($sSelect);
01621         if ($rs != false && $rs->recordCount() > 0) {
01622             while (!$rs->EOF) {
01623                 $aVouchers[] = $rs->fields['oxvouchernr'];
01624                 $rs->moveNext();
01625             }
01626         }
01627 
01628         return $aVouchers;
01629     }
01630 
01638     public function getOrderSum($blToday = false)
01639     {
01640         $sSelect = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01641         $sSelect .= 'oxshopid = "' . $this->getConfig()->getShopId() . '" and oxorder.oxstorno != "1" ';
01642 
01643         if ($blToday) {
01644             $sSelect .= 'and oxorderdate like "' . date('Y-m-d') . '%" ';
01645         }
01646 
01647         return ( double ) oxDb::getDb()->getOne($sSelect, false, false);
01648     }
01649 
01657     public function getOrderCnt($blToday = false)
01658     {
01659         $sSelect = 'select count(*) from oxorder where ';
01660         $sSelect .= 'oxshopid = "' . $this->getConfig()->getShopId() . '"  and oxorder.oxstorno != "1" ';
01661 
01662         if ($blToday) {
01663             $sSelect .= 'and oxorderdate like "' . date('Y-m-d') . '%" ';
01664         }
01665 
01666         return ( int ) oxDb::getDb()->getOne($sSelect, false, false);
01667     }
01668 
01669 
01677     protected function _checkOrderExist($sOxId = null)
01678     {
01679         if (!$sOxId) {
01680             return false;
01681         }
01682 
01683         $oDb = oxDb::getDb();
01684         if ($oDb->getOne('select oxid from oxorder where oxid = ' . $oDb->quote($sOxId), false, false)) {
01685             return true;
01686         }
01687 
01688         return false;
01689     }
01690 
01700     protected function _sendOrderByEmail($oUser = null, $oBasket = null, $oPayment = null)
01701     {
01702         $iRet = self::ORDER_STATE_MAILINGERROR;
01703 
01704         // add user, basket and payment to order
01705         $this->_oUser = $oUser;
01706         $this->_oBasket = $oBasket;
01707         $this->_oPayment = $oPayment;
01708 
01709         $oxEmail = oxNew('oxemail');
01710 
01711         // send order email to user
01712         if ($oxEmail->sendOrderEMailToUser($this)) {
01713             // mail to user was successfully sent
01714             $iRet = self::ORDER_STATE_OK;
01715         }
01716 
01717         // send order email to shop owner
01718         $oxEmail->sendOrderEMailToOwner($this);
01719 
01720         return $iRet;
01721     }
01722 
01728     public function getBasket()
01729     {
01730         return $this->_oBasket;
01731     }
01732 
01738     public function getPayment()
01739     {
01740         return $this->_oPayment;
01741     }
01742 
01748     public function getVoucherList()
01749     {
01750         return $this->_aVoucherList;
01751     }
01752 
01758     public function getDelSet()
01759     {
01760         if ($this->_oDelSet == null) {
01761             // load deliveryset info
01762             $this->_oDelSet = oxNew('oxdeliveryset');
01763             $this->_oDelSet->load($this->oxorder__oxdeltype->value);
01764         }
01765 
01766         return $this->_oDelSet;
01767     }
01768 
01774     public function getPaymentType()
01775     {
01776         if ($this->oxorder__oxpaymentid->value && $this->_oPaymentType === null) {
01777             $this->_oPaymentType = false;
01778             $oPaymentType = oxNew('oxuserpayment');
01779             if ($oPaymentType->load($this->oxorder__oxpaymentid->value)) {
01780                 $this->_oPaymentType = $oPaymentType;
01781             }
01782         }
01783 
01784         return $this->_oPaymentType;
01785     }
01786 
01792     public function getGiftCard()
01793     {
01794         if ($this->oxorder__oxcardid->value && $this->_oGiftCard == null) {
01795             $this->_oGiftCard = oxNew('oxwrapping');
01796             $this->_oGiftCard->load($this->oxorder__oxcardid->value);
01797         }
01798 
01799         return $this->_oGiftCard;
01800     }
01801 
01807     public function setSeparateNumbering($blSeparateNumbering = null)
01808     {
01809         $this->_blSeparateNumbering = $blSeparateNumbering;
01810     }
01811 
01819     public function getLastUserPaymentType($sUserId)
01820     {
01821         $oDb = oxDb::getDb();
01822         $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="' . $this->getConfig()->getShopId() . '" and oxorder.oxuserid=' . $oDb->quote($sUserId) . ' order by oxorder.oxorderdate desc ';
01823         $sLastPaymentId = $oDb->getOne($sQ, false, false);
01824 
01825         return $sLastPaymentId;
01826     }
01827 
01834     protected function _addOrderArticlesToBasket($oBasket, $aOrderArticles)
01835     {
01836         // if no order articles, return empty basket
01837         if (count($aOrderArticles) > 0) {
01838 
01839             //adding order articles to basket
01840             foreach ($aOrderArticles as $oOrderArticle) {
01841                 $oBasket->addOrderArticleToBasket($oOrderArticle);
01842             }
01843         }
01844     }
01845 
01852     protected function _addArticlesToBasket($oBasket, $aArticles)
01853     {
01854         // if no order articles
01855         if (count($aArticles) > 0) {
01856 
01857             //adding order articles to basket
01858             foreach ($aArticles as $oArticle) {
01859                 $aSel = isset($oArticle->oxorderarticles__oxselvariant) ? $oArticle->oxorderarticles__oxselvariant->value : null;
01860                 $aPersParam = isset($oArticle->oxorderarticles__oxpersparam) ? $oArticle->getPersParams() : null;
01861                 $oBasket->addToBasket(
01862                     $oArticle->oxorderarticles__oxartid->value,
01863                     $oArticle->oxorderarticles__oxamount->value,
01864                     $aSel, $aPersParam
01865                 );
01866             }
01867         }
01868     }
01869 
01875     public function getTotalOrderSum()
01876     {
01877         $oCur = $this->getConfig()->getActShopCurrencyObject();
01878 
01879         return number_format((double) $this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01880     }
01881 
01889     public function getProductVats($blFormatCurrency = true)
01890     {
01891         $aVats = array();
01892         if ($this->oxorder__oxartvat1->value) {
01893             $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
01894         }
01895         if ($this->oxorder__oxartvat2->value) {
01896             $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
01897         }
01898 
01899         if ($blFormatCurrency) {
01900             $oLang = oxRegistry::getLang();
01901             $oCur = $this->getConfig()->getActShopCurrencyObject();
01902             foreach ($aVats as $sKey => $dVat) {
01903                 $aVats[$sKey] = $oLang->formatCurrency($dVat, $oCur);
01904             }
01905         }
01906 
01907         return $aVats;
01908     }
01909 
01915     public function getBillCountry()
01916     {
01917         if (!$this->oxorder__oxbillcountry->value) {
01918             $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle($this->oxorder__oxbillcountryid->value));
01919         }
01920 
01921         return $this->oxorder__oxbillcountry;
01922     }
01923 
01929     public function getDelCountry()
01930     {
01931         if (!$this->oxorder__oxdelcountry->value) {
01932             $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle($this->oxorder__oxdelcountryid->value));
01933         }
01934 
01935         return $this->oxorder__oxdelcountry;
01936     }
01937 
01943     public function reloadDelivery($blReload)
01944     {
01945         $this->_blReloadDelivery = $blReload;
01946     }
01947 
01953     public function reloadDiscount($blReload)
01954     {
01955         $this->_blReloadDiscount = $blReload;
01956     }
01957 
01961     public function cancelOrder()
01962     {
01963         $this->oxorder__oxstorno = new oxField(1);
01964         if ($this->save()) {
01965 
01966 
01967             // canceling ordered products
01968             foreach ($this->getOrderArticles() as $oOrderArticle) {
01969 
01970 
01971                 $oOrderArticle->cancelOrderArticle();
01972             }
01973         }
01974     }
01975 
01982     public function getOrderCurrency()
01983     {
01984         if ($this->_oOrderCurrency === null) {
01985 
01986             // setting default in case unrecognized currency was set during order
01987             $aCurrencies = $this->getConfig()->getCurrencyArray();
01988             $this->_oOrderCurrency = current($aCurrencies);
01989 
01990             foreach ($aCurrencies as $oCurr) {
01991                 if ($oCurr->name == $this->oxorder__oxcurrency->value) {
01992                     $this->_oOrderCurrency = $oCurr;
01993                     break;
01994                 }
01995             }
01996         }
01997 
01998         return $this->_oOrderCurrency;
01999     }
02000 
02010     public function validateOrder($oBasket, $oUser)
02011     {
02012         // validating stock
02013         $iValidState = $this->validateStock($oBasket);
02014 
02015         if (!$iValidState) {
02016             // validating delivery
02017             $iValidState = $this->validateDelivery($oBasket);
02018         }
02019 
02020         if (!$iValidState) {
02021             // validating payment
02022             $iValidState = $this->validatePayment($oBasket);
02023         }
02024 
02025         if (!$iValidState) {
02026             //0003110 validating delivery address, it is not be changed during checkout process
02027             $iValidState = $this->validateDeliveryAddress($oUser);
02028         }
02029 
02030         if (!$iValidState) {
02031             // validating minimum price
02032             $iValidState = $this->validateBasket($oBasket);
02033         }
02034 
02035         return $iValidState;
02036     }
02037 
02045     public function validateBasket($oBasket)
02046     {
02047         return $oBasket->isBelowMinOrderPrice() ? self::ORDER_STATE_BELOWMINPRICE : null;
02048     }
02049 
02058     public function validateDeliveryAddress($oUser)
02059     {
02060         $sDelAddressMD5 = $this->getConfig()->getRequestParameter('sDeliveryAddressMD5');
02061 
02062         $sDeliveryAddress = $oUser->getEncodedDeliveryAddress();
02063 
02065         $oRequiredAddressFields = oxNew('oxRequiredAddressFields');
02066 
02068         $oFieldsValidator = oxNew('oxRequiredFieldsValidator');
02069         $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getBillingFields());
02070         $blFieldsValid = $oFieldsValidator->validateFields($oUser);
02071 
02073         $oDeliveryAddress = $this->getDelAddressInfo();
02074         if ($blFieldsValid && $oDeliveryAddress) {
02075             $sDeliveryAddress .= $oDeliveryAddress->getEncodedDeliveryAddress();
02076 
02077             $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getDeliveryFields());
02078             $blFieldsValid = $oFieldsValidator->validateFields($oDeliveryAddress);
02079         }
02080 
02081         $iState = 0;
02082         if ($sDelAddressMD5 != $sDeliveryAddress || !$blFieldsValid) {
02083             $iState = self::ORDER_STATE_INVALIDDElADDRESSCHANGED;
02084         }
02085 
02086         return $iState;
02087     }
02088 
02089 
02098     public function validateDelivery($oBasket)
02099     {
02100         // proceed with no delivery
02101         // used for other countries
02102         if ($oBasket->getPaymentId() == 'oxempty') {
02103             return;
02104         }
02105         $oDb = oxDb::getDb();
02106 
02107         $oDelSet = oxNew("oxdeliveryset");
02108         $sTable = $oDelSet->getViewName();
02109 
02110         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=" .
02111               $oDb->quote($oBasket->getShippingId()) . " and " . $oDelSet->getSqlActiveSnippet();
02112 
02113         if (!$oDb->getOne($sQ, false, false)) {
02114             // throwing exception
02115             return self::ORDER_STATE_INVALIDDELIVERY;
02116         }
02117     }
02118 
02127     public function validatePayment($oBasket)
02128     {
02129         $oDb = oxDb::getDb();
02130 
02131         $oPayment = oxNew("oxpayment");
02132         $sTable = $oPayment->getViewName();
02133 
02134         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=" .
02135               $oDb->quote($oBasket->getPaymentId()) . " and " . $oPayment->getSqlActiveSnippet();
02136 
02137         if (!$oDb->getOne($sQ, false, false)) {
02138             return self::ORDER_STATE_INVALIDPAYMENT;
02139         }
02140     }
02141 
02147     protected function _setTsProtection(oxBasket $oBasket)
02148     {
02149         // protection price
02150         if (($oTsProtectionCost = $oBasket->getCosts('oxtsprotection'))) {
02151             $this->oxorder__oxtsprotectcosts = new oxField($oTsProtectionCost->getBruttoPrice(), oxField::T_RAW);
02152         }
02153 
02154         // protection protduct id
02155         $this->oxorder__oxtsprotectid = new oxField($oBasket->getTsProductId(), oxField::T_RAW);
02156     }
02157 
02166     protected function _executeTsProtection(oxBasket $oBasket)
02167     {
02168         $aValues['tsProductId'] = $this->oxorder__oxtsprotectid->value;
02169         $aValues['amount'] = $oBasket->getTsInsuredSum();
02170         $oCur = $this->getConfig()->getActShopCurrencyObject();
02171         $aValues['currency'] = $oCur->name;
02172         $aValues['buyerEmail'] = $this->oxorder__oxbillemail->value;
02173         $aValues['shopCustomerID'] = $this->oxorder__oxuserid->value;
02174         $aValues['shopOrderID'] = $this->oxorder__oxordernr->value;
02175         $aValues['orderDate'] = $this->oxorder__oxorderdate->value;
02176         $sPaymentId = $oBasket->getPaymentId();
02177         $oTsProtection = oxNew('oxtsprotection');
02178         $blRes = $oTsProtection->requestForTsProtection($aValues, $sPaymentId);
02179 
02180         /*if ( !$blRes ) {
02181             $this->delete();
02182             return self::ORDER_STATE_INVALIDTSPROTECTION;
02183         }*/
02184 
02185         return true; // everything fine
02186     }
02187 
02193     public function getFormattedTotalNetSum()
02194     {
02195         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalnetsum->value, $this->getOrderCurrency());
02196     }
02197 
02203     public function getFormattedTotalBrutSum()
02204     {
02205         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalbrutsum->value, $this->getOrderCurrency());
02206     }
02207 
02213     public function getFormattedDeliveryCost()
02214     {
02215         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxdelcost->value, $this->getOrderCurrency());
02216     }
02217 
02225     public function getFormattedeliveryCost()
02226     {
02227         return $this->getFormattedDeliveryCost();
02228     }
02229 
02235     public function getFormattedPayCost()
02236     {
02237         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxpaycost->value, $this->getOrderCurrency());
02238     }
02239 
02245     public function getFormattedWrapCost()
02246     {
02247         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxwrapcost->value, $this->getOrderCurrency());
02248     }
02249 
02255     public function getFormattedGiftCardCost()
02256     {
02257         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxgiftcardcost->value, $this->getOrderCurrency());
02258     }
02259 
02265     public function getFormattedTotalVouchers()
02266     {
02267         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxvoucherdiscount->value, $this->getOrderCurrency());
02268     }
02269 
02275     public function getFormattedDiscount()
02276     {
02277         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxdiscount->value, $this->getOrderCurrency());
02278     }
02279 
02285     public function getFormattedTotalOrderSum()
02286     {
02287         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalordersum->value, $this->getOrderCurrency());
02288     }
02289 
02295     public function getTrackCode()
02296     {
02297         return $this->oxorder__oxtrackcode->value;
02298     }
02299 
02305     public function getShipmentTrackingUrl()
02306     {
02307         $oConfig = oxRegistry::getConfig();
02308         if ($this->_sShipTrackUrl === null) {
02309             $sParcelService = $oConfig->getConfigParam('sParcelService');
02310             $sTrackingCode = $this->getTrackCode();
02311             if ($sParcelService && $sTrackingCode) {
02312                 $this->_sShipTrackUrl = str_replace("##ID##", $sTrackingCode, $sParcelService);
02313             }
02314         }
02315 
02316         return $this->_sShipTrackUrl;
02317     }
02318 }