OXID eShop CE  4.10.8
 All Classes Namespaces Files Functions Variables Pages
oxerpcsv.php
Go to the documentation of this file.
1 <?php
2 
8 class oxErpCsv extends oxERPBase
9 {
10 
16  protected $_aSupportedVersions = array("0.1", "1.0", "1.1", "2.0");
17 
23  protected $_aCsv2BaseVersionsMap = array("0.1" => "1", "1.0" => "1", "1.1" => "1.1", "2.0" => "2");
24 
30  protected $_sCurrVersion = "";
31 
37  protected $_aData = array();
38 
44  protected $_iRetryRows = 0;
45 
51  protected $_sReturn;
52 
58  protected $_sPath;
59 
65  protected $_aImportedActions2Article = array();
66 
72  protected $_aImportedObject2Category = array();
73 
79  protected $_aImportedAccessoire2Article = array();
80 
86  protected function _beforeExport($sType)
87  {
88  }
89 
95  protected function _afterExport($sType)
96  {
97  }
98 
104  protected function _getImportType(&$aData)
105  {
106  }
107 
113  protected function _getImportMode($aData)
114  {
115  }
116 
123  protected function _modifyData($aData, $oType)
124  {
125  }
126 
132  public function loadSessionData($sSessionID)
133  {
134  }
135 
144  protected function _csvTextConvert($sText, $blMode)
145  {
146  $aSearch = array(chr(13), chr(10), '\'', '"');
147  $aReplace = array('&#13;', '&#10;', '&#39;', '&#34;');
148 
149  if ($blMode) {
150  $sText = str_replace($aSearch, $aReplace, $sText);
151  } else {
152  $sText = str_replace($aReplace, $aSearch, $sText);
153  }
154 
155  return $sText;
156  }
157 
161  public function import()
162  {
163  $this->_beforeImport();
164 
165  do {
166  while ($this->_importOne()) {
167  ;
168  }
169  } while (!$this->_afterImport());
170  }
171 
175  protected function _beforeImport()
176  {
177  if (!$this->_iRetryRows) {
178  //convert all text
179  foreach ($this->_aData as $key => $value) {
180  $this->_aData[$key] = $this->_csvTextConvert($value, false);
181  }
182  }
183 
184  }
185 
191  protected function _afterImport()
192  {
193  //check if there have been no errors or failures
194  $aStatistics = $this->getStatistics();
195  $iRetryRows = 0;
196 
197  foreach ($aStatistics as $key => $value) {
198  if ($value['r'] == false) {
199  $iRetryRows++;
200  $this->_sReturn .= "File[" . $this->_sPath . "] - dataset number: $key - Error: " . $value['m'] . " ---<br> " . PHP_EOL;
201  }
202  }
203 
204  if ($iRetryRows != $this->_iRetryRows && $iRetryRows > 0) {
205  $this->_resetIdx();
206  $this->_iRetryRows = $iRetryRows;
207  $this->_sReturn = '';
208 
209  return false;
210  }
211 
212  return true;
213  }
214 
222  public function getImportData($iIdx = null)
223  {
224  return $this->_aData[$this->_iIdx];
225  }
226 
233  private function getOldOrderArticleFieldList()
234  {
235  $aFieldList = array(
236  'OXID' => 'OXID',
237  'OXORDERID' => 'OXORDERID',
238  'OXAMOUNT' => 'OXAMOUNT',
239  'OXARTID' => 'OXARTID',
240  'OXARTNUM' => 'OXARTNUM',
241  'OXTITLE' => 'OXTITLE',
242  'OXSELVARIANT' => 'OXSELVARIANT',
243  'OXNETPRICE' => 'OXNETPRICE',
244  'OXBRUTPRICE' => 'OXBRUTPRICE',
245  'OXVAT' => 'OXVAT',
246  'OXPERSPARAM' => 'OXPERSPARAM',
247  'OXPRICE' => 'OXPRICE',
248  'OXBPRICE' => 'OXBPRICE',
249  'OXTPRICE' => 'OXTPRICE',
250  'OXWRAPID' => 'OXWRAPID',
251  'OXSTOCK' => 'OXSTOCK',
252  'OXORDERSHOPID' => 'OXORDERSHOPID',
253  'OXTOTALVAT' => 'OXTOTALVAT'
254  );
255 
256  return $aFieldList;
257  }
258 
265  private function getOldOrderFielsList()
266  {
267  $aFieldList = array(
268  'OXID' => 'OXID',
269  'OXSHOPID' => 'OXSHOPID',
270  'OXUSERID' => 'OXUSERID',
271  'OXORDERDATE' => 'OXORDERDATE',
272  'OXORDERNR' => 'OXORDERNR',
273  'OXBILLCOMPANY' => 'OXBILLCOMPANY',
274  'OXBILLEMAIL' => 'OXBILLEMAIL',
275  'OXBILLFNAME' => 'OXBILLFNAME',
276  'OXBILLLNAME' => 'OXBILLLNAME',
277  'OXBILLSTREET' => 'OXBILLSTREET',
278  'OXBILLSTREETNR' => 'OXBILLSTREETNR',
279  'OXBILLADDINFO' => 'OXBILLADDINFO',
280  'OXBILLUSTID' => 'OXBILLUSTID',
281  'OXBILLCITY' => 'OXBILLCITY',
282  'OXBILLCOUNTRY' => 'OXBILLCOUNTRY',
283  'OXBILLZIP' => 'OXBILLZIP',
284  'OXBILLFON' => 'OXBILLFON',
285  'OXBILLFAX' => 'OXBILLFAX',
286  'OXBILLSAL' => 'OXBILLSAL',
287  'OXDELCOMPANY' => 'OXDELCOMPANY',
288  'OXDELFNAME' => 'OXDELFNAME',
289  'OXDELLNAME' => 'OXDELLNAME',
290  'OXDELSTREET' => 'OXDELSTREET',
291  'OXDELSTREETNR' => 'OXDELSTREETNR',
292  'OXDELADDINFO' => 'OXDELADDINFO',
293  'OXDELCITY' => 'OXDELCITY',
294  'OXDELCOUNTRY' => 'OXDELCOUNTRY',
295  'OXDELZIP' => 'OXDELZIP',
296  'OXDELFON' => 'OXDELFON',
297  'OXDELFAX' => 'OXDELFAX',
298  'OXDELSAL' => 'OXDELSAL',
299  'OXDELCOST' => 'OXDELCOST',
300  'OXDELVAT' => 'OXDELVAT',
301  'OXPAYCOST' => 'OXPAYCOST',
302  'OXPAYVAT' => 'OXPAYVAT',
303  'OXWRAPCOST' => 'OXWRAPCOST',
304  'OXWRAPVAT' => 'OXWRAPVAT',
305  'OXCARDID' => 'OXCARDID',
306  'OXCARDTEXT' => 'OXCARDTEXT',
307  'OXDISCOUNT' => 'OXDISCOUNT',
308  'OXBILLNR' => 'OXBILLNR',
309  'OXREMARK' => 'OXREMARK',
310  'OXVOUCHERDISCOUNT' => 'OXVOUCHERDISCOUNT',
311  'OXCURRENCY' => 'OXCURRENCY',
312  'OXCURRATE' => 'OXCURRATE',
313  'OXTRANSID' => 'OXTRANSID',
314  'OXPAID' => 'OXPAID',
315  'OXIP' => 'OXIP',
316  'OXTRANSSTATUS' => 'OXTRANSSTATUS',
317  'OXLANG' => 'OXLANG',
318  'OXDELTYPE' => 'OXDELTYPE'
319  );
320 
321  return $aFieldList;
322  }
323 
329  protected function _checkIDField($sID)
330  {
331  if (!isset($sID) || !$sID) {
332  throw new Exception("ERROR: Articlenumber/ID missing!");
333  } elseif (strlen($sID) > 32) {
334  throw new Exception("ERROR: Articlenumber/ID longer then allowed (32 chars max.)!");
335  }
336  }
337 
345  protected function _getInstanceOfType($sType)
346  {
347  //due to backward compatibility
348  if ($sType == 'oldOrder') {
349  $oType = parent::_getInstanceOfType('order');
350  $oType->setFieldList($this->getOldOrderFielsList());
351  $oType->setFunctionSuffix('OldOrder');
352  } elseif ($sType == 'oldOrderArticle') {
353  $oType = parent::_getInstanceOfType('orderarticle');
354  $oType->setFieldList($this->getOldOrderArticleFieldList());
355  $oType->setFunctionSuffix('OldOrderArticle');
356  } elseif ($sType == 'article2vendor') {
357  $oType = parent::_getInstanceOfType('article');
358  $oType->setFieldList(array("OXID", "OXVENDORID"));
359  } elseif ($sType == 'mainarticle2categroy') {
360  $oType = parent::_getInstanceOfType('article2category');
361  $oType->setFieldList(array("OXOBJECTID", "OXCATNID", "OXTIME"));
362  $oType->setFunctionSuffix('mainarticle2category');
363  } else {
364  $oType = parent::_getInstanceOfType($sType);
365  }
366 
367  return $oType;
368  }
369 
370  // --------------------------------------------------------------------------
371  //
372  // Import Handler
373  // One _Import* method needed for each object defined in /objects/ folder, all these objects can be imported
374  //
375  // --------------------------------------------------------------------------
376 
385  protected function _importArticle(oxERPType $oType, $aRow)
386  {
387  if ($this->_sCurrVersion == "0.1") {
389  //to allow different shopid without consequences (ignored fields)
390  $myConfig->setConfigParam('blMallCustomPrice', false);
391  }
392 
393  if (isset($aRow['OXID'])) {
394  $this->_checkIDField($aRow['OXID']);
395  }
396  // #0004426
397  /*else {
398  $this->_checkIDField($aRow['OXARTNUM']);
399  $aRow['OXID'] = $aRow['OXARTNUM'];
400  }*/
401 
402  $sResult = $this->_save($oType, $aRow, $this->_sCurrVersion == "0.1"); // V0.1 allowes the shopid to be set no matter which login
403  return $sResult;
404  }
405 
414  protected function _importAccessoire(oxERPType $oType, $aRow)
415  {
416  // deleting old relations before import in V0.1
417  if ($this->_sCurrVersion == "0.1" && !isset($this->_aImportedAccessoire2Article[$aRow['OXARTICLENID']])) {
419  $oDb = oxDb::getDb();
420  $oDb->execute("delete from oxaccessoire2article where oxarticlenid = " . $oDb->quote($aRow['OXARTICLENID']));
421  $this->_aImportedAccessoire2Article[$aRow['OXARTICLENID']] = 1;
422  }
423 
424  $sResult = $this->_save($oType, $aRow);
425 
426  return $sResult;
427  }
428 
437  protected function _importArticle2Action(oxERPType $oType, $aRow)
438  {
439 
440  if ($this->_sCurrVersion == "0.1" && !isset($this->_aImportedActions2Article[$aRow['OXARTID']])) {
441  //only in V0.1 and only once per import/article
443  $oDb = oxDb::getDb();
444  $oDb->execute("delete from oxactions2article where oxartid = " . $oDb->quote($aRow['OXARTID']));
445  $this->_aImportedActions2Article[$aRow['OXARTID']] = 1;
446  }
447 
448  $sResult = $this->_save($oType, $aRow, $this->_sCurrVersion == "0.1");
449 
450  return $sResult;
451  }
452 
461  protected function _importArticle2Category(oxERPType $oType, $aRow)
462  {
463  // deleting old relations before import in V0.1
464  if ($this->_sCurrVersion == "0.1" && !isset($this->_aImportedObject2Category[$aRow['OXOBJECTID']])) {
466  $oDb = oxDb::getDb();
467  $oDb->execute("delete from oxobject2category where oxobjectid = " . $oDb->quote($aRow['OXOBJECTID']));
468  $this->_aImportedObject2Category[$aRow['OXOBJECTID']] = 1;
469  }
470 
471  $sResult = $this->_save($oType, $aRow);
472 
473  return $sResult;
474  }
475 
484  protected function _importMainArticle2Category(oxERPType $oType, $aRow)
485  {
486  $aRow['OXTIME'] = 0;
487 
489  $oDb = oxDb::getDb();
490 
491  $sSql = "select OXID from oxobject2category where oxobjectid = " . $oDb->quote($aRow['OXOBJECTID']) . " and OXCATNID = " . $oDb->quote($aRow['OXCATNID']);
492  $aRow['OXID'] = $oDb->getOne($sSql, false, false);
493 
494  $sResult = $this->_save($oType, $aRow);
495  if ((boolean) $sResult) {
496  $sSql = "Update oxobject2category set oxtime = oxtime+10 where oxobjectid = " . $oDb->quote($aRow['OXOBJECTID']) . " and oxcatnid != " . $oDb->quote($aRow['OXCATNID']) . " and oxshopid = '" . $myConfig->getShopId() . "'";
497  $oDb->Execute($sSql);
498  }
499 
500  return $sResult;
501  }
502 
511  protected function _importCategory(oxERPType $oType, $aRow)
512  {
513  $sResult = $this->_save($oType, $aRow, $this->_sCurrVersion == "0.1");
514 
515  return $sResult;
516  }
517 
526  protected function _importCrossselling(oxERPType $oType, $aRow)
527  {
528  // deleting old relations before import in V0.1
529  if ($this->_sCurrVersion == "0.1" && !isset($this->_aImportedObject2Article[$aRow['OXARTICLENID']])) {
531  $oDb = oxDb::getDb();
532  $oDb->Execute("delete from oxobject2article where oxarticlenid = " . $oDb->quote($aRow['OXARTICLENID']));
533  $this->aImportedObject2Article[$aRow['OXARTICLENID']] = 1;
534  }
535 
536  $sResult = $this->_save($oType, $aRow);
537 
538  return $sResult;
539  }
540 
549  protected function _importScaleprice(oxERPType $oType, $aRow)
550  {
551  $sResult = $this->_save($oType, $aRow, $this->_sCurrVersion == "0.1");
552 
553  return $sResult;
554  }
555 
564  protected function _importOrder(oxERPType $oType, $aRow)
565  {
566  $sResult = $this->_save($oType, $aRow);
567 
568  return $sResult;
569  //MAFI a unavoidable hack as oxorder->update() does always return null !!! a hotfix is needed
570  //hotfix was added? since it's working with proper return now
571  }
572 
581  protected function _importOrderArticle(oxERPType $oType, $aRow)
582  {
583  $sResult = $this->_save($oType, $aRow);
584 
585  return $sResult;
586  }
587 
596  protected function _importOrderStatus(oxERPType $oType, $aRow)
597  {
598  $oOrderArt = oxNew("oxorderarticle", "core");
599  $oOrderArt->load($aRow['OXID']);
600 
601  if ($oOrderArt->getId()) {
602 
603  try {
604  if ($this->_sCurrVersion != "0.1") {
605  $oType->checkWriteAccess($oOrderArt->getId());
606  }
607 
608  // store status
609  $aStatuses = unserialize($oOrderArt->oxorderarticles__oxerpstatus->value);
610 
611  $oStatus = new stdClass();
612  $oStatus->STATUS = $aRow['OXERPSTATUS_STATUS'];
613  $oStatus->date = $aRow['OXERPSTATUS_TIME'];
614  $oStatus->trackingid = $aRow['OXERPSTATUS_TRACKID'];
615 
616  $aStatuses[$aRow['OXERPSTATUS_TIME']] = $oStatus;
617  $oOrderArt->oxorderarticles__oxerpstatus = new oxField(serialize($aStatuses), oxField::T_RAW);
618  $oOrderArt->save();
619 
620  return true;
621  } catch (Exception $ex) {
622  return false;
623  }
624  }
625 
626  return false;
627  }
628 
637  protected function _importUser(oxERPType $oType, $aRow)
638  {
639  //Speciall check for user
640  if (isset($aRow['OXUSERNAME'])) {
641  $sID = $aRow['OXID'];
642  $sUserName = $aRow['OXUSERNAME'];
643 
644  $oUser = oxNew("oxuser", "core");
645  $oUser->oxuser__oxusername = new oxField($sUserName, oxField::T_RAW);
646 
647  //If user exists with and modifies OXID, throw an axception
648  //throw new Exception( "USER {$sUserName} already exists!");
649  if ($oUser->exists($sID) && $sID != $oUser->getId()) {
650  throw new Exception("USER $sUserName already exists!");
651  }
652 
653  }
654 
655  $sResult = $this->_save($oType, $aRow);
656 
657  return $sResult;
658  }
659 
668  protected function _importVendor(oxERPType $oType, $aRow)
669  {
670  $sResult = $this->_save($oType, $aRow, $this->_sCurrVersion == "0.1");
671 
672  return $sResult;
673  }
674 
683  protected function _importArtextends(oxERPType $oType, $aRow)
684  {
685  if (oxERPBase::getRequestedVersion() < 2) {
686  return false;
687  }
688  $sResult = $this->_save($oType, $aRow);
689 
690  return $sResult;
691  }
692 
701  protected function _importCountry(oxERPType $oType, $aRow)
702  {
703  $sResult = $this->_save($oType, $aRow);
704 
705  return $sResult;
706  }
707 
716  protected function _importArticleStock(oxERPType $oType, $aRow)
717  {
718  $sResult = $this->_save($oType, $aRow);
719 
720  return $sResult;
721  }
722 
727  public function getImportedRowCount()
728  {
729  return count($this->_aImportedIds);
730  }
731 
736  public function setImportedIds($key)
737  {
738  if (!array_key_exists($key, $this->_aImportedIds)) {
739  $this->_aImportedIds[$key] = true;
740  }
741  }
742 }