OXID eShop CE  4.9.7
 All Classes 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  // loading users notice list ..
1098  if ($oUserBasket = $oUser->getBasket('noticelist')) {
1099  // only if wishlist is enabled
1100  foreach ($aArticleList as $oContent) {
1101  $sProdId = $oContent->getProductId();
1102 
1103  // updating users notice list
1104  $oUserBasketItem = $oUserBasket->getItem($sProdId, $oContent->getSelList(), $oContent->getPersParams());
1105  $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
1106  if ($dNewAmount < 0) {
1107  $dNewAmount = 0;
1108  }
1109  $oUserBasket->addItemToBasket($sProdId, $dNewAmount, $oContent->getSelList(), true, $oContent->getPersParams());
1110  }
1111  }
1112  }
1113 
1117  protected function _updateOrderDate()
1118  {
1119  $oDb = oxDb::getDb();
1120  $sDate = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
1121  $sQ = 'update oxorder set oxorderdate=' . $oDb->quote($sDate) . ' where oxid=' . $oDb->quote($this->getId());
1122  $this->oxorder__oxorderdate = new oxField($sDate, oxField::T_RAW);
1123  $oDb->execute($sQ);
1124  }
1125 
1133  protected function _markVouchers($oBasket, $oUser)
1134  {
1135  $this->_aVoucherList = $oBasket->getVouchers();
1136 
1137  if (is_array($this->_aVoucherList)) {
1138  foreach ($this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
1139  $oVoucher = oxNew('oxvoucher');
1140  $oVoucher->load($sVoucherId);
1141  $oVoucher->markAsUsed($this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->dVoucherdiscount);
1142 
1143  $this->_aVoucherList[$sVoucherId] = $oVoucher;
1144  }
1145  }
1146  }
1147 
1153  public function save()
1154  {
1155  if (($blSave = parent::save())) {
1156 
1157  // saving order articles
1158  $oOrderArticles = $this->getOrderArticles();
1159  if ($oOrderArticles && count($oOrderArticles) > 0) {
1160  foreach ($oOrderArticles as $oOrderArticle) {
1161  $oOrderArticle->save();
1162  }
1163  }
1164  }
1165 
1166  return $blSave;
1167  }
1168 
1175  public function getDelAddressInfo()
1176  {
1177  $oDelAdress = null;
1178  if (!($soxAddressId = oxRegistry::getConfig()->getRequestParameter('deladrid'))) {
1179  $soxAddressId = oxRegistry::getSession()->getVariable('deladrid');
1180  }
1181  if ($soxAddressId) {
1182  $oDelAdress = oxNew('oxaddress');
1183  $oDelAdress->load($soxAddressId);
1184 
1185  //get delivery country name from delivery country id
1186  if ($oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1) {
1187  $oCountry = oxNew('oxcountry');
1188  $oCountry->load($oDelAdress->oxaddress__oxcountryid->value);
1189  $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
1190  }
1191  }
1192 
1193  return $oDelAdress;
1194  }
1195 
1204  public function validateStock($oBasket)
1205  {
1206  foreach ($oBasket->getContents() as $key => $oContent) {
1207  try {
1208  $oProd = $oContent->getArticle(true, null, true);
1209  } catch (oxNoArticleException $oEx) {
1210  $oBasket->removeItem($key);
1211  throw $oEx;
1212  } catch (oxArticleInputException $oEx) {
1213  $oBasket->removeItem($key);
1214  throw $oEx;
1215  }
1216 
1217  // check if its still available
1218  $dArtStockAmount = $oBasket->getArtStockInBasket($oProd->getId(), $key);
1219  $iOnStock = $oProd->checkForStock($oContent->getAmount(), $dArtStockAmount);
1220  if ($iOnStock !== true) {
1222  $oEx = oxNew('oxOutOfStockException');
1223  $oEx->setMessage('ERROR_MESSAGE_OUTOFSTOCK_OUTOFSTOCK');
1224  $oEx->setArticleNr($oProd->oxarticles__oxartnum->value);
1225  $oEx->setProductId($oProd->getId());
1226  $oEx->setBasketIndex($key);
1227 
1228  if (!is_numeric($iOnStock)) {
1229  $iOnStock = 0;
1230  }
1231  $oEx->setRemainingAmount($iOnStock);
1232  throw $oEx;
1233  }
1234  }
1235  }
1236 
1242  protected function _insert()
1243  {
1244  $myConfig = $this->getConfig();
1245  $oUtilsDate = oxRegistry::get("oxUtilsDate");
1246 
1247  //V #M525 orderdate must be the same as it was
1248  if (!$this->oxorder__oxorderdate->value) {
1249  $this->oxorder__oxorderdate = new oxField(date('Y-m-d H:i:s', $oUtilsDate->getTime()), oxField::T_RAW);
1250  } else {
1251  $this->oxorder__oxorderdate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxorderdate->value, true));
1252  }
1253 
1254  $this->oxorder__oxshopid = new oxField($myConfig->getShopId(), oxField::T_RAW);
1255  $this->oxorder__oxsenddate = new oxField($oUtilsDate->formatDBDate($this->oxorder__oxsenddate->value, true));
1256 
1257  $blInsert = parent::_insert();
1258 
1259  return $blInsert;
1260  }
1261 
1267  protected function _getCounterIdent()
1268  {
1269  $sCounterIdent = ($this->_blSeparateNumbering) ? 'oxOrder_' . $this->getConfig()->getShopId() : 'oxOrder';
1270 
1271  return $sCounterIdent;
1272  }
1273 
1274 
1280  protected function _setNumber()
1281  {
1282  $oDb = oxDb::getDb();
1283 
1284  $iCnt = oxNew('oxCounter')->getNext($this->_getCounterIdent());
1285  $sQ = "update oxorder set oxordernr = ? where oxid = ?";
1286  $blUpdate = ( bool ) $oDb->execute($sQ, array($iCnt, $this->getId()));
1287 
1288  if ($blUpdate) {
1289  $this->oxorder__oxordernr = new oxField($iCnt);
1290  }
1291 
1292  return $blUpdate;
1293  }
1294 
1300  protected function _update()
1301  {
1302  $this->_aSkipSaveFields = array('oxtimestamp', 'oxorderdate');
1303  $this->oxorder__oxsenddate = new oxField(oxRegistry::get("oxUtilsDate")->formatDBDate($this->oxorder__oxsenddate->value, true));
1304 
1305  return parent::_update();
1306  }
1307 
1316  public function delete($sOxId = null)
1317  {
1318  if ($sOxId) {
1319  if (!$this->load($sOxId)) {
1320  // such order does not exist
1321  return false;
1322  }
1323  } elseif (!$sOxId) {
1324  $sOxId = $this->getId();
1325  }
1326 
1327  // no order id is passed
1328  if (!$sOxId) {
1329  return false;
1330  }
1331 
1332 
1333  // delete order articles
1334  $oOrderArticles = $this->getOrderArticles(false);
1335  foreach ($oOrderArticles as $oOrderArticle) {
1336  $oOrderArticle->delete();
1337  }
1338 
1339  // #440 - deleting user payment info
1340  if ($oPaymentType = $this->getPaymentType()) {
1341  $oPaymentType->delete();
1342  }
1343 
1344  return parent::delete($sOxId);
1345  }
1346 
1356  public function recalculateOrder($aNewArticles = array())
1357  {
1358  oxDb::getDb()->startTransaction();
1359 
1360  try {
1361  $oBasket = $this->_getOrderBasket();
1362 
1363  // add this order articles to virtual basket and recalculates basket
1364  $this->_addOrderArticlesToBasket($oBasket, $this->getOrderArticles(true));
1365 
1366  // adding new articles to existing order
1367  $this->_addArticlesToBasket($oBasket, $aNewArticles);
1368 
1369  // recalculating basket
1370  $oBasket->calculateBasket(true);
1371 
1372  //finalizing order (skipping payment execution, vouchers marking and mail sending)
1373  $iRet = $this->finalizeOrder($oBasket, $this->getOrderUser(), true);
1374 
1375  //if finalizing order failed, rollback transaction
1376  if ($iRet !== 1) {
1377  oxDb::getDb()->rollbackTransaction();
1378  } else {
1379  oxDb::getDb()->commitTransaction();
1380  }
1381 
1382  } catch (Exception $oE) {
1383  // if exception, rollBack everything
1384  oxDb::getDb()->rollbackTransaction();
1385 
1386  if (defined('OXID_PHP_UNIT')) {
1387  throw $oE;
1388  }
1389  }
1390  }
1391 
1392  protected $_oOrderBasket = null;
1393 
1401  protected function _getOrderBasket($blStockCheck = true)
1402  {
1403  $this->_oOrderBasket = oxNew("oxBasket");
1404  $this->_oOrderBasket->enableSaveToDataBase(false);
1405 
1406  //setting recalculation mode
1407  $this->_oOrderBasket->setCalculationModeNetto($this->isNettoMode());
1408 
1409  // setting stock check mode
1410  $this->_oOrderBasket->setStockCheckMode($blStockCheck);
1411 
1412  // setting virtual basket user
1413  $this->_oOrderBasket->setBasketUser($this->getOrderUser());
1414 
1415  // transferring order id
1416  $this->_oOrderBasket->setOrderId($this->getId());
1417 
1418  // setting basket currency order uses
1419  $aCurrencies = $this->getConfig()->getCurrencyArray();
1420  foreach ($aCurrencies as $oCur) {
1421  if ($oCur->name == $this->oxorder__oxcurrency->value) {
1422  $oBasketCur = $oCur;
1423  break;
1424  }
1425  }
1426 
1427  // setting currency
1428  $this->_oOrderBasket->setBasketCurrency($oBasketCur);
1429 
1430  // set basket card id and message
1431  $this->_oOrderBasket->setCardId($this->oxorder__oxcardid->value);
1432  $this->_oOrderBasket->setCardMessage($this->oxorder__oxcardtext->value);
1433 
1434  if ($this->_blReloadDiscount) {
1436  // disabling availability check
1437  $this->_oOrderBasket->setSkipVouchersChecking(true);
1438 
1439  // add previously used vouchers
1440  $sQ = 'select oxid from oxvouchers where oxorderid = ' . $oDb->quote($this->getId());
1441  $aVouchers = $oDb->getAll($sQ);
1442  foreach ($aVouchers as $aVoucher) {
1443  $this->_oOrderBasket->addVoucher($aVoucher['oxid']);
1444  }
1445  } else {
1446  $this->_oOrderBasket->setDiscountCalcMode(false);
1447  $this->_oOrderBasket->setVoucherDiscount($this->oxorder__oxvoucherdiscount->value);
1448  $this->_oOrderBasket->setTotalDiscount($this->oxorder__oxdiscount->value);
1449  }
1450 
1451  // must be kept old delivery?
1452  if (!$this->_blReloadDelivery) {
1453  $this->_oOrderBasket->setDeliveryPrice($this->getOrderDeliveryPrice());
1454  } else {
1455  // set shipping
1456  $this->_oOrderBasket->setShipping($this->oxorder__oxdeltype->value);
1457  $this->_oOrderBasket->setDeliveryPrice(null);
1458  }
1459 
1460  //set basket payment
1461  $this->_oOrderBasket->setPayment($this->oxorder__oxpaymenttype->value);
1462 
1463  return $this->_oOrderBasket;
1464  }
1465 
1472  public function setDelivery($sDeliveryId)
1473  {
1474  $this->reloadDelivery(true);
1475  $this->oxorder__oxdeltype = new oxField($sDeliveryId);
1476  }
1477 
1483  public function getOrderUser()
1484  {
1485  if ($this->_oUser === null) {
1486  $this->_oUser = oxNew("oxuser");
1487  $this->_oUser->load($this->oxorder__oxuserid->value);
1488 
1489  // if object is loaded then reusing its order info
1490  if ($this->_isLoaded) {
1491  // bill address
1492  $this->_oUser->oxuser__oxcompany = clone $this->oxorder__oxbillcompany;
1493  $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
1494  $this->_oUser->oxuser__oxfname = clone $this->oxorder__oxbillfname;
1495  $this->_oUser->oxuser__oxlname = clone $this->oxorder__oxbilllname;
1496  $this->_oUser->oxuser__oxstreet = clone $this->oxorder__oxbillstreet;
1497  $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
1498  $this->_oUser->oxuser__oxaddinfo = clone $this->oxorder__oxbilladdinfo;
1499  $this->_oUser->oxuser__oxustid = clone $this->oxorder__oxbillustid;
1500 
1501 
1502  $this->_oUser->oxuser__oxcity = clone $this->oxorder__oxbillcity;
1503  $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
1504  $this->_oUser->oxuser__oxstateid = clone $this->oxorder__oxbillstateid;
1505  $this->_oUser->oxuser__oxzip = clone $this->oxorder__oxbillzip;
1506  $this->_oUser->oxuser__oxfon = clone $this->oxorder__oxbillfon;
1507  $this->_oUser->oxuser__oxfax = clone $this->oxorder__oxbillfax;
1508  $this->_oUser->oxuser__oxsal = clone $this->oxorder__oxbillsal;
1509  }
1510  }
1511 
1512  return $this->_oUser;
1513  }
1514 
1520  public function pdfFooter($oPdf)
1521  {
1522  }
1523 
1529  public function pdfHeaderplus($oPdf)
1530  {
1531  }
1532 
1538  public function pdfHeader($oPdf)
1539  {
1540  }
1541 
1548  public function genPdf($sFilename, $iSelLang = 0)
1549  {
1550  }
1551 
1557  public function getInvoiceNum()
1558  {
1559  $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "' . $this->getConfig()->getShopId() . '" ';
1560 
1561  return (( int ) oxDb::getDb()->getOne($sQ, false) + 1);
1562  }
1563 
1569  public function getNextBillNum()
1570  {
1571  $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "' . $this->getConfig()->getShopId() . '" ';
1572 
1573  return (( int ) oxDb::getDb()->getOne($sQ, false) + 1);
1574  }
1575 
1581  public function getShippingSetList()
1582  {
1583  // in which country we deliver
1584  if (!($sShipId = $this->oxorder__oxdelcountryid->value)) {
1585  $sShipId = $this->oxorder__oxbillcountryid->value;
1586  }
1587 
1588  $oBasket = $this->_getOrderBasket(false);
1589 
1590  // unsetting bundles
1591  $oOrderArticles = $this->getOrderArticles();
1592  foreach ($oOrderArticles as $sItemId => $oItem) {
1593  if ($oItem->isBundle()) {
1594  $oOrderArticles->offsetUnset($sItemId);
1595  }
1596  }
1597 
1598  // add this order articles to basket and recalculate basket
1599  $this->_addOrderArticlesToBasket($oBasket, $oOrderArticles);
1600 
1601  // recalculating basket
1602  $oBasket->calculateBasket(true);
1603 
1604  // load fitting deliveries list
1605  $oDeliveryList = oxNew("oxDeliveryList", "core");
1606  $oDeliveryList->setCollectFittingDeliveriesSets(true);
1607 
1608  return $oDeliveryList->getDeliveryList($oBasket, $this->getOrderUser(), $sShipId);
1609  }
1610 
1616  public function getVoucherNrList()
1617  {
1619  $aVouchers = array();
1620  $sSelect = "select oxvouchernr from oxvouchers where oxorderid = " . $oDb->quote($this->oxorder__oxid->value);
1621  $rs = $oDb->select($sSelect);
1622  if ($rs != false && $rs->recordCount() > 0) {
1623  while (!$rs->EOF) {
1624  $aVouchers[] = $rs->fields['oxvouchernr'];
1625  $rs->moveNext();
1626  }
1627  }
1628 
1629  return $aVouchers;
1630  }
1631 
1639  public function getOrderSum($blToday = false)
1640  {
1641  $sSelect = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
1642  $sSelect .= 'oxshopid = "' . $this->getConfig()->getShopId() . '" and oxorder.oxstorno != "1" ';
1643 
1644  if ($blToday) {
1645  $sSelect .= 'and oxorderdate like "' . date('Y-m-d') . '%" ';
1646  }
1647 
1648  return ( double ) oxDb::getDb()->getOne($sSelect, false, false);
1649  }
1650 
1658  public function getOrderCnt($blToday = false)
1659  {
1660  $sSelect = 'select count(*) from oxorder where ';
1661  $sSelect .= 'oxshopid = "' . $this->getConfig()->getShopId() . '" and oxorder.oxstorno != "1" ';
1662 
1663  if ($blToday) {
1664  $sSelect .= 'and oxorderdate like "' . date('Y-m-d') . '%" ';
1665  }
1666 
1667  return ( int ) oxDb::getDb()->getOne($sSelect, false, false);
1668  }
1669 
1670 
1678  protected function _checkOrderExist($sOxId = null)
1679  {
1680  if (!$sOxId) {
1681  return false;
1682  }
1683 
1684  $oDb = oxDb::getDb();
1685  if ($oDb->getOne('select oxid from oxorder where oxid = ' . $oDb->quote($sOxId), false, false)) {
1686  return true;
1687  }
1688 
1689  return false;
1690  }
1691 
1701  protected function _sendOrderByEmail($oUser = null, $oBasket = null, $oPayment = null)
1702  {
1704 
1705  // add user, basket and payment to order
1706  $this->_oUser = $oUser;
1707  $this->_oBasket = $oBasket;
1708  $this->_oPayment = $oPayment;
1709 
1710  $oxEmail = oxNew('oxemail');
1711 
1712  // send order email to user
1713  if ($oxEmail->sendOrderEMailToUser($this)) {
1714  // mail to user was successfully sent
1715  $iRet = self::ORDER_STATE_OK;
1716  }
1717 
1718  // send order email to shop owner
1719  $oxEmail->sendOrderEMailToOwner($this);
1720 
1721  return $iRet;
1722  }
1723 
1729  public function getBasket()
1730  {
1731  return $this->_oBasket;
1732  }
1733 
1739  public function getPayment()
1740  {
1741  return $this->_oPayment;
1742  }
1743 
1749  public function getVoucherList()
1750  {
1751  return $this->_aVoucherList;
1752  }
1753 
1759  public function getDelSet()
1760  {
1761  if ($this->_oDelSet == null) {
1762  // load deliveryset info
1763  $this->_oDelSet = oxNew('oxdeliveryset');
1764  $this->_oDelSet->load($this->oxorder__oxdeltype->value);
1765  }
1766 
1767  return $this->_oDelSet;
1768  }
1769 
1775  public function getPaymentType()
1776  {
1777  if ($this->oxorder__oxpaymentid->value && $this->_oPaymentType === null) {
1778  $this->_oPaymentType = false;
1779  $oPaymentType = oxNew('oxuserpayment');
1780  if ($oPaymentType->load($this->oxorder__oxpaymentid->value)) {
1781  $this->_oPaymentType = $oPaymentType;
1782  }
1783  }
1784 
1785  return $this->_oPaymentType;
1786  }
1787 
1793  public function getGiftCard()
1794  {
1795  if ($this->oxorder__oxcardid->value && $this->_oGiftCard == null) {
1796  $this->_oGiftCard = oxNew('oxwrapping');
1797  $this->_oGiftCard->load($this->oxorder__oxcardid->value);
1798  }
1799 
1800  return $this->_oGiftCard;
1801  }
1802 
1808  public function setSeparateNumbering($blSeparateNumbering = null)
1809  {
1810  $this->_blSeparateNumbering = $blSeparateNumbering;
1811  }
1812 
1820  public function getLastUserPaymentType($sUserId)
1821  {
1822  $oDb = oxDb::getDb();
1823  $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="' . $this->getConfig()->getShopId() . '" and oxorder.oxuserid=' . $oDb->quote($sUserId) . ' order by oxorder.oxorderdate desc ';
1824  $sLastPaymentId = $oDb->getOne($sQ, false, false);
1825 
1826  return $sLastPaymentId;
1827  }
1828 
1835  protected function _addOrderArticlesToBasket($oBasket, $aOrderArticles)
1836  {
1837  // if no order articles, return empty basket
1838  if (count($aOrderArticles) > 0) {
1839 
1840  //adding order articles to basket
1841  foreach ($aOrderArticles as $oOrderArticle) {
1842  $oBasket->addOrderArticleToBasket($oOrderArticle);
1843  }
1844  }
1845  }
1846 
1853  protected function _addArticlesToBasket($oBasket, $aArticles)
1854  {
1855  // if no order articles
1856  if (count($aArticles) > 0) {
1857 
1858  //adding order articles to basket
1859  foreach ($aArticles as $oArticle) {
1860  $aSel = isset($oArticle->oxorderarticles__oxselvariant) ? $oArticle->oxorderarticles__oxselvariant->value : null;
1861  $aPersParam = isset($oArticle->oxorderarticles__oxpersparam) ? $oArticle->getPersParams() : null;
1862  $oBasket->addToBasket(
1863  $oArticle->oxorderarticles__oxartid->value,
1864  $oArticle->oxorderarticles__oxamount->value,
1865  $aSel, $aPersParam
1866  );
1867  }
1868  }
1869  }
1870 
1876  public function getTotalOrderSum()
1877  {
1878  $oCur = $this->getConfig()->getActShopCurrencyObject();
1879 
1880  return number_format((double) $this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
1881  }
1882 
1890  public function getProductVats($blFormatCurrency = true)
1891  {
1892  $aVats = array();
1893  if ($this->oxorder__oxartvat1->value) {
1894  $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
1895  }
1896  if ($this->oxorder__oxartvat2->value) {
1897  $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
1898  }
1899 
1900  if ($blFormatCurrency) {
1901  $oLang = oxRegistry::getLang();
1902  $oCur = $this->getConfig()->getActShopCurrencyObject();
1903  foreach ($aVats as $sKey => $dVat) {
1904  $aVats[$sKey] = $oLang->formatCurrency($dVat, $oCur);
1905  }
1906  }
1907 
1908  return $aVats;
1909  }
1910 
1916  public function getBillCountry()
1917  {
1918  if (!$this->oxorder__oxbillcountry->value) {
1919  $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle($this->oxorder__oxbillcountryid->value));
1920  }
1921 
1922  return $this->oxorder__oxbillcountry;
1923  }
1924 
1930  public function getDelCountry()
1931  {
1932  if (!$this->oxorder__oxdelcountry->value) {
1933  $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle($this->oxorder__oxdelcountryid->value));
1934  }
1935 
1936  return $this->oxorder__oxdelcountry;
1937  }
1938 
1944  public function reloadDelivery($blReload)
1945  {
1946  $this->_blReloadDelivery = $blReload;
1947  }
1948 
1954  public function reloadDiscount($blReload)
1955  {
1956  $this->_blReloadDiscount = $blReload;
1957  }
1958 
1962  public function cancelOrder()
1963  {
1964  $this->oxorder__oxstorno = new oxField(1);
1965  if ($this->save()) {
1966 
1967 
1968  // canceling ordered products
1969  foreach ($this->getOrderArticles() as $oOrderArticle) {
1970 
1971 
1972  $oOrderArticle->cancelOrderArticle();
1973  }
1974  }
1975  }
1976 
1983  public function getOrderCurrency()
1984  {
1985  if ($this->_oOrderCurrency === null) {
1986 
1987  // setting default in case unrecognized currency was set during order
1988  $aCurrencies = $this->getConfig()->getCurrencyArray();
1989  $this->_oOrderCurrency = current($aCurrencies);
1990 
1991  foreach ($aCurrencies as $oCurr) {
1992  if ($oCurr->name == $this->oxorder__oxcurrency->value) {
1993  $this->_oOrderCurrency = $oCurr;
1994  break;
1995  }
1996  }
1997  }
1998 
1999  return $this->_oOrderCurrency;
2000  }
2001 
2011  public function validateOrder($oBasket, $oUser)
2012  {
2013  // validating stock
2014  $iValidState = $this->validateStock($oBasket);
2015 
2016  if (!$iValidState) {
2017  // validating delivery
2018  $iValidState = $this->validateDelivery($oBasket);
2019  }
2020 
2021  if (!$iValidState) {
2022  // validating payment
2023  $iValidState = $this->validatePayment($oBasket);
2024  }
2025 
2026  if (!$iValidState) {
2027  //0003110 validating delivery address, it is not be changed during checkout process
2028  $iValidState = $this->validateDeliveryAddress($oUser);
2029  }
2030 
2031  if (!$iValidState) {
2032  // validating minimum price
2033  $iValidState = $this->validateBasket($oBasket);
2034  }
2035 
2036  return $iValidState;
2037  }
2038 
2046  public function validateBasket($oBasket)
2047  {
2048  return $oBasket->isBelowMinOrderPrice() ? self::ORDER_STATE_BELOWMINPRICE : null;
2049  }
2050 
2059  public function validateDeliveryAddress($oUser)
2060  {
2061  $sDelAddressMD5 = $this->getConfig()->getRequestParameter('sDeliveryAddressMD5');
2062 
2063  $sDeliveryAddress = $oUser->getEncodedDeliveryAddress();
2064 
2066  $oRequiredAddressFields = oxNew('oxRequiredAddressFields');
2067 
2069  $oFieldsValidator = oxNew('oxRequiredFieldsValidator');
2070  $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getBillingFields());
2071  $blFieldsValid = $oFieldsValidator->validateFields($oUser);
2072 
2074  $oDeliveryAddress = $this->getDelAddressInfo();
2075  if ($blFieldsValid && $oDeliveryAddress) {
2076  $sDeliveryAddress .= $oDeliveryAddress->getEncodedDeliveryAddress();
2077 
2078  $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getDeliveryFields());
2079  $blFieldsValid = $oFieldsValidator->validateFields($oDeliveryAddress);
2080  }
2081 
2082  $iState = 0;
2083  if ($sDelAddressMD5 != $sDeliveryAddress || !$blFieldsValid) {
2085  }
2086 
2087  return $iState;
2088  }
2089 
2090 
2099  public function validateDelivery($oBasket)
2100  {
2101  // proceed with no delivery
2102  // used for other countries
2103  if ($oBasket->getPaymentId() == 'oxempty') {
2104  return;
2105  }
2106  $oDb = oxDb::getDb();
2107 
2108  $oDelSet = oxNew("oxdeliveryset");
2109  $sTable = $oDelSet->getViewName();
2110 
2111  $sQ = "select 1 from {$sTable} where {$sTable}.oxid=" .
2112  $oDb->quote($oBasket->getShippingId()) . " and " . $oDelSet->getSqlActiveSnippet();
2113 
2114  if (!$oDb->getOne($sQ, false, false)) {
2115  // throwing exception
2117  }
2118  }
2119 
2128  public function validatePayment($oBasket)
2129  {
2130  $oDb = oxDb::getDb();
2131 
2132  $oPayment = oxNew("oxpayment");
2133  $sTable = $oPayment->getViewName();
2134 
2135  $sQ = "select 1 from {$sTable} where {$sTable}.oxid=" .
2136  $oDb->quote($oBasket->getPaymentId()) . " and " . $oPayment->getSqlActiveSnippet();
2137 
2138  if (!$oDb->getOne($sQ, false, false)) {
2140  }
2141  }
2142 
2148  protected function _setTsProtection(oxBasket $oBasket)
2149  {
2150  // protection price
2151  if (($oTsProtectionCost = $oBasket->getCosts('oxtsprotection'))) {
2152  $this->oxorder__oxtsprotectcosts = new oxField($oTsProtectionCost->getBruttoPrice(), oxField::T_RAW);
2153  }
2154 
2155  // protection protduct id
2156  $this->oxorder__oxtsprotectid = new oxField($oBasket->getTsProductId(), oxField::T_RAW);
2157  }
2158 
2166  protected function _executeTsProtection(oxBasket $oBasket)
2167  {
2168  $aValues['tsProductId'] = $this->oxorder__oxtsprotectid->value;
2169  $aValues['amount'] = $oBasket->getTsInsuredSum();
2170  $oCur = $this->getConfig()->getActShopCurrencyObject();
2171  $aValues['currency'] = $oCur->name;
2172  $aValues['buyerEmail'] = $this->oxorder__oxbillemail->value;
2173  $aValues['shopCustomerID'] = $this->oxorder__oxuserid->value;
2174  $aValues['shopOrderID'] = $this->oxorder__oxordernr->value;
2175  $aValues['orderDate'] = $this->oxorder__oxorderdate->value;
2176  $sPaymentId = $oBasket->getPaymentId();
2177 
2179  $oTsProtection = oxNew('oxtsprotection');
2180  $oTsProtection->requestForTsProtection($aValues, $sPaymentId);
2181 
2182  return true;
2183  }
2184 
2190  public function getFormattedTotalNetSum()
2191  {
2192  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalnetsum->value, $this->getOrderCurrency());
2193  }
2194 
2200  public function getFormattedTotalBrutSum()
2201  {
2202  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalbrutsum->value, $this->getOrderCurrency());
2203  }
2204 
2210  public function getFormattedDeliveryCost()
2211  {
2212  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxdelcost->value, $this->getOrderCurrency());
2213  }
2214 
2222  public function getFormattedeliveryCost()
2223  {
2224  return $this->getFormattedDeliveryCost();
2225  }
2226 
2232  public function getFormattedPayCost()
2233  {
2234  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxpaycost->value, $this->getOrderCurrency());
2235  }
2236 
2242  public function getFormattedWrapCost()
2243  {
2244  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxwrapcost->value, $this->getOrderCurrency());
2245  }
2246 
2252  public function getFormattedGiftCardCost()
2253  {
2254  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxgiftcardcost->value, $this->getOrderCurrency());
2255  }
2256 
2262  public function getFormattedTotalVouchers()
2263  {
2264  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxvoucherdiscount->value, $this->getOrderCurrency());
2265  }
2266 
2272  public function getFormattedDiscount()
2273  {
2274  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxdiscount->value, $this->getOrderCurrency());
2275  }
2276 
2282  public function getFormattedTotalOrderSum()
2283  {
2284  return oxRegistry::getLang()->formatCurrency($this->oxorder__oxtotalordersum->value, $this->getOrderCurrency());
2285  }
2286 
2292  public function getTrackCode()
2293  {
2294  return $this->oxorder__oxtrackcode->value;
2295  }
2296 
2302  public function getShipmentTrackingUrl()
2303  {
2304  $oConfig = oxRegistry::getConfig();
2305  if ($this->_sShipTrackUrl === null) {
2306  $sParcelService = $oConfig->getConfigParam('sParcelService');
2307  $sTrackingCode = $this->getTrackCode();
2308  if ($sParcelService && $sTrackingCode) {
2309  $this->_sShipTrackUrl = str_replace("##ID##", $sTrackingCode, $sParcelService);
2310  }
2311  }
2312 
2313  return $this->_sShipTrackUrl;
2314  }
2315 }