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             $oArticle->setLoadParentData(true);
00357             if ($oArticle->load($sArticleId)) {
00358                 $this->_oOrderArticle = $oArticle;
00359             }
00360         }
00361 
00362         return $this->_oOrderArticle;
00363     }
00364 
00372     public function getSelectLists($sKeyPrefix = null)
00373     {
00374         $aSelLists = array();
00375         if ($oArticle = $this->_getOrderArticle()) {
00376             $aSelLists = $oArticle->getSelectLists();
00377         }
00378 
00379         return $aSelLists;
00380     }
00381 
00390     public function getOrderArticleSelectList($sArtId = null, $sOrderArtSelList = null)
00391     {
00392         if ($this->_aOrderArticleSelList === null) {
00393 
00394             $sOrderArtSelList = $sOrderArtSelList ? $sOrderArtSelList : $this->oxorderarticles__oxselvariant->value;
00395 
00396             $aRet = array();
00397 
00398             if ($oArticle = $this->_getOrderArticle($sArtId)) {
00399                 $aList = explode(", ", $sOrderArtSelList);
00400                 $oStr = getStr();
00401 
00402                 $aArticleSelList = $oArticle->getSelectLists();
00403 
00404                 //formatting temporary list array from string
00405                 foreach ($aList as $sList) {
00406                     if ($sList) {
00407 
00408                         $aVal = explode(":", $sList);
00409                         if (isset($aVal[0]) && isset($aVal[1])) {
00410                             $sOrderArtListTitle = $oStr->strtolower(trim($aVal[0]));
00411                             $sOrderArtSelValue = $oStr->strtolower(trim($aVal[1]));
00412 
00413                             //checking article list for matches with article list stored in oxOrderItem
00414                             $iSelListNum = 0;
00415                             if (count($aArticleSelList) > 0) {
00416                                 foreach ($aArticleSelList as $aSelect) {
00417                                     //check if selects titles are equal
00418 
00419                                     if ($oStr->strtolower($aSelect['name']) == $sOrderArtListTitle) {
00420                                         //try to find matching select items value
00421                                         $iSelValueNum = 0;
00422                                         foreach ($aSelect as $oSel) {
00423                                             if ($oStr->strtolower($oSel->name) == $sOrderArtSelValue) {
00424                                                 // found, adding to return array
00425                                                 $aRet[$iSelListNum] = $iSelValueNum;
00426                                             }
00427                                             //next article list item
00428                                             $iSelValueNum++;
00429                                         }
00430                                     }
00431                                     //next article list
00432                                     $iSelListNum++;
00433                                 }
00434                             }
00435                         }
00436                     }
00437                 }
00438             }
00439 
00440             $this->_aOrderArticleSelList = $aRet;
00441         }
00442 
00443         return $this->_aOrderArticleSelList;
00444     }
00445 
00455     public function getBasketPrice($dAmount, $aSelList, $oBasket)
00456     {
00457         $oArticle = $this->_getOrderArticle();
00458 
00459         if ($oArticle) {
00460             return $oArticle->getBasketPrice($dAmount, $aSelList, $oBasket);
00461         } else {
00462             return $this->getPrice();
00463         }
00464     }
00465 
00471     public function skipDiscounts()
00472     {
00473         return false;
00474     }
00475 
00484     public function getCategoryIds($blActCats = false, $blSkipCache = false)
00485     {
00486         $aCatIds = array();
00487         if ($oOrderArticle = $this->_getOrderArticle()) {
00488             $aCatIds = $oOrderArticle->getCategoryIds($blActCats, $blSkipCache);
00489         }
00490 
00491         return $aCatIds;
00492     }
00493 
00499     public function getLanguage()
00500     {
00501         return oxRegistry::getLang()->getBaseLanguage();
00502     }
00503 
00511     public function getBasePrice($dAmount = 1)
00512     {
00513 
00514         return $this->getPrice();
00515     }
00516 
00522     public function getPrice()
00523     {
00524         $oBasePrice = oxNew('oxPrice');
00525         // prices in db are ONLY brutto
00526         $oBasePrice->setBruttoPriceMode();
00527         $oBasePrice->setVat($this->oxorderarticles__oxvat->value);
00528         $oBasePrice->setPrice($this->oxorderarticles__oxbprice->value);
00529 
00530         return $oBasePrice;
00531     }
00532 
00538     public function setIsNewOrderItem($blIsNew)
00539     {
00540         $this->_blIsNewOrderItem = $blIsNew;
00541     }
00542 
00548     public function isNewOrderItem()
00549     {
00550         return $this->_blIsNewOrderItem;
00551     }
00552 
00560     public function setNewAmount($iNewAmount)
00561     {
00562         if ($iNewAmount >= 0) {
00563             // to update stock we must first check if it is possible - article exists?
00564             $oArticle = oxNew("oxarticle");
00565             if ($oArticle->load($this->oxorderarticles__oxartid->value)) {
00566 
00567                 // updating stock info
00568                 $iStockChange = $iNewAmount - $this->oxorderarticles__oxamount->value;
00569                 if ($iStockChange > 0 && ($iOnStock = $oArticle->checkForStock($iStockChange)) !== false) {
00570                     if ($iOnStock !== true) {
00571                         $iStockChange = $iOnStock;
00572                         $iNewAmount = $this->oxorderarticles__oxamount->value + $iStockChange;
00573                     }
00574                 }
00575 
00576                 $this->updateArticleStock($iStockChange * -1, $this->getConfig()->getConfigParam('blAllowNegativeStock'));
00577 
00578                 // updating self
00579                 $this->oxorderarticles__oxamount = new oxField($iNewAmount, oxField::T_RAW);
00580                 $this->save();
00581             }
00582         }
00583     }
00584 
00590     public function isOrderArticle()
00591     {
00592         return true;
00593     }
00594 
00595 
00600     public function cancelOrderArticle()
00601     {
00602         if ($this->oxorderarticles__oxstorno->value == 0) {
00603             $myConfig = $this->getConfig();
00604             $this->oxorderarticles__oxstorno = new oxField(1);
00605             if ($this->save()) {
00606                 $this->updateArticleStock($this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock'));
00607             }
00608         }
00609     }
00610 
00619     public function delete($sOXID = null)
00620     {
00621         if ($blDelete = parent::delete($sOXID)) {
00622             $myConfig = $this->getConfig();
00623             if ($this->oxorderarticles__oxstorno->value != 1) {
00624                 $this->updateArticleStock($this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock'));
00625             }
00626         }
00627 
00628         return $blDelete;
00629     }
00630 
00638     public function save()
00639     {
00640         // ordered articles
00641         if (($blSave = parent::save()) && $this->isNewOrderItem()) {
00642             $myConfig = $this->getConfig();
00643             if ($myConfig->getConfigParam('blUseStock') &&
00644                 $myConfig->getConfigParam('blPsBasketReservationEnabled')
00645             ) {
00646                 $this->getSession()
00647                     ->getBasketReservations()
00648                     ->commitArticleReservation(
00649                         $this->oxorderarticles__oxartid->value,
00650                         $this->oxorderarticles__oxamount->value
00651                     );
00652             } else {
00653                 $this->updateArticleStock($this->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam('blAllowNegativeStock'));
00654             }
00655 
00656             // seting downloadable products article files
00657             $this->_setOrderFiles();
00658 
00659             // marking object as "non new" disable further stock changes
00660             $this->setIsNewOrderItem(false);
00661         }
00662 
00663         return $blSave;
00664     }
00665 
00671     public function getWrapping()
00672     {
00673         if ($this->oxorderarticles__oxwrapid->value) {
00674             $oWrapping = oxNew('oxwrapping');
00675             if ($oWrapping->load($this->oxorderarticles__oxwrapid->value)) {
00676                 return $oWrapping;
00677             }
00678         }
00679 
00680         return null;
00681     }
00682 
00688     public function isBundle()
00689     {
00690         return ( bool ) $this->oxorderarticles__oxisbundle->value;
00691     }
00692 
00698     public function getTotalBrutPriceFormated()
00699     {
00700         $oLang = oxRegistry::getLang();
00701         $oOrder = $this->getOrder();
00702         $oCurrency = $this->getConfig()->getCurrencyObject($oOrder->oxorder__oxcurrency->value);
00703 
00704         return $oLang->formatCurrency($this->oxorderarticles__oxbrutprice->value, $oCurrency);
00705     }
00706 
00712     public function getBrutPriceFormated()
00713     {
00714         $oLang = oxRegistry::getLang();
00715         $oOrder = $this->getOrder();
00716         $oCurrency = $this->getConfig()->getCurrencyObject($oOrder->oxorder__oxcurrency->value);
00717 
00718         return $oLang->formatCurrency($this->oxorderarticles__oxbprice->value, $oCurrency);
00719     }
00720 
00726     public function getNetPriceFormated()
00727     {
00728         $oLang = oxRegistry::getLang();
00729         $oOrder = $this->getOrder();
00730         $oCurrency = $this->getConfig()->getCurrencyObject($oOrder->oxorder__oxcurrency->value);
00731 
00732         return $oLang->formatCurrency($this->oxorderarticles__oxnprice->value, $oCurrency);
00733     }
00734 
00740     public function getOrder()
00741     {
00742         if ($this->oxorderarticles__oxorderid->value) {
00743             // checking if the object already exists in the cache
00744             if (isset($this->_aOrderCache[$this->oxorderarticles__oxorderid->value])) {
00745                 // returning the cached object
00746                 return $this->_aOrderCache[$this->oxorderarticles__oxorderid->value];
00747             }
00748             // creatina new order object and trying to load it
00749             $oOrder = oxNew('oxOrder');
00750             if ($oOrder->load($this->oxorderarticles__oxorderid->value)) {
00751                 return $this->_aOrderCache[$this->oxorderarticles__oxorderid->value] = $oOrder;
00752             }
00753         }
00754 
00755         return null;
00756     }
00757 
00765     protected function _insert()
00766     {
00767         $iInsertTime = time();
00768         $now = date('Y-m-d H:i:s', $iInsertTime);
00769         $this->oxorderarticles__oxtimestamp = new oxField($now);
00770 
00771         return parent::_insert();
00772     }
00773 
00774 
00780     public function setArticle($oArticle)
00781     {
00782         $this->_oArticle = $oArticle;
00783     }
00784 
00790     public function getArticle()
00791     {
00792         if ($this->_oArticle === null) {
00793             $oArticle = oxNew('oxArticle');
00794             $oArticle->load($this->oxorderarticles__oxartid->value);
00795             $this->_oArticle = $oArticle;
00796         }
00797 
00798         return $this->_oArticle;
00799     }
00800 
00801 
00805     public function _setOrderFiles()
00806     {
00807         $oArticle = $this->getArticle();
00808 
00809         if ($oArticle->oxarticles__oxisdownloadable->value) {
00810 
00811             $oConfig = $this->getConfig();
00812             $sOrderId = $this->oxorderarticles__oxorderid->value;
00813             $sOrderArticleId = $this->getId();
00814             $sShopId = $oConfig->getShopId();
00815 
00816             $oUser = $oConfig->getUser();
00817 
00818             $oFiles = $oArticle->getArticleFiles(true);
00819 
00820             if ($oFiles) {
00821                 foreach ($oFiles as $oFile) {
00822                     $oOrderFile = oxNew('oxOrderFile');
00823                     $oOrderFile->setOrderId($sOrderId);
00824                     $oOrderFile->setOrderArticleId($sOrderArticleId);
00825                     $oOrderFile->setShopId($sShopId);
00826                     $iMaxDownloadCount = (!empty($oUser) && !$oUser->hasAccount()) ? $oFile->getMaxUnregisteredDownloadsCount() : $oFile->getMaxDownloadsCount();
00827                     $oOrderFile->setFile(
00828                         $oFile->oxfiles__oxfilename->value,
00829                         $oFile->getId(),
00830                         $iMaxDownloadCount * $this->oxorderarticles__oxamount->value,
00831                         $oFile->getLinkExpirationTime(),
00832                         $oFile->getDownloadExpirationTime()
00833                     );
00834 
00835                     $oOrderFile->save();
00836                 }
00837             }
00838         }
00839     }
00840 
00846     public function getTotalNetPriceFormated()
00847     {
00848         $oLang = oxRegistry::getLang();
00849         $oOrder = $this->getOrder();
00850         $oCurrency = $this->getConfig()->getCurrencyObject($oOrder->oxorder__oxcurrency->value);
00851 
00852         return $oLang->formatCurrency($this->oxorderarticles__oxnetprice->value, $oCurrency);
00853     }
00854 }