From 16aac3adc6d21afb1a4e08d522ad0f48eb83bca2 Mon Sep 17 00:00:00 2001 From: Marko <52066939+HeX87@users.noreply.github.com> Date: Fri, 24 Nov 2023 11:36:51 +0100 Subject: [PATCH] add fixed export --- config/services.yaml | 41 +++++---- src/Command/HiltesImportCommand.php | 29 +++++-- src/Helper/Ftp.php | 17 ++-- src/Helper/HiltesImport.php | 30 +++++-- src/Helper/Jtl.php | 116 ++++++++++++++++++------- src/Repository/StockRepository.php | 27 +++--- src/Repository/WarehouseRepository.php | 27 +++--- 7 files changed, 193 insertions(+), 94 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index e20161b..e861053 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -6,22 +6,29 @@ parameters: services: - # default configuration for services in *this* file - _defaults: - autowire: true # Automatically injects dependencies in your services. - autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. + # default configuration for services in *this* file + _defaults: + autowire: true # Automatically injects dependencies in your services. + autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. - # makes classes in src/ available to be used as services - # this creates a service per class whose id is the fully-qualified class name - App\: - resource: '../src/' - exclude: - - '../src/DependencyInjection/' - - '../src/Entity/' - - '../src/Kernel.php' - App\EventSubscriber\SlackNotifySubscriber: - arguments: - $slackWebhookUrl: '%env(SLACK_DSN)%' + # makes classes in src/ available to be used as services + # this creates a service per class whose id is the fully-qualified class name + App\: + resource: '../src/' + exclude: + - '../src/DependencyInjection/' + - '../src/Entity/' + - '../src/Kernel.php' + App\EventSubscriber\SlackNotifySubscriber: + arguments: + $slackWebhookUrl: '%env(SLACK_DSN)%' + App\Helper\Jtl: + arguments: + $rootPath: '%kernel.project_dir%' + App\Helper\HiltesImport: + arguments: + $rootPath: '%kernel.project_dir%' - # add more service definitions when explicit configuration is needed - # please note that last definitions always *replace* previous ones \ No newline at end of file + + # add more service definitions when explicit configuration is needed + # please note that last definitions always *replace* previous ones \ No newline at end of file diff --git a/src/Command/HiltesImportCommand.php b/src/Command/HiltesImportCommand.php index a53edda..bd09103 100644 --- a/src/Command/HiltesImportCommand.php +++ b/src/Command/HiltesImportCommand.php @@ -14,6 +14,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\HttpKernel\KernelInterface; #[AsCommand( name: 'hiltes:import', @@ -50,11 +51,11 @@ class HiltesImportCommand extends Command $io = new SymfonyStyle($input, $output); $io->success('Start Hiltes Import'); - + $rootPath = $this->GetProjectRootDir(); /** * @var HiltesImport */ - $hiltesImport = new HiltesImport($this->productRepository, $this->warehouseRepository, $this->stockRepository, $this->logger); + $hiltesImport = new HiltesImport($this->productRepository, $this->warehouseRepository, $this->stockRepository, $this->logger, $rootPath); $hiltesImport->startImport($input->getOption('delta')); @@ -66,13 +67,31 @@ class HiltesImportCommand extends Command } else { $io->info('Start Hiltes Push JTL'); - $jtl = new Jtl($this->productRepository, $this->warehouseRepository, $this->stockRepository, $this->logger); + $jtl = new Jtl($this->productRepository, $this->warehouseRepository, $this->stockRepository, $this->logger, $rootPath); - $jtl->createExportFile($jtl->getProducts(0), 'standard'); - $jtl->createExportFile($jtl->getProducts(8), 'wms'); + //Export für Standartlager + dump("Standard"); + $jtl->createExportFile($jtl->getProducts(['1', '3', '5', '10']), 'standard'); + dump("WMS"); + //Export für WMS Lager + $jtl->createExportFile($jtl->getProducts(['8']), 'wms'); $io->success('Done.'); return Command::SUCCESS; } } + + public static function GetProjectRootDir() + { + $dirFullPath = __DIR__; + + //PRE: $dirs = /app/public/src/Helpers + $dirs = explode('/', $dirFullPath); + + array_pop($dirs); //remove last element in array ('Helpers') + array_pop($dirs); //remove the next last element from array ('src') + + //POST: $dirs = /app/public + return implode('/', $dirs); + } } \ No newline at end of file diff --git a/src/Helper/Ftp.php b/src/Helper/Ftp.php index c0757a0..115aa48 100644 --- a/src/Helper/Ftp.php +++ b/src/Helper/Ftp.php @@ -29,12 +29,11 @@ class Ftp * Uploads a local file to a remote FTP server. * * @param string $localFile The local file path to upload. - * + * @param string $warehouse * @return void * @throws Exception If the upload fails or encounters an error. - * */ - public function uploadFile(string $localFile): void + public function uploadFile(string $localFile, string $warehouse): void { $ftp = ftp_ssl_connect($this->getHost(), $this->getPort()); @@ -49,8 +48,9 @@ class Ftp } ftp_pasv($ftp, true); + ftp_set_option($ftp, FTP_TIMEOUT_SEC, 120); - $remoteFile = 'stock_hiltes.csv'; + $remoteFile = 'stock_' . $warehouse . '.csv'; if (!file_exists($localFile)) { throw new Exception("Local file does not exist: $localFile"); @@ -60,7 +60,7 @@ class Ftp while ($r == FTP_MOREDATA) { // Continue uploading... - $r = ftp_nb_continue($ftp); + $r = @ftp_nb_continue($ftp); } if ($r != FTP_FINISHED) { @@ -74,11 +74,18 @@ class Ftp } } + /** + * @return string + */ public function getHost(): string { return $this->host; } + /** + * @param string $host + * @return void + */ public function setHost(string $host): void { $this->host = $host; diff --git a/src/Helper/HiltesImport.php b/src/Helper/HiltesImport.php index ab191c1..3f79e23 100644 --- a/src/Helper/HiltesImport.php +++ b/src/Helper/HiltesImport.php @@ -27,15 +27,17 @@ class HiltesImport private $logger; private $cachedProdIds; private $cachedStockIds; + private $rootPath; - public function __construct(ProductRepository $productRepository, WarehouseRepository $warehouseRepository, StockRepository $stockRepository, LoggerInterface $logger) + 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 = getcwd() . '/www'; + $this->currentDirPath = $this->rootPath; } @@ -71,7 +73,7 @@ class HiltesImport /** * @return bool */ - protected function getFiles($delta = false) + protected function getFiles($delta = false): bool { $finder = Finder::create(); $finder @@ -128,8 +130,10 @@ class HiltesImport $this->logger->error($e->getMessage()); } - unlink($srcFile); - unlink($srcFile . '.Ende'); +// unlink($srcFile); +// unlink($srcFile . '.Ende'); + + unlink($this->rootPath . '/hiltes/h2c/WS.FERTIG'); $this->logger->info($count . ' Datensätze importiert'); @@ -146,14 +150,22 @@ class HiltesImport } } - protected function trimArray(array &$arr) + /** + * @param array $arr + * @return void + */ + protected function trimArray(array &$arr): void { foreach ($arr as $k => $v) { $arr[$k] = trim($v); } } - protected function saveData(array $data) + /** + * @param array $data + * @return void + */ + protected function saveData(array $data): void { $warehouse = $this->checkWarehouseName($data[3]); $gtin = $this->checkProduct(substr($data[0], 1)); @@ -170,6 +182,10 @@ class HiltesImport $this->stockRepository->save($stock, true); } + /** + * @param string $warehouseName + * @return Warehouse|false|mixed|null + */ private function checkWarehouseName(string $warehouseName) { $warehouseName = ltrim($warehouseName, 0); diff --git a/src/Helper/Jtl.php b/src/Helper/Jtl.php index b8eb331..7f7318a 100644 --- a/src/Helper/Jtl.php +++ b/src/Helper/Jtl.php @@ -2,10 +2,12 @@ namespace App\Helper; +use App\Entity\Warehouse; use App\Repository\ProductRepository; use App\Repository\StockRepository; use App\Repository\WarehouseRepository; use ArrayIterator; +use Doctrine\Common\Collections\Criteria; use Exception; use League\Csv\Writer; use Psr\Log\LoggerInterface; @@ -17,12 +19,13 @@ class Jtl private $stockRepository; private $warehouseRepository; private $logger; + private $rootPath; /** * @var string[] $arrLager * Lagernamen aus der JTL Wawi */ - private $arrLager = array( + private array $arrLager = array( 1 => 'Lager 1 - Standardlager', 3 => 'Lager 3 - DLX', 5 => 'Lager 5 - Verkaufsfilale', @@ -30,74 +33,123 @@ class Jtl 10 => 'Lager 10 - OJ EF', ); - public function __construct(ProductRepository $productRepository, WarehouseRepository $warehouseRepository, StockRepository $stockRepository, LoggerInterface $logger) + /** + * @param ProductRepository $productRepository + * @param WarehouseRepository $warehouseRepository + * @param StockRepository $stockRepository + * @param LoggerInterface $logger + * @param string $rootPath + */ + 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; } /** * Holt alle Produkte und deren Lagerbestände + * @param array $warehousesName * @return array */ - public function getProducts(int $warehouseId): array + public function getProducts(array $warehousesName): array { - - $r = $this->productRepository->findAll(); $data = array(); + $products = $this->productRepository->findAll(); - foreach ($r as $product) { + foreach ($warehousesName as $wn) { + $warehouse = $this->warehouseRepository->findByWarehouseByName($wn); + } - if ($warehouseId != 0) { - $stock = $this->stockRepository->findBy(['product_id' => $product->getId(), 'warehouse_id' => $warehouseId]); - } else { - $stock = $this->stockRepository->findBy(['product_id' => $product->getId()]); - } + if ($warehouse) { + foreach ($warehouse as $w) { - if ($stock) { - foreach ($stock as $s) { - $warehouse = $s->getWarehouse(); - $warehouseName = $warehouse->getName(); + $stock = $this->stockRepository->findBy(['warehouse' => $w]); - $data[$product->getId() . $warehouseName] = [ - 'gtin' => $product->getGtin(), - 'stock' => $s->getInstock(), - 'warehouse' => $this->arrLager[$warehouseName] ?? 'Lager ' . $warehouseName - ]; + if ($stock) { + foreach ($stock as $s) { + $warehouse = $s->getWarehouse(); + $warehouseName = $warehouse->getName(); + $data[$s->getProductId() . $warehouseName] = [ + 'gtin' => $products[$s->getProductId()]->getGtin(), + 'stock' => $s->getInstock(), + 'warehouse' => $this->arrLager[$warehouseName] ?? 'Lager ' . $warehouseName + ]; + + } + } else { + //Wenn Product nicht gefunden werden kann, dann setzte Bestand auf Null +// $data[] = [ +// 'gtin' => $products[$s->getProductId()]->getGtin(), +// 'stock' => 0, +// 'warehouse' => 0 +// ]; +// $this->logger->info('No stock for product ' . s->getProductId()->getId()); } - } else { - $data[$product->getId()] = [ - 'gtin' => $product->getGtin(), - 'stock' => 0, - 'warehouse' => 0 - ]; - - $this->logger->info('No stock for product ' . $product->getId()); } } + +// foreach ($r as $product) { +// +// $stock = $this->stockRepository->findBy(['product_id' => $product->getId()]); +// +// if ($stock) { +// foreach ($stock as $s) { +// $warehouse = $s->getWarehouse(); +// $warehouseName = $warehouse->getName(); +// +// $data[$product->getId() . $warehouseName] = [ +// 'gtin' => $product->getGtin(), +// 'stock' => $s->getInstock(), +// 'warehouse' => $this->arrLager[$warehouseName] ?? 'Lager ' . $warehouseName +// ]; +// +// } +// } else { +// $data[$product->getId()] = [ +// 'gtin' => $product->getGtin(), +// 'stock' => 0, +// 'warehouse' => 0 +// ]; +// +// $this->logger->info('No stock for product ' . $product->getId()); +// } +// } + return $data; } /** - * @param $data + * @param array $data + * @param string $warehouse * @return void */ - public function createExportFile($data, $warehouse): void + public function createExportFile(array $data, string $warehouse): void { try { - $writer = Writer::createFromPath(getcwd() . '/www/jtl/' . $warehouse . '.csv', 'w+'); + + $file = $this->rootPath . '/jtl/' . $warehouse . '.csv'; + + dump($file); + + $writer = Writer::createFromPath($file, 'w+'); $bytes = $writer->insertAll(new ArrayIterator($data)); if ($bytes) { $this->logger->info('Exported ' . $bytes . ' bytes'); $FTP = new Ftp(); - $FTP->uploadFile(getcwd() . '/www/jtl/' . $warehouse . '.csv'); + $FTP->uploadFile($file, $warehouse); } } catch (Exception $e) { diff --git a/src/Repository/StockRepository.php b/src/Repository/StockRepository.php index e3c6da7..33a34ef 100644 --- a/src/Repository/StockRepository.php +++ b/src/Repository/StockRepository.php @@ -42,20 +42,19 @@ class StockRepository extends ServiceEntityRepository } } -// /** -// * @return Stock[] Returns an array of Stock objects -// */ -// public function findByExampleField($value): array -// { -// return $this->createQueryBuilder('s') -// ->andWhere('s.exampleField = :val') -// ->setParameter('val', $value) -// ->orderBy('s.id', 'ASC') -// ->setMaxResults(10) -// ->getQuery() -// ->getResult() -// ; -// } + /** + * @return Stock[] Returns an array of Stock objects + */ + public function findByWarehouseId($warehouseId): array + { + return $this->createQueryBuilder('s') + ->join('s.warehouse', 'w') + ->andWhere('w.warehouse_id = :val') + ->setParameter('val', $warehouseId) + ->orderBy('s.id', 'ASC') + ->getQuery() + ->getResult(); + } // public function findOneBySomeField($value): ?Stock // { diff --git a/src/Repository/WarehouseRepository.php b/src/Repository/WarehouseRepository.php index d94cf3b..99db806 100644 --- a/src/Repository/WarehouseRepository.php +++ b/src/Repository/WarehouseRepository.php @@ -42,20 +42,19 @@ class WarehouseRepository extends ServiceEntityRepository } } -// /** -// * @return Warehouse[] Returns an array of Warehouse objects -// */ -// public function findByExampleField($value): array -// { -// return $this->createQueryBuilder('w') -// ->andWhere('w.exampleField = :val') -// ->setParameter('val', $value) -// ->orderBy('w.id', 'ASC') -// ->setMaxResults(10) -// ->getQuery() -// ->getResult() -// ; -// } + /** + * @param $value + * @return Warehouse[] Returns an array of Warehouse objects + */ + public function findByWarehouseByName($value): array + { + return $this->createQueryBuilder('w') + ->andWhere('w.name = :val') + ->setParameter('val', $value) + ->orderBy('w.id', 'ASC') + ->getQuery() + ->getResult(); + } // public function findOneBySomeField($value): ?Warehouse // {