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