oxorderarticle.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxOrderArticle extends oxBase implements oxIArticle
00008 {
00014     protected $_sClassName = 'oxorderarticle';
00015 
00021     protected $_aPersParam = null;
00022 
00028     protected $_aStatuses = null;
00029 
00035     protected $_aOrderArticleSelList = null;
00036 
00042     protected $_oOrderArticle = null;
00043 
00049     protected $_blIsNewOrderItem = false;
00050 
00054     public function __construct()
00055     {
00056         parent::__construct();
00057         $this->init( 'oxorderarticles' );
00058     }
00059 
00067     public function copyThis( $oProduct )
00068     {
00069         $aObjectVars = get_object_vars( $oProduct );
00070 
00071         foreach ( $aObjectVars as $sName => $sValue ) {
00072             if ( isset( $oProduct->$sName->value ) ) {
00073                 $sFieldName = preg_replace('/oxarticles__/', 'oxorderarticles__', $sName);
00074                 $this->$sFieldName = $oProduct->$sName;
00075             }
00076         }
00077 
00078     }
00079 
00087     public function assign( $dbRecord )
00088     {
00089         parent::assign( $dbRecord );
00090         $this->_setArticleParams();
00091     }
00092 
00103     public function updateArticleStock( $dAddAmount, $blAllowNegativeStock = false )
00104     {
00105         // TODO: use oxarticle reduceStock
00106         // decrement stock if there is any
00107         $oArticle = oxNew( 'oxarticle' );
00108         $oArticle->load( $this->oxorderarticles__oxartid->value );
00109         $oArticle->beforeUpdate();
00110 
00111         // get real article stock count
00112         $iStockCount = $this->_getArtStock( $dAddAmount, $blAllowNegativeStock );
00113         $oDb = oxDb::getDb();
00114 
00115         // #874A. added oxarticles.oxtimestamp = oxarticles.oxtimestamp to keep old timestamp value
00116         $oArticle->oxarticles__oxstock = new oxField($iStockCount);
00117         $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->oxorderarticles__oxartid->value ) );
00118         $oArticle->onChange( ACTION_UPDATE_STOCK );
00119 
00120         //update article sold amount
00121         $oArticle->updateSoldAmount( $dAddAmount * ( -1 ) );
00122     }
00123 
00132     protected function _getArtStock( $dAddAmount = 0, $blAllowNegativeStock = false )
00133     {
00134         $oDb = oxDb::getDb();
00135 
00136         // #1592A. must take real value
00137         $sQ = 'select oxstock from oxarticles where oxid = '.$oDb->quote( $this->oxorderarticles__oxartid->value );
00138         $iStockCount  = ( float ) $oDb->getOne( $sQ );
00139 
00140         $iStockCount += $dAddAmount;
00141 
00142         // #1592A. calculating according new stock option
00143         if ( !$blAllowNegativeStock && $iStockCount < 0 ) {
00144             $iStockCount = 0;
00145         }
00146 
00147         return $iStockCount;
00148     }
00149 
00150 
00156     public function getPersParams()
00157     {
00158         if ( $this->_aPersParam != null ) {
00159             return $this->_aPersParam;
00160         }
00161 
00162         if ( $this->oxorderarticles__oxpersparam->value ) {
00163             $this->_aPersParam = unserialize( $this->oxorderarticles__oxpersparam->value );
00164         }
00165 
00166         return $this->_aPersParam;
00167     }
00168 
00176     public function setPersParams( $aParams )
00177     {
00178         $this->_aPersParam = $aParams;
00179 
00180         // serializing persisten info stored while ordering
00181         $this->oxorderarticles__oxpersparam = new oxField(serialize( $aParams ), oxField::T_RAW);
00182     }
00183 
00193     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
00194     {
00195         $sFieldName = strtolower($sFieldName);
00196         switch ( $sFieldName ) {
00197             case 'oxpersparam':
00198             case 'oxorderarticles__oxpersparam':
00199             case 'oxerpstatus':
00200             case 'oxorderarticles__oxerpstatus':
00201             case 'oxtitle':
00202             case 'oxorderarticles__oxtitle':
00203                 $iDataType = oxField::T_RAW;
00204                 break;
00205         }
00206         return parent::_setFieldData($sFieldName, $sValue, $iDataType);
00207     }
00208 
00217     public function loadInLang( $iLanguage, $sOxid )
00218     {
00219         return $this->load( $sOxid );
00220     }
00221 
00227     public function getProductId()
00228     {
00229         return $this->oxorderarticles__oxartid->value;
00230     }
00231 
00237     public function getProductParentId()
00238     {
00239         // when this field will be introduced there will be no need to load from real article
00240         if ( isset( $this->oxorderarticles__oxartparentid ) && $this->oxorderarticles__oxartparentid->value !== false ) {
00241             return $this->oxorderarticles__oxartparentid->value;
00242         }
00243 
00244         $oDb = oxDb::getDb();
00245         $oArticle = oxNew( "oxarticle" );
00246         $sQ = "select oxparentid from " . $oArticle->getViewName() . " where oxid=" . $oDb->quote( $this->getProductId() );
00247         $this->oxarticles__oxparentid = new oxField( $oDb->getOne( $sQ ) );
00248         return $this->oxarticles__oxparentid->value;
00249     }
00250 
00256     protected function _setArticleParams()
00257     {
00258         // creating needed fields
00259         $this->oxarticles__oxstock  = $this->oxorderarticles__oxamount;
00260         $this->oxarticles__oxtitle  = $this->oxorderarticles__oxtitle;
00261         $this->oxarticles__oxwidth  = $this->oxorderarticles__oxwidth;
00262         $this->oxarticles__oxlength = $this->oxorderarticles__oxlength;
00263         $this->oxarticles__oxheight = $this->oxorderarticles__oxheight;
00264         $this->oxarticles__oxweight = $this->oxorderarticles__oxweight;
00265         $this->oxarticles__oxsubclass  = $this->oxorderarticles__oxsubclass;
00266         $this->oxarticles__oxartnum    = $this->oxorderarticles__oxartnum;
00267         $this->oxarticles__oxshortdesc = $this->oxorderarticles__oxshortdesc;
00268 
00269         $this->oxarticles__oxvat    = $this->oxorderarticles__oxvat;
00270         $this->oxarticles__oxprice  = $this->oxorderarticles__oxprice;
00271         $this->oxarticles__oxbprice = $this->oxorderarticles__oxbprice;
00272 
00273         $this->oxarticles__oxthumb = $this->oxorderarticles__oxthumb;
00274         $this->oxarticles__oxpic1  = $this->oxorderarticles__oxpic1;
00275         $this->oxarticles__oxpic2  = $this->oxorderarticles__oxpic2;
00276         $this->oxarticles__oxpic3  = $this->oxorderarticles__oxpic3;
00277         $this->oxarticles__oxpic4  = $this->oxorderarticles__oxpic4;
00278         $this->oxarticles__oxpic5  = $this->oxorderarticles__oxpic5;
00279 
00280         $this->oxarticles__oxfile     = $this->oxorderarticles__oxfile;
00281         $this->oxarticles__oxdelivery = $this->oxorderarticles__oxdelivery;
00282         $this->oxarticles__oxissearch = $this->oxorderarticles__oxissearch;
00283         $this->oxarticles__oxfolder   = $this->oxorderarticles__oxfolder;
00284         $this->oxarticles__oxtemplate = $this->oxorderarticles__oxtemplate;
00285         $this->oxarticles__oxexturl   = $this->oxorderarticles__oxexturl;
00286         $this->oxarticles__oxurlimg   = $this->oxorderarticles__oxurlimg;
00287         $this->oxarticles__oxurldesc  = $this->oxorderarticles__oxurldesc;
00288         $this->oxarticles__oxshopid   = $this->oxorderarticles__oxordershopid;
00289         $this->oxarticles__oxquestionemail = $this->oxorderarticles__oxquestionemail;
00290         $this->oxarticles__oxsearchkeys    = $this->oxorderarticles__oxsearchkeys;
00291     }
00292 
00301     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
00302     {
00303         return true;
00304     }
00305 
00314     protected function _getOrderArticle( $sArticleId = null )
00315     {
00316         if ( $this->_oOrderArticle === null ) {
00317             $this->_oOrderArticle = false;
00318 
00319             $sArticleId = $sArticleId ? $sArticleId : $this->getProductId();
00320             $oArticle = oxNew( "oxArticle" );
00321             if ( $oArticle->load( $sArticleId ) ) {
00322                 $this->_oOrderArticle = $oArticle;
00323             }
00324         }
00325         return $this->_oOrderArticle;
00326     }
00327 
00335     public function getSelectLists( $sKeyPrefix = null )
00336     {
00337         $aSelLists = array();
00338         if ( $oArticle = $this->_getOrderArticle() ) {
00339             $aSelLists = $oArticle->getSelectLists();
00340         }
00341         return $aSelLists;
00342     }
00343 
00352     public function getOrderArticleSelectList( $sArtId = null, $sOrderArtSelList = null )
00353     {
00354         if ( $this->_aOrderArticleSelList === null ) {
00355 
00356             $sOrderArtSelList = $sOrderArtSelList ? $sOrderArtSelList : $this->oxorderarticles__oxselvariant->value;
00357 
00358             $aList = array();
00359             $aRet  = array();
00360 
00361             if ( $oArticle = $this->_getOrderArticle( $sArtId ) ) {
00362                 $aList = explode( ",", $sOrderArtSelList );
00363                 $oStr = getStr();
00364 
00365                 $aArticleSelList = $oArticle->getSelectLists();
00366 
00367                 //formating temporary list array from string
00368                 foreach ( $aList as $sList ) {
00369                     if ( $sList ) {
00370 
00371                         $aVal = explode( ":", $sList );
00372                         if ( isset($aVal[0]) && isset($aVal[1])) {
00373                             $sOrderArtListTitle = $oStr->strtolower( trim($aVal[0]) );
00374                             $sOrderArtSelValue  = $oStr->strtolower( trim($aVal[1]) );
00375 
00376                             //checking article list for matches with article list stored in oxorderitem
00377                             $iSelListNum = 0;
00378                             if ( count($aArticleSelList) > 0 ) {
00379                                 foreach ( $aArticleSelList as $aSelect ) {
00380                                     //chek if selects titles are equal
00381 
00382                                     if ( $oStr->strtolower($aSelect['name']) == $sOrderArtListTitle ) {
00383                                         //try to find matching select items value
00384                                         $iSelValueNum = 0;
00385                                         foreach ( $aSelect as $oSel ) {
00386                                             if ( $oStr->strtolower($oSel->name) == $sOrderArtSelValue ) {
00387                                                 // found, adding tu return array
00388                                                 $aRet[$iSelListNum] = $iSelValueNum;
00389                                             }
00390                                             //next article list item
00391                                             $iSelValueNum++;
00392                                         }
00393                                     }
00394                                     //next article list
00395                                     $iSelListNum++;
00396                                 }
00397                             }
00398                         }
00399                     }
00400                 }
00401             }
00402 
00403             $this->_aOrderArticleSelList = $aRet;
00404         }
00405 
00406         return $this->_aOrderArticleSelList;
00407     }
00408 
00418     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
00419     {
00420         return $this->getPrice();
00421     }
00422 
00428     public function skipDiscounts()
00429     {
00430         return false;
00431     }
00432 
00441     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
00442     {
00443         $aCatIds = array();
00444         if ( $oOrderArticle = $this->_getOrderArticle() ) {
00445             $aCatIds = $oOrderArticle->getCategoryIds( $blActCats, $blSkipCache );
00446         }
00447         return $aCatIds;
00448     }
00449 
00455     public function getLanguage()
00456     {
00457         return oxLang::getInstance()->getBaseLanguage();
00458     }
00459 
00467     public function getBasePrice( $dAmount = 1 )
00468     {
00469         return $this->getPrice();
00470     }
00471 
00477     public function getPrice()
00478     {
00479         $oBasePrice = oxNew( 'oxPrice' );
00480         // prices in db are ONLY brutto
00481         $oBasePrice->setBruttoPriceMode();
00482         $oBasePrice->setVat( $this->oxorderarticles__oxvat->value );
00483         $oBasePrice->setPrice( $this->oxorderarticles__oxbprice->value );
00484 
00485         return $oBasePrice;
00486     }
00487 
00495     public function setIsNewOrderItem( $blIsNew )
00496     {
00497         $this->_blIsNewOrderItem = $blIsNew;
00498     }
00499 
00505     public function isNewOrderItem()
00506     {
00507         return $this->_blIsNewOrderItem;
00508     }
00509 
00519     public function setNewAmount( $iNewAmount )
00520     {
00521         if ( $iNewAmount >= 0 ) {
00522             // to update stock we must first check if it is possible - article exists?
00523             $oArticle = oxNew( "oxarticle" );
00524             if ( $oArticle->load( $this->oxorderarticles__oxartid->value ) ) {
00525 
00526                 // updating stock info
00527                 $iStockChange = $iNewAmount - $this->oxorderarticles__oxamount->value;
00528                 if ( $iStockChange > 0 && ( $iOnStock = $oArticle->checkForStock( $iStockChange ) ) !== false ) {
00529                     if ( $iOnStock !== true ) {
00530                         $iStockChange = $iOnStock;
00531                         $iNewAmount   = $this->oxorderarticles__oxamount->value + $iStockChange;
00532                     }
00533                 }
00534 
00535                 $this->updateArticleStock( $iStockChange * -1, $this->getConfig()->getConfigParam( 'blAllowNegativeStock' ) );
00536 
00537                 // updating self
00538                 $this->oxorderarticles__oxamount = new oxField ( $iNewAmount, oxField::T_RAW );
00539                 $this->save();
00540             }
00541         }
00542     }
00543 
00549     public function isOrderArticle()
00550     {
00551         return true;
00552     }
00553 
00554 
00561     public function cancelOrderArticle()
00562     {
00563         if ( $this->oxorderarticles__oxstorno->value == 0 ) {
00564             $myConfig = $this->getConfig();
00565             $this->oxorderarticles__oxstorno->setValue( 1 );
00566             if ( $this->save() && $myConfig->getConfigParam( 'blUseStock' ) ) {
00567                 $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
00568             }
00569         }
00570     }
00571 
00580     public function delete( $sOXID = null)
00581     {
00582         if ( $blDelete = parent::delete( $sOXID ) ) {
00583             $myConfig = $this->getConfig();
00584             if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxorderarticles__oxstorno->value != 1 ) {
00585                 $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
00586             }
00587         }
00588         return $blDelete;
00589     }
00590 
00598     public function save()
00599     {
00600         // ordered articles
00601         if ( ( $blSave = parent::save() ) && $this->isNewOrderItem() ) {
00602             $myConfig = $this->getConfig();
00603             if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00604                 if ($myConfig->getConfigParam( 'blPsBasketReservationEnabled' )) {
00605                     $this->getSession()
00606                             ->getBasketReservations()
00607                             ->commitArticleReservation(
00608                                    $this->oxorderarticles__oxartid->value,
00609                                    $this->oxorderarticles__oxamount->value
00610                            );
00611                 } else {
00612                     $this->updateArticleStock( $this->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
00613                 }
00614             }
00615 
00616             // marking object as "non new" disable further stock changes
00617             $this->setIsNewOrderItem( false );
00618         }
00619 
00620         return $blSave;
00621     }
00622 
00628     public function getWrapping()
00629     {
00630         if ($this->oxorderarticles__oxwrapid->value) {
00631             $oWrapping = oxNew('oxwrapping');
00632             if ($oWrapping->load($this->oxorderarticles__oxwrapid->value)) {
00633                 return $oWrapping;
00634             }
00635         }
00636         return null;
00637     }
00638 
00644     public function isBundle()
00645     {
00646         return ( bool ) $this->oxorderarticles__oxisbundle->value;
00647     }
00648 
00654     public function getTotalBrutPriceFormated()
00655     {
00656         $oLang = oxLang::getInstance();
00657         return $oLang->formatCurrency( $this->oxorderarticles__oxbrutprice->value );
00658     }
00659 
00665     public function getBrutPriceFormated()
00666     {
00667         $oLang = oxLang::getInstance();
00668         return $oLang->formatCurrency(  $this->oxorderarticles__oxbprice->value );
00669     }
00670 
00676     public function getNetPriceFormated()
00677     {
00678         $oLang = oxLang::getInstance();
00679         return $oLang->formatCurrency(  $this->oxorderarticles__oxnprice->value );
00680     }
00681 
00682 }