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