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 
00037     public $aStatuses = null;
00038 
00046     public $aPersParam = null;
00047 
00055     public $ftotbrutprice = null;
00056 
00064     public $fbrutprice = null;
00065 
00073     public $fnetprice = null;
00074 
00080     protected $_aOrderArticleSelList = null;
00081 
00087     protected $_oOrderArticle = null;
00088 
00094     protected $_blIsNewOrderItem = false;
00095 
00099     public function __construct()
00100     {
00101         parent::__construct();
00102         $this->init( 'oxorderarticles' );
00103     }
00104 
00112     public function copyThis( $oProduct )
00113     {
00114         $aObjectVars = get_object_vars( $oProduct );
00115 
00116         foreach ( $aObjectVars as $sName => $sValue ) {
00117             if ( isset( $oProduct->$sName->value ) ) {
00118                 $sFieldName = preg_replace('/oxarticles__/', 'oxorderarticles__', $sName);
00119                 $this->$sFieldName = $oProduct->$sName;
00120             }
00121         }
00122 
00123     }
00124 
00132     public function assign( $dbRecord )
00133     {
00134         parent::assign( $dbRecord );
00135         $this->_setDeprecatedValues();
00136         $this->_setArticleParams();
00137     }
00138 
00149     public function updateArticleStock( $dAddAmount = null, $blAllowNegativeStock = null )
00150     {
00151         // decrement stock if there is any
00152         $oArticle = oxNew( 'oxarticle' );
00153         $oArticle->load( $this->oxorderarticles__oxartid->value );
00154         $oArticle->beforeUpdate();
00155 
00156         // get real article stock count
00157         $iStockCount = $this->_getArtStock( $dAddAmount, $blAllowNegativeStock );
00158         $oDb = oxDb::getDb();
00159 
00160         // #874A. added oxarticles.oxtimestamp = oxarticles.oxtimestamp to keep old timestamp value
00161         $oArticle->oxarticles__oxstock = new oxField($iStockCount);
00162         $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->oxorderarticles__oxartid->value ) );
00163         $oArticle->onChange( ACTION_UPDATE_STOCK );
00164 
00165         //update article sold amount
00166         $oArticle->updateSoldAmount( $dAddAmount * ( -1 ) );
00167     }
00168 
00177     protected function _getArtStock( $dAddAmount = null, $blAllowNegativeStock = null )
00178     {
00179         $oDb = oxDb::getDb();
00180 
00181         // #1592A. must take real value
00182         $sQ = 'select oxstock from oxarticles where oxid = '.$oDb->quote( $this->oxorderarticles__oxartid->value );
00183         $iStockCount  = ( float ) $oDb->getOne( $sQ );
00184 
00185         $iStockCount += $dAddAmount;
00186 
00187         // #1592A. calculating according new stock option
00188         if ( !$blAllowNegativeStock && $iStockCount < 0 ) {
00189             $iStockCount = 0;
00190         }
00191 
00192         return $iStockCount;
00193     }
00194 
00195 
00201     public function getPersParams()
00202     {
00203         if ( $this->_aPersParam != null ) {
00204             return $this->_aPersParam;
00205         }
00206 
00207         if ( $this->oxorderarticles__oxpersparam->value ) {
00208             $this->_aPersParam = unserialize( $this->oxorderarticles__oxpersparam->value );
00209         }
00210 
00211         return $this->_aPersParam;
00212     }
00213 
00221     public function setPersParams( $aParams )
00222     {
00223         $this->_aPersParam = $aParams;
00224 
00225         // serializing persisten info stored while ordering
00226         $this->oxorderarticles__oxpersparam = new oxField(serialize( $aParams ), oxField::T_RAW);
00227     }
00228 
00236     protected function _setDeprecatedValues()
00237     {
00238 
00239         $this->aPersParam = $this->getPersParams();
00240 
00241         if ( $this->oxorderarticles__oxstorno->value != 1 ) {
00242             $oLang = oxLang::getInstance();
00243             $this->ftotbrutprice = $oLang->formatCurrency( $this->oxorderarticles__oxbrutprice->value );
00244             $this->fbrutprice    = $oLang->formatCurrency( $this->oxorderarticles__oxbprice->value );
00245             $this->fnetprice     = $oLang->formatCurrency( $this->oxorderarticles__oxnprice->value );
00246         }
00247     }
00248 
00258     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
00259     {
00260         $sFieldName = strtolower($sFieldName);
00261         switch ( $sFieldName ) {
00262             case 'oxpersparam':
00263             case 'oxorderarticles__oxpersparam':
00264             case 'oxerpstatus':
00265             case 'oxorderarticles__oxerpstatus':
00266             case 'oxtitle':
00267             case 'oxorderarticles__oxtitle':
00268                 $iDataType = oxField::T_RAW;
00269                 break;
00270         }
00271         return parent::_setFieldData($sFieldName, $sValue, $iDataType);
00272     }
00273 
00282     public function loadInLang( $iLanguage, $sOxid )
00283     {
00284         return $this->load( $sOxid );
00285     }
00286 
00292     public function getProductId()
00293     {
00294         return $this->oxorderarticles__oxartid->value;
00295     }
00296 
00302     public function getProductParentId()
00303     {
00304         // when this field will be introduced there will be no need to load from real article
00305         if ( isset( $this->oxorderarticles__oxartparentid ) && $this->oxorderarticles__oxartparentid->value !== false ) {
00306             return $this->oxorderarticles__oxartparentid->value;
00307         }
00308 
00309         $oDb = oxDb::getDb();
00310         $oArticle = oxNew( "oxarticle" );
00311         $sQ = "select oxparentid from " . $oArticle->getViewName() . " where oxid=" . $oDb->quote( $this->getProductId() );
00312         $this->oxarticles__oxparentid = new oxField( $oDb->getOne( $sQ ) );
00313         return $this->oxarticles__oxparentid->value;
00314     }
00315 
00321     protected function _setArticleParams()
00322     {
00323         // creating needed fields
00324         $this->oxarticles__oxstock  = $this->oxorderarticles__oxamount;
00325         $this->oxarticles__oxtitle  = $this->oxorderarticles__oxtitle;
00326         $this->oxarticles__oxwidth  = $this->oxorderarticles__oxwidth;
00327         $this->oxarticles__oxlength = $this->oxorderarticles__oxlength;
00328         $this->oxarticles__oxheight = $this->oxorderarticles__oxheight;
00329         $this->oxarticles__oxweight = $this->oxorderarticles__oxweight;
00330         $this->oxarticles__oxsubclass  = $this->oxorderarticles__oxsubclass;
00331         $this->oxarticles__oxartnum    = $this->oxorderarticles__oxartnum;
00332         $this->oxarticles__oxshortdesc = $this->oxorderarticles__oxshortdesc;
00333 
00334         $this->oxarticles__oxvat    = $this->oxorderarticles__oxvat;
00335         $this->oxarticles__oxprice  = $this->oxorderarticles__oxprice;
00336         $this->oxarticles__oxbprice = $this->oxorderarticles__oxbprice;
00337 
00338         $this->oxarticles__oxthumb = $this->oxorderarticles__oxthumb;
00339         $this->oxarticles__oxpic1  = $this->oxorderarticles__oxpic1;
00340         $this->oxarticles__oxpic2  = $this->oxorderarticles__oxpic2;
00341         $this->oxarticles__oxpic3  = $this->oxorderarticles__oxpic3;
00342         $this->oxarticles__oxpic4  = $this->oxorderarticles__oxpic4;
00343         $this->oxarticles__oxpic5  = $this->oxorderarticles__oxpic5;
00344 
00345         $this->oxarticles__oxfile     = $this->oxorderarticles__oxfile;
00346         $this->oxarticles__oxdelivery = $this->oxorderarticles__oxdelivery;
00347         $this->oxarticles__oxissearch = $this->oxorderarticles__oxissearch;
00348         $this->oxarticles__oxfolder   = $this->oxorderarticles__oxfolder;
00349         $this->oxarticles__oxtemplate = $this->oxorderarticles__oxtemplate;
00350         $this->oxarticles__oxexturl   = $this->oxorderarticles__oxexturl;
00351         $this->oxarticles__oxurlimg   = $this->oxorderarticles__oxurlimg;
00352         $this->oxarticles__oxurldesc  = $this->oxorderarticles__oxurldesc;
00353         $this->oxarticles__oxshopid   = $this->oxorderarticles__oxordershopid;
00354         $this->oxarticles__oxquestionemail = $this->oxorderarticles__oxquestionemail;
00355         $this->oxarticles__oxsearchkeys    = $this->oxorderarticles__oxsearchkeys;
00356     }
00357 
00366     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
00367     {
00368         return true;
00369     }
00370 
00379     protected function _getOrderArticle( $sArticleId = null )
00380     {
00381         if ( $this->_oOrderArticle === null ) {
00382             $this->_oOrderArticle = false;
00383 
00384             $sArticleId = $sArticleId ? $sArticleId : $this->getProductId();
00385             $oArticle = oxNew( "oxArticle" );
00386             if ( $oArticle->load( $sArticleId ) ) {
00387                 $this->_oOrderArticle = $oArticle;
00388             }
00389         }
00390         return $this->_oOrderArticle;
00391     }
00392 
00400     public function getSelectLists( $sKeyPrefix = null )
00401     {
00402         $aSelLists = array();
00403         if ( $oArticle = $this->_getOrderArticle() ) {
00404             $aSelLists = $oArticle->getSelectLists();
00405         }
00406         return $aSelLists;
00407     }
00408 
00417     public function getOrderArticleSelectList( $sArtId = null, $sOrderArtSelList = null )
00418     {
00419         if ( $this->_aOrderArticleSelList === null ) {
00420 
00421             $sOrderArtSelList = $sOrderArtSelList ? $sOrderArtSelList : $this->oxorderarticles__oxselvariant->value;
00422 
00423             $aList = array();
00424             $aRet  = array();
00425 
00426             if ( $oArticle = $this->_getOrderArticle( $sArtId ) ) {
00427                 $aList = explode( ",", $sOrderArtSelList );
00428                 $oStr = getStr();
00429 
00430                 $aArticleSelList = $oArticle->getSelectLists();
00431 
00432                 //formating temporary list array from string
00433                 foreach ( $aList as $sList ) {
00434                     if ( $sList ) {
00435 
00436                         $aVal = explode( ":", $sList );
00437                         if ( isset($aVal[0]) && isset($aVal[1])) {
00438                             $sOrderArtListTitle = $oStr->strtolower( trim($aVal[0]) );
00439                             $sOrderArtSelValue  = $oStr->strtolower( trim($aVal[1]) );
00440 
00441                             //checking article list for matches with article list stored in oxorderitem
00442                             $iSelListNum = 0;
00443                             if ( count($aArticleSelList) > 0 ) {
00444                                 foreach ( $aArticleSelList as $aSelect ) {
00445                                     //chek if selects titles are equal
00446 
00447                                     if ( $oStr->strtolower($aSelect['name']) == $sOrderArtListTitle ) {
00448                                         //try to find matching select items value
00449                                         $iSelValueNum = 0;
00450                                         foreach ( $aSelect as $oSel ) {
00451                                             if ( $oStr->strtolower($oSel->name) == $sOrderArtSelValue ) {
00452                                                 // found, adding tu return array
00453                                                 $aRet[$iSelListNum] = $iSelValueNum;
00454                                             }
00455                                             //next article list item
00456                                             $iSelValueNum++;
00457                                         }
00458                                     }
00459                                     //next article list
00460                                     $iSelListNum++;
00461                                 }
00462                             }
00463                         }
00464                     }
00465                 }
00466             }
00467 
00468             $this->_aOrderArticleSelList = $aRet;
00469         }
00470 
00471         return $this->_aOrderArticleSelList;
00472     }
00473 
00483     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
00484     {
00485         return $this->getPrice();
00486     }
00487 
00493     public function skipDiscounts()
00494     {
00495         return false;
00496     }
00497 
00506     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
00507     {
00508         $aCatIds = array();
00509         if ( $oOrderArticle = $this->_getOrderArticle() ) {
00510             $aCatIds = $oOrderArticle->getCategoryIds( $blActCats, $blSkipCache );
00511         }
00512         return $aCatIds;
00513     }
00514 
00520     public function getLanguage()
00521     {
00522         return oxLang::getInstance()->getBaseLanguage();
00523     }
00524 
00532     public function getBasePrice( $dAmount = 1 )
00533     {
00534         return $this->getPrice();
00535     }
00536 
00542     public function getPrice()
00543     {
00544         $oBasePrice = oxNew( 'oxPrice' );
00545         // prices in db are ONLY brutto
00546         $oBasePrice->setBruttoPriceMode();
00547         $oBasePrice->setVat( $this->oxorderarticles__oxvat->value );
00548         $oBasePrice->setPrice( $this->oxorderarticles__oxbprice->value );
00549 
00550         return $oBasePrice;
00551     }
00552 
00560     public function setIsNewOrderItem( $blIsNew )
00561     {
00562         $this->_blIsNewOrderItem = $blIsNew;
00563     }
00564 
00570     public function isNewOrderItem()
00571     {
00572         return $this->_blIsNewOrderItem;
00573     }
00574 
00584     public function setNewAmount( $iNewAmount )
00585     {
00586         if ( $iNewAmount >= 0 ) {
00587             // to update stock we must first check if it is possible - article exists?
00588             $oArticle = oxNew( "oxarticle" );
00589             if ( $oArticle->load( $this->oxorderarticles__oxartid->value ) ) {
00590 
00591                 // updating stock info
00592                 $iStockChange = $iNewAmount - $this->oxorderarticles__oxamount->value;
00593                 if ( $iStockChange > 0 && ( $iOnStock = $oArticle->checkForStock( $iStockChange ) ) !== false ) {
00594                     if ( $iOnStock !== true ) {
00595                         $iStockChange = $iOnStock;
00596                         $iNewAmount   = $this->oxorderarticles__oxamount->value + $iStockChange;
00597                     }
00598                 }
00599 
00600                 $this->updateArticleStock( $iStockChange * -1, $this->getConfig()->getConfigParam( 'blAllowNegativeStock' ) );
00601 
00602                 // updating self
00603                 $this->oxorderarticles__oxamount = new oxField ( $iNewAmount, oxField::T_RAW );
00604                 $this->save();
00605             }
00606         }
00607     }
00608 
00614     public function isOrderArticle()
00615     {
00616         return true;
00617     }
00618 
00619 
00626    public function cancelOrderArticle()
00627    {
00628         if ( $this->oxorderarticles__oxstorno->value == 0 ) {
00629             $myConfig = $this->getConfig();
00630             $this->oxorderarticles__oxstorno->setValue( 1 );
00631             if ( $this->save() && $myConfig->getConfigParam( 'blUseStock' ) ) {
00632                 $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
00633             }
00634         }
00635    }
00636 
00645    public function delete( $sOXID = null)
00646    {
00647        if ( $blDelete = parent::delete( $sOXID ) ) {
00648            $myConfig = $this->getConfig();
00649            if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxorderarticles__oxstorno->value != 1 ) {
00650                $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
00651            }
00652        }
00653        return $blDelete;
00654    }
00655 
00663    public function save()
00664    {
00665        // ordered articles
00666        if ( ( $blSave = parent::save() ) && $this->isNewOrderItem() ) {
00667            $myConfig = $this->getConfig();
00668            if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00669                $this->updateArticleStock( $this->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
00670            }
00671 
00672            // marking object as "non new" disable further stock changes
00673            $this->setIsNewOrderItem( false );
00674        }
00675 
00676        return $blSave;
00677    }
00678 
00684    public function getWrapping()
00685    {
00686        if ($this->oxorderarticles__oxwrapid->value) {
00687            $oWrapping = oxNew('oxwrapping');
00688            if ($oWrapping->load($this->oxorderarticles__oxwrapid->value)) {
00689                return $oWrapping;
00690            }
00691        }
00692        return null;
00693    }
00694 }

Generated by  doxygen 1.6.2