OXID eShop CE  4.10.8
 All Classes Namespaces Files Functions Variables Pages
oxorder.php
Go to the documentation of this file.
1 <?php
2 
8 class oxOrder extends oxBase
9 {
10 
11  // defining order state constants
18 
24  const ORDER_STATE_OK = 1;
25 
32 
39 
46 
53 
62 
69 
76 
82  protected $_aSkipSaveFields = array('oxtimestamp');
83 
89  protected $_oArticles = null;
90 
96  protected $_oDelSet = null;
97 
103  protected $_oGiftCard = null;
104 
110  protected $_oPaymentType = null;
111 
117  protected $_oPayment = null;
118 
124  protected $_aVoucherList = null;
125 
131  protected $_oDelPrice = null;
132 
138  protected $_oUser = null;
139 
145  protected $_oBasket = null;
146 
152  protected $_oWrappingPrice = null;
153 
159  protected $_oGiftCardPrice = null;
160 
166  protected $_oPaymentPrice = null;
167 
175  protected $_oTsProtectionPrice = null;
176 
182  protected $_sClassName = 'oxorder';
183 
189  protected $_blSeparateNumbering = null;
190 
196  protected $_iOrderLang = null;
197 
203  protected $_blReloadDelivery = true;
204 
210  protected $_blReloadDiscount = true;
211 
217  protected $_oOrderCurrency = null;
218 
224  protected $_oOrderFiles = null;
225 
231  protected $_sShipTrackUrl = null;
232 
236  public function __construct()
237  {
239  $this->init('oxorder');
240 
241  // set usage of separate orders numbering for different shops
242  $this->setSeparateNumbering($this->getConfig()->getConfigParam('blSeparateNumbering'));
243 
244  }
245 
253  public function __get($sName)
254  {
255  if ($sName == 'oDelSet') {
256  return $this->getDelSet();
257  }
258 
259  if ($sName == 'oxorder__oxbillcountry') {
260  return $this->getBillCountry();
261  }
262 
263  if ($sName == 'oxorder__oxdelcountry') {
264  return $this->getDelCountry();
265  }
266  }
267 
275  public function assign($dbRecord)
276  {
277 
278  parent::assign($dbRecord);
279 
280  $oUtilsDate = oxRegistry::get("oxUtilsDate");
281 
282  // convert date's to international format
283  $this->oxorder__oxorderdate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxorderdate->value));
284  $this->oxorder__oxsenddate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxsenddate->value));
285  }
286 
294  protected function _getCountryTitle($sCountryId)
295  {
296  $sTitle = null;
297  if ($sCountryId && $sCountryId != '-1') {
298  $oCountry = oxNew('oxcountry');
299  $oCountry->loadInLang($this->getOrderLanguage(), $sCountryId);
300  $sTitle = $oCountry->oxcountry__oxtitle->value;
301  }
302 
303  return $sTitle;
304  }
305 
313  protected function _getArticles($blExcludeCanceled = false)
314  {
315  $sSelect = "SELECT `oxorderarticles`.* FROM `oxorderarticles`
316  WHERE `oxorderarticles`.`oxorderid` = '" . $this->getId() . "'" .
317  ($blExcludeCanceled ? " AND `oxorderarticles`.`oxstorno` != 1 " : " ") . "
318  ORDER BY `oxorderarticles`.`oxartid`, `oxorderarticles`.`oxselvariant`, `oxorderarticles`.`oxpersparam` ";
319 
320  // order articles
321  $oArticles = oxNew('oxlist');
322  $oArticles->init('oxorderarticle');
323  $oArticles->selectString($sSelect);
324 
325  return $oArticles;
326  }
327 
335  public function getOrderArticles($blExcludeCanceled = false)
336  {
337  // checking set value
338  if ($blExcludeCanceled) {
339 
340  return $this->_getArticles(true);
341 
342  } elseif ($this->_oArticles === null) {
343  $this->_oArticles = $this->_getArticles();
344  }
345 
346  return $this->_oArticles;
347  }
348 
354  public function setOrderArticleList($oOrderArticleList)
355  {
356  $this->_oArticles = $oOrderArticleList;
357  }
358 
364  public function getOrderDeliveryPrice()
365  {
366  if ($this->_oDelPrice != null) {
367  return $this->_oDelPrice;
368  }
369 
370  $this->_oDelPrice = oxNew('oxprice');
371  $this->_oDelPrice->setBruttoPriceMode();
372  $this->_oDelPrice->setPrice($this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value);
373 
374  return $this->_oDelPrice;
375  }
376 
382  public function getOrderWrappingPrice()
383  {
384  if ($this->_oWrappingPrice != null) {
385  return $this->_oWrappingPrice;
386  }
387 
388  $this->_oWrappingPrice = oxNew('oxprice');
389  $this->_oWrappingPrice->setBruttoPriceMode();
390  $this->_oWrappingPrice->setPrice($this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value);
391 
392  return $this->_oWrappingPrice;
393  }
394 
400  public function getOrderGiftCardPrice()
401  {
402  if ($this->_oGidtCardPrice != null) {
403  return $this->_oGidtCardPrice;
404  }
405 
406  $this->_oGidtCardPrice = oxNew('oxprice');
407  $this->_oGidtCardPrice->setBruttoPriceMode();
408  $this->_oGidtCardPrice->setPrice($this->oxorder__oxgiftcardcost->value, $this->oxorder__oxgiftcardvat->value);
409 
410  return $this->_oGidtCardPrice;
411  }
412 
413 
419  public function getOrderPaymentPrice()
420  {
421  if ($this->_oPaymentPrice != null) {
422  return $this->_oPaymentPrice;
423  }
424 
425  $this->_oPaymentPrice = oxNew('oxprice');
426  $this->_oPaymentPrice->setBruttoPriceMode();
427  $this->_oPaymentPrice->setPrice($this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value);
428 
429  return $this->_oPaymentPrice;
430  }
431 
439  public function getOrderTsProtectionPrice()
440  {
441  if ($this->_oTsProtectionPrice != null) {
443  }
444 
445  $this->_oTsProtectionPrice = oxNew('oxprice');
446  $this->_oTsProtectionPrice->setBruttoPriceMode();
447  $this->_oTsProtectionPrice->setPrice($this->oxorder__oxtsprotectcosts->value, $this->getConfig()->getConfigParam('dDefaultVAT'));
448 
450  }
451 
457  public function getOrderNetSum()
458  {
459  $dTotalNetSum = 0;
460 
461  $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
462  $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
463  $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
464  $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
465 
466  return $dTotalNetSum;
467  }
468 
489  public function finalizeOrder(oxBasket $oBasket, $oUser, $blRecalculatingOrder = false)
490  {
491  // check if this order is already stored
492  $sGetChallenge = oxRegistry::getSession()->getVariable('sess_challenge');
493  if ($this->_checkOrderExist($sGetChallenge)) {
494  oxRegistry::getUtils()->logger('BLOCKER');
495 
496  // we might use this later, this means that somebody klicked like mad on order button
498  }
499 
500  // if not recalculating order, use sess_challenge id, else leave old order id
501  if (!$blRecalculatingOrder) {
502  // use this ID
503  $this->setId($sGetChallenge);
504 
505  // validating various order/basket parameters before finalizing
506  if ($iOrderState = $this->validateOrder($oBasket, $oUser)) {
507  return $iOrderState;
508  }
509  }
510 
511  // copies user info
512  $this->_setUser($oUser);
513 
514  // copies basket info
515  $this->_loadFromBasket($oBasket);
516 
517  // payment information
518  $oUserPayment = $this->_setPayment($oBasket->getPaymentId());
519 
520  // set folder information, if order is new
521  // #M575 in recalculating order case folder must be the same as it was
522  if (!$blRecalculatingOrder) {
523  $this->_setFolder();
524  }
525 
526  // marking as not finished
527  $this->_setOrderStatus('NOT_FINISHED');
528 
529  //saving all order data to DB
530  $this->save();
531 
532  // executing payment (on failure deletes order and returns error code)
533  // in case when recalculating order, payment execution is skipped
534  if (!$blRecalculatingOrder) {
535  $blRet = $this->_executePayment($oBasket, $oUserPayment);
536  if ($blRet !== true) {
537  return $blRet;
538  }
539  }
540 
541  if (!$this->oxorder__oxordernr->value) {
542  $this->_setNumber();
543  } else {
544  oxNew('oxCounter')->update($this->_getCounterIdent(), $this->oxorder__oxordernr->value);
545  }
546 
547  // executing TS protection
548  if (!$blRecalculatingOrder && $oBasket->getTsProductId()) {
549  $blRet = $this->_executeTsProtection($oBasket);
550  if ($blRet !== true) {
551  return $blRet;
552  }
553  }
554 
555  // deleting remark info only when order is finished
556  oxRegistry::getSession()->deleteVariable('ordrem');
557  oxRegistry::getSession()->deleteVariable('stsprotection');
558 
559  //#4005: Order creation time is not updated when order processing is complete
560  if (!$blRecalculatingOrder) {
561  $this->_updateOrderDate();
562  }
563 
564  // updating order trans status (success status)
565  $this->_setOrderStatus('OK');
566 
567  // store orderid
568  $oBasket->setOrderId($this->getId());
569 
570  // updating wish lists
571  $this->_updateWishlist($oBasket->getContents(), $oUser);
572 
573  // updating users notice list
574  $this->_updateNoticeList($oBasket->getContents(), $oUser);
575 
576  // marking vouchers as used and sets them to $this->_aVoucherList (will be used in order email)
577  // skipping this action in case of order recalculation
578  if (!$blRecalculatingOrder) {
579  $this->_markVouchers($oBasket, $oUser);
580  }
581 
582  // send order by email to shop owner and current user
583  // skipping this action in case of order recalculation
584  if (!$blRecalculatingOrder) {
585  $iRet = $this->_sendOrderByEmail($oUser, $oBasket, $oUserPayment);
586  } else {
587  $iRet = self::ORDER_STATE_OK;
588  }
589 
590  return $iRet;
591  }
592 
598  public function isNettoMode()
599  {
600  return (bool) $this->oxorder__oxisnettomode->value;
601  }
602 
603 
609  protected function _setOrderStatus($sStatus)
610  {
611  $oDb = oxDb::getDb();
612  $sQ = 'update oxorder set oxtransstatus=' . $oDb->quote($sStatus) . ' where oxid=' . $oDb->quote($this->getId());
613  $oDb->execute($sQ);
614 
615  //updating order object
616  $this->oxorder__oxtransstatus = new oxField($sStatus, oxField::T_RAW);
617  }
618 
626  protected function _convertVat($sVat)
627  {
628  if (strpos($sVat, '.') < strpos($sVat, ',')) {
629  $sVat = str_replace(array('.', ','), array('', '.'), $sVat);
630  } else {
631  $sVat = str_replace(',', '', $sVat);
632  }
633 
634  return (float) $sVat;
635  }
636 
640  protected function _resetVats()
641  {
642  $this->oxorder__oxartvat1 = new oxField(null);
643  $this->oxorder__oxartvatprice1 = new oxField(null);
644  $this->oxorder__oxartvat2 = new oxField(null);
645  $this->oxorder__oxartvatprice2 = new oxField(null);
646  }
647 
656  protected function _loadFromBasket(oxBasket $oBasket)
657  {
658  $myConfig = $this->getConfig();
659 
660  // store IP Address - default must be FALSE as it is illegal to store
661  if ($myConfig->getConfigParam('blStoreIPs') && $this->oxorder__oxip->value === null) {
662  $this->oxorder__oxip = new oxField(oxRegistry::get("oxUtilsServer")->getRemoteAddress(), oxField::T_RAW);
663  }
664 
665  //setting view mode
666  $this->oxorder__oxisnettomode = new oxField($oBasket->isCalculationModeNetto());
667 
668  // copying main price info
669  $this->oxorder__oxtotalnetsum = new oxField($oBasket->getNettoSum());
670  $this->oxorder__oxtotalbrutsum = new oxField($oBasket->getBruttoSum());
671  $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
672 
673  // copying discounted VAT info
674  $this->_resetVats();
675  $iVatIndex = 1;
676  foreach ($oBasket->getProductVats(false) as $iVat => $dPrice) {
677  $this->{"oxorder__oxartvat$iVatIndex"} = new oxField($this->_convertVat($iVat), oxField::T_RAW);
678  $this->{"oxorder__oxartvatprice$iVatIndex"} = new oxField($dPrice, oxField::T_RAW);
679  $iVatIndex++;
680  }
681 
682  // payment costs if available
683  if (($oPaymentCost = $oBasket->getCosts('oxpayment'))) {
684  $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
685  $this->oxorder__oxpayvat = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
686  }
687 
688  // delivery info
689  if (($oDeliveryCost = $oBasket->getCosts('oxdelivery'))) {
690  $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
691  //V #M382: Save VAT, not VAT value for delivery costs
692  $this->oxorder__oxdelvat = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW); //V #M382
693  $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
694  }
695 
696  // user remark
697  if (!isset($this->oxorder__oxremark) || $this->oxorder__oxremark->value === null) {
698  $this->oxorder__oxremark = new oxField(oxRegistry::getSession()->getVariable('ordrem'), oxField::T_RAW);
699  }
700 
701  // currency
702  $oCur = $myConfig->getActShopCurrencyObject();
703  $this->oxorder__oxcurrency = new oxField($oCur->name);
704  $this->oxorder__oxcurrate = new oxField($oCur->rate, oxField::T_RAW);
705 
706  // store voucher discount
707  if (($oVoucherDiscount = $oBasket->getVoucherDiscount())) {
708  $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
709  }
710 
711  // general discount
712  if ($this->_blReloadDiscount) {
713  $dDiscount = 0;
714  $aDiscounts = $oBasket->getDiscounts();
715  if (count($aDiscounts) > 0) {
716  foreach ($aDiscounts as $oDiscount) {
717  $dDiscount += $oDiscount->dDiscount;
718  }
719  }
720  $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
721  }
722 
723  //order language
724  $this->oxorder__oxlang = new oxField($this->getOrderLanguage());
725 
726 
727  // initial status - 'ERROR'
728  $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
729 
730  // copies basket product info ...
731  $this->_setOrderArticles($oBasket->getContents());
732 
733  // copies wrapping info
734  $this->_setWrapping($oBasket);
735 
736  // copies TS protection info
737  $this->_setTsProtection($oBasket);
738  }
739 
746  public function getOrderLanguage()
747  {
748  if ($this->_iOrderLang === null) {
749  if (isset($this->oxorder__oxlang->value)) {
750  $this->_iOrderLang = oxRegistry::getLang()->validateLanguage($this->oxorder__oxlang->value);
751  } else {
752  $this->_iOrderLang = oxRegistry::getLang()->getBaseLanguage();
753  }
754  }
755 
756  return $this->_iOrderLang;
757  }
758 
764  protected function _setUser($oUser)
765  {
766 
767  $this->oxorder__oxuserid = new oxField($oUser->getId());
768 
769  // bill address
770  $this->oxorder__oxbillcompany = clone $oUser->oxuser__oxcompany;
771  $this->oxorder__oxbillemail = clone $oUser->oxuser__oxusername;
772  $this->oxorder__oxbillfname = clone $oUser->oxuser__oxfname;
773  $this->oxorder__oxbilllname = clone $oUser->oxuser__oxlname;
774  $this->oxorder__oxbillstreet = clone $oUser->oxuser__oxstreet;
775  $this->oxorder__oxbillstreetnr = clone $oUser->oxuser__oxstreetnr;
776  $this->oxorder__oxbilladdinfo = clone $oUser->oxuser__oxaddinfo;
777  $this->oxorder__oxbillustid = clone $oUser->oxuser__oxustid;
778  $this->oxorder__oxbillcity = clone $oUser->oxuser__oxcity;
779  $this->oxorder__oxbillcountryid = clone $oUser->oxuser__oxcountryid;
780  $this->oxorder__oxbillstateid = clone $oUser->oxuser__oxstateid;
781  $this->oxorder__oxbillzip = clone $oUser->oxuser__oxzip;
782  $this->oxorder__oxbillfon = clone $oUser->oxuser__oxfon;
783  $this->oxorder__oxbillfax = clone $oUser->oxuser__oxfax;
784  $this->oxorder__oxbillsal = clone $oUser->oxuser__oxsal;
785 
786 
787  // delivery address
788  if (($oDelAdress = $this->getDelAddressInfo())) {
789  // set delivery address
790  $this->oxorder__oxdelcompany = clone $oDelAdress->oxaddress__oxcompany;
791  $this->oxorder__oxdelfname = clone $oDelAdress->oxaddress__oxfname;
792  $this->oxorder__oxdellname = clone $oDelAdress->oxaddress__oxlname;
793  $this->oxorder__oxdelstreet = clone $oDelAdress->oxaddress__oxstreet;
794  $this->oxorder__oxdelstreetnr = clone $oDelAdress->oxaddress__oxstreetnr;
795  $this->oxorder__oxdeladdinfo = clone $oDelAdress->oxaddress__oxaddinfo;
796  $this->oxorder__oxdelcity = clone $oDelAdress->oxaddress__oxcity;
797  $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
798  $this->oxorder__oxdelstateid = clone $oDelAdress->oxaddress__oxstateid;
799  $this->oxorder__oxdelzip = clone $oDelAdress->oxaddress__oxzip;
800  $this->oxorder__oxdelfon = clone $oDelAdress->oxaddress__oxfon;
801  $this->oxorder__oxdelfax = clone $oDelAdress->oxaddress__oxfax;
802  $this->oxorder__oxdelsal = clone $oDelAdress->oxaddress__oxsal;
803  }
804  }
805 
811  protected function _setWrapping(oxBasket $oBasket)
812  {
813  $myConfig = $this->getConfig();
814 
815  // wrapping price
816  if (($oWrappingCost = $oBasket->getCosts('oxwrapping'))) {
817  $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
818  // wrapping VAT will be always calculated (#3757)
819  $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
820  }
821 
822  if (($oGiftCardCost = $oBasket->getCosts('oxgiftcard'))) {
823  $this->oxorder__oxgiftcardcost = new oxField($oGiftCardCost->getBruttoPrice(), oxField::T_RAW);
824  $this->oxorder__oxgiftcardvat = new oxField($oGiftCardCost->getVAT(), oxField::T_RAW);
825  }
826 
827  // greetings card
828  $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
829 
830  // card text will be stored in database
831  $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
832  }
833 
840  protected function _setOrderArticles($aArticleList)
841  {
842  // reset articles list
843  $this->_oArticles = oxNew('oxlist');
844  $iCurrLang = $this->getOrderLanguage();
845 
846  // add all the products we have on basket to the order
847  foreach ($aArticleList as $oContent) {
848 
849  //$oContent->oProduct = $oContent->getArticle();
850  // #M773 Do not use article lazy loading on order save
851  $oProduct = $oContent->getArticle(true, null, true);
852 
853  // copy only if object is oxarticle type
854  if ($oProduct->isOrderArticle()) {
855  $oOrderArticle = $oProduct;
856  } else {
857 
858  // if order language doe not match product language - article must be reloaded in order language
859  if ($iCurrLang != $oProduct->getLanguage()) {
860  $oProduct->loadInLang($iCurrLang, $oProduct->getProductId());
861  }
862 
863  // set chosen select list
864  $sSelList = '';
865  if (count($aChosenSelList = $oContent->getChosenSelList())) {
866  foreach ($aChosenSelList as $oItem) {
867  if ($sSelList) {
868  $sSelList .= ", ";
869  }
870  $sSelList .= "{$oItem->name} : {$oItem->value}";
871  }
872  }
873 
874  $oOrderArticle = oxNew('oxorderarticle');
875  $oOrderArticle->setIsNewOrderItem(true);
876  $oOrderArticle->copyThis($oProduct);
877  $oOrderArticle->setId();
878 
879  $oOrderArticle->oxorderarticles__oxartnum = clone $oProduct->oxarticles__oxartnum;
880  $oOrderArticle->oxorderarticles__oxselvariant = new oxField(trim($sSelList . ' ' . $oProduct->oxarticles__oxvarselect->getRawValue()), oxField::T_RAW);
881  $oOrderArticle->oxorderarticles__oxshortdesc = new oxField($oProduct->oxarticles__oxshortdesc->getRawValue(), oxField::T_RAW);
882  // #M974: duplicated entries for the name of variants in orders
883  $oOrderArticle->oxorderarticles__oxtitle = new oxField(trim($oProduct->oxarticles__oxtitle->getRawValue()), oxField::T_RAW);
884 
885  // copying persistent parameters ...
886  if (!is_array($aPersParams = $oProduct->getPersParams())) {
887  $aPersParams = $oContent->getPersParams();
888  }
889  if (is_array($aPersParams) && count($aPersParams)) {
890  $oOrderArticle->oxorderarticles__oxpersparam = new oxField(serialize($aPersParams), oxField::T_RAW);
891  }
892  }
893 
894  // ids, titles, numbers ...
895  $oOrderArticle->oxorderarticles__oxorderid = new oxField($this->getId());
896  $oOrderArticle->oxorderarticles__oxartid = new oxField($oContent->getProductId());
897  $oOrderArticle->oxorderarticles__oxamount = new oxField($oContent->getAmount());
898 
899  // prices
900  $oPrice = $oContent->getPrice();
901  $oOrderArticle->oxorderarticles__oxnetprice = new oxField($oPrice->getNettoPrice(), oxField::T_RAW);
902  $oOrderArticle->oxorderarticles__oxvatprice = new oxField($oPrice->getVatValue(), oxField::T_RAW);
903  $oOrderArticle->oxorderarticles__oxbrutprice = new oxField($oPrice->getBruttoPrice(), oxField::T_RAW);
904  $oOrderArticle->oxorderarticles__oxvat = new oxField($oPrice->getVat(), oxField::T_RAW);
905 
906  $oUnitPtice = $oContent->getUnitPrice();
907  $oOrderArticle->oxorderarticles__oxnprice = new oxField($oUnitPtice->getNettoPrice(), oxField::T_RAW);
908  $oOrderArticle->oxorderarticles__oxbprice = new oxField($oUnitPtice->getBruttoPrice(), oxField::T_RAW);
909 
910  // wrap id
911  $oOrderArticle->oxorderarticles__oxwrapid = new oxField($oContent->getWrappingId(), oxField::T_RAW);
912 
913  // items shop id
914  $oOrderArticle->oxorderarticles__oxordershopid = new oxField($oContent->getShopId(), oxField::T_RAW);
915 
916  // bundle?
917  $oOrderArticle->oxorderarticles__oxisbundle = new oxField($oContent->isBundle());
918 
919  // add information for eMail
920  //P
921  //TODO: check if this assign is needed at all
922  $oOrderArticle->oProduct = $oProduct;
923 
924  $oOrderArticle->setArticle($oProduct);
925 
926  // simulation order article list
927  $this->_oArticles->offsetSet($oOrderArticle->getId(), $oOrderArticle);
928  }
929  }
930 
942  protected function _executePayment(oxBasket $oBasket, $oUserpayment)
943  {
944  $oPayTransaction = $this->_getGateway();
945  $oPayTransaction->setPaymentParams($oUserpayment);
946 
947  if (!$oPayTransaction->executePayment($oBasket->getPrice()->getBruttoPrice(), $this)) {
948  $this->delete();
949 
950  // checking for error messages
951  if (method_exists($oPayTransaction, 'getLastError')) {
952  if (($sLastError = $oPayTransaction->getLastError())) {
953  return $sLastError;
954  }
955  }
956 
957  // checking for error codes
958  if (method_exists($oPayTransaction, 'getLastErrorNo')) {
959  if (($iLastErrorNo = $oPayTransaction->getLastErrorNo())) {
960  return $iLastErrorNo;
961  }
962  }
963 
964  return self::ORDER_STATE_PAYMENTERROR; // means no authentication
965  }
966 
967  return true; // everything fine
968  }
969 
976  protected function _getGateway()
977  {
978  return oxNew('oxPaymentGateway');
979  }
980 
988  protected function _setPayment($sPaymentid)
989  {
990  $oPayment = oxNew('oxpayment');
991 
992  if (!$oPayment->load($sPaymentid)) {
993  return null;
994  }
995 
996  $aDynvalue = $this->getDynamicValues();
997 
998  $oPayment->setDynValues(oxRegistry::getUtils()->assignValuesFromText($oPayment->oxpayments__oxvaldesc->value));
999 
1000  // collecting dynamic values
1001  $aDynVal = array();
1002 
1003  if (is_array($aPaymentDynValues = $oPayment->getDynValues())) {
1004  foreach ($aPaymentDynValues as $key => $oVal) {
1005  if (isset($aDynvalue[$oVal->name])) {
1006  $oVal->value = $aDynvalue[$oVal->name];
1007  }
1008 
1009  //$oPayment->setDynValue($key, $oVal);
1010  $aPaymentDynValues[$key] = $oVal;
1011  $aDynVal[$oVal->name] = $oVal->value;
1012  }
1013  }
1014 
1015  // Store this payment information, we might allow users later to
1016  // reactivate already give payment information
1017 
1018  $oUserpayment = oxNew('oxuserpayment');
1019  $oUserpayment->oxuserpayments__oxuserid = clone $this->oxorder__oxuserid;
1020  $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
1021  $oUserpayment->oxuserpayments__oxvalue = new oxField(oxRegistry::getUtils()->assignValuesToText($aDynVal), oxField::T_RAW);
1022  $oUserpayment->oxpayments__oxdesc = clone $oPayment->oxpayments__oxdesc;
1023  $oUserpayment->oxpayments__oxlongdesc = clone $oPayment->oxpayments__oxlongdesc;
1024  $oUserpayment->setDynValues($aPaymentDynValues);
1025  $oUserpayment->save();
1026 
1027  // storing payment information to order
1028  $this->oxorder__oxpaymentid = new oxField($oUserpayment->getId(), oxField::T_RAW);
1029  $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
1030 
1031  // returning user payment object which will be used later in code ...
1032  return $oUserpayment;
1033  }
1034 
1038  protected function _setFolder()
1039  {
1040  $myConfig = $this->getConfig();
1041  $this->oxorder__oxfolder = new oxField(key($myConfig->getShopConfVar('aOrderfolder', $myConfig->getShopId())), oxField::T_RAW);
1042  }
1043 
1051  protected function _updateWishlist($aArticleList, $oUser)
1052  {
1053 
1054  foreach ($aArticleList as $oContent) {
1055  if (($sWishId = $oContent->getWishId())) {
1056 
1057  // checking which wishlist user uses ..
1058  if ($sWishId == $oUser->getId()) {
1059  $oUserBasket = $oUser->getBasket('wishlist');
1060  } else {
1061  $aWhere = array('oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist');
1062  $oUserBasket = oxNew('oxuserbasket');
1063  $oUserBasket->assignRecord($oUserBasket->buildSelectString($aWhere));
1064  }
1065 
1066  // updating users wish list
1067  if ($oUserBasket) {
1068  if (!($sProdId = $oContent->getWishArticleId())) {
1069  $sProdId = $oContent->getProductId();
1070  }
1071  $oUserBasketItem = $oUserBasket->getItem($sProdId, $oContent->getSelList());
1072  $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
1073  if ($dNewAmount < 0) {
1074  $dNewAmount = 0;
1075  }
1076  $oUserBasket->addItemToBasket($sProdId, $dNewAmount, $oContent->getSelList(), true);
1077  }
1078  }
1079  }
1080  }
1081 
1089  protected function _updateNoticeList($aArticleList, $oUser)
1090  {
1091  /*
1092  * #6141
1093  * If there is no noticelist, don't create an empty one.
1094  * Because loading the list via $user->getBasket('noticelist') will create it if there isn't one, but it will
1095  * only exists in the session for now. So it is possible to check if it has an oxid. If yes then we had a list.
1096  * If no then it was just created and will cause a new row in oxuserbaskets without content in oxuserbasketitems.
1097  * Also it will prevent creating a row for guests.
1098  */
1099  if ($oUser->getBasket('noticelist')->oxuserbaskets__oxid->value === null) {
1100  return;
1101  }
1102 
1103  // loading users notice list ..
1104  if ($oUserBasket = $oUser->getBasket('noticelist')) {
1105  // only if wishlist is enabled
1106  foreach ($aArticleList as $oContent) {
1107  $sProdId = $oContent->getProductId();
1108 
1109  // updating users notice list
1110  $oUserBasketItem = $oUserBasket->getItem($sProdId, $oContent->getSelList(), $oContent->getPersParams());
1111  $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
1112  if ($dNewAmount < 0) {
1113  $dNewAmount = 0;
1114  }
1115  $oUserBasket->addItemToBasket($sProdId, $dNewAmount, $oContent->getSelList(), true, $oContent->getPersParams());
1116  }
1117  }
1118  }
1119 
1123  protected function _updateOrderDate()
1124  {
1125  $oDb = oxDb::getDb();
1126  $sDate = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
1127  $sQ = 'update oxorder set oxorderdate=' . $oDb->quote($sDate) . ' where oxid=' . $oDb->quote($this->getId());
1128  $this->oxorder__oxorderdate = new oxField($sDate, oxField::T_RAW);
1129  $oDb->execute($sQ);
1130  }
1131 
1139  protected function _markVouchers($oBasket, $oUser)
1140  {
1141  $this->_aVoucherList = $oBasket->getVouchers();
1142 
1143  if (is_array($this->_aVoucherList)) {
1144  foreach ($this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
1145  $oVoucher = oxNew('oxvoucher');
1146  $oVoucher->load($sVoucherId);
1147  $oVoucher->markAsUsed($this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->dVoucherdiscount);
1148 
1149  $this->_aVoucherList[$sVoucherId] = $oVoucher;
1150  }
1151  }
1152  }
1153 
1159  public function save()
1160  {
1161  if (($blSave = parent::save())) {
1162 
1163  // saving order articles
1164  $oOrderArticles = $this->getOrderArticles();
1165  if ($oOrderArticles && count($oOrderArticles) > 0) {
1166  foreach ($oOrderArticles as $oOrderArticle) {
1167  $oOrderArticle->save();
1168  }
1169  }
1170  }
1171 
1172  return $blSave;
1173  }
1174 
1181  public function getDelAddressInfo()
1182  {
1183  $oDelAdress = null;
1184  if (!($soxAddressId = oxRegistry::getConfig()->getRequestParameter('deladrid'))) {
1185  $soxAddressId = oxRegistry::getSession()->getVariable('deladrid');
1186  }
1187  if ($soxAddressId) {
1188  $oDelAdress = oxNew('oxaddress');
1189  $oDelAdress->load($soxAddressId);
1190 
1191  //get delivery country name from delivery country id
1192  if ($oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1) {
1193  $oCountry = oxNew('oxcountry');
1194  $oCountry->load($oDelAdress->oxaddress__oxcountryid->value);
1195  $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
1196  }
1197  }
1198 
1199  return $oDelAdress;
1200  }
1201 
1210  public function validateStock($oBasket)
1211  {
1212  foreach ($oBasket->getContents() as $key => $oContent) {
1213  try {
1214  $oProd = $oContent->getArticle(true, null, true);
1215  } catch (oxNoArticleException $oEx) {
1216  $oBasket->removeItem($key);
1217  throw $oEx;
1218  } catch (oxArticleInputException $oEx) {
1219  $oBasket->removeItem($key);
1220  throw $oEx;
1221  }
1222 
1223  // check if its still available
1224  $dArtStockAmount = $oBasket->getArtStockInBasket($oProd->getId(), $key);
1225  $iOnStock = $oProd->checkForStock($oContent->getAmount(), $dArtStockAmount);
1226  if ($iOnStock !== true) {
1228  $oEx = oxNew('oxOutOfStockException');
1229  $oEx->setMessage('ERROR_MESSAGE_OUTOFSTOCK_OUTOFSTOCK');
1230  $oEx->setArticleNr($oProd->oxarticles__oxartnum->value);
1231  $oEx->setProductId($oProd->getId());
1232  $oEx->setBasketIndex($key);
1233 
1234  if (!is_numeric($iOnStock)) {
1235  $iOnStock = 0;
1236  }
1237  $oEx->setRemainingAmount($iOnStock);
1238  throw $oEx;
1239  }
1240  }
1241  }
1242 
1248  protected function _insert()
1249  {
1250  $myConfig = $this->getConfig();
1251  $oUtilsDate = oxRegistry::get("oxUtilsDate");
1252 
1253  //V #M525 orderdate must be the same as it was
1254  if (!$this->oxorder__oxorderdate->value) {
1255  $this->oxorder__oxorderdate = new oxField(date('Y-m-d H:i:s', $oUtilsDate->getTime()), oxField::T_RAW);
1256  } else {
1257  $this->oxorder__oxorderdate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxorderdate->value, true));
1258  }
1259 
1260  $this->oxorder__oxshopid = new oxField($myConfig->getShopId(), oxField::T_RAW);
1261  $this->oxorder__oxsenddate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxsenddate->value, true));
1262 
1263  $blInsert = parent::_insert();
1264 
1265  return $blInsert;
1266  }
1267 
1273  protected function _getCounterIdent()
1274  {
1275  $sCounterIdent = ($this->_blSeparateNumbering) ? 'oxOrder_' . $this->getConfig()->getShopId() : 'oxOrder';
1276 
1277  return $sCounterIdent;
1278  }
1279 
1280 
1286  protected function _setNumber()
1287  {
1288  $oDb = oxDb::getDb();
1289 
1290  $iCnt = oxNew('oxCounter')->getNext($this->_getCounterIdent());
1291  $sQ = "update oxorder set oxordernr = ? where oxid = ?";
1292  $blUpdate = ( bool ) $oDb->execute($sQ, array($iCnt, $this->getId()));
1293 
1294  if ($blUpdate) {
1295  $this->oxorder__oxordernr = new oxField($iCnt);
1296  }
1297 
1298  return $blUpdate;
1299  }
1300 
1306  protected function _update()
1307  {
1308  $this->_aSkipSaveFields = array('oxtimestamp', 'oxorderdate');
1309  $this->oxorder__oxsenddate = new oxField(oxRegistry::get("oxUtilsDate")->formatDBDate($this->oxorder__oxsenddate->value, true));
1310 
1311  return parent::_update();
1312  }
1313 
1322  public function delete($sOxId = null)
1323  {
1324  if ($sOxId) {
1325  if (!$this->load($sOxId)) {
1326  // such order does not exist
1327  return false;
1328  }
1329  } elseif (!$sOxId) {
1330  $sOxId = $this->getId();
1331  }
1332 
1333  // no order id is passed
1334  if (!$sOxId) {
1335  return false;
1336  }
1337 
1338 
1339  // delete order articles
1340  $oOrderArticles = $this->getOrderArticles(false);
1341  foreach ($oOrderArticles as $oOrderArticle) {
1342  $oOrderArticle->delete();
1343  }
1344 
1345  // #440 - deleting user payment info
1346  if ($oPaymentType = $this->getPaymentType()) {
1347  $oPaymentType->delete();
1348  }
1349 
1350  return parent::delete($sOxId);
1351  }
1352 
1362  public function recalculateOrder($aNewArticles = array())
1363  {
1364  oxDb::getDb()->startTransaction();
1365 
1366  try {
1367  $oBasket = $this->_getOrderBasket();
1368 
1369  // add this order articles to virtual basket and recalculates basket
1370  $this->_addOrderArticlesToBasket($oBasket, $this->getOrderArticles(true));
1371 
1372  // adding new articles to existing order
1373  $this->_addArticlesToBasket($oBasket, $aNewArticles);
1374 
1375  // recalculating basket
1376  $oBasket->calculateBasket(true);
1377 
1378  //finalizing order (skipping payment execution, vouchers marking and mail sending)
1379  $iRet = $this->finalizeOrder($oBasket, $this->getOrderUser(), true);
1380 
1381  //if finalizing order failed, rollback transaction
1382  if ($iRet !== 1) {
1383  oxDb::getDb()->rollbackTransaction();
1384  } else {
1385  oxDb::getDb()->commitTransaction();
1386  }
1387 
1388  } catch (Exception $oE) {
1389  // if exception, rollBack everything
1390  oxDb::getDb()->rollbackTransaction();
1391 
1392  if (defined('OXID_PHP_UNIT')) {
1393  throw $oE;
1394  }
1395  }
1396  }
1397 
1398  protected $_oOrderBasket = null;
1399 
1407  protected function _getOrderBasket($blStockCheck = true)
1408  {
1409  $this->_oOrderBasket = oxNew("oxBasket");
1410  $this->_oOrderBasket->enableSaveToDataBase(false);
1411 
1412  //setting recalculation mode
1413  $this->_oOrderBasket->setCalculationModeNetto($this->isNettoMode());
1414 
1415  // setting stock check mode
1416  $this->_oOrderBasket->setStockCheckMode($blStockCheck);
1417 
1418  // setting virtual basket user
1419  $this->_oOrderBasket->setBasketUser($this->getOrderUser());
1420 
1421  // transferring order id
1422  $this->_oOrderBasket->setOrderId($this->getId());
1423 
1424  // setting basket currency order uses
1425  $aCurrencies = $this->getConfig()->getCurrencyArray();
1426  foreach ($aCurrencies as $oCur) {
1427  if ($oCur->name == $this->oxorder__oxcurrency->value) {
1428  $oBasketCur = $oCur;
1429  break;
1430  }
1431  }
1432 
1433  // setting currency
1434  $this->_oOrderBasket->setBasketCurrency($oBasketCur);
1435 
1436  // set basket card id and message
1437  $this->_oOrderBasket->setCardId($this->oxorder__oxcardid->value);
1438  $this->_oOrderBasket->setCardMessage($this->oxorder__oxcardtext->value);
1439 
1440  if ($this->_blReloadDiscount) {
1442  // disabling availability check
1443  $this->_oOrderBasket->setSkipVouchersChecking(true);
1444 
1445  // add previously used vouchers
1446  $sQ = 'select oxid from oxvouchers where oxorderid = ' . $oDb->quote($this->getId());
1447  $aVouchers = $oDb->getAll($sQ);
1448  foreach ($aVouchers as $aVoucher) {
1449  $this->_oOrderBasket->addVoucher($aVoucher['oxid']);
1450  }
1451  } else {
1452  $this->_oOrderBasket->setDiscountCalcMode(false);
1453  $this->_oOrderBasket->setVoucherDiscount($this->oxorder__oxvoucherdiscount->value);
1454  $this->_oOrderBasket->setTotalDiscount($this->oxorder__oxdiscount->value);
1455  }
1456 
1457  // must be kept old delivery?
1458  if (!$this->_blReloadDelivery) {
1459  $this->_oOrderBasket->setDeliveryPrice($this->getOrderDeliveryPrice());
1460  } else {
1461  // set shipping
1462  $this->_oOrderBasket->setShipping($this->oxorder__oxdeltype->value);
1463  $this->_oOrderBasket->setDeliveryPrice(null);
1464  }
1465 
1466  //set basket payment
1467  $this->_oOrderBasket->setPayment($this->oxorder__oxpaymenttype->value);
1468 
1469  return $this->_oOrderBasket;
1470  }
1471 
1478  public function setDelivery($sDeliveryId)
1479  {
1480  $this->reloadDelivery(true);
1481  $this->oxorder__oxdeltype = new oxField($sDeliveryId);
1482  }
1483 
1489  public function getOrderUser()
1490  {
1491  if ($this->_oUser === null) {
1492  $this->_oUser = oxNew("oxuser");
1493  $this->_oUser->load($this->oxorder__oxuserid->value);
1494 
1495  // if object is loaded then reusing its order info
1496  if ($this->_isLoaded) {
1497  // bill address
1498  $this->_oUser->oxuser__oxcompany = clone $this->oxorder__oxbillcompany;
1499  $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
1500  $this->_oUser->oxuser__oxfname = clone $this->oxorder__oxbillfname;
1501  $this->_oUser->oxuser__oxlname = clone $this->oxorder__oxbilllname;
1502  $this->_oUser->oxuser__oxstreet = clone $this->oxorder__oxbillstreet;
1503  $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
1504  $this->_oUser->oxuser__oxaddinfo = clone $this->oxorder__oxbilladdinfo;
1505  $this->_oUser->oxuser__oxustid = clone $this->oxorder__oxbillustid;
1506 
1507 
1508  $this->_oUser->oxuser__oxcity = clone $this->oxorder__oxbillcity;
1509  $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
1510  $this->_oUser->oxuser__oxstateid = clone $this->oxorder__oxbillstateid;
1511  $this->_oUser->oxuser__oxzip = clone $this->oxorder__oxbillzip;
1512  $this->_oUser->oxuser__oxfon = clone $this->oxorder__oxbillfon;
1513  $this->_oUser->oxuser__oxfax = clone $this->oxorder__oxbillfax;
1514  $this->_oUser->oxuser__oxsal = clone $this->oxorder__oxbillsal;
1515  }
1516  }
1517 
1518  return $this->_oUser;
1519  }
1520 
1526  public function pdfFooter($oPdf)
1527  {
1528  }
1529 
1535  public function pdfHeaderplus($oPdf)
1536  {
1537  }
1538 
1544  public function pdfHeader($oPdf)
1545  {
1546  }
1547 
1554  public function genPdf($sFilename, $iSelLang = 0)
1555  {
1556  }
1557 
1563  public function getInvoiceNum()
1564  {
1565  $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "' . $this->getConfig()->getShopId() . '" ';
1566 
1567  return (( int ) oxDb::getDb()->getOne($sQ, false) + 1);
1568  }
1569 
1575  public function getNextBillNum()
1576  {
1577  $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "' . $this->getConfig()->getShopId() . '" ';
1578 
1579  return (( int ) oxDb::getDb()->getOne($sQ, false) + 1);
1580  }
1581 
1587  public function getShippingSetList()
1588  {
1589  // in which country we deliver
1590  if (!($sShipId = $this->oxorder__oxdelcountryid->value)) {
1591  $sShipId = $this->oxorder__oxbillcountryid->value;
1592  }
1593 
1594  $oBasket = $this->_getOrderBasket(false);
1595 
1596  // unsetting bundles
1597  $oOrderArticles = $this->getOrderArticles();
1598  foreach ($oOrderArticles as $sItemId => $oItem) {
1599  if ($oItem->isBundle()) {
1600  $oOrderArticles->offsetUnset($sItemId);
1601  }
1602  }
1603 
1604  // add this order articles to basket and recalculate basket
1605  $this->_addOrderArticlesToBasket($oBasket, $oOrderArticles);
1606 
1607  // recalculating basket
1608  $oBasket->calculateBasket(true);
1609 
1610  // load fitting deliveries list
1611  $oDeliveryList = oxNew("oxDeliveryList", "core");
1612  $oDeliveryList->setCollectFittingDeliveriesSets(true);
1613 
1614  return $oDeliveryList->getDeliveryList($oBasket, $this->getOrderUser(), $sShipId);
1615  }
1616 
1622  public function getVoucherNrList()
1623  {
1625  $aVouchers = array();
1626  $sSelect = "select oxvouchernr from oxvouchers where oxorderid = " . $oDb->quote($this->oxorder__oxid->value);
1627  $rs = $oDb->select($sSelect);
1628  if ($rs != false && $rs->recordCount() > 0) {
1629  while (!$rs->EOF) {
1630  $aVouchers[] = $rs->fields['oxvouchernr'];
1631  $rs->moveNext();
1632  }
1633  }
1634 
1635  return $aVouchers;
1636  }
1637 
1645  public function getOrderSum($blToday = false)
1646  {
1647  $sSelect = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
1648  $sSelect .= 'oxshopid = "' . $this->getConfig()->getShopId() . '" and oxorder.oxstorno != "1" ';
1649 
1650  if ($blToday) {
1651  $sSelect .= 'and oxorderdate like "' . date('Y-m-d') . '%" ';
1652  }
1653 
1654  return ( double ) oxDb::getDb()->getOne($sSelect, false, false);
1655  }
1656 
1664  public function getOrderCnt($blToday = false)
1665  {
1666  $sSelect = 'select count(*) from oxorder where ';
1667  $sSelect .= 'oxshopid = "' . $this->getConfig()->getShopId() . '" and oxorder.oxstorno != "1" ';
1668 
1669  if ($blToday) {
1670  $sSelect .= 'and oxorderdate like "' . date('Y-m-d') . '%" ';
1671  }
1672 
1673  return ( int ) oxDb::getDb()->getOne($sSelect, false, false);
1674  }
1675 
1676 
1684  protected function _checkOrderExist($sOxId = null)
1685  {
1686  if (!$sOxId) {
1687  return false;
1688  }
1689 
1690  $oDb = oxDb::getDb();
1691  if ($oDb->getOne('select oxid from oxorder where oxid = ' . $oDb->quote($sOxId), false, false)) {
1692  return true;
1693  }
1694 
1695  return false;
1696  }
1697 
1707  protected function _sendOrderByEmail($oUser = null, $oBasket = null, $oPayment = null)
1708  {
1710 
1711  // add user, basket and payment to order
1712  $this->_oUser = $oUser;
1713  $this->_oBasket = $oBasket;
1714  $this->_oPayment = $oPayment;
1715 
1716  $oxEmail = oxNew('oxemail');
1717 
1718  // send order email to user
1719  if ($oxEmail->sendOrderEMailToUser($this)) {
1720  // mail to user was successfully sent
1721  $iRet = self::ORDER_STATE_OK;
1722  }
1723 
1724  // send order email to shop owner
1725  $oxEmail->sendOrderEMailToOwner($this);
1726 
1727  return $iRet;
1728  }
1729 
1735  public function getBasket()
1736  {
1737  return $this->_oBasket;
1738  }
1739 
1745  public function getPayment()
1746  {
1747  return $this->_oPayment;
1748  }
1749 
1755  public function getVoucherList()
1756  {
1757  return $this->_aVoucherList;
1758  }
1759 
1765  public function getDelSet()
1766  {
1767  if ($this->_oDelSet == null) {
1768  // load deliveryset info
1769  $this->_oDelSet = oxNew('oxdeliveryset');
1770  $this->_oDelSet->load($this->oxorder__oxdeltype->value);
1771  }
1772 
1773  return $this->_oDelSet;
1774  }
1775 
1781  public function getPaymentType()
1782  {
1783  if ($this->oxorder__oxpaymentid->value && $this->_oPaymentType === null) {
1784  $this->_oPaymentType = false;
1785  $oPaymentType = oxNew('oxuserpayment');
1786  if ($oPaymentType->load($this->oxorder__oxpaymentid->value)) {
1787  $this->_oPaymentType = $oPaymentType;
1788  }
1789  }
1790 
1791  return $this->_oPaymentType;
1792  }
1793 
1799  public function getGiftCard()
1800  {
1801  if ($this->oxorder__oxcardid->value && $this->_oGiftCard == null) {
1802  $this->_oGiftCard = oxNew('oxwrapping');
1803  $this->_oGiftCard->load($this->oxorder__oxcardid->value);
1804  }
1805 
1806  return $this->_oGiftCard;
1807  }
1808 
1814  public function setSeparateNumbering($blSeparateNumbering = null)
1815  {
1816  $this->_blSeparateNumbering = $blSeparateNumbering;
1817  }
1818 
1826  public function getLastUserPaymentType($sUserId)
1827  {
1828  $oDb = oxDb::getDb();
1829  $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="' . $this->getConfig()->getShopId() . '" and oxorder.oxuserid=' . $oDb->quote($sUserId) . ' order by oxorder.oxorderdate desc ';
1830  $sLastPaymentId = $oDb->getOne($sQ, false, false);
1831 
1832  return $sLastPaymentId;
1833  }
1834 
1841  protected function _addOrderArticlesToBasket($oBasket, $aOrderArticles)
1842  {
1843  // if no order articles, return empty basket
1844  if (count($aOrderArticles) > 0) {
1845 
1846  //adding order articles to basket
1847  foreach ($aOrderArticles as $oOrderArticle) {
1848  $oBasket->addOrderArticleToBasket($oOrderArticle);
1849  }
1850  }
1851  }
1852 
1859  protected function _addArticlesToBasket($oBasket, $aArticles)
1860  {
1861  // if no order articles
1862  if (count($aArticles) > 0) {
1863 
1864  //adding order articles to basket
1865  foreach ($aArticles as $oArticle) {
1866  $aSel = isset($oArticle->oxorderarticles__oxselvariant) ? $oArticle->oxorderarticles__oxselvariant->value : null;
1867  $aPersParam = isset($oArticle->oxorderarticles__oxpersparam) ? $oArticle->getPersParams() : null;
1868  $oBasket->addToBasket(
1869  $oArticle->oxorderarticles__oxartid->value,
1870  $oArticle->oxorderarticles__oxamount->value,
1871  $aSel, $aPersParam
1872  );
1873  }
1874  }
1875  }
1876 
1882  public function getTotalOrderSum()
1883  {
1884  $oCur = $this->getConfig()->getActShopCurrencyObject();
1885 
1886  return number_format((double) $this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
1887  }
1888 
1896  public function getProductVats($blFormatCurrency = true)
1897  {
1898  $aVats = array();
1899  if ($this->oxorder__oxartvat1->value) {
1900  $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
1901  }
1902  if ($this->oxorder__oxartvat2->value) {
1903  $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
1904  }
1905 
1906  if ($blFormatCurrency) {
1907  $oLang = oxRegistry::getLang();
1908  $oCur = $this->getConfig()->getActShopCurrencyObject();
1909  foreach ($aVats as $sKey => $dVat) {
1910  $aVats[$sKey] = $oLang->formatCurrency($dVat, $oCur);
1911  }
1912  }
1913 
1914  return $aVats;
1915  }
1916 
1922  public function getBillCountry()
1923  {
1924  if (!$this->oxorder__oxbillcountry->value) {
1925  $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle($this->oxorder__oxbillcountryid->value));
1926  }
1927 
1928  return $this->oxorder__oxbillcountry;
1929  }
1930 
1936  public function getDelCountry()
1937  {
1938  if (!$this->oxorder__oxdelcountry->value) {
1939  $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle($this->oxorder__oxdelcountryid->value));
1940  }
1941 
1942  return $this->oxorder__oxdelcountry;
1943  }
1944 
1950  public function reloadDelivery($blReload)
1951  {
1952  $this->_blReloadDelivery = $blReload;
1953  }
1954 
1960  public function reloadDiscount($blReload)
1961  {
1962  $this->_blReloadDiscount = $blReload;
1963  }
1964 
1968  public function cancelOrder()
1969  {
1970  $this->oxorder__oxstorno = new oxField(1);
1971  if ($this->save()) {
1972 
1973 
1974  // canceling ordered products
1975  foreach ($this->getOrderArticles() as $oOrderArticle) {
1976 
1977 
1978  $oOrderArticle->cancelOrderArticle();
1979  }
1980  }
1981  }
1982 
1989  public function getOrderCurrency()
1990  {
1991  if ($this->_oOrderCurrency === null) {
1992 
1993  // setting default in case unrecognized currency was set during order
1994  $aCurrencies = $this->getConfig()->getCurrencyArray();
1995  $this->_oOrderCurrency = current($aCurrencies);
1996 
1997  foreach ($aCurrencies as $oCurr) {
1998  if ($oCurr->name == $this->oxorder__oxcurrency->value) {
1999  $this->_oOrderCurrency = $oCurr;
2000  break;
2001  }
2002  }
2003  }
2004 
2005  return $this->_oOrderCurrency;
2006  }
2007 
2017  public function validateOrder($oBasket, $oUser)
2018  {
2019  // validating stock
2020  $iValidState = $this->validateStock($oBasket);
2021 
2022  if (!$iValidState) {
2023  // validating delivery
2024  $iValidState = $this->validateDelivery($oBasket);
2025  }
2026 
2027  if (!$iValidState) {
2028  // validating payment
2029  $iValidState = $this->validatePayment($oBasket);
2030  }
2031 
2032  if (!$iValidState) {
2033  //0003110 validating delivery address, it is not be changed during checkout process
2034  $iValidState = $this->validateDeliveryAddress($oUser);
2035  }
2036 
2037  if (!$iValidState) {
2038  // validating minimum price
2039  $iValidState = $this->validateBasket($oBasket);
2040  }
2041 
2042  return $iValidState;
2043  }
2044 
2052  public function validateBasket($oBasket)
2053  {
2054  return $oBasket->isBelowMinOrderPrice() ? self::ORDER_STATE_BELOWMINPRICE : null;
2055  }
2056 
2065  public function validateDeliveryAddress($oUser)
2066  {
2067  $sDelAddressMD5 = $this->getConfig()->getRequestParameter('sDeliveryAddressMD5');
2068 
2069  $sDeliveryAddress = $oUser->getEncodedDeliveryAddress();
2070 
2072  $oRequiredAddressFields = oxNew('oxRequiredAddressFields');
2073 
2075  $oFieldsValidator = oxNew('oxRequiredFieldsValidator');
2076  $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getBillingFields());
2077  $blFieldsValid = $oFieldsValidator->validateFields($oUser);
2078 
2080  $oDeliveryAddress = $this->getDelAddressInfo();
2081  if ($blFieldsValid && $oDeliveryAddress) {
2082  $sDeliveryAddress .= $oDeliveryAddress->getEncodedDeliveryAddress();
2083 
2084  $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getDeliveryFields());
2085  $blFieldsValid = $oFieldsValidator->validateFields($oDeliveryAddress);
2086  }
2087 
2088  $iState = 0;
2089  if ($sDelAddressMD5 != $sDeliveryAddress || !$blFieldsValid) {
2091  }
2092 
2093  return $iState;
2094  }
2095 
2096 
2105  public function validateDelivery($oBasket)
2106  {
2107  // proceed with no delivery
2108  // used for other countries
2109  if ($oBasket->getPaymentId() == 'oxempty') {
2110  return;
2111  }
2112  $oDb = oxDb::getDb();
2113 
2114  $oDelSet = oxNew("oxdeliveryset");
2115  $sTable = $oDelSet->getViewName();
2116 
2117  $sQ = "select 1 from {$sTable} where {$sTable}.oxid=" .
2118  $oDb->quote($oBasket->getShippingId()) . " and " . $oDelSet->getSqlActiveSnippet();
2119 
2120  if (!$oDb->getOne($sQ, false, false)) {
2121  // throwing exception
2123  }
2124  }
2125 
2134  public function validatePayment($oBasket)
2135  {
2136  $paymentId = $oBasket->getPaymentId();
2137 
2138  if (!$this->isValidPaymentId($paymentId) || !$this->isValidPayment($oBasket)) {
2140  }
2141  }
2142 
2151  protected function _setTsProtection(oxBasket $oBasket)
2152  {
2153  // protection price
2154  if (($oTsProtectionCost = $oBasket->getCosts('oxtsprotection'))) {
2155  $this->oxorder__oxtsprotectcosts = new oxField($oTsProtectionCost->getBruttoPrice(), oxField::T_RAW);
2156  }
2157 
2158  // protection protduct id
2159  $this->oxorder__oxtsprotectid = new oxField($oBasket->getTsProductId(), oxField::T_RAW);
2160  }
2161 
2171  protected function _executeTsProtection(oxBasket $oBasket)
2172  {
2173  $aValues['tsProductId'] = $this->oxorder__oxtsprotectid->value;
2174  $aValues['amount'] = $oBasket->getTsInsuredSum();
2175  $oCur = $this->getConfig()->getActShopCurrencyObject();
2176  $aValues['currency'] = $oCur->name;
2177  $aValues['buyerEmail'] = $this->oxorder__oxbillemail->value;
2178  $aValues['shopCustomerID'] = $this->oxorder__oxuserid->value;
2179  $aValues['shopOrderID'] = $this->oxorder__oxordernr->value;
2180  $aValues['orderDate'] = $this->oxorder__oxorderdate->value;
2181  $sPaymentId = $oBasket->getPaymentId();
2182 
2184  $oTsProtection = oxNew('oxtsprotection');
2185  $oTsProtection->requestForTsProtection($aValues, $sPaymentId);
2186 
2187  return true;
2188  }
2189 
2195  public function getFormattedTotalNetSum()
2196  {
2197  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalnetsum->value, $this->getOrderCurrency());
2198  }
2199 
2205  public function getFormattedTotalBrutSum()
2206  {
2207  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalbrutsum->value, $this->getOrderCurrency());
2208  }
2209 
2215  public function getFormattedDeliveryCost()
2216  {
2217  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxdelcost->value, $this->getOrderCurrency());
2218  }
2219 
2227  public function getFormattedeliveryCost()
2228  {
2229  return $this->getFormattedDeliveryCost();
2230  }
2231 
2237  public function getFormattedPayCost()
2238  {
2239  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxpaycost->value, $this->getOrderCurrency());
2240  }
2241 
2247  public function getFormattedWrapCost()
2248  {
2249  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxwrapcost->value, $this->getOrderCurrency());
2250  }
2251 
2257  public function getFormattedGiftCardCost()
2258  {
2259  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxgiftcardcost->value, $this->getOrderCurrency());
2260  }
2261 
2267  public function getFormattedTotalVouchers()
2268  {
2269  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxvoucherdiscount->value, $this->getOrderCurrency());
2270  }
2271 
2277  public function getFormattedDiscount()
2278  {
2279  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxdiscount->value, $this->getOrderCurrency());
2280  }
2281 
2287  public function getFormattedTotalOrderSum()
2288  {
2289  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalordersum->value, $this->getOrderCurrency());
2290  }
2291 
2297  public function getTrackCode()
2298  {
2299  return $this->oxorder__oxtrackcode->value;
2300  }
2301 
2307  public function getShipmentTrackingUrl()
2308  {
2309  $oConfig = oxRegistry::getConfig();
2310  if ($this->_sShipTrackUrl === null) {
2311  $sParcelService = $oConfig->getConfigParam('sParcelService');
2312  $sTrackingCode = $this->getTrackCode();
2313  if ($sParcelService && $sTrackingCode) {
2314  $this->_sShipTrackUrl = str_replace("##ID##", $sTrackingCode, $sParcelService);
2315  }
2316  }
2317 
2318  return $this->_sShipTrackUrl;
2319  }
2320 
2328  private function isValidPaymentId($paymentId)
2329  {
2330  // We force reading from master to prevent issues with slow replications or open transactions (see ESDEV-3804).
2331  $db = oxDb::getDb();
2332 
2333  $paymentModel = oxNew("oxpayment");
2334  $tableName = $paymentModel->getViewName();
2335 
2336  $sql = "
2337  select
2338  1
2339  from
2340  {$tableName}
2341  where
2342  {$tableName}.oxid = {$db->quote($paymentId)}
2343  and {$paymentModel->getSqlActiveSnippet()}
2344  ";
2345 
2346  return (bool) $db->getOne($sql);
2347  }
2348 
2356  private function isValidPayment($basket)
2357  {
2358  $paymentId = $basket->getPaymentId();
2359  $paymentModel = oxNew("oxpayment");
2360  $paymentModel->load($paymentId);
2361 
2362  $dynamicValues = $this->getDynamicValues();
2363  $shopId = $this->getConfig()->getShopId();
2364 
2365  return $paymentModel->isValidPayment(
2366  $dynamicValues,
2367  $shopId,
2368  $this->getUser(),
2369  $basket->getPriceForPayment(),
2370  $basket->getShippingId()
2371  );
2372  }
2373 
2377  private function getDynamicValues()
2378  {
2379  $dynamicValues = $this->getSession()->getVariable('dynvalue');
2380 
2381  if (!$dynamicValues) {
2382  $dynamicValues = oxRegistry::getConfig()->getRequestParameter('dynvalue');
2383  }
2384 
2385  if (!$dynamicValues && $this->getPaymentType()) {
2386  $dynamicValues = $this->getDynamicValuesFromPaymentType();
2387  }
2388 
2389  return $dynamicValues;
2390  }
2391 
2396  {
2397  $dynamicValues = null;
2398  $dynamicValuesList = $this->getPaymentType()->getDynValues();
2399 
2400  if (is_array($dynamicValuesList)) {
2401  foreach ($dynamicValuesList as $value) {
2402  $dynamicValues[$value->name] = $value->value;
2403  }
2404  }
2405 
2406  return $dynamicValues;
2407  }
2408 }