OXID eShop CE  4.10.4
 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 
60 
67 
74 
80  protected $_aSkipSaveFields = array('oxtimestamp');
81 
87  protected $_oArticles = null;
88 
94  protected $_oDelSet = null;
95 
101  protected $_oGiftCard = null;
102 
108  protected $_oPaymentType = null;
109 
115  protected $_oPayment = null;
116 
122  protected $_aVoucherList = null;
123 
129  protected $_oDelPrice = null;
130 
136  protected $_oUser = null;
137 
143  protected $_oBasket = null;
144 
150  protected $_oWrappingPrice = null;
151 
157  protected $_oGiftCardPrice = null;
158 
164  protected $_oPaymentPrice = null;
165 
171  protected $_oTsProtectionPrice = null;
172 
178  protected $_sClassName = 'oxorder';
179 
185  protected $_blSeparateNumbering = null;
186 
192  protected $_iOrderLang = null;
193 
199  protected $_blReloadDelivery = true;
200 
206  protected $_blReloadDiscount = true;
207 
213  protected $_oOrderCurrency = null;
214 
220  protected $_oOrderFiles = null;
221 
227  protected $_sShipTrackUrl = null;
228 
232  public function __construct()
233  {
235  $this->init('oxorder');
236 
237  // set usage of separate orders numbering for different shops
238  $this->setSeparateNumbering($this->getConfig()->getConfigParam('blSeparateNumbering'));
239 
240  }
241 
249  public function __get($sName)
250  {
251  if ($sName == 'oDelSet') {
252  return $this->getDelSet();
253  }
254 
255  if ($sName == 'oxorder__oxbillcountry') {
256  return $this->getBillCountry();
257  }
258 
259  if ($sName == 'oxorder__oxdelcountry') {
260  return $this->getDelCountry();
261  }
262  }
263 
271  public function assign($dbRecord)
272  {
273 
274  parent::assign($dbRecord);
275 
276  $oUtilsDate = oxRegistry::get("oxUtilsDate");
277 
278  // convert date's to international format
279  $this->oxorder__oxorderdate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxorderdate->value));
280  $this->oxorder__oxsenddate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxsenddate->value));
281  }
282 
290  protected function _getCountryTitle($sCountryId)
291  {
292  $sTitle = null;
293  if ($sCountryId && $sCountryId != '-1') {
294  $oCountry = oxNew('oxcountry');
295  $oCountry->loadInLang($this->getOrderLanguage(), $sCountryId);
296  $sTitle = $oCountry->oxcountry__oxtitle->value;
297  }
298 
299  return $sTitle;
300  }
301 
309  protected function _getArticles($blExcludeCanceled = false)
310  {
311  $sSelect = "SELECT `oxorderarticles`.* FROM `oxorderarticles`
312  WHERE `oxorderarticles`.`oxorderid` = '" . $this->getId() . "'" .
313  ($blExcludeCanceled ? " AND `oxorderarticles`.`oxstorno` != 1 " : " ") . "
314  ORDER BY `oxorderarticles`.`oxartid`, `oxorderarticles`.`oxselvariant`, `oxorderarticles`.`oxpersparam` ";
315 
316  // order articles
317  $oArticles = oxNew('oxlist');
318  $oArticles->init('oxorderarticle');
319  $oArticles->selectString($sSelect);
320 
321  return $oArticles;
322  }
323 
331  public function getOrderArticles($blExcludeCanceled = false)
332  {
333  // checking set value
334  if ($blExcludeCanceled) {
335 
336  return $this->_getArticles(true);
337 
338  } elseif ($this->_oArticles === null) {
339  $this->_oArticles = $this->_getArticles();
340  }
341 
342  return $this->_oArticles;
343  }
344 
350  public function setOrderArticleList($oOrderArticleList)
351  {
352  $this->_oArticles = $oOrderArticleList;
353  }
354 
360  public function getOrderDeliveryPrice()
361  {
362  if ($this->_oDelPrice != null) {
363  return $this->_oDelPrice;
364  }
365 
366  $this->_oDelPrice = oxNew('oxprice');
367  $this->_oDelPrice->setBruttoPriceMode();
368  $this->_oDelPrice->setPrice($this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value);
369 
370  return $this->_oDelPrice;
371  }
372 
378  public function getOrderWrappingPrice()
379  {
380  if ($this->_oWrappingPrice != null) {
381  return $this->_oWrappingPrice;
382  }
383 
384  $this->_oWrappingPrice = oxNew('oxprice');
385  $this->_oWrappingPrice->setBruttoPriceMode();
386  $this->_oWrappingPrice->setPrice($this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value);
387 
388  return $this->_oWrappingPrice;
389  }
390 
396  public function getOrderGiftCardPrice()
397  {
398  if ($this->_oGidtCardPrice != null) {
399  return $this->_oGidtCardPrice;
400  }
401 
402  $this->_oGidtCardPrice = oxNew('oxprice');
403  $this->_oGidtCardPrice->setBruttoPriceMode();
404  $this->_oGidtCardPrice->setPrice($this->oxorder__oxgiftcardcost->value, $this->oxorder__oxgiftcardvat->value);
405 
406  return $this->_oGidtCardPrice;
407  }
408 
409 
415  public function getOrderPaymentPrice()
416  {
417  if ($this->_oPaymentPrice != null) {
418  return $this->_oPaymentPrice;
419  }
420 
421  $this->_oPaymentPrice = oxNew('oxprice');
422  $this->_oPaymentPrice->setBruttoPriceMode();
423  $this->_oPaymentPrice->setPrice($this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value);
424 
425  return $this->_oPaymentPrice;
426  }
427 
433  public function getOrderTsProtectionPrice()
434  {
435  if ($this->_oTsProtectionPrice != null) {
437  }
438 
439  $this->_oTsProtectionPrice = oxNew('oxprice');
440  $this->_oTsProtectionPrice->setBruttoPriceMode();
441  $this->_oTsProtectionPrice->setPrice($this->oxorder__oxtsprotectcosts->value, $this->getConfig()->getConfigParam('dDefaultVAT'));
442 
444  }
445 
451  public function getOrderNetSum()
452  {
453  $dTotalNetSum = 0;
454 
455  $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
456  $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
457  $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
458  $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
459 
460  return $dTotalNetSum;
461  }
462 
483  public function finalizeOrder(oxBasket $oBasket, $oUser, $blRecalculatingOrder = false)
484  {
485  // check if this order is already stored
486  $sGetChallenge = oxRegistry::getSession()->getVariable('sess_challenge');
487  if ($this->_checkOrderExist($sGetChallenge)) {
488  oxRegistry::getUtils()->logger('BLOCKER');
489 
490  // we might use this later, this means that somebody klicked like mad on order button
492  }
493 
494  // if not recalculating order, use sess_challenge id, else leave old order id
495  if (!$blRecalculatingOrder) {
496  // use this ID
497  $this->setId($sGetChallenge);
498 
499  // validating various order/basket parameters before finalizing
500  if ($iOrderState = $this->validateOrder($oBasket, $oUser)) {
501  return $iOrderState;
502  }
503  }
504 
505  // copies user info
506  $this->_setUser($oUser);
507 
508  // copies basket info
509  $this->_loadFromBasket($oBasket);
510 
511  // payment information
512  $oUserPayment = $this->_setPayment($oBasket->getPaymentId());
513 
514  // set folder information, if order is new
515  // #M575 in recalculating order case folder must be the same as it was
516  if (!$blRecalculatingOrder) {
517  $this->_setFolder();
518  }
519 
520  // marking as not finished
521  $this->_setOrderStatus('NOT_FINISHED');
522 
523  //saving all order data to DB
524  $this->save();
525 
526  // executing payment (on failure deletes order and returns error code)
527  // in case when recalculating order, payment execution is skipped
528  if (!$blRecalculatingOrder) {
529  $blRet = $this->_executePayment($oBasket, $oUserPayment);
530  if ($blRet !== true) {
531  return $blRet;
532  }
533  }
534 
535  if (!$this->oxorder__oxordernr->value) {
536  $this->_setNumber();
537  } else {
538  oxNew('oxCounter')->update($this->_getCounterIdent(), $this->oxorder__oxordernr->value);
539  }
540 
541  // executing TS protection
542  if (!$blRecalculatingOrder && $oBasket->getTsProductId()) {
543  $blRet = $this->_executeTsProtection($oBasket);
544  if ($blRet !== true) {
545  return $blRet;
546  }
547  }
548 
549  // deleting remark info only when order is finished
550  oxRegistry::getSession()->deleteVariable('ordrem');
551  oxRegistry::getSession()->deleteVariable('stsprotection');
552 
553  //#4005: Order creation time is not updated when order processing is complete
554  if (!$blRecalculatingOrder) {
555  $this->_updateOrderDate();
556  }
557 
558  // updating order trans status (success status)
559  $this->_setOrderStatus('OK');
560 
561  // store orderid
562  $oBasket->setOrderId($this->getId());
563 
564  // updating wish lists
565  $this->_updateWishlist($oBasket->getContents(), $oUser);
566 
567  // updating users notice list
568  $this->_updateNoticeList($oBasket->getContents(), $oUser);
569 
570  // marking vouchers as used and sets them to $this->_aVoucherList (will be used in order email)
571  // skipping this action in case of order recalculation
572  if (!$blRecalculatingOrder) {
573  $this->_markVouchers($oBasket, $oUser);
574  }
575 
576  // send order by email to shop owner and current user
577  // skipping this action in case of order recalculation
578  if (!$blRecalculatingOrder) {
579  $iRet = $this->_sendOrderByEmail($oUser, $oBasket, $oUserPayment);
580  } else {
581  $iRet = self::ORDER_STATE_OK;
582  }
583 
584  return $iRet;
585  }
586 
592  public function isNettoMode()
593  {
594  return (bool) $this->oxorder__oxisnettomode->value;
595  }
596 
597 
603  protected function _setOrderStatus($sStatus)
604  {
605  $oDb = oxDb::getDb();
606  $sQ = 'update oxorder set oxtransstatus=' . $oDb->quote($sStatus) . ' where oxid=' . $oDb->quote($this->getId());
607  $oDb->execute($sQ);
608 
609  //updating order object
610  $this->oxorder__oxtransstatus = new oxField($sStatus, oxField::T_RAW);
611  }
612 
620  protected function _convertVat($sVat)
621  {
622  if (strpos($sVat, '.') < strpos($sVat, ',')) {
623  $sVat = str_replace(array('.', ','), array('', '.'), $sVat);
624  } else {
625  $sVat = str_replace(',', '', $sVat);
626  }
627 
628  return (float) $sVat;
629  }
630 
634  protected function _resetVats()
635  {
636  $this->oxorder__oxartvat1 = new oxField(null);
637  $this->oxorder__oxartvatprice1 = new oxField(null);
638  $this->oxorder__oxartvat2 = new oxField(null);
639  $this->oxorder__oxartvatprice2 = new oxField(null);
640  }
641 
650  protected function _loadFromBasket(oxBasket $oBasket)
651  {
652  $myConfig = $this->getConfig();
653 
654  // store IP Address - default must be FALSE as it is illegal to store
655  if ($myConfig->getConfigParam('blStoreIPs') && $this->oxorder__oxip->value === null) {
656  $this->oxorder__oxip = new oxField(oxRegistry::get("oxUtilsServer")->getRemoteAddress(), oxField::T_RAW);
657  }
658 
659  //setting view mode
660  $this->oxorder__oxisnettomode = new oxField($oBasket->isCalculationModeNetto());
661 
662  // copying main price info
663  $this->oxorder__oxtotalnetsum = new oxField($oBasket->getNettoSum());
664  $this->oxorder__oxtotalbrutsum = new oxField($oBasket->getBruttoSum());
665  $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
666 
667  // copying discounted VAT info
668  $this->_resetVats();
669  $iVatIndex = 1;
670  foreach ($oBasket->getProductVats(false) as $iVat => $dPrice) {
671  $this->{"oxorder__oxartvat$iVatIndex"} = new oxField($this->_convertVat($iVat), oxField::T_RAW);
672  $this->{"oxorder__oxartvatprice$iVatIndex"} = new oxField($dPrice, oxField::T_RAW);
673  $iVatIndex++;
674  }
675 
676  // payment costs if available
677  if (($oPaymentCost = $oBasket->getCosts('oxpayment'))) {
678  $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
679  $this->oxorder__oxpayvat = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
680  }
681 
682  // delivery info
683  if (($oDeliveryCost = $oBasket->getCosts('oxdelivery'))) {
684  $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
685  //V #M382: Save VAT, not VAT value for delivery costs
686  $this->oxorder__oxdelvat = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW); //V #M382
687  $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
688  }
689 
690  // user remark
691  if (!isset($this->oxorder__oxremark) || $this->oxorder__oxremark->value === null) {
692  $this->oxorder__oxremark = new oxField(oxRegistry::getSession()->getVariable('ordrem'), oxField::T_RAW);
693  }
694 
695  // currency
696  $oCur = $myConfig->getActShopCurrencyObject();
697  $this->oxorder__oxcurrency = new oxField($oCur->name);
698  $this->oxorder__oxcurrate = new oxField($oCur->rate, oxField::T_RAW);
699 
700  // store voucher discount
701  if (($oVoucherDiscount = $oBasket->getVoucherDiscount())) {
702  $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
703  }
704 
705  // general discount
706  if ($this->_blReloadDiscount) {
707  $dDiscount = 0;
708  $aDiscounts = $oBasket->getDiscounts();
709  if (count($aDiscounts) > 0) {
710  foreach ($aDiscounts as $oDiscount) {
711  $dDiscount += $oDiscount->dDiscount;
712  }
713  }
714  $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
715  }
716 
717  //order language
718  $this->oxorder__oxlang = new oxField($this->getOrderLanguage());
719 
720 
721  // initial status - 'ERROR'
722  $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
723 
724  // copies basket product info ...
725  $this->_setOrderArticles($oBasket->getContents());
726 
727  // copies wrapping info
728  $this->_setWrapping($oBasket);
729 
730  // copies TS protection info
731  $this->_setTsProtection($oBasket);
732  }
733 
740  public function getOrderLanguage()
741  {
742  if ($this->_iOrderLang === null) {
743  if (isset($this->oxorder__oxlang->value)) {
744  $this->_iOrderLang = oxRegistry::getLang()->validateLanguage($this->oxorder__oxlang->value);
745  } else {
746  $this->_iOrderLang = oxRegistry::getLang()->getBaseLanguage();
747  }
748  }
749 
750  return $this->_iOrderLang;
751  }
752 
758  protected function _setUser($oUser)
759  {
760 
761  $this->oxorder__oxuserid = new oxField($oUser->getId());
762 
763  // bill address
764  $this->oxorder__oxbillcompany = clone $oUser->oxuser__oxcompany;
765  $this->oxorder__oxbillemail = clone $oUser->oxuser__oxusername;
766  $this->oxorder__oxbillfname = clone $oUser->oxuser__oxfname;
767  $this->oxorder__oxbilllname = clone $oUser->oxuser__oxlname;
768  $this->oxorder__oxbillstreet = clone $oUser->oxuser__oxstreet;
769  $this->oxorder__oxbillstreetnr = clone $oUser->oxuser__oxstreetnr;
770  $this->oxorder__oxbilladdinfo = clone $oUser->oxuser__oxaddinfo;
771  $this->oxorder__oxbillustid = clone $oUser->oxuser__oxustid;
772  $this->oxorder__oxbillcity = clone $oUser->oxuser__oxcity;
773  $this->oxorder__oxbillcountryid = clone $oUser->oxuser__oxcountryid;
774  $this->oxorder__oxbillstateid = clone $oUser->oxuser__oxstateid;
775  $this->oxorder__oxbillzip = clone $oUser->oxuser__oxzip;
776  $this->oxorder__oxbillfon = clone $oUser->oxuser__oxfon;
777  $this->oxorder__oxbillfax = clone $oUser->oxuser__oxfax;
778  $this->oxorder__oxbillsal = clone $oUser->oxuser__oxsal;
779 
780 
781  // delivery address
782  if (($oDelAdress = $this->getDelAddressInfo())) {
783  // set delivery address
784  $this->oxorder__oxdelcompany = clone $oDelAdress->oxaddress__oxcompany;
785  $this->oxorder__oxdelfname = clone $oDelAdress->oxaddress__oxfname;
786  $this->oxorder__oxdellname = clone $oDelAdress->oxaddress__oxlname;
787  $this->oxorder__oxdelstreet = clone $oDelAdress->oxaddress__oxstreet;
788  $this->oxorder__oxdelstreetnr = clone $oDelAdress->oxaddress__oxstreetnr;
789  $this->oxorder__oxdeladdinfo = clone $oDelAdress->oxaddress__oxaddinfo;
790  $this->oxorder__oxdelcity = clone $oDelAdress->oxaddress__oxcity;
791  $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
792  $this->oxorder__oxdelstateid = clone $oDelAdress->oxaddress__oxstateid;
793  $this->oxorder__oxdelzip = clone $oDelAdress->oxaddress__oxzip;
794  $this->oxorder__oxdelfon = clone $oDelAdress->oxaddress__oxfon;
795  $this->oxorder__oxdelfax = clone $oDelAdress->oxaddress__oxfax;
796  $this->oxorder__oxdelsal = clone $oDelAdress->oxaddress__oxsal;
797  }
798  }
799 
805  protected function _setWrapping(oxBasket $oBasket)
806  {
807  $myConfig = $this->getConfig();
808 
809  // wrapping price
810  if (($oWrappingCost = $oBasket->getCosts('oxwrapping'))) {
811  $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
812  // wrapping VAT will be always calculated (#3757)
813  $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
814  }
815 
816  if (($oGiftCardCost = $oBasket->getCosts('oxgiftcard'))) {
817  $this->oxorder__oxgiftcardcost = new oxField($oGiftCardCost->getBruttoPrice(), oxField::T_RAW);
818  $this->oxorder__oxgiftcardvat = new oxField($oGiftCardCost->getVAT(), oxField::T_RAW);
819  }
820 
821  // greetings card
822  $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
823 
824  // card text will be stored in database
825  $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
826  }
827 
834  protected function _setOrderArticles($aArticleList)
835  {
836  // reset articles list
837  $this->_oArticles = oxNew('oxlist');
838  $iCurrLang = $this->getOrderLanguage();
839 
840  // add all the products we have on basket to the order
841  foreach ($aArticleList as $oContent) {
842 
843  //$oContent->oProduct = $oContent->getArticle();
844  // #M773 Do not use article lazy loading on order save
845  $oProduct = $oContent->getArticle(true, null, true);
846 
847  // copy only if object is oxarticle type
848  if ($oProduct->isOrderArticle()) {
849  $oOrderArticle = $oProduct;
850  } else {
851 
852  // if order language doe not match product language - article must be reloaded in order language
853  if ($iCurrLang != $oProduct->getLanguage()) {
854  $oProduct->loadInLang($iCurrLang, $oProduct->getProductId());
855  }
856 
857  // set chosen select list
858  $sSelList = '';
859  if (count($aChosenSelList = $oContent->getChosenSelList())) {
860  foreach ($aChosenSelList as $oItem) {
861  if ($sSelList) {
862  $sSelList .= ", ";
863  }
864  $sSelList .= "{$oItem->name} : {$oItem->value}";
865  }
866  }
867 
868  $oOrderArticle = oxNew('oxorderarticle');
869  $oOrderArticle->setIsNewOrderItem(true);
870  $oOrderArticle->copyThis($oProduct);
871  $oOrderArticle->setId();
872 
873  $oOrderArticle->oxorderarticles__oxartnum = clone $oProduct->oxarticles__oxartnum;
874  $oOrderArticle->oxorderarticles__oxselvariant = new oxField(trim($sSelList . ' ' . $oProduct->oxarticles__oxvarselect->getRawValue()), oxField::T_RAW);
875  $oOrderArticle->oxorderarticles__oxshortdesc = new oxField($oProduct->oxarticles__oxshortdesc->getRawValue(), oxField::T_RAW);
876  // #M974: duplicated entries for the name of variants in orders
877  $oOrderArticle->oxorderarticles__oxtitle = new oxField(trim($oProduct->oxarticles__oxtitle->getRawValue()), oxField::T_RAW);
878 
879  // copying persistent parameters ...
880  if (!is_array($aPersParams = $oProduct->getPersParams())) {
881  $aPersParams = $oContent->getPersParams();
882  }
883  if (is_array($aPersParams) && count($aPersParams)) {
884  $oOrderArticle->oxorderarticles__oxpersparam = new oxField(serialize($aPersParams), oxField::T_RAW);
885  }
886  }
887 
888  // ids, titles, numbers ...
889  $oOrderArticle->oxorderarticles__oxorderid = new oxField($this->getId());
890  $oOrderArticle->oxorderarticles__oxartid = new oxField($oContent->getProductId());
891  $oOrderArticle->oxorderarticles__oxamount = new oxField($oContent->getAmount());
892 
893  // prices
894  $oPrice = $oContent->getPrice();
895  $oOrderArticle->oxorderarticles__oxnetprice = new oxField($oPrice->getNettoPrice(), oxField::T_RAW);
896  $oOrderArticle->oxorderarticles__oxvatprice = new oxField($oPrice->getVatValue(), oxField::T_RAW);
897  $oOrderArticle->oxorderarticles__oxbrutprice = new oxField($oPrice->getBruttoPrice(), oxField::T_RAW);
898  $oOrderArticle->oxorderarticles__oxvat = new oxField($oPrice->getVat(), oxField::T_RAW);
899 
900  $oUnitPtice = $oContent->getUnitPrice();
901  $oOrderArticle->oxorderarticles__oxnprice = new oxField($oUnitPtice->getNettoPrice(), oxField::T_RAW);
902  $oOrderArticle->oxorderarticles__oxbprice = new oxField($oUnitPtice->getBruttoPrice(), oxField::T_RAW);
903 
904  // wrap id
905  $oOrderArticle->oxorderarticles__oxwrapid = new oxField($oContent->getWrappingId(), oxField::T_RAW);
906 
907  // items shop id
908  $oOrderArticle->oxorderarticles__oxordershopid = new oxField($oContent->getShopId(), oxField::T_RAW);
909 
910  // bundle?
911  $oOrderArticle->oxorderarticles__oxisbundle = new oxField($oContent->isBundle());
912 
913  // add information for eMail
914  //P
915  //TODO: check if this assign is needed at all
916  $oOrderArticle->oProduct = $oProduct;
917 
918  $oOrderArticle->setArticle($oProduct);
919 
920  // simulation order article list
921  $this->_oArticles->offsetSet($oOrderArticle->getId(), $oOrderArticle);
922  }
923  }
924 
936  protected function _executePayment(oxBasket $oBasket, $oUserpayment)
937  {
938  $oPayTransaction = $this->_getGateway();
939  $oPayTransaction->setPaymentParams($oUserpayment);
940 
941  if (!$oPayTransaction->executePayment($oBasket->getPrice()->getBruttoPrice(), $this)) {
942  $this->delete();
943 
944  // checking for error messages
945  if (method_exists($oPayTransaction, 'getLastError')) {
946  if (($sLastError = $oPayTransaction->getLastError())) {
947  return $sLastError;
948  }
949  }
950 
951  // checking for error codes
952  if (method_exists($oPayTransaction, 'getLastErrorNo')) {
953  if (($iLastErrorNo = $oPayTransaction->getLastErrorNo())) {
954  return $iLastErrorNo;
955  }
956  }
957 
958  return self::ORDER_STATE_PAYMENTERROR; // means no authentication
959  }
960 
961  return true; // everything fine
962  }
963 
970  protected function _getGateway()
971  {
972  return oxNew('oxPaymentGateway');
973  }
974 
982  protected function _setPayment($sPaymentid)
983  {
984  // copying payment info fields
985  $aDynvalue = oxRegistry::getSession()->getVariable('dynvalue');
986  $aDynvalue = $aDynvalue ? $aDynvalue : oxRegistry::getConfig()->getRequestParameter('dynvalue');
987 
988  // loading payment object
989  $oPayment = oxNew('oxpayment');
990 
991  if (!$oPayment->load($sPaymentid)) {
992  return null;
993  }
994 
995  // #756M Preserve already stored payment information
996  if (!$aDynvalue && ($oUserpayment = $this->getPaymentType())) {
997  if (is_array($aStoredDynvalue = $oUserpayment->getDynValues())) {
998  foreach ($aStoredDynvalue as $oVal) {
999  $aDynvalue[$oVal->name] = $oVal->value;
1000  }
1001  }
1002  }
1003 
1004  $oPayment->setDynValues(oxRegistry::getUtils()->assignValuesFromText($oPayment->oxpayments__oxvaldesc->value));
1005 
1006  // collecting dynamic values
1007  $aDynVal = array();
1008 
1009  if (is_array($aPaymentDynValues = $oPayment->getDynValues())) {
1010  foreach ($aPaymentDynValues as $key => $oVal) {
1011  if (isset($aDynvalue[$oVal->name])) {
1012  $oVal->value = $aDynvalue[$oVal->name];
1013  }
1014 
1015  //$oPayment->setDynValue($key, $oVal);
1016  $aPaymentDynValues[$key] = $oVal;
1017  $aDynVal[$oVal->name] = $oVal->value;
1018  }
1019  }
1020 
1021  // Store this payment information, we might allow users later to
1022  // reactivate already give payment information
1023 
1024  $oUserpayment = oxNew('oxuserpayment');
1025  $oUserpayment->oxuserpayments__oxuserid = clone $this->oxorder__oxuserid;
1026  $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
1027  $oUserpayment->oxuserpayments__oxvalue = new oxField(oxRegistry::getUtils()->assignValuesToText($aDynVal), oxField::T_RAW);
1028  $oUserpayment->oxpayments__oxdesc = clone $oPayment->oxpayments__oxdesc;
1029  $oUserpayment->oxpayments__oxlongdesc = clone $oPayment->oxpayments__oxlongdesc;
1030  $oUserpayment->setDynValues($aPaymentDynValues);
1031  $oUserpayment->save();
1032 
1033  // storing payment information to order
1034  $this->oxorder__oxpaymentid = new oxField($oUserpayment->getId(), oxField::T_RAW);
1035  $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
1036 
1037  // returning user payment object which will be used later in code ...
1038  return $oUserpayment;
1039  }
1040 
1044  protected function _setFolder()
1045  {
1046  $myConfig = $this->getConfig();
1047  $this->oxorder__oxfolder = new oxField(key($myConfig->getShopConfVar('aOrderfolder', $myConfig->getShopId())), oxField::T_RAW);
1048  }
1049 
1057  protected function _updateWishlist($aArticleList, $oUser)
1058  {
1059 
1060  foreach ($aArticleList as $oContent) {
1061  if (($sWishId = $oContent->getWishId())) {
1062 
1063  // checking which wishlist user uses ..
1064  if ($sWishId == $oUser->getId()) {
1065  $oUserBasket = $oUser->getBasket('wishlist');
1066  } else {
1067  $aWhere = array('oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist');
1068  $oUserBasket = oxNew('oxuserbasket');
1069  $oUserBasket->assignRecord($oUserBasket->buildSelectString($aWhere));
1070  }
1071 
1072  // updating users wish list
1073  if ($oUserBasket) {
1074  if (!($sProdId = $oContent->getWishArticleId())) {
1075  $sProdId = $oContent->getProductId();
1076  }
1077  $oUserBasketItem = $oUserBasket->getItem($sProdId, $oContent->getSelList());
1078  $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
1079  if ($dNewAmount < 0) {
1080  $dNewAmount = 0;
1081  }
1082  $oUserBasket->addItemToBasket($sProdId, $dNewAmount, $oContent->getSelList(), true);
1083  }
1084  }
1085  }
1086  }
1087 
1095  protected function _updateNoticeList($aArticleList, $oUser)
1096  {
1097  /*
1098  * #6141
1099  * If there is no noticelist, don't create an empty one.
1100  * Because loading the list via $user->getBasket('noticelist') will create it if there isn't one, but it will
1101  * 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.
1102  * If no then it was just created and will cause a new row in oxuserbaskets without content in oxuserbasketitems.
1103  * Also it will prevent creating a row for guests.
1104  */
1105  if ($oUser->getBasket('noticelist')->oxuserbaskets__oxid->value === null) {
1106  return;
1107  }
1108 
1109  // loading users notice list ..
1110  if ($oUserBasket = $oUser->getBasket('noticelist')) {
1111  // only if wishlist is enabled
1112  foreach ($aArticleList as $oContent) {
1113  $sProdId = $oContent->getProductId();
1114 
1115  // updating users notice list
1116  $oUserBasketItem = $oUserBasket->getItem($sProdId, $oContent->getSelList(), $oContent->getPersParams());
1117  $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
1118  if ($dNewAmount < 0) {
1119  $dNewAmount = 0;
1120  }
1121  $oUserBasket->addItemToBasket($sProdId, $dNewAmount, $oContent->getSelList(), true, $oContent->getPersParams());
1122  }
1123  }
1124  }
1125 
1129  protected function _updateOrderDate()
1130  {
1131  $oDb = oxDb::getDb();
1132  $sDate = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
1133  $sQ = 'update oxorder set oxorderdate=' . $oDb->quote($sDate) . ' where oxid=' . $oDb->quote($this->getId());
1134  $this->oxorder__oxorderdate = new oxField($sDate, oxField::T_RAW);
1135  $oDb->execute($sQ);
1136  }
1137 
1145  protected function _markVouchers($oBasket, $oUser)
1146  {
1147  $this->_aVoucherList = $oBasket->getVouchers();
1148 
1149  if (is_array($this->_aVoucherList)) {
1150  foreach ($this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
1151  $oVoucher = oxNew('oxvoucher');
1152  $oVoucher->load($sVoucherId);
1153  $oVoucher->markAsUsed($this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->dVoucherdiscount);
1154 
1155  $this->_aVoucherList[$sVoucherId] = $oVoucher;
1156  }
1157  }
1158  }
1159 
1165  public function save()
1166  {
1167  if (($blSave = parent::save())) {
1168 
1169  // saving order articles
1170  $oOrderArticles = $this->getOrderArticles();
1171  if ($oOrderArticles && count($oOrderArticles) > 0) {
1172  foreach ($oOrderArticles as $oOrderArticle) {
1173  $oOrderArticle->save();
1174  }
1175  }
1176  }
1177 
1178  return $blSave;
1179  }
1180 
1187  public function getDelAddressInfo()
1188  {
1189  $oDelAdress = null;
1190  if (!($soxAddressId = oxRegistry::getConfig()->getRequestParameter('deladrid'))) {
1191  $soxAddressId = oxRegistry::getSession()->getVariable('deladrid');
1192  }
1193  if ($soxAddressId) {
1194  $oDelAdress = oxNew('oxaddress');
1195  $oDelAdress->load($soxAddressId);
1196 
1197  //get delivery country name from delivery country id
1198  if ($oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1) {
1199  $oCountry = oxNew('oxcountry');
1200  $oCountry->load($oDelAdress->oxaddress__oxcountryid->value);
1201  $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
1202  }
1203  }
1204 
1205  return $oDelAdress;
1206  }
1207 
1216  public function validateStock($oBasket)
1217  {
1218  foreach ($oBasket->getContents() as $key => $oContent) {
1219  try {
1220  $oProd = $oContent->getArticle(true, null, true);
1221  } catch (oxNoArticleException $oEx) {
1222  $oBasket->removeItem($key);
1223  throw $oEx;
1224  } catch (oxArticleInputException $oEx) {
1225  $oBasket->removeItem($key);
1226  throw $oEx;
1227  }
1228 
1229  // check if its still available
1230  $dArtStockAmount = $oBasket->getArtStockInBasket($oProd->getId(), $key);
1231  $iOnStock = $oProd->checkForStock($oContent->getAmount(), $dArtStockAmount);
1232  if ($iOnStock !== true) {
1234  $oEx = oxNew('oxOutOfStockException');
1235  $oEx->setMessage('ERROR_MESSAGE_OUTOFSTOCK_OUTOFSTOCK');
1236  $oEx->setArticleNr($oProd->oxarticles__oxartnum->value);
1237  $oEx->setProductId($oProd->getId());
1238  $oEx->setBasketIndex($key);
1239 
1240  if (!is_numeric($iOnStock)) {
1241  $iOnStock = 0;
1242  }
1243  $oEx->setRemainingAmount($iOnStock);
1244  throw $oEx;
1245  }
1246  }
1247  }
1248 
1254  protected function _insert()
1255  {
1256  $myConfig = $this->getConfig();
1257  $oUtilsDate = oxRegistry::get("oxUtilsDate");
1258 
1259  //V #M525 orderdate must be the same as it was
1260  if (!$this->oxorder__oxorderdate->value) {
1261  $this->oxorder__oxorderdate = new oxField(date('Y-m-d H:i:s', $oUtilsDate->getTime()), oxField::T_RAW);
1262  } else {
1263  $this->oxorder__oxorderdate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxorderdate->value, true));
1264  }
1265 
1266  $this->oxorder__oxshopid = new oxField($myConfig->getShopId(), oxField::T_RAW);
1267  $this->oxorder__oxsenddate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxsenddate->value, true));
1268 
1269  $blInsert = parent::_insert();
1270 
1271  return $blInsert;
1272  }
1273 
1279  protected function _getCounterIdent()
1280  {
1281  $sCounterIdent = ($this->_blSeparateNumbering) ? 'oxOrder_' . $this->getConfig()->getShopId() : 'oxOrder';
1282 
1283  return $sCounterIdent;
1284  }
1285 
1286 
1292  protected function _setNumber()
1293  {
1294  $oDb = oxDb::getDb();
1295 
1296  $iCnt = oxNew('oxCounter')->getNext($this->_getCounterIdent());
1297  $sQ = "update oxorder set oxordernr = ? where oxid = ?";
1298  $blUpdate = ( bool ) $oDb->execute($sQ, array($iCnt, $this->getId()));
1299 
1300  if ($blUpdate) {
1301  $this->oxorder__oxordernr = new oxField($iCnt);
1302  }
1303 
1304  return $blUpdate;
1305  }
1306 
1312  protected function _update()
1313  {
1314  $this->_aSkipSaveFields = array('oxtimestamp', 'oxorderdate');
1315  $this->oxorder__oxsenddate = new oxField(oxRegistry::get("oxUtilsDate")->formatDBDate($this->oxorder__oxsenddate->value, true));
1316 
1317  return parent::_update();
1318  }
1319 
1328  public function delete($sOxId = null)
1329  {
1330  if ($sOxId) {
1331  if (!$this->load($sOxId)) {
1332  // such order does not exist
1333  return false;
1334  }
1335  } elseif (!$sOxId) {
1336  $sOxId = $this->getId();
1337  }
1338 
1339  // no order id is passed
1340  if (!$sOxId) {
1341  return false;
1342  }
1343 
1344 
1345  // delete order articles
1346  $oOrderArticles = $this->getOrderArticles(false);
1347  foreach ($oOrderArticles as $oOrderArticle) {
1348  $oOrderArticle->delete();
1349  }
1350 
1351  // #440 - deleting user payment info
1352  if ($oPaymentType = $this->getPaymentType()) {
1353  $oPaymentType->delete();
1354  }
1355 
1356  return parent::delete($sOxId);
1357  }
1358 
1368  public function recalculateOrder($aNewArticles = array())
1369  {
1370  oxDb::getDb()->startTransaction();
1371 
1372  try {
1373  $oBasket = $this->_getOrderBasket();
1374 
1375  // add this order articles to virtual basket and recalculates basket
1376  $this->_addOrderArticlesToBasket($oBasket, $this->getOrderArticles(true));
1377 
1378  // adding new articles to existing order
1379  $this->_addArticlesToBasket($oBasket, $aNewArticles);
1380 
1381  // recalculating basket
1382  $oBasket->calculateBasket(true);
1383 
1384  //finalizing order (skipping payment execution, vouchers marking and mail sending)
1385  $iRet = $this->finalizeOrder($oBasket, $this->getOrderUser(), true);
1386 
1387  //if finalizing order failed, rollback transaction
1388  if ($iRet !== 1) {
1389  oxDb::getDb()->rollbackTransaction();
1390  } else {
1391  oxDb::getDb()->commitTransaction();
1392  }
1393 
1394  } catch (Exception $oE) {
1395  // if exception, rollBack everything
1396  oxDb::getDb()->rollbackTransaction();
1397 
1398  if (defined('OXID_PHP_UNIT')) {
1399  throw $oE;
1400  }
1401  }
1402  }
1403 
1404  protected $_oOrderBasket = null;
1405 
1413  protected function _getOrderBasket($blStockCheck = true)
1414  {
1415  $this->_oOrderBasket = oxNew("oxBasket");
1416  $this->_oOrderBasket->enableSaveToDataBase(false);
1417 
1418  //setting recalculation mode
1419  $this->_oOrderBasket->setCalculationModeNetto($this->isNettoMode());
1420 
1421  // setting stock check mode
1422  $this->_oOrderBasket->setStockCheckMode($blStockCheck);
1423 
1424  // setting virtual basket user
1425  $this->_oOrderBasket->setBasketUser($this->getOrderUser());
1426 
1427  // transferring order id
1428  $this->_oOrderBasket->setOrderId($this->getId());
1429 
1430  // setting basket currency order uses
1431  $aCurrencies = $this->getConfig()->getCurrencyArray();
1432  foreach ($aCurrencies as $oCur) {
1433  if ($oCur->name == $this->oxorder__oxcurrency->value) {
1434  $oBasketCur = $oCur;
1435  break;
1436  }
1437  }
1438 
1439  // setting currency
1440  $this->_oOrderBasket->setBasketCurrency($oBasketCur);
1441 
1442  // set basket card id and message
1443  $this->_oOrderBasket->setCardId($this->oxorder__oxcardid->value);
1444  $this->_oOrderBasket->setCardMessage($this->oxorder__oxcardtext->value);
1445 
1446  if ($this->_blReloadDiscount) {
1448  // disabling availability check
1449  $this->_oOrderBasket->setSkipVouchersChecking(true);
1450 
1451  // add previously used vouchers
1452  $sQ = 'select oxid from oxvouchers where oxorderid = ' . $oDb->quote($this->getId());
1453  $aVouchers = $oDb->getAll($sQ);
1454  foreach ($aVouchers as $aVoucher) {
1455  $this->_oOrderBasket->addVoucher($aVoucher['oxid']);
1456  }
1457  } else {
1458  $this->_oOrderBasket->setDiscountCalcMode(false);
1459  $this->_oOrderBasket->setVoucherDiscount($this->oxorder__oxvoucherdiscount->value);
1460  $this->_oOrderBasket->setTotalDiscount($this->oxorder__oxdiscount->value);
1461  }
1462 
1463  // must be kept old delivery?
1464  if (!$this->_blReloadDelivery) {
1465  $this->_oOrderBasket->setDeliveryPrice($this->getOrderDeliveryPrice());
1466  } else {
1467  // set shipping
1468  $this->_oOrderBasket->setShipping($this->oxorder__oxdeltype->value);
1469  $this->_oOrderBasket->setDeliveryPrice(null);
1470  }
1471 
1472  //set basket payment
1473  $this->_oOrderBasket->setPayment($this->oxorder__oxpaymenttype->value);
1474 
1475  return $this->_oOrderBasket;
1476  }
1477 
1484  public function setDelivery($sDeliveryId)
1485  {
1486  $this->reloadDelivery(true);
1487  $this->oxorder__oxdeltype = new oxField($sDeliveryId);
1488  }
1489 
1495  public function getOrderUser()
1496  {
1497  if ($this->_oUser === null) {
1498  $this->_oUser = oxNew("oxuser");
1499  $this->_oUser->load($this->oxorder__oxuserid->value);
1500 
1501  // if object is loaded then reusing its order info
1502  if ($this->_isLoaded) {
1503  // bill address
1504  $this->_oUser->oxuser__oxcompany = clone $this->oxorder__oxbillcompany;
1505  $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
1506  $this->_oUser->oxuser__oxfname = clone $this->oxorder__oxbillfname;
1507  $this->_oUser->oxuser__oxlname = clone $this->oxorder__oxbilllname;
1508  $this->_oUser->oxuser__oxstreet = clone $this->oxorder__oxbillstreet;
1509  $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
1510  $this->_oUser->oxuser__oxaddinfo = clone $this->oxorder__oxbilladdinfo;
1511  $this->_oUser->oxuser__oxustid = clone $this->oxorder__oxbillustid;
1512 
1513 
1514  $this->_oUser->oxuser__oxcity = clone $this->oxorder__oxbillcity;
1515  $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
1516  $this->_oUser->oxuser__oxstateid = clone $this->oxorder__oxbillstateid;
1517  $this->_oUser->oxuser__oxzip = clone $this->oxorder__oxbillzip;
1518  $this->_oUser->oxuser__oxfon = clone $this->oxorder__oxbillfon;
1519  $this->_oUser->oxuser__oxfax = clone $this->oxorder__oxbillfax;
1520  $this->_oUser->oxuser__oxsal = clone $this->oxorder__oxbillsal;
1521  }
1522  }
1523 
1524  return $this->_oUser;
1525  }
1526 
1532  public function pdfFooter($oPdf)
1533  {
1534  }
1535 
1541  public function pdfHeaderplus($oPdf)
1542  {
1543  }
1544 
1550  public function pdfHeader($oPdf)
1551  {
1552  }
1553 
1560  public function genPdf($sFilename, $iSelLang = 0)
1561  {
1562  }
1563 
1569  public function getInvoiceNum()
1570  {
1571  $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "' . $this->getConfig()->getShopId() . '" ';
1572 
1573  return (( int ) oxDb::getDb()->getOne($sQ, false) + 1);
1574  }
1575 
1581  public function getNextBillNum()
1582  {
1583  $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "' . $this->getConfig()->getShopId() . '" ';
1584 
1585  return (( int ) oxDb::getDb()->getOne($sQ, false) + 1);
1586  }
1587 
1593  public function getShippingSetList()
1594  {
1595  // in which country we deliver
1596  if (!($sShipId = $this->oxorder__oxdelcountryid->value)) {
1597  $sShipId = $this->oxorder__oxbillcountryid->value;
1598  }
1599 
1600  $oBasket = $this->_getOrderBasket(false);
1601 
1602  // unsetting bundles
1603  $oOrderArticles = $this->getOrderArticles();
1604  foreach ($oOrderArticles as $sItemId => $oItem) {
1605  if ($oItem->isBundle()) {
1606  $oOrderArticles->offsetUnset($sItemId);
1607  }
1608  }
1609 
1610  // add this order articles to basket and recalculate basket
1611  $this->_addOrderArticlesToBasket($oBasket, $oOrderArticles);
1612 
1613  // recalculating basket
1614  $oBasket->calculateBasket(true);
1615 
1616  // load fitting deliveries list
1617  $oDeliveryList = oxNew("oxDeliveryList", "core");
1618  $oDeliveryList->setCollectFittingDeliveriesSets(true);
1619 
1620  return $oDeliveryList->getDeliveryList($oBasket, $this->getOrderUser(), $sShipId);
1621  }
1622 
1628  public function getVoucherNrList()
1629  {
1631  $aVouchers = array();
1632  $sSelect = "select oxvouchernr from oxvouchers where oxorderid = " . $oDb->quote($this->oxorder__oxid->value);
1633  $rs = $oDb->select($sSelect);
1634  if ($rs != false && $rs->recordCount() > 0) {
1635  while (!$rs->EOF) {
1636  $aVouchers[] = $rs->fields['oxvouchernr'];
1637  $rs->moveNext();
1638  }
1639  }
1640 
1641  return $aVouchers;
1642  }
1643 
1651  public function getOrderSum($blToday = false)
1652  {
1653  $sSelect = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
1654  $sSelect .= 'oxshopid = "' . $this->getConfig()->getShopId() . '" and oxorder.oxstorno != "1" ';
1655 
1656  if ($blToday) {
1657  $sSelect .= 'and oxorderdate like "' . date('Y-m-d') . '%" ';
1658  }
1659 
1660  return ( double ) oxDb::getDb()->getOne($sSelect, false, false);
1661  }
1662 
1670  public function getOrderCnt($blToday = false)
1671  {
1672  $sSelect = 'select count(*) from oxorder where ';
1673  $sSelect .= 'oxshopid = "' . $this->getConfig()->getShopId() . '" and oxorder.oxstorno != "1" ';
1674 
1675  if ($blToday) {
1676  $sSelect .= 'and oxorderdate like "' . date('Y-m-d') . '%" ';
1677  }
1678 
1679  return ( int ) oxDb::getDb()->getOne($sSelect, false, false);
1680  }
1681 
1682 
1690  protected function _checkOrderExist($sOxId = null)
1691  {
1692  if (!$sOxId) {
1693  return false;
1694  }
1695 
1696  $oDb = oxDb::getDb();
1697  if ($oDb->getOne('select oxid from oxorder where oxid = ' . $oDb->quote($sOxId), false, false)) {
1698  return true;
1699  }
1700 
1701  return false;
1702  }
1703 
1713  protected function _sendOrderByEmail($oUser = null, $oBasket = null, $oPayment = null)
1714  {
1716 
1717  // add user, basket and payment to order
1718  $this->_oUser = $oUser;
1719  $this->_oBasket = $oBasket;
1720  $this->_oPayment = $oPayment;
1721 
1722  $oxEmail = oxNew('oxemail');
1723 
1724  // send order email to user
1725  if ($oxEmail->sendOrderEMailToUser($this)) {
1726  // mail to user was successfully sent
1727  $iRet = self::ORDER_STATE_OK;
1728  }
1729 
1730  // send order email to shop owner
1731  $oxEmail->sendOrderEMailToOwner($this);
1732 
1733  return $iRet;
1734  }
1735 
1741  public function getBasket()
1742  {
1743  return $this->_oBasket;
1744  }
1745 
1751  public function getPayment()
1752  {
1753  return $this->_oPayment;
1754  }
1755 
1761  public function getVoucherList()
1762  {
1763  return $this->_aVoucherList;
1764  }
1765 
1771  public function getDelSet()
1772  {
1773  if ($this->_oDelSet == null) {
1774  // load deliveryset info
1775  $this->_oDelSet = oxNew('oxdeliveryset');
1776  $this->_oDelSet->load($this->oxorder__oxdeltype->value);
1777  }
1778 
1779  return $this->_oDelSet;
1780  }
1781 
1787  public function getPaymentType()
1788  {
1789  if ($this->oxorder__oxpaymentid->value && $this->_oPaymentType === null) {
1790  $this->_oPaymentType = false;
1791  $oPaymentType = oxNew('oxuserpayment');
1792  if ($oPaymentType->load($this->oxorder__oxpaymentid->value)) {
1793  $this->_oPaymentType = $oPaymentType;
1794  }
1795  }
1796 
1797  return $this->_oPaymentType;
1798  }
1799 
1805  public function getGiftCard()
1806  {
1807  if ($this->oxorder__oxcardid->value && $this->_oGiftCard == null) {
1808  $this->_oGiftCard = oxNew('oxwrapping');
1809  $this->_oGiftCard->load($this->oxorder__oxcardid->value);
1810  }
1811 
1812  return $this->_oGiftCard;
1813  }
1814 
1820  public function setSeparateNumbering($blSeparateNumbering = null)
1821  {
1822  $this->_blSeparateNumbering = $blSeparateNumbering;
1823  }
1824 
1832  public function getLastUserPaymentType($sUserId)
1833  {
1834  $oDb = oxDb::getDb();
1835  $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="' . $this->getConfig()->getShopId() . '" and oxorder.oxuserid=' . $oDb->quote($sUserId) . ' order by oxorder.oxorderdate desc ';
1836  $sLastPaymentId = $oDb->getOne($sQ, false, false);
1837 
1838  return $sLastPaymentId;
1839  }
1840 
1847  protected function _addOrderArticlesToBasket($oBasket, $aOrderArticles)
1848  {
1849  // if no order articles, return empty basket
1850  if (count($aOrderArticles) > 0) {
1851 
1852  //adding order articles to basket
1853  foreach ($aOrderArticles as $oOrderArticle) {
1854  $oBasket->addOrderArticleToBasket($oOrderArticle);
1855  }
1856  }
1857  }
1858 
1865  protected function _addArticlesToBasket($oBasket, $aArticles)
1866  {
1867  // if no order articles
1868  if (count($aArticles) > 0) {
1869 
1870  //adding order articles to basket
1871  foreach ($aArticles as $oArticle) {
1872  $aSel = isset($oArticle->oxorderarticles__oxselvariant) ? $oArticle->oxorderarticles__oxselvariant->value : null;
1873  $aPersParam = isset($oArticle->oxorderarticles__oxpersparam) ? $oArticle->getPersParams() : null;
1874  $oBasket->addToBasket(
1875  $oArticle->oxorderarticles__oxartid->value,
1876  $oArticle->oxorderarticles__oxamount->value,
1877  $aSel, $aPersParam
1878  );
1879  }
1880  }
1881  }
1882 
1888  public function getTotalOrderSum()
1889  {
1890  $oCur = $this->getConfig()->getActShopCurrencyObject();
1891 
1892  return number_format((double) $this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
1893  }
1894 
1902  public function getProductVats($blFormatCurrency = true)
1903  {
1904  $aVats = array();
1905  if ($this->oxorder__oxartvat1->value) {
1906  $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
1907  }
1908  if ($this->oxorder__oxartvat2->value) {
1909  $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
1910  }
1911 
1912  if ($blFormatCurrency) {
1913  $oLang = oxRegistry::getLang();
1914  $oCur = $this->getConfig()->getActShopCurrencyObject();
1915  foreach ($aVats as $sKey => $dVat) {
1916  $aVats[$sKey] = $oLang->formatCurrency($dVat, $oCur);
1917  }
1918  }
1919 
1920  return $aVats;
1921  }
1922 
1928  public function getBillCountry()
1929  {
1930  if (!$this->oxorder__oxbillcountry->value) {
1931  $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle($this->oxorder__oxbillcountryid->value));
1932  }
1933 
1934  return $this->oxorder__oxbillcountry;
1935  }
1936 
1942  public function getDelCountry()
1943  {
1944  if (!$this->oxorder__oxdelcountry->value) {
1945  $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle($this->oxorder__oxdelcountryid->value));
1946  }
1947 
1948  return $this->oxorder__oxdelcountry;
1949  }
1950 
1956  public function reloadDelivery($blReload)
1957  {
1958  $this->_blReloadDelivery = $blReload;
1959  }
1960 
1966  public function reloadDiscount($blReload)
1967  {
1968  $this->_blReloadDiscount = $blReload;
1969  }
1970 
1974  public function cancelOrder()
1975  {
1976  $this->oxorder__oxstorno = new oxField(1);
1977  if ($this->save()) {
1978 
1979 
1980  // canceling ordered products
1981  foreach ($this->getOrderArticles() as $oOrderArticle) {
1982 
1983 
1984  $oOrderArticle->cancelOrderArticle();
1985  }
1986  }
1987  }
1988 
1995  public function getOrderCurrency()
1996  {
1997  if ($this->_oOrderCurrency === null) {
1998 
1999  // setting default in case unrecognized currency was set during order
2000  $aCurrencies = $this->getConfig()->getCurrencyArray();
2001  $this->_oOrderCurrency = current($aCurrencies);
2002 
2003  foreach ($aCurrencies as $oCurr) {
2004  if ($oCurr->name == $this->oxorder__oxcurrency->value) {
2005  $this->_oOrderCurrency = $oCurr;
2006  break;
2007  }
2008  }
2009  }
2010 
2011  return $this->_oOrderCurrency;
2012  }
2013 
2023  public function validateOrder($oBasket, $oUser)
2024  {
2025  // validating stock
2026  $iValidState = $this->validateStock($oBasket);
2027 
2028  if (!$iValidState) {
2029  // validating delivery
2030  $iValidState = $this->validateDelivery($oBasket);
2031  }
2032 
2033  if (!$iValidState) {
2034  // validating payment
2035  $iValidState = $this->validatePayment($oBasket);
2036  }
2037 
2038  if (!$iValidState) {
2039  //0003110 validating delivery address, it is not be changed during checkout process
2040  $iValidState = $this->validateDeliveryAddress($oUser);
2041  }
2042 
2043  if (!$iValidState) {
2044  // validating minimum price
2045  $iValidState = $this->validateBasket($oBasket);
2046  }
2047 
2048  return $iValidState;
2049  }
2050 
2058  public function validateBasket($oBasket)
2059  {
2060  return $oBasket->isBelowMinOrderPrice() ? self::ORDER_STATE_BELOWMINPRICE : null;
2061  }
2062 
2071  public function validateDeliveryAddress($oUser)
2072  {
2073  $sDelAddressMD5 = $this->getConfig()->getRequestParameter('sDeliveryAddressMD5');
2074 
2075  $sDeliveryAddress = $oUser->getEncodedDeliveryAddress();
2076 
2078  $oRequiredAddressFields = oxNew('oxRequiredAddressFields');
2079 
2081  $oFieldsValidator = oxNew('oxRequiredFieldsValidator');
2082  $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getBillingFields());
2083  $blFieldsValid = $oFieldsValidator->validateFields($oUser);
2084 
2086  $oDeliveryAddress = $this->getDelAddressInfo();
2087  if ($blFieldsValid && $oDeliveryAddress) {
2088  $sDeliveryAddress .= $oDeliveryAddress->getEncodedDeliveryAddress();
2089 
2090  $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getDeliveryFields());
2091  $blFieldsValid = $oFieldsValidator->validateFields($oDeliveryAddress);
2092  }
2093 
2094  $iState = 0;
2095  if ($sDelAddressMD5 != $sDeliveryAddress || !$blFieldsValid) {
2097  }
2098 
2099  return $iState;
2100  }
2101 
2102 
2111  public function validateDelivery($oBasket)
2112  {
2113  // proceed with no delivery
2114  // used for other countries
2115  if ($oBasket->getPaymentId() == 'oxempty') {
2116  return;
2117  }
2118  $oDb = oxDb::getDb();
2119 
2120  $oDelSet = oxNew("oxdeliveryset");
2121  $sTable = $oDelSet->getViewName();
2122 
2123  $sQ = "select 1 from {$sTable} where {$sTable}.oxid=" .
2124  $oDb->quote($oBasket->getShippingId()) . " and " . $oDelSet->getSqlActiveSnippet();
2125 
2126  if (!$oDb->getOne($sQ, false, false)) {
2127  // throwing exception
2129  }
2130  }
2131 
2140  public function validatePayment($oBasket)
2141  {
2142  $oDb = oxDb::getDb();
2143 
2144  $oPayment = oxNew("oxpayment");
2145  $sTable = $oPayment->getViewName();
2146 
2147  $sQ = "select 1 from {$sTable} where {$sTable}.oxid=" .
2148  $oDb->quote($oBasket->getPaymentId()) . " and " . $oPayment->getSqlActiveSnippet();
2149 
2150  if (!$oDb->getOne($sQ, false, false)) {
2152  }
2153  }
2154 
2160  protected function _setTsProtection(oxBasket $oBasket)
2161  {
2162  // protection price
2163  if (($oTsProtectionCost = $oBasket->getCosts('oxtsprotection'))) {
2164  $this->oxorder__oxtsprotectcosts = new oxField($oTsProtectionCost->getBruttoPrice(), oxField::T_RAW);
2165  }
2166 
2167  // protection protduct id
2168  $this->oxorder__oxtsprotectid = new oxField($oBasket->getTsProductId(), oxField::T_RAW);
2169  }
2170 
2178  protected function _executeTsProtection(oxBasket $oBasket)
2179  {
2180  $aValues['tsProductId'] = $this->oxorder__oxtsprotectid->value;
2181  $aValues['amount'] = $oBasket->getTsInsuredSum();
2182  $oCur = $this->getConfig()->getActShopCurrencyObject();
2183  $aValues['currency'] = $oCur->name;
2184  $aValues['buyerEmail'] = $this->oxorder__oxbillemail->value;
2185  $aValues['shopCustomerID'] = $this->oxorder__oxuserid->value;
2186  $aValues['shopOrderID'] = $this->oxorder__oxordernr->value;
2187  $aValues['orderDate'] = $this->oxorder__oxorderdate->value;
2188  $sPaymentId = $oBasket->getPaymentId();
2189 
2191  $oTsProtection = oxNew('oxtsprotection');
2192  $oTsProtection->requestForTsProtection($aValues, $sPaymentId);
2193 
2194  return true;
2195  }
2196 
2202  public function getFormattedTotalNetSum()
2203  {
2204  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalnetsum->value, $this->getOrderCurrency());
2205  }
2206 
2212  public function getFormattedTotalBrutSum()
2213  {
2214  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalbrutsum->value, $this->getOrderCurrency());
2215  }
2216 
2222  public function getFormattedDeliveryCost()
2223  {
2224  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxdelcost->value, $this->getOrderCurrency());
2225  }
2226 
2234  public function getFormattedeliveryCost()
2235  {
2236  return $this->getFormattedDeliveryCost();
2237  }
2238 
2244  public function getFormattedPayCost()
2245  {
2246  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxpaycost->value, $this->getOrderCurrency());
2247  }
2248 
2254  public function getFormattedWrapCost()
2255  {
2256  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxwrapcost->value, $this->getOrderCurrency());
2257  }
2258 
2264  public function getFormattedGiftCardCost()
2265  {
2266  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxgiftcardcost->value, $this->getOrderCurrency());
2267  }
2268 
2274  public function getFormattedTotalVouchers()
2275  {
2276  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxvoucherdiscount->value, $this->getOrderCurrency());
2277  }
2278 
2284  public function getFormattedDiscount()
2285  {
2286  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxdiscount->value, $this->getOrderCurrency());
2287  }
2288 
2294  public function getFormattedTotalOrderSum()
2295  {
2296  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalordersum->value, $this->getOrderCurrency());
2297  }
2298 
2304  public function getTrackCode()
2305  {
2306  return $this->oxorder__oxtrackcode->value;
2307  }
2308 
2314  public function getShipmentTrackingUrl()
2315  {
2316  $oConfig = oxRegistry::getConfig();
2317  if ($this->_sShipTrackUrl === null) {
2318  $sParcelService = $oConfig->getConfigParam('sParcelService');
2319  $sTrackingCode = $this->getTrackCode();
2320  if ($sParcelService && $sTrackingCode) {
2321  $this->_sShipTrackUrl = str_replace("##ID##", $sTrackingCode, $sParcelService);
2322  }
2323  }
2324 
2325  return $this->_sShipTrackUrl;
2326  }
2327 }