Files
CdsConnector/src/Helper/HiltesImport.php
Marko c6bc1776bd
Some checks failed
continuous-integration/drone/push Build is failing
add fixed export
2023-12-13 12:53:31 +01:00

245 lines
7.1 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 Exception;
use Psr\Log\LoggerInterface;
use RuntimeException;
use SplFileObject;
use Symfony\Component\Finder\Finder;
class HiltesImport
{
protected $currentDirPath;
protected $cachedWarehouse;
protected $arrData = array();
private $productRepository;
private $stockRepository;
private $warehouseRepository;
private $logger;
private $cachedProdIds;
private $cachedStockIds;
private $rootPath;
private $deleteFiles = false;
public function __construct(ProductRepository $productRepository, WarehouseRepository $warehouseRepository, StockRepository $stockRepository, LoggerInterface $logger, string $rootPath)
{
$this->productRepository = $productRepository;
$this->warehouseRepository = $warehouseRepository;
$this->stockRepository = $stockRepository;
$this->logger = $logger;
$this->rootPath = $rootPath;
$this->currentDirPath = $this->rootPath;
}
/**
* @return void
*/
public function startImport($delta = false)
{
#*** Holt alle Dateien
if ($this->getFiles($delta)) {
#*** Holt alle Stocks und setzt ein Array **************
$this->getStocks();
$count = 0;
if (!empty($this->arrData['orgFiles']['data']) && count($this->arrData['orgFiles']['data'])) {
foreach ($this->arrData['orgFiles']['data'] as $file) {
if (is_file($file['realPath'])) {
$count += $this->loadFiles($file['realPath']);
} else {
throw new RuntimeException("Error: File not found - " . $file['realPath']);
}
}
$this->logger->info("Imported $count stocks");
} else {
$this->logger->info('No Files');
}
} else {
throw new RuntimeException('No Files to Import');
}
}
/**
* @return bool
*/
protected function getFiles($delta = false): bool
{
$finder = Finder::create();
$finder
->in($this->currentDirPath . '/hiltes/h2c/')
->depth(0);
if ($delta) {
$finder->name('/_D/');
} else {
$finder->notName('/_D/');
}
if ($finder->hasResults()) {
foreach ($finder as $file) {
if ($file->getExtension() != 'Ende') {
//prüfen ob Ende Datei vorhanden ist
if (file_exists($this->currentDirPath . '/hiltes/h2c/' . $file->getRelativePathname() . '.Ende')) {
$this->arrData['orgFiles']['data'][] = array(
'realPath' => $file->getRealPath(),
'fileSize' => $file->getFileInfo()->getSize(),
'onlyFileName' => $file->getRelativePathname(),
);
}
}
}
} else {
return false;
}
return true;
}
protected function getStocks()
{
$stocks = $this->stockRepository->findAll();
foreach ($stocks as $stock) {
$this->cachedStockIds[$stock->getProductId()][$stock->getWarehouse()->getId()] = $stock;
}
}
protected function loadFiles($srcFile)
{
try {
$file = new SplFileObject($srcFile);
$this->logger->info('Starte Import von ' . $file->getRealPath());
$count = 0;
while (!$file->eof()) {
$this->processLine($file->fgets());
$count++;
}
} catch (Exception $e) {
$this->logger->error($e->getMessage());
}
if ($this->deleteFiles) {
unlink($srcFile);
unlink($srcFile . '.Ende');
}
$this->logger->info($count . ' Datensätze importiert');
return $count;
}
protected function processLine($line)
{
$data = str_getcsv($line, ';', '"');
if (!empty($data[0])) {
$this->trimArray($data);
$this->saveData($data);
}
}
/**
* @param array $arr
* @return void
*/
private function trimArray(array &$arr): void
{
foreach ($arr as $k => $v) {
$arr[$k] = trim($v);
}
}
/**
* @param array $data
* @return false
*/
protected function saveData(array $data): bool
{
if (!isset($data[3])) {
$this->logger->error('No Warehouse' . $data[3]);
return false;
}
$warehouse = $this->checkWarehouseName(trim($data[3]));
$gtin = $this->checkProduct(substr($data[0], 1));
if (!empty($warehouse) && !empty($this->cachedStockIds[$gtin][$warehouse->getId()])) {
$stock = $this->cachedStockIds[$gtin][$warehouse->getId()];
} else {
$stock = new Stock();
$stock->setProductId($gtin);
$stock->setWarehouse($warehouse);
}
$stock->setInstock((int)$data[1] / 100);
$this->stockRepository->save($stock, true);
return true;
}
/**
* @param string $warehouseName
* @return int
*/
private function checkWarehouseName(string $warehouseName)
{
$warehouseName = ltrim($warehouseName, 0);
if (empty($this->cachedWarehouse[$warehouseName])) {
$warehouse = $this->warehouseRepository->findOneBy(['name' => $warehouseName]);
//Wenn kein Lager gefunden wurde, dann lege es an
if (empty($warehouse)) {
$warehouse = new Warehouse();
//$warehouse->setId((int)$warehouseName);
$warehouse->setName($warehouseName);
$warehouseId = $this->warehouseRepository->save($warehouse, true);
$newWarehouse = $this->warehouseRepository->findOneBy(['id' => $warehouseId]);
$this->cachedWarehouse[$warehouseName] = $newWarehouse;
} else {
$this->cachedWarehouse[$warehouseName] = $warehouse;
}
}
return $this->cachedWarehouse[$warehouseName];
}
/**
* @param string $gtin
* @return false|int|mixed|null
*/
private function checkProduct(string $gtin)
{
#*** WEnn keine geCached Id Vorhanden
if (empty($this->cachedProdIds[$gtin])) {
$product = $this->productRepository->findOneBy(['gtin' => $gtin]);
if (empty($product)) {
$product = new Product();
$product->setGtin($gtin);
$this->cachedProdIds[$gtin] = $this->productRepository->save($product, true);
} else {
$this->cachedProdIds[$gtin] = $product->getId();
}
}
if (!empty($this->cachedProdIds[$gtin])) {
return $this->cachedProdIds[$gtin];
}
return false;
}
}