OXID eShop CE  4.8.12
 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 
89  public function copyThis( $oProduct )
90  {
91  $aObjectVars = get_object_vars( $oProduct );
92 
93  foreach ( $aObjectVars as $sName => $sValue ) {
94  if ( isset( $oProduct->$sName->value ) ) {
95  $sFieldName = preg_replace('/oxarticles__/', 'oxorderarticles__', $sName);
96  if ( $sFieldName != "oxorderarticles__oxtimestamp" ) {
97  $this->$sFieldName = $oProduct->$sName;
98  }
99  // formatting view
100  if ( !$this->getConfig()->getConfigParam( 'blSkipFormatConversion' ) ) {
101  if ( $sFieldName == "oxorderarticles__oxinsert" ) {
102  oxRegistry::get("oxUtilsDate")->convertDBDate( $this->$sFieldName, true );
103  }
104  }
105  }
106  }
107 
108  }
109 
117  public function assign( $dbRecord )
118  {
119  parent::assign( $dbRecord );
120  $this->_setArticleParams();
121  }
122 
133  public function updateArticleStock( $dAddAmount, $blAllowNegativeStock = false )
134  {
135  // TODO: use oxarticle reduceStock
136  // decrement stock if there is any
137  $oArticle = oxNew( 'oxarticle' );
138  $oArticle->load( $this->oxorderarticles__oxartid->value );
139  $oArticle->beforeUpdate();
140 
141  if ( $this->getConfig()->getConfigParam( 'blUseStock' ) ) {
142  // get real article stock count
143  $iStockCount = $this->_getArtStock( $dAddAmount, $blAllowNegativeStock );
144  $oDb = oxDb::getDb();
145 
146  $oArticle->oxarticles__oxstock = new oxField($iStockCount);
147  $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->oxorderarticles__oxartid->value ) );
148  $oArticle->onChange( ACTION_UPDATE_STOCK );
149  }
150 
151  //update article sold amount
152  $oArticle->updateSoldAmount( $dAddAmount * ( -1 ) );
153  }
154 
163  protected function _getArtStock( $dAddAmount = 0, $blAllowNegativeStock = false )
164  {
165  $oDb = oxDb::getDb();
166 
167  // #1592A. must take real value
168  $sQ = 'select oxstock from oxarticles where oxid = '.$oDb->quote( $this->oxorderarticles__oxartid->value );
169  $iStockCount = ( float ) $oDb->getOne( $sQ, false, false );
170 
171  $iStockCount += $dAddAmount;
172 
173  // #1592A. calculating according new stock option
174  if ( !$blAllowNegativeStock && $iStockCount < 0 ) {
175  $iStockCount = 0;
176  }
177 
178  return $iStockCount;
179  }
180 
181 
187  public function getPersParams()
188  {
189  if ( $this->_aPersParam != null ) {
190  return $this->_aPersParam;
191  }
192 
193  if ( $this->oxorderarticles__oxpersparam->value ) {
194  $this->_aPersParam = unserialize( $this->oxorderarticles__oxpersparam->value );
195  }
196 
197  return $this->_aPersParam;
198  }
199 
207  public function setPersParams( $aParams )
208  {
209  $this->_aPersParam = $aParams;
210 
211  // serializing persisten info stored while ordering
212  $this->oxorderarticles__oxpersparam = new oxField(serialize( $aParams ), oxField::T_RAW);
213  }
214 
224  protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
225  {
226  $sFieldName = strtolower($sFieldName);
227  switch ( $sFieldName ) {
228  case 'oxpersparam':
229  case 'oxorderarticles__oxpersparam':
230  case 'oxerpstatus':
231  case 'oxorderarticles__oxerpstatus':
232  case 'oxtitle':
233  case 'oxorderarticles__oxtitle':
234  $iDataType = oxField::T_RAW;
235  break;
236  }
237  return parent::_setFieldData($sFieldName, $sValue, $iDataType);
238  }
239 
248  public function loadInLang( $iLanguage, $sOxid )
249  {
250  return $this->load( $sOxid );
251  }
252 
258  public function getProductId()
259  {
260  return $this->oxorderarticles__oxartid->value;
261  }
262 
268  public function getProductParentId()
269  {
270  // when this field will be introduced there will be no need to load from real article
271  if ( isset( $this->oxorderarticles__oxartparentid ) && $this->oxorderarticles__oxartparentid->value !== false ) {
272  return $this->oxorderarticles__oxartparentid->value;
273  }
274 
275  $oDb = oxDb::getDb();
276  $oArticle = oxNew( "oxarticle" );
277  $sQ = "select oxparentid from " . $oArticle->getViewName() . " where oxid=" . $oDb->quote( $this->getProductId() );
278  $this->oxarticles__oxparentid = new oxField( $oDb->getOne( $sQ ) );
279  return $this->oxarticles__oxparentid->value;
280  }
281 
287  protected function _setArticleParams()
288  {
289  // creating needed fields
290  $this->oxarticles__oxstock = $this->oxorderarticles__oxamount;
291  $this->oxarticles__oxtitle = $this->oxorderarticles__oxtitle;
292  $this->oxarticles__oxwidth = $this->oxorderarticles__oxwidth;
293  $this->oxarticles__oxlength = $this->oxorderarticles__oxlength;
294  $this->oxarticles__oxheight = $this->oxorderarticles__oxheight;
295  $this->oxarticles__oxweight = $this->oxorderarticles__oxweight;
296  $this->oxarticles__oxsubclass = $this->oxorderarticles__oxsubclass;
297  $this->oxarticles__oxartnum = $this->oxorderarticles__oxartnum;
298  $this->oxarticles__oxshortdesc = $this->oxorderarticles__oxshortdesc;
299 
300  $this->oxarticles__oxvat = $this->oxorderarticles__oxvat;
301  $this->oxarticles__oxprice = $this->oxorderarticles__oxprice;
302  $this->oxarticles__oxbprice = $this->oxorderarticles__oxbprice;
303 
304  $this->oxarticles__oxthumb = $this->oxorderarticles__oxthumb;
305  $this->oxarticles__oxpic1 = $this->oxorderarticles__oxpic1;
306  $this->oxarticles__oxpic2 = $this->oxorderarticles__oxpic2;
307  $this->oxarticles__oxpic3 = $this->oxorderarticles__oxpic3;
308  $this->oxarticles__oxpic4 = $this->oxorderarticles__oxpic4;
309  $this->oxarticles__oxpic5 = $this->oxorderarticles__oxpic5;
310 
311  $this->oxarticles__oxfile = $this->oxorderarticles__oxfile;
312  $this->oxarticles__oxdelivery = $this->oxorderarticles__oxdelivery;
313  $this->oxarticles__oxissearch = $this->oxorderarticles__oxissearch;
314  $this->oxarticles__oxfolder = $this->oxorderarticles__oxfolder;
315  $this->oxarticles__oxtemplate = $this->oxorderarticles__oxtemplate;
316  $this->oxarticles__oxexturl = $this->oxorderarticles__oxexturl;
317  $this->oxarticles__oxurlimg = $this->oxorderarticles__oxurlimg;
318  $this->oxarticles__oxurldesc = $this->oxorderarticles__oxurldesc;
319  $this->oxarticles__oxshopid = $this->oxorderarticles__oxordershopid;
320  $this->oxarticles__oxquestionemail = $this->oxorderarticles__oxquestionemail;
321  $this->oxarticles__oxsearchkeys = $this->oxorderarticles__oxsearchkeys;
322  }
323 
332  public function checkForStock( $dAmount, $dArtStockAmount = 0 )
333  {
334  return true;
335  }
336 
345  protected function _getOrderArticle( $sArticleId = null )
346  {
347  if ( $this->_oOrderArticle === null ) {
348  $this->_oOrderArticle = false;
349 
350  $sArticleId = $sArticleId ? $sArticleId : $this->getProductId();
351  $oArticle = oxNew( "oxArticle" );
352  $oArticle->setLoadParentData(true);
353  if ( $oArticle->load( $sArticleId ) ) {
354  $this->_oOrderArticle = $oArticle;
355  }
356  }
357  return $this->_oOrderArticle;
358  }
359 
367  public function getSelectLists( $sKeyPrefix = null )
368  {
369  $aSelLists = array();
370  if ( $oArticle = $this->_getOrderArticle() ) {
371  $aSelLists = $oArticle->getSelectLists();
372  }
373  return $aSelLists;
374  }
375 
384  public function getOrderArticleSelectList( $sArtId = null, $sOrderArtSelList = null )
385  {
386  if ( $this->_aOrderArticleSelList === null ) {
387 
388  $sOrderArtSelList = $sOrderArtSelList ? $sOrderArtSelList : $this->oxorderarticles__oxselvariant->value;
389 
390  $aList = array();
391  $aRet = array();
392 
393  if ( $oArticle = $this->_getOrderArticle( $sArtId ) ) {
394  $aList = explode( ", ", $sOrderArtSelList );
395  $oStr = getStr();
396 
397  $aArticleSelList = $oArticle->getSelectLists();
398 
399  //formatting temporary list array from string
400  foreach ( $aList as $sList ) {
401  if ( $sList ) {
402 
403  $aVal = explode( ":", $sList );
404  if ( isset($aVal[0]) && isset($aVal[1])) {
405  $sOrderArtListTitle = $oStr->strtolower( trim($aVal[0]) );
406  $sOrderArtSelValue = $oStr->strtolower( trim($aVal[1]) );
407 
408  //checking article list for matches with article list stored in oxOrderItem
409  $iSelListNum = 0;
410  if ( count($aArticleSelList) > 0 ) {
411  foreach ( $aArticleSelList as $aSelect ) {
412  //check if selects titles are equal
413 
414  if ( $oStr->strtolower($aSelect['name']) == $sOrderArtListTitle ) {
415  //try to find matching select items value
416  $iSelValueNum = 0;
417  foreach ( $aSelect as $oSel ) {
418  if ( $oStr->strtolower($oSel->name) == $sOrderArtSelValue ) {
419  // found, adding to return array
420  $aRet[$iSelListNum] = $iSelValueNum;
421  }
422  //next article list item
423  $iSelValueNum++;
424  }
425  }
426  //next article list
427  $iSelListNum++;
428  }
429  }
430  }
431  }
432  }
433  }
434 
435  $this->_aOrderArticleSelList = $aRet;
436  }
437 
439  }
440 
450  public function getBasketPrice( $dAmount, $aSelList, $oBasket )
451  {
452  $oArticle = $this->_getOrderArticle();
453  if ( $oArticle ) {
454  return $oArticle->getBasketPrice( $dAmount, $aSelList, $oBasket );
455  } else {
456  return $this->getPrice();
457  }
458  }
459 
465  public function skipDiscounts()
466  {
467  return false;
468  }
469 
478  public function getCategoryIds( $blActCats = false, $blSkipCache = false )
479  {
480  $aCatIds = array();
481  if ( $oOrderArticle = $this->_getOrderArticle() ) {
482  $aCatIds = $oOrderArticle->getCategoryIds( $blActCats, $blSkipCache );
483  }
484  return $aCatIds;
485  }
486 
492  public function getLanguage()
493  {
494  return oxRegistry::getLang()->getBaseLanguage();
495  }
496 
504  public function getBasePrice( $dAmount = 1 )
505  {
506 
507  return $this->getPrice();
508  }
509 
515  public function getPrice()
516  {
517  $oBasePrice = oxNew( 'oxPrice' );
518  // prices in db are ONLY brutto
519  $oBasePrice->setBruttoPriceMode();
520  $oBasePrice->setVat( $this->oxorderarticles__oxvat->value );
521  $oBasePrice->setPrice( $this->oxorderarticles__oxbprice->value );
522 
523  return $oBasePrice;
524  }
525 
533  public function setIsNewOrderItem( $blIsNew )
534  {
535  $this->_blIsNewOrderItem = $blIsNew;
536  }
537 
543  public function isNewOrderItem()
544  {
546  }
547 
557  public function setNewAmount( $iNewAmount )
558  {
559  if ( $iNewAmount >= 0 ) {
560  // to update stock we must first check if it is possible - article exists?
561  $oArticle = oxNew( "oxarticle" );
562  if ( $oArticle->load( $this->oxorderarticles__oxartid->value ) ) {
563 
564  // updating stock info
565  $iStockChange = $iNewAmount - $this->oxorderarticles__oxamount->value;
566  if ( $iStockChange > 0 && ( $iOnStock = $oArticle->checkForStock( $iStockChange ) ) !== false ) {
567  if ( $iOnStock !== true ) {
568  $iStockChange = $iOnStock;
569  $iNewAmount = $this->oxorderarticles__oxamount->value + $iStockChange;
570  }
571  }
572 
573  $this->updateArticleStock( $iStockChange * -1, $this->getConfig()->getConfigParam( 'blAllowNegativeStock' ) );
574 
575  // updating self
576  $this->oxorderarticles__oxamount = new oxField ( $iNewAmount, oxField::T_RAW );
577  $this->save();
578  }
579  }
580  }
581 
587  public function isOrderArticle()
588  {
589  return true;
590  }
591 
592 
599  public function cancelOrderArticle()
600  {
601  if ( $this->oxorderarticles__oxstorno->value == 0 ) {
602  $myConfig = $this->getConfig();
603  $this->oxorderarticles__oxstorno = new oxField( 1 );
604  if ( $this->save() ) {
605  $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
606  }
607  }
608  }
609 
618  public function delete( $sOXID = null)
619  {
620  if ( $blDelete = parent::delete( $sOXID ) ) {
621  $myConfig = $this->getConfig();
622  if ( $this->oxorderarticles__oxstorno->value != 1 ) {
623  $this->updateArticleStock( $this->oxorderarticles__oxamount->value, $myConfig->getConfigParam('blAllowNegativeStock') );
624  }
625  }
626  return $blDelete;
627  }
628 
636  public function save()
637  {
638  // ordered articles
639  if ( ( $blSave = parent::save() ) && $this->isNewOrderItem() ) {
640  $myConfig = $this->getConfig();
641  if ( $myConfig->getConfigParam( 'blUseStock' ) &&
642  $myConfig->getConfigParam( 'blPsBasketReservationEnabled' ) ) {
643  $this->getSession()
644  ->getBasketReservations()
645  ->commitArticleReservation(
646  $this->oxorderarticles__oxartid->value,
647  $this->oxorderarticles__oxamount->value
648  );
649  } else {
650  $this->updateArticleStock( $this->oxorderarticles__oxamount->value * (-1), $myConfig->getConfigParam( 'blAllowNegativeStock' ) );
651  }
652 
653  // seting downloadable products article files
654  $this->_setOrderFiles();
655 
656  // marking object as "non new" disable further stock changes
657  $this->setIsNewOrderItem( false );
658  }
659 
660  return $blSave;
661  }
662 
668  public function getWrapping()
669  {
670  if ($this->oxorderarticles__oxwrapid->value) {
671  $oWrapping = oxNew('oxwrapping');
672  if ($oWrapping->load($this->oxorderarticles__oxwrapid->value)) {
673  return $oWrapping;
674  }
675  }
676  return null;
677  }
678 
684  public function isBundle()
685  {
686  return ( bool ) $this->oxorderarticles__oxisbundle->value;
687  }
688 
694  public function getTotalBrutPriceFormated()
695  {
696  $oLang = oxRegistry::getLang();
697  $oOrder = $this->getOrder();
698  $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
699  return $oLang->formatCurrency( $this->oxorderarticles__oxbrutprice->value, $oCurrency );
700  }
701 
707  public function getBrutPriceFormated()
708  {
709  $oLang = oxRegistry::getLang();
710  $oOrder = $this->getOrder();
711  $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
712  return $oLang->formatCurrency( $this->oxorderarticles__oxbprice->value, $oCurrency );
713  }
714 
720  public function getNetPriceFormated()
721  {
722  $oLang = oxRegistry::getLang();
723  $oOrder = $this->getOrder();
724  $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
725  return $oLang->formatCurrency( $this->oxorderarticles__oxnprice->value, $oCurrency );
726  }
727 
733  public function getOrder()
734  {
735  if ( $this->oxorderarticles__oxorderid->value ) {
736  // checking if the object already exists in the cache
737  if ( isset( $this->_aOrderCache[ $this->oxorderarticles__oxorderid->value ] )) {
738  // returning the cached object
739  return $this->_aOrderCache[ $this->oxorderarticles__oxorderid->value ];
740  }
741  // creatina new order object and trying to load it
742  $oOrder = oxNew( 'oxOrder' );
743  if ( $oOrder->load( $this->oxorderarticles__oxorderid->value )) {
744  return $this->_aOrderCache[$this->oxorderarticles__oxorderid->value] = $oOrder;
745  }
746  }
747 
748  return null;
749  }
750 
758  protected function _insert()
759  {
760  $iInsertTime = time();
761  $now = date('Y-m-d H:i:s', $iInsertTime);
762  $this->oxorderarticles__oxtimestamp = new oxField( $now );
763 
764  return parent::_insert();
765  }
766 
767 
775  public function setArticle( $oArticle )
776  {
777  $this->_oArticle = $oArticle;
778  }
779 
785  public function getArticle()
786  {
787  if ( $this->_oArticle === null ) {
788  $oArticle = oxNew( 'oxArticle' );
789  $oArticle->load($this->oxorderarticles__oxartid->value);
790  $this->_oArticle = $oArticle;
791  }
792 
793  return $this->_oArticle;
794  }
795 
796 
797 
803  public function _setOrderFiles()
804  {
805  $oArticle = $this->getArticle();
806 
807  if ( $oArticle->oxarticles__oxisdownloadable->value ) {
808 
809  $oConfig = $this->getConfig();
810  $sOrderId = $this->oxorderarticles__oxorderid->value;
811  $sOrderArticleId = $this->getId();
812  $sShopId = $oConfig->getShopId();
813 
814  $oUser = $oConfig->getUser();
815 
816  $oFiles = $oArticle->getArticleFiles( true );
817 
818  if ( $oFiles ) {
819  foreach ($oFiles as $oFile) {
820  $oOrderFile = oxNew( 'oxOrderFile' );
821  $oOrderFile->setOrderId( $sOrderId );
822  $oOrderFile->setOrderArticleId( $sOrderArticleId );
823  $oOrderFile->setShopId( $sShopId );
824  $iMaxDownloadCount = (!empty($oUser) && !$oUser->hasAccount()) ? $oFile->getMaxUnregisteredDownloadsCount() : $oFile->getMaxDownloadsCount();
825  $oOrderFile->setFile(
826  $oFile->oxfiles__oxfilename->value,
827  $oFile->getId(),
828  $iMaxDownloadCount * $this->oxorderarticles__oxamount->value,
829  $oFile->getLinkExpirationTime(),
830  $oFile->getDownloadExpirationTime()
831  );
832 
833  $oOrderFile->save();
834  }
835  }
836  }
837  }
838 
844  public function getTotalNetPriceFormated()
845  {
846  $oLang = oxRegistry::getLang();
847  $oOrder = $this->getOrder();
848  $oCurrency = $this->getConfig()->getCurrencyObject( $oOrder->oxorder__oxcurrency->value );
849  return $oLang->formatCurrency( $this->oxorderarticles__oxnetprice->value, $oCurrency );
850  }
851 }