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 
00221     public function getImportData()
00222     {
00223         return $this->_aData[$this->_iIdx];
00224     }
00225 
00232     private function getOldOrderArticleFieldList()
00233     {
00234         $aFieldList = array(
00235             'OXID'          => 'OXID',
00236             'OXORDERID'     => 'OXORDERID',
00237             'OXAMOUNT'      => 'OXAMOUNT',
00238             'OXARTID'       => 'OXARTID',
00239             'OXARTNUM'      => 'OXARTNUM',
00240             'OXTITLE'       => 'OXTITLE',
00241             'OXSELVARIANT'  => 'OXSELVARIANT',
00242             'OXNETPRICE'    => 'OXNETPRICE',
00243             'OXBRUTPRICE'   => 'OXBRUTPRICE',
00244             'OXVAT'         => 'OXVAT',
00245             'OXPERSPARAM'   => 'OXPERSPARAM',
00246             'OXPRICE'       => 'OXPRICE',
00247             'OXBPRICE'      => 'OXBPRICE',
00248             'OXTPRICE'      => 'OXTPRICE',
00249             'OXWRAPID'      => 'OXWRAPID',
00250             'OXSTOCK'       =>  'OXSTOCK',
00251             'OXORDERSHOPID' => 'OXORDERSHOPID',
00252             'OXTOTALVAT'    => 'OXTOTALVAT'
00253         );
00254 
00255         return $aFieldList;
00256     }
00257 
00264     private function getOldOrderFielsList()
00265     {
00266          $aFieldList = array(
00267             'OXID'           => 'OXID',
00268             'OXSHOPID'       => 'OXSHOPID',
00269             'OXUSERID'       => 'OXUSERID',
00270             'OXORDERDATE'    => 'OXORDERDATE',
00271             'OXORDERNR'      => 'OXORDERNR',
00272             'OXBILLCOMPANY'  => 'OXBILLCOMPANY',
00273             'OXBILLEMAIL'    => 'OXBILLEMAIL',
00274             'OXBILLFNAME'    => 'OXBILLFNAME',
00275             'OXBILLLNAME'    => 'OXBILLLNAME',
00276             'OXBILLSTREET'   => 'OXBILLSTREET',
00277             'OXBILLSTREETNR' => 'OXBILLSTREETNR',
00278             'OXBILLADDINFO'  => 'OXBILLADDINFO',
00279             'OXBILLUSTID'    => 'OXBILLUSTID',
00280             'OXBILLCITY'     => 'OXBILLCITY',
00281             'OXBILLCOUNTRY'  => 'OXBILLCOUNTRY',
00282             'OXBILLZIP'      => 'OXBILLZIP',
00283             'OXBILLFON'      => 'OXBILLFON',
00284             'OXBILLFAX'      => 'OXBILLFAX',
00285             'OXBILLSAL'      => 'OXBILLSAL',
00286             'OXDELCOMPANY'   => 'OXDELCOMPANY',
00287             'OXDELFNAME'     => 'OXDELFNAME',
00288             'OXDELLNAME'     => 'OXDELLNAME',
00289             'OXDELSTREET'    => 'OXDELSTREET',
00290             'OXDELSTREETNR'  => 'OXDELSTREETNR',
00291             'OXDELADDINFO'   => 'OXDELADDINFO',
00292             'OXDELCITY'      => 'OXDELCITY',
00293             'OXDELCOUNTRY'   => 'OXDELCOUNTRY',
00294             'OXDELZIP'       => 'OXDELZIP',
00295             'OXDELFON'       => 'OXDELFON',
00296             'OXDELFAX'       => 'OXDELFAX',
00297             'OXDELSAL'       => 'OXDELSAL',
00298             'OXDELCOST'      => 'OXDELCOST',
00299             'OXDELVAT'       => 'OXDELVAT',
00300             'OXPAYCOST'      => 'OXPAYCOST',
00301             'OXPAYVAT'       => 'OXPAYVAT',
00302             'OXWRAPCOST'     => 'OXWRAPCOST',
00303             'OXWRAPVAT'      => 'OXWRAPVAT',
00304             'OXCARDID'       => 'OXCARDID',
00305             'OXCARDTEXT'     => 'OXCARDTEXT',
00306             'OXDISCOUNT'     => 'OXDISCOUNT',
00307             'OXBILLNR'       => 'OXBILLNR',
00308             'OXREMARK'       => 'OXREMARK',
00309             'OXVOUCHERDISCOUNT'      => 'OXVOUCHERDISCOUNT',
00310             'OXCURRENCY'     => 'OXCURRENCY',
00311             'OXCURRATE'      => 'OXCURRATE',
00312             'OXTRANSID'      => 'OXTRANSID',
00313             'OXPAID'         => 'OXPAID',
00314             'OXIP'           => 'OXIP',
00315             'OXTRANSSTATUS'  => 'OXTRANSSTATUS',
00316             'OXLANG'         => 'OXLANG',
00317             'OXDELTYPE'      => 'OXDELTYPE'
00318             );
00319 
00320             return $aFieldList;
00321     }
00322 
00330     protected  function _checkIDField( $sID )
00331     {
00332         if ( !isset( $sID ) || !$sID ) {
00333             throw new Exception("ERROR: Articlenumber/ID missing!");
00334         } elseif ( strlen( $sID) > 32 ) {
00335             throw new Exception( "ERROR: Articlenumber/ID longer then allowed (32 chars max.)!");
00336         }
00337     }
00338 
00346     protected function _getInstanceOfType( $sType)
00347     {
00348         //due to backward compatibility
00349         if ( $sType == 'oldOrder' ) {
00350             $oType = parent::_getInstanceOfType('order');
00351             $oType->setFieldList($this->getOldOrderFielsList());
00352             $oType->setFunctionSuffix('OldOrder');
00353         } elseif ( $sType == 'oldOrderArticle' ) {
00354             $oType = parent::_getInstanceOfType('orderarticle');
00355             $oType->setFieldList($this->getOldOrderArticleFieldList());
00356             $oType->setFunctionSuffix('OldOrderArticle');
00357         } elseif ( $sType == 'article2vendor' ) {
00358             $oType = parent::_getInstanceOfType('article');
00359             $oType->setFieldList(array("OXID", "OXVENDORID"));
00360         } elseif ( $sType == 'mainarticle2categroy') {
00361             $oType = parent::_getInstanceOfType('article2category');
00362             $oType->setFieldList(array("OXOBJECTID", "OXCATNID", "OXTIME"));
00363             $oType->setFunctionSuffix('mainarticle2category');
00364         } else {
00365             $oType = parent::_getInstanceOfType($sType);
00366         }
00367 
00368         return $oType;
00369     }
00370 
00371     // --------------------------------------------------------------------------
00372     //
00373     // Import Handler
00374     // One _Import* method needed for each object defined in /objects/ folder, all these objects  can be imported
00375     //
00376     // --------------------------------------------------------------------------
00377 
00386     protected function _importArticle( oxERPType $oType, $aRow)
00387     {
00388         if ( $this->_sCurrVersion == "0.1" ) {
00389             $myConfig = oxConfig::getInstance();
00390             //to allow different shopid without consequences (ignored fields)
00391             $myConfig->setConfigParam('blMallCustomPrice', false);
00392         }
00393 
00394         if ( isset($aRow['OXID'] ) ) {
00395             $this->_checkIDField($aRow['OXID']);
00396         } else {
00397             $this->_checkIDField($aRow['OXARTNUM']);
00398             $aRow['OXID'] = $aRow['OXARTNUM'];
00399         }
00400 
00401         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1"); // V0.1 allowes the shopid to be set no matter which login
00402         return (boolean) $sResult;
00403     }
00404 
00413     protected function _importAccessoire( oxERPType $oType, $aRow)
00414     {
00415         // deleting old relations before import in V0.1
00416         if ( $this->_sCurrVersion == "0.1" && !isset($this->_aImportedAccessoire2Article[$aRow['OXARTICLENID']] ) ) {
00417             $myConfig = oxConfig::getInstance();
00418             $oDb = oxDb::getDb();
00419             $oDb->execute( "delete from oxaccessoire2article where oxarticlenid = ".$oDb->quote( $aRow['OXARTICLENID'] ) );
00420             $this->_aImportedAccessoire2Article[$aRow['OXARTICLENID']] = 1;
00421         }
00422 
00423         $sResult = $this->_save( $oType, $aRow);
00424         return (boolean) $sResult;
00425     }
00426 
00435     protected function _importArticle2Action( oxERPType $oType, $aRow)
00436     {
00437 
00438         if ( $this->_sCurrVersion == "0.1" && !isset( $this->_aImportedActions2Article[$aRow['OXARTID']] ) ) {
00439             //only in V0.1 and only once per import/article
00440             $myConfig = oxConfig::getInstance();
00441             $oDb = oxDb::getDb();
00442             $oDb->execute( "delete from oxactions2article where oxartid = ".$oDb->quote( $aRow['OXARTID'] ) );
00443             $this->_aImportedActions2Article[$aRow['OXARTID']] = 1;
00444         }
00445 
00446         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00447         return (boolean) $sResult;
00448     }
00449 
00458     protected function _importArticle2Category( oxERPType $oType, $aRow)
00459     {
00460         // deleting old relations before import in V0.1
00461         if ( $this->_sCurrVersion == "0.1" && !isset( $this->_aImportedObject2Category[$aRow['OXOBJECTID']] ) ) {
00462             $myConfig = oxConfig::getInstance();
00463             $oDb = oxDb::getDb();
00464             $oDb->execute( "delete from oxobject2category where oxobjectid = ".$oDb->quote( $aRow['OXOBJECTID'] ) );
00465             $this->_aImportedObject2Category[$aRow['OXOBJECTID']] = 1;
00466         }
00467 
00468         $sResult = $this->_save( $oType, $aRow);
00469         return (boolean) $sResult;
00470     }
00471 
00480     protected function _importMainArticle2Category( oxERPType $oType, $aRow)
00481     {
00482         $aRow['OXTIME'] = 0;
00483 
00484         $myConfig = oxConfig::getInstance();
00485         $oDb = oxDb::getDb();
00486 
00487         $sSql = "select OXID from oxobject2category where oxobjectid = ".$oDb->quote( $aRow['OXOBJECTID'] )." and OXCATNID = ".$oDb->quote( $aRow['OXCATNID'] );
00488         $aRow['OXID'] = $oDb->GetOne($sSql);
00489 
00490         $sResult = $this->_save( $oType, $aRow);
00491         if ((boolean) $sResult) {
00492             $sSql = "Update oxobject2category set oxtime = oxtime+10 where oxobjectid = ".$oDb->quote( $aRow['OXOBJECTID'] ) ." and oxcatnid != ".$oDb->quote( $aRow['OXCATNID'] ) ." and oxshopid = '".$myConfig->getShopId()."'";
00493             $oDb->Execute($sSql);
00494         }
00495 
00496         return (boolean) $sResult;
00497     }
00498 
00507     protected function _importCategory( oxERPType $oType, $aRow)
00508     {
00509         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00510         return (boolean) $sResult;
00511     }
00512 
00521     protected function _importCrossselling( oxERPType $oType, $aRow)
00522     {
00523         // deleting old relations before import in V0.1
00524         if ( $this->_sCurrVersion == "0.1" && !isset($this->_aImportedObject2Article[$aRow['OXARTICLENID']] ) ) {
00525             $myConfig = oxConfig::getInstance();
00526             $oDb = oxDb::getDb();
00527             $oDb->Execute( "delete from oxobject2article where oxarticlenid = ".$oDb->quote( $aRow['OXARTICLENID'] ) );
00528             $this->aImportedObject2Article[$aRow['OXARTICLENID']] = 1;
00529         }
00530 
00531         $sResult = $this->_save( $oType, $aRow);
00532         return (boolean) $sResult;
00533     }
00534 
00543     protected function _importScaleprice( oxERPType $oType, $aRow)
00544     {
00545         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00546         return (boolean) $sResult;
00547     }
00548 
00557     protected function _importOrder( oxERPType $oType, $aRow)
00558     {
00559         $sResult = $this->_save( $oType, $aRow);
00560         return true; //MAFI a unavoidable hack as oxorder->update() does always return null !!! a hotfix is needed
00561         //return (boolean) $sResult;
00562     }
00563 
00572     protected function _importOrderArticle( oxERPType $oType, $aRow)
00573     {
00574         $sResult = $this->_save( $oType, $aRow);
00575         return (boolean) $sResult;
00576     }
00577 
00586     protected function _importOrderStatus( oxERPType $oType, $aRow)
00587     {
00588         $oOrderArt = oxNew( "oxorderarticle", "core");
00589         $oOrderArt->load( $aRow['OXID']);
00590 
00591         if ( $oOrderArt->getId()) {
00592 
00593             try {
00594                 if ( $this->_sCurrVersion != "0.1") {
00595                     $oType->checkWriteAccess($oOrderArt->getId());
00596                 }
00597 
00598                     // store status
00599                 $aStatuses = unserialize( $oOrderArt->oxorderarticles__oxerpstatus->value );
00600 
00601                 $oStatus = new stdClass();
00602                 $oStatus->STATUS = $aRow['OXERPSTATUS_STATUS'];
00603                 $oStatus->date = $aRow['OXERPSTATUS_TIME'];
00604                 $oStatus->trackingid = $aRow['OXERPSTATUS_TRACKID'];
00605 
00606                 $aStatuses[$aRow['OXERPSTATUS_TIME']] = $oStatus;
00607                 $oOrderArt->oxorderarticles__oxerpstatus = new oxField(serialize( $aStatuses), oxField::T_RAW);
00608                 $oOrderArt->save();
00609                 return true;
00610             } catch (Exception $ex) {
00611                 return false;
00612             }
00613         }
00614 
00615         return false;
00616     }
00617 
00626     protected function _importUser( oxERPType $oType, $aRow)
00627     {
00628         //Speciall check for user
00629         if ( isset($aRow['OXUSERNAME'] ) ) {
00630             $sID = $aRow['OXID'];
00631             $sUserName = $aRow['OXUSERNAME'];
00632 
00633             $oUser = oxNew( "oxuser", "core");
00634             $oUser->oxuser__oxusername = new oxField($sUserName, oxField::T_RAW);
00635 
00636             //If user exists with and modifies OXID, throw an axception
00637             //throw new Exception( "USER {$sUserName} already exists!");
00638             if ( $oUser->exists( $sID) && $sID != $oUser->getId() ) {
00639                 throw new Exception( "USER $sUserName already exists!");
00640             }
00641 
00642         }
00643 
00644         $sResult  = $this->_save( $oType, $aRow);
00645         return (boolean) $sResult;
00646     }
00647 
00656     protected function _importVendor( oxERPType $oType, $aRow)
00657     {
00658         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00659         return (boolean) $sResult;
00660     }
00661 
00670     protected function _importArtextends( oxERPType $oType, $aRow)
00671     {
00672         if ( oxERPBase::getRequestedVersion() < 2 ) {
00673             return false;
00674         }
00675         $sResult = $this->_save( $oType, $aRow);
00676         return (boolean) $sResult;
00677     }
00678 
00687     protected function _importCountry( oxERPType $oType, $aRow)
00688     {
00689         $sResult = $this->_save( $oType, $aRow);
00690         return (boolean) $sResult;
00691     }
00692 
00701     protected function _importArticleStock( oxERPType $oType, $aRow )
00702     {
00703         $sResult = $this->_save( $oType, $aRow);
00704         return (boolean) $sResult;
00705     }
00706 }