oxerpcsv.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxErpCsv extends oxERPBase
00007 {
00008 
00014     protected $_aSupportedVersions = array("0.1", "1.0", "1.1", "2.0");
00015 
00021     protected $_aCsv2BaseVersionsMap = array("0.1" => "1", "1.0" => "1", "1.1" => "1.1", "2.0" => "2");
00022 
00028     protected $_sCurrVersion = "";
00029 
00035     protected $_aData = array();
00036 
00042     protected $_iRetryRows = 0;
00043 
00049     protected $_sReturn;
00050 
00056     protected $_sPath;
00057 
00063     protected $_aImportedActions2Article = array();
00064 
00070     protected $_aImportedObject2Category = array();
00071 
00077     protected $_aImportedAccessoire2Article = array();
00078 
00084     protected function _beforeExport($sType)
00085     {
00086     }
00087 
00093     protected function _afterExport($sType)
00094     {
00095     }
00096 
00102     protected function _getImportType(&$aData)
00103     {
00104     }
00105 
00111     protected function _getImportMode($aData)
00112     {
00113     }
00114 
00121     protected function _modifyData($aData, $oType)
00122     {
00123     }
00124 
00130     public function loadSessionData($sSessionID)
00131     {
00132     }
00133 
00142     protected function _csvTextConvert($sText, $blMode)
00143     {
00144         $aSearch = array(chr(13), chr(10), '\'', '"');
00145         $aReplace = array('&#13;', '&#10;', '&#39;', '&#34;');
00146 
00147         if ($blMode) {
00148             $sText = str_replace($aSearch, $aReplace, $sText);
00149         } else {
00150             $sText = str_replace($aReplace, $aSearch, $sText);
00151         }
00152 
00153         return $sText;
00154     }
00155 
00159     public function import()
00160     {
00161         $this->_beforeImport();
00162 
00163         do {
00164             while ($this->_importOne()) {
00165                 ;
00166             }
00167         } while (!$this->_afterImport());
00168     }
00169 
00173     protected function _beforeImport()
00174     {
00175         if (!$this->_iRetryRows) {
00176             //convert all text
00177             foreach ($this->_aData as $key => $value) {
00178                 $this->_aData[$key] = $this->_csvTextConvert($value, false);
00179             }
00180         }
00181 
00182     }
00183 
00189     protected function _afterImport()
00190     {
00191         //check if there have been no errors or failures
00192         $aStatistics = $this->getStatistics();
00193         $iRetryRows = 0;
00194 
00195         foreach ($aStatistics as $key => $value) {
00196             if ($value['r'] == false) {
00197                 $iRetryRows++;
00198                 $this->_sReturn .= "File[" . $this->_sPath . "] - dataset number: $key - Error: " . $value['m'] . " ---<br> " . PHP_EOL;
00199             }
00200         }
00201 
00202         if ($iRetryRows != $this->_iRetryRows && $iRetryRows > 0) {
00203             $this->_resetIdx();
00204             $this->_iRetryRows = $iRetryRows;
00205             $this->_sReturn = '';
00206 
00207             return false;
00208         }
00209 
00210         return true;
00211     }
00212 
00220     public function getImportData($iIdx = null)
00221     {
00222         return $this->_aData[$this->_iIdx];
00223     }
00224 
00231     private function getOldOrderArticleFieldList()
00232     {
00233         $aFieldList = array(
00234             'OXID'          => 'OXID',
00235             'OXORDERID'     => 'OXORDERID',
00236             'OXAMOUNT'      => 'OXAMOUNT',
00237             'OXARTID'       => 'OXARTID',
00238             'OXARTNUM'      => 'OXARTNUM',
00239             'OXTITLE'       => 'OXTITLE',
00240             'OXSELVARIANT'  => 'OXSELVARIANT',
00241             'OXNETPRICE'    => 'OXNETPRICE',
00242             'OXBRUTPRICE'   => 'OXBRUTPRICE',
00243             'OXVAT'         => 'OXVAT',
00244             'OXPERSPARAM'   => 'OXPERSPARAM',
00245             'OXPRICE'       => 'OXPRICE',
00246             'OXBPRICE'      => 'OXBPRICE',
00247             'OXTPRICE'      => 'OXTPRICE',
00248             'OXWRAPID'      => 'OXWRAPID',
00249             'OXSTOCK'       => 'OXSTOCK',
00250             'OXORDERSHOPID' => 'OXORDERSHOPID',
00251             'OXTOTALVAT'    => 'OXTOTALVAT'
00252         );
00253 
00254         return $aFieldList;
00255     }
00256 
00263     private function getOldOrderFielsList()
00264     {
00265         $aFieldList = array(
00266             'OXID'              => 'OXID',
00267             'OXSHOPID'          => 'OXSHOPID',
00268             'OXUSERID'          => 'OXUSERID',
00269             'OXORDERDATE'       => 'OXORDERDATE',
00270             'OXORDERNR'         => 'OXORDERNR',
00271             'OXBILLCOMPANY'     => 'OXBILLCOMPANY',
00272             'OXBILLEMAIL'       => 'OXBILLEMAIL',
00273             'OXBILLFNAME'       => 'OXBILLFNAME',
00274             'OXBILLLNAME'       => 'OXBILLLNAME',
00275             'OXBILLSTREET'      => 'OXBILLSTREET',
00276             'OXBILLSTREETNR'    => 'OXBILLSTREETNR',
00277             'OXBILLADDINFO'     => 'OXBILLADDINFO',
00278             'OXBILLUSTID'       => 'OXBILLUSTID',
00279             'OXBILLCITY'        => 'OXBILLCITY',
00280             'OXBILLCOUNTRY'     => 'OXBILLCOUNTRY',
00281             'OXBILLZIP'         => 'OXBILLZIP',
00282             'OXBILLFON'         => 'OXBILLFON',
00283             'OXBILLFAX'         => 'OXBILLFAX',
00284             'OXBILLSAL'         => 'OXBILLSAL',
00285             'OXDELCOMPANY'      => 'OXDELCOMPANY',
00286             'OXDELFNAME'        => 'OXDELFNAME',
00287             'OXDELLNAME'        => 'OXDELLNAME',
00288             'OXDELSTREET'       => 'OXDELSTREET',
00289             'OXDELSTREETNR'     => 'OXDELSTREETNR',
00290             'OXDELADDINFO'      => 'OXDELADDINFO',
00291             'OXDELCITY'         => 'OXDELCITY',
00292             'OXDELCOUNTRY'      => 'OXDELCOUNTRY',
00293             'OXDELZIP'          => 'OXDELZIP',
00294             'OXDELFON'          => 'OXDELFON',
00295             'OXDELFAX'          => 'OXDELFAX',
00296             'OXDELSAL'          => 'OXDELSAL',
00297             'OXDELCOST'         => 'OXDELCOST',
00298             'OXDELVAT'          => 'OXDELVAT',
00299             'OXPAYCOST'         => 'OXPAYCOST',
00300             'OXPAYVAT'          => 'OXPAYVAT',
00301             'OXWRAPCOST'        => 'OXWRAPCOST',
00302             'OXWRAPVAT'         => 'OXWRAPVAT',
00303             'OXCARDID'          => 'OXCARDID',
00304             'OXCARDTEXT'        => 'OXCARDTEXT',
00305             'OXDISCOUNT'        => 'OXDISCOUNT',
00306             'OXBILLNR'          => 'OXBILLNR',
00307             'OXREMARK'          => 'OXREMARK',
00308             'OXVOUCHERDISCOUNT' => 'OXVOUCHERDISCOUNT',
00309             'OXCURRENCY'        => 'OXCURRENCY',
00310             'OXCURRATE'         => 'OXCURRATE',
00311             'OXTRANSID'         => 'OXTRANSID',
00312             'OXPAID'            => 'OXPAID',
00313             'OXIP'              => 'OXIP',
00314             'OXTRANSSTATUS'     => 'OXTRANSSTATUS',
00315             'OXLANG'            => 'OXLANG',
00316             'OXDELTYPE'         => 'OXDELTYPE'
00317         );
00318 
00319         return $aFieldList;
00320     }
00321 
00327     protected function _checkIDField($sID)
00328     {
00329         if (!isset($sID) || !$sID) {
00330             throw new Exception("ERROR: Articlenumber/ID missing!");
00331         } elseif (strlen($sID) > 32) {
00332             throw new Exception("ERROR: Articlenumber/ID longer then allowed (32 chars max.)!");
00333         }
00334     }
00335 
00343     protected function _getInstanceOfType($sType)
00344     {
00345         //due to backward compatibility
00346         if ($sType == 'oldOrder') {
00347             $oType = parent::_getInstanceOfType('order');
00348             $oType->setFieldList($this->getOldOrderFielsList());
00349             $oType->setFunctionSuffix('OldOrder');
00350         } elseif ($sType == 'oldOrderArticle') {
00351             $oType = parent::_getInstanceOfType('orderarticle');
00352             $oType->setFieldList($this->getOldOrderArticleFieldList());
00353             $oType->setFunctionSuffix('OldOrderArticle');
00354         } elseif ($sType == 'article2vendor') {
00355             $oType = parent::_getInstanceOfType('article');
00356             $oType->setFieldList(array("OXID", "OXVENDORID"));
00357         } elseif ($sType == 'mainarticle2categroy') {
00358             $oType = parent::_getInstanceOfType('article2category');
00359             $oType->setFieldList(array("OXOBJECTID", "OXCATNID", "OXTIME"));
00360             $oType->setFunctionSuffix('mainarticle2category');
00361         } else {
00362             $oType = parent::_getInstanceOfType($sType);
00363         }
00364 
00365         return $oType;
00366     }
00367 
00368     // --------------------------------------------------------------------------
00369     //
00370     // Import Handler
00371     // One _Import* method needed for each object defined in /objects/ folder, all these objects  can be imported
00372     //
00373     // --------------------------------------------------------------------------
00374 
00383     protected function _importArticle(oxERPType $oType, $aRow)
00384     {
00385         if ($this->_sCurrVersion == "0.1") {
00386             $myConfig = oxRegistry::getConfig();
00387             //to allow different shopid without consequences (ignored fields)
00388             $myConfig->setConfigParam('blMallCustomPrice', false);
00389         }
00390 
00391         if (isset($aRow['OXID'])) {
00392             $this->_checkIDField($aRow['OXID']);
00393         }
00394         // #0004426
00395         /*else {
00396             $this->_checkIDField($aRow['OXARTNUM']);
00397             $aRow['OXID'] = $aRow['OXARTNUM'];
00398         }*/
00399 
00400         $sResult = $this->_save($oType, $aRow, $this->_sCurrVersion == "0.1"); // V0.1 allowes the shopid to be set no matter which login
00401         return $sResult;
00402     }
00403 
00412     protected function _importAccessoire(oxERPType $oType, $aRow)
00413     {
00414         // deleting old relations before import in V0.1
00415         if ($this->_sCurrVersion == "0.1" && !isset($this->_aImportedAccessoire2Article[$aRow['OXARTICLENID']])) {
00416             $myConfig = oxRegistry::getConfig();
00417             $oDb = oxDb::getDb();
00418             $oDb->execute("delete from oxaccessoire2article where oxarticlenid = " . $oDb->quote($aRow['OXARTICLENID']));
00419             $this->_aImportedAccessoire2Article[$aRow['OXARTICLENID']] = 1;
00420         }
00421 
00422         $sResult = $this->_save($oType, $aRow);
00423 
00424         return $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 = oxRegistry::getConfig();
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 
00448         return $sResult;
00449     }
00450 
00459     protected function _importArticle2Category(oxERPType $oType, $aRow)
00460     {
00461         // deleting old relations before import in V0.1
00462         if ($this->_sCurrVersion == "0.1" && !isset($this->_aImportedObject2Category[$aRow['OXOBJECTID']])) {
00463             $myConfig = oxRegistry::getConfig();
00464             $oDb = oxDb::getDb();
00465             $oDb->execute("delete from oxobject2category where oxobjectid = " . $oDb->quote($aRow['OXOBJECTID']));
00466             $this->_aImportedObject2Category[$aRow['OXOBJECTID']] = 1;
00467         }
00468 
00469         $sResult = $this->_save($oType, $aRow);
00470 
00471         return $sResult;
00472     }
00473 
00482     protected function _importMainArticle2Category(oxERPType $oType, $aRow)
00483     {
00484         $aRow['OXTIME'] = 0;
00485 
00486         $myConfig = oxRegistry::getConfig();
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, false, false);
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 $sResult;
00499     }
00500 
00509     protected function _importCategory(oxERPType $oType, $aRow)
00510     {
00511         $sResult = $this->_save($oType, $aRow, $this->_sCurrVersion == "0.1");
00512 
00513         return $sResult;
00514     }
00515 
00524     protected function _importCrossselling(oxERPType $oType, $aRow)
00525     {
00526         // deleting old relations before import in V0.1
00527         if ($this->_sCurrVersion == "0.1" && !isset($this->_aImportedObject2Article[$aRow['OXARTICLENID']])) {
00528             $myConfig = oxRegistry::getConfig();
00529             $oDb = oxDb::getDb();
00530             $oDb->Execute("delete from oxobject2article where oxarticlenid = " . $oDb->quote($aRow['OXARTICLENID']));
00531             $this->aImportedObject2Article[$aRow['OXARTICLENID']] = 1;
00532         }
00533 
00534         $sResult = $this->_save($oType, $aRow);
00535 
00536         return $sResult;
00537     }
00538 
00547     protected function _importScaleprice(oxERPType $oType, $aRow)
00548     {
00549         $sResult = $this->_save($oType, $aRow, $this->_sCurrVersion == "0.1");
00550 
00551         return $sResult;
00552     }
00553 
00562     protected function _importOrder(oxERPType $oType, $aRow)
00563     {
00564         $sResult = $this->_save($oType, $aRow);
00565 
00566         return $sResult;
00567         //MAFI a unavoidable hack as oxorder->update() does always return null !!! a hotfix is needed
00568         //hotfix was added? since it's working with proper return now
00569     }
00570 
00579     protected function _importOrderArticle(oxERPType $oType, $aRow)
00580     {
00581         $sResult = $this->_save($oType, $aRow);
00582 
00583         return $sResult;
00584     }
00585 
00594     protected function _importOrderStatus(oxERPType $oType, $aRow)
00595     {
00596         $oOrderArt = oxNew("oxorderarticle", "core");
00597         $oOrderArt->load($aRow['OXID']);
00598 
00599         if ($oOrderArt->getId()) {
00600 
00601             try {
00602                 if ($this->_sCurrVersion != "0.1") {
00603                     $oType->checkWriteAccess($oOrderArt->getId());
00604                 }
00605 
00606                 // store status
00607                 $aStatuses = unserialize($oOrderArt->oxorderarticles__oxerpstatus->value);
00608 
00609                 $oStatus = new stdClass();
00610                 $oStatus->STATUS = $aRow['OXERPSTATUS_STATUS'];
00611                 $oStatus->date = $aRow['OXERPSTATUS_TIME'];
00612                 $oStatus->trackingid = $aRow['OXERPSTATUS_TRACKID'];
00613 
00614                 $aStatuses[$aRow['OXERPSTATUS_TIME']] = $oStatus;
00615                 $oOrderArt->oxorderarticles__oxerpstatus = new oxField(serialize($aStatuses), oxField::T_RAW);
00616                 $oOrderArt->save();
00617 
00618                 return true;
00619             } catch (Exception $ex) {
00620                 return false;
00621             }
00622         }
00623 
00624         return false;
00625     }
00626 
00635     protected function _importUser(oxERPType $oType, $aRow)
00636     {
00637         //Speciall check for user
00638         if (isset($aRow['OXUSERNAME'])) {
00639             $sID = $aRow['OXID'];
00640             $sUserName = $aRow['OXUSERNAME'];
00641 
00642             $oUser = oxNew("oxuser", "core");
00643             $oUser->oxuser__oxusername = new oxField($sUserName, oxField::T_RAW);
00644 
00645             //If user exists with and modifies OXID, throw an axception
00646             //throw new Exception( "USER {$sUserName} already exists!");
00647             if ($oUser->exists($sID) && $sID != $oUser->getId()) {
00648                 throw new Exception("USER $sUserName already exists!");
00649             }
00650 
00651         }
00652 
00653         $sResult = $this->_save($oType, $aRow);
00654 
00655         return $sResult;
00656     }
00657 
00666     protected function _importVendor(oxERPType $oType, $aRow)
00667     {
00668         $sResult = $this->_save($oType, $aRow, $this->_sCurrVersion == "0.1");
00669 
00670         return $sResult;
00671     }
00672 
00681     protected function _importArtextends(oxERPType $oType, $aRow)
00682     {
00683         if (oxERPBase::getRequestedVersion() < 2) {
00684             return false;
00685         }
00686         $sResult = $this->_save($oType, $aRow);
00687 
00688         return $sResult;
00689     }
00690 
00699     protected function _importCountry(oxERPType $oType, $aRow)
00700     {
00701         $sResult = $this->_save($oType, $aRow);
00702 
00703         return $sResult;
00704     }
00705 
00714     protected function _importArticleStock(oxERPType $oType, $aRow)
00715     {
00716         $sResult = $this->_save($oType, $aRow);
00717 
00718         return $sResult;
00719     }
00720 
00725     public function getImportedRowCount()
00726     {
00727         return count($this->_aImportedIds);
00728     }
00729 
00734     public function setImportedIds($key)
00735     {
00736         if (!array_key_exists($key, $this->_aImportedIds)) {
00737             $this->_aImportedIds[$key] = true;
00738         }
00739     }
00740 }