OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxerpcsv.php
Go to the documentation of this file.
1 <?php
2 
6 class oxErpCsv extends oxERPBase
7 {
12  protected $_aSupportedVersions = array("0.1", "1.0", "1.1", "2.0");
13 
18  protected $_aCsv2BaseVersionsMap = array("0.1" => "1", "1.0" => "1", "1.1"=>"1.1", "2.0" => "2");
19 
24  protected $_sCurrVersion = "";
25 
30  protected $_aData = array();
31 
36  protected $_iRetryRows = 0;
37 
42  protected $_sReturn;
43 
48  protected $_sPath;
49 
54  protected $_aImportedActions2Article = array();
55 
60  protected $_aImportedObject2Category = array();
61 
66  protected $_aImportedAccessoire2Article = array();
67 
75  protected function _beforeExport($sType)
76  {
77  }
78 
86  protected function _afterExport($sType)
87  {
88  }
89 
97  protected function _getImportType( &$aData )
98  {
99  }
100 
108  protected function _getImportMode( $aData )
109  {
110  }
111 
120  protected function _modifyData($aData, $oType)
121  {
122  }
123 
131  public function loadSessionData( $sSessionID )
132  {
133  }
134 
143  protected function _csvTextConvert( $sText, $blMode )
144  {
145  $aSearch = array( chr(13), chr(10), '\'', '"' );
146  $aReplace = array( '&#13;', '&#10;', '&#39;', '&#34;' );
147 
148  if ( $blMode ) {
149  $sText = str_replace( $aSearch, $aReplace, $sText );
150  } else {
151  $sText = str_replace( $aReplace, $aSearch, $sText );
152  }
153 
154  return $sText;
155  }
156 
162  public function import()
163  {
164  $this->_beforeImport();
165 
166  do {
167  while( $this->_importOne() );
168  } while ( !$this->_afterImport() );
169  }
170 
176  protected function _beforeImport()
177  {
178  if ( !$this->_iRetryRows ) {
179  //convert all text
180  foreach ($this->_aData as $key => $value) {
181  $this->_aData[$key] = $this->_csvTextConvert($value, false);
182  }
183  }
184 
185  }
186 
192  protected function _afterImport()
193  {
194  //check if there have been no errors or failures
195  $aStatistics = $this->getStatistics();
196  $iRetryRows = 0;
197 
198  foreach ( $aStatistics as $key => $value) {
199  if ( $value['r'] == false ) {
200  $iRetryRows++;
201  $this->_sReturn .= "File[".$this->_sPath."] - dataset number: $key - Error: ".$value['m']." ---<br> ".PHP_EOL;
202  }
203  }
204 
205  if ( $iRetryRows != $this->_iRetryRows && $iRetryRows>0 ) {
206  $this->_resetIdx();
207  $this->_iRetryRows = $iRetryRows;
208  $this->_sReturn = '';
209 
210  return false;
211  }
212 
213  return true;
214  }
215 
223  public function getImportData($iIdx = null)
224  {
225  return $this->_aData[$this->_iIdx];
226  }
227 
234  private function getOldOrderArticleFieldList()
235  {
236  $aFieldList = array(
237  'OXID' => 'OXID',
238  'OXORDERID' => 'OXORDERID',
239  'OXAMOUNT' => 'OXAMOUNT',
240  'OXARTID' => 'OXARTID',
241  'OXARTNUM' => 'OXARTNUM',
242  'OXTITLE' => 'OXTITLE',
243  'OXSELVARIANT' => 'OXSELVARIANT',
244  'OXNETPRICE' => 'OXNETPRICE',
245  'OXBRUTPRICE' => 'OXBRUTPRICE',
246  'OXVAT' => 'OXVAT',
247  'OXPERSPARAM' => 'OXPERSPARAM',
248  'OXPRICE' => 'OXPRICE',
249  'OXBPRICE' => 'OXBPRICE',
250  'OXTPRICE' => 'OXTPRICE',
251  'OXWRAPID' => 'OXWRAPID',
252  'OXSTOCK' => 'OXSTOCK',
253  'OXORDERSHOPID' => 'OXORDERSHOPID',
254  'OXTOTALVAT' => 'OXTOTALVAT'
255  );
256 
257  return $aFieldList;
258  }
259 
266  private function getOldOrderFielsList()
267  {
268  $aFieldList = array(
269  'OXID' => 'OXID',
270  'OXSHOPID' => 'OXSHOPID',
271  'OXUSERID' => 'OXUSERID',
272  'OXORDERDATE' => 'OXORDERDATE',
273  'OXORDERNR' => 'OXORDERNR',
274  'OXBILLCOMPANY' => 'OXBILLCOMPANY',
275  'OXBILLEMAIL' => 'OXBILLEMAIL',
276  'OXBILLFNAME' => 'OXBILLFNAME',
277  'OXBILLLNAME' => 'OXBILLLNAME',
278  'OXBILLSTREET' => 'OXBILLSTREET',
279  'OXBILLSTREETNR' => 'OXBILLSTREETNR',
280  'OXBILLADDINFO' => 'OXBILLADDINFO',
281  'OXBILLUSTID' => 'OXBILLUSTID',
282  'OXBILLCITY' => 'OXBILLCITY',
283  'OXBILLCOUNTRY' => 'OXBILLCOUNTRY',
284  'OXBILLZIP' => 'OXBILLZIP',
285  'OXBILLFON' => 'OXBILLFON',
286  'OXBILLFAX' => 'OXBILLFAX',
287  'OXBILLSAL' => 'OXBILLSAL',
288  'OXDELCOMPANY' => 'OXDELCOMPANY',
289  'OXDELFNAME' => 'OXDELFNAME',
290  'OXDELLNAME' => 'OXDELLNAME',
291  'OXDELSTREET' => 'OXDELSTREET',
292  'OXDELSTREETNR' => 'OXDELSTREETNR',
293  'OXDELADDINFO' => 'OXDELADDINFO',
294  'OXDELCITY' => 'OXDELCITY',
295  'OXDELCOUNTRY' => 'OXDELCOUNTRY',
296  'OXDELZIP' => 'OXDELZIP',
297  'OXDELFON' => 'OXDELFON',
298  'OXDELFAX' => 'OXDELFAX',
299  'OXDELSAL' => 'OXDELSAL',
300  'OXDELCOST' => 'OXDELCOST',
301  'OXDELVAT' => 'OXDELVAT',
302  'OXPAYCOST' => 'OXPAYCOST',
303  'OXPAYVAT' => 'OXPAYVAT',
304  'OXWRAPCOST' => 'OXWRAPCOST',
305  'OXWRAPVAT' => 'OXWRAPVAT',
306  'OXCARDID' => 'OXCARDID',
307  'OXCARDTEXT' => 'OXCARDTEXT',
308  'OXDISCOUNT' => 'OXDISCOUNT',
309  'OXBILLNR' => 'OXBILLNR',
310  'OXREMARK' => 'OXREMARK',
311  'OXVOUCHERDISCOUNT' => 'OXVOUCHERDISCOUNT',
312  'OXCURRENCY' => 'OXCURRENCY',
313  'OXCURRATE' => 'OXCURRATE',
314  'OXTRANSID' => 'OXTRANSID',
315  'OXPAID' => 'OXPAID',
316  'OXIP' => 'OXIP',
317  'OXTRANSSTATUS' => 'OXTRANSSTATUS',
318  'OXLANG' => 'OXLANG',
319  'OXDELTYPE' => 'OXDELTYPE'
320  );
321 
322  return $aFieldList;
323  }
324 
332  protected function _checkIDField( $sID )
333  {
334  if ( !isset( $sID ) || !$sID ) {
335  throw new Exception("ERROR: Articlenumber/ID missing!");
336  } elseif ( strlen( $sID) > 32 ) {
337  throw new Exception( "ERROR: Articlenumber/ID longer then allowed (32 chars max.)!");
338  }
339  }
340 
348  protected function _getInstanceOfType( $sType)
349  {
350  //due to backward compatibility
351  if ( $sType == 'oldOrder' ) {
352  $oType = parent::_getInstanceOfType('order');
353  $oType->setFieldList($this->getOldOrderFielsList());
354  $oType->setFunctionSuffix('OldOrder');
355  } elseif ( $sType == 'oldOrderArticle' ) {
356  $oType = parent::_getInstanceOfType('orderarticle');
357  $oType->setFieldList($this->getOldOrderArticleFieldList());
358  $oType->setFunctionSuffix('OldOrderArticle');
359  } elseif ( $sType == 'article2vendor' ) {
360  $oType = parent::_getInstanceOfType('article');
361  $oType->setFieldList(array("OXID", "OXVENDORID"));
362  } elseif ( $sType == 'mainarticle2categroy') {
363  $oType = parent::_getInstanceOfType('article2category');
364  $oType->setFieldList(array("OXOBJECTID", "OXCATNID", "OXTIME"));
365  $oType->setFunctionSuffix('mainarticle2category');
366  } else {
367  $oType = parent::_getInstanceOfType($sType);
368  }
369 
370  return $oType;
371  }
372 
373  // --------------------------------------------------------------------------
374  //
375  // Import Handler
376  // One _Import* method needed for each object defined in /objects/ folder, all these objects can be imported
377  //
378  // --------------------------------------------------------------------------
379 
388  protected function _importArticle( oxERPType $oType, $aRow)
389  {
390  if ( $this->_sCurrVersion == "0.1" ) {
392  //to allow different shopid without consequences (ignored fields)
393  $myConfig->setConfigParam('blMallCustomPrice', false);
394  }
395 
396  if ( isset($aRow['OXID'] ) ) {
397  $this->_checkIDField($aRow['OXID']);
398  }
399  // #0004426
400  /*else {
401  $this->_checkIDField($aRow['OXARTNUM']);
402  $aRow['OXID'] = $aRow['OXARTNUM'];
403  }*/
404 
405  $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1"); // V0.1 allowes the shopid to be set no matter which login
406  return $sResult;
407  }
408 
417  protected function _importAccessoire( oxERPType $oType, $aRow)
418  {
419  // deleting old relations before import in V0.1
420  if ( $this->_sCurrVersion == "0.1" && !isset($this->_aImportedAccessoire2Article[$aRow['OXARTICLENID']] ) ) {
422  $oDb = oxDb::getDb();
423  $oDb->execute( "delete from oxaccessoire2article where oxarticlenid = ".$oDb->quote( $aRow['OXARTICLENID'] ) );
424  $this->_aImportedAccessoire2Article[$aRow['OXARTICLENID']] = 1;
425  }
426 
427  $sResult = $this->_save( $oType, $aRow);
428  return $sResult;
429  }
430 
439  protected function _importArticle2Action( oxERPType $oType, $aRow)
440  {
441 
442  if ( $this->_sCurrVersion == "0.1" && !isset( $this->_aImportedActions2Article[$aRow['OXARTID']] ) ) {
443  //only in V0.1 and only once per import/article
445  $oDb = oxDb::getDb();
446  $oDb->execute( "delete from oxactions2article where oxartid = ".$oDb->quote( $aRow['OXARTID'] ) );
447  $this->_aImportedActions2Article[$aRow['OXARTID']] = 1;
448  }
449 
450  $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
451  return $sResult;
452  }
453 
462  protected function _importArticle2Category( oxERPType $oType, $aRow)
463  {
464  // deleting old relations before import in V0.1
465  if ( $this->_sCurrVersion == "0.1" && !isset( $this->_aImportedObject2Category[$aRow['OXOBJECTID']] ) ) {
467  $oDb = oxDb::getDb();
468  $oDb->execute( "delete from oxobject2category where oxobjectid = ".$oDb->quote( $aRow['OXOBJECTID'] ) );
469  $this->_aImportedObject2Category[$aRow['OXOBJECTID']] = 1;
470  }
471 
472  $sResult = $this->_save( $oType, $aRow);
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  return $sResult;
515  }
516 
525  protected function _importCrossselling( oxERPType $oType, $aRow)
526  {
527  // deleting old relations before import in V0.1
528  if ( $this->_sCurrVersion == "0.1" && !isset($this->_aImportedObject2Article[$aRow['OXARTICLENID']] ) ) {
530  $oDb = oxDb::getDb();
531  $oDb->Execute( "delete from oxobject2article where oxarticlenid = ".$oDb->quote( $aRow['OXARTICLENID'] ) );
532  $this->aImportedObject2Article[$aRow['OXARTICLENID']] = 1;
533  }
534 
535  $sResult = $this->_save( $oType, $aRow);
536  return $sResult;
537  }
538 
547  protected function _importScaleprice( oxERPType $oType, $aRow)
548  {
549  $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
550  return $sResult;
551  }
552 
561  protected function _importOrder( oxERPType $oType, $aRow)
562  {
563  $sResult = $this->_save( $oType, $aRow);
564  return $sResult;
565  //MAFI a unavoidable hack as oxorder->update() does always return null !!! a hotfix is needed
566  //hotfix was added? since it's working with proper return now
567  }
568 
577  protected function _importOrderArticle( oxERPType $oType, $aRow)
578  {
579  $sResult = $this->_save( $oType, $aRow);
580  return $sResult;
581  }
582 
591  protected function _importOrderStatus( oxERPType $oType, $aRow)
592  {
593  $oOrderArt = oxNew( "oxorderarticle", "core");
594  $oOrderArt->load( $aRow['OXID']);
595 
596  if ( $oOrderArt->getId()) {
597 
598  try {
599  if ( $this->_sCurrVersion != "0.1") {
600  $oType->checkWriteAccess($oOrderArt->getId());
601  }
602 
603  // store status
604  $aStatuses = unserialize( $oOrderArt->oxorderarticles__oxerpstatus->value );
605 
606  $oStatus = new stdClass();
607  $oStatus->STATUS = $aRow['OXERPSTATUS_STATUS'];
608  $oStatus->date = $aRow['OXERPSTATUS_TIME'];
609  $oStatus->trackingid = $aRow['OXERPSTATUS_TRACKID'];
610 
611  $aStatuses[$aRow['OXERPSTATUS_TIME']] = $oStatus;
612  $oOrderArt->oxorderarticles__oxerpstatus = new oxField(serialize( $aStatuses), oxField::T_RAW);
613  $oOrderArt->save();
614  return true;
615  } catch (Exception $ex) {
616  return false;
617  }
618  }
619 
620  return false;
621  }
622 
631  protected function _importUser( oxERPType $oType, $aRow)
632  {
633  //Speciall check for user
634  if ( isset($aRow['OXUSERNAME'] ) ) {
635  $sID = $aRow['OXID'];
636  $sUserName = $aRow['OXUSERNAME'];
637 
638  $oUser = oxNew( "oxuser", "core");
639  $oUser->oxuser__oxusername = new oxField($sUserName, oxField::T_RAW);
640 
641  //If user exists with and modifies OXID, throw an axception
642  //throw new Exception( "USER {$sUserName} already exists!");
643  if ( $oUser->exists( $sID) && $sID != $oUser->getId() ) {
644  throw new Exception( "USER $sUserName already exists!");
645  }
646 
647  }
648 
649  $sResult = $this->_save( $oType, $aRow);
650  return $sResult;
651  }
652 
661  protected function _importVendor( oxERPType $oType, $aRow)
662  {
663  $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
664  return $sResult;
665  }
666 
675  protected function _importArtextends( oxERPType $oType, $aRow)
676  {
677  if ( oxERPBase::getRequestedVersion() < 2 ) {
678  return false;
679  }
680  $sResult = $this->_save( $oType, $aRow);
681  return $sResult;
682  }
683 
692  protected function _importCountry( oxERPType $oType, $aRow)
693  {
694  $sResult = $this->_save( $oType, $aRow);
695  return $sResult;
696  }
697 
706  protected function _importArticleStock( oxERPType $oType, $aRow )
707  {
708  $sResult = $this->_save( $oType, $aRow);
709  return $sResult;
710  }
711 
716  public function getImportedRowCount()
717  {
718  return count ( $this->_aImportedIds );
719  }
720 
727  public function setImportedIds( $key )
728  {
729  if ( !array_key_exists( $key, $this->_aImportedIds ) )
730  $this->_aImportedIds[$key] = true;
731  }
732 }