oxorderarticle.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxOrderArticle extends oxBase implements oxIArticle
00008 {
00009 
00013     protected static $_aOrderCache = array();
00014 
00020     protected $_sClassName = 'oxorderarticle';
00021 
00027     protected $_aPersParam = null;
00028 
00034     protected $_aStatuses = null;
00035 
00041     protected $_aOrderArticleSelList = null;
00042 
00048     protected $_oOrderArticle = null;
00049 
00055     protected $_oArticle = null;
00056 
00062     protected $_blIsNewOrderItem = false;
00063 
00070     protected $_aSkipSaveFields = array( 'oxtimestamp' );
00071 
00075     public function __construct()
00076     {
00077         parent::__construct();
00078         $this->init( 'oxorderarticles' );
00079     }
00080 
00088     public function copyThis( $oProduct )
00089     {
00090         $aObjectVars = get_object_vars( $oProduct );
00091 
00092         foreach ( $aObjectVars as $sName => $sValue ) {
00093             if ( isset( $oProduct->$sName->value ) ) {
00094                 $sFieldName = preg_replace('/oxarticles__/', 'oxorderarticles__', $sName);
00095                 if ( $sFieldName != "oxorderarticles__oxtimestamp" ) {
00096                     $this->$sFieldName = $oProduct->$sName;
00097                 }
00098                 // formatting view
00099                 if ( !$this->getConfig()->getConfigParam( 'blSkipFormatConversion' ) ) {
00100                     if ( $sFieldName == "oxorderarticles__oxinsert" ) {
00101                         oxDb::getInstance()->convertDBDate( $this->$sFieldName, true );
00102                     }
00103                 }
00104             }
00105         }
00106 
00107     }
00108 
00116     public function assign( $dbRecord )
00117     {
00118         parent::assign( $dbRecord );
00119         $this->_setArticleParams();
00120     }
00121 
00132     public function updateArticleStock( $dAddAmount, $blAllowNegativeStock = false )
00133     {
00134         // TODO: use oxarticle reduceStock
00135         // decrement stock if there is any
00136         $oArticle = oxNew( 'oxarticle' );
00137         $oArticle->load( $this->oxorderarticles__oxartid->value );
00138         $oArticle->beforeUpdate();
00139 
00140         // get real article stock count
00141         $iStockCount = $this->_getArtStock( $dAddAmount, $blAllowNegativeStock );
00142         $oDb = oxDb::getDb();
00143 
00144         $oArticle->oxarticles__oxstock = new oxField($iStockCount);
00145         $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->oxorderarticles__oxartid->value ) );
00146         $oArticle->onChange( ACTION_UPDATE_STOCK );
00147 
00148         //update article sold amount
00149         $oArticle->updateSoldAmount( $dAddAmount * ( -1 ) );
00150     }
00151 
00160     protected function _getArtStock( $dAddAmount = 0, $blAllowNegativeStock = false )
00161     {
00162         $oDb = oxDb::getDb();
00163 
00164         // #1592A. must take real value
00165         $sQ = 'select oxstock from oxarticles where oxid = '.$oDb->quote( $this->oxorderarticles__oxartid->value );
00166         $iStockCount  = ( float ) $oDb->getOne( $sQ, false, false );
00167 
00168         $iStockCount += $dAddAmount;
00169 
00170         // #1592A. calculating according new stock option
00171         if ( !$blAllowNegativeStock && $iStockCount < 0 ) {
00172             $iStockCount = 0;
00173         }
00174 
00175         return $iStockCount;
00176     }
00177 
00178 
00184     public function getPersParams()
00185     {
00186         if ( $this->_aPersParam != null ) {
00187             return $this->_aPersParam;
00188         }
00189 
00190         if ( $this->oxorderarticles__oxpersparam->value ) {
00191             $this->_aPersParam = unserialize( $this->oxorderarticles__oxpersparam->value );
00192         }
00193 
00194         return $this->_aPersParam;
00195     }
00196 
00204     public function setPersParams( $aParams )
00205     {
00206         $this->_aPersParam = $aParams;
00207 
00208         // serializing persisten info stored while ordering
00209         $this->oxorderarticles__oxpersparam = new oxField(serialize( $aParams ), oxField::T_RAW);
00210     }
00211 
00221     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
00222     {
00223         $sFieldName = strtolower($sFieldName);
00224         switch ( $sFieldName ) {
00225             case 'oxpersparam':
00226             case 'oxorderarticles__oxpersparam':
00227             case 'oxerpstatus':
00228             case 'oxorderarticles__oxerpstatus':
00229             case 'oxtitle':
00230             case 'oxorderarticles__oxtitle':
00231                 $iDataType = oxField::T_RAW;
00232                 break;
00233         }
00234         return parent::_setFieldData($sFieldName, $sValue, $iDataType);
00235     }
00236 
00245     public function loadInLang( $iLanguage, $sOxid )
00246     {
00247         return $this->load( $sOxid );
00248     }
00249 
00255     public function getProductId()
00256     {
00257         return $this->oxorderarticles__oxartid->value;
00258     }
00259 
00265     public function getProductParentId()
00266     {
00267         // when this field will be introduced there will be no need to load from real article
00268         if ( isset( $this->oxorderarticles__oxartparentid ) && $this->oxorderarticles__oxartparentid->value !== false ) {
00269             return $this->oxorderarticles__oxartparentid->value;
00270         }
00271 
00272         $oDb = oxDb::getDb();
00273         $oArticle = oxNew( "oxarticle" );
00274         $sQ = "select oxparentid from " . $oArticle->getViewName() . " where oxid=" . $oDb->quote( $this->getProductId() );
00275         $this->oxarticles__oxparentid = new oxField( $oDb->getOne( $sQ ) );
00276         return $this->oxarticles__oxparentid->value;
00277     }
00278 
00284     protected function _setArticleParams()
00285     {
00286         // creating needed fields
00287         $this->oxarticles__oxstock  = $this->oxorderarticles__oxamount;
00288         $this->oxarticles__oxtitle  = $this->oxorderarticles__oxtitle;
00289         $this->oxarticles__oxwidth  = $this->oxorderarticles__oxwidth;
00290         $this->oxarticles__oxlength = $this->oxorderarticles__oxlength;
00291         $this->oxarticles__oxheight = $this->oxorderarticles__oxheight;
00292         $this->oxarticles__oxweight = $this->oxorderarticles__oxweight;
00293         $this->oxarticles__oxsubclass  = $this->oxorderarticles__oxsubclass;
00294         $this->oxarticles__oxartnum    = $this->oxorderarticles__oxartnum;
00295         $this->oxarticles__oxshortdesc = $this->oxorderarticles__oxshortdesc;
00296 
00297         $this->oxarticles__oxvat    = $this->oxorderarticles__oxvat;
00298         $this->oxarticles__oxprice  = $this->oxorderarticles__oxprice;
00299         $this->oxarticles__oxbprice = $this->oxorderarticles__oxbprice;
00300 
00301         $this->oxarticles__oxthumb = $this->oxorderarticles__oxthumb;
00302         $this->oxarticles__oxpic1  = $this->oxorderarticles__oxpic1;
00303         $this->oxarticles__oxpic2  = $this->oxorderarticles__oxpic2;
00304         $this->oxarticles__oxpic3  = $this->oxorderarticles__oxpic3;
00305         $this->oxarticles__oxpic4  = $this->oxorderarticles__oxpic4;
00306         $this->oxarticles__oxpic5  = $this->oxorderarticles__oxpic5;
00307 
00308         $this->oxarticles__oxfile     = $this->oxorderarticles__oxfile;
00309         $this->oxarticles__oxdelivery = $this->oxorderarticles__oxdelivery;
00310         $this->oxarticles__oxissearch = $this->oxorderarticles__oxissearch;
00311         $this->oxarticles__oxfolder   = $this->oxorderarticles__oxfolder;
00312         $this->oxarticles__oxtemplate = $this->oxorderarticles__oxtemplate;
00313         $this->oxarticles__oxexturl   = $this->oxorderarticles__oxexturl;
00314         $this->oxarticles__oxurlimg   = $this->oxorderarticles__oxurlimg;
00315         $this->oxarticles__oxurldesc  = $this->oxorderarticles__oxurldesc;
00316         $this->oxarticles__oxshopid   = $this->oxorderarticles__oxordershopid;
00317         $this->oxarticles__oxquestionemail = $this->oxorderarticles__oxquestionemail;
00318         $this->oxarticles__oxsearchkeys    = $this->oxorderarticles__oxsearchkeys;
00319     }
00320 
00329     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
00330     {
00331         return true;
00332     }
00333 
00342     protected function _getOrderArticle( $sArticleId = null )
00343     {
00344         if ( $this->_oOrderArticle === null ) {
00345             $this->_oOrderArticle = false;
00346 
00347             $sArticleId = $sArticleId ? $sArticleId : $this->getProductId();
00348             $oArticle = oxNew( "oxArticle" );
00349             if ( $oArticle->load( $sArticleId ) ) {
00350                 $this->_oOrderArticle = $oArticle;
00351             }
00352         }
00353         return $this->_oOrderArticle;
00354     }
00355 
00363     public function getSelectLists( $sKeyPrefix = null )
00364     {
00365         $aSelLists = array();
00366         if ( $oArticle = $this->_getOrderArticle() ) {
00367             $aSelLists = $oArticle->getSelectLists();
00368         }
00369         return $aSelLists;
00370     }
00371 
00380     public function getOrderArticleSelectList( $sArtId = null, $sOrderArtSelList = null )
00381     {
00382         if ( $this->_aOrderArticleSelList === null ) {
00383 
00384             $sOrderArtSelList = $sOrderArtSelList ? $sOrderArtSelList : $this->oxorderarticles__oxselvariant->value;
00385 
00386             $aList = array();
00387             $aRet  = array();
00388 
00389             if ( $oArticle = $this->_getOrderArticle( $sArtId ) ) {
00390                 $aList = explode( ",", $sOrderArtSelList );
00391                 $oStr = getStr();
00392 
00393                 $aArticleSelList = $oArticle->getSelectLists();
00394 
00395                 //formating temporary list array from string
00396                 foreach ( $aList as $sList ) {
00397                     if ( $sList ) {
00398 
00399                         $aVal = explode( ":", $sList );
00400                         if ( isset($aVal[0]) && isset($aVal[1])) {
00401                             $sOrderArtListTitle = $oStr->strtolower( trim($aVal[0]) );
00402                             $sOrderArtSelValue  = $oStr->strtolower( trim($aVal[1]) );
00403 
00404                             //checking article list for matches with article list stored in oxorderitem
00405                             $iSelListNum = 0;
00406                             if ( count($aArticleSelList) > 0 ) {
00407                                 foreach ( $aArticleSelList as $aSelect ) {
00408                                     //chek if selects titles are equal
00409 
00410                                     if ( $oStr->strtolower($aSelect['name']) == $sOrderArtListTitle ) {
00411                                         //try to find matching select items value
00412                                         $iSelValueNum = 0;
00413                                         foreach ( $aSelect as $oSel ) {
00414                                             if ( $oStr->strtolower($oSel->name) == $sOrderArtSelValue ) {
00415                                                 // found, adding tu return array
00416                                                 $aRet[$iSelListNum] = $iSelValueNum;
00417                                             }
00418                                             //next article list item
00419                                             $iSelValueNum++;
00420                                         }
00421                                     }
00422                                     //next article list
00423                                     $iSelListNum++;
00424                                 }
00425                             }
00426                         }
00427                     }
00428                 }
00429             }
00430 
00431             $this->_aOrderArticleSelList = $aRet;
00432         }
00433 
00434         return $this->_aOrderArticleSelList;
00435     }
00436 
00446     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
00447     {
00448         $oArticle = $this->_getOrderArticle();
00449         if ( $oArticle ) {
00450             return $oArticle->getBasketPrice( $dAmount, $aSelList, $oBasket );
00451         } else {
00452             return $this->getPrice();
00453         }
00454     }
00455 
00461     public function skipDiscounts()
00462     {
00463         return false;
00464     }
00465 
00474     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
00475     {
00476         $aCatIds = array();
00477         if ( $oOrderArticle = $this->_getOrderArticle() ) {
00478             $aCatIds = $oOrderArticle->getCategoryIds( $blActCats, $blSkipCache );
00479         }
00480         return $aCatIds;
00481     }
00482 
00488     public function getLanguage()
00489     {
00490         return oxLang::getInstance()->getBaseLanguage();
00491     }
00492 
00500     public function getBasePrice( $dAmount = 1 )
00501     {
00502 
00503         return $this->getPrice();
00504     }
00505 
00511     public function getPrice()
00512     {
00513         $oBasePrice = oxNew( 'oxPrice' );
00514         // prices in db are ONLY brutto
00515         $oBasePrice->setBruttoPriceMode();
00516         $oBasePrice->setVat( $this->oxorderarticles__oxvat->value );
00517         $oBasePrice->setPrice( $this->oxorderarticles__oxbprice->value );
00518 
00519         return $oBasePrice;
00520     }
00521 
00529     public function setIsNewOrderItem( $blIsNew )
00530     {
00531         $this->_blIsNewOrderItem = $blIsNew;
00532     }
00533 
00539     public function isNewOrderItem()
00540     {
00541         return $this->_blIsNewOrderItem;
00542     }
00543 
00553     public function setNewAmount( $iNewAmount )
00554     {
00555         if ( $iNewAmount >= 0 ) {
00556             // to update stock we must first check if it is possible - article exists?
00557             $oArticle = oxNew( "oxarticle" );
00558             if ( $oArticle->load( $this->oxorderarticles__oxartid->value ) ) {
00559 
00560                 // updating stock info
00561                 $iStockChange = $iNewAmount - $this->oxorderarticles__oxamount->value;
00562                 if ( $iStockChange > 0 && ( $iOnStock = $oArticle->checkForStock( $iStockChange ) ) !== false ) {
00563                     if ( $iOnStock !== true ) {
00564                         $iStockChange = $iOnStock;
00565                         $iNewAmount   = $this->oxorderarticles__oxamount->value + $iStockChange;
00566                     }
00567                 }
00568 
00569                 $this->updateArticleStock( $iStockChange * -1, $this->getConfig()->getConfigParam( 'blAllowNegativeStock' ) );
00570 
00571                 // updating self
00572                 $this->oxorderarticles__oxamount = new oxField ( $iNewAmount, oxField::T_RAW );
00573                 $this->save();
00574             }
00575         }
00576     }
00577 
00583     public function isOrderArticle()
00584     {
00585         return true;
00586     }
00587 
00588 
00595     public function cancelOrderArticle()
00596     {
00597         if ( $this->oxorderarticles__oxstorno->value == 0 ) {
00598             $myConfig = $this->getConfig();
00599             $this->oxorderarticles__oxstorno->setValue( 1 );
00600             if ( $this->save() && $myConfig->getConfigParam( 'blUseStock' ) ) {
00601                 $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
00602             }
00603         }
00604     }
00605 
00614     public function delete( $sOXID = null)
00615     {
00616         if ( $blDelete = parent::delete( $sOXID ) ) {
00617             $myConfig = $this->getConfig();
00618             if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxorderarticles__oxstorno->value != 1 ) {
00619                 $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
00620             }
00621         }
00622         return $blDelete;
00623     }
00624 
00632     public function save()
00633     {
00634         // ordered articles
00635         if ( ( $blSave = parent::save() ) && $this->isNewOrderItem() ) {
00636             $myConfig = $this->getConfig();
00637             if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00638                 if ($myConfig->getConfigParam( 'blPsBasketReservationEnabled' )) {
00639                     $this->getSession()
00640                             ->getBasketReservations()
00641                             ->commitArticleReservation(
00642                                    $this->oxorderarticles__oxartid->value,
00643                                    $this->oxorderarticles__oxamount->value
00644                            );
00645                 } else {
00646                     $this->updateArticleStock( $this->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
00647                 }
00648             }
00649 
00650             // seting downloadable products article files
00651             $this->_setOrderFiles();
00652 
00653             // marking object as "non new" disable further stock changes
00654             $this->setIsNewOrderItem( false );
00655         }
00656 
00657         return $blSave;
00658     }
00659 
00665     public function getWrapping()
00666     {
00667         if ($this->oxorderarticles__oxwrapid->value) {
00668             $oWrapping = oxNew('oxwrapping');
00669             if ($oWrapping->load($this->oxorderarticles__oxwrapid->value)) {
00670                 return $oWrapping;
00671             }
00672         }
00673         return null;
00674     }
00675 
00681     public function isBundle()
00682     {
00683         return ( bool ) $this->oxorderarticles__oxisbundle->value;
00684     }
00685 
00691     public function getTotalBrutPriceFormated()
00692     {
00693         $oLang = oxLang::getInstance();
00694         $oOrder = $this->getOrder();
00695         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
00696         return $oLang->formatCurrency( $this->oxorderarticles__oxbrutprice->value, $oCurrency );
00697     }
00698 
00704     public function getBrutPriceFormated()
00705     {
00706         $oLang = oxLang::getInstance();
00707         $oOrder = $this->getOrder();
00708         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
00709         return $oLang->formatCurrency(  $this->oxorderarticles__oxbprice->value, $oCurrency );
00710     }
00711 
00717     public function getNetPriceFormated()
00718     {
00719         $oLang = oxLang::getInstance();
00720         $oOrder = $this->getOrder();
00721         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
00722         return $oLang->formatCurrency(  $this->oxorderarticles__oxnprice->value, $oCurrency );
00723     }
00724 
00730     public function getOrder()
00731     {
00732         if ( $this->oxorderarticles__oxorderid->value ) {
00733             // checking if the object already exists in the cache
00734             if ( isset( $this->_aOrderCache[ $this->oxorderarticles__oxorderid->value ] )) {
00735                 // returning the cached object
00736                 return $this->_aOrderCache[ $this->oxorderarticles__oxorderid->value ];
00737             }
00738             // creatina new order object and trying to load it
00739             $oOrder = oxNew( 'oxOrder' );
00740             if ( $oOrder->load( $this->oxorderarticles__oxorderid->value )) {
00741                 return $this->_aOrderCache[$this->oxorderarticles__oxorderid->value] = $oOrder;
00742             }
00743         }
00744 
00745         return null;
00746     }
00747 
00755     protected function _insert()
00756     {
00757         $iInsertTime = time();
00758         $now = date('Y-m-d H:i:s', $iInsertTime);
00759         $this->oxorderarticles__oxtimestamp = new oxField( $now );
00760 
00761         return parent::_insert();
00762     }
00763 
00764 
00772     public function setArticle( $oArticle )
00773     {
00774         $this->_oArticle = $oArticle;
00775     }
00776 
00782     public function getArticle()
00783     {
00784         if ( $this->_oArticle === null ) {
00785             $oArticle = oxNew( 'oxArticle' );
00786             $oArticle->load($this->oxorderarticles__oxartid->value);
00787             $this->_oArticle = $oArticle;
00788         }
00789 
00790         return $this->_oArticle;
00791     }
00792 
00793 
00794 
00800     public function _setOrderFiles()
00801     {
00802         $oArticle = $this->getArticle();
00803 
00804         if ( $oArticle->oxarticles__oxisdownloadable->value ) {
00805 
00806             $oConfig          = $this->getConfig();
00807             $sOrderId          = $this->oxorderarticles__oxorderid->value;
00808             $sOrderArticleId = $this->getId();
00809             $sShopId          = $oConfig->getShopId();
00810 
00811             $oUser             = $oConfig->getUser();
00812 
00813             $oFiles = $oArticle->getArticleFiles( true );
00814 
00815             if ( $oFiles ) {
00816                 foreach ($oFiles as $oFile) {
00817                     $oOrderFile = oxNew( 'oxOrderFile' );
00818                     $oOrderFile->setOrderId( $sOrderId );
00819                     $oOrderFile->setOrderArticleId( $sOrderArticleId );
00820                     $oOrderFile->setShopId( $sShopId );
00821                     $iMaxDownloadCount = (!empty($oUser) && !$oUser->hasAccount()) ? $oFile->getMaxUnregisteredDownloadsCount() :  $oFile->getMaxDownloadsCount();
00822                     $oOrderFile->setFile(
00823                         $oFile->oxfiles__oxfilename->value,
00824                         $oFile->getId(),
00825                         $iMaxDownloadCount * $this->oxorderarticles__oxamount->value,
00826                         $oFile->getLinkExpirationTime(),
00827                         $oFile->getDownloadExpirationTime()
00828                     );
00829 
00830                     $oOrderFile->save();
00831                 }
00832             }
00833         }
00834     }
00835 }