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 
00087     public function copyThis($oProduct)
00088     {
00089         $aObjectVars = get_object_vars($oProduct);
00090 
00091         foreach ($aObjectVars as $sName => $sValue) {
00092             if (isset($oProduct->$sName->value)) {
00093                 $sFieldName = preg_replace('/oxarticles__/', 'oxorderarticles__', $sName);
00094                 if ($sFieldName != "oxorderarticles__oxtimestamp") {
00095                     $this->$sFieldName = $oProduct->$sName;
00096                 }
00097                 // formatting view
00098                 if (!$this->getConfig()->getConfigParam('blSkipFormatConversion')) {
00099                     if ($sFieldName == "oxorderarticles__oxinsert") {
00100                         oxRegistry::get("oxUtilsDate")->convertDBDate($this->$sFieldName, true);
00101                     }
00102                 }
00103             }
00104         }
00105 
00106     }
00107 
00113     public function assign($dbRecord)
00114     {
00115         parent::assign($dbRecord);
00116         $this->_setArticleParams();
00117     }
00118 
00127     public function updateArticleStock($dAddAmount, $blAllowNegativeStock = false)
00128     {
00129         // TODO: use oxarticle reduceStock
00130         // decrement stock if there is any
00131         $oArticle = oxNew('oxarticle');
00132         $oArticle->load($this->oxorderarticles__oxartid->value);
00133         $oArticle->beforeUpdate();
00134 
00135         if ($this->getConfig()->getConfigParam('blUseStock')) {
00136             // get real article stock count
00137             $iStockCount = $this->_getArtStock($dAddAmount, $blAllowNegativeStock);
00138             $oDb = oxDb::getDb();
00139 
00140             $oArticle->oxarticles__oxstock = new oxField($iStockCount);
00141             $oDb->execute('update oxarticles set oxarticles.oxstock = ' . $oDb->quote($iStockCount) . ' where oxarticles.oxid = ' . $oDb->quote($this->oxorderarticles__oxartid->value));
00142             $oArticle->onChange(ACTION_UPDATE_STOCK);
00143         }
00144 
00145         //update article sold amount
00146         $oArticle->updateSoldAmount($dAddAmount * (-1));
00147     }
00148 
00157     protected function _getArtStock($dAddAmount = 0, $blAllowNegativeStock = false)
00158     {
00159         $oDb = oxDb::getDb();
00160 
00161         // #1592A. must take real value
00162         $sQ = 'select oxstock from oxarticles where oxid = ' . $oDb->quote($this->oxorderarticles__oxartid->value);
00163         $iStockCount = ( float ) $oDb->getOne($sQ, false, false);
00164 
00165         $iStockCount += $dAddAmount;
00166 
00167         // #1592A. calculating according new stock option
00168         if (!$blAllowNegativeStock && $iStockCount < 0) {
00169             $iStockCount = 0;
00170         }
00171 
00172         return $iStockCount;
00173     }
00174 
00175 
00181     public function getPersParams()
00182     {
00183         if ($this->_aPersParam != null) {
00184             return $this->_aPersParam;
00185         }
00186 
00187         if ($this->oxorderarticles__oxpersparam->value) {
00188             $this->_aPersParam = unserialize($this->oxorderarticles__oxpersparam->value);
00189         }
00190 
00191         return $this->_aPersParam;
00192     }
00193 
00199     public function setPersParams($aParams)
00200     {
00201         $this->_aPersParam = $aParams;
00202 
00203         // serializing persisten info stored while ordering
00204         $this->oxorderarticles__oxpersparam = new oxField(serialize($aParams), oxField::T_RAW);
00205     }
00206 
00216     protected function _setFieldData($sFieldName, $sValue, $iDataType = oxField::T_TEXT)
00217     {
00218         $sFieldName = strtolower($sFieldName);
00219         switch ($sFieldName) {
00220             case 'oxpersparam':
00221             case 'oxorderarticles__oxpersparam':
00222             case 'oxerpstatus':
00223             case 'oxorderarticles__oxerpstatus':
00224             case 'oxtitle':
00225             case 'oxorderarticles__oxtitle':
00226                 $iDataType = oxField::T_RAW;
00227                 break;
00228         }
00229 
00230         return parent::_setFieldData($sFieldName, $sValue, $iDataType);
00231     }
00232 
00241     public function loadInLang($iLanguage, $sOxid)
00242     {
00243         return $this->load($sOxid);
00244     }
00245 
00251     public function getProductId()
00252     {
00253         return $this->oxorderarticles__oxartid->value;
00254     }
00255 
00263     public function getProductParentId()
00264     {
00265         return $this->getParentId();
00266     }
00267 
00273     public function getParentId()
00274     {
00275         // when this field will be introduced there will be no need to load from real article
00276         if (isset($this->oxorderarticles__oxartparentid) && $this->oxorderarticles__oxartparentid->value !== false) {
00277             return $this->oxorderarticles__oxartparentid->value;
00278         }
00279 
00280         $oDb = oxDb::getDb();
00281         $oArticle = oxNew("oxarticle");
00282         $sQ = "select oxparentid from " . $oArticle->getViewName() . " where oxid=" . $oDb->quote($this->getProductId());
00283         $this->oxarticles__oxparentid = new oxField($oDb->getOne($sQ));
00284 
00285         return $this->oxarticles__oxparentid->value;
00286     }
00287 
00291     protected function _setArticleParams()
00292     {
00293         // creating needed fields
00294         $this->oxarticles__oxstock = $this->oxorderarticles__oxamount;
00295         $this->oxarticles__oxtitle = $this->oxorderarticles__oxtitle;
00296         $this->oxarticles__oxwidth = $this->oxorderarticles__oxwidth;
00297         $this->oxarticles__oxlength = $this->oxorderarticles__oxlength;
00298         $this->oxarticles__oxheight = $this->oxorderarticles__oxheight;
00299         $this->oxarticles__oxweight = $this->oxorderarticles__oxweight;
00300         $this->oxarticles__oxsubclass = $this->oxorderarticles__oxsubclass;
00301         $this->oxarticles__oxartnum = $this->oxorderarticles__oxartnum;
00302         $this->oxarticles__oxshortdesc = $this->oxorderarticles__oxshortdesc;
00303 
00304         $this->oxarticles__oxvat = $this->oxorderarticles__oxvat;
00305         $this->oxarticles__oxprice = $this->oxorderarticles__oxprice;
00306         $this->oxarticles__oxbprice = $this->oxorderarticles__oxbprice;
00307 
00308         $this->oxarticles__oxthumb = $this->oxorderarticles__oxthumb;
00309         $this->oxarticles__oxpic1 = $this->oxorderarticles__oxpic1;
00310         $this->oxarticles__oxpic2 = $this->oxorderarticles__oxpic2;
00311         $this->oxarticles__oxpic3 = $this->oxorderarticles__oxpic3;
00312         $this->oxarticles__oxpic4 = $this->oxorderarticles__oxpic4;
00313         $this->oxarticles__oxpic5 = $this->oxorderarticles__oxpic5;
00314 
00315         $this->oxarticles__oxfile = $this->oxorderarticles__oxfile;
00316         $this->oxarticles__oxdelivery = $this->oxorderarticles__oxdelivery;
00317         $this->oxarticles__oxissearch = $this->oxorderarticles__oxissearch;
00318         $this->oxarticles__oxfolder = $this->oxorderarticles__oxfolder;
00319         $this->oxarticles__oxtemplate = $this->oxorderarticles__oxtemplate;
00320         $this->oxarticles__oxexturl = $this->oxorderarticles__oxexturl;
00321         $this->oxarticles__oxurlimg = $this->oxorderarticles__oxurlimg;
00322         $this->oxarticles__oxurldesc = $this->oxorderarticles__oxurldesc;
00323         $this->oxarticles__oxshopid = $this->oxorderarticles__oxordershopid;
00324         $this->oxarticles__oxquestionemail = $this->oxorderarticles__oxquestionemail;
00325         $this->oxarticles__oxsearchkeys = $this->oxorderarticles__oxsearchkeys;
00326     }
00327 
00336     public function checkForStock($dAmount, $dArtStockAmount = 0)
00337     {
00338         return true;
00339     }
00340 
00349     protected function _getOrderArticle($sArticleId = null)
00350     {
00351         if ($this->_oOrderArticle === null) {
00352             $this->_oOrderArticle = false;
00353 
00354             $sArticleId = $sArticleId ? $sArticleId : $this->getProductId();
00355             $oArticle = oxNew("oxArticle");
00356             if ($oArticle->load($sArticleId)) {
00357                 $this->_oOrderArticle = $oArticle;
00358             }
00359         }
00360 
00361         return $this->_oOrderArticle;
00362     }
00363 
00371     public function getSelectLists($sKeyPrefix = null)
00372     {
00373         $aSelLists = array();
00374         if ($oArticle = $this->_getOrderArticle()) {
00375             $aSelLists = $oArticle->getSelectLists();
00376         }
00377 
00378         return $aSelLists;
00379     }
00380 
00389     public function getOrderArticleSelectList($sArtId = null, $sOrderArtSelList = null)
00390     {
00391         if ($this->_aOrderArticleSelList === null) {
00392 
00393             $sOrderArtSelList = $sOrderArtSelList ? $sOrderArtSelList : $this->oxorderarticles__oxselvariant->value;
00394 
00395             $aRet = array();
00396 
00397             if ($oArticle = $this->_getOrderArticle($sArtId)) {
00398                 $aList = explode(", ", $sOrderArtSelList);
00399                 $oStr = getStr();
00400 
00401                 $aArticleSelList = $oArticle->getSelectLists();
00402 
00403                 //formatting temporary list array from string
00404                 foreach ($aList as $sList) {
00405                     if ($sList) {
00406 
00407                         $aVal = explode(":", $sList);
00408                         if (isset($aVal[0]) && isset($aVal[1])) {
00409                             $sOrderArtListTitle = $oStr->strtolower(trim($aVal[0]));
00410                             $sOrderArtSelValue = $oStr->strtolower(trim($aVal[1]));
00411 
00412                             //checking article list for matches with article list stored in oxOrderItem
00413                             $iSelListNum = 0;
00414                             if (count($aArticleSelList) > 0) {
00415                                 foreach ($aArticleSelList as $aSelect) {
00416                                     //check if selects titles are equal
00417 
00418                                     if ($oStr->strtolower($aSelect['name']) == $sOrderArtListTitle) {
00419                                         //try to find matching select items value
00420                                         $iSelValueNum = 0;
00421                                         foreach ($aSelect as $oSel) {
00422                                             if ($oStr->strtolower($oSel->name) == $sOrderArtSelValue) {
00423                                                 // found, adding to return array
00424                                                 $aRet[$iSelListNum] = $iSelValueNum;
00425                                             }
00426                                             //next article list item
00427                                             $iSelValueNum++;
00428                                         }
00429                                     }
00430                                     //next article list
00431                                     $iSelListNum++;
00432                                 }
00433                             }
00434                         }
00435                     }
00436                 }
00437             }
00438 
00439             $this->_aOrderArticleSelList = $aRet;
00440         }
00441 
00442         return $this->_aOrderArticleSelList;
00443     }
00444 
00454     public function getBasketPrice($dAmount, $aSelList, $oBasket)
00455     {
00456         $oArticle = $this->_getOrderArticle();
00457         if ($oArticle) {
00458             return $oArticle->getBasketPrice($dAmount, $aSelList, $oBasket);
00459         } else {
00460             return $this->getPrice();
00461         }
00462     }
00463 
00469     public function skipDiscounts()
00470     {
00471         return false;
00472     }
00473 
00482     public function getCategoryIds($blActCats = false, $blSkipCache = false)
00483     {
00484         $aCatIds = array();
00485         if ($oOrderArticle = $this->_getOrderArticle()) {
00486             $aCatIds = $oOrderArticle->getCategoryIds($blActCats, $blSkipCache);
00487         }
00488 
00489         return $aCatIds;
00490     }
00491 
00497     public function getLanguage()
00498     {
00499         return oxRegistry::getLang()->getBaseLanguage();
00500     }
00501 
00509     public function getBasePrice($dAmount = 1)
00510     {
00511 
00512         return $this->getPrice();
00513     }
00514 
00520     public function getPrice()
00521     {
00522         $oBasePrice = oxNew('oxPrice');
00523         // prices in db are ONLY brutto
00524         $oBasePrice->setBruttoPriceMode();
00525         $oBasePrice->setVat($this->oxorderarticles__oxvat->value);
00526         $oBasePrice->setPrice($this->oxorderarticles__oxbprice->value);
00527 
00528         return $oBasePrice;
00529     }
00530 
00536     public function setIsNewOrderItem($blIsNew)
00537     {
00538         $this->_blIsNewOrderItem = $blIsNew;
00539     }
00540 
00546     public function isNewOrderItem()
00547     {
00548         return $this->_blIsNewOrderItem;
00549     }
00550 
00558     public function setNewAmount($iNewAmount)
00559     {
00560         if ($iNewAmount >= 0) {
00561             // to update stock we must first check if it is possible - article exists?
00562             $oArticle = oxNew("oxarticle");
00563             if ($oArticle->load($this->oxorderarticles__oxartid->value)) {
00564 
00565                 // updating stock info
00566                 $iStockChange = $iNewAmount - $this->oxorderarticles__oxamount->value;
00567                 if ($iStockChange > 0 && ($iOnStock = $oArticle->checkForStock($iStockChange)) !== false) {
00568                     if ($iOnStock !== true) {
00569                         $iStockChange = $iOnStock;
00570                         $iNewAmount = $this->oxorderarticles__oxamount->value + $iStockChange;
00571                     }
00572                 }
00573 
00574                 $this->updateArticleStock($iStockChange * -1, $this->getConfig()->getConfigParam('blAllowNegativeStock'));
00575 
00576                 // updating self
00577                 $this->oxorderarticles__oxamount = new oxField($iNewAmount, oxField::T_RAW);
00578                 $this->save();
00579             }
00580         }
00581     }
00582 
00588     public function isOrderArticle()
00589     {
00590         return true;
00591     }
00592 
00593 
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 
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             ) {
00644                 $this->getSession()
00645                     ->getBasketReservations()
00646                     ->commitArticleReservation(
00647                         $this->oxorderarticles__oxartid->value,
00648                         $this->oxorderarticles__oxamount->value
00649                     );
00650             } else {
00651                 $this->updateArticleStock($this->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam('blAllowNegativeStock'));
00652             }
00653 
00654             // seting downloadable products article files
00655             $this->_setOrderFiles();
00656 
00657             // marking object as "non new" disable further stock changes
00658             $this->setIsNewOrderItem(false);
00659         }
00660 
00661         return $blSave;
00662     }
00663 
00669     public function getWrapping()
00670     {
00671         if ($this->oxorderarticles__oxwrapid->value) {
00672             $oWrapping = oxNew('oxwrapping');
00673             if ($oWrapping->load($this->oxorderarticles__oxwrapid->value)) {
00674                 return $oWrapping;
00675             }
00676         }
00677 
00678         return null;
00679     }
00680 
00686     public function isBundle()
00687     {
00688         return ( bool ) $this->oxorderarticles__oxisbundle->value;
00689     }
00690 
00696     public function getTotalBrutPriceFormated()
00697     {
00698         $oLang = oxRegistry::getLang();
00699         $oOrder = $this->getOrder();
00700         $oCurrency = $this->getConfig()->getCurrencyObject($oOrder->oxorder__oxcurrency->value);
00701 
00702         return $oLang->formatCurrency($this->oxorderarticles__oxbrutprice->value, $oCurrency);
00703     }
00704 
00710     public function getBrutPriceFormated()
00711     {
00712         $oLang = oxRegistry::getLang();
00713         $oOrder = $this->getOrder();
00714         $oCurrency = $this->getConfig()->getCurrencyObject($oOrder->oxorder__oxcurrency->value);
00715 
00716         return $oLang->formatCurrency($this->oxorderarticles__oxbprice->value, $oCurrency);
00717     }
00718 
00724     public function getNetPriceFormated()
00725     {
00726         $oLang = oxRegistry::getLang();
00727         $oOrder = $this->getOrder();
00728         $oCurrency = $this->getConfig()->getCurrencyObject($oOrder->oxorder__oxcurrency->value);
00729 
00730         return $oLang->formatCurrency($this->oxorderarticles__oxnprice->value, $oCurrency);
00731     }
00732 
00738     public function getOrder()
00739     {
00740         if ($this->oxorderarticles__oxorderid->value) {
00741             // checking if the object already exists in the cache
00742             if (isset($this->_aOrderCache[$this->oxorderarticles__oxorderid->value])) {
00743                 // returning the cached object
00744                 return $this->_aOrderCache[$this->oxorderarticles__oxorderid->value];
00745             }
00746             // creatina new order object and trying to load it
00747             $oOrder = oxNew('oxOrder');
00748             if ($oOrder->load($this->oxorderarticles__oxorderid->value)) {
00749                 return $this->_aOrderCache[$this->oxorderarticles__oxorderid->value] = $oOrder;
00750             }
00751         }
00752 
00753         return null;
00754     }
00755 
00763     protected function _insert()
00764     {
00765         $iInsertTime = time();
00766         $now = date('Y-m-d H:i:s', $iInsertTime);
00767         $this->oxorderarticles__oxtimestamp = new oxField($now);
00768 
00769         return parent::_insert();
00770     }
00771 
00772 
00778     public function setArticle($oArticle)
00779     {
00780         $this->_oArticle = $oArticle;
00781     }
00782 
00788     public function getArticle()
00789     {
00790         if ($this->_oArticle === null) {
00791             $oArticle = oxNew('oxArticle');
00792             $oArticle->load($this->oxorderarticles__oxartid->value);
00793             $this->_oArticle = $oArticle;
00794         }
00795 
00796         return $this->_oArticle;
00797     }
00798 
00799 
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 
00850         return $oLang->formatCurrency($this->oxorderarticles__oxnetprice->value, $oCurrency);
00851     }
00852 }