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 $_blIsNewOrderItem = false;
00056 
00060     public function __construct()
00061     {
00062         parent::__construct();
00063         $this->init( 'oxorderarticles' );
00064     }
00065 
00073     public function copyThis( $oProduct )
00074     {
00075         $aObjectVars = get_object_vars( $oProduct );
00076 
00077         foreach ( $aObjectVars as $sName => $sValue ) {
00078             if ( isset( $oProduct->$sName->value ) ) {
00079                 $sFieldName = preg_replace('/oxarticles__/', 'oxorderarticles__', $sName);
00080                 $this->$sFieldName = $oProduct->$sName;
00081             }
00082         }
00083 
00084     }
00085 
00093     public function assign( $dbRecord )
00094     {
00095         parent::assign( $dbRecord );
00096         $this->_setArticleParams();
00097     }
00098 
00109     public function updateArticleStock( $dAddAmount, $blAllowNegativeStock = false )
00110     {
00111         // TODO: use oxarticle reduceStock
00112         // decrement stock if there is any
00113         $oArticle = oxNew( 'oxarticle' );
00114         $oArticle->load( $this->oxorderarticles__oxartid->value );
00115         $oArticle->beforeUpdate();
00116 
00117         // get real article stock count
00118         $iStockCount = $this->_getArtStock( $dAddAmount, $blAllowNegativeStock );
00119         $oDb = oxDb::getDb();
00120 
00121         // #874A. added oxarticles.oxtimestamp = oxarticles.oxtimestamp to keep old timestamp value
00122         $oArticle->oxarticles__oxstock = new oxField($iStockCount);
00123         $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->oxorderarticles__oxartid->value ) );
00124         $oArticle->onChange( ACTION_UPDATE_STOCK );
00125 
00126         //update article sold amount
00127         $oArticle->updateSoldAmount( $dAddAmount * ( -1 ) );
00128     }
00129 
00138     protected function _getArtStock( $dAddAmount = 0, $blAllowNegativeStock = false )
00139     {
00140         $oDb = oxDb::getDb();
00141 
00142         // #1592A. must take real value
00143         $sQ = 'select oxstock from oxarticles where oxid = '.$oDb->quote( $this->oxorderarticles__oxartid->value );
00144         $iStockCount  = ( float ) $oDb->getOne( $sQ );
00145 
00146         $iStockCount += $dAddAmount;
00147 
00148         // #1592A. calculating according new stock option
00149         if ( !$blAllowNegativeStock && $iStockCount < 0 ) {
00150             $iStockCount = 0;
00151         }
00152 
00153         return $iStockCount;
00154     }
00155 
00156 
00162     public function getPersParams()
00163     {
00164         if ( $this->_aPersParam != null ) {
00165             return $this->_aPersParam;
00166         }
00167 
00168         if ( $this->oxorderarticles__oxpersparam->value ) {
00169             $this->_aPersParam = unserialize( $this->oxorderarticles__oxpersparam->value );
00170         }
00171 
00172         return $this->_aPersParam;
00173     }
00174 
00182     public function setPersParams( $aParams )
00183     {
00184         $this->_aPersParam = $aParams;
00185 
00186         // serializing persisten info stored while ordering
00187         $this->oxorderarticles__oxpersparam = new oxField(serialize( $aParams ), oxField::T_RAW);
00188     }
00189 
00199     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
00200     {
00201         $sFieldName = strtolower($sFieldName);
00202         switch ( $sFieldName ) {
00203             case 'oxpersparam':
00204             case 'oxorderarticles__oxpersparam':
00205             case 'oxerpstatus':
00206             case 'oxorderarticles__oxerpstatus':
00207             case 'oxtitle':
00208             case 'oxorderarticles__oxtitle':
00209                 $iDataType = oxField::T_RAW;
00210                 break;
00211         }
00212         return parent::_setFieldData($sFieldName, $sValue, $iDataType);
00213     }
00214 
00223     public function loadInLang( $iLanguage, $sOxid )
00224     {
00225         return $this->load( $sOxid );
00226     }
00227 
00233     public function getProductId()
00234     {
00235         return $this->oxorderarticles__oxartid->value;
00236     }
00237 
00243     public function getProductParentId()
00244     {
00245         // when this field will be introduced there will be no need to load from real article
00246         if ( isset( $this->oxorderarticles__oxartparentid ) && $this->oxorderarticles__oxartparentid->value !== false ) {
00247             return $this->oxorderarticles__oxartparentid->value;
00248         }
00249 
00250         $oDb = oxDb::getDb();
00251         $oArticle = oxNew( "oxarticle" );
00252         $sQ = "select oxparentid from " . $oArticle->getViewName() . " where oxid=" . $oDb->quote( $this->getProductId() );
00253         $this->oxarticles__oxparentid = new oxField( $oDb->getOne( $sQ ) );
00254         return $this->oxarticles__oxparentid->value;
00255     }
00256 
00262     protected function _setArticleParams()
00263     {
00264         // creating needed fields
00265         $this->oxarticles__oxstock  = $this->oxorderarticles__oxamount;
00266         $this->oxarticles__oxtitle  = $this->oxorderarticles__oxtitle;
00267         $this->oxarticles__oxwidth  = $this->oxorderarticles__oxwidth;
00268         $this->oxarticles__oxlength = $this->oxorderarticles__oxlength;
00269         $this->oxarticles__oxheight = $this->oxorderarticles__oxheight;
00270         $this->oxarticles__oxweight = $this->oxorderarticles__oxweight;
00271         $this->oxarticles__oxsubclass  = $this->oxorderarticles__oxsubclass;
00272         $this->oxarticles__oxartnum    = $this->oxorderarticles__oxartnum;
00273         $this->oxarticles__oxshortdesc = $this->oxorderarticles__oxshortdesc;
00274 
00275         $this->oxarticles__oxvat    = $this->oxorderarticles__oxvat;
00276         $this->oxarticles__oxprice  = $this->oxorderarticles__oxprice;
00277         $this->oxarticles__oxbprice = $this->oxorderarticles__oxbprice;
00278 
00279         $this->oxarticles__oxthumb = $this->oxorderarticles__oxthumb;
00280         $this->oxarticles__oxpic1  = $this->oxorderarticles__oxpic1;
00281         $this->oxarticles__oxpic2  = $this->oxorderarticles__oxpic2;
00282         $this->oxarticles__oxpic3  = $this->oxorderarticles__oxpic3;
00283         $this->oxarticles__oxpic4  = $this->oxorderarticles__oxpic4;
00284         $this->oxarticles__oxpic5  = $this->oxorderarticles__oxpic5;
00285 
00286         $this->oxarticles__oxfile     = $this->oxorderarticles__oxfile;
00287         $this->oxarticles__oxdelivery = $this->oxorderarticles__oxdelivery;
00288         $this->oxarticles__oxissearch = $this->oxorderarticles__oxissearch;
00289         $this->oxarticles__oxfolder   = $this->oxorderarticles__oxfolder;
00290         $this->oxarticles__oxtemplate = $this->oxorderarticles__oxtemplate;
00291         $this->oxarticles__oxexturl   = $this->oxorderarticles__oxexturl;
00292         $this->oxarticles__oxurlimg   = $this->oxorderarticles__oxurlimg;
00293         $this->oxarticles__oxurldesc  = $this->oxorderarticles__oxurldesc;
00294         $this->oxarticles__oxshopid   = $this->oxorderarticles__oxordershopid;
00295         $this->oxarticles__oxquestionemail = $this->oxorderarticles__oxquestionemail;
00296         $this->oxarticles__oxsearchkeys    = $this->oxorderarticles__oxsearchkeys;
00297     }
00298 
00307     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
00308     {
00309         return true;
00310     }
00311 
00320     protected function _getOrderArticle( $sArticleId = null )
00321     {
00322         if ( $this->_oOrderArticle === null ) {
00323             $this->_oOrderArticle = false;
00324 
00325             $sArticleId = $sArticleId ? $sArticleId : $this->getProductId();
00326             $oArticle = oxNew( "oxArticle" );
00327             if ( $oArticle->load( $sArticleId ) ) {
00328                 $this->_oOrderArticle = $oArticle;
00329             }
00330         }
00331         return $this->_oOrderArticle;
00332     }
00333 
00341     public function getSelectLists( $sKeyPrefix = null )
00342     {
00343         $aSelLists = array();
00344         if ( $oArticle = $this->_getOrderArticle() ) {
00345             $aSelLists = $oArticle->getSelectLists();
00346         }
00347         return $aSelLists;
00348     }
00349 
00358     public function getOrderArticleSelectList( $sArtId = null, $sOrderArtSelList = null )
00359     {
00360         if ( $this->_aOrderArticleSelList === null ) {
00361 
00362             $sOrderArtSelList = $sOrderArtSelList ? $sOrderArtSelList : $this->oxorderarticles__oxselvariant->value;
00363 
00364             $aList = array();
00365             $aRet  = array();
00366 
00367             if ( $oArticle = $this->_getOrderArticle( $sArtId ) ) {
00368                 $aList = explode( ",", $sOrderArtSelList );
00369                 $oStr = getStr();
00370 
00371                 $aArticleSelList = $oArticle->getSelectLists();
00372 
00373                 //formating temporary list array from string
00374                 foreach ( $aList as $sList ) {
00375                     if ( $sList ) {
00376 
00377                         $aVal = explode( ":", $sList );
00378                         if ( isset($aVal[0]) && isset($aVal[1])) {
00379                             $sOrderArtListTitle = $oStr->strtolower( trim($aVal[0]) );
00380                             $sOrderArtSelValue  = $oStr->strtolower( trim($aVal[1]) );
00381 
00382                             //checking article list for matches with article list stored in oxorderitem
00383                             $iSelListNum = 0;
00384                             if ( count($aArticleSelList) > 0 ) {
00385                                 foreach ( $aArticleSelList as $aSelect ) {
00386                                     //chek if selects titles are equal
00387 
00388                                     if ( $oStr->strtolower($aSelect['name']) == $sOrderArtListTitle ) {
00389                                         //try to find matching select items value
00390                                         $iSelValueNum = 0;
00391                                         foreach ( $aSelect as $oSel ) {
00392                                             if ( $oStr->strtolower($oSel->name) == $sOrderArtSelValue ) {
00393                                                 // found, adding tu return array
00394                                                 $aRet[$iSelListNum] = $iSelValueNum;
00395                                             }
00396                                             //next article list item
00397                                             $iSelValueNum++;
00398                                         }
00399                                     }
00400                                     //next article list
00401                                     $iSelListNum++;
00402                                 }
00403                             }
00404                         }
00405                     }
00406                 }
00407             }
00408 
00409             $this->_aOrderArticleSelList = $aRet;
00410         }
00411 
00412         return $this->_aOrderArticleSelList;
00413     }
00414 
00424     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
00425     {
00426         return $this->getPrice();
00427     }
00428 
00434     public function skipDiscounts()
00435     {
00436         return false;
00437     }
00438 
00447     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
00448     {
00449         $aCatIds = array();
00450         if ( $oOrderArticle = $this->_getOrderArticle() ) {
00451             $aCatIds = $oOrderArticle->getCategoryIds( $blActCats, $blSkipCache );
00452         }
00453         return $aCatIds;
00454     }
00455 
00461     public function getLanguage()
00462     {
00463         return oxLang::getInstance()->getBaseLanguage();
00464     }
00465 
00473     public function getBasePrice( $dAmount = 1 )
00474     {
00475         return $this->getPrice();
00476     }
00477 
00483     public function getPrice()
00484     {
00485         $oBasePrice = oxNew( 'oxPrice' );
00486         // prices in db are ONLY brutto
00487         $oBasePrice->setBruttoPriceMode();
00488         $oBasePrice->setVat( $this->oxorderarticles__oxvat->value );
00489         $oBasePrice->setPrice( $this->oxorderarticles__oxbprice->value );
00490 
00491         return $oBasePrice;
00492     }
00493 
00501     public function setIsNewOrderItem( $blIsNew )
00502     {
00503         $this->_blIsNewOrderItem = $blIsNew;
00504     }
00505 
00511     public function isNewOrderItem()
00512     {
00513         return $this->_blIsNewOrderItem;
00514     }
00515 
00525     public function setNewAmount( $iNewAmount )
00526     {
00527         if ( $iNewAmount >= 0 ) {
00528             // to update stock we must first check if it is possible - article exists?
00529             $oArticle = oxNew( "oxarticle" );
00530             if ( $oArticle->load( $this->oxorderarticles__oxartid->value ) ) {
00531 
00532                 // updating stock info
00533                 $iStockChange = $iNewAmount - $this->oxorderarticles__oxamount->value;
00534                 if ( $iStockChange > 0 && ( $iOnStock = $oArticle->checkForStock( $iStockChange ) ) !== false ) {
00535                     if ( $iOnStock !== true ) {
00536                         $iStockChange = $iOnStock;
00537                         $iNewAmount   = $this->oxorderarticles__oxamount->value + $iStockChange;
00538                     }
00539                 }
00540 
00541                 $this->updateArticleStock( $iStockChange * -1, $this->getConfig()->getConfigParam( 'blAllowNegativeStock' ) );
00542 
00543                 // updating self
00544                 $this->oxorderarticles__oxamount = new oxField ( $iNewAmount, oxField::T_RAW );
00545                 $this->save();
00546             }
00547         }
00548     }
00549 
00555     public function isOrderArticle()
00556     {
00557         return true;
00558     }
00559 
00560 
00567     public function cancelOrderArticle()
00568     {
00569         if ( $this->oxorderarticles__oxstorno->value == 0 ) {
00570             $myConfig = $this->getConfig();
00571             $this->oxorderarticles__oxstorno->setValue( 1 );
00572             if ( $this->save() && $myConfig->getConfigParam( 'blUseStock' ) ) {
00573                 $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
00574             }
00575         }
00576     }
00577 
00586     public function delete( $sOXID = null)
00587     {
00588         if ( $blDelete = parent::delete( $sOXID ) ) {
00589             $myConfig = $this->getConfig();
00590             if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxorderarticles__oxstorno->value != 1 ) {
00591                 $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
00592             }
00593         }
00594         return $blDelete;
00595     }
00596 
00604     public function save()
00605     {
00606         // ordered articles
00607         if ( ( $blSave = parent::save() ) && $this->isNewOrderItem() ) {
00608             $myConfig = $this->getConfig();
00609             if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00610                 if ($myConfig->getConfigParam( 'blPsBasketReservationEnabled' )) {
00611                     $this->getSession()
00612                             ->getBasketReservations()
00613                             ->commitArticleReservation(
00614                                    $this->oxorderarticles__oxartid->value,
00615                                    $this->oxorderarticles__oxamount->value
00616                            );
00617                 } else {
00618                     $this->updateArticleStock( $this->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
00619                 }
00620             }
00621 
00622             // marking object as "non new" disable further stock changes
00623             $this->setIsNewOrderItem( false );
00624         }
00625 
00626         return $blSave;
00627     }
00628 
00634     public function getWrapping()
00635     {
00636         if ($this->oxorderarticles__oxwrapid->value) {
00637             $oWrapping = oxNew('oxwrapping');
00638             if ($oWrapping->load($this->oxorderarticles__oxwrapid->value)) {
00639                 return $oWrapping;
00640             }
00641         }
00642         return null;
00643     }
00644 
00650     public function isBundle()
00651     {
00652         return ( bool ) $this->oxorderarticles__oxisbundle->value;
00653     }
00654 
00660     public function getTotalBrutPriceFormated()
00661     {
00662         $oLang = oxLang::getInstance();
00663         $oOrder = $this->getOrder();
00664         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );        
00665         return $oLang->formatCurrency( $this->oxorderarticles__oxbrutprice->value, $oCurrency );
00666     }
00667 
00673     public function getBrutPriceFormated()
00674     {
00675         $oLang = oxLang::getInstance();
00676         $oOrder = $this->getOrder();
00677         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );    
00678         return $oLang->formatCurrency(  $this->oxorderarticles__oxbprice->value, $oCurrency );
00679     }
00680 
00686     public function getNetPriceFormated()
00687     {
00688         $oLang = oxLang::getInstance();
00689         $oOrder = $this->getOrder();
00690         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
00691         return $oLang->formatCurrency(  $this->oxorderarticles__oxnprice->value, $oCurrency );
00692     }
00693 
00699     public function getOrder()
00700     {
00701         if ( $this->oxorderarticles__oxorderid->value ) {
00702             // checking if the object already exists in the cache
00703             if ( isset( $this->_aOrderCache[ $this->oxorderarticles__oxorderid->value ] )) {
00704                 // returning the cached object
00705                 return $this->_aOrderCache[ $this->oxorderarticles__oxorderid->value ];
00706             }
00707             // creatina new order object and trying to load it
00708             $oOrder = oxNew( 'oxOrder' );
00709             if ( $oOrder->load( $this->oxorderarticles__oxorderid->value )) {
00710                 return $this->_aOrderCache[$this->oxorderarticles__oxorderid->value] = $oOrder;
00711             }
00712         }
00713         
00714         return null;
00715     }
00716 }