OXID eShop CE  4.8.11
 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  // defining order state constants
16 
21  const ORDER_STATE_OK = 1;
22 
28 
34 
40 
46 
52 
58 
64 
70  protected $_aSkipSaveFields = array( 'oxtimestamp' );
71 
77  protected $_oArticles = null;
78 
84  protected $_oDelSet = null;
85 
91  protected $_oGiftCard = null;
92 
98  protected $_oPaymentType = null;
99 
105  protected $_oPayment = null;
106 
112  protected $_aVoucherList = null;
113 
119  protected $_oDelPrice = null;
120 
126  protected $_oUser = null;
127 
133  protected $_oBasket = null;
134 
140  protected $_oWrappingPrice = null;
141 
147  protected $_oGiftCardPrice = null;
148 
154  protected $_oPaymentPrice = null;
155 
161  protected $_oTsProtectionPrice = null;
162 
168  protected $_sClassName = 'oxorder';
169 
175  protected $_blSeparateNumbering = null;
176 
182  protected $_iOrderLang = null;
183 
189  protected $_blReloadDelivery = true;
190 
196  protected $_blReloadDiscount = true;
197 
203  protected $_oOrderCurrency = null;
204 
210  protected $_oOrderFiles = null;
211 
215  public function __construct()
216  {
218  $this->init( 'oxorder' );
219 
220  // set usage of separate orders numbering for different shops
221  $this->setSeparateNumbering( $this->getConfig()->getConfigParam( 'blSeparateNumbering') );
222 
223  }
224 
232  public function __get( $sName )
233  {
234  if ( $sName == 'oDelSet' ) {
235  return $this->getDelSet();
236  }
237 
238  if ( $sName == 'oxorder__oxbillcountry' ) {
239  return $this->getBillCountry();
240  }
241 
242  if ( $sName == 'oxorder__oxdelcountry' ) {
243  return $this->getDelCountry();
244  }
245  }
246 
254  public function assign( $dbRecord )
255  {
256 
257  parent::assign( $dbRecord );
258 
259  $oUtilsDate = oxRegistry::get("oxUtilsDate");
260 
261  // convert date's to international format
262  $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value));
263  $this->oxorder__oxsenddate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value));
264  }
265 
273  protected function _getCountryTitle( $sCountryId )
274  {
275  $sTitle = null;
276  if ( $sCountryId && $sCountryId != '-1' ) {
277  $oCountry = oxNew( 'oxcountry' );
278  $oCountry->loadInLang( $this->getOrderLanguage(), $sCountryId );
279  $sTitle = $oCountry->oxcountry__oxtitle->value;
280  }
281 
282  return $sTitle;
283  }
284 
292  protected function _getArticles( $blExcludeCanceled = false )
293  {
294  $sSelect = "SELECT `oxorderarticles`.* FROM `oxorderarticles`
295  WHERE `oxorderarticles`.`oxorderid` = '".$this->getId() . "'" .
296  ( $blExcludeCanceled ? " AND `oxorderarticles`.`oxstorno` != 1 ": " " ) ."
297  ORDER BY `oxorderarticles`.`oxartid`, `oxorderarticles`.`oxselvariant`, `oxorderarticles`.`oxpersparam` ";
298 
299  // order articles
300  $oArticles = oxNew( 'oxlist' );
301  $oArticles->init( 'oxorderarticle' );
302  $oArticles->selectString( $sSelect );
303 
304  return $oArticles;
305  }
306 
314  public function getOrderArticles( $blExcludeCanceled = false )
315  {
316  // checking set value
317  if ( $blExcludeCanceled ) {
318 
319  return $this->_getArticles( true );
320 
321  } elseif ( $this->_oArticles === null ) {
322  $this->_oArticles = $this->_getArticles();
323  }
324 
325  return $this->_oArticles;
326  }
327 
335  public function setOrderArticleList( $oOrderArticleList )
336  {
337  $this->_oArticles = $oOrderArticleList;
338  }
339 
345  public function getOrderDeliveryPrice()
346  {
347  if ( $this->_oDelPrice != null ) {
348  return $this->_oDelPrice;
349  }
350 
351  $this->_oDelPrice = oxNew( 'oxprice' );
352  $this->_oDelPrice->setBruttoPriceMode();
353  $this->_oDelPrice->setPrice( $this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value );
354  return $this->_oDelPrice;
355  }
356 
362  public function getOrderWrappingPrice()
363  {
364  if ( $this->_oWrappingPrice != null ) {
365  return $this->_oWrappingPrice;
366  }
367 
368  $this->_oWrappingPrice = oxNew( 'oxprice' );
369  $this->_oWrappingPrice->setBruttoPriceMode();
370  $this->_oWrappingPrice->setPrice( $this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value );
371  return $this->_oWrappingPrice;
372  }
373 
379  public function getOrderGiftCardPrice()
380  {
381  if ( $this->_oGidtCardPrice != null ) {
382  return $this->_oGidtCardPrice;
383  }
384 
385  $this->_oGidtCardPrice = oxNew( 'oxprice' );
386  $this->_oGidtCardPrice->setBruttoPriceMode();
387  $this->_oGidtCardPrice->setPrice( $this->oxorder__oxgiftcardcost->value, $this->oxorder__oxgiftcardvat->value );
388  return $this->_oGidtCardPrice;
389  }
390 
391 
397  public function getOrderPaymentPrice()
398  {
399  if ( $this->_oPaymentPrice != null ) {
400  return $this->_oPaymentPrice;
401  }
402 
403  $this->_oPaymentPrice = oxNew( 'oxprice' );
404  $this->_oPaymentPrice->setBruttoPriceMode();
405  $this->_oPaymentPrice->setPrice( $this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value );
406  return $this->_oPaymentPrice;
407  }
408 
414  public function getOrderTsProtectionPrice()
415  {
416  if ( $this->_oTsProtectionPrice != null ) {
418  }
419 
420  $this->_oTsProtectionPrice = oxNew( 'oxprice' );
421  $this->_oTsProtectionPrice->setBruttoPriceMode();
422  $this->_oTsProtectionPrice->setPrice( $this->oxorder__oxtsprotectcosts->value, $this->getConfig()->getConfigParam( 'dDefaultVAT' ) );
424  }
425 
432  public function getOrderNetSum()
433  {
434  $dTotalNetSum = 0;
435 
436  $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
437  $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
438  $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
439  $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
440 
441  return $dTotalNetSum;
442  }
443 
464  public function finalizeOrder( oxBasket $oBasket, $oUser, $blRecalculatingOrder = false )
465  {
466  // check if this order is already stored
467  $sGetChallenge = oxSession::getVar( 'sess_challenge' );
468  if ( $this->_checkOrderExist( $sGetChallenge ) ) {
469  oxRegistry::getUtils()->logger( 'BLOCKER' );
470  // we might use this later, this means that somebody klicked like mad on order button
472  }
473 
474  // if not recalculating order, use sess_challenge id, else leave old order id
475  if ( !$blRecalculatingOrder ) {
476  // use this ID
477  $this->setId( $sGetChallenge );
478 
479  // validating various order/basket parameters before finalizing
480  if ( $iOrderState = $this->validateOrder( $oBasket, $oUser ) ) {
481  return $iOrderState;
482  }
483  }
484 
485  // copies user info
486  $this->_setUser( $oUser );
487 
488  // copies basket info
489  $this->_loadFromBasket( $oBasket );
490 
491  // payment information
492  $oUserPayment = $this->_setPayment( $oBasket->getPaymentId() );
493 
494  // set folder information, if order is new
495  // #M575 in recalculating order case folder must be the same as it was
496  if ( !$blRecalculatingOrder ) {
497  $this->_setFolder();
498  }
499 
500  // marking as not finished
501  $this->_setOrderStatus( 'NOT_FINISHED' );
502 
503  //saving all order data to DB
504  $this->save();
505 
506  // executing payment (on failure deletes order and returns error code)
507  // in case when recalculating order, payment execution is skipped
508  if ( !$blRecalculatingOrder ) {
509  $blRet = $this->_executePayment( $oBasket, $oUserPayment );
510  if ( $blRet !== true ) {
511  return $blRet;
512  }
513  }
514 
515  if ( !$this->oxorder__oxordernr->value ) {
516  $this->_setNumber();
517  } else {
518  oxNew( 'oxCounter' )->update( $this->_getCounterIdent(), $this->oxorder__oxordernr->value );
519  }
520 
521  // executing TS protection
522  if ( !$blRecalculatingOrder && $oBasket->getTsProductId()) {
523  $blRet = $this->_executeTsProtection( $oBasket );
524  if ( $blRet !== true ) {
525  return $blRet;
526  }
527  }
528 
529  // deleting remark info only when order is finished
530  oxSession::deleteVar( 'ordrem' );
531  oxSession::deleteVar( 'stsprotection' );
532 
533  //#4005: Order creation time is not updated when order processing is complete
534  if ( !$blRecalculatingOrder ) {
535  $this-> _updateOrderDate();
536  }
537 
538  // updating order trans status (success status)
539  $this->_setOrderStatus( 'OK' );
540 
541  // store orderid
542  $oBasket->setOrderId( $this->getId() );
543 
544  // updating wish lists
545  $this->_updateWishlist( $oBasket->getContents(), $oUser );
546 
547  // updating users notice list
548  $this->_updateNoticeList( $oBasket->getContents(), $oUser );
549 
550  // marking vouchers as used and sets them to $this->_aVoucherList (will be used in order email)
551  // skipping this action in case of order recalculation
552  if ( !$blRecalculatingOrder ) {
553  $this->_markVouchers( $oBasket, $oUser );
554  }
555 
556  // send order by email to shop owner and current user
557  // skipping this action in case of order recalculation
558  if ( !$blRecalculatingOrder ) {
559  $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
560  } else {
561  $iRet = self::ORDER_STATE_OK;
562  }
563 
564  return $iRet;
565  }
566 
572  public function isNettoMode()
573  {
574  return (bool) $this->oxorder__oxisnettomode->value;
575  }
576 
577 
585  protected function _setOrderStatus( $sStatus )
586  {
587  $oDb = oxDb::getDb();
588  $sQ = 'update oxorder set oxtransstatus='.$oDb->quote( $sStatus ).' where oxid='.$oDb->quote( $this->getId() );
589  $oDb->execute( $sQ );
590 
591  //updating order object
592  $this->oxorder__oxtransstatus = new oxField( $sStatus, oxField::T_RAW );
593  }
594 
602  protected function _convertVat( $sVat )
603  {
604  if ( strpos( $sVat, '.' ) < strpos( $sVat, ',' ) ) {
605  $sVat = str_replace( array( '.', ',' ), array( '', '.' ), $sVat );
606  } else {
607  $sVat = str_replace( ',', '', $sVat );
608  }
609  return (float) $sVat;
610  }
611 
617  protected function _resetVats()
618  {
619  $this->oxorder__oxartvat1 = new oxField( null );
620  $this->oxorder__oxartvatprice1 = new oxField( null );
621  $this->oxorder__oxartvat2 = new oxField( null );
622  $this->oxorder__oxartvatprice2 = new oxField( null );
623  }
624 
635  protected function _loadFromBasket( oxBasket $oBasket )
636  {
637  $myConfig = $this->getConfig();
638 
639  // store IP Address - default must be FALSE as it is illegal to store
640  if ( $myConfig->getConfigParam( 'blStoreIPs' ) && $this->oxorder__oxip->value === null ) {
641  $this->oxorder__oxip = new oxField(oxRegistry::get("oxUtilsServer")->getRemoteAddress(), oxField::T_RAW);
642  }
643 
644  //setting view mode
645  $this->oxorder__oxisnettomode = new oxField($oBasket->isCalculationModeNetto());
646 
647  // copying main price info
648  $this->oxorder__oxtotalnetsum = new oxField($oBasket->getNettoSum());
649  $this->oxorder__oxtotalbrutsum = new oxField($oBasket->getBruttoSum());
650  $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
651 
652  // copying discounted VAT info
653  $this->_resetVats();
654  $iVatIndex = 1;
655  foreach ( $oBasket->getProductVats(false) as $iVat => $dPrice ) {
656  $this->{"oxorder__oxartvat$iVatIndex"} = new oxField( $this->_convertVat( $iVat ), oxField::T_RAW);
657  $this->{"oxorder__oxartvatprice$iVatIndex"} = new oxField($dPrice, oxField::T_RAW);
658  $iVatIndex ++;
659  }
660 
661  // payment costs if available
662  if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
663  $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
664  $this->oxorder__oxpayvat = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
665  }
666 
667  // delivery info
668  if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
669  $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
670  //V #M382: Save VAT, not VAT value for delivery costs
671  $this->oxorder__oxdelvat = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW); //V #M382
672  $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
673  }
674 
675  // user remark
676  if ( !isset( $this->oxorder__oxremark ) || $this->oxorder__oxremark->value === null ) {
677  $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
678  }
679 
680  // currency
681  $oCur = $myConfig->getActShopCurrencyObject();
682  $this->oxorder__oxcurrency = new oxField($oCur->name);
683  $this->oxorder__oxcurrate = new oxField($oCur->rate, oxField::T_RAW);
684 
685  // store voucher discount
686  if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
687  $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
688  }
689 
690  // general discount
691  if ( $this->_blReloadDiscount ) {
692  $dDiscount = 0;
693  $aDiscounts = $oBasket->getDiscounts();
694  if ( count($aDiscounts) > 0 ) {
695  foreach ($aDiscounts as $oDiscount) {
696  $dDiscount += $oDiscount->dDiscount;
697  }
698  }
699  $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
700  }
701 
702  //order language
703  $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
704 
705 
706  // initial status - 'ERROR'
707  $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
708 
709  // copies basket product info ...
710  $this->_setOrderArticles( $oBasket->getContents() );
711 
712  // copies wrapping info
713  $this->_setWrapping( $oBasket );
714 
715  // copies TS protection info
716  $this->_setTsProtection( $oBasket );
717  }
718 
725  public function getOrderLanguage()
726  {
727  if ( $this->_iOrderLang === null ) {
728  if ( isset( $this->oxorder__oxlang->value ) ) {
729  $this->_iOrderLang = oxRegistry::getLang()->validateLanguage( $this->oxorder__oxlang->value );
730  } else {
731  $this->_iOrderLang = oxRegistry::getLang()->getBaseLanguage();
732  }
733  }
734  return $this->_iOrderLang;
735  }
736 
744  protected function _setUser( $oUser )
745  {
746 
747  $this->oxorder__oxuserid = new oxField($oUser->getId());
748 
749  // bill address
750  $this->oxorder__oxbillcompany = clone $oUser->oxuser__oxcompany;
751  $this->oxorder__oxbillemail = clone $oUser->oxuser__oxusername;
752  $this->oxorder__oxbillfname = clone $oUser->oxuser__oxfname;
753  $this->oxorder__oxbilllname = clone $oUser->oxuser__oxlname;
754  $this->oxorder__oxbillstreet = clone $oUser->oxuser__oxstreet;
755  $this->oxorder__oxbillstreetnr = clone $oUser->oxuser__oxstreetnr;
756  $this->oxorder__oxbilladdinfo = clone $oUser->oxuser__oxaddinfo;
757  $this->oxorder__oxbillustid = clone $oUser->oxuser__oxustid;
758  $this->oxorder__oxbillcity = clone $oUser->oxuser__oxcity;
759  $this->oxorder__oxbillcountryid = clone $oUser->oxuser__oxcountryid;
760  $this->oxorder__oxbillstateid = clone $oUser->oxuser__oxstateid;
761  $this->oxorder__oxbillzip = clone $oUser->oxuser__oxzip;
762  $this->oxorder__oxbillfon = clone $oUser->oxuser__oxfon;
763  $this->oxorder__oxbillfax = clone $oUser->oxuser__oxfax;
764  $this->oxorder__oxbillsal = clone $oUser->oxuser__oxsal;
765 
766 
767  // delivery address
768  if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
769  // set delivery address
770  $this->oxorder__oxdelcompany = clone $oDelAdress->oxaddress__oxcompany;
771  $this->oxorder__oxdelfname = clone $oDelAdress->oxaddress__oxfname;
772  $this->oxorder__oxdellname = clone $oDelAdress->oxaddress__oxlname;
773  $this->oxorder__oxdelstreet = clone $oDelAdress->oxaddress__oxstreet;
774  $this->oxorder__oxdelstreetnr = clone $oDelAdress->oxaddress__oxstreetnr;
775  $this->oxorder__oxdeladdinfo = clone $oDelAdress->oxaddress__oxaddinfo;
776  $this->oxorder__oxdelcity = clone $oDelAdress->oxaddress__oxcity;
777  $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
778  $this->oxorder__oxdelstateid = clone $oDelAdress->oxaddress__oxstateid;
779  $this->oxorder__oxdelzip = clone $oDelAdress->oxaddress__oxzip;
780  $this->oxorder__oxdelfon = clone $oDelAdress->oxaddress__oxfon;
781  $this->oxorder__oxdelfax = clone $oDelAdress->oxaddress__oxfax;
782  $this->oxorder__oxdelsal = clone $oDelAdress->oxaddress__oxsal;
783  }
784  }
785 
793  protected function _setWrapping( oxBasket $oBasket )
794  {
795  $myConfig = $this->getConfig();
796 
797  // wrapping price
798  if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
799  $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
800  // wrapping VAT will be always calculated (#3757)
801  $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
802  }
803 
804  if ( ( $oGiftCardCost = $oBasket->getCosts( 'oxgiftcard' ) ) ) {
805  $this->oxorder__oxgiftcardcost = new oxField( $oGiftCardCost->getBruttoPrice(), oxField::T_RAW);
806  $this->oxorder__oxgiftcardvat = new oxField( $oGiftCardCost->getVAT(), oxField::T_RAW);
807  }
808 
809  // greetings card
810  $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
811 
812  // card text will be stored in database
813  $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
814  }
815 
824  protected function _setOrderArticles( $aArticleList )
825  {
826  // reset articles list
827  $this->_oArticles = oxNew( 'oxlist' );
828  $iCurrLang = $this->getOrderLanguage();
829 
830  // add all the products we have on basket to the order
831  foreach ( $aArticleList as $oContent ) {
832 
833  //$oContent->oProduct = $oContent->getArticle();
834  // #M773 Do not use article lazy loading on order save
835  $oProduct = $oContent->getArticle( true, null, true);
836 
837  // copy only if object is oxarticle type
838  if ( $oProduct->isOrderArticle() ) {
839  $oOrderArticle = $oProduct;
840  } else {
841 
842  // if order language doe not match product language - article must be reloaded in order language
843  if ( $iCurrLang != $oProduct->getLanguage() ) {
844  $oProduct->loadInLang( $iCurrLang, $oProduct->getProductId() );
845  }
846 
847  // set chosen select list
848  $sSelList = '';
849  if ( count( $aChosenSelList = $oContent->getChosenSelList() ) ) {
850  foreach ( $aChosenSelList as $oItem ) {
851  if ( $sSelList ) {
852  $sSelList .= ", ";
853  }
854  $sSelList .= "{$oItem->name} : {$oItem->value}";
855  }
856  }
857 
858  $oOrderArticle = oxNew( 'oxorderarticle' );
859  $oOrderArticle->setIsNewOrderItem( true );
860  $oOrderArticle->copyThis( $oProduct );
861  $oOrderArticle->setId();
862 
863  $oOrderArticle->oxorderarticles__oxartnum = clone $oProduct->oxarticles__oxartnum;
864  $oOrderArticle->oxorderarticles__oxselvariant = new oxField( trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->getRawValue() ), oxField::T_RAW );
865  $oOrderArticle->oxorderarticles__oxshortdesc = new oxField( $oProduct->oxarticles__oxshortdesc->getRawValue(), oxField::T_RAW );
866  // #M974: duplicated entries for the name of variants in orders
867  $oOrderArticle->oxorderarticles__oxtitle = new oxField( trim( $oProduct->oxarticles__oxtitle->getRawValue() ), oxField::T_RAW );
868 
869  // copying persistent parameters ...
870  if ( !is_array( $aPersParams = $oProduct->getPersParams() ) ) {
871  $aPersParams = $oContent->getPersParams();
872  }
873  if ( is_array( $aPersParams ) && count( $aPersParams )) {
874  $oOrderArticle->oxorderarticles__oxpersparam = new oxField( serialize( $aPersParams ), oxField::T_RAW );
875  }
876  }
877 
878  // ids, titles, numbers ...
879  $oOrderArticle->oxorderarticles__oxorderid = new oxField( $this->getId() );
880  $oOrderArticle->oxorderarticles__oxartid = new oxField( $oContent->getProductId() );
881  $oOrderArticle->oxorderarticles__oxamount = new oxField( $oContent->getAmount() );
882 
883  // prices
884  $oPrice = $oContent->getPrice();
885  $oOrderArticle->oxorderarticles__oxnetprice = new oxField( $oPrice->getNettoPrice(), oxField::T_RAW );
886  $oOrderArticle->oxorderarticles__oxvatprice = new oxField( $oPrice->getVatValue(), oxField::T_RAW );
887  $oOrderArticle->oxorderarticles__oxbrutprice = new oxField( $oPrice->getBruttoPrice(), oxField::T_RAW );
888  $oOrderArticle->oxorderarticles__oxvat = new oxField( $oPrice->getVat(), oxField::T_RAW );
889 
890  $oUnitPtice = $oContent->getUnitPrice();
891  $oOrderArticle->oxorderarticles__oxnprice = new oxField( $oUnitPtice->getNettoPrice(), oxField::T_RAW );
892  $oOrderArticle->oxorderarticles__oxbprice = new oxField( $oUnitPtice->getBruttoPrice(), oxField::T_RAW );
893 
894  // wrap id
895  $oOrderArticle->oxorderarticles__oxwrapid = new oxField( $oContent->getWrappingId(), oxField::T_RAW );
896 
897  // items shop id
898  $oOrderArticle->oxorderarticles__oxordershopid = new oxField( $oContent->getShopId(), oxField::T_RAW );
899 
900  // bundle?
901  $oOrderArticle->oxorderarticles__oxisbundle = new oxField( $oContent->isBundle() );
902 
903  // add information for eMail
904  //P
905  //TODO: check if this assign is needed at all
906  $oOrderArticle->oProduct = $oProduct;
907 
908  $oOrderArticle->setArticle( $oProduct );
909 
910  // simulation order article list
911  $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
912  }
913  }
914 
926  protected function _executePayment( oxBasket $oBasket, $oUserpayment )
927  {
928  $oPayTransaction = $this->_getGateway();
929  $oPayTransaction->setPaymentParams( $oUserpayment );
930 
931  if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
932  $this->delete();
933 
934  // checking for error messages
935  if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
936  if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
937  return $sLastError;
938  }
939  }
940 
941  // checking for error codes
942  if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
943  if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
944  return $iLastErrorNo;
945  }
946  }
947 
948  return self::ORDER_STATE_PAYMENTERROR; // means no authentication
949  }
950  return true; // everything fine
951  }
952 
959  protected function _getGateway()
960  {
961  return oxNew( 'oxPaymentGateway' );
962  }
963 
971  protected function _setPayment( $sPaymentid )
972  {
973  // copying payment info fields
974  $aDynvalue = oxSession::getVar( 'dynvalue' );
975  $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
976 
977  // loading payment object
978  $oPayment = oxNew( 'oxpayment' );
979 
980  if (!$oPayment->load( $sPaymentid )) {
981  return null;
982  }
983 
984  // #756M Preserve already stored payment information
985  if ( !$aDynvalue && ( $oUserpayment = $this->getPaymentType() ) ) {
986  if ( is_array( $aStoredDynvalue = $oUserpayment->getDynValues() ) ) {
987  foreach ( $aStoredDynvalue as $oVal ) {
988  $aDynvalue[$oVal->name] = $oVal->value;
989  }
990  }
991  }
992 
993  $oPayment->setDynValues( oxRegistry::getUtils()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
994 
995  // collecting dynamic values
996  $aDynVal = array();
997 
998  if ( is_array( $aPaymentDynValues = $oPayment->getDynValues() ) ) {
999  foreach ( $aPaymentDynValues as $key => $oVal ) {
1000  if ( isset( $aDynvalue[$oVal->name] ) ) {
1001  $oVal->value = $aDynvalue[$oVal->name];
1002  }
1003 
1004  //$oPayment->setDynValue($key, $oVal);
1005  $aPaymentDynValues[$key] = $oVal;
1006  $aDynVal[$oVal->name] = $oVal->value;
1007  }
1008  }
1009 
1010  // Store this payment information, we might allow users later to
1011  // reactivate already give payment information
1012 
1013  $oUserpayment = oxNew( 'oxuserpayment' );
1014  $oUserpayment->oxuserpayments__oxuserid = clone $this->oxorder__oxuserid;
1015  $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
1016  $oUserpayment->oxuserpayments__oxvalue = new oxField(oxRegistry::getUtils()->assignValuesToText( $aDynVal ), oxField::T_RAW);
1017  $oUserpayment->oxpayments__oxdesc = clone $oPayment->oxpayments__oxdesc;
1018  $oUserpayment->oxpayments__oxlongdesc = clone $oPayment->oxpayments__oxlongdesc;
1019  $oUserpayment->setDynValues( $aPaymentDynValues );
1020  $oUserpayment->save();
1021 
1022  // storing payment information to order
1023  $this->oxorder__oxpaymentid = new oxField($oUserpayment->getId(), oxField::T_RAW);
1024  $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
1025 
1026  // returning user payment object which will be used later in code ...
1027  return $oUserpayment;
1028  }
1029 
1035  protected function _setFolder()
1036  {
1037  $myConfig = $this->getConfig();
1038  $this->oxorder__oxfolder = new oxField(key( $myConfig->getShopConfVar( 'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
1039  }
1040 
1050  protected function _updateWishlist( $aArticleList, $oUser )
1051  {
1052 
1053  foreach ( $aArticleList as $oContent) {
1054  if ( ( $sWishId = $oContent->getWishId() ) ) {
1055 
1056  // checking which wishlist user uses ..
1057  if ( $sWishId == $oUser->getId() ) {
1058  $oUserBasket = $oUser->getBasket( 'wishlist' );
1059  } else {
1060  $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
1061  $oUserBasket = oxNew( 'oxuserbasket' );
1062  $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
1063  }
1064 
1065  // updating users wish list
1066  if ( $oUserBasket ) {
1067  if ( !($sProdId = $oContent->getWishArticleId() )) {
1068  $sProdId = $oContent->getProductId();
1069  }
1070  $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
1071  $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
1072  if ( $dNewAmount < 0) {
1073  $dNewAmount = 0;
1074  }
1075  $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
1076  }
1077  }
1078  }
1079  }
1080 
1090  protected function _updateNoticeList( $aArticleList, $oUser )
1091  {
1092  // loading users notice list ..
1093  if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
1094  // only if wishlist is enabled
1095  foreach ( $aArticleList as $oContent) {
1096  $sProdId = $oContent->getProductId();
1097 
1098  // updating users notice list
1099  $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList(), $oContent->getPersParams() );
1100  $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
1101  if ( $dNewAmount < 0) {
1102  $dNewAmount = 0;
1103  }
1104  $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true, $oContent->getPersParams() );
1105  }
1106  }
1107  }
1108 
1114  protected function _updateOrderDate()
1115  {
1116  $oDb = oxDb::getDb();
1117  $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
1118  $sQ = 'update oxorder set oxorderdate=' . $oDb->quote( $sDate ) . ' where oxid=' . $oDb->quote( $this->getId() );
1119  $this->oxorder__oxorderdate = new oxField( $sDate, oxField::T_RAW );
1120  $oDb->execute( $sQ );
1121  }
1122 
1132  protected function _markVouchers( $oBasket, $oUser )
1133  {
1134  $this->_aVoucherList = $oBasket->getVouchers();
1135 
1136  if ( is_array( $this->_aVoucherList ) ) {
1137  foreach ( $this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
1138  $oVoucher = oxNew( 'oxvoucher' );
1139  $oVoucher->load( $sVoucherId );
1140  $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->dVoucherdiscount );
1141 
1142  $this->_aVoucherList[$sVoucherId] = $oVoucher;
1143  }
1144  }
1145  }
1146 
1152  public function save()
1153  {
1154  if ( ( $blSave = parent::save() ) ) {
1155 
1156  // saving order articles
1157  $oOrderArticles = $this->getOrderArticles();
1158  if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
1159  foreach ( $oOrderArticles as $oOrderArticle ) {
1160  $oOrderArticle->save();
1161  }
1162  }
1163  }
1164 
1165  return $blSave;
1166  }
1167 
1174  public function getDelAddressInfo()
1175  {
1176  $oDelAdress = null;
1177  if (! ($soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
1178  $soxAddressId = oxSession::getVar( 'deladrid' );
1179  }
1180  if ( $soxAddressId ) {
1181  $oDelAdress = oxNew( 'oxaddress' );
1182  $oDelAdress->load( $soxAddressId );
1183 
1184  //get delivery country name from delivery country id
1185  if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
1186  $oCountry = oxNew( 'oxcountry' );
1187  $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
1188  $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
1189  }
1190  }
1191  return $oDelAdress;
1192  }
1193 
1204  public function validateStock( $oBasket )
1205  {
1206  foreach ( $oBasket->getContents() as $key => $oContent ) {
1207  try {
1208  $oProd = $oContent->getArticle( 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 ) {
1221  $oEx = oxNew( 'oxOutOfStockException' );
1222  $oEx->setMessage( 'ERROR_MESSAGE_OUTOFSTOCK_OUTOFSTOCK' );
1223  $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
1224  $oEx->setProductId( $oProd->getId() );
1225 
1226  if (!is_numeric($iOnStock)) {
1227  $iOnStock = 0;
1228  }
1229  $oEx->setRemainingAmount( $iOnStock );
1230  throw $oEx;
1231  }
1232  }
1233  }
1234 
1240  protected function _insert()
1241  {
1242  $myConfig = $this->getConfig();
1243  $oUtilsDate = oxRegistry::get("oxUtilsDate");
1244 
1245  //V #M525 orderdate must be the same as it was
1246  if ( !$this->oxorder__oxorderdate->value ) {
1247  $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
1248  } else {
1249  $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
1250  }
1251 
1252  $this->oxorder__oxshopid = new oxField($myConfig->getShopId(), oxField::T_RAW);
1253  $this->oxorder__oxsenddate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
1254 
1255  $blInsert = parent::_insert();
1256 
1257  return $blInsert;
1258  }
1259 
1265  protected function _getCounterIdent()
1266  {
1267  $sCounterIdent = ( $this->_blSeparateNumbering ) ? 'oxOrder_' . $this->getConfig()->getShopId() : 'oxOrder';
1268  return $sCounterIdent;
1269  }
1270 
1271 
1277  protected function _setNumber()
1278  {
1279  $oDb = oxDb::getDb();
1280 
1281  $iCnt = oxNew( 'oxCounter' )->getNext( $this->_getCounterIdent() );
1282  $sQ = "update oxorder set oxordernr = ? where oxid = ?";
1283  $blUpdate = ( bool ) $oDb->execute( $sQ, array($iCnt, $this->getId() ) );
1284 
1285  if ( $blUpdate ) {
1286  $this->oxorder__oxordernr = new oxField( $iCnt );
1287  }
1288 
1289  return $blUpdate;
1290  }
1291 
1297  protected function _update()
1298  {
1299  $this->_aSkipSaveFields = array( 'oxtimestamp', 'oxorderdate' );
1300  $this->oxorder__oxsenddate = new oxField(oxRegistry::get("oxUtilsDate")->formatDBDate( $this->oxorder__oxsenddate->value, true ));
1301  return parent::_update();
1302  }
1303 
1312  public function delete( $sOxId = null )
1313  {
1314  if ( $sOxId ) {
1315  if ( !$this->load( $sOxId ) ) {
1316  // such order does not exist
1317  return false;
1318  }
1319  } elseif ( !$sOxId ) {
1320  $sOxId = $this->getId();
1321  }
1322 
1323  // no order id is passed
1324  if ( !$sOxId ) {
1325  return false;
1326  }
1327 
1328 
1329  // delete order articles
1330  $oOrderArticles = $this->getOrderArticles( false );
1331  foreach ( $oOrderArticles as $oOrderArticle ) {
1332  $oOrderArticle->delete();
1333  }
1334 
1335  // #440 - deleting user payment info
1336  if ( $oPaymentType = $this->getPaymentType() ) {
1337  $oPaymentType->delete();
1338  }
1339 
1340  return parent::delete( $sOxId );
1341  }
1342 
1352  public function recalculateOrder( $aNewArticles = array() )
1353  {
1354  oxDb::getDb()->startTransaction();
1355 
1356  try {
1357  $oBasket = $this->_getOrderBasket();
1358 
1359  // add this order articles to virtual basket and recalculates basket
1360  $this->_addOrderArticlesToBasket( $oBasket, $this->getOrderArticles( true ) );
1361 
1362  // adding new articles to existing order
1363  $this->_addArticlesToBasket( $oBasket, $aNewArticles );
1364 
1365  // recalculating basket
1366  $oBasket->calculateBasket( true );
1367 
1368  //finalizing order (skipping payment execution, vouchers marking and mail sending)
1369  $iRet = $this->finalizeOrder( $oBasket, $this->getOrderUser(), true );
1370 
1371  //if finalizing order failed, rollback transaction
1372  if ( $iRet !== 1 ) {
1373  oxDb::getDb()->rollbackTransaction();
1374  } else {
1375  oxDb::getDb()->commitTransaction();
1376  }
1377 
1378  } catch( Exception $oE ) {
1379  // if exception, rollBack everything
1380  oxDb::getDb()->rollbackTransaction();
1381 
1382  if ( defined( 'OXID_PHP_UNIT' ) ) {
1383  throw $oE;
1384  }
1385  }
1386  }
1387 
1388  protected $_oOrderBasket = null;
1396  protected function _getOrderBasket( $blStockCheck = true )
1397  {
1398  $this->_oOrderBasket = oxNew( "oxbasket" );
1399 
1400  //setting recalculation mode
1401  $this->_oOrderBasket->setCalculationModeNetto( $this->isNettoMode() );
1402 
1403  // setting stock check mode
1404  $this->_oOrderBasket->setStockCheckMode( $blStockCheck );
1405 
1406  // setting virtual basket user
1407  $this->_oOrderBasket->setBasketUser( $this->getOrderUser() );
1408 
1409  // transferring order id
1410  $this->_oOrderBasket->setOrderId( $this->getId() );
1411 
1412  // setting basket currency order uses
1413  $aCurrencies = $this->getConfig()->getCurrencyArray();
1414  foreach ( $aCurrencies as $oCur ) {
1415  if ($oCur->name == $this->oxorder__oxcurrency->value) {
1416  $oBasketCur = $oCur;
1417  break;
1418  }
1419  }
1420 
1421  // setting currency
1422  $this->_oOrderBasket->setBasketCurrency( $oBasketCur );
1423 
1424  // set basket card id and message
1425  $this->_oOrderBasket->setCardId( $this->oxorder__oxcardid->value );
1426  $this->_oOrderBasket->setCardMessage( $this->oxorder__oxcardtext->value );
1427 
1428  if ( $this->_blReloadDiscount ) {
1430  // disabling availability check
1431  $this->_oOrderBasket->setSkipVouchersChecking( true );
1432 
1433  // add previously used vouchers
1434  $sQ = 'select oxid from oxvouchers where oxorderid = '.$oDb->quote( $this->getId() );
1435  $aVouchers = $oDb->getAll( $sQ );
1436  foreach ( $aVouchers as $aVoucher ) {
1437  $this->_oOrderBasket->addVoucher( $aVoucher['oxid'] );
1438  }
1439  } else {
1440  $this->_oOrderBasket->setDiscountCalcMode( false );
1441  $this->_oOrderBasket->setVoucherDiscount( $this->oxorder__oxvoucherdiscount->value );
1442  $this->_oOrderBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
1443  }
1444 
1445  // must be kept old delivery?
1446  if ( !$this->_blReloadDelivery ) {
1447  $this->_oOrderBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
1448  } else {
1449  // set shipping
1450  $this->_oOrderBasket->setShipping( $this->oxorder__oxdeltype->value );
1451  $this->_oOrderBasket->setDeliveryPrice( null );
1452  }
1453 
1454  //set basket payment
1455  $this->_oOrderBasket->setPayment( $this->oxorder__oxpaymenttype->value );
1456 
1457  return $this->_oOrderBasket;
1458  }
1459 
1468  public function setDelivery( $sDeliveryId )
1469  {
1470  $this->reloadDelivery( true );
1471  $this->oxorder__oxdeltype = new oxField( $sDeliveryId );
1472  }
1473 
1479  public function getOrderUser()
1480  {
1481  if ($this->_oUser === null ) {
1482  $this->_oUser = oxNew( "oxuser" );
1483  $this->_oUser->load( $this->oxorder__oxuserid->value );
1484 
1485  // if object is loaded then reusing its order info
1486  if ( $this->_isLoaded ) {
1487  // bill address
1488  $this->_oUser->oxuser__oxcompany = clone $this->oxorder__oxbillcompany;
1489  $this->_oUser->oxuser__oxusername = clone $this->oxorder__oxbillemail;
1490  $this->_oUser->oxuser__oxfname = clone $this->oxorder__oxbillfname;
1491  $this->_oUser->oxuser__oxlname = clone $this->oxorder__oxbilllname;
1492  $this->_oUser->oxuser__oxstreet = clone $this->oxorder__oxbillstreet;
1493  $this->_oUser->oxuser__oxstreetnr = clone $this->oxorder__oxbillstreetnr;
1494  $this->_oUser->oxuser__oxaddinfo = clone $this->oxorder__oxbilladdinfo;
1495  $this->_oUser->oxuser__oxustid = clone $this->oxorder__oxbillustid;
1496 
1497 
1498  $this->_oUser->oxuser__oxcity = clone $this->oxorder__oxbillcity;
1499  $this->_oUser->oxuser__oxcountryid = clone $this->oxorder__oxbillcountryid;
1500  $this->_oUser->oxuser__oxstateid = clone $this->oxorder__oxbillstateid;
1501  $this->_oUser->oxuser__oxzip = clone $this->oxorder__oxbillzip;
1502  $this->_oUser->oxuser__oxfon = clone $this->oxorder__oxbillfon;
1503  $this->_oUser->oxuser__oxfax = clone $this->oxorder__oxbillfax;
1504  $this->_oUser->oxuser__oxsal = clone $this->oxorder__oxbillsal;
1505  }
1506  }
1507 
1508  return $this->_oUser;
1509  }
1510 
1518  public function pdfFooter( $oPdf )
1519  {
1520  }
1521 
1529  public function pdfHeaderplus( $oPdf )
1530  {
1531  }
1532 
1540  public function pdfHeader( $oPdf )
1541  {
1542  }
1543 
1552  public function genPdf( $sFilename, $iSelLang = 0 )
1553  {
1554  }
1555 
1561  public function getInvoiceNum()
1562  {
1563  $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
1564  return ( ( int ) oxDb::getDb()->getOne( $sQ, false ) + 1 );
1565  }
1566 
1572  public function getNextBillNum()
1573  {
1574  $sQ = 'select max(cast(oxorder.oxbillnr as unsigned)) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
1575  return ( ( int ) oxDb::getDb()->getOne( $sQ, false ) + 1 );
1576  }
1577 
1583  public function getShippingSetList()
1584  {
1585  // in which country we deliver
1586  if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
1587  $sShipId = $this->oxorder__oxbillcountryid->value;
1588  }
1589 
1590  $oBasket = $this->_getOrderBasket( false );
1591 
1592  // unsetting bundles
1593  $oOrderArticles = $this->getOrderArticles();
1594  foreach ( $oOrderArticles as $sItemId => $oItem ) {
1595  if ( $oItem->isBundle() ) {
1596  $oOrderArticles->offsetUnset( $sItemId );
1597  }
1598  }
1599 
1600  // add this order articles to basket and recalculate basket
1601  $this->_addOrderArticlesToBasket( $oBasket, $oOrderArticles );
1602 
1603  // recalculating basket
1604  $oBasket->calculateBasket( true );
1605 
1606  // load fitting deliveries list
1607  $oDeliveryList = oxNew( "oxDeliveryList", "core" );
1608  $oDeliveryList->setCollectFittingDeliveriesSets( true );
1609 
1610  return $oDeliveryList->getDeliveryList( $oBasket, $this->getOrderUser(), $sShipId );
1611  }
1612 
1618  public function getVoucherNrList()
1619  {
1621  $aVouchers = array();
1622  $sSelect = "select oxvouchernr from oxvouchers where oxorderid = ".$oDb->quote( $this->oxorder__oxid->value );
1623  $rs = $oDb->select( $sSelect );
1624  if ($rs != false && $rs->recordCount() > 0) {
1625  while (!$rs->EOF) {
1626  $aVouchers[] = $rs->fields['oxvouchernr'];
1627  $rs->moveNext();
1628  }
1629  }
1630  return $aVouchers;
1631  }
1632 
1640  public function getOrderSum( $blToday = false )
1641  {
1642  $sSelect = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
1643  $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
1644 
1645  if ( $blToday ) {
1646  $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
1647  }
1648 
1649  return ( double ) oxDb::getDb()->getOne( $sSelect, false, false );
1650  }
1651 
1659  public function getOrderCnt( $blToday = false )
1660  {
1661  $sSelect = 'select count(*) from oxorder where ';
1662  $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
1663 
1664  if ( $blToday ) {
1665  $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
1666  }
1667 
1668  return ( int ) oxDb::getDb()->getOne( $sSelect, false, false );
1669  }
1670 
1671 
1679  protected function _checkOrderExist( $sOxId = null )
1680  {
1681  if ( !$sOxId) {
1682  return false;
1683  }
1684 
1685  $oDb = oxDb::getDb();
1686  if ( $oDb->getOne( 'select oxid from oxorder where oxid = '.$oDb->quote( $sOxId ), false, false ) ) {
1687  return true;
1688  }
1689 
1690  return false;
1691  }
1692 
1702  protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
1703  {
1705 
1706  // add user, basket and payment to order
1707  $this->_oUser = $oUser;
1708  $this->_oBasket = $oBasket;
1709  $this->_oPayment = $oPayment;
1710 
1711  $oxEmail = oxNew( 'oxemail' );
1712 
1713  // send order email to user
1714  if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
1715  // mail to user was successfully sent
1716  $iRet = self::ORDER_STATE_OK;
1717  }
1718 
1719  // send order email to shop owner
1720  $oxEmail->sendOrderEMailToOwner( $this );
1721 
1722  return $iRet;
1723  }
1724 
1730  public function getBasket()
1731  {
1732  return $this->_oBasket;
1733  }
1734 
1740  public function getPayment()
1741  {
1742  return $this->_oPayment;
1743  }
1744 
1750  public function getVoucherList()
1751  {
1752  return $this->_aVoucherList;
1753  }
1754 
1760  public function getDelSet()
1761  {
1762  if ( $this->_oDelSet == null ) {
1763  // load deliveryset info
1764  $this->_oDelSet = oxNew( 'oxdeliveryset' );
1765  $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
1766  }
1767 
1768  return $this->_oDelSet;
1769  }
1770 
1776  public function getPaymentType()
1777  {
1778  if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType === null ) {
1779  $this->_oPaymentType = false;
1780  $oPaymentType = oxNew( 'oxuserpayment' );
1781  if ( $oPaymentType->load( $this->oxorder__oxpaymentid->value ) ) {
1782  $this->_oPaymentType = $oPaymentType;
1783  }
1784  }
1785 
1786  return $this->_oPaymentType;
1787  }
1788 
1794  public function getGiftCard()
1795  {
1796  if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
1797  $this->_oGiftCard = oxNew( 'oxwrapping' );
1798  $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
1799  }
1800 
1801  return $this->_oGiftCard;
1802  }
1803 
1811  public function setSeparateNumbering( $blSeparateNumbering = null )
1812  {
1813  $this->_blSeparateNumbering = $blSeparateNumbering;
1814  }
1815 
1823  public function getLastUserPaymentType( $sUserId)
1824  {
1825  $oDb = oxDb::getDb();
1826  $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid='.$oDb->quote( $sUserId ).' order by oxorder.oxorderdate desc ';
1827  $sLastPaymentId = $oDb->getOne( $sQ, false, false );
1828  return $sLastPaymentId;
1829  }
1830 
1839  protected function _addOrderArticlesToBasket( $oBasket, $aOrderArticles )
1840  {
1841  // if no order articles, return empty basket
1842  if ( count( $aOrderArticles ) > 0 ) {
1843 
1844  //adding order articles to basket
1845  foreach ( $aOrderArticles as $oOrderArticle ) {
1846  $oBasket->addOrderArticleToBasket( $oOrderArticle );
1847  }
1848  }
1849  }
1850 
1859  protected function _addArticlesToBasket( $oBasket, $aArticles )
1860  {
1861  // if no order articles
1862  if ( count($aArticles ) > 0 ) {
1863 
1864  //adding order articles to basket
1865  foreach ( $aArticles as $oArticle ) {
1866  $aSel = isset( $oArticle->oxorderarticles__oxselvariant ) ? $oArticle->oxorderarticles__oxselvariant->value : null;
1867  $aPersParam = isset( $oArticle->oxorderarticles__oxpersparam ) ? $oArticle->getPersParams() : null;
1868  $oBasket->addToBasket( $oArticle->oxorderarticles__oxartid->value,
1869  $oArticle->oxorderarticles__oxamount->value,
1870  $aSel, $aPersParam );
1871  }
1872  }
1873  }
1874 
1880  public function getTotalOrderSum()
1881  {
1882  $oCur = $this->getConfig()->getActShopCurrencyObject();
1883  return number_format( (double)$this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
1884  }
1885 
1893  public function getProductVats( $blFormatCurrency = true )
1894  {
1895  $aVats = array();
1896  if ($this->oxorder__oxartvat1->value) {
1897  $aVats[$this->oxorder__oxartvat1->value] = $this->oxorder__oxartvatprice1->value;
1898  }
1899  if ($this->oxorder__oxartvat2->value) {
1900  $aVats[$this->oxorder__oxartvat2->value] = $this->oxorder__oxartvatprice2->value;
1901  }
1902 
1903  if ( $blFormatCurrency ) {
1904  $oLang = oxRegistry::getLang();
1905  $oCur = $this->getConfig()->getActShopCurrencyObject();
1906  foreach ( $aVats as $sKey => $dVat ) {
1907  $aVats[$sKey] = $oLang->formatCurrency( $dVat, $oCur );
1908  }
1909  }
1910  return $aVats;
1911  }
1912 
1918  public function getBillCountry()
1919  {
1920  if ( !$this->oxorder__oxbillcountry->value ) {
1921  $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
1922  }
1923  return $this->oxorder__oxbillcountry;
1924  }
1925 
1931  public function getDelCountry()
1932  {
1933  if ( !$this->oxorder__oxdelcountry->value ) {
1934  $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
1935  }
1936  return $this->oxorder__oxdelcountry;
1937  }
1945  public function reloadDelivery( $blReload )
1946  {
1947  $this->_blReloadDelivery = $blReload;
1948  }
1949 
1957  public function reloadDiscount( $blReload )
1958  {
1959  $this->_blReloadDiscount = $blReload;
1960  }
1961 
1967  public function cancelOrder()
1968  {
1969  $this->oxorder__oxstorno = new oxField( 1 );
1970  if ( $this->save() ) {
1971 
1972 
1973  // canceling ordered products
1974  foreach ( $this->getOrderArticles() as $oOrderArticle ) {
1975 
1976 
1977  $oOrderArticle->cancelOrderArticle();
1978  }
1979  }
1980  }
1981 
1988  public function getOrderCurrency()
1989  {
1990  if ( $this->_oOrderCurrency === null ) {
1991 
1992  // setting default in case unrecognized currency was set during order
1993  $aCurrencies = $this->getConfig()->getCurrencyArray();
1994  $this->_oOrderCurrency = current( $aCurrencies );
1995 
1996  foreach ( $aCurrencies as $oCurr ) {
1997  if ( $oCurr->name == $this->oxorder__oxcurrency->value ) {
1998  $this->_oOrderCurrency = $oCurr;
1999  break;
2000  }
2001  }
2002  }
2003  return $this->_oOrderCurrency;
2004  }
2005 
2015  public function validateOrder( $oBasket, $oUser )
2016  {
2017  // validating stock
2018  $iValidState = $this->validateStock( $oBasket );
2019 
2020  if ( !$iValidState ) {
2021  // validating delivery
2022  $iValidState = $this->validateDelivery( $oBasket );
2023  }
2024 
2025  if ( !$iValidState ) {
2026  // validating payment
2027  $iValidState = $this->validatePayment( $oBasket );
2028  }
2029 
2030  if ( !$iValidState ) {
2031  //0003110 validating delivewry address, it is not be changed during checkout process
2032  $iValidState = $this->validateDeliveryAddress( $oUser );
2033  }
2034 
2035  if ( !$iValidState ) {
2036  // validatign minimum price
2037  $iValidState = $this->validateBasket( $oBasket );
2038  }
2039  return $iValidState;
2040  }
2041 
2049  public function validateBasket( $oBasket )
2050  {
2051  return $oBasket->isBelowMinOrderPrice() ? self::ORDER_STATE_BELOWMINPRICE : null;
2052  }
2053 
2062  public function validateDeliveryAddress( $oUser )
2063  {
2064  $sDelAddressMD5 = oxConfig::getParameter( 'sDeliveryAddressMD5' );
2065 
2066  $sDelAddress = $oUser->getEncodedDeliveryAddress();
2067 
2068  // if delivery address is set, add it to delivery address check
2069  if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
2070  $sDelAddress .= $oDelAdress->getEncodedDeliveryAddress();
2071  }
2072 
2073  if ( $sDelAddressMD5 != $sDelAddress ) {
2075  }
2076 
2077  return;
2078  }
2079 
2080 
2081 
2090  public function validateDelivery( $oBasket )
2091  {
2092  // proceed with no delivery
2093  // used for other countries
2094  if ( $oBasket->getPaymentId() == 'oxempty') {
2095  return;
2096  }
2097  $oDb = oxDb::getDb();
2098 
2099  $oDelSet = oxNew( "oxdeliveryset" );
2100  $sTable = $oDelSet->getViewName();
2101 
2102  $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
2103  $oDb->quote( $oBasket->getShippingId() )." and ".$oDelSet->getSqlActiveSnippet();
2104 
2105  if ( !$oDb->getOne( $sQ, false, false ) ) {
2106  // throwing exception
2108  }
2109  }
2110 
2119  public function validatePayment( $oBasket )
2120  {
2121  $oDb = oxDb::getDb();
2122 
2123  $oPayment = oxNew( "oxpayment" );
2124  $sTable = $oPayment->getViewName();
2125 
2126  $sQ = "select 1 from {$sTable} where {$sTable}.oxid=".
2127  $oDb->quote( $oBasket->getPaymentId() )." and ".$oPayment->getSqlActiveSnippet();
2128 
2129  if ( !$oDb->getOne( $sQ, false, false ) ) {
2131  }
2132  }
2133 
2141  protected function _setTsProtection( oxBasket $oBasket )
2142  {
2143  // protection price
2144  if ( ( $oTsProtectionCost = $oBasket->getCosts( 'oxtsprotection' ) ) ) {
2145  $this->oxorder__oxtsprotectcosts = new oxField($oTsProtectionCost->getBruttoPrice(), oxField::T_RAW);
2146  }
2147 
2148  // protection protduct id
2149  $this->oxorder__oxtsprotectid = new oxField($oBasket->getTsProductId(), oxField::T_RAW);
2150  }
2151 
2159  protected function _executeTsProtection( oxBasket $oBasket )
2160  {
2161  $aValues['tsProductId'] = $this->oxorder__oxtsprotectid->value;
2162  $aValues['amount'] = $oBasket->getPrice()->getBruttoPrice();
2163  $oCur = $this->getConfig()->getActShopCurrencyObject();
2164  $aValues['currency'] = $oCur->name;
2165  $aValues['buyerEmail'] = $this->oxorder__oxbillemail->value;
2166  $aValues['shopCustomerID'] = $this->oxorder__oxuserid->value;
2167  $aValues['shopOrderID'] = $this->oxorder__oxordernr->value;
2168  $aValues['orderDate'] = $this->oxorder__oxorderdate->value;
2169  $sPaymentId = $oBasket->getPaymentId();
2170 
2172  $oTsProtection = oxNew('oxtsprotection');
2173  $oTsProtection->requestForTsProtection( $aValues, $sPaymentId );
2174 
2175  return true;
2176  }
2177 
2183  public function getFormattedTotalNetSum()
2184  {
2185  return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxtotalnetsum->value, $this->getOrderCurrency() );
2186  }
2187 
2193  public function getFormattedTotalBrutSum()
2194  {
2195  return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $this->getOrderCurrency() );
2196  }
2197 
2203  public function getFormattedeliveryCost()
2204  {
2205  return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxdelcost->value, $this->getOrderCurrency() );
2206  }
2207 
2213  public function getFormattedPayCost()
2214  {
2215  return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxpaycost->value, $this->getOrderCurrency() );
2216  }
2217 
2223  public function getFormattedWrapCost()
2224  {
2225  return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxwrapcost->value, $this->getOrderCurrency() );
2226  }
2227 
2233  public function getFormattedGiftCardCost()
2234  {
2235  return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxgiftcardcost->value, $this->getOrderCurrency() );
2236  }
2237 
2243  public function getFormattedTotalVouchers()
2244  {
2245  return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $this->getOrderCurrency() );
2246  }
2247 
2253  public function getFormattedDiscount()
2254  {
2255  return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxdiscount->value, $this->getOrderCurrency() );
2256  }
2257 
2263  public function getFormattedTotalOrderSum()
2264  {
2265  return oxRegistry::getLang()->formatCurrency( $this->oxorder__oxtotalordersum->value, $this->getOrderCurrency() );
2266  }
2267 
2273  public function getShipmentTrackingUrl()
2274  {
2275  if ( $this->_sShipTrackUrl === null && $this->oxorder__oxtrackcode->value ) {
2276  $this->_sShipTrackUrl = "http://www.dpd.de/cgi-bin/delistrack?typ=1&amp;lang=de&amp;pknr=".$this->oxorder__oxtrackcode->value;
2277  }
2278 
2279  return $this->_sShipTrackUrl;
2280  }
2281 
2282 }