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         if (!$this->oxorder__oxordernr->value) {
00536             $this->_setNumber();
00537         } else {
00538             oxNew('oxCounter')->update($this->_getCounterIdent(), $this->oxorder__oxordernr->value);
00539         }
00540 
00541         // executing TS protection
00542         if (!$blRecalculatingOrder && $oBasket->getTsProductId()) {
00543             $blRet = $this->_executeTsProtection($oBasket);
00544             if ($blRet !== true) {
00545                 return $blRet;
00546             }
00547         }
00548 
00549         // deleting remark info only when order is finished
00550         oxRegistry::getSession()->deleteVariable('ordrem');
00551         oxRegistry::getSession()->deleteVariable('stsprotection');
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, null, 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                 $oEx->setBasketIndex($key);
01227 
01228                 if (!is_numeric($iOnStock)) {
01229                     $iOnStock = 0;
01230                 }
01231                 $oEx->setRemainingAmount($iOnStock);
01232                 throw $oEx;
01233             }
01234         }
01235     }
01236 
01242     protected function _insert()
01243     {
01244         $myConfig = $this->getConfig();
01245         $oUtilsDate = oxRegistry::get("oxUtilsDate");
01246 
01247         //V #M525 orderdate must be the same as it was
01248         if (!$this->oxorder__oxorderdate->value) {
01249             $this->oxorder__oxorderdate = new oxField(date('Y-m-d H:i:s', $oUtilsDate->getTime()), oxField::T_RAW);
01250         } else {
01251             $this->oxorder__oxorderdate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxorderdate->value, true));
01252         }
01253 
01254         $this->oxorder__oxshopid = new oxField($myConfig->getShopId(), oxField::T_RAW);
01255         $this->oxorder__oxsenddate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxsenddate->value, true));
01256 
01257         $blInsert = parent::_insert();
01258 
01259         return $blInsert;
01260     }
01261 
01267     protected function _getCounterIdent()
01268     {
01269         $sCounterIdent = ($this->_blSeparateNumbering) ? 'oxOrder_' . $this->getConfig()->getShopId() : 'oxOrder';
01270 
01271         return $sCounterIdent;
01272     }
01273 
01274 
01280     protected function _setNumber()
01281     {
01282         $oDb = oxDb::getDb();
01283 
01284         $iCnt = oxNew('oxCounter')->getNext($this->_getCounterIdent());
01285         $sQ = "update oxorder set oxordernr = ? where oxid = ?";
01286         $blUpdate = ( bool ) $oDb->execute($sQ, array($iCnt, $this->getId()));
01287 
01288         if ($blUpdate) {
01289             $this->oxorder__oxordernr = new oxField($iCnt);
01290         }
01291 
01292         return $blUpdate;
01293     }
01294 
01300     protected function _update()
01301     {
01302         $this->_aSkipSaveFields = array('oxtimestamp', 'oxorderdate');
01303         $this->oxorder__oxsenddate = new oxField(oxRegistry::get("oxUtilsDate")->formatDBDate($this->oxorder__oxsenddate->value, true));
01304 
01305         return parent::_update();
01306     }
01307 
01316     public function delete($sOxId = null)
01317     {
01318         if ($sOxId) {
01319             if (!$this->load($sOxId)) {
01320                 // such order does not exist
01321                 return false;
01322             }
01323         } elseif (!$sOxId) {
01324             $sOxId = $this->getId();
01325         }
01326 
01327         // no order id is passed
01328         if (!$sOxId) {
01329             return false;
01330         }
01331 
01332 
01333         // delete order articles
01334         $oOrderArticles = $this->getOrderArticles(false);
01335         foreach ($oOrderArticles as $oOrderArticle) {
01336             $oOrderArticle->delete();
01337         }
01338 
01339         // #440 - deleting user payment info
01340         if ($oPaymentType = $this->getPaymentType()) {
01341             $oPaymentType->delete();
01342         }
01343 
01344         return parent::delete($sOxId);
01345     }
01346 
01356     public function recalculateOrder($aNewArticles = array())
01357     {
01358         oxDb::getDb()->startTransaction();
01359 
01360         try {
01361             $oBasket = $this->_getOrderBasket();
01362 
01363             // add this order articles to virtual basket and recalculates basket
01364             $this->_addOrderArticlesToBasket($oBasket, $this->getOrderArticles(true));
01365 
01366             // adding new articles to existing order
01367             $this->_addArticlesToBasket($oBasket, $aNewArticles);
01368 
01369             // recalculating basket
01370             $oBasket->calculateBasket(true);
01371 
01372             //finalizing order (skipping payment execution, vouchers marking and mail sending)
01373             $iRet = $this->finalizeOrder($oBasket, $this->getOrderUser(), true);
01374 
01375             //if finalizing order failed, rollback transaction
01376             if ($iRet !== 1) {
01377                 oxDb::getDb()->rollbackTransaction();
01378             } else {
01379                 oxDb::getDb()->commitTransaction();
01380             }
01381 
01382         } catch (Exception $oE) {
01383             // if exception, rollBack everything
01384             oxDb::getDb()->rollbackTransaction();
01385 
01386             if (defined('OXID_PHP_UNIT')) {
01387                 throw $oE;
01388             }
01389         }
01390     }
01391 
01392     protected $_oOrderBasket = null;
01393 
01401     protected function _getOrderBasket($blStockCheck = true)
01402     {
01403         $this->_oOrderBasket = oxNew("oxBasket");
01404         $this->_oOrderBasket->enableSaveToDataBase(false);
01405 
01406         //setting recalculation mode
01407         $this->_oOrderBasket->setCalculationModeNetto($this->isNettoMode());
01408 
01409         // setting stock check mode
01410         $this->_oOrderBasket->setStockCheckMode($blStockCheck);
01411 
01412         // setting virtual basket user
01413         $this->_oOrderBasket->setBasketUser($this->getOrderUser());
01414 
01415         // transferring order id
01416         $this->_oOrderBasket->setOrderId($this->getId());
01417 
01418         // setting basket currency order uses
01419         $aCurrencies = $this->getConfig()->getCurrencyArray();
01420         foreach ($aCurrencies as $oCur) {
01421             if ($oCur->name == $this->oxorder__oxcurrency->value) {
01422                 $oBasketCur = $oCur;
01423                 break;
01424             }
01425         }
01426 
01427         // setting currency
01428         $this->_oOrderBasket->setBasketCurrency($oBasketCur);
01429 
01430         // set basket card id and message
01431         $this->_oOrderBasket->setCardId($this->oxorder__oxcardid->value);
01432         $this->_oOrderBasket->setCardMessage($this->oxorder__oxcardtext->value);
01433 
01434         if ($this->_blReloadDiscount) {
01435             $oDb = oxDb::getDb(oxDb::FETCH_MODE_ASSOC);
01436             // disabling availability check
01437             $this->_oOrderBasket->setSkipVouchersChecking(true);
01438 
01439             // add previously used vouchers
01440             $sQ = 'select oxid from oxvouchers where oxorderid = ' . $oDb->quote($this->getId());
01441             $aVouchers = $oDb->getAll($sQ);
01442             foreach ($aVouchers as $aVoucher) {
01443                 $this->_oOrderBasket->addVoucher($aVoucher['oxid']);
01444             }
01445         } else {
01446             $this->_oOrderBasket->setDiscountCalcMode(false);
01447             $this->_oOrderBasket->setVoucherDiscount($this->oxorder__oxvoucherdiscount->value);
01448             $this->_oOrderBasket->setTotalDiscount($this->oxorder__oxdiscount->value);
01449         }
01450 
01451         // must be kept old delivery?
01452         if (!$this->_blReloadDelivery) {
01453             $this->_oOrderBasket->setDeliveryPrice($this->getOrderDeliveryPrice());
01454         } else {
01455             //  set shipping
01456             $this->_oOrderBasket->setShipping($this->oxorder__oxdeltype->value);
01457             $this->_oOrderBasket->setDeliveryPrice(null);
01458         }
01459 
01460         //set basket payment
01461         $this->_oOrderBasket->setPayment($this->oxorder__oxpaymenttype->value);
01462 
01463         return $this->_oOrderBasket;
01464     }
01465 
01472     public function setDelivery($sDeliveryId)
01473     {
01474         $this->reloadDelivery(true);
01475         $this->oxorder__oxdeltype = new oxField($sDeliveryId);
01476     }
01477 
01483     public function getOrderUser()
01484     {
01485         if ($this->_oUser === null) {
01486             $this->_oUser = oxNew("oxuser");
01487             $this->_oUser->load($this->oxorder__oxuserid->value);
01488 
01489             // if object is loaded then reusing its order info
01490             if ($this->_isLoaded) {
01491                 // bill address
01492                 $this->_oUser->oxuser__oxcompany = clone $this->oxorder__oxbillcompany;
01493                 $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
01494                 $this->_oUser->oxuser__oxfname = clone $this->oxorder__oxbillfname;
01495                 $this->_oUser->oxuser__oxlname = clone $this->oxorder__oxbilllname;
01496                 $this->_oUser->oxuser__oxstreet = clone $this->oxorder__oxbillstreet;
01497                 $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
01498                 $this->_oUser->oxuser__oxaddinfo = clone $this->oxorder__oxbilladdinfo;
01499                 $this->_oUser->oxuser__oxustid = clone $this->oxorder__oxbillustid;
01500 
01501 
01502                 $this->_oUser->oxuser__oxcity = clone $this->oxorder__oxbillcity;
01503                 $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
01504                 $this->_oUser->oxuser__oxstateid = clone $this->oxorder__oxbillstateid;
01505                 $this->_oUser->oxuser__oxzip = clone $this->oxorder__oxbillzip;
01506                 $this->_oUser->oxuser__oxfon = clone $this->oxorder__oxbillfon;
01507                 $this->_oUser->oxuser__oxfax = clone $this->oxorder__oxbillfax;
01508                 $this->_oUser->oxuser__oxsal = clone $this->oxorder__oxbillsal;
01509             }
01510         }
01511 
01512         return $this->_oUser;
01513     }
01514 
01520     public function pdfFooter($oPdf)
01521     {
01522     }
01523 
01529     public function pdfHeaderplus($oPdf)
01530     {
01531     }
01532 
01538     public function pdfHeader($oPdf)
01539     {
01540     }
01541 
01548     public function genPdf($sFilename, $iSelLang = 0)
01549     {
01550     }
01551 
01557     public function getInvoiceNum()
01558     {
01559         $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "' . $this->getConfig()->getShopId() . '" ';
01560 
01561         return (( int ) oxDb::getDb()->getOne($sQ, false) + 1);
01562     }
01563 
01569     public function getNextBillNum()
01570     {
01571         $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "' . $this->getConfig()->getShopId() . '" ';
01572 
01573         return (( int ) oxDb::getDb()->getOne($sQ, false) + 1);
01574     }
01575 
01581     public function getShippingSetList()
01582     {
01583         // in which country we deliver
01584         if (!($sShipId = $this->oxorder__oxdelcountryid->value)) {
01585             $sShipId = $this->oxorder__oxbillcountryid->value;
01586         }
01587 
01588         $oBasket = $this->_getOrderBasket(false);
01589 
01590         // unsetting bundles
01591         $oOrderArticles = $this->getOrderArticles();
01592         foreach ($oOrderArticles as $sItemId => $oItem) {
01593             if ($oItem->isBundle()) {
01594                 $oOrderArticles->offsetUnset($sItemId);
01595             }
01596         }
01597 
01598         // add this order articles to basket and recalculate basket
01599         $this->_addOrderArticlesToBasket($oBasket, $oOrderArticles);
01600 
01601         // recalculating basket
01602         $oBasket->calculateBasket(true);
01603 
01604         // load fitting deliveries list
01605         $oDeliveryList = oxNew("oxDeliveryList", "core");
01606         $oDeliveryList->setCollectFittingDeliveriesSets(true);
01607 
01608         return $oDeliveryList->getDeliveryList($oBasket, $this->getOrderUser(), $sShipId);
01609     }
01610 
01616     public function getVoucherNrList()
01617     {
01618         $oDb = oxDb::getDb(oxDb::FETCH_MODE_ASSOC);
01619         $aVouchers = array();
01620         $sSelect = "select oxvouchernr from oxvouchers where oxorderid = " . $oDb->quote($this->oxorder__oxid->value);
01621         $rs = $oDb->select($sSelect);
01622         if ($rs != false && $rs->recordCount() > 0) {
01623             while (!$rs->EOF) {
01624                 $aVouchers[] = $rs->fields['oxvouchernr'];
01625                 $rs->moveNext();
01626             }
01627         }
01628 
01629         return $aVouchers;
01630     }
01631 
01639     public function getOrderSum($blToday = false)
01640     {
01641         $sSelect = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01642         $sSelect .= 'oxshopid = "' . $this->getConfig()->getShopId() . '" and oxorder.oxstorno != "1" ';
01643 
01644         if ($blToday) {
01645             $sSelect .= 'and oxorderdate like "' . date('Y-m-d') . '%" ';
01646         }
01647 
01648         return ( double ) oxDb::getDb()->getOne($sSelect, false, false);
01649     }
01650 
01658     public function getOrderCnt($blToday = false)
01659     {
01660         $sSelect = 'select count(*) from oxorder where ';
01661         $sSelect .= 'oxshopid = "' . $this->getConfig()->getShopId() . '"  and oxorder.oxstorno != "1" ';
01662 
01663         if ($blToday) {
01664             $sSelect .= 'and oxorderdate like "' . date('Y-m-d') . '%" ';
01665         }
01666 
01667         return ( int ) oxDb::getDb()->getOne($sSelect, false, false);
01668     }
01669 
01670 
01678     protected function _checkOrderExist($sOxId = null)
01679     {
01680         if (!$sOxId) {
01681             return false;
01682         }
01683 
01684         $oDb = oxDb::getDb();
01685         if ($oDb->getOne('select oxid from oxorder where oxid = ' . $oDb->quote($sOxId), false, false)) {
01686             return true;
01687         }
01688 
01689         return false;
01690     }
01691 
01701     protected function _sendOrderByEmail($oUser = null, $oBasket = null, $oPayment = null)
01702     {
01703         $iRet = self::ORDER_STATE_MAILINGERROR;
01704 
01705         // add user, basket and payment to order
01706         $this->_oUser = $oUser;
01707         $this->_oBasket = $oBasket;
01708         $this->_oPayment = $oPayment;
01709 
01710         $oxEmail = oxNew('oxemail');
01711 
01712         // send order email to user
01713         if ($oxEmail->sendOrderEMailToUser($this)) {
01714             // mail to user was successfully sent
01715             $iRet = self::ORDER_STATE_OK;
01716         }
01717 
01718         // send order email to shop owner
01719         $oxEmail->sendOrderEMailToOwner($this);
01720 
01721         return $iRet;
01722     }
01723 
01729     public function getBasket()
01730     {
01731         return $this->_oBasket;
01732     }
01733 
01739     public function getPayment()
01740     {
01741         return $this->_oPayment;
01742     }
01743 
01749     public function getVoucherList()
01750     {
01751         return $this->_aVoucherList;
01752     }
01753 
01759     public function getDelSet()
01760     {
01761         if ($this->_oDelSet == null) {
01762             // load deliveryset info
01763             $this->_oDelSet = oxNew('oxdeliveryset');
01764             $this->_oDelSet->load($this->oxorder__oxdeltype->value);
01765         }
01766 
01767         return $this->_oDelSet;
01768     }
01769 
01775     public function getPaymentType()
01776     {
01777         if ($this->oxorder__oxpaymentid->value && $this->_oPaymentType === null) {
01778             $this->_oPaymentType = false;
01779             $oPaymentType = oxNew('oxuserpayment');
01780             if ($oPaymentType->load($this->oxorder__oxpaymentid->value)) {
01781                 $this->_oPaymentType = $oPaymentType;
01782             }
01783         }
01784 
01785         return $this->_oPaymentType;
01786     }
01787 
01793     public function getGiftCard()
01794     {
01795         if ($this->oxorder__oxcardid->value && $this->_oGiftCard == null) {
01796             $this->_oGiftCard = oxNew('oxwrapping');
01797             $this->_oGiftCard->load($this->oxorder__oxcardid->value);
01798         }
01799 
01800         return $this->_oGiftCard;
01801     }
01802 
01808     public function setSeparateNumbering($blSeparateNumbering = null)
01809     {
01810         $this->_blSeparateNumbering = $blSeparateNumbering;
01811     }
01812 
01820     public function getLastUserPaymentType($sUserId)
01821     {
01822         $oDb = oxDb::getDb();
01823         $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="' . $this->getConfig()->getShopId() . '" and oxorder.oxuserid=' . $oDb->quote($sUserId) . ' order by oxorder.oxorderdate desc ';
01824         $sLastPaymentId = $oDb->getOne($sQ, false, false);
01825 
01826         return $sLastPaymentId;
01827     }
01828 
01835     protected function _addOrderArticlesToBasket($oBasket, $aOrderArticles)
01836     {
01837         // if no order articles, return empty basket
01838         if (count($aOrderArticles) > 0) {
01839 
01840             //adding order articles to basket
01841             foreach ($aOrderArticles as $oOrderArticle) {
01842                 $oBasket->addOrderArticleToBasket($oOrderArticle);
01843             }
01844         }
01845     }
01846 
01853     protected function _addArticlesToBasket($oBasket, $aArticles)
01854     {
01855         // if no order articles
01856         if (count($aArticles) > 0) {
01857 
01858             //adding order articles to basket
01859             foreach ($aArticles as $oArticle) {
01860                 $aSel = isset($oArticle->oxorderarticles__oxselvariant) ? $oArticle->oxorderarticles__oxselvariant->value : null;
01861                 $aPersParam = isset($oArticle->oxorderarticles__oxpersparam) ? $oArticle->getPersParams() : null;
01862                 $oBasket->addToBasket(
01863                     $oArticle->oxorderarticles__oxartid->value,
01864                     $oArticle->oxorderarticles__oxamount->value,
01865                     $aSel, $aPersParam
01866                 );
01867             }
01868         }
01869     }
01870 
01876     public function getTotalOrderSum()
01877     {
01878         $oCur = $this->getConfig()->getActShopCurrencyObject();
01879 
01880         return number_format((double) $this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01881     }
01882 
01890     public function getProductVats($blFormatCurrency = true)
01891     {
01892         $aVats = array();
01893         if ($this->oxorder__oxartvat1->value) {
01894             $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
01895         }
01896         if ($this->oxorder__oxartvat2->value) {
01897             $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
01898         }
01899 
01900         if ($blFormatCurrency) {
01901             $oLang = oxRegistry::getLang();
01902             $oCur = $this->getConfig()->getActShopCurrencyObject();
01903             foreach ($aVats as $sKey => $dVat) {
01904                 $aVats[$sKey] = $oLang->formatCurrency($dVat, $oCur);
01905             }
01906         }
01907 
01908         return $aVats;
01909     }
01910 
01916     public function getBillCountry()
01917     {
01918         if (!$this->oxorder__oxbillcountry->value) {
01919             $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle($this->oxorder__oxbillcountryid->value));
01920         }
01921 
01922         return $this->oxorder__oxbillcountry;
01923     }
01924 
01930     public function getDelCountry()
01931     {
01932         if (!$this->oxorder__oxdelcountry->value) {
01933             $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle($this->oxorder__oxdelcountryid->value));
01934         }
01935 
01936         return $this->oxorder__oxdelcountry;
01937     }
01938 
01944     public function reloadDelivery($blReload)
01945     {
01946         $this->_blReloadDelivery = $blReload;
01947     }
01948 
01954     public function reloadDiscount($blReload)
01955     {
01956         $this->_blReloadDiscount = $blReload;
01957     }
01958 
01962     public function cancelOrder()
01963     {
01964         $this->oxorder__oxstorno = new oxField(1);
01965         if ($this->save()) {
01966 
01967 
01968             // canceling ordered products
01969             foreach ($this->getOrderArticles() as $oOrderArticle) {
01970 
01971 
01972                 $oOrderArticle->cancelOrderArticle();
01973             }
01974         }
01975     }
01976 
01983     public function getOrderCurrency()
01984     {
01985         if ($this->_oOrderCurrency === null) {
01986 
01987             // setting default in case unrecognized currency was set during order
01988             $aCurrencies = $this->getConfig()->getCurrencyArray();
01989             $this->_oOrderCurrency = current($aCurrencies);
01990 
01991             foreach ($aCurrencies as $oCurr) {
01992                 if ($oCurr->name == $this->oxorder__oxcurrency->value) {
01993                     $this->_oOrderCurrency = $oCurr;
01994                     break;
01995                 }
01996             }
01997         }
01998 
01999         return $this->_oOrderCurrency;
02000     }
02001 
02011     public function validateOrder($oBasket, $oUser)
02012     {
02013         // validating stock
02014         $iValidState = $this->validateStock($oBasket);
02015 
02016         if (!$iValidState) {
02017             // validating delivery
02018             $iValidState = $this->validateDelivery($oBasket);
02019         }
02020 
02021         if (!$iValidState) {
02022             // validating payment
02023             $iValidState = $this->validatePayment($oBasket);
02024         }
02025 
02026         if (!$iValidState) {
02027             //0003110 validating delivery address, it is not be changed during checkout process
02028             $iValidState = $this->validateDeliveryAddress($oUser);
02029         }
02030 
02031         if (!$iValidState) {
02032             // validating minimum price
02033             $iValidState = $this->validateBasket($oBasket);
02034         }
02035 
02036         return $iValidState;
02037     }
02038 
02046     public function validateBasket($oBasket)
02047     {
02048         return $oBasket->isBelowMinOrderPrice() ? self::ORDER_STATE_BELOWMINPRICE : null;
02049     }
02050 
02059     public function validateDeliveryAddress($oUser)
02060     {
02061         $sDelAddressMD5 = $this->getConfig()->getRequestParameter('sDeliveryAddressMD5');
02062 
02063         $sDeliveryAddress = $oUser->getEncodedDeliveryAddress();
02064 
02066         $oRequiredAddressFields = oxNew('oxRequiredAddressFields');
02067 
02069         $oFieldsValidator = oxNew('oxRequiredFieldsValidator');
02070         $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getBillingFields());
02071         $blFieldsValid = $oFieldsValidator->validateFields($oUser);
02072 
02074         $oDeliveryAddress = $this->getDelAddressInfo();
02075         if ($blFieldsValid && $oDeliveryAddress) {
02076             $sDeliveryAddress .= $oDeliveryAddress->getEncodedDeliveryAddress();
02077 
02078             $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getDeliveryFields());
02079             $blFieldsValid = $oFieldsValidator->validateFields($oDeliveryAddress);
02080         }
02081 
02082         $iState = 0;
02083         if ($sDelAddressMD5 != $sDeliveryAddress || !$blFieldsValid) {
02084             $iState = self::ORDER_STATE_INVALIDDElADDRESSCHANGED;
02085         }
02086 
02087         return $iState;
02088     }
02089 
02090 
02099     public function validateDelivery($oBasket)
02100     {
02101         // proceed with no delivery
02102         // used for other countries
02103         if ($oBasket->getPaymentId() == 'oxempty') {
02104             return;
02105         }
02106         $oDb = oxDb::getDb();
02107 
02108         $oDelSet = oxNew("oxdeliveryset");
02109         $sTable = $oDelSet->getViewName();
02110 
02111         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=" .
02112               $oDb->quote($oBasket->getShippingId()) . " and " . $oDelSet->getSqlActiveSnippet();
02113 
02114         if (!$oDb->getOne($sQ, false, false)) {
02115             // throwing exception
02116             return self::ORDER_STATE_INVALIDDELIVERY;
02117         }
02118     }
02119 
02128     public function validatePayment($oBasket)
02129     {
02130         $oDb = oxDb::getDb();
02131 
02132         $oPayment = oxNew("oxpayment");
02133         $sTable = $oPayment->getViewName();
02134 
02135         $sQ = "select 1 from {$sTable} where {$sTable}.oxid=" .
02136               $oDb->quote($oBasket->getPaymentId()) . " and " . $oPayment->getSqlActiveSnippet();
02137 
02138         if (!$oDb->getOne($sQ, false, false)) {
02139             return self::ORDER_STATE_INVALIDPAYMENT;
02140         }
02141     }
02142 
02148     protected function _setTsProtection(oxBasket $oBasket)
02149     {
02150         // protection price
02151         if (($oTsProtectionCost = $oBasket->getCosts('oxtsprotection'))) {
02152             $this->oxorder__oxtsprotectcosts = new oxField($oTsProtectionCost->getBruttoPrice(), oxField::T_RAW);
02153         }
02154 
02155         // protection protduct id
02156         $this->oxorder__oxtsprotectid = new oxField($oBasket->getTsProductId(), oxField::T_RAW);
02157     }
02158 
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 
02179         $oTsProtection = oxNew('oxtsprotection');
02180         $oTsProtection->requestForTsProtection($aValues, $sPaymentId);
02181 
02182         return true;
02183     }
02184 
02190     public function getFormattedTotalNetSum()
02191     {
02192         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalnetsum->value, $this->getOrderCurrency());
02193     }
02194 
02200     public function getFormattedTotalBrutSum()
02201     {
02202         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalbrutsum->value, $this->getOrderCurrency());
02203     }
02204 
02210     public function getFormattedDeliveryCost()
02211     {
02212         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxdelcost->value, $this->getOrderCurrency());
02213     }
02214 
02222     public function getFormattedeliveryCost()
02223     {
02224         return $this->getFormattedDeliveryCost();
02225     }
02226 
02232     public function getFormattedPayCost()
02233     {
02234         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxpaycost->value, $this->getOrderCurrency());
02235     }
02236 
02242     public function getFormattedWrapCost()
02243     {
02244         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxwrapcost->value, $this->getOrderCurrency());
02245     }
02246 
02252     public function getFormattedGiftCardCost()
02253     {
02254         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxgiftcardcost->value, $this->getOrderCurrency());
02255     }
02256 
02262     public function getFormattedTotalVouchers()
02263     {
02264         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxvoucherdiscount->value, $this->getOrderCurrency());
02265     }
02266 
02272     public function getFormattedDiscount()
02273     {
02274         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxdiscount->value, $this->getOrderCurrency());
02275     }
02276 
02282     public function getFormattedTotalOrderSum()
02283     {
02284         return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalordersum->value, $this->getOrderCurrency());
02285     }
02286 
02292     public function getTrackCode()
02293     {
02294         return $this->oxorder__oxtrackcode->value;
02295     }
02296 
02302     public function getShipmentTrackingUrl()
02303     {
02304         $oConfig = oxRegistry::getConfig();
02305         if ($this->_sShipTrackUrl === null) {
02306             $sParcelService = $oConfig->getConfigParam('sParcelService');
02307             $sTrackingCode = $this->getTrackCode();
02308             if ($sParcelService && $sTrackingCode) {
02309                 $this->_sShipTrackUrl = str_replace("##ID##", $sTrackingCode, $sParcelService);
02310             }
02311         }
02312 
02313         return $this->_sShipTrackUrl;
02314     }
02315 }