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         $oArticle = $this->_getOrderArticle();
00427         if ( $oArticle ) {
00428             return $oArticle->getBasketPrice( $dAmount, $aSelList, $oBasket );
00429         } else {
00430             return $this->getPrice();
00431         }
00432     }
00433 
00439     public function skipDiscounts()
00440     {
00441         return false;
00442     }
00443 
00452     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
00453     {
00454         $aCatIds = array();
00455         if ( $oOrderArticle = $this->_getOrderArticle() ) {
00456             $aCatIds = $oOrderArticle->getCategoryIds( $blActCats, $blSkipCache );
00457         }
00458         return $aCatIds;
00459     }
00460 
00466     public function getLanguage()
00467     {
00468         return oxLang::getInstance()->getBaseLanguage();
00469     }
00470 
00478     public function getBasePrice( $dAmount = 1 )
00479     {
00480         return $this->getPrice();
00481     }
00482 
00488     public function getPrice()
00489     {
00490         $oBasePrice = oxNew( 'oxPrice' );
00491         // prices in db are ONLY brutto
00492         $oBasePrice->setBruttoPriceMode();
00493         $oBasePrice->setVat( $this->oxorderarticles__oxvat->value );
00494         $oBasePrice->setPrice( $this->oxorderarticles__oxbprice->value );
00495 
00496         return $oBasePrice;
00497     }
00498 
00506     public function setIsNewOrderItem( $blIsNew )
00507     {
00508         $this->_blIsNewOrderItem = $blIsNew;
00509     }
00510 
00516     public function isNewOrderItem()
00517     {
00518         return $this->_blIsNewOrderItem;
00519     }
00520 
00530     public function setNewAmount( $iNewAmount )
00531     {
00532         if ( $iNewAmount >= 0 ) {
00533             // to update stock we must first check if it is possible - article exists?
00534             $oArticle = oxNew( "oxarticle" );
00535             if ( $oArticle->load( $this->oxorderarticles__oxartid->value ) ) {
00536 
00537                 // updating stock info
00538                 $iStockChange = $iNewAmount - $this->oxorderarticles__oxamount->value;
00539                 if ( $iStockChange > 0 && ( $iOnStock = $oArticle->checkForStock( $iStockChange ) ) !== false ) {
00540                     if ( $iOnStock !== true ) {
00541                         $iStockChange = $iOnStock;
00542                         $iNewAmount   = $this->oxorderarticles__oxamount->value + $iStockChange;
00543                     }
00544                 }
00545 
00546                 $this->updateArticleStock( $iStockChange * -1, $this->getConfig()->getConfigParam( 'blAllowNegativeStock' ) );
00547 
00548                 // updating self
00549                 $this->oxorderarticles__oxamount = new oxField ( $iNewAmount, oxField::T_RAW );
00550                 $this->save();
00551             }
00552         }
00553     }
00554 
00560     public function isOrderArticle()
00561     {
00562         return true;
00563     }
00564 
00565 
00572     public function cancelOrderArticle()
00573     {
00574         if ( $this->oxorderarticles__oxstorno->value == 0 ) {
00575             $myConfig = $this->getConfig();
00576             $this->oxorderarticles__oxstorno->setValue( 1 );
00577             if ( $this->save() && $myConfig->getConfigParam( 'blUseStock' ) ) {
00578                 $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
00579             }
00580         }
00581     }
00582 
00591     public function delete( $sOXID = null)
00592     {
00593         if ( $blDelete = parent::delete( $sOXID ) ) {
00594             $myConfig = $this->getConfig();
00595             if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxorderarticles__oxstorno->value != 1 ) {
00596                 $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
00597             }
00598         }
00599         return $blDelete;
00600     }
00601 
00609     public function save()
00610     {
00611         // ordered articles
00612         if ( ( $blSave = parent::save() ) && $this->isNewOrderItem() ) {
00613             $myConfig = $this->getConfig();
00614             if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00615                 if ($myConfig->getConfigParam( 'blPsBasketReservationEnabled' )) {
00616                     $this->getSession()
00617                             ->getBasketReservations()
00618                             ->commitArticleReservation(
00619                                    $this->oxorderarticles__oxartid->value,
00620                                    $this->oxorderarticles__oxamount->value
00621                            );
00622                 } else {
00623                     $this->updateArticleStock( $this->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
00624                 }
00625             }
00626 
00627             // marking object as "non new" disable further stock changes
00628             $this->setIsNewOrderItem( false );
00629         }
00630 
00631         return $blSave;
00632     }
00633 
00639     public function getWrapping()
00640     {
00641         if ($this->oxorderarticles__oxwrapid->value) {
00642             $oWrapping = oxNew('oxwrapping');
00643             if ($oWrapping->load($this->oxorderarticles__oxwrapid->value)) {
00644                 return $oWrapping;
00645             }
00646         }
00647         return null;
00648     }
00649 
00655     public function isBundle()
00656     {
00657         return ( bool ) $this->oxorderarticles__oxisbundle->value;
00658     }
00659 
00665     public function getTotalBrutPriceFormated()
00666     {
00667         $oLang = oxLang::getInstance();
00668         $oOrder = $this->getOrder();
00669         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );        
00670         return $oLang->formatCurrency( $this->oxorderarticles__oxbrutprice->value, $oCurrency );
00671     }
00672 
00678     public function getBrutPriceFormated()
00679     {
00680         $oLang = oxLang::getInstance();
00681         $oOrder = $this->getOrder();
00682         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );    
00683         return $oLang->formatCurrency(  $this->oxorderarticles__oxbprice->value, $oCurrency );
00684     }
00685 
00691     public function getNetPriceFormated()
00692     {
00693         $oLang = oxLang::getInstance();
00694         $oOrder = $this->getOrder();
00695         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
00696         return $oLang->formatCurrency(  $this->oxorderarticles__oxnprice->value, $oCurrency );
00697     }
00698 
00704     public function getOrder()
00705     {
00706         if ( $this->oxorderarticles__oxorderid->value ) {
00707             // checking if the object already exists in the cache
00708             if ( isset( $this->_aOrderCache[ $this->oxorderarticles__oxorderid->value ] )) {
00709                 // returning the cached object
00710                 return $this->_aOrderCache[ $this->oxorderarticles__oxorderid->value ];
00711             }
00712             // creatina new order object and trying to load it
00713             $oOrder = oxNew( 'oxOrder' );
00714             if ( $oOrder->load( $this->oxorderarticles__oxorderid->value )) {
00715                 return $this->_aOrderCache[$this->oxorderarticles__oxorderid->value] = $oOrder;
00716             }
00717         }
00718         
00719         return null;
00720     }
00721 }