OXID eShop CE  4.9.7
 All Classes Files Functions Variables Pages
oxorderarticle.php
Go to the documentation of this file.
1 <?php
2 
8 class oxOrderArticle extends oxBase implements oxIArticle
9 {
10 
14  protected static $_aOrderCache = array();
15 
21  protected $_sClassName = 'oxorderarticle';
22 
28  protected $_aPersParam = null;
29 
35  protected $_aStatuses = null;
36 
42  protected $_aOrderArticleSelList = null;
43 
49  protected $_oOrderArticle = null;
50 
56  protected $_oArticle = null;
57 
63  protected $_blIsNewOrderItem = false;
64 
71  protected $_aSkipSaveFields = array('oxtimestamp');
72 
76  public function __construct()
77  {
79  $this->init('oxorderarticles');
80  }
81 
87  public function copyThis($oProduct)
88  {
89  $aObjectVars = get_object_vars($oProduct);
90 
91  foreach ($aObjectVars as $sName => $sValue) {
92  if (isset($oProduct->$sName->value)) {
93  $sFieldName = preg_replace('/oxarticles__/', 'oxorderarticles__', $sName);
94  if ($sFieldName != "oxorderarticles__oxtimestamp") {
95  $this->$sFieldName = $oProduct->$sName;
96  }
97  // formatting view
98  if (!$this->getConfig()->getConfigParam('blSkipFormatConversion')) {
99  if ($sFieldName == "oxorderarticles__oxinsert") {
100  oxRegistry::get("oxUtilsDate")->convertDBDate($this->$sFieldName, true);
101  }
102  }
103  }
104  }
105 
106  }
107 
113  public function assign($dbRecord)
114  {
115  parent::assign($dbRecord);
116  $this->_setArticleParams();
117  }
118 
127  public function updateArticleStock($dAddAmount, $blAllowNegativeStock = false)
128  {
129  // TODO: use oxarticle reduceStock
130  // decrement stock if there is any
131  $oArticle = oxNew('oxarticle');
132  $oArticle->load($this->oxorderarticles__oxartid->value);
133  $oArticle->beforeUpdate();
134 
135  if ($this->getConfig()->getConfigParam('blUseStock')) {
136  // get real article stock count
137  $iStockCount = $this->_getArtStock($dAddAmount, $blAllowNegativeStock);
138  $oDb = oxDb::getDb();
139 
140  $oArticle->oxarticles__oxstock = new oxField($iStockCount);
141  $oDb->execute('update oxarticles set oxarticles.oxstock = ' . $oDb->quote($iStockCount) . ' where oxarticles.oxid = ' . $oDb->quote($this->oxorderarticles__oxartid->value));
142  $oArticle->onChange(ACTION_UPDATE_STOCK);
143  }
144 
145  //update article sold amount
146  $oArticle->updateSoldAmount($dAddAmount * (-1));
147  }
148 
157  protected function _getArtStock($dAddAmount = 0, $blAllowNegativeStock = false)
158  {
159  $oDb = oxDb::getDb();
160 
161  // #1592A. must take real value
162  $sQ = 'select oxstock from oxarticles where oxid = ' . $oDb->quote($this->oxorderarticles__oxartid->value);
163  $iStockCount = ( float ) $oDb->getOne($sQ, false, false);
164 
165  $iStockCount += $dAddAmount;
166 
167  // #1592A. calculating according new stock option
168  if (!$blAllowNegativeStock && $iStockCount < 0) {
169  $iStockCount = 0;
170  }
171 
172  return $iStockCount;
173  }
174 
175 
181  public function getPersParams()
182  {
183  if ($this->_aPersParam != null) {
184  return $this->_aPersParam;
185  }
186 
187  if ($this->oxorderarticles__oxpersparam->value) {
188  $this->_aPersParam = unserialize($this->oxorderarticles__oxpersparam->value);
189  }
190 
191  return $this->_aPersParam;
192  }
193 
199  public function setPersParams($aParams)
200  {
201  $this->_aPersParam = $aParams;
202 
203  // serializing persisten info stored while ordering
204  $this->oxorderarticles__oxpersparam = new oxField(serialize($aParams), oxField::T_RAW);
205  }
206 
216  protected function _setFieldData($sFieldName, $sValue, $iDataType = oxField::T_TEXT)
217  {
218  $sFieldName = strtolower($sFieldName);
219  switch ($sFieldName) {
220  case 'oxpersparam':
221  case 'oxorderarticles__oxpersparam':
222  case 'oxerpstatus':
223  case 'oxorderarticles__oxerpstatus':
224  case 'oxtitle':
225  case 'oxorderarticles__oxtitle':
226  $iDataType = oxField::T_RAW;
227  break;
228  }
229 
230  return parent::_setFieldData($sFieldName, $sValue, $iDataType);
231  }
232 
241  public function loadInLang($iLanguage, $sOxid)
242  {
243  return $this->load($sOxid);
244  }
245 
251  public function getProductId()
252  {
253  return $this->oxorderarticles__oxartid->value;
254  }
255 
263  public function getProductParentId()
264  {
265  return $this->getParentId();
266  }
267 
273  public function getParentId()
274  {
275  // when this field will be introduced there will be no need to load from real article
276  if (isset($this->oxorderarticles__oxartparentid) && $this->oxorderarticles__oxartparentid->value !== false) {
277  return $this->oxorderarticles__oxartparentid->value;
278  }
279 
280  $oDb = oxDb::getDb();
281  $oArticle = oxNew("oxarticle");
282  $sQ = "select oxparentid from " . $oArticle->getViewName() . " where oxid=" . $oDb->quote($this->getProductId());
283  $this->oxarticles__oxparentid = new oxField($oDb->getOne($sQ));
284 
285  return $this->oxarticles__oxparentid->value;
286  }
287 
291  protected function _setArticleParams()
292  {
293  // creating needed fields
294  $this->oxarticles__oxstock = $this->oxorderarticles__oxamount;
295  $this->oxarticles__oxtitle = $this->oxorderarticles__oxtitle;
296  $this->oxarticles__oxwidth = $this->oxorderarticles__oxwidth;
297  $this->oxarticles__oxlength = $this->oxorderarticles__oxlength;
298  $this->oxarticles__oxheight = $this->oxorderarticles__oxheight;
299  $this->oxarticles__oxweight = $this->oxorderarticles__oxweight;
300  $this->oxarticles__oxsubclass = $this->oxorderarticles__oxsubclass;
301  $this->oxarticles__oxartnum = $this->oxorderarticles__oxartnum;
302  $this->oxarticles__oxshortdesc = $this->oxorderarticles__oxshortdesc;
303 
304  $this->oxarticles__oxvat = $this->oxorderarticles__oxvat;
305  $this->oxarticles__oxprice = $this->oxorderarticles__oxprice;
306  $this->oxarticles__oxbprice = $this->oxorderarticles__oxbprice;
307 
308  $this->oxarticles__oxthumb = $this->oxorderarticles__oxthumb;
309  $this->oxarticles__oxpic1 = $this->oxorderarticles__oxpic1;
310  $this->oxarticles__oxpic2 = $this->oxorderarticles__oxpic2;
311  $this->oxarticles__oxpic3 = $this->oxorderarticles__oxpic3;
312  $this->oxarticles__oxpic4 = $this->oxorderarticles__oxpic4;
313  $this->oxarticles__oxpic5 = $this->oxorderarticles__oxpic5;
314 
315  $this->oxarticles__oxfile = $this->oxorderarticles__oxfile;
316  $this->oxarticles__oxdelivery = $this->oxorderarticles__oxdelivery;
317  $this->oxarticles__oxissearch = $this->oxorderarticles__oxissearch;
318  $this->oxarticles__oxfolder = $this->oxorderarticles__oxfolder;
319  $this->oxarticles__oxtemplate = $this->oxorderarticles__oxtemplate;
320  $this->oxarticles__oxexturl = $this->oxorderarticles__oxexturl;
321  $this->oxarticles__oxurlimg = $this->oxorderarticles__oxurlimg;
322  $this->oxarticles__oxurldesc = $this->oxorderarticles__oxurldesc;
323  $this->oxarticles__oxshopid = $this->oxorderarticles__oxordershopid;
324  $this->oxarticles__oxquestionemail = $this->oxorderarticles__oxquestionemail;
325  $this->oxarticles__oxsearchkeys = $this->oxorderarticles__oxsearchkeys;
326  }
327 
336  public function checkForStock($dAmount, $dArtStockAmount = 0)
337  {
338  return true;
339  }
340 
349  protected function _getOrderArticle($sArticleId = null)
350  {
351  if ($this->_oOrderArticle === null) {
352  $this->_oOrderArticle = false;
353 
354  $sArticleId = $sArticleId ? $sArticleId : $this->getProductId();
355  $oArticle = oxNew("oxArticle");
356  $oArticle->setLoadParentData(true);
357  if ($oArticle->load($sArticleId)) {
358  $this->_oOrderArticle = $oArticle;
359  }
360  }
361 
362  return $this->_oOrderArticle;
363  }
364 
372  public function getSelectLists($sKeyPrefix = null)
373  {
374  $aSelLists = array();
375  if ($oArticle = $this->_getOrderArticle()) {
376  $aSelLists = $oArticle->getSelectLists();
377  }
378 
379  return $aSelLists;
380  }
381 
390  public function getOrderArticleSelectList($sArtId = null, $sOrderArtSelList = null)
391  {
392  if ($this->_aOrderArticleSelList === null) {
393 
394  $sOrderArtSelList = $sOrderArtSelList ? $sOrderArtSelList : $this->oxorderarticles__oxselvariant->value;
395 
396  $aRet = array();
397 
398  if ($oArticle = $this->_getOrderArticle($sArtId)) {
399  $aList = explode(", ", $sOrderArtSelList);
400  $oStr = getStr();
401 
402  $aArticleSelList = $oArticle->getSelectLists();
403 
404  //formatting temporary list array from string
405  foreach ($aList as $sList) {
406  if ($sList) {
407 
408  $aVal = explode(":", $sList);
409  if (isset($aVal[0]) && isset($aVal[1])) {
410  $sOrderArtListTitle = $oStr->strtolower(trim($aVal[0]));
411  $sOrderArtSelValue = $oStr->strtolower(trim($aVal[1]));
412 
413  //checking article list for matches with article list stored in oxOrderItem
414  $iSelListNum = 0;
415  if (count($aArticleSelList) > 0) {
416  foreach ($aArticleSelList as $aSelect) {
417  //check if selects titles are equal
418 
419  if ($oStr->strtolower($aSelect['name']) == $sOrderArtListTitle) {
420  //try to find matching select items value
421  $iSelValueNum = 0;
422  foreach ($aSelect as $oSel) {
423  if ($oStr->strtolower($oSel->name) == $sOrderArtSelValue) {
424  // found, adding to return array
425  $aRet[$iSelListNum] = $iSelValueNum;
426  }
427  //next article list item
428  $iSelValueNum++;
429  }
430  }
431  //next article list
432  $iSelListNum++;
433  }
434  }
435  }
436  }
437  }
438  }
439 
440  $this->_aOrderArticleSelList = $aRet;
441  }
442 
444  }
445 
455  public function getBasketPrice($dAmount, $aSelList, $oBasket)
456  {
457  $oArticle = $this->_getOrderArticle();
458 
459  if ($oArticle) {
460  return $oArticle->getBasketPrice($dAmount, $aSelList, $oBasket);
461  } else {
462  return $this->getPrice();
463  }
464  }
465 
471  public function skipDiscounts()
472  {
473  return false;
474  }
475 
484  public function getCategoryIds($blActCats = false, $blSkipCache = false)
485  {
486  $aCatIds = array();
487  if ($oOrderArticle = $this->_getOrderArticle()) {
488  $aCatIds = $oOrderArticle->getCategoryIds($blActCats, $blSkipCache);
489  }
490 
491  return $aCatIds;
492  }
493 
499  public function getLanguage()
500  {
501  return oxRegistry::getLang()->getBaseLanguage();
502  }
503 
511  public function getBasePrice($dAmount = 1)
512  {
513 
514  return $this->getPrice();
515  }
516 
522  public function getPrice()
523  {
524  $oBasePrice = oxNew('oxPrice');
525  // prices in db are ONLY brutto
526  $oBasePrice->setBruttoPriceMode();
527  $oBasePrice->setVat($this->oxorderarticles__oxvat->value);
528  $oBasePrice->setPrice($this->oxorderarticles__oxbprice->value);
529 
530  return $oBasePrice;
531  }
532 
538  public function setIsNewOrderItem($blIsNew)
539  {
540  $this->_blIsNewOrderItem = $blIsNew;
541  }
542 
548  public function isNewOrderItem()
549  {
551  }
552 
560  public function setNewAmount($iNewAmount)
561  {
562  if ($iNewAmount >= 0) {
563  // to update stock we must first check if it is possible - article exists?
564  $oArticle = oxNew("oxarticle");
565  if ($oArticle->load($this->oxorderarticles__oxartid->value)) {
566 
567  // updating stock info
568  $iStockChange = $iNewAmount - $this->oxorderarticles__oxamount->value;
569  if ($iStockChange > 0 && ($iOnStock = $oArticle->checkForStock($iStockChange)) !== false) {
570  if ($iOnStock !== true) {
571  $iStockChange = $iOnStock;
572  $iNewAmount = $this->oxorderarticles__oxamount->value + $iStockChange;
573  }
574  }
575 
576  $this->updateArticleStock($iStockChange * -1, $this->getConfig()->getConfigParam('blAllowNegativeStock'));
577 
578  // updating self
579  $this->oxorderarticles__oxamount = new oxField($iNewAmount, oxField::T_RAW);
580  $this->save();
581  }
582  }
583  }
584 
590  public function isOrderArticle()
591  {
592  return true;
593  }
594 
595 
600  public function cancelOrderArticle()
601  {
602  if ($this->oxorderarticles__oxstorno->value == 0) {
603  $myConfig = $this->getConfig();
604  $this->oxorderarticles__oxstorno = new oxField(1);
605  if ($this->save()) {
606  $this->updateArticleStock($this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock'));
607  }
608  }
609  }
610 
619  public function delete($sOXID = null)
620  {
621  if ($blDelete = parent::delete($sOXID)) {
622  $myConfig = $this->getConfig();
623  if ($this->oxorderarticles__oxstorno->value != 1) {
624  $this->updateArticleStock($this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock'));
625  }
626  }
627 
628  return $blDelete;
629  }
630 
638  public function save()
639  {
640  // ordered articles
641  if (($blSave = parent::save()) && $this->isNewOrderItem()) {
642  $myConfig = $this->getConfig();
643  if ($myConfig->getConfigParam('blUseStock') &&
644  $myConfig->getConfigParam('blPsBasketReservationEnabled')
645  ) {
646  $this->getSession()
647  ->getBasketReservations()
648  ->commitArticleReservation(
649  $this->oxorderarticles__oxartid->value,
650  $this->oxorderarticles__oxamount->value
651  );
652  } else {
653  $this->updateArticleStock($this->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam('blAllowNegativeStock'));
654  }
655 
656  // seting downloadable products article files
657  $this->_setOrderFiles();
658 
659  // marking object as "non new" disable further stock changes
660  $this->setIsNewOrderItem(false);
661  }
662 
663  return $blSave;
664  }
665 
671  public function getWrapping()
672  {
673  if ($this->oxorderarticles__oxwrapid->value) {
674  $oWrapping = oxNew('oxwrapping');
675  if ($oWrapping->load($this->oxorderarticles__oxwrapid->value)) {
676  return $oWrapping;
677  }
678  }
679 
680  return null;
681  }
682 
688  public function isBundle()
689  {
690  return ( bool ) $this->oxorderarticles__oxisbundle->value;
691  }
692 
698  public function getTotalBrutPriceFormated()
699  {
700  $oLang = oxRegistry::getLang();
701  $oOrder = $this->getOrder();
702  $oCurrency = $this->getConfig()->getCurrencyObject($oOrder->oxorder__oxcurrency->value);
703 
704  return $oLang->formatCurrency($this->oxorderarticles__oxbrutprice->value, $oCurrency);
705  }
706 
712  public function getBrutPriceFormated()
713  {
714  $oLang = oxRegistry::getLang();
715  $oOrder = $this->getOrder();
716  $oCurrency = $this->getConfig()->getCurrencyObject($oOrder->oxorder__oxcurrency->value);
717 
718  return $oLang->formatCurrency($this->oxorderarticles__oxbprice->value, $oCurrency);
719  }
720 
726  public function getNetPriceFormated()
727  {
728  $oLang = oxRegistry::getLang();
729  $oOrder = $this->getOrder();
730  $oCurrency = $this->getConfig()->getCurrencyObject($oOrder->oxorder__oxcurrency->value);
731 
732  return $oLang->formatCurrency($this->oxorderarticles__oxnprice->value, $oCurrency);
733  }
734 
740  public function getOrder()
741  {
742  if ($this->oxorderarticles__oxorderid->value) {
743  // checking if the object already exists in the cache
744  if (isset($this->_aOrderCache[$this->oxorderarticles__oxorderid->value])) {
745  // returning the cached object
746  return $this->_aOrderCache[$this->oxorderarticles__oxorderid->value];
747  }
748  // creatina new order object and trying to load it
749  $oOrder = oxNew('oxOrder');
750  if ($oOrder->load($this->oxorderarticles__oxorderid->value)) {
751  return $this->_aOrderCache[$this->oxorderarticles__oxorderid->value] = $oOrder;
752  }
753  }
754 
755  return null;
756  }
757 
765  protected function _insert()
766  {
767  $iInsertTime = time();
768  $now = date('Y-m-d H:i:s', $iInsertTime);
769  $this->oxorderarticles__oxtimestamp = new oxField($now);
770 
771  return parent::_insert();
772  }
773 
774 
780  public function setArticle($oArticle)
781  {
782  $this->_oArticle = $oArticle;
783  }
784 
790  public function getArticle()
791  {
792  if ($this->_oArticle === null) {
793  $oArticle = oxNew('oxArticle');
794  $oArticle->load($this->oxorderarticles__oxartid->value);
795  $this->_oArticle = $oArticle;
796  }
797 
798  return $this->_oArticle;
799  }
800 
801 
805  public function _setOrderFiles()
806  {
807  $oArticle = $this->getArticle();
808 
809  if ($oArticle->oxarticles__oxisdownloadable->value) {
810 
811  $oConfig = $this->getConfig();
812  $sOrderId = $this->oxorderarticles__oxorderid->value;
813  $sOrderArticleId = $this->getId();
814  $sShopId = $oConfig->getShopId();
815 
816  $oUser = $oConfig->getUser();
817 
818  $oFiles = $oArticle->getArticleFiles(true);
819 
820  if ($oFiles) {
821  foreach ($oFiles as $oFile) {
822  $oOrderFile = oxNew('oxOrderFile');
823  $oOrderFile->setOrderId($sOrderId);
824  $oOrderFile->setOrderArticleId($sOrderArticleId);
825  $oOrderFile->setShopId($sShopId);
826  $iMaxDownloadCount = (!empty($oUser) && !$oUser->hasAccount()) ? $oFile->getMaxUnregisteredDownloadsCount() : $oFile->getMaxDownloadsCount();
827  $oOrderFile->setFile(
828  $oFile->oxfiles__oxfilename->value,
829  $oFile->getId(),
830  $iMaxDownloadCount * $this->oxorderarticles__oxamount->value,
831  $oFile->getLinkExpirationTime(),
832  $oFile->getDownloadExpirationTime()
833  );
834 
835  $oOrderFile->save();
836  }
837  }
838  }
839  }
840 
846  public function getTotalNetPriceFormated()
847  {
848  $oLang = oxRegistry::getLang();
849  $oOrder = $this->getOrder();
850  $oCurrency = $this->getConfig()->getCurrencyObject($oOrder->oxorder__oxcurrency->value);
851 
852  return $oLang->formatCurrency($this->oxorderarticles__oxnetprice->value, $oCurrency);
853  }
854 }