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']] ) ) { //only in V0.1 and only once per import/article
00439             $myConfig = oxConfig::getInstance();
00440             $oDb = oxDb::getDb();
00441             $oDb->execute( "delete from oxactions2article where oxartid = ".$oDb->quote( $aRow['OXARTID'] ) );
00442             $this->_aImportedActions2Article[$aRow['OXARTID']] = 1;
00443         }
00444 
00445         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00446         return (boolean) $sResult;
00447     }
00448 
00457     protected function _importArticle2Category( oxERPType $oType, $aRow)
00458     {
00459         // deleting old relations before import in V0.1
00460         if ( $this->_sCurrVersion == "0.1" && !isset( $this->_aImportedObject2Category[$aRow['OXOBJECTID']] ) ) {
00461             $myConfig = oxConfig::getInstance();
00462             $oDb = oxDb::getDb();
00463             $oDb->execute( "delete from oxobject2category where oxobjectid = ".$oDb->quote( $aRow['OXOBJECTID'] ) );
00464             $this->_aImportedObject2Category[$aRow['OXOBJECTID']] = 1;
00465         }
00466 
00467         $sResult = $this->_save( $oType, $aRow);
00468         return (boolean) $sResult;
00469     }
00470 
00479     protected function _importMainArticle2Category( oxERPType $oType, $aRow)
00480     {
00481         $aRow['OXTIME'] = 0;
00482 
00483         $myConfig = oxConfig::getInstance();
00484         $oDb = oxDb::getDb();
00485 
00486         $sSql = "select OXID from oxobject2category where oxobjectid = ".$oDb->quote( $aRow['OXOBJECTID'] )." and OXCATNID = ".$oDb->quote( $aRow['OXCATNID'] );
00487         $aRow['OXID'] = $oDb->GetOne($sSql);
00488 
00489         $sResult = $this->_save( $oType, $aRow);
00490         if ((boolean) $sResult) {
00491             $sSql = "Update oxobject2category set oxtime = oxtime+10 where oxobjectid = ".$oDb->quote( $aRow['OXOBJECTID'] ) ." and oxcatnid != ".$oDb->quote( $aRow['OXCATNID'] ) ." and oxshopid = '".$myConfig->getShopId()."'";
00492             $oDb->Execute($sSql);
00493         }
00494 
00495         return (boolean) $sResult;
00496     }
00497 
00506     protected function _importCategory( oxERPType $oType, $aRow)
00507     {
00508         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00509         return (boolean) $sResult;
00510     }
00511 
00520     protected function _importCrossselling( oxERPType $oType, $aRow)
00521     {
00522         // deleting old relations before import in V0.1
00523         if ( $this->_sCurrVersion == "0.1" && !isset($this->_aImportedObject2Article[$aRow['OXARTICLENID']] ) ) {
00524             $myConfig = oxConfig::getInstance();
00525             $oDb = oxDb::getDb();
00526             $oDb->Execute( "delete from oxobject2article where oxarticlenid = ".$oDb->quote( $aRow['OXARTICLENID'] ) );
00527             $this->aImportedObject2Article[$aRow['OXARTICLENID']] = 1;
00528         }
00529 
00530         $sResult = $this->_save( $oType, $aRow);
00531         return (boolean) $sResult;
00532     }
00533 
00542     protected function _importScaleprice( oxERPType $oType, $aRow)
00543     {
00544         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00545         return (boolean) $sResult;
00546     }
00547 
00556     protected function _importOrder( oxERPType $oType, $aRow)
00557     {
00558         $sResult = $this->_save( $oType, $aRow);
00559         return true; //MAFI a unavoidable hack as oxorder->update() does always return null !!! a hotfix is needed
00560         //return (boolean) $sResult;
00561     }
00562 
00571     protected function _importOrderArticle( oxERPType $oType, $aRow)
00572     {
00573         $sResult = $this->_save( $oType, $aRow);
00574         return (boolean) $sResult;
00575     }
00576 
00585     protected function _importOrderStatus( oxERPType $oType, $aRow)
00586     {
00587         $oOrderArt = oxNew( "oxorderarticle", "core");
00588         $oOrderArt->load( $aRow['OXID']);
00589 
00590         if ( $oOrderArt->getId()) {
00591 
00592             try {
00593                 if ( $this->_sCurrVersion != "0.1") {
00594                     $oType->checkWriteAccess($oOrderArt->getId());
00595                 }
00596 
00597                     // store status
00598                 $aStatuses = unserialize( $oOrderArt->oxorderarticles__oxerpstatus->value );
00599 
00600                 $oStatus = new stdClass();
00601                 $oStatus->STATUS        = $aRow['OXERPSTATUS_STATUS'];
00602                 $oStatus->date          = $aRow['OXERPSTATUS_TIME'];
00603                 $oStatus->trackingid    = $aRow['OXERPSTATUS_TRACKID'];
00604 
00605                 $aStatuses[$aRow['OXERPSTATUS_TIME']] = $oStatus;
00606                 $oOrderArt->oxorderarticles__oxerpstatus = new oxField(serialize( $aStatuses), oxField::T_RAW);
00607                 $oOrderArt->save();
00608                 return true;
00609             } catch (Exception $ex) {
00610                 return false;
00611             }
00612         }
00613 
00614         return false;
00615     }
00616 
00625     protected function _importUser( oxERPType $oType, $aRow)
00626     {
00627         //Speciall check for user
00628         if ( isset($aRow['OXUSERNAME'] ) ) {
00629             $sID = $aRow['OXID'];
00630             $sUserName = $aRow['OXUSERNAME'];
00631 
00632             $oUser = oxNew( "oxuser", "core");
00633             $oUser->oxuser__oxusername = new oxField($sUserName, oxField::T_RAW);
00634 
00635             //If user exists with and modifies OXID, throw an axception
00636             //throw new Exception( "USER {$sUserName} already exists!");
00637             if ( $oUser->exists( $sID) && $sID != $oUser->getId() ) {
00638                 throw new Exception( "USER $sUserName already exists!");
00639             }
00640 
00641         }
00642 
00643         $sResult  = $this->_save( $oType, $aRow);
00644         return (boolean) $sResult;
00645     }
00646 
00655     protected function _importVendor( oxERPType $oType, $aRow)
00656     {
00657         $sResult = $this->_save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00658         return (boolean) $sResult;
00659     }
00660 
00669     protected function _importArtextends( oxERPType $oType, $aRow)
00670     {
00671         if ( oxERPBase::getRequestedVersion() < 2 ) {
00672             return false;
00673         }
00674         $sResult = $this->_save( $oType, $aRow);
00675         return (boolean) $sResult;
00676     }
00677 
00686     protected function _importCountry( oxERPType $oType, $aRow)
00687     {
00688         $sResult = $this->_save( $oType, $aRow);
00689         return (boolean) $sResult;
00690     }
00691 
00700     protected function _importArticleStock( oxERPType $oType, $aRow )
00701     {
00702         $sResult = $this->_save( $oType, $aRow);
00703         return (boolean) $sResult;
00704     }
00705 }

Generated on Mon Oct 26 20:07:16 2009 for OXID eShop CE by  doxygen 1.5.5