00001 <?php
00002
00006 require_once oxConfig::getInstance()->getConfigParam( 'sCoreDir' ) . 'fpdf/oxpdf.php';
00007
00012 class oxOrder extends oxBase
00013 {
00014
00020 protected $_aSkipSaveFields = array( 'oxorderdate' );
00021
00027 protected $_oArticles = null;
00028
00034 protected $_oDelSet = null;
00035
00041 protected $_oGiftCard = null;
00042
00048 protected $_oPaymentType = null;
00049
00055 protected $_oPayment = null;
00056
00062 protected $_aVoucherList = null;
00063
00069 protected $_oDelPrice = null;
00070
00076 protected $_oUser = null;
00077
00083 protected $_oBasket = null;
00084
00090 protected $_oWrappingPrice = null;
00091
00097 protected $_oPaymentPrice = null;
00098
00104 protected $_sClassName = 'oxorder';
00105
00111 protected $_blSeparateNumbering = null;
00112
00118 protected $_iOrderLang = null;
00119
00123 public function __construct()
00124 {
00125 parent::__construct();
00126 $this->init( 'oxorder' );
00127
00128
00129 $this->setSeparateNumbering( $this->getConfig()->getConfigParam( 'blSeparateNumbering') );
00130
00131 }
00132
00140 public function __get( $sName )
00141 {
00142 if ( $sName == 'oDelSet' ) {
00143 return $this->getDelSet();
00144 }
00145 }
00146
00154 public function assign( $dbRecord )
00155 {
00156
00157 parent::assign( $dbRecord );
00158
00159 $oUtilsDate = oxUtilsDate::getInstance();
00160
00161
00162 $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value));
00163 $this->oxorder__oxsenddate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value));
00164
00165
00166
00167 if ( !$this->oxorder__oxbillcountry->value ) {
00168 $this->oxorder__oxbillcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxbillcountryid->value ));
00169 }
00170
00171
00172 if ( !$this->oxorder__oxdelcountry->value ) {
00173 $this->oxorder__oxdelcountry = new oxField($this->_getCountryTitle( $this->oxorder__oxdelcountryid->value ));
00174 }
00175
00176
00177 $this->_setDeprecatedValues();
00178 }
00179
00187 protected function _getCountryTitle( $sCountryId )
00188 {
00189 $sTitle = null;
00190 if ( $sCountryId && $sCountryId != '-1' ) {
00191 $oCountry = oxNew( 'oxcountry' );
00192 $oCountry->load( $sCountryId );
00193 $sTitle = $oCountry->oxcountry__oxtitle->value;
00194 }
00195
00196 return $sTitle;
00197 }
00198
00204 public function getOrderArticles()
00205 {
00206 if ( $this->_oArticles == null ) {
00207
00208
00209 $this->_oArticles = oxNew( 'oxlist' );
00210 $this->_oArticles->init( 'oxorderarticle' );
00211
00212 $sSelect = 'select oxorderarticles.* from oxorderarticles
00213 where oxorderarticles.oxorderid="'.$this->getId().'"
00214 order by oxorderarticles.oxartid';
00215 $this->_oArticles->selectString( $sSelect );
00216 }
00217
00218 return $this->_oArticles;
00219 }
00220
00226 public function getOrderDeliveryPrice()
00227 {
00228 if ( $this->_oDelPrice != null ) {
00229 return $this->_oDelPrice;
00230 }
00231
00232 $this->_oDelPrice = oxNew( 'oxprice' );
00233 $this->_oDelPrice->setBruttoPriceMode();
00234 $this->_oDelPrice->setPrice( $this->oxorder__oxdelcost->value, $this->oxorder__oxdelvat->value );
00235 return $this->_oDelPrice;
00236 }
00237
00243 public function getOrderWrappingPrice()
00244 {
00245 if ( $this->_oWrappingPrice != null ) {
00246 return $this->_oWrappingPrice;
00247 }
00248
00249 $this->_oWrappingPrice = oxNew( 'oxprice' );
00250 $this->_oWrappingPrice->setBruttoPriceMode();
00251 $this->_oWrappingPrice->setPrice( $this->oxorder__oxwrapcost->value, $this->oxorder__oxwrapvat->value );
00252 return $this->_oWrappingPrice;
00253 }
00254
00260 public function getOrderPaymentPrice()
00261 {
00262 if ( $this->_oPaymentPrice != null ) {
00263 return $this->_oPaymentPrice;
00264 }
00265
00266 $this->_oPaymentPrice = oxNew( 'oxprice' );
00267 $this->_oPaymentPrice->setBruttoPriceMode();
00268 $this->_oPaymentPrice->setPrice( $this->oxorder__oxpaycost->value, $this->oxorder__oxpayvat->value );
00269 return $this->_oPaymentPrice;
00270 }
00271
00278 public function getOrderNetSum()
00279 {
00280 $dTotalNetSum = 0;
00281
00282 $dTotalNetSum += $this->oxorder__oxtotalnetsum->value;
00283 $dTotalNetSum += $this->getOrderDeliveryPrice()->getNettoPrice();
00284 $dTotalNetSum += $this->getOrderWrappingPrice()->getNettoPrice();
00285 $dTotalNetSum += $this->getOrderPaymentPrice()->getNettoPrice();
00286
00287 return $dTotalNetSum;
00288 }
00289
00310 public function finalizeOrder( oxBasket $oBasket, $oUser, $blRecalculatingOrder = false )
00311 {
00312
00313 $sGetChallenge = oxSession::getVar( 'sess_challenge' );
00314 if ( $this->_checkOrderExist( $sGetChallenge ) ) {
00315 oxUtils::getInstance()->logger( 'BLOCKER' );
00316
00317 return 3;
00318 }
00319
00320
00321 if ( !$blRecalculatingOrder ) {
00322
00323 $this->setId( $sGetChallenge );
00324 }
00325
00326
00327 $this->_setUser( $oUser );
00328
00329
00330 $this->_loadFromBasket( $oBasket );
00331
00332
00333 $oUserPayment = $this->_setPayment( $oBasket->getPaymentId() );
00334
00335
00336
00337 if ( !$blRecalculatingOrder ) {
00338 $this->_setFolder();
00339 }
00340
00341
00342 $this->save();
00343
00344
00345
00346 if ( !$blRecalculatingOrder ) {
00347 $blRet = $this->_executePayment( $oBasket, $oUserPayment );
00348 if ( $blRet !== true ) {
00349 return $blRet;
00350 }
00351 }
00352
00353
00354 oxSession::deleteVar( 'ordrem' );
00355
00356
00357 $this->_setOrderStatus( 'OK' );
00358
00359
00360 $oBasket->setOrderId( $this->getId() );
00361
00362
00363 $this->_updateStock();
00364
00365
00366 $this->_updateWishlist( $oBasket->getContents(), $oUser );
00367
00368
00369 $this->_updateNoticeList( $oBasket->getContents(), $oUser );
00370
00371
00372
00373 if ( !$blRecalculatingOrder ) {
00374 $this->_markVouchers( $oBasket, $oUser );
00375 }
00376
00377
00378
00379 if ( !$blRecalculatingOrder ) {
00380 $iRet = $this->_sendOrderByEmail( $oUser, $oBasket, $oUserPayment );
00381 } else {
00382 $iRet = 1;
00383 }
00384
00385 return $iRet;
00386 }
00387
00395 protected function _setOrderStatus( $sStatus )
00396 {
00397 $sQ = 'update oxorder set oxtransstatus="'.$sStatus.'" where oxid="'.$this->getId().'" ';
00398 oxDb::getDb()->execute( $sQ );
00399 }
00400
00411 protected function _loadFromBasket( oxBasket $oBasket )
00412 {
00413 $myConfig = $this->getConfig();
00414
00415
00416 if ( $myConfig->getConfigParam( 'blStoreIPs' ) && $this->oxorder__oxip->value === null ) {
00417 $this->oxorder__oxip = new oxField(oxUtilsServer::getInstance()->getRemoteAddress(), oxField::T_RAW);
00418 }
00419
00420
00421 $this->oxorder__oxtotalnetsum = new oxField(oxUtils::getInstance()->fRound($oBasket->getProductsPrice()->getNettoSum()), oxField::T_RAW);
00422 $this->oxorder__oxtotalbrutsum = new oxField($oBasket->getProductsPrice()->getBruttoSum(), oxField::T_RAW);
00423 $this->oxorder__oxtotalordersum = new oxField($oBasket->getPrice()->getBruttoPrice(), oxField::T_RAW);
00424
00425
00426 if ( ( $oPaymentCost = $oBasket->getCosts( 'oxpayment' ) ) ) {
00427 $this->oxorder__oxpaycost = new oxField($oPaymentCost->getBruttoPrice(), oxField::T_RAW);
00428 $this->oxorder__oxpayvat = new oxField($oPaymentCost->getVAT(), oxField::T_RAW);
00429 }
00430
00431
00432 if ( ( $oDeliveryCost = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00433 $this->oxorder__oxdelcost = new oxField($oDeliveryCost->getBruttoPrice(), oxField::T_RAW);
00434
00435 $this->oxorder__oxdelvat = new oxField($oDeliveryCost->getVAT(), oxField::T_RAW);
00436 $this->oxorder__oxdeltype = new oxField($oBasket->getShippingId(), oxField::T_RAW);
00437 }
00438
00439
00440 if ( $this->oxorder__oxremark->value === null ) {
00441 $this->oxorder__oxremark = new oxField(oxSession::getVar( 'ordrem' ), oxField::T_RAW);
00442 }
00443
00444
00445 $oCur = $myConfig->getActShopCurrencyObject();
00446 $this->oxorder__oxcurrency = new oxField($oCur->name);
00447 $this->oxorder__oxcurrate = new oxField($oCur->rate, oxField::T_RAW);
00448
00449
00450 if ( ( $oVoucherDiscount = $oBasket->getVoucherDiscount() ) ) {
00451 $this->oxorder__oxvoucherdiscount = new oxField($oVoucherDiscount->getBruttoPrice(), oxField::T_RAW);
00452 }
00453
00454
00455 $dDiscount = 0;
00456 $aDiscounts = $oBasket->getDiscounts();
00457 if ( count($aDiscounts) > 0 ) {
00458 foreach ($aDiscounts as $oDiscount) {
00459 $dDiscount += $oDiscount->dDiscount;
00460 }
00461 }
00462 if ( $dDiscount ) {
00463 $this->oxorder__oxdiscount = new oxField($dDiscount, oxField::T_RAW);
00464 }
00465
00466
00467 $this->oxorder__oxlang = new oxField( $this->getOrderLanguage() );
00468
00469
00470
00471 $this->oxorder__oxtransstatus = new oxField('ERROR', oxField::T_RAW);
00472
00473
00474 $this->_setOrderArticles( $oBasket->getContents() );
00475
00476
00477 $this->_setWrapping( $oBasket );
00478 }
00479
00486 public function getOrderLanguage()
00487 {
00488 if ( $this->_iOrderLang === null ) {
00489 if ( isset( $this->oxorder__oxlang->value ) ) {
00490 $this->_iOrderLang = oxLang::getInstance()->validateLanguage( $this->oxorder__oxlang->value );
00491 } else {
00492 $this->_iOrderLang = oxLang::getInstance()->getBaseLanguage();
00493 }
00494 }
00495 return $this->_iOrderLang;
00496 }
00497
00505 protected function _setUser( $oUser )
00506 {
00507
00508 $this->oxorder__oxuserid = new oxField($oUser->getId());
00509
00510
00511 $this->oxorder__oxbillcompany = clone $oUser->oxuser__oxcompany;
00512 $this->oxorder__oxbillemail = clone $oUser->oxuser__oxusername;
00513 $this->oxorder__oxbillfname = clone $oUser->oxuser__oxfname;
00514 $this->oxorder__oxbilllname = clone $oUser->oxuser__oxlname;
00515 $this->oxorder__oxbillstreet = clone $oUser->oxuser__oxstreet;
00516 $this->oxorder__oxbillstreetnr = clone $oUser->oxuser__oxstreetnr;
00517 $this->oxorder__oxbilladdinfo = clone $oUser->oxuser__oxaddinfo;
00518 $this->oxorder__oxbillustid = clone $oUser->oxuser__oxustid;
00519 $this->oxorder__oxbillcity = clone $oUser->oxuser__oxcity;
00520 $this->oxorder__oxbillcountryid = clone $oUser->oxuser__oxcountryid;
00521 $this->oxorder__oxbillzip = clone $oUser->oxuser__oxzip;
00522 $this->oxorder__oxbillfon = clone $oUser->oxuser__oxfon;
00523 $this->oxorder__oxbillfax = clone $oUser->oxuser__oxfax;
00524 $this->oxorder__oxbillsal = clone $oUser->oxuser__oxsal;
00525
00526
00527
00528 if ( ( $oDelAdress = $this->getDelAddressInfo() ) ) {
00529
00530 $this->oxorder__oxdelcompany = clone $oDelAdress->oxaddress__oxcompany;
00531 $this->oxorder__oxdelfname = clone $oDelAdress->oxaddress__oxfname;
00532 $this->oxorder__oxdellname = clone $oDelAdress->oxaddress__oxlname;
00533 $this->oxorder__oxdelstreet = clone $oDelAdress->oxaddress__oxstreet;
00534 $this->oxorder__oxdelstreetnr = clone $oDelAdress->oxaddress__oxstreetnr;
00535 $this->oxorder__oxdeladdinfo = clone $oDelAdress->oxaddress__oxaddinfo;
00536 $this->oxorder__oxdelcity = clone $oDelAdress->oxaddress__oxcity;
00537 $this->oxorder__oxdelcountryid = clone $oDelAdress->oxaddress__oxcountryid;
00538 $this->oxorder__oxdelzip = clone $oDelAdress->oxaddress__oxzip;
00539 $this->oxorder__oxdelfon = clone $oDelAdress->oxaddress__oxfon;
00540 $this->oxorder__oxdelfax = clone $oDelAdress->oxaddress__oxfax;
00541 $this->oxorder__oxdelsal = clone $oDelAdress->oxaddress__oxsal;
00542 }
00543 }
00544
00552 protected function _setWrapping( oxBasket $oBasket )
00553 {
00554 $myConfig = $this->getConfig();
00555
00556
00557 if ( ( $oWrappingCost = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00558 $this->oxorder__oxwrapcost = new oxField($oWrappingCost->getBruttoPrice(), oxField::T_RAW);
00559
00560
00561 if ( $myConfig->getConfigParam( 'blCalcVatForWrapping' ) ) {
00562 $this->oxorder__oxwrapvat = new oxField($oWrappingCost->getVAT(), oxField::T_RAW);
00563 }
00564 }
00565
00566
00567 $this->oxorder__oxcardid = new oxField($oBasket->getCardId(), oxField::T_RAW);
00568
00569
00570 $this->oxorder__oxcardtext = new oxField($oBasket->getCardMessage(), oxField::T_RAW);
00571 }
00572
00581 protected function _setOrderArticles( $aArticleList )
00582 {
00583
00584 $this->_oArticles = oxNew( 'oxlist' );
00585 $iCurrLang = $this->getOrderLanguage();
00586
00587
00588 foreach ( $aArticleList as $oContent ) {
00589
00590
00591 $oProduct = $oContent->getArticle();
00592
00593
00594 if ( $iCurrLang != $oProduct->getLanguage() ) {
00595 $oProduct->loadInLang( $iCurrLang, $oProduct->getId() );
00596 }
00597
00598 $aSelList = array();
00599
00600 $sSelList = '';
00601 if ( count($oContent->getChosenSelList()) ) {
00602 foreach ( $oContent->getChosenSelList() as $oItem ) {
00603 $aSelList[] = $oItem->name.' : '.$oItem->value;
00604 }
00605 $sSelList = implode( ', ', $aSelList );
00606 }
00607
00608 $oOrderArticle = oxNew( 'oxorderarticle' );
00609
00610
00611 $oOrderArticle->copyThis( $oProduct );
00612
00613
00614
00615 $oOrderArticle->setId();
00616 $oOrderArticle->oxorderarticles__oxorderid = new oxField($this->getId());
00617 $oOrderArticle->oxorderarticles__oxartid = new oxField($oContent->getProductId());
00618 $oOrderArticle->oxorderarticles__oxamount = new oxField($oContent->getAmount());
00619
00620
00621 $oOrderArticle->oxorderarticles__oxartnum = clone $oProduct->oxarticles__oxartnum;
00622
00623
00624 $oOrderArticle->oxorderarticles__oxselvariant = new oxField(trim( $sSelList.' '.$oProduct->oxarticles__oxvarselect->value ), oxField::T_RAW);
00625
00626
00627 $oOrderArticle->oxorderarticles__oxtitle = new oxField(trim( $oProduct->oxarticles__oxtitle->value.' '.$oOrderArticle->oxorderarticles__oxselvariant->value ), oxField::T_RAW);
00628
00629
00630 $oOrderArticle->oxorderarticles__oxshortdesc = new oxField( $oProduct->oxarticles__oxshortdesc->value, oxField::T_RAW);
00631
00632
00633 $oOrderArticle->oxorderarticles__oxnetprice = new oxField($oContent->getPrice()->getNettoPrice(), oxField::T_RAW);
00634 $oOrderArticle->oxorderarticles__oxvatprice = new oxField($oContent->getPrice()->getVATValue(), oxField::T_RAW);
00635 $oOrderArticle->oxorderarticles__oxbrutprice = new oxField($oContent->getPrice()->getBruttoPrice(), oxField::T_RAW);
00636 $oOrderArticle->oxorderarticles__oxnprice = new oxField($oContent->getUnitPrice()->getNettoPrice(), oxField::T_RAW);
00637 $oOrderArticle->oxorderarticles__oxbprice = new oxField($oContent->getUnitPrice()->getBruttoPrice(), oxField::T_RAW);
00638 $oOrderArticle->oxorderarticles__oxvat = new oxField($oContent->getPrice()->getVAT(), oxField::T_RAW);
00639
00640
00641 $oOrderArticle->oxorderarticles__oxwrapid = new oxField($oContent->getWrappingId(), oxField::T_RAW);
00642
00643
00644 $oOrderArticle->oxorderarticles__oxordershopid = new oxField($oContent->getShopId(), oxField::T_RAW);
00645
00646
00647 if ( count( $oProduct->getPersParams() ) ) {
00648 $oOrderArticle->oxorderarticles__oxpersparam = new oxField(serialize( $oProduct->getPersParams() ), oxField::T_RAW);
00649 } elseif ( count( $oContent->getPersParams() ) ) {
00650 $oOrderArticle->oxorderarticles__oxpersparam = new oxField(serialize( $oContent->getPersParams()), oxField::T_RAW);
00651 }
00652
00653
00654
00655
00656 $oOrderArticle->oProduct = $oProduct;
00657
00658
00659 $this->_oArticles->offsetSet( $oOrderArticle->getId(), $oOrderArticle );
00660 }
00661 }
00662
00674 protected function _executePayment( oxBasket $oBasket, $oUserpayment )
00675 {
00676 $oPayTransaction = $this->_getGateway();
00677 $oPayTransaction->setPaymentParams( $oUserpayment );
00678
00679 if ( !$oPayTransaction->executePayment( $oBasket->getPrice()->getBruttoPrice(), $this ) ) {
00680 $this->delete();
00681
00682
00683 if ( method_exists( $oPayTransaction, 'getLastError' ) ) {
00684 if ( ( $sLastError = $oPayTransaction->getLastError() ) ) {
00685 return $sLastError;
00686 }
00687 }
00688
00689
00690 if ( method_exists( $oPayTransaction, 'getLastErrorNo' ) ) {
00691 if ( ( $iLastErrorNo = $oPayTransaction->getLastErrorNo() ) ) {
00692 return $iLastErrorNo;
00693 }
00694 }
00695
00696 return 2;
00697 }
00698 return true;
00699 }
00700
00707 protected function _getGateway()
00708 {
00709 return oxNew( 'oxPaymentGateway' );
00710 }
00711
00719 protected function _setPayment( $sPaymentid )
00720 {
00721
00722 $aDynvalue = oxSession::getVar( 'dynvalue' );
00723 $aDynvalue = $aDynvalue ? $aDynvalue : oxConfig::getParameter( 'dynvalue' );
00724
00725
00726 $oPayment = oxNew( 'oxpayment' );
00727
00728 if (!$oPayment->load( $sPaymentid )) {
00729 return null;
00730 }
00731
00732
00733 if( !$aDynvalue && $oUserpayment = $this->getPaymentType() ) {
00734 $aStoredDynvalue = $oUserpayment->getDynValues();
00735 foreach ( $aStoredDynvalue as $oVal ){
00736 $aDynvalue[$oVal->name] = $oVal->value;
00737 }
00738 }
00739
00740 $oPayment->setDynValues( oxUtils::getInstance()->assignValuesFromText( $oPayment->oxpayments__oxvaldesc->value ) );
00741
00742
00743 $aDynVal = array();
00744
00745 $aPaymentDynValues = $oPayment->getDynValues();
00746 foreach ( $aPaymentDynValues as $key => $oVal ) {
00747 if ( isset( $aDynvalue[$oVal->name] ) ) {
00748 $oVal->value = $aDynvalue[$oVal->name];
00749 }
00750
00751
00752 $aPaymentDynValues[$key] = $oVal;
00753 $aDynVal[$oVal->name] = $oVal->value;
00754 }
00755
00756
00757
00758
00759 $oUserpayment = oxNew( 'oxuserpayment' );
00760 $oUserpayment->oxuserpayments__oxuserid = clone $this->oxorder__oxuserid;
00761 $oUserpayment->oxuserpayments__oxpaymentsid = new oxField($sPaymentid, oxField::T_RAW);
00762 $oUserpayment->oxuserpayments__oxvalue = new oxField(oxUtils::getInstance()->assignValuesToText( $aDynVal ), oxField::T_RAW);
00763 $oUserpayment->oxpayments__oxdesc = clone $oPayment->oxpayments__oxdesc;
00764 $oUserpayment->setDynValues( $aPaymentDynValues );
00765 $oUserpayment->save();
00766
00767
00768 $this->oxorder__oxpaymentid = new oxField($oUserpayment->getId(), oxField::T_RAW);
00769 $this->oxorder__oxpaymenttype = clone $oUserpayment->oxuserpayments__oxpaymentsid;
00770
00771
00772 return $oUserpayment;
00773 }
00774
00780 protected function _setFolder()
00781 {
00782 $myConfig = $this->getConfig();
00783 $this->oxorder__oxfolder = new oxField(key( $myConfig->getShopConfVar( 'aOrderfolder', $myConfig->getShopId() ) ), oxField::T_RAW);
00784 }
00785
00795 protected function _updateWishlist( $aArticleList, $oUser )
00796 {
00797
00798 foreach ( $aArticleList as $oContent) {
00799 if ( ( $sWishId = $oContent->getWishId() ) ) {
00800
00801
00802 if ( $sWishId == $oUser->getId() ) {
00803 $oUserBasket = $oUser->getBasket( 'wishlist' );
00804 } else {
00805 $aWhere = array( 'oxuserbaskets.oxuserid' => $sWishId, 'oxuserbaskets.oxtitle' => 'wishlist' );
00806 $oUserBasket = oxNew( 'oxuserbasket' );
00807 $oUserBasket->assignRecord( $oUserBasket->buildSelectString( $aWhere ) );
00808 }
00809
00810
00811 if ( $oUserBasket ) {
00812 if ( !($sProdId = $oContent->getWishArticleId() )) {
00813 $sProdId = $oContent->getProductId();
00814 }
00815 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00816 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00817 if ( $dNewAmount < 0) {
00818 $dNewAmount = 0;
00819 }
00820 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00821 }
00822 }
00823 }
00824 }
00825
00835 protected function _updateNoticeList( $aArticleList, $oUser )
00836 {
00837
00838 if ( $oUserBasket = $oUser->getBasket( 'noticelist' ) ) {
00839
00840 foreach ( $aArticleList as $oContent) {
00841 $sProdId = $oContent->getProductId();
00842
00843
00844 $oUserBasketItem = $oUserBasket->getItem( $sProdId, $oContent->getSelList() );
00845 $dNewAmount = $oUserBasketItem->oxuserbasketitems__oxamount->value - $oContent->getAmount();
00846 if ( $dNewAmount < 0) {
00847 $dNewAmount = 0;
00848 }
00849 $oUserBasket->addItemToBasket( $sProdId, $dNewAmount, $oContent->getSelList(), true );
00850 }
00851 }
00852 }
00853
00859 protected function _updateStock()
00860 {
00861 $myConfig = $this->getConfig();
00862 $blUseStock = $myConfig->getConfigParam( 'blUseStock' );
00863
00864
00865 $oOrderArticles = $this->getOrderArticles();
00866 if ( $oOrderArticles && count( $oOrderArticles ) > 0 && $blUseStock ) {
00867 foreach ( $oOrderArticles as $oOrderArticle ) {
00868 $oOrderArticle->updateArticleStock( $oOrderArticle->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
00869 }
00870 }
00871 }
00872
00884 protected function _markVouchers( $oBasket, $oUser )
00885 {
00886 $this->_aVoucherList = $oBasket->getVouchers();
00887
00888 if ( is_array( $this->_aVoucherList ) ) {
00889 foreach ( array_keys( $this->_aVoucherList ) as $sVoucherId ) {
00890 $oVoucher = oxNew( 'oxvoucher' );
00891 $oVoucher->load( $sVoucherId );
00892 $oVoucher->markAsUsed( $this->oxorder__oxid->value, $oUser->oxuser__oxid->value );
00893
00894
00895 $oVoucher->oxmodvouchers__oxvouchernr = $oVoucher->oxvouchers__oxvouchernr;
00896 $oVSerie = $oVoucher->getSerie();
00897 $oVoucher->oxmodvouchers__oxdiscount = clone $oVSerie->oxvoucherseries__oxdiscount;
00898 $oVoucher->oxmodvouchers__oxdiscounttype = clone $oVSerie->oxvoucherseries__oxdiscounttype;
00899
00900
00901 $this->_aVoucherList[$sVoucherId] = $oVoucher;
00902 }
00903 }
00904 }
00905
00911 public function save()
00912 {
00913 if ( ( $blSave = parent::save() ) ) {
00914
00915
00916 $oOrderArticles = $this->getOrderArticles();
00917 if ( $oOrderArticles && count( $oOrderArticles ) > 0 ) {
00918 foreach ( $oOrderArticles as $oOrderArticle ) {
00919 $oOrderArticle->save();
00920 }
00921 }
00922 }
00923
00924 return $blSave;
00925 }
00926
00933 public function getDelAddressInfo()
00934 {
00935 $oDelAdress = null;
00936 if ( ( $soxAddressId = oxConfig::getParameter( 'deladrid' ) ) ) {
00937 $oDelAdress = oxNew( 'oxbase' );
00938 $oDelAdress->init( 'oxaddress' );
00939 $oDelAdress->load( $soxAddressId );
00940
00941
00942 if ( $oDelAdress->oxaddress__oxcountryid->value && $oDelAdress->oxaddress__oxcountryid->value != -1 ) {
00943 $oCountry = oxNew( 'oxcountry' );
00944 $oCountry->load( $oDelAdress->oxaddress__oxcountryid->value );
00945 $oDelAdress->oxaddress__oxcountry = clone $oCountry->oxcountry__oxtitle;
00946 }
00947 }
00948 return $oDelAdress;
00949 }
00950
00961 public function validateStock( $oBasket )
00962 {
00963 foreach ( $oBasket->getContents() as $oContent ) {
00964 $oProd = $oContent->getArticle();
00965
00966
00967 $iOnStock = $oProd->checkForStock( $oContent->getAmount() );
00968 if ( $iOnStock !== true ) {
00969 $oEx = oxNew( 'oxOutOfStockException' );
00970 $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
00971 $oEx->setArticleNr( $oProd->oxarticles__oxartnum->value );
00972 $oEx->setRemainingAmount( $oProd->oxarticles__oxstock->value );
00973 throw $oEx;
00974 }
00975 }
00976 }
00977
00983 protected function _insert()
00984 {
00985 $myConfig = $this->getConfig();
00986 $oUtilsDate = oxUtilsDate::getInstance();
00987
00988
00989 if ( !$this->oxorder__oxorderdate->value ) {
00990 $this->oxorder__oxorderdate = new oxField(date( 'Y-m-d H:i:s', $oUtilsDate->getTime() ), oxField::T_RAW);
00991 } else {
00992 $this->oxorder__oxorderdate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxorderdate->value, true ));
00993 }
00994 $this->oxorder__oxshopid = new oxField($myConfig->getShopId(), oxField::T_RAW);
00995
00996 $this->oxorder__oxsenddate = new oxField( $oUtilsDate->formatDBDate( $this->oxorder__oxsenddate->value, true ));
00997
00998 if ( ( $blInsert = parent::_insert() ) ) {
00999
01000 if ( !$this->oxorder__oxordernr->value ) {
01001 $aWhere = '';
01002
01003 if ( $this->_blSeparateNumbering ) {
01004 $aWhere = array( 'oxshopid = "'.$myConfig->getShopId().'"' );
01005 }
01006 $this->_setRecordNumber( 'oxordernr', $aWhere );
01007 }
01008 }
01009 return $blInsert;
01010 }
01011
01017 protected function _update()
01018 {
01019 $this->oxorder__oxsenddate = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxorder__oxsenddate->value, true ));
01020 return parent::_update();
01021 }
01022
01031 public function delete( $sOxId = null )
01032 {
01033 if ( $sOxId ) {
01034 if ( !$this->load( $sOxId ) ) {
01035
01036 return false;
01037 }
01038 } elseif ( !$sOxId ) {
01039 $sOxId = $this->getId();
01040 }
01041
01042
01043 if ( !$sOxId ) {
01044 return false;
01045 }
01046
01047
01048
01049 $myConfig = $this->getConfig();
01050 $blUseStock = $myConfig->getConfigParam( 'blUseStock' );
01051
01052 $oOrderArticles = $this->getOrderArticles();
01053 foreach ( $oOrderArticles as $oOrderArticle ) {
01054
01055 if ( $blUseStock ) {
01056 if ( !$oOrderArticle->oxorderarticles__oxstorno->value ) {
01057 $oOrderArticle->updateArticleStock( $oOrderArticle->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
01058 }
01059 }
01060 $oOrderArticle->delete();
01061 }
01062
01063
01064 if ( $oPaymentType = $this->getPaymentType() ) {
01065 $oPaymentType->delete();
01066 }
01067
01068 return parent::delete( $sOxId );
01069 }
01070
01082 public function recalculateOrder( $aNewOrderArticles = array(), $blChangeDelivery = false, $blChangeDiscount = false )
01083 {
01084 oxDb::startTransaction();
01085
01086 try {
01087
01088
01089 $this->delete();
01090
01091 $oUser = oxNew( "oxuser" );
01092 $oUser->load( $this->oxorder__oxuserid->value );
01093
01094
01095 $oBasket = oxNew( "oxBasket" );
01096
01097 $aCanceledArticles = array();
01098 $aArticlesIds = array();
01099
01100
01101 if ( $this->_oArticles = $this->getOrderArticles() ) {
01102 $this->_oArticles->rewind();
01103 while ( $oOrderArticle = $this->_oArticles->current() ) {
01104 $sOrderArticleId = $this->_oArticles->key();
01105
01106
01107 $aArticlesIds[$sOrderArticleId] = $oOrderArticle->oxorderarticles__oxartid->value;
01108
01109
01110
01111 if ( $oOrderArticle->oxorderarticles__oxstorno->value == '1') {
01112 $aCanceledArticles[$sOrderArticleId] = $oOrderArticle;
01113
01114
01115
01116 $this->_oArticles->offsetUnset( $sOrderArticleId );
01117 } else {
01118 $this->_oArticles->next();
01119 }
01120 }
01121 }
01122
01123
01124 foreach ($aNewOrderArticles as $oNewOrderArticle) {
01125
01126 $sNewOrderArtId = null;
01127 $blIsOldOrderArticle = false;
01128
01129
01130 if ( ( $sNewOrderArtId = array_search( $oNewOrderArticle->oxorderarticles__oxartid->value, $aArticlesIds ) ) !== false ) {
01131 $blIsOldOrderArticle = true;
01132 }
01133
01134
01135 if ( $oNewOrderArticle->oxorderarticles__oxamount->value == 0 ) {
01136
01137
01138 if ( array_key_exists( $sNewOrderArtId, $aCanceledArticles) ) {
01139
01140
01141 $this->_oArticles->offsetSet( $sNewOrderArtId, $aCanceledArticles[$sNewOrderArtId] );
01142
01143
01144
01145 unset($aCanceledArticles[$sNewOrderArtId]);
01146 }
01147 }
01148
01149 if ( $blIsOldOrderArticle ) {
01150
01151 $this->_oArticles->offsetGet( $sNewOrderArtId )->oxorderarticles__oxamount = clone $oNewOrderArticle->oxorderarticles__oxamount;
01152
01153 } else {
01154
01155 $this->_oArticles->offsetSet( $oNewOrderArticle->getId(), $oNewOrderArticle );
01156
01157 }
01158 }
01159
01160
01161 $oBasket = $this->_addOrderArticlesToBasket( $oUser, $this->_oArticles, $blChangeDelivery, $blChangeDiscount );
01162
01163
01164 $iRet = $this->finalizeOrder( $oBasket, $oUser, true );
01165
01166
01167 if ( count($aCanceledArticles) > 0 ) {
01168 foreach ($aCanceledArticles as $oCanceledOrderArticle ) {
01169 $oCanceledOrderArticle->save();
01170 }
01171 }
01172
01173
01174 if ( $iRet !== 1 ) {
01175 oxDb::rollbackTransaction();
01176 } else {
01177 oxDb::commitTransaction();
01178 }
01179
01180 } catch( Exception $oE ) {
01181
01182 oxDb::rollbackTransaction();
01183 }
01184 }
01185
01193 public function pdfFooter( $oPdf )
01194 {
01195 }
01196
01204 public function pdfHeaderplus( $oPdf )
01205 {
01206 }
01207
01215 public function pdfHeader( $oPdf )
01216 {
01217 }
01218
01227 public function genPdf( $sFilename, $iSelLang = 0 )
01228 {
01229 }
01230
01236 public function getInvoiceNum()
01237 {
01238 $sQ = 'select max(oxorder.oxinvoicenr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01239 return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01240 }
01241
01247 public function getNextBillNum()
01248 {
01249 $sQ = 'select max(oxorder.oxbillnr) from oxorder where oxorder.oxshopid = "'.$this->getConfig()->getShopId().'" ';
01250 return ( ( int ) oxDb::getDb()->getOne( $sQ ) + 1 );
01251 }
01252
01258 public function getShippingSetList()
01259 {
01260
01261 if ( !( $sShipId = $this->oxorder__oxdelcountryid->value ) ) {
01262 $sShipId = $this->oxorder__oxbillcountryid->value;
01263 }
01264
01265 $oUser = oxNew( "oxuser" );
01266 $oUser->load( $this->oxorder__oxuserid->value );
01267
01268
01269 $oBasket = $this->_addOrderArticlesToBasket( $oUser, $this->getOrderArticles(), false, false, false );
01270
01271
01272 $oDeliveryList = oxNew( "oxDeliveryList", "core" );
01273 $oDeliveryList->setCollectFittingDeliveriesSets( true );
01274
01275 return $oDeliveryList->getDeliveryList( $oBasket, $oUser, $sShipId );
01276 }
01277
01283 public function getVoucherNrList()
01284 {
01285 $oDB = oxDb::getDb( true );
01286 $aVouchers = array();
01287 $sSelect = "select oxvouchernr from oxvouchers where oxorderid = '".$this->oxorder__oxid->value."'";
01288 $rs = $oDB->execute( $sSelect);
01289 if ($rs != false && $rs->recordCount() > 0) {
01290 while (!$rs->EOF) {
01291 $aVouchers[] = $rs->fields['oxvouchernr'];
01292 $rs->moveNext();
01293 }
01294 }
01295 return $aVouchers;
01296 }
01297
01305 public function getOrderSum( $blToday = false )
01306 {
01307 $sSelect = 'select sum(oxtotalordersum / oxcurrate) from oxorder where ';
01308 $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01309
01310 if ( $blToday ) {
01311 $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01312 }
01313
01314 return ( double ) oxDb::getDb()->getOne( $sSelect );
01315 }
01316
01324 public function getOrderCnt( $blToday = false )
01325 {
01326 $sSelect = 'select count(*) from oxorder where ';
01327 $sSelect .= 'oxshopid = "'.$this->getConfig()->getShopId().'" and oxorder.oxstorno != "1" ';
01328
01329 if ( $blToday ) {
01330 $sSelect .= 'and oxorderdate like "'.date( 'Y-m-d').'%" ';
01331 }
01332
01333 return ( int ) oxDb::getDb()->getOne( $sSelect );
01334 }
01335
01336
01344 protected function _checkOrderExist( $sOxId = null )
01345 {
01346 if ( !$sOxId) {
01347 return false;
01348 }
01349
01350 if ( oxDb::getDb()->getOne( 'select oxid from oxorder where oxid = "'.$sOxId.'"' ) ) {
01351 return true;
01352 }
01353
01354 return false;
01355 }
01356
01366 protected function _sendOrderByEmail( $oUser = null, $oBasket = null, $oPayment = null )
01367 {
01368 $iRet = 0;
01369
01370
01371 $this->_oUser = $oUser;
01372 $this->_oBasket = $oBasket;
01373 $this->_oPayment = $oPayment;
01374
01375 $oxEmail = oxNew( 'oxemail' );
01376
01377
01378 if ( $oxEmail->sendOrderEMailToUser( $this ) ) {
01379
01380 $iRet = 1;
01381 }
01382
01383
01384 $oxEmail->sendOrderEMailToOwner( $this );
01385
01386 return $iRet;
01387 }
01388
01394 public function getUser()
01395 {
01396 return $this->_oUser;
01397 }
01398
01404 public function getBasket()
01405 {
01406 return $this->_oBasket;
01407 }
01408
01414 public function getPayment()
01415 {
01416 return $this->_oPayment;
01417 }
01418
01424 public function getVoucherList()
01425 {
01426 return $this->_aVoucherList;
01427 }
01428
01434 public function getDelSet()
01435 {
01436 if ( $this->_oDelSet == null ) {
01437
01438 $this->_oDelSet = oxNew( 'oxdeliveryset' );
01439 $this->_oDelSet->load( $this->oxorder__oxdeltype->value );
01440 }
01441
01442 return $this->_oDelSet;
01443 }
01444
01450 public function getPaymentType()
01451 {
01452 if ( $this->oxorder__oxpaymentid->value && $this->_oPaymentType == null ) {
01453 $this->_oPaymentType = oxNew( 'oxuserpayment' );
01454 $this->_oPaymentType->load( $this->oxorder__oxpaymentid->value );
01455 }
01456
01457 return $this->_oPaymentType;
01458 }
01459
01465 public function getGiftCard()
01466 {
01467 if ( $this->oxorder__oxcardid->value && $this->_oGiftCard == null ) {
01468 $this->_oGiftCard = oxNew( 'oxwrapping' );
01469 $this->_oGiftCard->load( $this->oxorder__oxcardid->value );
01470 }
01471
01472 return $this->_oGiftCard;
01473 }
01474
01482 public function setSeparateNumbering( $blSeparateNumbering = null )
01483 {
01484 $this->_blSeparateNumbering = $blSeparateNumbering;
01485 }
01486
01494 public function getLastUserPaymentType( $sUserId)
01495 {
01496 $sQ = 'select oxorder.oxpaymenttype from oxorder where oxorder.oxshopid="'.$this->getConfig()->getShopId().'" and oxorder.oxuserid="'.$sUserId.'" order by oxorder.oxorderdate desc ';
01497 $sLastPaymentId = oxDb::getDb()->getOne( $sQ );
01498 return $sLastPaymentId;
01499 }
01500
01511 protected function _makeSelListArray( $sArtId = null, $sOrderArtSelList = null )
01512 {
01513 $aList = array();
01514 $aRet = array();
01515
01516 if ( $sArtId ) {
01517 $aList = explode( ",", $sOrderArtSelList );
01518 $oStr = getStr();
01519
01520
01521 $oArticle = oxNew( "oxArticle" );
01522 $oArticle->load( $sArtId );
01523 $aArticleSelList = $oArticle->getSelectLists();
01524
01525
01526 foreach ( $aList as $sList ) {
01527 if ( $sList ) {
01528
01529 $aVal = explode( ":", $sList );
01530 if ( isset($aVal[0]) && isset($aVal[1])) {
01531 $sOrderArtListTitle = $oStr->strtolower( trim($aVal[0]) );
01532 $sOrderArtSelValue = $oStr->strtolower( trim($aVal[1]) );
01533
01534
01535 $iSelListNum = 0;
01536 if ( count($aArticleSelList) > 0 ) {
01537 foreach ( $aArticleSelList as $aSelect ) {
01538
01539
01540 if ( $oStr->strtolower($aSelect['name']) == $sOrderArtListTitle ) {
01541
01542 $iSelValueNum = 0;
01543 foreach ( $aSelect as $oSel ) {
01544 if ( $oStr->strtolower($oSel->name) == $sOrderArtSelValue ) {
01545
01546 $aRet[$iSelListNum] = $iSelValueNum;
01547 }
01548
01549 $iSelValueNum++;
01550 }
01551 }
01552
01553 $iSelListNum++;
01554 }
01555 }
01556 }
01557 }
01558 }
01559 }
01560
01561 return $aRet;
01562 }
01563
01575 protected function _addOrderArticlesToBasket( $oUser = null, $aOrderArticles = null, $blChangeDelivery = false, $blChangeDiscount = false, $blStockCheck = true )
01576 {
01577 $oBasket = oxNew( "oxbasket" );
01578
01579
01580 $oBasket->setStockCheckMode( $blStockCheck );
01581
01582
01583 $oBasket->setBasketUser( $oUser );
01584
01585
01586 $aCurrencies = $this->getConfig()->getCurrencyArray();
01587 foreach ( $aCurrencies as $oCur ) {
01588 if ($oCur->name == $this->oxorder__oxcurrency->value) {
01589 $oBasketCur = $oCur;
01590 break;
01591 }
01592 }
01593
01594 $oBasket->setBasketCurrency( $oBasketCur );
01595
01596
01597 if ( count($aOrderArticles ) < 1 ) {
01598 return $oBasket;
01599 }
01600
01601
01602 foreach ( $aOrderArticles as $oOrderArticle ) {
01603
01604 $aPersParam = null;
01605 if ( $oOrderArticle->oxorderarticles__oxpersparam->value ) {
01606 $aPersParam = unserialize( $oOrderArticle->oxorderarticles__oxpersparam->value );
01607 }
01608
01609 $aSel = $this->_makeSelListArray( $oOrderArticle->oxorderarticles__oxartid->value,
01610 $oOrderArticle->oxorderarticles__oxselvariant->value );
01611
01612 $oBasketItem = $oBasket->addToBasket( $oOrderArticle->oxorderarticles__oxartid->value,
01613 $oOrderArticle->oxorderarticles__oxamount->value, $aSel, $aPersParam );
01614 if ( $oBasketItem ) {
01615 $oBasketItem->setWrapping( $oOrderArticle->oxorderarticles__oxwrapid->value );
01616 }
01617 }
01618
01619
01620 $oBasket->setCardId( $this->oxorder__oxcardid->value );
01621 $oBasket->setCardMessage( $this->oxorder__oxcardtext->value );
01622
01623
01624 $oBasket->setSkipVouchersChecking( true );
01625
01626
01627 $sQ = 'select oxid from oxvouchers where oxorderid = "'.$this->getId().'"';
01628 $aVouchers = oxDb::getDb( true )->getAll( $sQ );
01629 foreach ( $aVouchers AS $aVoucher ) {
01630 $oBasket->addVoucher($aVoucher['oxid']);
01631 }
01632
01633
01634 $oBasket->setShipping( $this->oxorder__oxdeltype->value );
01635
01636
01637 if ( $blChangeDelivery ) {
01638 $oBasket->setDeliveryPrice( $this->getOrderDeliveryPrice() );
01639 }
01640 if ( $blChangeDiscount ) {
01641 $oBasket->setTotalDiscount( $this->oxorder__oxdiscount->value );
01642 }
01643
01644 $oBasket->setPayment( $this->oxorder__oxpaymenttype->value );
01645
01646
01647 $oBasket->calculateBasket( true );
01648
01649 return $oBasket;
01650 }
01651
01659 protected function _setDeprecatedValues()
01660 {
01661 if ( $this->oxorder__oxstorno->value != 1 ) {
01662 $oCur = $this->getConfig()->getActShopCurrencyObject();
01663 $oLang = oxLang::getInstance();
01664
01665 $this->totalnetsum = $this->oxorder__oxtotalnetsum->value;
01666 $this->totalbrutsum = $this->oxorder__oxtotalbrutsum->value;
01667 $this->totalorder = $this->oxorder__oxtotalordersum->value;
01668 $this->ftotalnetsum = $oLang->formatCurrency( $this->oxorder__oxtotalnetsum->value, $oCur );
01669 $this->ftotalbrutsum = $oLang->formatCurrency( $this->oxorder__oxtotalbrutsum->value, $oCur );
01670 $this->fdelcost = $oLang->formatCurrency( $this->oxorder__oxdelcost->value, $oCur );
01671 $this->fpaycost = $oLang->formatCurrency( $this->oxorder__oxpaycost->value, $oCur );
01672 $this->fwrapcost = $oLang->formatCurrency( $this->oxorder__oxwrapcost->value, $oCur );
01673 $this->ftotalorder = $this->getTotalOrderSum();
01674 $this->totalvouchers = 0;
01675
01676 if ( $this->oxorder__oxvoucherdiscount->value ) {
01677 $this->totalvouchers = $oLang->formatCurrency( $this->oxorder__oxvoucherdiscount->value, $oCur );
01678 }
01679
01680 if ( $this->oxorder__oxdiscount->value ) {
01681 $this->discount = $this->oxorder__oxdiscount->value;
01682 $this->fdiscount = $oLang->formatCurrency( $this->oxorder__oxdiscount->value, $oCur );
01683 }
01684 }
01685 }
01686
01692 public function getTotalOrderSum()
01693 {
01694 $oCur = $this->getConfig()->getActShopCurrencyObject();
01695 return number_format( $this->oxorder__oxtotalordersum->value, $oCur->decimal, '.', '');
01696 }
01697 }