00001 <?php
00002
00006 require_once oxConfig::getInstance()->getConfigParam( 'sCoreDir' ) . 'fpdf/oxpdf.php';
00007
00013 class oxOrder extends oxBase
00014 {
00015
00021 protected $_aSkipSaveFields = array( 'oxorderdate' );
00022
00028 protected $_oArticles = null;
00029
00035 protected $_oDelSet = null;
00036
00042 protected $_oGiftCard = null;
00043
00049 protected $_oPaymentType = null;
00050
00056 protected $_oPayment = null;
00057
00063 protected $_aVoucherList = null;
00064
00070 protected $_oDelPrice = null;
00071
00077 protected $_oUser = null;
00078
00084 protected $_oBasket = null;
00085
00091 protected $_oWrappingPrice = null;
00092
00098 protected $_oPaymentPrice = null;
00099
00105 protected $_sClassName = 'oxorder';
00106
00112 protected $_blSeparateNumbering = null;
00113
00119 protected $_iOrderLang = null;
00120
00124 public function __construct()
00125 {
00126 parent::__construct();
00127 $this->init( 'oxorder' );
00128
00129
00130 $this->setSeparateNumbering( $this->getConfig()->getConfigParam( 'blSeparateNumbering') );
00131
00132 }
00133
00141 public function __get( $sName )
00142 {
00143 if ( $sName == 'oDelSet' ) {
00144 return $this->getDelSet();
00145 }
00146 }
00147
00155 public function assign( $dbRecord )
00156 {
00157
00158 parent::assign( $dbRecord );
00159
00160
00161 $this->oxorder__oxorderdate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxorderdate->value));
00162 $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value));
00163
00164
00165
00166 if ( !$this->oxorder__oxbillcountry->value ) {
00167 $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
00168 }
00169
00170
00171 if ( !$this->oxorder__oxdelcountry->value ) {
00172 $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
00173 }
00174
00175
00176 $this->_setDeprecatedValues();
00177 }
00178
00186 protected function _getCountryTitle( $sCountryId )
00187 {
00188 $sTitle = null;
00189 if ( $sCountryId && $sCountryId != '-1' ) {
00190 $oCountry = oxNew( 'oxcountry' );
00191 $oCountry->load( $sCountryId );
00192 $sTitle = $oCountry->oxcountry__oxtitle->value;
00193 }
00194
00195 return $sTitle;
00196 }
00197
00203 public function getOrderArticles()
00204 {
00205 if ( $this->_oArticles == null ) {
00206
00207
00208 $this->_oArticles = oxNew( 'oxlist' );
00209 $this->_oArticles->init( 'oxorderarticle' );
00210
00211 $sSelect = 'select oxorderarticles.* from oxorderarticles
00212 where oxorderarticles.oxorderid="'.$this->getId().'"
00213 order by oxorderarticles.oxartid';
00214 $this->_oArticles->selectString( $sSelect );
00215 }
00216
00217 return $this->_oArticles;
00218 }
00219
00225 public function getOrderDeliveryPrice()
00226 {
00227 if ( $this->_oDelPrice != null ) {
00228 return $this->_oDelPrice;
00229 }
00230
00231 $this->_oDelPrice = oxNew( 'oxprice' );
00232 $this->_oDelPrice->setBruttoPriceMode();
00233 $this->_oDelPrice->setPrice( $this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value );
00234 return $this->_oDelPrice;
00235 }
00236
00242 public function getOrderWrappingPrice()
00243 {
00244 if ( $this->_oWrappingPrice != null ) {
00245 return $this->_oWrappingPrice;
00246 }
00247
00248 $this->_oWrappingPrice = oxNew( 'oxprice' );
00249 $this->_oWrappingPrice->setBruttoPriceMode();
00250 $this->_oWrappingPrice->setPrice( $this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value );
00251 return $this->_oWrappingPrice;
00252 }
00253
00259 public function getOrderPaymentPrice()
00260 {
00261 if ( $this->_oPaymentPrice != null ) {
00262 return $this->_oPaymentPrice;
00263 }
00264
00265 $this->_oPaymentPrice = oxNew( 'oxprice' );
00266 $this->_oPaymentPrice->setBruttoPriceMode();
00267 $this->_oPaymentPrice->setPrice( $this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value );
00268 return $this->_oPaymentPrice;
00269 }
00270
00277 public function getOrderNetSum()
00278 {
00279 $dTotalNetSum = 0;
00280
00281 $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
00282 $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
00283 $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
00284 $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
00285
00286 return $dTotalNetSum;
00287 }
00288
00309 public function finalizeOrder( oxBasket $oBasket, $oUser, $blRecalculatingOrder = false )
00310 {
00311
00312 $sGetChallenge = oxSession::getVar( 'sess_challenge' );
00313 if ( $this->_checkOrderExist( $sGetChallenge ) ) {
00314 oxUtils::getInstance()->logger( 'BLOCKER' );
00315
00316 return 3;
00317 }
00318
00319
00320 if ( !$blRecalculatingOrder ) {
00321
00322 $this->setId( $sGetChallenge );
00323 }
00324
00325
00326 $this->_setUser( $oUser );
00327
00328
00329 $this->_loadFromBasket( $oBasket );
00330
00331
00332 $oUserPayment = $this->_setPayment( $oBasket->getPaymentId() );
00333
00334
00335 $this->save();
00336
00337
00338
00339 if ( !$blRecalculatingOrder ) {
00340 $blRet = $this->_executePayment( $oBasket, $oUserPayment );
00341 if ( $blRet !== true ) {
00342 return $blRet;
00343 }
00344 }
00345
00346
00347 oxSession::deleteVar( 'ordrem' );
00348
00349
00350 $this->_setOrderStatus( 'OK' );
00351
00352
00353 $oBasket->setOrderId( $this->getId() );
00354
00355
00356 $this->_updateStock();
00357
00358
00359 $this->_updateWishlist( $oBasket->getContents(), $oUser );
00360
00361
00362 $this->_updateNoticeList( $oBasket->getContents(), $oUser );
00363
00364
00365
00366 if ( !$blRecalculatingOrder ) {
00367 $this->_markVouchers( $oBasket, $oUser );
00368 }
00369
00370
00371
00372 if ( !$blRecalculatingOrder ) {
00373 $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
00374 } else {
00375 $iRet = 1;
00376 }
00377
00378 return $iRet;
00379 }
00380
00388 protected function _setOrderStatus( $sStatus )
00389 {
00390 $sQ = 'update oxorder set oxtransstatus="'.$sStatus.'" where oxid="'.$this->getId().'" ';
00391 oxDb::getDb()->execute( $sQ );
00392 }
00393
00404 protected function _loadFromBasket( oxBasket $oBasket )
00405 {
00406 $myConfig = $this->getConfig();
00407
00408
00409 if ( $myConfig->getConfigParam( 'blStoreIPs' ) ) {
00410 $this->oxorder__oxip = new oxField(oxUtilsServer::getInstance()->getRemoteAddress(), oxField::T_RAW);
00411 }
00412
00413
00414 $this->oxorder__oxtotalnetsum = new oxField(oxUtils::getInstance()->fRound($oBasket->getProductsPrice()->getNettoSum()), oxField::T_RAW);
00415 $this->oxorder__oxtotalbrutsum = new oxField($oBasket->getProductsPrice()->getBruttoSum(), oxField::T_RAW);
00416 $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00417
00418
00419 if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00420 $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00421 $this->oxorder__oxpayvat = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00422 }
00423
00424
00425 if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00426 $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00427
00428 $this->oxorder__oxdelvat = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW);
00429 $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00430 }
00431
00432
00433 $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00434
00435
00436 $oCur = $myConfig->getActShopCurrencyObject();
00437 $this->oxorder__oxcurrency = new oxField($oCur->name);
00438 $this->oxorder__oxcurrate = new oxField($oCur->rate, oxField::T_RAW);
00439
00440
00441 if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00442 $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00443 }
00444
00445
00446 if ( ( $oTotalDiscount = $oBasket->getTotalDiscount() ) ) {
00447 $this->oxorder__oxdiscount = new oxField($oTotalDiscount->getBruttoPrice(), oxField::T_RAW);
00448 }
00449
00450
00451 $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00452
00453
00454
00455 $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00456
00457
00458 $this->_setOrderArticles( $oBasket->getContents() );
00459
00460
00461 $this->_setWrapping( $oBasket );
00462 }
00463
00470 public function getOrderLanguage()
00471 {
00472 if ( $this->_iOrderLang === null ) {
00473 if ( isset( $this->oxorder__oxlang->value ) ) {
00474 $this->_iOrderLang = oxLang::getInstance()->validateLanguage( $this->oxorder__oxlang->value );
00475 } else {
00476 $this->_iOrderLang = oxLang::getInstance()->getBaseLanguage();
00477 }
00478 }
00479 return $this->_iOrderLang;
00480 }
00481
00489 protected function _setUser( $oUser )
00490 {
00491
00492 $this->oxorder__oxuserid = new oxField($oUser->getId());
00493
00494
00495 $this->oxorder__oxbillcompany = clone $oUser->oxuser__oxcompany;
00496 $this->oxorder__oxbillemail = clone $oUser->oxuser__oxusername;
00497 $this->oxorder__oxbillfname = clone $oUser->oxuser__oxfname;
00498 $this->oxorder__oxbilllname = clone $oUser->oxuser__oxlname;
00499 $this->oxorder__oxbillstreet = clone $oUser->oxuser__oxstreet;
00500 $this->oxorder__oxbillstreetnr = clone $oUser->oxuser__oxstreetnr;
00501 $this->oxorder__oxbilladdinfo = clone $oUser->oxuser__oxaddinfo;
00502 $this->oxorder__oxbillustid = clone $oUser->oxuser__oxustid;
00503 $this->oxorder__oxbillcity = clone $oUser->oxuser__oxcity;
00504 $this->oxorder__oxbillcountryid = clone $oUser->oxuser__oxcountryid;
00505 $this->oxorder__oxbillzip = clone $oUser->oxuser__oxzip;
00506 $this->oxorder__oxbillfon = clone $oUser->oxuser__oxfon;
00507 $this->oxorder__oxbillfax = clone $oUser->oxuser__oxfax;
00508 $this->oxorder__oxbillsal = clone $oUser->oxuser__oxsal;
00509
00510
00511
00512 if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00513
00514 $this->oxorder__oxdelcompany = clone $oDelAdress->oxaddress__oxcompany;
00515 $this->oxorder__oxdelfname = clone $oDelAdress->oxaddress__oxfname;
00516 $this->oxorder__oxdellname = clone $oDelAdress->oxaddress__oxlname;
00517 $this->oxorder__oxdelstreet = clone $oDelAdress->oxaddress__oxstreet;
00518 $this->oxorder__oxdelstreetnr = clone $oDelAdress->oxaddress__oxstreetnr;
00519 $this->oxorder__oxdeladdinfo = clone $oDelAdress->oxaddress__oxaddinfo;
00520 $this->oxorder__oxdelcity = clone $oDelAdress->oxaddress__oxcity;
00521 $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00522 $this->oxorder__oxdelzip = clone $oDelAdress->oxaddress__oxzip;
00523 $this->oxorder__oxdelfon = clone $oDelAdress->oxaddress__oxfon;
00524 $this->oxorder__oxdelfax = clone $oDelAdress->oxaddress__oxfax;
00525 $this->oxorder__oxdelsal = clone $oDelAdress->oxaddress__oxsal;
00526 }
00527 }
00528
00536 protected function _setWrapping( oxBasket $oBasket )
00537 {
00538 $myConfig = $this->getConfig();
00539
00540
00541 if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00542 $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00543
00544
00545 if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
00546 $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00547 }
00548 }
00549
00550
00551 $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00552
00553
00554 $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00555 }
00556
00565 protected function _setOrderArticles( $aArticleList )
00566 {
00567
00568 $this->_oArticles = oxNew( 'oxlist' );
00569 $iCurrLang = $this->getOrderLanguage();
00570
00571
00572 foreach ( $aArticleList as $oContent ) {
00573
00574
00575 $oProduct = $oContent->getArticle();
00576
00577
00578 if ( $iCurrLang != $oProduct->getLanguage() ) {
00579 $oProduct->loadInLang( $iCurrLang, $oProduct->getId() );
00580 }
00581
00582 $aSelList = array();
00583
00584 $sSelList = '';
00585 if ( count($oContent->getChosenSelList()) ) {
00586 foreach ( $oContent->getChosenSelList() as $oItem ) {
00587 $aSelList[] = $oItem->name.' : '.$oItem->value;
00588 }
00589 $sSelList = implode( ', ', $aSelList );
00590 }
00591
00592 $oOrderArticle = oxNew( 'oxorderarticle' );
00593
00594
00595 $oOrderArticle->copyThis( $oProduct );
00596
00597
00598
00599 $oOrderArticle->setId();
00600 $oOrderArticle->oxorderarticles__oxorderid = new oxField($this->getId());
00601 $oOrderArticle->oxorderarticles__oxartid = new oxField($oContent->getProductId());
00602 $oOrderArticle->oxorderarticles__oxamount = new oxField($oContent->getAmount());
00603
00604
00605 $oOrderArticle->oxorderarticles__oxartnum = clone $oProduct->oxarticles__oxartnum;
00606
00607
00608 $oOrderArticle->oxorderarticles__oxselvariant = new oxField(trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->value ), oxField::T_RAW);
00609
00610
00611 $oOrderArticle->oxorderarticles__oxtitle = new oxField(trim( $oProduct->oxarticles__oxtitle->value.' '.$oOrderArticle->oxorderarticles__oxselvariant->value ), oxField::T_RAW);
00612
00613
00614 $oOrderArticle->oxorderarticles__oxshortdesc = new oxField( $oProduct->oxarticles__oxshortdesc->value, oxField::T_RAW);
00615
00616
00617 $oOrderArticle->oxorderarticles__oxnetprice = new oxField($oContent->getPrice()->getNettoPrice(), oxField::T_RAW);
00618 $oOrderArticle->oxorderarticles__oxvatprice = new oxField($oContent->getPrice()->getVATValue(), oxField::T_RAW);
00619 $oOrderArticle->oxorderarticles__oxbrutprice = new oxField($oContent->getPrice()->getBruttoPrice(), oxField::T_RAW);
00620 $oOrderArticle->oxorderarticles__oxnprice = new oxField($oContent->getUnitPrice()->getNettoPrice(), oxField::T_RAW);
00621 $oOrderArticle->oxorderarticles__oxbprice = new oxField($oContent->getUnitPrice()->getBruttoPrice(), oxField::T_RAW);
00622 $oOrderArticle->oxorderarticles__oxvat = new oxField($oContent->getPrice()->getVAT(), oxField::T_RAW);
00623
00624
00625 $oOrderArticle->oxorderarticles__oxwrapid = new oxField($oContent->getWrappingId(), oxField::T_RAW);
00626
00627
00628 $oOrderArticle->oxorderarticles__oxordershopid = new oxField($oContent->getShopId(), oxField::T_RAW);
00629
00630
00631 if ( count( $oProduct->getPersParams() ) ) {
00632 $oOrderArticle->oxorderarticles__oxpersparam = new oxField(serialize( $oProduct->getPersParams() ), oxField::T_RAW);
00633 } elseif ( count( $oContent->getPersParams() ) ) {
00634 $oOrderArticle->oxorderarticles__oxpersparam = new oxField(serialize( $oContent->getPersParams()), oxField::T_RAW);
00635 }
00636
00637
00638
00639
00640 $oOrderArticle->oProduct = $oProduct;
00641
00642
00643 $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
00644 }
00645 }
00646
00658 protected function _executePayment( oxBasket $oBasket, $oUserpayment )
00659 {
00660 $oPayTransaction = $this->_getGateway();
00661 $oPayTransaction->setPaymentParams( $oUserpayment );
00662
00663 if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
00664 $this->delete();
00665
00666
00667 if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
00668 if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
00669 return $sLastError;
00670 }
00671 }
00672
00673
00674 if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
00675 if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
00676 return $iLastErrorNo;
00677 }
00678 }
00679
00680 return 2;
00681 }
00682 return true;
00683 }
00684
00691 protected function _getGateway()
00692 {
00693 return oxNew( 'oxPaymentGateway' );
00694 }
00695
00703 protected function _setPayment( $sPaymentid )
00704 {
00705
00706 $aDynvalue = oxSession::getVar( 'dynvalue' );
00707 $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
00708
00709
00710 $oPayment = oxNew( 'oxpayment' );
00711
00712 if (!$oPayment->load( $sPaymentid )) {
00713 return null;
00714 }
00715
00716 $oPayment->setDynValues( oxUtils::getInstance()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
00717
00718
00719 $aDynVal = array();
00720
00721 $aPaymentDynValues = $oPayment->getDynValues();
00722 foreach ( $aPaymentDynValues as $key => $oVal ) {
00723 if ( isset( $aDynvalue[$oVal->name] ) ) {
00724 $oVal->value = $aDynvalue[$oVal->name];
00725 }
00726
00727
00728 $aPaymentDynValues[$key] = $oVal;
00729 $aDynVal[$oVal->name] = $oVal->value;
00730 }
00731
00732
00733
00734
00735 $oUserpayment = oxNew( 'oxuserpayment' );
00736 $oUserpayment->oxuserpayments__oxuserid = clone $this->oxorder__oxuserid;
00737 $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
00738 $oUserpayment->oxuserpayments__oxvalue = new oxField(oxUtils::getInstance()->assignValuesToText( $aDynVal ), oxField::T_RAW);
00739 $oUserpayment->oxpayments__oxdesc = clone $oPayment->oxpayments__oxdesc;
00740 $oUserpayment->setDynValues( $aPaymentDynValues );
00741 $oUserpayment->save();
00742
00743
00744 $this->oxorder__oxpaymentid = new oxField($oUserpayment->getId(), oxField::T_RAW);
00745 $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
00746
00747
00748 return $oUserpayment;
00749 }
00750
00760 protected function _updateWishlist( $aArticleList, $oUser )
00761 {
00762
00763 foreach ( $aArticleList as $oContent) {
00764 if ( ( $sWishId = $oContent->getWishId() ) ) {
00765
00766
00767 if ( $sWishId == $oUser->getId() ) {
00768 $oUserBasket = $oUser->getBasket( 'wishlist' );
00769 } else {
00770 $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
00771 $oUserBasket = oxNew( 'oxuserbasket' );
00772 $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
00773 }
00774
00775
00776 if ( $oUserBasket ) {
00777 if ( !($sProdId = $oContent->getWishArticleId() )) {
00778 $sProdId = $oContent->getProductId();
00779 }
00780 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00781 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00782 if ( $dNewAmount < 0) {
00783 $dNewAmount = 0;
00784 }
00785 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00786 }
00787 }
00788 }
00789 }
00790
00800 protected function _updateNoticeList( $aArticleList, $oUser )
00801 {
00802
00803 if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
00804
00805 foreach ( $aArticleList as $oContent) {
00806 $sProdId = $oContent->getProductId();
00807
00808
00809 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00810 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00811 if ( $dNewAmount < 0) {
00812 $dNewAmount = 0;
00813 }
00814 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00815 }
00816 }
00817 }
00818
00824 protected function _updateStock()
00825 {
00826 $myConfig = $this->getConfig();
00827 $blUseStock = $myConfig->getConfigParam( 'blUseStock' );
00828
00829
00830 $oOrderArticles = $this->getOrderArticles();
00831 if ( $oOrderArticles && count( $oOrderArticles ) > 0 && $blUseStock ) {
00832 foreach ( $oOrderArticles as $oOrderArticle ) {
00833 $oOrderArticle->updateArticleStock( $oOrderArticle->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
00834 }
00835 }
00836 }
00837
00849 protected function _markVouchers( $oBasket, $oUser )
00850 {
00851 $this->_aVoucherList = $oBasket->getVouchers();
00852
00853 if ( is_array( $this->_aVoucherList ) ) {
00854 foreach ( array_keys( $this->_aVoucherList ) as $sVoucherId ) {
00855 $oVoucher = oxNew( 'oxvoucher' );
00856 $oVoucher->load( $sVoucherId );
00857 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value );
00858
00859
00860 $oVoucher->oxmodvouchers__oxvouchernr = $oVoucher->oxvouchers__oxvouchernr;
00861 $oVSerie = $oVoucher->getSerie();
00862 $oVoucher->oxmodvouchers__oxdiscount = clone $oVSerie->oxvoucherseries__oxdiscount;
00863 $oVoucher->oxmodvouchers__oxdiscounttype = clone $oVSerie->oxvoucherseries__oxdiscounttype;
00864
00865
00866 $this->_aVoucherList[$sVoucherId] = $oVoucher;
00867 }
00868 }
00869 }
00870
00876 public function save()
00877 {
00878 if ( ( $blSave = parent::save() ) ) {
00879
00880
00881 $oOrderArticles = $this->getOrderArticles();
00882 if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
00883 foreach ( $oOrderArticles as $oOrderArticle ) {
00884 $oOrderArticle->save();
00885 }
00886 }
00887 }
00888
00889 return $blSave;
00890 }
00891
00898 public function getDelAddressInfo()
00899 {
00900 $oDelAdress = null;
00901 if ( ( $soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
00902 $oDelAdress = oxNew( 'oxbase' );
00903 $oDelAdress->init( 'oxaddress' );
00904 $oDelAdress->load( $soxAddressId );
00905
00906
00907 if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
00908 $oCountry = oxNew( 'oxcountry' );
00909 $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
00910 $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
00911 }
00912 }
00913 return $oDelAdress;
00914 }
00915
00926 public function validateStock( $oBasket )
00927 {
00928 foreach ( $oBasket->getContents() as $oContent ) {
00929 $oProd = $oContent->getArticle();
00930
00931
00932 $iOnStock = $oProd->checkForStock( $oContent->getAmount() );
00933 if ( $iOnStock !== true ) {
00934 $oEx = oxNew( 'oxOutOfStockException' );
00935 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
00936 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
00937 $oEx->setRemainingAmount( $oProd->oxarticles__oxstock->value );
00938 throw $oEx;
00939 }
00940 }
00941 }
00942
00948 protected function _insert()
00949 {
00950 $myConfig = $this->getConfig();
00951
00952
00953 if ( !$this->oxorder__oxorderdate->value ) {
00954 $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() ), oxField::T_RAW);
00955 } else {
00956 $this->oxorder__oxorderdate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxorderdate->value, true ));
00957 }
00958 $this->oxorder__oxshopid = new oxField($myConfig->getShopId(), oxField::T_RAW);
00959 $this->oxorder__oxfolder = new oxField(key( $myConfig->getShopConfVar( 'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
00960 $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
00961
00962 if ( ( $blInsert = parent::_insert() ) ) {
00963
00964 if ( !$this->oxorder__oxordernr->value ) {
00965 $aWhere = '';
00966
00967 if ( $this->_blSeparateNumbering ) {
00968 $aWhere = array( 'oxshopid = "'.$myConfig->getShopId().'"' );
00969 }
00970 $this->_setRecordNumber( 'oxordernr', $aWhere );
00971 }
00972 }
00973 return $blInsert;
00974 }
00975
00981 protected function _update()
00982 {
00983 $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
00984 return parent::_update();
00985 }
00986
00995 public function delete( $sOxId = null )
00996 {
00997 if ( $sOxId ) {
00998 if ( !$this->load( $sOxId ) ) {
00999
01000 return false;
01001 }
01002 } elseif ( !$sOxId ) {
01003 $sOxId = $this->getId();
01004 }
01005
01006
01007 if ( !$sOxId ) {
01008 return false;
01009 }
01010
01011
01012
01013 $myConfig = $this->getConfig();
01014 $blUseStock = $myConfig->getConfigParam( 'blUseStock' );
01015
01016 $oOrderArticles = $this->getOrderArticles();
01017 foreach ( $oOrderArticles as $oOrderArticle ) {
01018
01019 if ( $blUseStock ) {
01020 if ( !$oOrderArticle->oxorderarticles__oxstorno->value ) {
01021 $oOrderArticle->updateArticleStock( $oOrderArticle->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
01022 }
01023 }
01024 $oOrderArticle->delete();
01025 }
01026
01027
01028 if ( $this->oxorder__oxpaymentid->value ) {
01029 $oDB = oxDb::getDb();
01030 $oDB->execute( 'delete from oxuserpayments where oxuserpayments.oxid = "'.$this->oxorder__oxpaymentid->value.'"' );
01031 }
01032
01033 return parent::delete( $sOxId );
01034 }
01035
01046 public function recalculateOrder( $aNewOrderArticles = array(), $blChangeDelivery = false )
01047 {
01048 oxDb::startTransaction();
01049
01050 try {
01051
01052
01053 $this->delete();
01054
01055 $oUser = oxNew( "oxuser" );
01056 $oUser->load( $this->oxorder__oxuserid->value );
01057
01058
01059 $oBasket = oxNew( "oxBasket" );
01060
01061 $aCanceledArticles = array();
01062 $aArticlesIds = array();
01063
01064
01065 if ( $this->_oArticles = $this->getOrderArticles() ) {
01066 $this->_oArticles->rewind();
01067 while ( $oOrderArticle = $this->_oArticles->current() ) {
01068 $sOrderArticleId = $this->_oArticles->key();
01069
01070
01071 $aArticlesIds[$sOrderArticleId] = $oOrderArticle->oxorderarticles__oxartid->value;
01072
01073
01074
01075 if ( $oOrderArticle->oxorderarticles__oxstorno->value == '1') {
01076 $aCanceledArticles[$sOrderArticleId] = $oOrderArticle;
01077
01078
01079
01080 $this->_oArticles->offsetUnset( $sOrderArticleId );
01081 } else {
01082 $this->_oArticles->next();
01083 }
01084 }
01085 }
01086
01087
01088 foreach ($aNewOrderArticles as $oNewOrderArticle) {
01089
01090 $sNewOrderArtId = null;
01091 $blIsOldOrderArticle = false;
01092
01093
01094 if ( ( $sNewOrderArtId = array_search( $oNewOrderArticle->oxorderarticles__oxartid->value, $aArticlesIds ) ) !== false ) {
01095 $blIsOldOrderArticle = true;
01096 }
01097
01098
01099 if ( $oNewOrderArticle->oxorderarticles__oxamount->value == 0 ) {
01100
01101
01102 if ( array_key_exists( $sNewOrderArtId, $aCanceledArticles) ) {
01103
01104
01105 $this->_oArticles->offsetSet( $sNewOrderArtId, $aCanceledArticles[$sNewOrderArtId] );
01106
01107
01108
01109 unset($aCanceledArticles[$sNewOrderArtId]);
01110 }
01111 }
01112
01113 if ( $blIsOldOrderArticle ) {
01114
01115 $this->_oArticles->offsetGet( $sNewOrderArtId )->oxorderarticles__oxamount = clone $oNewOrderArticle->oxorderarticles__oxamount;
01116
01117 } else {
01118
01119 $this->_oArticles->offsetSet( $oNewOrderArticle->getId(), $oNewOrderArticle );
01120
01121 }
01122 }
01123
01124
01125 $oBasket = $this->_addOrderArticlesToBasket( $oUser, $this->_oArticles, $blChangeDelivery );
01126
01127
01128 $iRet = $this->finalizeOrder( $oBasket, $oUser, true );
01129
01130
01131 if ( count($aCanceledArticles) > 0 ) {
01132 foreach($aCanceledArticles as $oCanceledOrderArticle ) {
01133 $oCanceledOrderArticle->save();
01134 }
01135 }
01136
01137
01138 if ( $iRet !== 1 ) {
01139 oxDb::rollbackTransaction();
01140 } else {
01141 oxDb::commitTransaction();
01142 }
01143
01144 } catch( Exception $oE ) {
01145
01146 oxDb::rollbackTransaction();
01147 }
01148 }
01149
01157 public function pdfFooter( $oPdf )
01158 {
01159 }
01160
01168 public function pdfHeaderplus( $oPdf )
01169 {
01170 }
01171
01179 public function pdfHeader( $oPdf )
01180 {
01181 }
01182
01191 public function genPdf( $sFilename, $iSelLang = 0 )
01192 {
01193 }
01194
01200 public function getInvoiceNum()
01201 {
01202 $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01203 return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01204 }
01205
01206
01212 public function getShippingSetList()
01213 {
01214 $myConfig = $this->getConfig();
01215
01216
01217 $sShipID = $this->oxorder__oxdelcountryid->value;
01218
01219 if (!$sShipID) {
01220 $sShipID = $this->oxorder__oxbillcountryid->value;
01221 }
01222
01223 $oUser = oxNew( "oxuser" );
01224 $oUser->load( $this->oxorder__oxuserid->value );
01225
01226
01227 $oBasket = $this->_addOrderArticlesToBasket( $oUser, $this->getOrderArticles() );
01228
01229 $aOrderDelSetList = array();
01230
01231
01232 $oDleliveryList = oxNew( "oxDeliveryList", "core");
01233 $oDleliveryList->setCollectFittingDeliveriesSets( true );
01234 $aOrderDelSetList = $oDleliveryList->getDeliveryList( $oBasket, $oUser, $sShipID, null );
01235
01236 return $aOrderDelSetList;
01237 }
01238
01244 public function getVoucherNrList()
01245 {
01246 $oDB = oxDb::getDb( true );
01247 $aVouchers = array();
01248 $sSelect = "select oxvouchernr from oxvouchers where oxorderid = '".$this->oxorder__oxid->value."'";
01249 $rs = $oDB->execute( $sSelect);
01250 if ($rs != false && $rs->recordCount() > 0) {
01251 while (!$rs->EOF) {
01252 $aVouchers[] = $rs->fields['oxvouchernr'];
01253 $rs->moveNext();
01254 }
01255 }
01256 return $aVouchers;
01257 }
01258
01266 public function getOrderSum( $blToday = false )
01267 {
01268 $sSelect = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01269 $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01270
01271 if ( $blToday ) {
01272 $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01273 }
01274
01275 return ( double ) oxDb::getDb()->getOne( $sSelect );
01276 }
01277
01285 public function getOrderCnt( $blToday = false )
01286 {
01287 $sSelect = 'select count(*) from oxorder where ';
01288 $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01289
01290 if ( $blToday ) {
01291 $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01292 }
01293
01294 return ( int ) oxDb::getDb()->getOne( $sSelect );
01295 }
01296
01297
01305 protected function _checkOrderExist( $sOxId = null )
01306 {
01307 if ( !$sOxId) {
01308 return false;
01309 }
01310
01311 if ( oxDb::getDb()->getOne( 'select oxid from oxorder where oxid = "'.$sOxId.'"' ) ) {
01312 return true;
01313 }
01314
01315 return false;
01316 }
01317
01327 protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01328 {
01329 $iRet = 0;
01330
01331
01332 $this->_oUser = $oUser;
01333 $this->_oBasket = $oBasket;
01334 $this->_oPayment = $oPayment;
01335
01336 $oxEmail = oxNew( 'oxemail' );
01337
01338
01339 if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01340
01341 $iRet = 1;
01342 }
01343
01344
01345 $oxEmail->sendOrderEMailToOwner( $this );
01346
01347 return $iRet;
01348 }
01349
01355 public function getUser()
01356 {
01357 return $this->_oUser;
01358 }
01359
01365 public function getBasket()
01366 {
01367 return $this->_oBasket;
01368 }
01369
01375 public function getPayment()
01376 {
01377 return $this->_oPayment;
01378 }
01379
01385 public function getVoucherList()
01386 {
01387 return $this->_aVoucherList;
01388 }
01389
01395 public function getDelSet()
01396 {
01397 if ( $this->_oDelSet == null ) {
01398
01399 $this->_oDelSet = oxNew( 'oxdeliveryset' );
01400 $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01401 }
01402
01403 return $this->_oDelSet;
01404 }
01405
01411 public function getPaymentType()
01412 {
01413 if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType == null ) {
01414 $this->_oPaymentType = oxNew( 'oxuserpayment' );
01415 $this->_oPaymentType->load( $this->oxorder__oxpaymentid->value );
01416 }
01417
01418 return $this->_oPaymentType;
01419 }
01420
01426 public function getGiftCard()
01427 {
01428 if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01429 $this->_oGiftCard = oxNew( 'oxwrapping' );
01430 $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01431 }
01432
01433 return $this->_oGiftCard;
01434 }
01435
01443 public function setSeparateNumbering( $blSeparateNumbering = null )
01444 {
01445 $this->_blSeparateNumbering = $blSeparateNumbering;
01446 }
01447
01455 public function getLastUserPaymentType( $sUserId)
01456 {
01457 $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid="'.$sUserId.'" order by oxorder.oxorderdate desc ';
01458 $sLastPaymentId = oxDb::getDb()->getOne( $sQ );
01459 return $sLastPaymentId;
01460 }
01461
01472 protected function _makeSelListArray( $sArtId = null, $sOrderArtSelList = null )
01473 {
01474 $aList = array();
01475 $aRet = array();
01476
01477 if ( $sArtId ) {
01478 $aList = explode( ",", $sOrderArtSelList );
01479
01480
01481 $oArticle = oxNew( "oxArticle" );
01482 $oArticle->load( $sArtId );
01483 $aArticleSelList = $oArticle->getSelectLists();
01484
01485
01486 foreach ( $aList as $sList ) {
01487 if ( $sList ) {
01488
01489 $aVal = explode( ":", $sList );
01490
01491 if ( isset($aVal[0]) && isset($aVal[1])) {
01492 $sOrderArtListTitle = strtolower( trim($aVal[0]) );
01493 $sOrderArtSelValue = strtolower( trim($aVal[1]) );
01494
01495
01496 $iSelListNum = 0;
01497 if ( count($aArticleSelList) > 0 ) {
01498 foreach ( $aArticleSelList as $aSelect ) {
01499
01500
01501 if ( strtolower($aSelect['name']) == $sOrderArtListTitle ) {
01502
01503 $iSelValueNum = 0;
01504 foreach ( $aSelect as $oSel ) {
01505 if ( strtolower($oSel->name) == $sOrderArtSelValue ) {
01506
01507 $aRet[$iSelListNum] = $iSelValueNum;
01508 }
01509
01510 $iSelValueNum++;
01511 }
01512 }
01513
01514 $iSelListNum++;
01515 }
01516 }
01517 }
01518 }
01519 }
01520 }
01521
01522 return $aRet;
01523 }
01524
01534 protected function _addOrderArticlesToBasket( $oUser = null, $aOrderArticles = null, $blChangeDelivery = false )
01535 {
01536 $myConfig = $this->getConfig();
01537
01538 $oBasket = oxNew( "oxbasket" );
01539
01540
01541 $oBasket->setBasketUser( $oUser );
01542
01543
01544 $aCurrencies = $myConfig->getCurrencyArray();
01545 foreach ($aCurrencies as $oCur) {
01546 if ($oCur->name == $this->oxorder__oxcurrency->value) {
01547 $oBasketCur = $oCur;
01548 break;
01549 }
01550 }
01551
01552 $oBasket->setBasketCurrency( $oBasketCur );
01553
01554
01555 if (count($aOrderArticles) < 1) {
01556 return $oBasket;
01557 }
01558
01559
01560 foreach ( $aOrderArticles as $oOrderArticle ) {
01561 $sArtId = $oOrderArticle->oxorderarticles__oxartid->value;
01562 $dAmount = $oOrderArticle->oxorderarticles__oxamount->value;
01563 $sSelVariant = $oOrderArticle->oxorderarticles__oxselvariant->value;
01564
01565 if ( $oOrderArticle->oxorderarticles__oxpersparam->value ) {
01566 $aPersParam = unserialize($oOrderArticle->oxorderarticles__oxpersparam->value);
01567 } else {
01568 $aPersParam = null;
01569 }
01570
01571 $aSel = $this->_makeSelListArray( $sArtId, $sSelVariant );
01572
01573 $oBasketItem = $oBasket->addToBasket( $sArtId, $dAmount, $aSel, $aPersParam );
01574 if ( $oBasketItem ) {
01575 $oBasketItem->setWrapping( $oOrderArticle->oxorderarticles__oxwrapid->value );
01576 }
01577 }
01578
01579
01580 $oBasket->setCardId( $this->oxorder__oxcardid->value );
01581 $oBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01582
01583
01584 $oBasket->setSkipVouchersChecking( true );
01585
01586
01587 $sQ = 'select oxid from oxvouchers where oxorderid = "'.$this->getId().'"';
01588 $aVouchers = oxDb::getDb( true )->getAll( $sQ );
01589 foreach ( $aVouchers AS $aVoucher ) {
01590 $oBasket->addVoucher($aVoucher['oxid']);
01591 }
01592
01593
01594 $oBasket->setShipping( $this->oxorder__oxdeltype->value );
01595
01596
01597 if ( $blChangeDelivery ) {
01598 $oBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01599 }
01600
01601 $oBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01602
01603
01604 $oBasket->calculateBasket( true );
01605
01606 return $oBasket;
01607 }
01608
01616 protected function _setDeprecatedValues()
01617 {
01618 if ( $this->oxorder__oxstorno->value != 1 ) {
01619 $oCur = $this->getConfig()->getActShopCurrencyObject();
01620
01621 $this->totalnetsum = $this->oxorder__oxtotalnetsum->value;
01622 $this->totalbrutsum = $this->oxorder__oxtotalbrutsum->value;
01623 $this->totalorder = $this->oxorder__oxtotalordersum->value;
01624 $this->ftotalnetsum = oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalnetsum->value, $oCur );
01625 $this->ftotalbrutsum = oxLang::getInstance()->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $oCur );
01626 $this->fdelcost = oxLang::getInstance()->formatCurrency( $this->oxorder__oxdelcost->value, $oCur );
01627 $this->fpaycost = oxLang::getInstance()->formatCurrency( $this->oxorder__oxpaycost->value, $oCur );
01628 $this->fwrapcost = oxLang::getInstance()->formatCurrency( $this->oxorder__oxwrapcost->value, $oCur );
01629 $this->ftotalorder = $this->getTotalOrderSum();
01630 $this->totalvouchers = 0;
01631
01632 if ( $this->oxorder__oxvoucherdiscount->value ) {
01633 $this->totalvouchers = oxLang::getInstance()->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $oCur );
01634 }
01635
01636 if ( $this->oxorder__oxdiscount->value ) {
01637 $this->discount = $this->oxorder__oxdiscount->value;
01638 $this->fdiscount = oxLang::getInstance()->formatCurrency( $this->oxorder__oxdiscount->value, $oCur );
01639 }
01640 }
01641 }
01642
01648 public function getTotalOrderSum()
01649 {
01650 $oCur = $this->getConfig()->getActShopCurrencyObject();
01651 return number_format( $this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01652 }
01653 }