00001 <?php
00002
00006 class oxErpCsv extends oxERPBase {
00007
00008 protected $_aSupportedVersions = array("0.1", "1.0", "1.1", "2.0");
00009 protected $_aCsv2BaseVersionsMap = array("0.1" => "1", "1.0" => "1", "1.1"=>"1.1", "2.0" => "2");
00010
00011
00012 protected $_sCurrVersion = "";
00013
00014 protected $_aData = array();
00015
00016 protected $_iRetryRows = 0;
00017
00018 protected $_sReturn;
00019
00020 protected $_sPath;
00021
00022 protected $_aImportedActions2Article = array();
00023 protected $_aImportedObject2Category = array();
00024 protected $_aImportedAccessoire2Article = array();
00025
00026 protected function _beforeExport($sType){}
00027 protected function _afterExport($sType) {}
00028 protected function _getImportType(& $aData) {}
00029 protected function _getImportMode($Data) {}
00030 protected function _modifyData($aData, $oType) {}
00031 public function loadSessionData( $sSessionID ) {}
00032
00041 protected function _csvTextConvert($sText, $blMode)
00042 {
00043 $aSearch = array(chr(13), chr(10), '\'', '"');
00044 $aReplace = array(' ', ' ', ''', '"');
00045
00046 if( $blMode)
00047 $sText = str_replace( $aSearch, $aReplace ,$sText);
00048 else
00049 $sText = str_replace( $aReplace, $aSearch ,$sText);
00050
00051 return $sText;
00052 }
00053
00054 public function Import()
00055 {
00056 $this->_beforeImport();
00057
00058 do{
00059 while( $this->_importOne());
00060 }
00061 while ( !$this->_afterImport() );
00062
00063 }
00064
00065 protected function _beforeImport()
00066 {
00067 if(!$this->_iRetryRows){
00068
00069 foreach ($this->_aData as $key => $value) {
00070 $this->_aData[$key] = $this->_csvTextConvert($value, false);
00071 }
00072 }
00073
00074 }
00075 protected function _afterImport()
00076 {
00077
00078 $aStatistics = $this->getStatistics();
00079 $iRetryRows = 0;
00080
00081 foreach ($aStatistics as $key => $value) {
00082 if($value['r'] == false){
00083 $iRetryRows ++;
00084 $this->_sReturn.= "File[".$this->_sPath."] - dataset number: $key - Error: ".$value['m']." ---<br> ".PHP_EOL;
00085 }
00086 }
00087
00088 if($iRetryRows != $this->_iRetryRows && $iRetryRows>0){
00089 $this->_resetIdx();
00090 $this->_iRetryRows = $iRetryRows;
00091 $this->_sReturn = '';
00092
00093 return false;
00094 }
00095
00096 return true;
00097 }
00098 public function getImportData()
00099 {
00100 return $this->_aData[$this->_iIdx];
00101 }
00102
00108 private function getOldOrderArticleFieldList()
00109 {
00110 $aFieldList = array(
00111 'OXID' => 'OXID',
00112 'OXORDERID' => 'OXORDERID',
00113 'OXAMOUNT' => 'OXAMOUNT',
00114 'OXARTID' => 'OXARTID',
00115 'OXARTNUM' => 'OXARTNUM',
00116 'OXTITLE' => 'OXTITLE',
00117 'OXSELVARIANT' => 'OXSELVARIANT',
00118 'OXNETPRICE' => 'OXNETPRICE',
00119 'OXBRUTPRICE' => 'OXBRUTPRICE',
00120 'OXVAT' => 'OXVAT',
00121 'OXPERSPARAM' => 'OXPERSPARAM',
00122 'OXPRICE' => 'OXPRICE',
00123 'OXBPRICE' => 'OXBPRICE',
00124 'OXTPRICE' => 'OXTPRICE',
00125 'OXWRAPID' => 'OXWRAPID',
00126 'OXSTOCK' => 'OXSTOCK',
00127 'OXORDERSHOPID' => 'OXORDERSHOPID',
00128 'OXTOTALVAT' => 'OXTOTALVAT'
00129 );
00130
00131 return $aFieldList;
00132 }
00133
00139 private function getOldOrderFielsList()
00140 {
00141 $aFieldList = array(
00142 'OXID' => 'OXID',
00143 'OXSHOPID' => 'OXSHOPID',
00144 'OXUSERID' => 'OXUSERID',
00145 'OXORDERDATE' => 'OXORDERDATE',
00146 'OXORDERNR' => 'OXORDERNR',
00147 'OXBILLCOMPANY' => 'OXBILLCOMPANY',
00148 'OXBILLEMAIL' => 'OXBILLEMAIL',
00149 'OXBILLFNAME' => 'OXBILLFNAME',
00150 'OXBILLLNAME' => 'OXBILLLNAME',
00151 'OXBILLSTREET' => 'OXBILLSTREET',
00152 'OXBILLSTREETNR' => 'OXBILLSTREETNR',
00153 'OXBILLADDINFO' => 'OXBILLADDINFO',
00154 'OXBILLUSTID' => 'OXBILLUSTID',
00155 'OXBILLCITY' => 'OXBILLCITY',
00156 'OXBILLCOUNTRY' => 'OXBILLCOUNTRY',
00157 'OXBILLZIP' => 'OXBILLZIP',
00158 'OXBILLFON' => 'OXBILLFON',
00159 'OXBILLFAX' => 'OXBILLFAX',
00160 'OXBILLSAL' => 'OXBILLSAL',
00161 'OXDELCOMPANY' => 'OXDELCOMPANY',
00162 'OXDELFNAME' => 'OXDELFNAME',
00163 'OXDELLNAME' => 'OXDELLNAME',
00164 'OXDELSTREET' => 'OXDELSTREET',
00165 'OXDELSTREETNR' => 'OXDELSTREETNR',
00166 'OXDELADDINFO' => 'OXDELADDINFO',
00167 'OXDELCITY' => 'OXDELCITY',
00168 'OXDELCOUNTRY' => 'OXDELCOUNTRY',
00169 'OXDELZIP' => 'OXDELZIP',
00170 'OXDELFON' => 'OXDELFON',
00171 'OXDELFAX' => 'OXDELFAX',
00172 'OXDELSAL' => 'OXDELSAL',
00173 'OXDELCOST' => 'OXDELCOST',
00174 'OXDELVAT' => 'OXDELVAT',
00175 'OXPAYCOST' => 'OXPAYCOST',
00176 'OXPAYVAT' => 'OXPAYVAT',
00177 'OXWRAPCOST' => 'OXWRAPCOST',
00178 'OXWRAPVAT' => 'OXWRAPVAT',
00179 'OXCARDID' => 'OXCARDID',
00180 'OXCARDTEXT' => 'OXCARDTEXT',
00181 'OXDISCOUNT' => 'OXDISCOUNT',
00182 'OXBILLNR' => 'OXBILLNR',
00183 'OXREMARK' => 'OXREMARK',
00184 'OXVOUCHERDISCOUNT' => 'OXVOUCHERDISCOUNT',
00185 'OXCURRENCY' => 'OXCURRENCY',
00186 'OXCURRATE' => 'OXCURRATE',
00187 'OXTRANSID' => 'OXTRANSID',
00188 'OXPAID' => 'OXPAID',
00189 'OXIP' => 'OXIP',
00190 'OXTRANSSTATUS' => 'OXTRANSSTATUS',
00191 'OXLANG' => 'OXLANG',
00192 'OXDELTYPE' => 'OXDELTYPE'
00193 );
00194
00195 return $aFieldList;
00196 }
00197
00198 protected function _checkIDField( $sID)
00199 {
00200 if( !isset( $sID) || !$sID)
00201 throw new Exception("ERROR: Articlenumber/ID missing!");
00202 elseif( strlen( $sID) > 32)
00203 throw new Exception( "ERROR: Articlenumber/ID longer then allowed (32 chars max.)!");
00204 }
00205
00213 protected function _getInstanceOfType( $sType)
00214 {
00215
00216 if($sType == 'oldOrder'){
00217 $oType = parent::_getInstanceOfType('order');
00218 $oType->setFieldList($this->getOldOrderFielsList());
00219 $oType->setFunctionSuffix('OldOrder');
00220 }elseif($sType == 'oldOrderArticle'){
00221 $oType = parent::_getInstanceOfType('orderarticle');
00222 $oType->setFieldList($this->getOldOrderArticleFieldList());
00223 $oType->setFunctionSuffix('OldOrderArticle');
00224 }elseif($sType == 'article2vendor'){
00225 $oType = parent::_getInstanceOfType('article');
00226 $oType->setFieldList(array("OXID", "OXVENDORID"));
00227 }elseif($sType == 'mainarticle2categroy') {
00228 $oType = parent::_getInstanceOfType('article2category');
00229 $oType->setFieldList(array("OXOBJECTID", "OXCATNID", "OXTIME"));
00230 $oType->setFunctionSuffix('mainarticle2category');
00231 }
00232 else{
00233 $oType = parent::_getInstanceOfType($sType);
00234 }
00235
00236 return $oType;
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 protected function _ImportArticle( oxERPType & $oType, $aRow)
00250 {
00251
00252 if($this->_sCurrVersion == "0.1")
00253 {
00254 $myConfig = oxConfig::getInstance();
00255
00256 $myConfig->setConfigParam('blMallCustomPrice', false);
00257 }
00258
00259
00260 if(isset($aRow['OXID'])){
00261 $this->_checkIDField($aRow['OXID']);
00262 }else{
00263 $this->_checkIDField($aRow['OXARTNUM']);
00264 $aRow['OXID'] = $aRow['OXARTNUM'];
00265 }
00266
00267 $sResult = $this->_Save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00268 return (boolean) $sResult;
00269 }
00270
00271 protected function _ImportAccessoire( oxERPType & $oType, $aRow) {
00272
00273
00274 if ( $this->_sCurrVersion == "0.1" && !isset($this->_aImportedAccessoire2Article[$aRow['OXARTICLENID']] ) ) {
00275 $myConfig = oxConfig::getInstance();
00276 $sDeleteSQL = "delete from oxaccessoire2article where oxarticlenid = '{$aRow['OXARTICLENID']}'";
00277 oxDb::getDb()->Execute( $sDeleteSQL );
00278 $this->_aImportedAccessoire2Article[$aRow['OXARTICLENID']] = 1;
00279 }
00280
00281 $sResult = $this->_Save( $oType, $aRow);
00282 return (boolean) $sResult;
00283 }
00284
00285 protected function _ImportArticle2Action( oxERPType & $oType, $aRow)
00286 {
00287
00288 if ( $this->_sCurrVersion == "0.1" && !isset( $this->_aImportedActions2Article[$aRow['OXARTID']] ) ) {
00289
00290
00291 $myConfig = oxConfig::getInstance();
00292 $sDeleteSQL = "delete from oxactions2article where oxartid = '{$aRow['OXARTID']}'";
00293 oxDb::getDb()->Execute( $sDeleteSQL );
00294 $this->_aImportedActions2Article[$aRow['OXARTID']] = 1;
00295 }
00296
00297 $sResult = $this->_Save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00298 return (boolean) $sResult;
00299 }
00300
00301 protected function _ImportArticle2Category( oxERPType & $oType, $aRow)
00302 {
00303
00304
00305 if ( $this->_sCurrVersion == "0.1" && !isset( $this->_aImportedObject2Category[$aRow['OXOBJECTID']] ) ) {
00306 $myConfig = oxConfig::getInstance();
00307 $sDeleteSQL = "delete from oxobject2category where oxobjectid = '{$aRow['OXOBJECTID']}'";
00308 oxDb::getDb()->Execute( $sDeleteSQL );
00309 $this->_aImportedObject2Category[$aRow['OXOBJECTID']] = 1;
00310 }
00311
00312 $sResult = $this->_Save( $oType, $aRow);
00313 return (boolean) $sResult;
00314 }
00315
00316 protected function _ImportMainArticle2Category( oxERPType & $oType, $aRow)
00317 {
00318 $aRow['OXTIME'] = 0;
00319
00320 $myConfig = oxConfig::getInstance();
00321 $sSql = "select OXID from oxobject2category where oxobjectid = '".$aRow['OXOBJECTID']."' and OXCATNID = '".$aRow['OXCATNID']."'";
00322 $aRow['OXID'] = oxDb::getDb()->GetOne($sSql);
00323
00324 $sResult = $this->_Save( $oType, $aRow);
00325
00326 if ((boolean) $sResult) {
00327
00328 $sSql = "Update oxobject2category set oxtime = oxtime+10 where oxobjectid = '" . $aRow['OXOBJECTID'] ."' and oxcatnid != '". $aRow['OXCATNID'] ."' and oxshopid = '".$myConfig->getShopId()."'";
00329 oxDb::getDb()->Execute($sSql);
00330
00331 }
00332
00333 return (boolean) $sResult;
00334 }
00335
00336 protected function _ImportCategory( oxERPType & $oType, $aRow)
00337 {
00338
00339 $sResult = $this->_Save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00340 return (boolean) $sResult;
00341 }
00342
00343 protected function _ImportCrossselling( oxERPType & $oType, $aRow)
00344 {
00345
00346
00347
00348 if ( $this->_sCurrVersion == "0.1" && !isset($this->_aImportedObject2Article[$aRow['OXARTICLENID']] ) ) {
00349 $myConfig = oxConfig::getInstance();
00350 $sDeleteSQL = "delete from oxobject2article where oxarticlenid = '{$aRow['OXARTICLENID']}'";
00351 oxDb::getDb()->Execute( $sDeleteSQL );
00352 $this->aImportedObject2Article[$aRow['OXARTICLENID']] = 1;
00353 }
00354
00355 $sResult = $this->_Save( $oType, $aRow);
00356 return (boolean) $sResult;
00357 }
00358
00359 protected function _ImportScaleprice( oxERPType & $oType, $aRow)
00360 {
00361
00362 $sResult = $this->_Save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00363 return (boolean) $sResult;
00364 }
00365
00366 protected function _ImportOrder( oxERPType & $oType, $aRow)
00367 {
00368
00369 $sResult = $this->_Save( $oType, $aRow);
00370 return true;
00371
00372 }
00373
00374 protected function _ImportOrderArticle( oxERPType & $oType, $aRow)
00375 {
00376
00377 $sResult = $this->_Save( $oType, $aRow);
00378 return (boolean) $sResult;
00379 }
00380
00381 protected function _ImportOrderStatus( oxERPType & $oType, $aRow) {
00382 $oOrderArt = oxNew( "oxorderarticle", "core");
00383 $oOrderArt->Load( $aRow['OXID']);
00384
00385 if( $oOrderArt->getId()) {
00386
00387 try {
00388 if( $this->_sCurrVersion != "0.1")
00389 $oType->checkWriteAccess($oOrderArt->getId());
00390
00391
00392 $aStatuses = unserialize( $oOrderArt->oxorderarticles__oxerpstatus->value );
00393
00394 $oStatus = new stdClass();
00395 $oStatus->STATUS = $aRow['OXERPSTATUS_STATUS'];
00396 $oStatus->date = $aRow['OXERPSTATUS_TIME'];
00397 $oStatus->trackingid = $aRow['OXERPSTATUS_TRACKID'];
00398
00399 $aStatuses[$aRow['OXERPSTATUS_TIME']] = $oStatus;
00400 $oOrderArt->oxorderarticles__oxerpstatus = new oxField(serialize( $aStatuses), oxField::T_RAW);
00401 $oOrderArt->Save();
00402 return true;
00403 } catch (Exception $ex) {
00404 return false;
00405 }
00406 }
00407
00408 return false;
00409 }
00410
00411 protected function _ImportUser( oxERPType & $oType, $aRow)
00412 {
00413
00414
00415 if(isset($aRow['OXUSERNAME']))
00416 {
00417 $sID = $aRow['OXID'];
00418 $sUserName = $aRow['OXUSERNAME'];
00419
00420 $oUser = oxNew( "oxuser", "core");
00421 $oUser->oxuser__oxusername = new oxField($sUserName, oxField::T_RAW);
00422
00423
00424
00425 if( $oUser->exists( $sID) && $sID != $oUser->getId() ) {
00426 throw new Exception( "USER $sUserName already exists!");
00427 }
00428
00429 }
00430
00431 $sResult = $this->_Save( $oType, $aRow);
00432 return (boolean) $sResult;
00433 }
00434
00435 protected function _ImportVendor( oxERPType & $oType, $aRow)
00436 {
00437
00438 $sResult = $this->_Save( $oType, $aRow, $this->_sCurrVersion == "0.1");
00439 return (boolean) $sResult;
00440 }
00441
00442 protected function _ImportArtextends( oxERPType & $oType, $aRow) {
00443 if (oxERPBase::getRequestedVersion() < 2) {
00444 return false;
00445 }
00446 $sResult = $this->_Save( $oType, $aRow);
00447 return (boolean) $sResult;
00448 }
00449
00450 protected function _ImportCountry( oxERPType & $oType, $aRow)
00451 {
00452 $sResult = $this->_Save( $oType, $aRow);
00453 return (boolean) $sResult;
00454 }
00455
00456 protected function _ImportArticleStock( oxERPType & $oType, $aRow) {
00457 $sResult = $this->_Save( $oType, $aRow);
00458 return (boolean) $sResult;
00459 }
00460 }