OXID eShop CE  4.10.6
 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  // copying payment info fields
991  $aDynvalue = oxRegistry::getSession()->getVariable('dynvalue');
992  $aDynvalue = $aDynvalue ? $aDynvalue : oxRegistry::getConfig()->getRequestParameter('dynvalue');
993 
994  // loading payment object
995  $oPayment = oxNew('oxpayment');
996 
997  if (!$oPayment->load($sPaymentid)) {
998  return null;
999  }
1000 
1001  // #756M Preserve already stored payment information
1002  if (!$aDynvalue && ($oUserpayment = $this->getPaymentType())) {
1003  if (is_array($aStoredDynvalue = $oUserpayment->getDynValues())) {
1004  foreach ($aStoredDynvalue as $oVal) {
1005  $aDynvalue[$oVal->name] = $oVal->value;
1006  }
1007  }
1008  }
1009 
1010  $oPayment->setDynValues(oxRegistry::getUtils()->assignValuesFromText($oPayment->oxpayments__oxvaldesc->value));
1011 
1012  // collecting dynamic values
1013  $aDynVal = array();
1014 
1015  if (is_array($aPaymentDynValues = $oPayment->getDynValues())) {
1016  foreach ($aPaymentDynValues as $key => $oVal) {
1017  if (isset($aDynvalue[$oVal->name])) {
1018  $oVal->value = $aDynvalue[$oVal->name];
1019  }
1020 
1021  //$oPayment->setDynValue($key, $oVal);
1022  $aPaymentDynValues[$key] = $oVal;
1023  $aDynVal[$oVal->name] = $oVal->value;
1024  }
1025  }
1026 
1027  // Store this payment information, we might allow users later to
1028  // reactivate already give payment information
1029 
1030  $oUserpayment = oxNew('oxuserpayment');
1031  $oUserpayment->oxuserpayments__oxuserid = clone $this->oxorder__oxuserid;
1032  $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
1033  $oUserpayment->oxuserpayments__oxvalue = new oxField(oxRegistry::getUtils()->assignValuesToText($aDynVal), oxField::T_RAW);
1034  $oUserpayment->oxpayments__oxdesc = clone $oPayment->oxpayments__oxdesc;
1035  $oUserpayment->oxpayments__oxlongdesc = clone $oPayment->oxpayments__oxlongdesc;
1036  $oUserpayment->setDynValues($aPaymentDynValues);
1037  $oUserpayment->save();
1038 
1039  // storing payment information to order
1040  $this->oxorder__oxpaymentid = new oxField($oUserpayment->getId(), oxField::T_RAW);
1041  $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
1042 
1043  // returning user payment object which will be used later in code ...
1044  return $oUserpayment;
1045  }
1046 
1050  protected function _setFolder()
1051  {
1052  $myConfig = $this->getConfig();
1053  $this->oxorder__oxfolder = new oxField(key($myConfig->getShopConfVar('aOrderfolder', $myConfig->getShopId())), oxField::T_RAW);
1054  }
1055 
1063  protected function _updateWishlist($aArticleList, $oUser)
1064  {
1065 
1066  foreach ($aArticleList as $oContent) {
1067  if (($sWishId = $oContent->getWishId())) {
1068 
1069  // checking which wishlist user uses ..
1070  if ($sWishId == $oUser->getId()) {
1071  $oUserBasket = $oUser->getBasket('wishlist');
1072  } else {
1073  $aWhere = array('oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist');
1074  $oUserBasket = oxNew('oxuserbasket');
1075  $oUserBasket->assignRecord($oUserBasket->buildSelectString($aWhere));
1076  }
1077 
1078  // updating users wish list
1079  if ($oUserBasket) {
1080  if (!($sProdId = $oContent->getWishArticleId())) {
1081  $sProdId = $oContent->getProductId();
1082  }
1083  $oUserBasketItem = $oUserBasket->getItem($sProdId, $oContent->getSelList());
1084  $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
1085  if ($dNewAmount < 0) {
1086  $dNewAmount = 0;
1087  }
1088  $oUserBasket->addItemToBasket($sProdId, $dNewAmount, $oContent->getSelList(), true);
1089  }
1090  }
1091  }
1092  }
1093 
1101  protected function _updateNoticeList($aArticleList, $oUser)
1102  {
1103  /*
1104  * #6141
1105  * If there is no noticelist, don't create an empty one.
1106  * Because loading the list via $user->getBasket('noticelist') will create it if there isn't one, but it will
1107  * 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.
1108  * If no then it was just created and will cause a new row in oxuserbaskets without content in oxuserbasketitems.
1109  * Also it will prevent creating a row for guests.
1110  */
1111  if ($oUser->getBasket('noticelist')->oxuserbaskets__oxid->value === null) {
1112  return;
1113  }
1114 
1115  // loading users notice list ..
1116  if ($oUserBasket = $oUser->getBasket('noticelist')) {
1117  // only if wishlist is enabled
1118  foreach ($aArticleList as $oContent) {
1119  $sProdId = $oContent->getProductId();
1120 
1121  // updating users notice list
1122  $oUserBasketItem = $oUserBasket->getItem($sProdId, $oContent->getSelList(), $oContent->getPersParams());
1123  $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
1124  if ($dNewAmount < 0) {
1125  $dNewAmount = 0;
1126  }
1127  $oUserBasket->addItemToBasket($sProdId, $dNewAmount, $oContent->getSelList(), true, $oContent->getPersParams());
1128  }
1129  }
1130  }
1131 
1135  protected function _updateOrderDate()
1136  {
1137  $oDb = oxDb::getDb();
1138  $sDate = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
1139  $sQ = 'update oxorder set oxorderdate=' . $oDb->quote($sDate) . ' where oxid=' . $oDb->quote($this->getId());
1140  $this->oxorder__oxorderdate = new oxField($sDate, oxField::T_RAW);
1141  $oDb->execute($sQ);
1142  }
1143 
1151  protected function _markVouchers($oBasket, $oUser)
1152  {
1153  $this->_aVoucherList = $oBasket->getVouchers();
1154 
1155  if (is_array($this->_aVoucherList)) {
1156  foreach ($this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
1157  $oVoucher = oxNew('oxvoucher');
1158  $oVoucher->load($sVoucherId);
1159  $oVoucher->markAsUsed($this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->dVoucherdiscount);
1160 
1161  $this->_aVoucherList[$sVoucherId] = $oVoucher;
1162  }
1163  }
1164  }
1165 
1171  public function save()
1172  {
1173  if (($blSave = parent::save())) {
1174 
1175  // saving order articles
1176  $oOrderArticles = $this->getOrderArticles();
1177  if ($oOrderArticles && count($oOrderArticles) > 0) {
1178  foreach ($oOrderArticles as $oOrderArticle) {
1179  $oOrderArticle->save();
1180  }
1181  }
1182  }
1183 
1184  return $blSave;
1185  }
1186 
1193  public function getDelAddressInfo()
1194  {
1195  $oDelAdress = null;
1196  if (!($soxAddressId = oxRegistry::getConfig()->getRequestParameter('deladrid'))) {
1197  $soxAddressId = oxRegistry::getSession()->getVariable('deladrid');
1198  }
1199  if ($soxAddressId) {
1200  $oDelAdress = oxNew('oxaddress');
1201  $oDelAdress->load($soxAddressId);
1202 
1203  //get delivery country name from delivery country id
1204  if ($oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1) {
1205  $oCountry = oxNew('oxcountry');
1206  $oCountry->load($oDelAdress->oxaddress__oxcountryid->value);
1207  $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
1208  }
1209  }
1210 
1211  return $oDelAdress;
1212  }
1213 
1222  public function validateStock($oBasket)
1223  {
1224  foreach ($oBasket->getContents() as $key => $oContent) {
1225  try {
1226  $oProd = $oContent->getArticle(true, null, true);
1227  } catch (oxNoArticleException $oEx) {
1228  $oBasket->removeItem($key);
1229  throw $oEx;
1230  } catch (oxArticleInputException $oEx) {
1231  $oBasket->removeItem($key);
1232  throw $oEx;
1233  }
1234 
1235  // check if its still available
1236  $dArtStockAmount = $oBasket->getArtStockInBasket($oProd->getId(), $key);
1237  $iOnStock = $oProd->checkForStock($oContent->getAmount(), $dArtStockAmount);
1238  if ($iOnStock !== true) {
1240  $oEx = oxNew('oxOutOfStockException');
1241  $oEx->setMessage('ERROR_MESSAGE_OUTOFSTOCK_OUTOFSTOCK');
1242  $oEx->setArticleNr($oProd->oxarticles__oxartnum->value);
1243  $oEx->setProductId($oProd->getId());
1244  $oEx->setBasketIndex($key);
1245 
1246  if (!is_numeric($iOnStock)) {
1247  $iOnStock = 0;
1248  }
1249  $oEx->setRemainingAmount($iOnStock);
1250  throw $oEx;
1251  }
1252  }
1253  }
1254 
1260  protected function _insert()
1261  {
1262  $myConfig = $this->getConfig();
1263  $oUtilsDate = oxRegistry::get("oxUtilsDate");
1264 
1265  //V #M525 orderdate must be the same as it was
1266  if (!$this->oxorder__oxorderdate->value) {
1267  $this->oxorder__oxorderdate = new oxField(date('Y-m-d H:i:s', $oUtilsDate->getTime()), oxField::T_RAW);
1268  } else {
1269  $this->oxorder__oxorderdate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxorderdate->value, true));
1270  }
1271 
1272  $this->oxorder__oxshopid = new oxField($myConfig->getShopId(), oxField::T_RAW);
1273  $this->oxorder__oxsenddate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxsenddate->value, true));
1274 
1275  $blInsert = parent::_insert();
1276 
1277  return $blInsert;
1278  }
1279 
1285  protected function _getCounterIdent()
1286  {
1287  $sCounterIdent = ($this->_blSeparateNumbering) ? 'oxOrder_' . $this->getConfig()->getShopId() : 'oxOrder';
1288 
1289  return $sCounterIdent;
1290  }
1291 
1292 
1298  protected function _setNumber()
1299  {
1300  $oDb = oxDb::getDb();
1301 
1302  $iCnt = oxNew('oxCounter')->getNext($this->_getCounterIdent());
1303  $sQ = "update oxorder set oxordernr = ? where oxid = ?";
1304  $blUpdate = ( bool ) $oDb->execute($sQ, array($iCnt, $this->getId()));
1305 
1306  if ($blUpdate) {
1307  $this->oxorder__oxordernr = new oxField($iCnt);
1308  }
1309 
1310  return $blUpdate;
1311  }
1312 
1318  protected function _update()
1319  {
1320  $this->_aSkipSaveFields = array('oxtimestamp', 'oxorderdate');
1321  $this->oxorder__oxsenddate = new oxField(oxRegistry::get("oxUtilsDate")->formatDBDate($this->oxorder__oxsenddate->value, true));
1322 
1323  return parent::_update();
1324  }
1325 
1334  public function delete($sOxId = null)
1335  {
1336  if ($sOxId) {
1337  if (!$this->load($sOxId)) {
1338  // such order does not exist
1339  return false;
1340  }
1341  } elseif (!$sOxId) {
1342  $sOxId = $this->getId();
1343  }
1344 
1345  // no order id is passed
1346  if (!$sOxId) {
1347  return false;
1348  }
1349 
1350 
1351  // delete order articles
1352  $oOrderArticles = $this->getOrderArticles(false);
1353  foreach ($oOrderArticles as $oOrderArticle) {
1354  $oOrderArticle->delete();
1355  }
1356 
1357  // #440 - deleting user payment info
1358  if ($oPaymentType = $this->getPaymentType()) {
1359  $oPaymentType->delete();
1360  }
1361 
1362  return parent::delete($sOxId);
1363  }
1364 
1374  public function recalculateOrder($aNewArticles = array())
1375  {
1376  oxDb::getDb()->startTransaction();
1377 
1378  try {
1379  $oBasket = $this->_getOrderBasket();
1380 
1381  // add this order articles to virtual basket and recalculates basket
1382  $this->_addOrderArticlesToBasket($oBasket, $this->getOrderArticles(true));
1383 
1384  // adding new articles to existing order
1385  $this->_addArticlesToBasket($oBasket, $aNewArticles);
1386 
1387  // recalculating basket
1388  $oBasket->calculateBasket(true);
1389 
1390  //finalizing order (skipping payment execution, vouchers marking and mail sending)
1391  $iRet = $this->finalizeOrder($oBasket, $this->getOrderUser(), true);
1392 
1393  //if finalizing order failed, rollback transaction
1394  if ($iRet !== 1) {
1395  oxDb::getDb()->rollbackTransaction();
1396  } else {
1397  oxDb::getDb()->commitTransaction();
1398  }
1399 
1400  } catch (Exception $oE) {
1401  // if exception, rollBack everything
1402  oxDb::getDb()->rollbackTransaction();
1403 
1404  if (defined('OXID_PHP_UNIT')) {
1405  throw $oE;
1406  }
1407  }
1408  }
1409 
1410  protected $_oOrderBasket = null;
1411 
1419  protected function _getOrderBasket($blStockCheck = true)
1420  {
1421  $this->_oOrderBasket = oxNew("oxBasket");
1422  $this->_oOrderBasket->enableSaveToDataBase(false);
1423 
1424  //setting recalculation mode
1425  $this->_oOrderBasket->setCalculationModeNetto($this->isNettoMode());
1426 
1427  // setting stock check mode
1428  $this->_oOrderBasket->setStockCheckMode($blStockCheck);
1429 
1430  // setting virtual basket user
1431  $this->_oOrderBasket->setBasketUser($this->getOrderUser());
1432 
1433  // transferring order id
1434  $this->_oOrderBasket->setOrderId($this->getId());
1435 
1436  // setting basket currency order uses
1437  $aCurrencies = $this->getConfig()->getCurrencyArray();
1438  foreach ($aCurrencies as $oCur) {
1439  if ($oCur->name == $this->oxorder__oxcurrency->value) {
1440  $oBasketCur = $oCur;
1441  break;
1442  }
1443  }
1444 
1445  // setting currency
1446  $this->_oOrderBasket->setBasketCurrency($oBasketCur);
1447 
1448  // set basket card id and message
1449  $this->_oOrderBasket->setCardId($this->oxorder__oxcardid->value);
1450  $this->_oOrderBasket->setCardMessage($this->oxorder__oxcardtext->value);
1451 
1452  if ($this->_blReloadDiscount) {
1454  // disabling availability check
1455  $this->_oOrderBasket->setSkipVouchersChecking(true);
1456 
1457  // add previously used vouchers
1458  $sQ = 'select oxid from oxvouchers where oxorderid = ' . $oDb->quote($this->getId());
1459  $aVouchers = $oDb->getAll($sQ);
1460  foreach ($aVouchers as $aVoucher) {
1461  $this->_oOrderBasket->addVoucher($aVoucher['oxid']);
1462  }
1463  } else {
1464  $this->_oOrderBasket->setDiscountCalcMode(false);
1465  $this->_oOrderBasket->setVoucherDiscount($this->oxorder__oxvoucherdiscount->value);
1466  $this->_oOrderBasket->setTotalDiscount($this->oxorder__oxdiscount->value);
1467  }
1468 
1469  // must be kept old delivery?
1470  if (!$this->_blReloadDelivery) {
1471  $this->_oOrderBasket->setDeliveryPrice($this->getOrderDeliveryPrice());
1472  } else {
1473  // set shipping
1474  $this->_oOrderBasket->setShipping($this->oxorder__oxdeltype->value);
1475  $this->_oOrderBasket->setDeliveryPrice(null);
1476  }
1477 
1478  //set basket payment
1479  $this->_oOrderBasket->setPayment($this->oxorder__oxpaymenttype->value);
1480 
1481  return $this->_oOrderBasket;
1482  }
1483 
1490  public function setDelivery($sDeliveryId)
1491  {
1492  $this->reloadDelivery(true);
1493  $this->oxorder__oxdeltype = new oxField($sDeliveryId);
1494  }
1495 
1501  public function getOrderUser()
1502  {
1503  if ($this->_oUser === null) {
1504  $this->_oUser = oxNew("oxuser");
1505  $this->_oUser->load($this->oxorder__oxuserid->value);
1506 
1507  // if object is loaded then reusing its order info
1508  if ($this->_isLoaded) {
1509  // bill address
1510  $this->_oUser->oxuser__oxcompany = clone $this->oxorder__oxbillcompany;
1511  $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
1512  $this->_oUser->oxuser__oxfname = clone $this->oxorder__oxbillfname;
1513  $this->_oUser->oxuser__oxlname = clone $this->oxorder__oxbilllname;
1514  $this->_oUser->oxuser__oxstreet = clone $this->oxorder__oxbillstreet;
1515  $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
1516  $this->_oUser->oxuser__oxaddinfo = clone $this->oxorder__oxbilladdinfo;
1517  $this->_oUser->oxuser__oxustid = clone $this->oxorder__oxbillustid;
1518 
1519 
1520  $this->_oUser->oxuser__oxcity = clone $this->oxorder__oxbillcity;
1521  $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
1522  $this->_oUser->oxuser__oxstateid = clone $this->oxorder__oxbillstateid;
1523  $this->_oUser->oxuser__oxzip = clone $this->oxorder__oxbillzip;
1524  $this->_oUser->oxuser__oxfon = clone $this->oxorder__oxbillfon;
1525  $this->_oUser->oxuser__oxfax = clone $this->oxorder__oxbillfax;
1526  $this->_oUser->oxuser__oxsal = clone $this->oxorder__oxbillsal;
1527  }
1528  }
1529 
1530  return $this->_oUser;
1531  }
1532 
1538  public function pdfFooter($oPdf)
1539  {
1540  }
1541 
1547  public function pdfHeaderplus($oPdf)
1548  {
1549  }
1550 
1556  public function pdfHeader($oPdf)
1557  {
1558  }
1559 
1566  public function genPdf($sFilename, $iSelLang = 0)
1567  {
1568  }
1569 
1575  public function getInvoiceNum()
1576  {
1577  $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "' . $this->getConfig()->getShopId() . '" ';
1578 
1579  return (( int ) oxDb::getDb()->getOne($sQ, false) + 1);
1580  }
1581 
1587  public function getNextBillNum()
1588  {
1589  $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "' . $this->getConfig()->getShopId() . '" ';
1590 
1591  return (( int ) oxDb::getDb()->getOne($sQ, false) + 1);
1592  }
1593 
1599  public function getShippingSetList()
1600  {
1601  // in which country we deliver
1602  if (!($sShipId = $this->oxorder__oxdelcountryid->value)) {
1603  $sShipId = $this->oxorder__oxbillcountryid->value;
1604  }
1605 
1606  $oBasket = $this->_getOrderBasket(false);
1607 
1608  // unsetting bundles
1609  $oOrderArticles = $this->getOrderArticles();
1610  foreach ($oOrderArticles as $sItemId => $oItem) {
1611  if ($oItem->isBundle()) {
1612  $oOrderArticles->offsetUnset($sItemId);
1613  }
1614  }
1615 
1616  // add this order articles to basket and recalculate basket
1617  $this->_addOrderArticlesToBasket($oBasket, $oOrderArticles);
1618 
1619  // recalculating basket
1620  $oBasket->calculateBasket(true);
1621 
1622  // load fitting deliveries list
1623  $oDeliveryList = oxNew("oxDeliveryList", "core");
1624  $oDeliveryList->setCollectFittingDeliveriesSets(true);
1625 
1626  return $oDeliveryList->getDeliveryList($oBasket, $this->getOrderUser(), $sShipId);
1627  }
1628 
1634  public function getVoucherNrList()
1635  {
1637  $aVouchers = array();
1638  $sSelect = "select oxvouchernr from oxvouchers where oxorderid = " . $oDb->quote($this->oxorder__oxid->value);
1639  $rs = $oDb->select($sSelect);
1640  if ($rs != false && $rs->recordCount() > 0) {
1641  while (!$rs->EOF) {
1642  $aVouchers[] = $rs->fields['oxvouchernr'];
1643  $rs->moveNext();
1644  }
1645  }
1646 
1647  return $aVouchers;
1648  }
1649 
1657  public function getOrderSum($blToday = false)
1658  {
1659  $sSelect = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
1660  $sSelect .= 'oxshopid = "' . $this->getConfig()->getShopId() . '" and oxorder.oxstorno != "1" ';
1661 
1662  if ($blToday) {
1663  $sSelect .= 'and oxorderdate like "' . date('Y-m-d') . '%" ';
1664  }
1665 
1666  return ( double ) oxDb::getDb()->getOne($sSelect, false, false);
1667  }
1668 
1676  public function getOrderCnt($blToday = false)
1677  {
1678  $sSelect = 'select count(*) from oxorder where ';
1679  $sSelect .= 'oxshopid = "' . $this->getConfig()->getShopId() . '" and oxorder.oxstorno != "1" ';
1680 
1681  if ($blToday) {
1682  $sSelect .= 'and oxorderdate like "' . date('Y-m-d') . '%" ';
1683  }
1684 
1685  return ( int ) oxDb::getDb()->getOne($sSelect, false, false);
1686  }
1687 
1688 
1696  protected function _checkOrderExist($sOxId = null)
1697  {
1698  if (!$sOxId) {
1699  return false;
1700  }
1701 
1702  $oDb = oxDb::getDb();
1703  if ($oDb->getOne('select oxid from oxorder where oxid = ' . $oDb->quote($sOxId), false, false)) {
1704  return true;
1705  }
1706 
1707  return false;
1708  }
1709 
1719  protected function _sendOrderByEmail($oUser = null, $oBasket = null, $oPayment = null)
1720  {
1722 
1723  // add user, basket and payment to order
1724  $this->_oUser = $oUser;
1725  $this->_oBasket = $oBasket;
1726  $this->_oPayment = $oPayment;
1727 
1728  $oxEmail = oxNew('oxemail');
1729 
1730  // send order email to user
1731  if ($oxEmail->sendOrderEMailToUser($this)) {
1732  // mail to user was successfully sent
1733  $iRet = self::ORDER_STATE_OK;
1734  }
1735 
1736  // send order email to shop owner
1737  $oxEmail->sendOrderEMailToOwner($this);
1738 
1739  return $iRet;
1740  }
1741 
1747  public function getBasket()
1748  {
1749  return $this->_oBasket;
1750  }
1751 
1757  public function getPayment()
1758  {
1759  return $this->_oPayment;
1760  }
1761 
1767  public function getVoucherList()
1768  {
1769  return $this->_aVoucherList;
1770  }
1771 
1777  public function getDelSet()
1778  {
1779  if ($this->_oDelSet == null) {
1780  // load deliveryset info
1781  $this->_oDelSet = oxNew('oxdeliveryset');
1782  $this->_oDelSet->load($this->oxorder__oxdeltype->value);
1783  }
1784 
1785  return $this->_oDelSet;
1786  }
1787 
1793  public function getPaymentType()
1794  {
1795  if ($this->oxorder__oxpaymentid->value && $this->_oPaymentType === null) {
1796  $this->_oPaymentType = false;
1797  $oPaymentType = oxNew('oxuserpayment');
1798  if ($oPaymentType->load($this->oxorder__oxpaymentid->value)) {
1799  $this->_oPaymentType = $oPaymentType;
1800  }
1801  }
1802 
1803  return $this->_oPaymentType;
1804  }
1805 
1811  public function getGiftCard()
1812  {
1813  if ($this->oxorder__oxcardid->value && $this->_oGiftCard == null) {
1814  $this->_oGiftCard = oxNew('oxwrapping');
1815  $this->_oGiftCard->load($this->oxorder__oxcardid->value);
1816  }
1817 
1818  return $this->_oGiftCard;
1819  }
1820 
1826  public function setSeparateNumbering($blSeparateNumbering = null)
1827  {
1828  $this->_blSeparateNumbering = $blSeparateNumbering;
1829  }
1830 
1838  public function getLastUserPaymentType($sUserId)
1839  {
1840  $oDb = oxDb::getDb();
1841  $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="' . $this->getConfig()->getShopId() . '" and oxorder.oxuserid=' . $oDb->quote($sUserId) . ' order by oxorder.oxorderdate desc ';
1842  $sLastPaymentId = $oDb->getOne($sQ, false, false);
1843 
1844  return $sLastPaymentId;
1845  }
1846 
1853  protected function _addOrderArticlesToBasket($oBasket, $aOrderArticles)
1854  {
1855  // if no order articles, return empty basket
1856  if (count($aOrderArticles) > 0) {
1857 
1858  //adding order articles to basket
1859  foreach ($aOrderArticles as $oOrderArticle) {
1860  $oBasket->addOrderArticleToBasket($oOrderArticle);
1861  }
1862  }
1863  }
1864 
1871  protected function _addArticlesToBasket($oBasket, $aArticles)
1872  {
1873  // if no order articles
1874  if (count($aArticles) > 0) {
1875 
1876  //adding order articles to basket
1877  foreach ($aArticles as $oArticle) {
1878  $aSel = isset($oArticle->oxorderarticles__oxselvariant) ? $oArticle->oxorderarticles__oxselvariant->value : null;
1879  $aPersParam = isset($oArticle->oxorderarticles__oxpersparam) ? $oArticle->getPersParams() : null;
1880  $oBasket->addToBasket(
1881  $oArticle->oxorderarticles__oxartid->value,
1882  $oArticle->oxorderarticles__oxamount->value,
1883  $aSel, $aPersParam
1884  );
1885  }
1886  }
1887  }
1888 
1894  public function getTotalOrderSum()
1895  {
1896  $oCur = $this->getConfig()->getActShopCurrencyObject();
1897 
1898  return number_format((double) $this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
1899  }
1900 
1908  public function getProductVats($blFormatCurrency = true)
1909  {
1910  $aVats = array();
1911  if ($this->oxorder__oxartvat1->value) {
1912  $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
1913  }
1914  if ($this->oxorder__oxartvat2->value) {
1915  $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
1916  }
1917 
1918  if ($blFormatCurrency) {
1919  $oLang = oxRegistry::getLang();
1920  $oCur = $this->getConfig()->getActShopCurrencyObject();
1921  foreach ($aVats as $sKey => $dVat) {
1922  $aVats[$sKey] = $oLang->formatCurrency($dVat, $oCur);
1923  }
1924  }
1925 
1926  return $aVats;
1927  }
1928 
1934  public function getBillCountry()
1935  {
1936  if (!$this->oxorder__oxbillcountry->value) {
1937  $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle($this->oxorder__oxbillcountryid->value));
1938  }
1939 
1940  return $this->oxorder__oxbillcountry;
1941  }
1942 
1948  public function getDelCountry()
1949  {
1950  if (!$this->oxorder__oxdelcountry->value) {
1951  $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle($this->oxorder__oxdelcountryid->value));
1952  }
1953 
1954  return $this->oxorder__oxdelcountry;
1955  }
1956 
1962  public function reloadDelivery($blReload)
1963  {
1964  $this->_blReloadDelivery = $blReload;
1965  }
1966 
1972  public function reloadDiscount($blReload)
1973  {
1974  $this->_blReloadDiscount = $blReload;
1975  }
1976 
1980  public function cancelOrder()
1981  {
1982  $this->oxorder__oxstorno = new oxField(1);
1983  if ($this->save()) {
1984 
1985 
1986  // canceling ordered products
1987  foreach ($this->getOrderArticles() as $oOrderArticle) {
1988 
1989 
1990  $oOrderArticle->cancelOrderArticle();
1991  }
1992  }
1993  }
1994 
2001  public function getOrderCurrency()
2002  {
2003  if ($this->_oOrderCurrency === null) {
2004 
2005  // setting default in case unrecognized currency was set during order
2006  $aCurrencies = $this->getConfig()->getCurrencyArray();
2007  $this->_oOrderCurrency = current($aCurrencies);
2008 
2009  foreach ($aCurrencies as $oCurr) {
2010  if ($oCurr->name == $this->oxorder__oxcurrency->value) {
2011  $this->_oOrderCurrency = $oCurr;
2012  break;
2013  }
2014  }
2015  }
2016 
2017  return $this->_oOrderCurrency;
2018  }
2019 
2029  public function validateOrder($oBasket, $oUser)
2030  {
2031  // validating stock
2032  $iValidState = $this->validateStock($oBasket);
2033 
2034  if (!$iValidState) {
2035  // validating delivery
2036  $iValidState = $this->validateDelivery($oBasket);
2037  }
2038 
2039  if (!$iValidState) {
2040  // validating payment
2041  $iValidState = $this->validatePayment($oBasket);
2042  }
2043 
2044  if (!$iValidState) {
2045  //0003110 validating delivery address, it is not be changed during checkout process
2046  $iValidState = $this->validateDeliveryAddress($oUser);
2047  }
2048 
2049  if (!$iValidState) {
2050  // validating minimum price
2051  $iValidState = $this->validateBasket($oBasket);
2052  }
2053 
2054  return $iValidState;
2055  }
2056 
2064  public function validateBasket($oBasket)
2065  {
2066  return $oBasket->isBelowMinOrderPrice() ? self::ORDER_STATE_BELOWMINPRICE : null;
2067  }
2068 
2077  public function validateDeliveryAddress($oUser)
2078  {
2079  $sDelAddressMD5 = $this->getConfig()->getRequestParameter('sDeliveryAddressMD5');
2080 
2081  $sDeliveryAddress = $oUser->getEncodedDeliveryAddress();
2082 
2084  $oRequiredAddressFields = oxNew('oxRequiredAddressFields');
2085 
2087  $oFieldsValidator = oxNew('oxRequiredFieldsValidator');
2088  $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getBillingFields());
2089  $blFieldsValid = $oFieldsValidator->validateFields($oUser);
2090 
2092  $oDeliveryAddress = $this->getDelAddressInfo();
2093  if ($blFieldsValid && $oDeliveryAddress) {
2094  $sDeliveryAddress .= $oDeliveryAddress->getEncodedDeliveryAddress();
2095 
2096  $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getDeliveryFields());
2097  $blFieldsValid = $oFieldsValidator->validateFields($oDeliveryAddress);
2098  }
2099 
2100  $iState = 0;
2101  if ($sDelAddressMD5 != $sDeliveryAddress || !$blFieldsValid) {
2103  }
2104 
2105  return $iState;
2106  }
2107 
2108 
2117  public function validateDelivery($oBasket)
2118  {
2119  // proceed with no delivery
2120  // used for other countries
2121  if ($oBasket->getPaymentId() == 'oxempty') {
2122  return;
2123  }
2124  $oDb = oxDb::getDb();
2125 
2126  $oDelSet = oxNew("oxdeliveryset");
2127  $sTable = $oDelSet->getViewName();
2128 
2129  $sQ = "select 1 from {$sTable} where {$sTable}.oxid=" .
2130  $oDb->quote($oBasket->getShippingId()) . " and " . $oDelSet->getSqlActiveSnippet();
2131 
2132  if (!$oDb->getOne($sQ, false, false)) {
2133  // throwing exception
2135  }
2136  }
2137 
2146  public function validatePayment($oBasket)
2147  {
2148  $oDb = oxDb::getDb();
2149 
2150  $oPayment = oxNew("oxpayment");
2151  $sTable = $oPayment->getViewName();
2152 
2153  $sQ = "select 1 from {$sTable} where {$sTable}.oxid=" .
2154  $oDb->quote($oBasket->getPaymentId()) . " and " . $oPayment->getSqlActiveSnippet();
2155 
2156  if (!$oDb->getOne($sQ, false, false)) {
2158  }
2159  }
2160 
2169  protected function _setTsProtection(oxBasket $oBasket)
2170  {
2171  // protection price
2172  if (($oTsProtectionCost = $oBasket->getCosts('oxtsprotection'))) {
2173  $this->oxorder__oxtsprotectcosts = new oxField($oTsProtectionCost->getBruttoPrice(), oxField::T_RAW);
2174  }
2175 
2176  // protection protduct id
2177  $this->oxorder__oxtsprotectid = new oxField($oBasket->getTsProductId(), oxField::T_RAW);
2178  }
2179 
2189  protected function _executeTsProtection(oxBasket $oBasket)
2190  {
2191  $aValues['tsProductId'] = $this->oxorder__oxtsprotectid->value;
2192  $aValues['amount'] = $oBasket->getTsInsuredSum();
2193  $oCur = $this->getConfig()->getActShopCurrencyObject();
2194  $aValues['currency'] = $oCur->name;
2195  $aValues['buyerEmail'] = $this->oxorder__oxbillemail->value;
2196  $aValues['shopCustomerID'] = $this->oxorder__oxuserid->value;
2197  $aValues['shopOrderID'] = $this->oxorder__oxordernr->value;
2198  $aValues['orderDate'] = $this->oxorder__oxorderdate->value;
2199  $sPaymentId = $oBasket->getPaymentId();
2200 
2202  $oTsProtection = oxNew('oxtsprotection');
2203  $oTsProtection->requestForTsProtection($aValues, $sPaymentId);
2204 
2205  return true;
2206  }
2207 
2213  public function getFormattedTotalNetSum()
2214  {
2215  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalnetsum->value, $this->getOrderCurrency());
2216  }
2217 
2223  public function getFormattedTotalBrutSum()
2224  {
2225  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalbrutsum->value, $this->getOrderCurrency());
2226  }
2227 
2233  public function getFormattedDeliveryCost()
2234  {
2235  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxdelcost->value, $this->getOrderCurrency());
2236  }
2237 
2245  public function getFormattedeliveryCost()
2246  {
2247  return $this->getFormattedDeliveryCost();
2248  }
2249 
2255  public function getFormattedPayCost()
2256  {
2257  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxpaycost->value, $this->getOrderCurrency());
2258  }
2259 
2265  public function getFormattedWrapCost()
2266  {
2267  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxwrapcost->value, $this->getOrderCurrency());
2268  }
2269 
2275  public function getFormattedGiftCardCost()
2276  {
2277  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxgiftcardcost->value, $this->getOrderCurrency());
2278  }
2279 
2285  public function getFormattedTotalVouchers()
2286  {
2287  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxvoucherdiscount->value, $this->getOrderCurrency());
2288  }
2289 
2295  public function getFormattedDiscount()
2296  {
2297  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxdiscount->value, $this->getOrderCurrency());
2298  }
2299 
2305  public function getFormattedTotalOrderSum()
2306  {
2307  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalordersum->value, $this->getOrderCurrency());
2308  }
2309 
2315  public function getTrackCode()
2316  {
2317  return $this->oxorder__oxtrackcode->value;
2318  }
2319 
2325  public function getShipmentTrackingUrl()
2326  {
2327  $oConfig = oxRegistry::getConfig();
2328  if ($this->_sShipTrackUrl === null) {
2329  $sParcelService = $oConfig->getConfigParam('sParcelService');
2330  $sTrackingCode = $this->getTrackCode();
2331  if ($sParcelService && $sTrackingCode) {
2332  $this->_sShipTrackUrl = str_replace("##ID##", $sTrackingCode, $sParcelService);
2333  }
2334  }
2335 
2336  return $this->_sShipTrackUrl;
2337  }
2338 }