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