oxerpcsv.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxErpCsv extends oxERPBase
00007 {
00012     protected $_aSupportedVersions = array("0.1", "1.0", "1.1", "2.0");
00013 
00018     protected $_aCsv2BaseVersionsMap = array("0.1" => "1", "1.0" => "1", "1.1"=>"1.1", "2.0" => "2");
00019 
00024     protected $_sCurrVersion = "";
00025 
00030     protected $_aData = array();
00031 
00036     protected $_iRetryRows = 0;
00037 
00042     protected $_sReturn;
00043 
00048     protected $_sPath;
00049 
00054     protected $_aImportedActions2Article = array();
00055 
00060     protected $_aImportedObject2Category = array();
00061 
00066     protected $_aImportedAccessoire2Article = array();
00067 
00075     protected function _beforeExport($sType)
00076     {
00077     }
00078 
00086     protected function _afterExport($sType)
00087     {
00088     }
00089 
00097     protected function _getImportType( &$aData )
00098     {
00099     }
00100 
00108     protected function _getImportMode( $aData )
00109     {
00110     }
00111 
00120     protected function _modifyData($aData, $oType)
00121     {
00122     }
00123 
00131     public function loadSessionData( $sSessionID )
00132     {
00133     }
00134 
00143     protected function _csvTextConvert( $sText, $blMode )
00144     {
00145         $aSearch  = array( chr(13), chr(10), '\'', '"' );
00146         $aReplace = array( '&#13;', '&#10;', '&#39;', '&#34;' );
00147 
00148         if ( $blMode ) {
00149             $sText = str_replace( $aSearch, $aReplace, $sText );
00150         } else {
00151             $sText = str_replace( $aReplace, $aSearch, $sText );
00152         }
00153 
00154         return $sText;
00155     }
00156 
00162     public function import()
00163     {
00164         $this->_beforeImport();
00165 
00166         do {
00167             while( $this->_importOne() );
00168         } while ( !$this->_afterImport() );
00169     }
00170 
00176     protected function _beforeImport()
00177     {
00178         if ( !$this->_iRetryRows ) {
00179             //convert all text
00180             foreach ($this->_aData as $key => $value) {
00181                 $this->_aData[$key] = $this->_csvTextConvert($value, false);
00182             }
00183         }
00184 
00185     }
00186 
00192     protected function _afterImport()
00193     {
00194         //check if there have been no errors or failures
00195         $aStatistics = $this->getStatistics();
00196         $iRetryRows  = 0;
00197 
00198         foreach ( $aStatistics as $key => $value) {
00199             if ( $value['r'] == false ) {
00200                 $iRetryRows++;
00201                 $this->_sReturn .= "File[".$this->_sPath."] - dataset number: $key - Error: ".$value['m']." ---<br> ".PHP_EOL;
00202             }
00203         }
00204 
00205         if ( $iRetryRows != $this->_iRetryRows && $iRetryRows>0 ) {
00206             $this->_resetIdx();
00207             $this->_iRetryRows = $iRetryRows;
00208             $this->_sReturn    = '';
00209 
00210             return false;
00211         }
00212 
00213         return true;
00214     }
00215 
00223     public function getImportData($iIdx = null)
00224     {
00225         return $this->_aData[$this->_iIdx];
00226     }
00227 
00234     private function getOldOrderArticleFieldList()
00235     {
00236         $aFieldList = array(
00237             'OXID'          => 'OXID',
00238             'OXORDERID'     => 'OXORDERID',
00239             'OXAMOUNT'      => 'OXAMOUNT',
00240             'OXARTID'       => 'OXARTID',
00241             'OXARTNUM'      => 'OXARTNUM',
00242             'OXTITLE'       => 'OXTITLE',
00243             'OXSELVARIANT'  => 'OXSELVARIANT',
00244             'OXNETPRICE'    => 'OXNETPRICE',
00245             'OXBRUTPRICE'   => 'OXBRUTPRICE',
00246             'OXVAT'         => 'OXVAT',
00247             'OXPERSPARAM'   => 'OXPERSPARAM',
00248             'OXPRICE'       => 'OXPRICE',
00249             'OXBPRICE'      => 'OXBPRICE',
00250             'OXTPRICE'      => 'OXTPRICE',
00251             'OXWRAPID'      => 'OXWRAPID',
00252             'OXSTOCK'       =>  'OXSTOCK',
00253             'OXORDERSHOPID' => 'OXORDERSHOPID',
00254             'OXTOTALVAT'    => 'OXTOTALVAT'
00255         );
00256 
00257         return $aFieldList;
00258     }
00259 
00266     private function getOldOrderFielsList()
00267     {
00268          $aFieldList = array(
00269             'OXID'             => 'OXID',
00270             'OXSHOPID'         => 'OXSHOPID',
00271             'OXUSERID'         => 'OXUSERID',
00272             'OXORDERDATE'     => 'OXORDERDATE',
00273             'OXORDERNR'         => 'OXORDERNR',
00274             'OXBILLCOMPANY'     => 'OXBILLCOMPANY',
00275             'OXBILLEMAIL'     => 'OXBILLEMAIL',
00276             'OXBILLFNAME'     => 'OXBILLFNAME',
00277             'OXBILLLNAME'     => 'OXBILLLNAME',
00278             'OXBILLSTREET'     => 'OXBILLSTREET',
00279             'OXBILLSTREETNR' => 'OXBILLSTREETNR',
00280             'OXBILLADDINFO'     => 'OXBILLADDINFO',
00281             'OXBILLUSTID'     => 'OXBILLUSTID',
00282             'OXBILLCITY'     => 'OXBILLCITY',
00283             'OXBILLCOUNTRY'     => 'OXBILLCOUNTRY',
00284             'OXBILLZIP'         => 'OXBILLZIP',
00285             'OXBILLFON'         => 'OXBILLFON',
00286             'OXBILLFAX'         => 'OXBILLFAX',
00287             'OXBILLSAL'         => 'OXBILLSAL',
00288             'OXDELCOMPANY'     => 'OXDELCOMPANY',
00289             'OXDELFNAME'     => 'OXDELFNAME',
00290             'OXDELLNAME'     => 'OXDELLNAME',
00291             'OXDELSTREET'     => 'OXDELSTREET',
00292             'OXDELSTREETNR'     => 'OXDELSTREETNR',
00293             'OXDELADDINFO'     => 'OXDELADDINFO',
00294             'OXDELCITY'         => 'OXDELCITY',
00295             'OXDELCOUNTRY'     => 'OXDELCOUNTRY',
00296             'OXDELZIP'         => 'OXDELZIP',
00297             'OXDELFON'         => 'OXDELFON',
00298             'OXDELFAX'         => 'OXDELFAX',
00299             'OXDELSAL'         => 'OXDELSAL',
00300             'OXDELCOST'         => 'OXDELCOST',
00301             'OXDELVAT'         => 'OXDELVAT',
00302             'OXPAYCOST'         => 'OXPAYCOST',
00303             'OXPAYVAT'         => 'OXPAYVAT',
00304             'OXWRAPCOST'     => 'OXWRAPCOST',
00305             'OXWRAPVAT'         => 'OXWRAPVAT',
00306             'OXCARDID'         => 'OXCARDID',
00307             'OXCARDTEXT'     => 'OXCARDTEXT',
00308             'OXDISCOUNT'     => 'OXDISCOUNT',
00309             'OXBILLNR'         => 'OXBILLNR',
00310             'OXREMARK'         => 'OXREMARK',
00311             'OXVOUCHERDISCOUNT'         => 'OXVOUCHERDISCOUNT',
00312             'OXCURRENCY'     => 'OXCURRENCY',
00313             'OXCURRATE'         => 'OXCURRATE',
00314             'OXTRANSID'         => 'OXTRANSID',
00315             'OXPAID'         => 'OXPAID',
00316             'OXIP'             => 'OXIP',
00317             'OXTRANSSTATUS'     => 'OXTRANSSTATUS',
00318             'OXLANG'         => 'OXLANG',
00319             'OXDELTYPE'         => 'OXDELTYPE'
00320             );
00321 
00322             return $aFieldList;
00323     }
00324 
00332     protected  function _checkIDField( $sID )
00333     {
00334         if ( !isset( $sID ) || !$sID ) {
00335             throw new Exception("ERROR: Articlenumber/ID missing!");
00336         } elseif ( strlen( $sID) > 32 ) {
00337             throw new Exception( "ERROR: Articlenumber/ID longer then allowed (32 chars max.)!");
00338         }
00339     }
00340 
00348     protected function _getInstanceOfType( $sType)
00349     {
00350         //due to backward compatibility
00351         if ( $sType == 'oldOrder' ) {
00352             $oType = parent::_getInstanceOfType('order');
00353             $oType->setFieldList($this->getOldOrderFielsList());
00354             $oType->setFunctionSuffix('OldOrder');
00355         } elseif ( $sType == 'oldOrderArticle' ) {
00356             $oType = parent::_getInstanceOfType('orderarticle');
00357             $oType->setFieldList($this->getOldOrderArticleFieldList());
00358             $oType->setFunctionSuffix('OldOrderArticle');
00359         } elseif ( $sType == 'article2vendor' ) {
00360             $oType = parent::_getInstanceOfType('article');
00361             $oType->setFieldList(array("OXID", "OXVENDORID"));
00362         } elseif ( $sType == 'mainarticle2categroy') {
00363             $oType = parent::_getInstanceOfType('article2category');
00364             $oType->setFieldList(array("OXOBJECTID", "OXCATNID", "OXTIME"));
00365             $oType->setFunctionSuffix('mainarticle2category');
00366         } else {
00367             $oType = parent::_getInstanceOfType($sType);
00368         }
00369 
00370         return $oType;
00371     }
00372 
00373     // --------------------------------------------------------------------------
00374     //
00375     // Import Handler
00376     // One _Import* method needed for each object defined in /objects/ folder, all these objects  can be imported
00377     //
00378     // --------------------------------------------------------------------------
00379 
00388     protected function _importArticle( oxERPType $oType, $aRow)
00389     {
00390         if ( $this->_sCurrVersion == "0.1" ) {
00391             $myConfig = oxRegistry::getConfig();
00392             //to allow different shopid without consequences (ignored fields)
00393             $myConfig->setConfigParam('blMallCustomPrice', false);
00394         }
00395 
00396         if ( isset($aRow['OXID'] ) ) {
00397             $this->_checkIDField($aRow['OXID']);
00398         }
00399         // #0004426
00400         /*else {
00401             $this->_checkIDField($aRow['OXARTNUM']);
00402             $aRow['OXID'] = $aRow['OXARTNUM'];
00403         }*/
00404 
00405         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1"); // V0.1 allowes the shopid to be set no matter which login
00406         return $sResult;
00407     }
00408 
00417     protected function _importAccessoire( oxERPType $oType, $aRow)
00418     {
00419         // deleting old relations before import in V0.1
00420         if ( $this->_sCurrVersion == "0.1" && !isset($this->_aImportedAccessoire2Article[$aRow['OXARTICLENID']] ) ) {
00421             $myConfig = oxRegistry::getConfig();
00422             $oDb = oxDb::getDb();
00423             $oDb->execute( "delete from oxaccessoire2article where oxarticlenid = ".$oDb->quote( $aRow['OXARTICLENID'] ) );
00424             $this->_aImportedAccessoire2Article[$aRow['OXARTICLENID']] = 1;
00425         }
00426 
00427         $sResult = $this->_save( $oType, $aRow);
00428         return $sResult;
00429     }
00430 
00439     protected function _importArticle2Action( oxERPType $oType, $aRow)
00440     {
00441 
00442         if ( $this->_sCurrVersion == "0.1" && !isset( $this->_aImportedActions2Article[$aRow['OXARTID']] ) ) {
00443             //only in V0.1 and only once per import/article
00444             $myConfig = oxRegistry::getConfig();
00445             $oDb = oxDb::getDb();
00446             $oDb->execute( "delete from oxactions2article where oxartid = ".$oDb->quote( $aRow['OXARTID'] ) );
00447             $this->_aImportedActions2Article[$aRow['OXARTID']] = 1;
00448         }
00449 
00450         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00451         return $sResult;
00452     }
00453 
00462     protected function _importArticle2Category( oxERPType $oType, $aRow)
00463     {
00464         // deleting old relations before import in V0.1
00465         if ( $this->_sCurrVersion == "0.1" && !isset( $this->_aImportedObject2Category[$aRow['OXOBJECTID']] ) ) {
00466             $myConfig = oxRegistry::getConfig();
00467             $oDb = oxDb::getDb();
00468             $oDb->execute( "delete from oxobject2category where oxobjectid = ".$oDb->quote( $aRow['OXOBJECTID'] ) );
00469             $this->_aImportedObject2Category[$aRow['OXOBJECTID']] = 1;
00470         }
00471 
00472         $sResult = $this->_save( $oType, $aRow);
00473         return $sResult;
00474     }
00475 
00484     protected function _importMainArticle2Category( oxERPType $oType, $aRow)
00485     {
00486         $aRow['OXTIME'] = 0;
00487 
00488         $myConfig = oxRegistry::getConfig();
00489         $oDb = oxDb::getDb();
00490 
00491         $sSql = "select OXID from oxobject2category where oxobjectid = ".$oDb->quote( $aRow['OXOBJECTID'] )." and OXCATNID = ".$oDb->quote( $aRow['OXCATNID'] );
00492         $aRow['OXID'] = $oDb->getOne( $sSql, false, false );
00493 
00494         $sResult = $this->_save( $oType, $aRow);
00495         if ((boolean) $sResult) {
00496             $sSql = "Update oxobject2category set oxtime = oxtime+10 where oxobjectid = ".$oDb->quote( $aRow['OXOBJECTID'] ) ." and oxcatnid != ".$oDb->quote( $aRow['OXCATNID'] ) ." and oxshopid = '".$myConfig->getShopId()."'";
00497             $oDb->Execute($sSql);
00498         }
00499 
00500         return $sResult;
00501     }
00502 
00511     protected function _importCategory( oxERPType $oType, $aRow)
00512     {
00513         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00514         return $sResult;
00515     }
00516 
00525     protected function _importCrossselling( oxERPType $oType, $aRow)
00526     {
00527         // deleting old relations before import in V0.1
00528         if ( $this->_sCurrVersion == "0.1" && !isset($this->_aImportedObject2Article[$aRow['OXARTICLENID']] ) ) {
00529             $myConfig = oxRegistry::getConfig();
00530             $oDb = oxDb::getDb();
00531             $oDb->Execute( "delete from oxobject2article where oxarticlenid = ".$oDb->quote( $aRow['OXARTICLENID'] ) );
00532             $this->aImportedObject2Article[$aRow['OXARTICLENID']] = 1;
00533         }
00534 
00535         $sResult = $this->_save( $oType, $aRow);
00536         return $sResult;
00537     }
00538 
00547     protected function _importScaleprice( oxERPType $oType, $aRow)
00548     {
00549         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00550         return $sResult;
00551     }
00552 
00561     protected function _importOrder( oxERPType $oType, $aRow)
00562     {
00563         $sResult = $this->_save( $oType, $aRow);
00564         return $sResult;
00565         //MAFI a unavoidable hack as oxorder->update() does always return null !!! a hotfix is needed
00566         //hotfix was added? since it's working with proper return now
00567     }
00568 
00577     protected function _importOrderArticle( oxERPType $oType, $aRow)
00578     {
00579         $sResult = $this->_save( $oType, $aRow);
00580         return $sResult;
00581     }
00582 
00591     protected function _importOrderStatus( oxERPType $oType, $aRow)
00592     {
00593         $oOrderArt = oxNew( "oxorderarticle", "core");
00594         $oOrderArt->load( $aRow['OXID']);
00595 
00596         if ( $oOrderArt->getId()) {
00597 
00598             try {
00599                 if ( $this->_sCurrVersion != "0.1") {
00600                     $oType->checkWriteAccess($oOrderArt->getId());
00601                 }
00602 
00603                     // store status
00604                 $aStatuses = unserialize( $oOrderArt->oxorderarticles__oxerpstatus->value );
00605 
00606                 $oStatus = new stdClass();
00607                 $oStatus->STATUS = $aRow['OXERPSTATUS_STATUS'];
00608                 $oStatus->date = $aRow['OXERPSTATUS_TIME'];
00609                 $oStatus->trackingid = $aRow['OXERPSTATUS_TRACKID'];
00610 
00611                 $aStatuses[$aRow['OXERPSTATUS_TIME']] = $oStatus;
00612                 $oOrderArt->oxorderarticles__oxerpstatus = new oxField(serialize( $aStatuses), oxField::T_RAW);
00613                 $oOrderArt->save();
00614                 return true;
00615             } catch (Exception $ex) {
00616                 return false;
00617             }
00618         }
00619 
00620         return false;
00621     }
00622 
00631     protected function _importUser( oxERPType $oType, $aRow)
00632     {
00633         //Speciall check for user
00634         if ( isset($aRow['OXUSERNAME'] ) ) {
00635             $sID = $aRow['OXID'];
00636             $sUserName = $aRow['OXUSERNAME'];
00637 
00638             $oUser = oxNew( "oxuser", "core");
00639             $oUser->oxuser__oxusername = new oxField($sUserName, oxField::T_RAW);
00640 
00641             //If user exists with and modifies OXID, throw an axception
00642             //throw new Exception( "USER {$sUserName} already exists!");
00643             if ( $oUser->exists( $sID) && $sID != $oUser->getId() ) {
00644                 throw new Exception( "USER $sUserName already exists!");
00645             }
00646 
00647         }
00648 
00649         $sResult  = $this->_save( $oType, $aRow);
00650         return $sResult;
00651     }
00652 
00661     protected function _importVendor( oxERPType $oType, $aRow)
00662     {
00663         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00664         return $sResult;
00665     }
00666 
00675     protected function _importArtextends( oxERPType $oType, $aRow)
00676     {
00677         if ( oxERPBase::getRequestedVersion() < 2 ) {
00678             return false;
00679         }
00680         $sResult = $this->_save( $oType, $aRow);
00681         return $sResult;
00682     }
00683 
00692     protected function _importCountry( oxERPType $oType, $aRow)
00693     {
00694         $sResult = $this->_save( $oType, $aRow);
00695         return $sResult;
00696     }
00697 
00706     protected function _importArticleStock( oxERPType $oType, $aRow )
00707     {
00708         $sResult = $this->_save( $oType, $aRow);
00709         return $sResult;
00710     }
00711 
00716     public function getImportedRowCount()
00717     {
00718         return count ( $this->_aImportedIds );
00719     }
00720 
00727     public function setImportedIds( $key )
00728     {
00729         if ( !array_key_exists( $key, $this->_aImportedIds ) )
00730             $this->_aImportedIds[$key] = true;
00731     }
00732 }