productRepository = $productRepository; $this->warehouseRepository = $warehouseRepository; $this->stockRepository = $stockRepository; $this->logger = $logger; $this->currentDirPath = getcwd(); } /** * @return array|void */ public function startImport() { #*** Holt alle Dateien if($this->getFiles()){ #*** Holt Alle Stocks und setzt ein Array ************** $this->getStocks(); if(!empty($this->arrData['orgFiles']['data']) && count($this->arrData['orgFiles']['data'])){ foreach ($this->arrData['orgFiles']['data'] as $file) { if(is_file($file['realPath'])){ $this->loadFiles($file['realPath'],$file['fileSize']); }else{ return array('error'=>1,'text'=>'Error is_file('.$file['realPath'].')'); } } }else return array('success'=>1,'text'=>'No Files'); }else return array('error'=>1,'text'=>'Error in getFiles or no file WS.FERTIG'); } /** * @return bool */ protected function getFiles() { $finder = Finder::create(); $finder ->in($this->currentDirPath.'/hiltes/h2c/') ->depth(0); #->filter(static function (SplFileInfo $file) { # return $file->isDir() || \preg_match('/\.(php|json)$/', $file->getPathname()); #}); $hasFoundFertigFile = false; if ($finder->hasResults()) { #dump($finder); foreach ($finder as $file) { if($file->getRelativePathname()=='WS.FERTIG'){ $hasFoundFertigFile = true; continue; } $this->arrData['orgFiles']['data'][] = array( 'realPath' => $file->getRealPath(), 'fileSize' => $file->getFileInfo()->getSize(), 'onlyFileName' => $file->getRelativePathname(), ); } }else{ return false; } return $hasFoundFertigFile; } protected function getStocks() { $r = $this->stockRepository->findAll(); foreach ($r as $v) { $this->cachedStockIds[$v->getProductId()][$v->getWarehouse()->getId()] = $v; } #dd($this->cachedStockIds); } protected function loadFiles($srcFile,$size) { #*** Check File **** #dd($srcFile); $file = new \SplFileObject($srcFile); #dump($file->getRealPath()); $this->logger->info('Starte Import von ' . $file->getRealPath()); dump('Starte Import von ' . $file->getRealPath()); $c = 0; while (!$file->eof()) { // Header überspringen $c++; #*** Convertiert die Zeile in UTF8 $data = iconv('ISO-8859-1','UTF-8',$file->fgets()); #*** Zerlegt die Zeile ********** //$data = $file->fgets(); if($c <= 1){ continue; } $this->switchSaveData($this->splitLine($data)); } // $em = $this->productRepository->getEntityManager(); // $em->flush(); dump($c . ' Datensätze importiert'); } protected function splitLine($str){ return str_getcsv($str,';','"'); } /** * Hier wird festgelegt wo und wie die Zeile gespeichert wird * * @param array $arr * @return void */ protected function switchSaveData(array $arr){ if(!empty($arr[0])){ #*** Leerzeichen löschen bei den einzelnen Einträgen ********** $this->trimArray($arr); #*** $this->saveData($arr); } } protected function trimArray(Array &$arr){ foreach ($arr as $k=>$v) { $arr[$k] = trim($v); } } /** * @param array $arr * @return void */ protected function saveData(Array $arr){ #dump($arr); #*** Speichert und/oder Holt WareHouse ab $wareHouse = $this->checkWareHouseName($arr[0]); #*** PRodukt wird angelegt oder geholt **** #filiale;etikettnr;menge;modellnr;modellbez $prodId = $this->checkProduct($arr[1],$arr[3],$arr[4]); #*** Ermitteln ob schon ein ID vorhadnen für ProdId und WareHouseId if(!empty($wareHouse) && !empty($this->cachedStockIds[$prodId][$wareHouse->getId()])){ $stock = $this->cachedStockIds[$prodId][$wareHouse->getId()]; #dd('JAAAAA'); }else{ $stock = new Stock(); $stock->setProductId($prodId); $stock->setWarehouse($wareHouse); } #dd('CHCEK'); $stock->setInstock((int)$arr[2]); #$stock->setWarehouseId($wareHouseId); #dump($stock); $this->stockRepository->save($stock,true); #dd($stock); //$stock->setStock((int)$arr[2]); #filiale; // // $warehouse = $this->warehouseRepository->findOneBy(['id'=> $arr[2]]); // // // // if($warehouse instanceof Warehouse){ // $stock->setWarehouseId($warehouse->getId()); // }else{ // $warehouse = new Warehouse(); // $warehouse->setId((int)$arr[2]); // $warehouse->setPriority(0); // $id = $this->warehouseRepository->add($warehouse, true); // # $stock->setWarehouseId($id); // } # // $stock->setWarehouseId($warehouse->getId()); // try{ // $this->stockRepository->upsert($stock); // }catch (\Exception $e){ // $this->logger->error($e->getMessage()); // dump($e->getMessage()); // } } /** * @param string $wareHouseName * @return Warehouse|false|int|mixed|null */ private function checkWareHouseName(string $wareHouseName){ #*** WEnn keine geCached Id Vorhanden if(empty($this->cachedWarehouseIds[$wareHouseName])){ #*** Check $warehouse2 = $this->warehouseRepository->findOneBy(['id'=> (int)$wareHouseName]); //dump($warehouse2); if(empty($warehouse2)){ $warehouse = new Warehouse(); $warehouse->setId((int)$wareHouseName); $warehouse->setName($wareHouseName); # $this->warehouseRepository->save($warehouse,true); $this->cachedWarehouseIds[$wareHouseName] = $warehouse; #***************** #$warehouse2 = $this->warehouseRepository->findOneBy(['id'=> (int)$wareHouseName]); #dump($warehouse2); }else{ $this->cachedWarehouseIds[$wareHouseName] = $warehouse2; } } if(!empty($this->cachedWarehouseIds[$wareHouseName])) return $this->cachedWarehouseIds[$wareHouseName]; return false; } /** * @param string $gtin * @param string $modellNr * @param string $modellBez * @return false|int|mixed|null */ private function checkProduct(string $gtin,string $modellNr,string $modellBez){ #*** WEnn keine geCached Id Vorhanden if(empty($this->cachedProdIds[$gtin])){ #*** Check $prod2 = $this->productRepository->findOneBy(['gtin'=> $gtin]); #dump($warehouse2); if(empty($prod2)){ $prod = new Product(); $prod->setGtin($gtin); $prod->setModellNr(trim($modellNr)); $prod->setModellBez(trim($modellBez)); $prod->setShopwareId(""); # $this->cachedProdIds[$gtin] = $this->productRepository->save($prod,true); #***************** //$prod2 = $this->productRepository->findOneBy(['gtin'=> $gtin]); #dump($warehouse2); }else{ $this->cachedProdIds[$gtin] = $prod2->getId(); } } if(!empty($this->cachedProdIds[$gtin])) return $this->cachedProdIds[$gtin]; return false; } }