oxorderarticle.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxOrderArticle extends oxBase implements oxIArticle
00009 {
00010 
00014     protected static $_aOrderCache = array();
00015 
00021     protected $_sClassName = 'oxorderarticle';
00022 
00028     protected $_aPersParam = null;
00029 
00035     protected $_aStatuses = null;
00036 
00042     protected $_aOrderArticleSelList = null;
00043 
00049     protected $_oOrderArticle = null;
00050 
00056     protected $_oArticle = null;
00057 
00063     protected $_blIsNewOrderItem = false;
00064 
00071     protected $_aSkipSaveFields = array( 'oxtimestamp' );
00072 
00076     public function __construct()
00077     {
00078         parent::__construct();
00079         $this->init( 'oxorderarticles' );
00080     }
00081 
00089     public function copyThis( $oProduct )
00090     {
00091         $aObjectVars = get_object_vars( $oProduct );
00092 
00093         foreach ( $aObjectVars as $sName => $sValue ) {
00094             if ( isset( $oProduct->$sName->value ) ) {
00095                 $sFieldName = preg_replace('/oxarticles__/', 'oxorderarticles__', $sName);
00096                 if ( $sFieldName != "oxorderarticles__oxtimestamp" ) {
00097                     $this->$sFieldName = $oProduct->$sName;
00098                 }
00099                 // formatting view
00100                 if ( !$this->getConfig()->getConfigParam( 'blSkipFormatConversion' ) ) {
00101                     if ( $sFieldName == "oxorderarticles__oxinsert" ) {
00102                         oxRegistry::get("oxUtilsDate")->convertDBDate( $this->$sFieldName, true );
00103                     }
00104                 }
00105             }
00106         }
00107 
00108     }
00109 
00117     public function assign( $dbRecord )
00118     {
00119         parent::assign( $dbRecord );
00120         $this->_setArticleParams();
00121     }
00122 
00133     public function updateArticleStock( $dAddAmount, $blAllowNegativeStock = false )
00134     {
00135         // TODO: use oxarticle reduceStock
00136         // decrement stock if there is any
00137         $oArticle = oxNew( 'oxarticle' );
00138         $oArticle->load( $this->oxorderarticles__oxartid->value );
00139         $oArticle->beforeUpdate();
00140 
00141         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) ) {
00142             // get real article stock count
00143             $iStockCount = $this->_getArtStock( $dAddAmount, $blAllowNegativeStock );
00144             $oDb = oxDb::getDb();
00145 
00146             $oArticle->oxarticles__oxstock = new oxField($iStockCount);
00147             $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->oxorderarticles__oxartid->value ) );
00148             $oArticle->onChange( ACTION_UPDATE_STOCK );
00149         }
00150 
00151         //update article sold amount
00152         $oArticle->updateSoldAmount( $dAddAmount * ( -1 ) );
00153     }
00154 
00163     protected function _getArtStock( $dAddAmount = 0, $blAllowNegativeStock = false )
00164     {
00165         $oDb = oxDb::getDb();
00166 
00167         // #1592A. must take real value
00168         $sQ = 'select oxstock from oxarticles where oxid = '.$oDb->quote( $this->oxorderarticles__oxartid->value );
00169         $iStockCount  = ( float ) $oDb->getOne( $sQ, false, false );
00170 
00171         $iStockCount += $dAddAmount;
00172 
00173         // #1592A. calculating according new stock option
00174         if ( !$blAllowNegativeStock && $iStockCount < 0 ) {
00175             $iStockCount = 0;
00176         }
00177 
00178         return $iStockCount;
00179     }
00180 
00181 
00187     public function getPersParams()
00188     {
00189         if ( $this->_aPersParam != null ) {
00190             return $this->_aPersParam;
00191         }
00192 
00193         if ( $this->oxorderarticles__oxpersparam->value ) {
00194             $this->_aPersParam = unserialize( $this->oxorderarticles__oxpersparam->value );
00195         }
00196 
00197         return $this->_aPersParam;
00198     }
00199 
00207     public function setPersParams( $aParams )
00208     {
00209         $this->_aPersParam = $aParams;
00210 
00211         // serializing persisten info stored while ordering
00212         $this->oxorderarticles__oxpersparam = new oxField(serialize( $aParams ), oxField::T_RAW);
00213     }
00214 
00224     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
00225     {
00226         $sFieldName = strtolower($sFieldName);
00227         switch ( $sFieldName ) {
00228             case 'oxpersparam':
00229             case 'oxorderarticles__oxpersparam':
00230             case 'oxerpstatus':
00231             case 'oxorderarticles__oxerpstatus':
00232             case 'oxtitle':
00233             case 'oxorderarticles__oxtitle':
00234                 $iDataType = oxField::T_RAW;
00235                 break;
00236         }
00237         return parent::_setFieldData($sFieldName, $sValue, $iDataType);
00238     }
00239 
00248     public function loadInLang( $iLanguage, $sOxid )
00249     {
00250         return $this->load( $sOxid );
00251     }
00252 
00258     public function getProductId()
00259     {
00260         return $this->oxorderarticles__oxartid->value;
00261     }
00262 
00268     public function getProductParentId()
00269     {
00270         // when this field will be introduced there will be no need to load from real article
00271         if ( isset( $this->oxorderarticles__oxartparentid ) && $this->oxorderarticles__oxartparentid->value !== false ) {
00272             return $this->oxorderarticles__oxartparentid->value;
00273         }
00274 
00275         $oDb = oxDb::getDb();
00276         $oArticle = oxNew( "oxarticle" );
00277         $sQ = "select oxparentid from " . $oArticle->getViewName() . " where oxid=" . $oDb->quote( $this->getProductId() );
00278         $this->oxarticles__oxparentid = new oxField( $oDb->getOne( $sQ ) );
00279         return $this->oxarticles__oxparentid->value;
00280     }
00281 
00287     protected function _setArticleParams()
00288     {
00289         // creating needed fields
00290         $this->oxarticles__oxstock  = $this->oxorderarticles__oxamount;
00291         $this->oxarticles__oxtitle  = $this->oxorderarticles__oxtitle;
00292         $this->oxarticles__oxwidth  = $this->oxorderarticles__oxwidth;
00293         $this->oxarticles__oxlength = $this->oxorderarticles__oxlength;
00294         $this->oxarticles__oxheight = $this->oxorderarticles__oxheight;
00295         $this->oxarticles__oxweight = $this->oxorderarticles__oxweight;
00296         $this->oxarticles__oxsubclass  = $this->oxorderarticles__oxsubclass;
00297         $this->oxarticles__oxartnum    = $this->oxorderarticles__oxartnum;
00298         $this->oxarticles__oxshortdesc = $this->oxorderarticles__oxshortdesc;
00299 
00300         $this->oxarticles__oxvat    = $this->oxorderarticles__oxvat;
00301         $this->oxarticles__oxprice  = $this->oxorderarticles__oxprice;
00302         $this->oxarticles__oxbprice = $this->oxorderarticles__oxbprice;
00303 
00304         $this->oxarticles__oxthumb = $this->oxorderarticles__oxthumb;
00305         $this->oxarticles__oxpic1  = $this->oxorderarticles__oxpic1;
00306         $this->oxarticles__oxpic2  = $this->oxorderarticles__oxpic2;
00307         $this->oxarticles__oxpic3  = $this->oxorderarticles__oxpic3;
00308         $this->oxarticles__oxpic4  = $this->oxorderarticles__oxpic4;
00309         $this->oxarticles__oxpic5  = $this->oxorderarticles__oxpic5;
00310 
00311         $this->oxarticles__oxfile     = $this->oxorderarticles__oxfile;
00312         $this->oxarticles__oxdelivery = $this->oxorderarticles__oxdelivery;
00313         $this->oxarticles__oxissearch = $this->oxorderarticles__oxissearch;
00314         $this->oxarticles__oxfolder   = $this->oxorderarticles__oxfolder;
00315         $this->oxarticles__oxtemplate = $this->oxorderarticles__oxtemplate;
00316         $this->oxarticles__oxexturl   = $this->oxorderarticles__oxexturl;
00317         $this->oxarticles__oxurlimg   = $this->oxorderarticles__oxurlimg;
00318         $this->oxarticles__oxurldesc  = $this->oxorderarticles__oxurldesc;
00319         $this->oxarticles__oxshopid   = $this->oxorderarticles__oxordershopid;
00320         $this->oxarticles__oxquestionemail = $this->oxorderarticles__oxquestionemail;
00321         $this->oxarticles__oxsearchkeys    = $this->oxorderarticles__oxsearchkeys;
00322     }
00323 
00332     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
00333     {
00334         return true;
00335     }
00336 
00345     protected function _getOrderArticle( $sArticleId = null )
00346     {
00347         if ( $this->_oOrderArticle === null ) {
00348             $this->_oOrderArticle = false;
00349 
00350             $sArticleId = $sArticleId ? $sArticleId : $this->getProductId();
00351             $oArticle = oxNew( "oxArticle" );
00352             if ( $oArticle->load( $sArticleId ) ) {
00353                 $this->_oOrderArticle = $oArticle;
00354             }
00355         }
00356         return $this->_oOrderArticle;
00357     }
00358 
00366     public function getSelectLists( $sKeyPrefix = null )
00367     {
00368         $aSelLists = array();
00369         if ( $oArticle = $this->_getOrderArticle() ) {
00370             $aSelLists = $oArticle->getSelectLists();
00371         }
00372         return $aSelLists;
00373     }
00374 
00383     public function getOrderArticleSelectList( $sArtId = null, $sOrderArtSelList = null )
00384     {
00385         if ( $this->_aOrderArticleSelList === null ) {
00386 
00387             $sOrderArtSelList = $sOrderArtSelList ? $sOrderArtSelList : $this->oxorderarticles__oxselvariant->value;
00388 
00389             $aList = array();
00390             $aRet  = array();
00391 
00392             if ( $oArticle = $this->_getOrderArticle( $sArtId ) ) {
00393                 $aList = explode( ", ", $sOrderArtSelList );
00394                 $oStr = getStr();
00395 
00396                 $aArticleSelList = $oArticle->getSelectLists();
00397 
00398                 //formatting temporary list array from string
00399                 foreach ( $aList as $sList ) {
00400                     if ( $sList ) {
00401 
00402                         $aVal = explode( ":", $sList );
00403                         if ( isset($aVal[0]) && isset($aVal[1])) {
00404                             $sOrderArtListTitle = $oStr->strtolower( trim($aVal[0]) );
00405                             $sOrderArtSelValue  = $oStr->strtolower( trim($aVal[1]) );
00406 
00407                             //checking article list for matches with article list stored in oxOrderItem
00408                             $iSelListNum = 0;
00409                             if ( count($aArticleSelList) > 0 ) {
00410                                 foreach ( $aArticleSelList as $aSelect ) {
00411                                     //check if selects titles are equal
00412 
00413                                     if ( $oStr->strtolower($aSelect['name']) == $sOrderArtListTitle ) {
00414                                         //try to find matching select items value
00415                                         $iSelValueNum = 0;
00416                                         foreach ( $aSelect as $oSel ) {
00417                                             if ( $oStr->strtolower($oSel->name) == $sOrderArtSelValue ) {
00418                                                 // found, adding to return array
00419                                                 $aRet[$iSelListNum] = $iSelValueNum;
00420                                             }
00421                                             //next article list item
00422                                             $iSelValueNum++;
00423                                         }
00424                                     }
00425                                     //next article list
00426                                     $iSelListNum++;
00427                                 }
00428                             }
00429                         }
00430                     }
00431                 }
00432             }
00433 
00434             $this->_aOrderArticleSelList = $aRet;
00435         }
00436 
00437         return $this->_aOrderArticleSelList;
00438     }
00439 
00449     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
00450     {
00451         $oArticle = $this->_getOrderArticle();
00452         if ( $oArticle ) {
00453             return $oArticle->getBasketPrice( $dAmount, $aSelList, $oBasket );
00454         } else {
00455             return $this->getPrice();
00456         }
00457     }
00458 
00464     public function skipDiscounts()
00465     {
00466         return false;
00467     }
00468 
00477     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
00478     {
00479         $aCatIds = array();
00480         if ( $oOrderArticle = $this->_getOrderArticle() ) {
00481             $aCatIds = $oOrderArticle->getCategoryIds( $blActCats, $blSkipCache );
00482         }
00483         return $aCatIds;
00484     }
00485 
00491     public function getLanguage()
00492     {
00493         return oxRegistry::getLang()->getBaseLanguage();
00494     }
00495 
00503     public function getBasePrice( $dAmount = 1 )
00504     {
00505 
00506         return $this->getPrice();
00507     }
00508 
00514     public function getPrice()
00515     {
00516         $oBasePrice = oxNew( 'oxPrice' );
00517         // prices in db are ONLY brutto
00518         $oBasePrice->setBruttoPriceMode();
00519         $oBasePrice->setVat( $this->oxorderarticles__oxvat->value );
00520         $oBasePrice->setPrice( $this->oxorderarticles__oxbprice->value );
00521 
00522         return $oBasePrice;
00523     }
00524 
00532     public function setIsNewOrderItem( $blIsNew )
00533     {
00534         $this->_blIsNewOrderItem = $blIsNew;
00535     }
00536 
00542     public function isNewOrderItem()
00543     {
00544         return $this->_blIsNewOrderItem;
00545     }
00546 
00556     public function setNewAmount( $iNewAmount )
00557     {
00558         if ( $iNewAmount >= 0 ) {
00559             // to update stock we must first check if it is possible - article exists?
00560             $oArticle = oxNew( "oxarticle" );
00561             if ( $oArticle->load( $this->oxorderarticles__oxartid->value ) ) {
00562 
00563                 // updating stock info
00564                 $iStockChange = $iNewAmount - $this->oxorderarticles__oxamount->value;
00565                 if ( $iStockChange > 0 && ( $iOnStock = $oArticle->checkForStock( $iStockChange ) ) !== false ) {
00566                     if ( $iOnStock !== true ) {
00567                         $iStockChange = $iOnStock;
00568                         $iNewAmount   = $this->oxorderarticles__oxamount->value + $iStockChange;
00569                     }
00570                 }
00571 
00572                 $this->updateArticleStock( $iStockChange * -1, $this->getConfig()->getConfigParam( 'blAllowNegativeStock' ) );
00573 
00574                 // updating self
00575                 $this->oxorderarticles__oxamount = new oxField ( $iNewAmount, oxField::T_RAW );
00576                 $this->save();
00577             }
00578         }
00579     }
00580 
00586     public function isOrderArticle()
00587     {
00588         return true;
00589     }
00590 
00591 
00598     public function cancelOrderArticle()
00599     {
00600         if ( $this->oxorderarticles__oxstorno->value == 0 ) {
00601             $myConfig = $this->getConfig();
00602             $this->oxorderarticles__oxstorno = new oxField( 1 );
00603             if ( $this->save() ) {
00604                 $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
00605             }
00606         }
00607     }
00608 
00617     public function delete( $sOXID = null)
00618     {
00619         if ( $blDelete = parent::delete( $sOXID ) ) {
00620             $myConfig = $this->getConfig();
00621             if ( $this->oxorderarticles__oxstorno->value != 1 ) {
00622                 $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
00623             }
00624         }
00625         return $blDelete;
00626     }
00627 
00635     public function save()
00636     {
00637         // ordered articles
00638         if ( ( $blSave = parent::save() ) && $this->isNewOrderItem() ) {
00639             $myConfig = $this->getConfig();
00640             if ( $myConfig->getConfigParam( 'blUseStock' ) &&
00641                  $myConfig->getConfigParam( 'blPsBasketReservationEnabled' ) ) {
00642                     $this->getSession()
00643                             ->getBasketReservations()
00644                             ->commitArticleReservation(
00645                                    $this->oxorderarticles__oxartid->value,
00646                                    $this->oxorderarticles__oxamount->value
00647                            );
00648             } else {
00649                 $this->updateArticleStock( $this->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
00650             }
00651 
00652             // seting downloadable products article files
00653             $this->_setOrderFiles();
00654 
00655             // marking object as "non new" disable further stock changes
00656             $this->setIsNewOrderItem( false );
00657         }
00658 
00659         return $blSave;
00660     }
00661 
00667     public function getWrapping()
00668     {
00669         if ($this->oxorderarticles__oxwrapid->value) {
00670             $oWrapping = oxNew('oxwrapping');
00671             if ($oWrapping->load($this->oxorderarticles__oxwrapid->value)) {
00672                 return $oWrapping;
00673             }
00674         }
00675         return null;
00676     }
00677 
00683     public function isBundle()
00684     {
00685         return ( bool ) $this->oxorderarticles__oxisbundle->value;
00686     }
00687 
00693     public function getTotalBrutPriceFormated()
00694     {
00695         $oLang = oxRegistry::getLang();
00696         $oOrder = $this->getOrder();
00697         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
00698         return $oLang->formatCurrency( $this->oxorderarticles__oxbrutprice->value, $oCurrency );
00699     }
00700 
00706     public function getBrutPriceFormated()
00707     {
00708         $oLang = oxRegistry::getLang();
00709         $oOrder = $this->getOrder();
00710         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
00711         return $oLang->formatCurrency(  $this->oxorderarticles__oxbprice->value, $oCurrency );
00712     }
00713 
00719     public function getNetPriceFormated()
00720     {
00721         $oLang = oxRegistry::getLang();
00722         $oOrder = $this->getOrder();
00723         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
00724         return $oLang->formatCurrency(  $this->oxorderarticles__oxnprice->value, $oCurrency );
00725     }
00726 
00732     public function getOrder()
00733     {
00734         if ( $this->oxorderarticles__oxorderid->value ) {
00735             // checking if the object already exists in the cache
00736             if ( isset( $this->_aOrderCache[ $this->oxorderarticles__oxorderid->value ] )) {
00737                 // returning the cached object
00738                 return $this->_aOrderCache[ $this->oxorderarticles__oxorderid->value ];
00739             }
00740             // creatina new order object and trying to load it
00741             $oOrder = oxNew( 'oxOrder' );
00742             if ( $oOrder->load( $this->oxorderarticles__oxorderid->value )) {
00743                 return $this->_aOrderCache[$this->oxorderarticles__oxorderid->value] = $oOrder;
00744             }
00745         }
00746 
00747         return null;
00748     }
00749 
00757     protected function _insert()
00758     {
00759         $iInsertTime = time();
00760         $now = date('Y-m-d H:i:s', $iInsertTime);
00761         $this->oxorderarticles__oxtimestamp = new oxField( $now );
00762 
00763         return parent::_insert();
00764     }
00765 
00766 
00774     public function setArticle( $oArticle )
00775     {
00776         $this->_oArticle = $oArticle;
00777     }
00778 
00784     public function getArticle()
00785     {
00786         if ( $this->_oArticle === null ) {
00787             $oArticle = oxNew( 'oxArticle' );
00788             $oArticle->load($this->oxorderarticles__oxartid->value);
00789             $this->_oArticle = $oArticle;
00790         }
00791 
00792         return $this->_oArticle;
00793     }
00794 
00795 
00796 
00802     public function _setOrderFiles()
00803     {
00804         $oArticle = $this->getArticle();
00805 
00806         if ( $oArticle->oxarticles__oxisdownloadable->value ) {
00807 
00808             $oConfig          = $this->getConfig();
00809             $sOrderId          = $this->oxorderarticles__oxorderid->value;
00810             $sOrderArticleId = $this->getId();
00811             $sShopId          = $oConfig->getShopId();
00812 
00813             $oUser             = $oConfig->getUser();
00814 
00815             $oFiles = $oArticle->getArticleFiles( true );
00816 
00817             if ( $oFiles ) {
00818                 foreach ($oFiles as $oFile) {
00819                     $oOrderFile = oxNew( 'oxOrderFile' );
00820                     $oOrderFile->setOrderId( $sOrderId );
00821                     $oOrderFile->setOrderArticleId( $sOrderArticleId );
00822                     $oOrderFile->setShopId( $sShopId );
00823                     $iMaxDownloadCount = (!empty($oUser) && !$oUser->hasAccount()) ? $oFile->getMaxUnregisteredDownloadsCount() :  $oFile->getMaxDownloadsCount();
00824                     $oOrderFile->setFile(
00825                         $oFile->oxfiles__oxfilename->value,
00826                         $oFile->getId(),
00827                         $iMaxDownloadCount * $this->oxorderarticles__oxamount->value,
00828                         $oFile->getLinkExpirationTime(),
00829                         $oFile->getDownloadExpirationTime()
00830                     );
00831 
00832                     $oOrderFile->save();
00833                 }
00834             }
00835         }
00836     }
00837 
00843     public function getTotalNetPriceFormated()
00844     {
00845         $oLang = oxRegistry::getLang();
00846         $oOrder = $this->getOrder();
00847         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
00848         return $oLang->formatCurrency( $this->oxorderarticles__oxnetprice->value, $oCurrency );
00849     }
00850 }