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 = oxConfig::getInstance();
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         } else {
00399             $this->_checkIDField($aRow['OXARTNUM']);
00400             $aRow['OXID'] = $aRow['OXARTNUM'];
00401         }
00402 
00403         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1"); // V0.1 allowes the shopid to be set no matter which login
00404         return (boolean) $sResult;
00405     }
00406 
00415     protected function _importAccessoire( oxERPType $oType, $aRow)
00416     {
00417         // deleting old relations before import in V0.1
00418         if ( $this->_sCurrVersion == "0.1" && !isset($this->_aImportedAccessoire2Article[$aRow['OXARTICLENID']] ) ) {
00419             $myConfig = oxConfig::getInstance();
00420             $oDb = oxDb::getDb();
00421             $oDb->execute( "delete from oxaccessoire2article where oxarticlenid = ".$oDb->quote( $aRow['OXARTICLENID'] ) );
00422             $this->_aImportedAccessoire2Article[$aRow['OXARTICLENID']] = 1;
00423         }
00424 
00425         $sResult = $this->_save( $oType, $aRow);
00426         return (boolean) $sResult;
00427     }
00428 
00437     protected function _importArticle2Action( oxERPType $oType, $aRow)
00438     {
00439 
00440         if ( $this->_sCurrVersion == "0.1" && !isset( $this->_aImportedActions2Article[$aRow['OXARTID']] ) ) {
00441             //only in V0.1 and only once per import/article
00442             $myConfig = oxConfig::getInstance();
00443             $oDb = oxDb::getDb();
00444             $oDb->execute( "delete from oxactions2article where oxartid = ".$oDb->quote( $aRow['OXARTID'] ) );
00445             $this->_aImportedActions2Article[$aRow['OXARTID']] = 1;
00446         }
00447 
00448         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00449         return (boolean) $sResult;
00450     }
00451 
00460     protected function _importArticle2Category( oxERPType $oType, $aRow)
00461     {
00462         // deleting old relations before import in V0.1
00463         if ( $this->_sCurrVersion == "0.1" && !isset( $this->_aImportedObject2Category[$aRow['OXOBJECTID']] ) ) {
00464             $myConfig = oxConfig::getInstance();
00465             $oDb = oxDb::getDb();
00466             $oDb->execute( "delete from oxobject2category where oxobjectid = ".$oDb->quote( $aRow['OXOBJECTID'] ) );
00467             $this->_aImportedObject2Category[$aRow['OXOBJECTID']] = 1;
00468         }
00469 
00470         $sResult = $this->_save( $oType, $aRow);
00471         return (boolean) $sResult;
00472     }
00473 
00482     protected function _importMainArticle2Category( oxERPType $oType, $aRow)
00483     {
00484         $aRow['OXTIME'] = 0;
00485 
00486         $myConfig = oxConfig::getInstance();
00487         $oDb = oxDb::getDb();
00488 
00489         $sSql = "select OXID from oxobject2category where oxobjectid = ".$oDb->quote( $aRow['OXOBJECTID'] )." and OXCATNID = ".$oDb->quote( $aRow['OXCATNID'] );
00490         $aRow['OXID'] = $oDb->GetOne($sSql);
00491 
00492         $sResult = $this->_save( $oType, $aRow);
00493         if ((boolean) $sResult) {
00494             $sSql = "Update oxobject2category set oxtime = oxtime+10 where oxobjectid = ".$oDb->quote( $aRow['OXOBJECTID'] ) ." and oxcatnid != ".$oDb->quote( $aRow['OXCATNID'] ) ." and oxshopid = '".$myConfig->getShopId()."'";
00495             $oDb->Execute($sSql);
00496         }
00497 
00498         return (boolean) $sResult;
00499     }
00500 
00509     protected function _importCategory( oxERPType $oType, $aRow)
00510     {
00511         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00512         return (boolean) $sResult;
00513     }
00514 
00523     protected function _importCrossselling( oxERPType $oType, $aRow)
00524     {
00525         // deleting old relations before import in V0.1
00526         if ( $this->_sCurrVersion == "0.1" && !isset($this->_aImportedObject2Article[$aRow['OXARTICLENID']] ) ) {
00527             $myConfig = oxConfig::getInstance();
00528             $oDb = oxDb::getDb();
00529             $oDb->Execute( "delete from oxobject2article where oxarticlenid = ".$oDb->quote( $aRow['OXARTICLENID'] ) );
00530             $this->aImportedObject2Article[$aRow['OXARTICLENID']] = 1;
00531         }
00532 
00533         $sResult = $this->_save( $oType, $aRow);
00534         return (boolean) $sResult;
00535     }
00536 
00545     protected function _importScaleprice( oxERPType $oType, $aRow)
00546     {
00547         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00548         return (boolean) $sResult;
00549     }
00550 
00559     protected function _importOrder( oxERPType $oType, $aRow)
00560     {
00561         $sResult = $this->_save( $oType, $aRow);
00562         return true; //MAFI a unavoidable hack as oxorder->update() does always return null !!! a hotfix is needed
00563         //return (boolean) $sResult;
00564     }
00565 
00574     protected function _importOrderArticle( oxERPType $oType, $aRow)
00575     {
00576         $sResult = $this->_save( $oType, $aRow);
00577         return (boolean) $sResult;
00578     }
00579 
00588     protected function _importOrderStatus( oxERPType $oType, $aRow)
00589     {
00590         $oOrderArt = oxNew( "oxorderarticle", "core");
00591         $oOrderArt->load( $aRow['OXID']);
00592 
00593         if ( $oOrderArt->getId()) {
00594 
00595             try {
00596                 if ( $this->_sCurrVersion != "0.1") {
00597                     $oType->checkWriteAccess($oOrderArt->getId());
00598                 }
00599 
00600                     // store status
00601                 $aStatuses = unserialize( $oOrderArt->oxorderarticles__oxerpstatus->value );
00602 
00603                 $oStatus = new stdClass();
00604                 $oStatus->STATUS = $aRow['OXERPSTATUS_STATUS'];
00605                 $oStatus->date = $aRow['OXERPSTATUS_TIME'];
00606                 $oStatus->trackingid = $aRow['OXERPSTATUS_TRACKID'];
00607 
00608                 $aStatuses[$aRow['OXERPSTATUS_TIME']] = $oStatus;
00609                 $oOrderArt->oxorderarticles__oxerpstatus = new oxField(serialize( $aStatuses), oxField::T_RAW);
00610                 $oOrderArt->save();
00611                 return true;
00612             } catch (Exception $ex) {
00613                 return false;
00614             }
00615         }
00616 
00617         return false;
00618     }
00619 
00628     protected function _importUser( oxERPType $oType, $aRow)
00629     {
00630         //Speciall check for user
00631         if ( isset($aRow['OXUSERNAME'] ) ) {
00632             $sID = $aRow['OXID'];
00633             $sUserName = $aRow['OXUSERNAME'];
00634 
00635             $oUser = oxNew( "oxuser", "core");
00636             $oUser->oxuser__oxusername = new oxField($sUserName, oxField::T_RAW);
00637 
00638             //If user exists with and modifies OXID, throw an axception
00639             //throw new Exception( "USER {$sUserName} already exists!");
00640             if ( $oUser->exists( $sID) && $sID != $oUser->getId() ) {
00641                 throw new Exception( "USER $sUserName already exists!");
00642             }
00643 
00644         }
00645 
00646         $sResult  = $this->_save( $oType, $aRow);
00647         return (boolean) $sResult;
00648     }
00649 
00658     protected function _importVendor( oxERPType $oType, $aRow)
00659     {
00660         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00661         return (boolean) $sResult;
00662     }
00663 
00672     protected function _importArtextends( oxERPType $oType, $aRow)
00673     {
00674         if ( oxERPBase::getRequestedVersion() < 2 ) {
00675             return false;
00676         }
00677         $sResult = $this->_save( $oType, $aRow);
00678         return (boolean) $sResult;
00679     }
00680 
00689     protected function _importCountry( oxERPType $oType, $aRow)
00690     {
00691         $sResult = $this->_save( $oType, $aRow);
00692         return (boolean) $sResult;
00693     }
00694 
00703     protected function _importArticleStock( oxERPType $oType, $aRow )
00704     {
00705         $sResult = $this->_save( $oType, $aRow);
00706         return (boolean) $sResult;
00707     }
00708 }