Files
CdsConnector/src/Helper/HiltesImport.php
2023-02-06 10:11:39 +01:00

260 lines
6.2 KiB
PHP

<?php
namespace App\Helper;
use App\Entity\Product;
use App\Entity\Stock;
use App\Entity\Warehouse;
use App\Repository\ProductRepository;
use App\Repository\StockRepository;
use App\Repository\WarehouseRepository;
use Doctrine\ORM\EntityManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\Finder\Finder;
class HiltesImport
{
private $productRepository;
private $stockRepository;
private $warehouseRepository;
private $logger;
protected $currentDirPath;
protected $arrData = array();
public function __construct(ProductRepository $productRepository, LoggerInterface $logger)
{
// $this->stockRepository = $stockRepository;
// $this->warehouseRepository = $warehouseRepository;
$this->productRepository = $productRepository;
$this->logger = $logger;
$this->currentDirPath = getcwd();
}
public function startImport()
{
#*** Holt alle Dateien
if($this->getFiles()){
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');
}
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());
#});
if ($finder->hasResults()) {
foreach ($finder as $file) {
$this->arrData['orgFiles']['data'][] = array(
'realPath' => $file->getRealPath(),
'fileSize' => $file->getFileInfo()->getSize(),
'onlyFileName' => $file->getRelativePathname(),
);
}
}else{
return false;
}
return true;
}
protected function loadFiles($srcFile,$size)
{
$file = new \SplFileObject($srcFile);
dump($file->getRealPath());
$this->logger->info('Starte Import von ' . $file->getRealPath());
$c = 0;
while (!$file->eof()) {
// Header überspringen
$c++;
#*** Convertiert die Zeile in UTF8
//$str = iconv('ISO-8859-1','UTF-8',$file->fgets());
#*** Zerlegt die Zeile **********
$data = $file->fgets();
if($c <= 1){ continue; }
$this->switchSaveData($this->splitLine($data));
}
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);
#***
// $arr[0] = strtolower($arr[0]);
// switch ($arr[0]){
// case 'datei': # Datei
// $this->saveInfoDatei($arr);
// break;
// case 'filiale': # Filiale
// #$this->saveInfoFiliale($arr);
// break;
// case 'land': # Länder
// $this->saveInfoLand($arr);
// break;
// case 'hwg': # Hauptwarengruppe
// #$this->saveInfoHauptWarenGruppe($arr);
// break;
// case 'wg': # Warengruppe
// #$this->saveInfoWarenGruppe($arr);
// break;
// case 'anrede': # Anrede
// #$this->saveInfoAnrede($arr);
// break;
// case 'lieferant': # Lieferant
// #$this->saveInfoLieferant($arr);
// break;
// case 'farb': # Farbcodierung
// #$this->saveInfoFarbCodierung($arr);
// break;
// case 'nlart': # NachlassArt
// #$this->saveInfoNachlassArt($arr);
// break;
// case 'kollektion': # Kollektion
// #$this->saveInfoKollektion($arr);
// break;
// case 'bestand': # Bestand
// #$this->saveInfoBestand($arr);
// break;
// case 'merkmale': # Made In XXXX = Land
// #$this->saveInfoMerkmale($arr);
// break;
// case 'material': # MAterial
// #$this->saveInfoMaterial($arr);
// break;
// default: #
// dump('!!!!! KEIN DEFINIERTER ANFANG "'.$arr[0].'" !!!!!!!!!');
// break;
// }
$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);
$prod = new Product();
$stock = new Stock();
$stock->setInstock((int)$arr[2]);
$warehouse = new Warehouse();
$warehouse->setName($arr[0]);
// $stock->setWarehouse($warehouse);
#menge;
$prod->setStock($stock);
#gtin;
$prod->setGtin($arr[1]);
$prod->setShopwareId("");
// dump($prod);
$this->productRepository->save($prod,true);
//$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 $id
* @return mixed
*/
private function getWarehouseId($id){
$warehouse = $this->warehouseRepository->findOneBy(array('id'=>$id));
if($warehouse instanceof Warehouse){
return $warehouse->getId();
}else{
$warehouse = new Warehouse();
$warehouse->setPriority(0);
$warehouse->setId($this->warehouseRepository->add($warehouse, true));
dump($warehouse);
return $warehouse;
}
}
}