oxorderarticle.php

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