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             $oArticle->setLoadParentData(true);
00353             if ( $oArticle->load( $sArticleId ) ) {
00354                 $this->_oOrderArticle = $oArticle;
00355             }
00356         }
00357         return $this->_oOrderArticle;
00358     }
00359 
00367     public function getSelectLists( $sKeyPrefix = null )
00368     {
00369         $aSelLists = array();
00370         if ( $oArticle = $this->_getOrderArticle() ) {
00371             $aSelLists = $oArticle->getSelectLists();
00372         }
00373         return $aSelLists;
00374     }
00375 
00384     public function getOrderArticleSelectList( $sArtId = null, $sOrderArtSelList = null )
00385     {
00386         if ( $this->_aOrderArticleSelList === null ) {
00387 
00388             $sOrderArtSelList = $sOrderArtSelList ? $sOrderArtSelList : $this->oxorderarticles__oxselvariant->value;
00389 
00390             $aList = array();
00391             $aRet  = array();
00392 
00393             if ( $oArticle = $this->_getOrderArticle( $sArtId ) ) {
00394                 $aList = explode( ", ", $sOrderArtSelList );
00395                 $oStr = getStr();
00396 
00397                 $aArticleSelList = $oArticle->getSelectLists();
00398 
00399                 //formatting temporary list array from string
00400                 foreach ( $aList as $sList ) {
00401                     if ( $sList ) {
00402 
00403                         $aVal = explode( ":", $sList );
00404                         if ( isset($aVal[0]) && isset($aVal[1])) {
00405                             $sOrderArtListTitle = $oStr->strtolower( trim($aVal[0]) );
00406                             $sOrderArtSelValue  = $oStr->strtolower( trim($aVal[1]) );
00407 
00408                             //checking article list for matches with article list stored in oxOrderItem
00409                             $iSelListNum = 0;
00410                             if ( count($aArticleSelList) > 0 ) {
00411                                 foreach ( $aArticleSelList as $aSelect ) {
00412                                     //check if selects titles are equal
00413 
00414                                     if ( $oStr->strtolower($aSelect['name']) == $sOrderArtListTitle ) {
00415                                         //try to find matching select items value
00416                                         $iSelValueNum = 0;
00417                                         foreach ( $aSelect as $oSel ) {
00418                                             if ( $oStr->strtolower($oSel->name) == $sOrderArtSelValue ) {
00419                                                 // found, adding to return array
00420                                                 $aRet[$iSelListNum] = $iSelValueNum;
00421                                             }
00422                                             //next article list item
00423                                             $iSelValueNum++;
00424                                         }
00425                                     }
00426                                     //next article list
00427                                     $iSelListNum++;
00428                                 }
00429                             }
00430                         }
00431                     }
00432                 }
00433             }
00434 
00435             $this->_aOrderArticleSelList = $aRet;
00436         }
00437 
00438         return $this->_aOrderArticleSelList;
00439     }
00440 
00450     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
00451     {
00452         $oArticle = $this->_getOrderArticle();
00453         if ( $oArticle ) {
00454             return $oArticle->getBasketPrice( $dAmount, $aSelList, $oBasket );
00455         } else {
00456             return $this->getPrice();
00457         }
00458     }
00459 
00465     public function skipDiscounts()
00466     {
00467         return false;
00468     }
00469 
00478     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
00479     {
00480         $aCatIds = array();
00481         if ( $oOrderArticle = $this->_getOrderArticle() ) {
00482             $aCatIds = $oOrderArticle->getCategoryIds( $blActCats, $blSkipCache );
00483         }
00484         return $aCatIds;
00485     }
00486 
00492     public function getLanguage()
00493     {
00494         return oxRegistry::getLang()->getBaseLanguage();
00495     }
00496 
00504     public function getBasePrice( $dAmount = 1 )
00505     {
00506 
00507         return $this->getPrice();
00508     }
00509 
00515     public function getPrice()
00516     {
00517         $oBasePrice = oxNew( 'oxPrice' );
00518         // prices in db are ONLY brutto
00519         $oBasePrice->setBruttoPriceMode();
00520         $oBasePrice->setVat( $this->oxorderarticles__oxvat->value );
00521         $oBasePrice->setPrice( $this->oxorderarticles__oxbprice->value );
00522 
00523         return $oBasePrice;
00524     }
00525 
00533     public function setIsNewOrderItem( $blIsNew )
00534     {
00535         $this->_blIsNewOrderItem = $blIsNew;
00536     }
00537 
00543     public function isNewOrderItem()
00544     {
00545         return $this->_blIsNewOrderItem;
00546     }
00547 
00557     public function setNewAmount( $iNewAmount )
00558     {
00559         if ( $iNewAmount >= 0 ) {
00560             // to update stock we must first check if it is possible - article exists?
00561             $oArticle = oxNew( "oxarticle" );
00562             if ( $oArticle->load( $this->oxorderarticles__oxartid->value ) ) {
00563 
00564                 // updating stock info
00565                 $iStockChange = $iNewAmount - $this->oxorderarticles__oxamount->value;
00566                 if ( $iStockChange > 0 && ( $iOnStock = $oArticle->checkForStock( $iStockChange ) ) !== false ) {
00567                     if ( $iOnStock !== true ) {
00568                         $iStockChange = $iOnStock;
00569                         $iNewAmount   = $this->oxorderarticles__oxamount->value + $iStockChange;
00570                     }
00571                 }
00572 
00573                 $this->updateArticleStock( $iStockChange * -1, $this->getConfig()->getConfigParam( 'blAllowNegativeStock' ) );
00574 
00575                 // updating self
00576                 $this->oxorderarticles__oxamount = new oxField ( $iNewAmount, oxField::T_RAW );
00577                 $this->save();
00578             }
00579         }
00580     }
00581 
00587     public function isOrderArticle()
00588     {
00589         return true;
00590     }
00591 
00592 
00599     public function cancelOrderArticle()
00600     {
00601         if ( $this->oxorderarticles__oxstorno->value == 0 ) {
00602             $myConfig = $this->getConfig();
00603             $this->oxorderarticles__oxstorno = new oxField( 1 );
00604             if ( $this->save() ) {
00605                 $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
00606             }
00607         }
00608     }
00609 
00618     public function delete( $sOXID = null)
00619     {
00620         if ( $blDelete = parent::delete( $sOXID ) ) {
00621             $myConfig = $this->getConfig();
00622             if ( $this->oxorderarticles__oxstorno->value != 1 ) {
00623                 $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
00624             }
00625         }
00626         return $blDelete;
00627     }
00628 
00636     public function save()
00637     {
00638         // ordered articles
00639         if ( ( $blSave = parent::save() ) && $this->isNewOrderItem() ) {
00640             $myConfig = $this->getConfig();
00641             if ( $myConfig->getConfigParam( 'blUseStock' ) &&
00642                  $myConfig->getConfigParam( 'blPsBasketReservationEnabled' ) ) {
00643                     $this->getSession()
00644                             ->getBasketReservations()
00645                             ->commitArticleReservation(
00646                                    $this->oxorderarticles__oxartid->value,
00647                                    $this->oxorderarticles__oxamount->value
00648                            );
00649             } else {
00650                 $this->updateArticleStock( $this->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
00651             }
00652 
00653             // seting downloadable products article files
00654             $this->_setOrderFiles();
00655 
00656             // marking object as "non new" disable further stock changes
00657             $this->setIsNewOrderItem( false );
00658         }
00659 
00660         return $blSave;
00661     }
00662 
00668     public function getWrapping()
00669     {
00670         if ($this->oxorderarticles__oxwrapid->value) {
00671             $oWrapping = oxNew('oxwrapping');
00672             if ($oWrapping->load($this->oxorderarticles__oxwrapid->value)) {
00673                 return $oWrapping;
00674             }
00675         }
00676         return null;
00677     }
00678 
00684     public function isBundle()
00685     {
00686         return ( bool ) $this->oxorderarticles__oxisbundle->value;
00687     }
00688 
00694     public function getTotalBrutPriceFormated()
00695     {
00696         $oLang = oxRegistry::getLang();
00697         $oOrder = $this->getOrder();
00698         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
00699         return $oLang->formatCurrency( $this->oxorderarticles__oxbrutprice->value, $oCurrency );
00700     }
00701 
00707     public function getBrutPriceFormated()
00708     {
00709         $oLang = oxRegistry::getLang();
00710         $oOrder = $this->getOrder();
00711         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
00712         return $oLang->formatCurrency(  $this->oxorderarticles__oxbprice->value, $oCurrency );
00713     }
00714 
00720     public function getNetPriceFormated()
00721     {
00722         $oLang = oxRegistry::getLang();
00723         $oOrder = $this->getOrder();
00724         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
00725         return $oLang->formatCurrency(  $this->oxorderarticles__oxnprice->value, $oCurrency );
00726     }
00727 
00733     public function getOrder()
00734     {
00735         if ( $this->oxorderarticles__oxorderid->value ) {
00736             // checking if the object already exists in the cache
00737             if ( isset( $this->_aOrderCache[ $this->oxorderarticles__oxorderid->value ] )) {
00738                 // returning the cached object
00739                 return $this->_aOrderCache[ $this->oxorderarticles__oxorderid->value ];
00740             }
00741             // creatina new order object and trying to load it
00742             $oOrder = oxNew( 'oxOrder' );
00743             if ( $oOrder->load( $this->oxorderarticles__oxorderid->value )) {
00744                 return $this->_aOrderCache[$this->oxorderarticles__oxorderid->value] = $oOrder;
00745             }
00746         }
00747 
00748         return null;
00749     }
00750 
00758     protected function _insert()
00759     {
00760         $iInsertTime = time();
00761         $now = date('Y-m-d H:i:s', $iInsertTime);
00762         $this->oxorderarticles__oxtimestamp = new oxField( $now );
00763 
00764         return parent::_insert();
00765     }
00766 
00767 
00775     public function setArticle( $oArticle )
00776     {
00777         $this->_oArticle = $oArticle;
00778     }
00779 
00785     public function getArticle()
00786     {
00787         if ( $this->_oArticle === null ) {
00788             $oArticle = oxNew( 'oxArticle' );
00789             $oArticle->load($this->oxorderarticles__oxartid->value);
00790             $this->_oArticle = $oArticle;
00791         }
00792 
00793         return $this->_oArticle;
00794     }
00795 
00796 
00797 
00803     public function _setOrderFiles()
00804     {
00805         $oArticle = $this->getArticle();
00806 
00807         if ( $oArticle->oxarticles__oxisdownloadable->value ) {
00808 
00809             $oConfig          = $this->getConfig();
00810             $sOrderId          = $this->oxorderarticles__oxorderid->value;
00811             $sOrderArticleId = $this->getId();
00812             $sShopId          = $oConfig->getShopId();
00813 
00814             $oUser             = $oConfig->getUser();
00815 
00816             $oFiles = $oArticle->getArticleFiles( true );
00817 
00818             if ( $oFiles ) {
00819                 foreach ($oFiles as $oFile) {
00820                     $oOrderFile = oxNew( 'oxOrderFile' );
00821                     $oOrderFile->setOrderId( $sOrderId );
00822                     $oOrderFile->setOrderArticleId( $sOrderArticleId );
00823                     $oOrderFile->setShopId( $sShopId );
00824                     $iMaxDownloadCount = (!empty($oUser) && !$oUser->hasAccount()) ? $oFile->getMaxUnregisteredDownloadsCount() :  $oFile->getMaxDownloadsCount();
00825                     $oOrderFile->setFile(
00826                         $oFile->oxfiles__oxfilename->value,
00827                         $oFile->getId(),
00828                         $iMaxDownloadCount * $this->oxorderarticles__oxamount->value,
00829                         $oFile->getLinkExpirationTime(),
00830                         $oFile->getDownloadExpirationTime()
00831                     );
00832 
00833                     $oOrderFile->save();
00834                 }
00835             }
00836         }
00837     }
00838 
00844     public function getTotalNetPriceFormated()
00845     {
00846         $oLang = oxRegistry::getLang();
00847         $oOrder = $this->getOrder();
00848         $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
00849         return $oLang->formatCurrency( $this->oxorderarticles__oxnetprice->value, $oCurrency );
00850     }
00851 }