add fixed export
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Marko 2023-11-24 11:36:51 +01:00
parent 6e98fca7ad
commit 16aac3adc6
No known key found for this signature in database
7 changed files with 193 additions and 94 deletions

View File

@ -22,6 +22,13 @@ services:
App\EventSubscriber\SlackNotifySubscriber: App\EventSubscriber\SlackNotifySubscriber:
arguments: arguments:
$slackWebhookUrl: '%env(SLACK_DSN)%' $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 # add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones # please note that last definitions always *replace* previous ones

View File

@ -14,6 +14,7 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpKernel\KernelInterface;
#[AsCommand( #[AsCommand(
name: 'hiltes:import', name: 'hiltes:import',
@ -50,11 +51,11 @@ class HiltesImportCommand extends Command
$io = new SymfonyStyle($input, $output); $io = new SymfonyStyle($input, $output);
$io->success('Start Hiltes Import'); $io->success('Start Hiltes Import');
$rootPath = $this->GetProjectRootDir();
/** /**
* @var HiltesImport * @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')); $hiltesImport->startImport($input->getOption('delta'));
@ -66,13 +67,31 @@ class HiltesImportCommand extends Command
} else { } else {
$io->info('Start Hiltes Push JTL'); $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'); //Export für Standartlager
$jtl->createExportFile($jtl->getProducts(8), 'wms'); 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.'); $io->success('Done.');
return Command::SUCCESS; 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);
}
} }

View File

@ -29,12 +29,11 @@ class Ftp
* Uploads a local file to a remote FTP server. * Uploads a local file to a remote FTP server.
* *
* @param string $localFile The local file path to upload. * @param string $localFile The local file path to upload.
* * @param string $warehouse
* @return void * @return void
* @throws Exception If the upload fails or encounters an error. * @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()); $ftp = ftp_ssl_connect($this->getHost(), $this->getPort());
@ -49,8 +48,9 @@ class Ftp
} }
ftp_pasv($ftp, true); ftp_pasv($ftp, true);
ftp_set_option($ftp, FTP_TIMEOUT_SEC, 120);
$remoteFile = 'stock_hiltes.csv'; $remoteFile = 'stock_' . $warehouse . '.csv';
if (!file_exists($localFile)) { if (!file_exists($localFile)) {
throw new Exception("Local file does not exist: $localFile"); throw new Exception("Local file does not exist: $localFile");
@ -60,7 +60,7 @@ class Ftp
while ($r == FTP_MOREDATA) { while ($r == FTP_MOREDATA) {
// Continue uploading... // Continue uploading...
$r = ftp_nb_continue($ftp); $r = @ftp_nb_continue($ftp);
} }
if ($r != FTP_FINISHED) { if ($r != FTP_FINISHED) {
@ -74,11 +74,18 @@ class Ftp
} }
} }
/**
* @return string
*/
public function getHost(): string public function getHost(): string
{ {
return $this->host; return $this->host;
} }
/**
* @param string $host
* @return void
*/
public function setHost(string $host): void public function setHost(string $host): void
{ {
$this->host = $host; $this->host = $host;

View File

@ -27,15 +27,17 @@ class HiltesImport
private $logger; private $logger;
private $cachedProdIds; private $cachedProdIds;
private $cachedStockIds; 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->productRepository = $productRepository;
$this->warehouseRepository = $warehouseRepository; $this->warehouseRepository = $warehouseRepository;
$this->stockRepository = $stockRepository; $this->stockRepository = $stockRepository;
$this->logger = $logger; $this->logger = $logger;
$this->rootPath = $rootPath;
$this->currentDirPath = getcwd() . '/www'; $this->currentDirPath = $this->rootPath;
} }
@ -71,7 +73,7 @@ class HiltesImport
/** /**
* @return bool * @return bool
*/ */
protected function getFiles($delta = false) protected function getFiles($delta = false): bool
{ {
$finder = Finder::create(); $finder = Finder::create();
$finder $finder
@ -128,8 +130,10 @@ class HiltesImport
$this->logger->error($e->getMessage()); $this->logger->error($e->getMessage());
} }
unlink($srcFile); // unlink($srcFile);
unlink($srcFile . '.Ende'); // unlink($srcFile . '.Ende');
unlink($this->rootPath . '/hiltes/h2c/WS.FERTIG');
$this->logger->info($count . ' Datensätze importiert'); $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) { foreach ($arr as $k => $v) {
$arr[$k] = trim($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]); $warehouse = $this->checkWarehouseName($data[3]);
$gtin = $this->checkProduct(substr($data[0], 1)); $gtin = $this->checkProduct(substr($data[0], 1));
@ -170,6 +182,10 @@ class HiltesImport
$this->stockRepository->save($stock, true); $this->stockRepository->save($stock, true);
} }
/**
* @param string $warehouseName
* @return Warehouse|false|mixed|null
*/
private function checkWarehouseName(string $warehouseName) private function checkWarehouseName(string $warehouseName)
{ {
$warehouseName = ltrim($warehouseName, 0); $warehouseName = ltrim($warehouseName, 0);

View File

@ -2,10 +2,12 @@
namespace App\Helper; namespace App\Helper;
use App\Entity\Warehouse;
use App\Repository\ProductRepository; use App\Repository\ProductRepository;
use App\Repository\StockRepository; use App\Repository\StockRepository;
use App\Repository\WarehouseRepository; use App\Repository\WarehouseRepository;
use ArrayIterator; use ArrayIterator;
use Doctrine\Common\Collections\Criteria;
use Exception; use Exception;
use League\Csv\Writer; use League\Csv\Writer;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -17,12 +19,13 @@ class Jtl
private $stockRepository; private $stockRepository;
private $warehouseRepository; private $warehouseRepository;
private $logger; private $logger;
private $rootPath;
/** /**
* @var string[] $arrLager * @var string[] $arrLager
* Lagernamen aus der JTL Wawi * Lagernamen aus der JTL Wawi
*/ */
private $arrLager = array( private array $arrLager = array(
1 => 'Lager 1 - Standardlager', 1 => 'Lager 1 - Standardlager',
3 => 'Lager 3 - DLX', 3 => 'Lager 3 - DLX',
5 => 'Lager 5 - Verkaufsfilale', 5 => 'Lager 5 - Verkaufsfilale',
@ -30,74 +33,123 @@ class Jtl
10 => 'Lager 10 - OJ EF', 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->productRepository = $productRepository;
$this->warehouseRepository = $warehouseRepository; $this->warehouseRepository = $warehouseRepository;
$this->stockRepository = $stockRepository; $this->stockRepository = $stockRepository;
$this->logger = $logger; $this->logger = $logger;
$this->rootPath = $rootPath;
} }
/** /**
* Holt alle Produkte und deren Lagerbestände * Holt alle Produkte und deren Lagerbestände
* @param array $warehousesName
* @return array * @return array
*/ */
public function getProducts(int $warehouseId): array public function getProducts(array $warehousesName): array
{ {
$r = $this->productRepository->findAll();
$data = array(); $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) {
$stock = $this->stockRepository->findBy(['warehouse' => $w]);
if ($stock) { if ($stock) {
foreach ($stock as $s) { foreach ($stock as $s) {
$warehouse = $s->getWarehouse(); $warehouse = $s->getWarehouse();
$warehouseName = $warehouse->getName(); $warehouseName = $warehouse->getName();
$data[$product->getId() . $warehouseName] = [ $data[$s->getProductId() . $warehouseName] = [
'gtin' => $product->getGtin(), 'gtin' => $products[$s->getProductId()]->getGtin(),
'stock' => $s->getInstock(), 'stock' => $s->getInstock(),
'warehouse' => $this->arrLager[$warehouseName] ?? 'Lager ' . $warehouseName 'warehouse' => $this->arrLager[$warehouseName] ?? 'Lager ' . $warehouseName
]; ];
} }
} else { } else {
$data[$product->getId()] = [ //Wenn Product nicht gefunden werden kann, dann setzte Bestand auf Null
'gtin' => $product->getGtin(), // $data[] = [
'stock' => 0, // 'gtin' => $products[$s->getProductId()]->getGtin(),
'warehouse' => 0 // 'stock' => 0,
]; // 'warehouse' => 0
// ];
// $this->logger->info('No stock for product ' . s->getProductId()->getId());
}
}
}
$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; return $data;
} }
/** /**
* @param $data * @param array $data
* @param string $warehouse
* @return void * @return void
*/ */
public function createExportFile($data, $warehouse): void public function createExportFile(array $data, string $warehouse): void
{ {
try { 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)); $bytes = $writer->insertAll(new ArrayIterator($data));
if ($bytes) { if ($bytes) {
$this->logger->info('Exported ' . $bytes . ' bytes'); $this->logger->info('Exported ' . $bytes . ' bytes');
$FTP = new Ftp(); $FTP = new Ftp();
$FTP->uploadFile(getcwd() . '/www/jtl/' . $warehouse . '.csv'); $FTP->uploadFile($file, $warehouse);
} }
} catch (Exception $e) { } catch (Exception $e) {

View File

@ -42,20 +42,19 @@ class StockRepository extends ServiceEntityRepository
} }
} }
// /** /**
// * @return Stock[] Returns an array of Stock objects * @return Stock[] Returns an array of Stock objects
// */ */
// public function findByExampleField($value): array public function findByWarehouseId($warehouseId): array
// { {
// return $this->createQueryBuilder('s') return $this->createQueryBuilder('s')
// ->andWhere('s.exampleField = :val') ->join('s.warehouse', 'w')
// ->setParameter('val', $value) ->andWhere('w.warehouse_id = :val')
// ->orderBy('s.id', 'ASC') ->setParameter('val', $warehouseId)
// ->setMaxResults(10) ->orderBy('s.id', 'ASC')
// ->getQuery() ->getQuery()
// ->getResult() ->getResult();
// ; }
// }
// public function findOneBySomeField($value): ?Stock // public function findOneBySomeField($value): ?Stock
// { // {

View File

@ -42,20 +42,19 @@ class WarehouseRepository extends ServiceEntityRepository
} }
} }
// /** /**
// * @return Warehouse[] Returns an array of Warehouse objects * @param $value
// */ * @return Warehouse[] Returns an array of Warehouse objects
// public function findByExampleField($value): array */
// { public function findByWarehouseByName($value): array
// return $this->createQueryBuilder('w') {
// ->andWhere('w.exampleField = :val') return $this->createQueryBuilder('w')
// ->setParameter('val', $value) ->andWhere('w.name = :val')
// ->orderBy('w.id', 'ASC') ->setParameter('val', $value)
// ->setMaxResults(10) ->orderBy('w.id', 'ASC')
// ->getQuery() ->getQuery()
// ->getResult() ->getResult();
// ; }
// }
// public function findOneBySomeField($value): ?Warehouse // public function findOneBySomeField($value): ?Warehouse
// { // {