This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
namespace App\Helper;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
class Ftp
|
||||
{
|
||||
private string $host;
|
||||
@@ -23,6 +25,55 @@ class Ftp
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads a local file to a remote FTP server.
|
||||
*
|
||||
* @param string $localFile The local file path to upload.
|
||||
*
|
||||
* @return void
|
||||
* @throws Exception If the upload fails or encounters an error.
|
||||
*
|
||||
*/
|
||||
public function uploadFile(string $localFile): void
|
||||
{
|
||||
$ftp = ftp_ssl_connect($this->getHost(), $this->getPort());
|
||||
|
||||
if (!$ftp) {
|
||||
throw new Exception("Failed to connect to FTP server");
|
||||
}
|
||||
|
||||
$ftp_login = ftp_login($ftp, $this->getUser(), $this->getPassword());
|
||||
|
||||
if (!$ftp_login) {
|
||||
throw new Exception("FTP login failed");
|
||||
}
|
||||
|
||||
ftp_pasv($ftp, true);
|
||||
|
||||
$remoteFile = 'stock_hiltes.csv';
|
||||
|
||||
if (!file_exists($localFile)) {
|
||||
throw new Exception("Local file does not exist: $localFile");
|
||||
}
|
||||
|
||||
$r = ftp_nb_put($ftp, $this->getRemoteDir() . $remoteFile, $localFile, FTP_BINARY);
|
||||
|
||||
while ($r == FTP_MOREDATA) {
|
||||
// Continue uploading...
|
||||
$r = ftp_nb_continue($ftp);
|
||||
}
|
||||
|
||||
if ($r != FTP_FINISHED) {
|
||||
throw new Exception("File upload failed");
|
||||
}
|
||||
|
||||
ftp_close($ftp);
|
||||
|
||||
if (!unlink($localFile)) {
|
||||
throw new Exception("Failed to delete local file: $localFile");
|
||||
}
|
||||
}
|
||||
|
||||
public function getHost(): string
|
||||
{
|
||||
return $this->host;
|
||||
@@ -32,6 +83,7 @@ class Ftp
|
||||
{
|
||||
$this->host = $host;
|
||||
}
|
||||
|
||||
public function getPort(): int
|
||||
{
|
||||
return $this->port;
|
||||
@@ -71,53 +123,4 @@ class Ftp
|
||||
{
|
||||
$this->remoteDir = $remoteDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads a local file to a remote FTP server.
|
||||
*
|
||||
* @param string $localFile The local file path to upload.
|
||||
*
|
||||
* @throws \Exception If the upload fails or encounters an error.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function uploadFile(string $localFile): void
|
||||
{
|
||||
$ftp = ftp_ssl_connect($this->getHost(), $this->getPort());
|
||||
|
||||
if (!$ftp) {
|
||||
throw new \Exception("Failed to connect to FTP server");
|
||||
}
|
||||
|
||||
$ftp_login = ftp_login($ftp, $this->getUser(), $this->getPassword());
|
||||
|
||||
if (!$ftp_login) {
|
||||
throw new \Exception("FTP login failed");
|
||||
}
|
||||
|
||||
ftp_pasv($ftp, true);
|
||||
|
||||
$remoteFile = 'stock_hiltes.csv';
|
||||
|
||||
if (!file_exists($localFile)) {
|
||||
throw new \Exception("Local file does not exist: $localFile");
|
||||
}
|
||||
|
||||
$r = ftp_nb_put($ftp, $this->getRemoteDir() . $remoteFile, $localFile, FTP_BINARY);
|
||||
|
||||
while ($r == FTP_MOREDATA) {
|
||||
// Continue uploading...
|
||||
$r = ftp_nb_continue($ftp);
|
||||
}
|
||||
|
||||
if ($r != FTP_FINISHED) {
|
||||
throw new \Exception("File upload failed");
|
||||
}
|
||||
|
||||
ftp_close($ftp);
|
||||
|
||||
if (!unlink($localFile)) {
|
||||
throw new \Exception("Failed to delete local file: $localFile");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ namespace App\Helper;
|
||||
|
||||
use App\Entity\Order;
|
||||
use App\Repository\OrderRepository;
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
@@ -97,8 +96,8 @@ class Hiltes
|
||||
if ($order) {
|
||||
|
||||
|
||||
//foreach ($orders as $order) {
|
||||
// if ($order->getStatus() > 0) continue;
|
||||
//foreach ($orders as $order) {
|
||||
// if ($order->getStatus() > 0) continue;
|
||||
$tA = $order->getData();#json_decode($order->getData());
|
||||
#$tA['orderdate'] = "2023-08-30T12:05:24.000Z";
|
||||
|
||||
@@ -211,7 +210,7 @@ class Hiltes
|
||||
dd($arr);
|
||||
|
||||
$data = $this->createJson($arr);
|
||||
// }
|
||||
// }
|
||||
|
||||
return $this->sendOrderToHiltes($data);
|
||||
|
||||
|
||||
@@ -9,118 +9,122 @@ 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 $cachedWarehouseIds;
|
||||
protected $arrData = array();
|
||||
private $productRepository;
|
||||
private $stockRepository;
|
||||
private $stockRepository;
|
||||
private $warehouseRepository;
|
||||
private $logger;
|
||||
protected $currentDirPath;
|
||||
protected $cachedWarehouseIds;
|
||||
private $cachedProdIds;
|
||||
private $cachedStockIds;
|
||||
protected $arrData = array();
|
||||
private $cachedProdIds;
|
||||
private $cachedStockIds;
|
||||
|
||||
public function __construct(ProductRepository $productRepository,WarehouseRepository $warehouseRepository,StockRepository $stockRepository, LoggerInterface $logger)
|
||||
{
|
||||
public function __construct(ProductRepository $productRepository, WarehouseRepository $warehouseRepository, StockRepository $stockRepository, LoggerInterface $logger)
|
||||
{
|
||||
$this->productRepository = $productRepository;
|
||||
$this->warehouseRepository = $warehouseRepository;
|
||||
$this->stockRepository = $stockRepository;
|
||||
$this->stockRepository = $stockRepository;
|
||||
$this->logger = $logger;
|
||||
|
||||
$this->currentDirPath = getcwd();
|
||||
}
|
||||
$this->currentDirPath = getcwd();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array|void
|
||||
*/
|
||||
public function startImport($delta = false)
|
||||
{
|
||||
#*** Holt alle Dateien
|
||||
if($this->getFiles($delta)){
|
||||
#*** Holt alle Stocks und setzt ein Array **************
|
||||
$this->getStocks();
|
||||
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 (!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']);
|
||||
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('Error in getFiles or no file WS.FERTIG');
|
||||
throw new RuntimeException('Error in getFiles or no file WS.FERTIG');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
protected function getFiles($delta = false)
|
||||
{
|
||||
$finder = Finder::create();
|
||||
$finder
|
||||
->in($this->currentDirPath.'/hiltes/h2c/')
|
||||
->depth(0);
|
||||
protected function getFiles($delta = false)
|
||||
{
|
||||
$finder = Finder::create();
|
||||
$finder
|
||||
->in($this->currentDirPath . '/hiltes/h2c/')
|
||||
->depth(0);
|
||||
|
||||
if($delta) {
|
||||
if ($delta) {
|
||||
$finder->name('/_D/');
|
||||
}else{
|
||||
} else {
|
||||
$finder->notName('/_D/');
|
||||
}
|
||||
|
||||
if ($finder->hasResults()) {
|
||||
foreach ($finder as $file) {
|
||||
if($file->getExtension() != 'Ende') {
|
||||
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')) {
|
||||
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(),
|
||||
'realPath' => $file->getRealPath(),
|
||||
'fileSize' => $file->getFileInfo()->getSize(),
|
||||
'onlyFileName' => $file->getRelativePathname(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
protected function getStocks()
|
||||
{
|
||||
}
|
||||
|
||||
protected function getStocks()
|
||||
{
|
||||
$stocks = $this->stockRepository->findAll();
|
||||
|
||||
foreach ($stocks as $stock) {
|
||||
$this->cachedStockIds[$stock->getProductId()][$stock->getWarehouse()->getId()] = $stock;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function loadFiles($srcFile)
|
||||
{
|
||||
protected function loadFiles($srcFile)
|
||||
{
|
||||
try {
|
||||
$file = new \SplFileObject($srcFile);
|
||||
$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) {
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
|
||||
@@ -130,7 +134,7 @@ class HiltesImport
|
||||
$this->logger->info($count . ' Datensätze importiert');
|
||||
|
||||
return $count;
|
||||
}
|
||||
}
|
||||
|
||||
protected function processLine($line)
|
||||
{
|
||||
@@ -139,15 +143,15 @@ class HiltesImport
|
||||
if (!empty($data[0])) {
|
||||
$this->trimArray($data);
|
||||
$this->saveData($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function trimArray(array &$arr)
|
||||
{
|
||||
foreach ($arr as $k=>$v) {
|
||||
$arr[$k] = trim($v);
|
||||
}
|
||||
}
|
||||
foreach ($arr as $k => $v) {
|
||||
$arr[$k] = trim($v);
|
||||
}
|
||||
}
|
||||
|
||||
protected function saveData(array $data)
|
||||
{
|
||||
@@ -156,63 +160,64 @@ class HiltesImport
|
||||
|
||||
if (!empty($warehouse) && !empty($this->cachedStockIds[$gtin][$warehouse->getId()])) {
|
||||
$stock = $this->cachedStockIds[$gtin][$warehouse->getId()];
|
||||
}else{
|
||||
$stock = new Stock();
|
||||
$stock->setProductId($gtin);
|
||||
} else {
|
||||
$stock = new Stock();
|
||||
$stock->setProductId($gtin);
|
||||
$stock->setWarehouse($warehouse);
|
||||
}
|
||||
}
|
||||
|
||||
$stock->setInstock((int) $data[1] / 100);
|
||||
$this->stockRepository->save($stock,true);
|
||||
}
|
||||
$stock->setInstock((int)$data[1] / 100);
|
||||
$this->stockRepository->save($stock, true);
|
||||
}
|
||||
|
||||
private function checkWarehouseName(string $warehouseName)
|
||||
{
|
||||
$warehouseName = ltrim($warehouseName, 0);
|
||||
|
||||
if (empty($this->cachedWarehouseIds[$warehouseName])) {
|
||||
$warehouse = $this->warehouseRepository->findOneBy(['id' => (int) $warehouseName]);
|
||||
$warehouse = $this->warehouseRepository->findOneBy(['id' => (int)$warehouseName]);
|
||||
|
||||
if (empty($warehouse)) {
|
||||
$warehouse = new Warehouse();
|
||||
$warehouse->setId((int) $warehouseName);
|
||||
$warehouse = new Warehouse();
|
||||
$warehouse->setId((int)$warehouseName);
|
||||
$warehouse->setName($warehouseName);
|
||||
$this->warehouseRepository->save($warehouse,true);
|
||||
$this->warehouseRepository->save($warehouse, true);
|
||||
$this->cachedWarehouseIds[$warehouseName] = $warehouse;
|
||||
}else{
|
||||
} else {
|
||||
$this->cachedWarehouseIds[$warehouseName] = $warehouse;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($this->cachedWarehouseIds[$warehouseName])) {
|
||||
return $this->cachedWarehouseIds[$warehouseName];
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $gtin
|
||||
* @return false|int|mixed|null
|
||||
*/
|
||||
private function checkProduct(string $gtin){
|
||||
#*** WEnn keine geCached Id Vorhanden
|
||||
if(empty($this->cachedProdIds[$gtin])){
|
||||
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{
|
||||
} else {
|
||||
$this->cachedProdIds[$gtin] = $product->getId();
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($this->cachedProdIds[$gtin])) {
|
||||
return $this->cachedProdIds[$gtin];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
if (!empty($this->cachedProdIds[$gtin])) {
|
||||
return $this->cachedProdIds[$gtin];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,10 @@ namespace App\Helper;
|
||||
use App\Repository\ProductRepository;
|
||||
use App\Repository\StockRepository;
|
||||
use App\Repository\WarehouseRepository;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ArrayIterator;
|
||||
use Exception;
|
||||
use League\Csv\Writer;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
|
||||
class Jtl
|
||||
@@ -28,7 +30,7 @@ class Jtl
|
||||
10 => 'Lager 10 - OJ EF',
|
||||
);
|
||||
|
||||
public function __construct(ProductRepository $productRepository,WarehouseRepository $warehouseRepository,StockRepository $stockRepository, LoggerInterface $logger)
|
||||
public function __construct(ProductRepository $productRepository, WarehouseRepository $warehouseRepository, StockRepository $stockRepository, LoggerInterface $logger)
|
||||
{
|
||||
$this->productRepository = $productRepository;
|
||||
$this->warehouseRepository = $warehouseRepository;
|
||||
@@ -47,31 +49,31 @@ class Jtl
|
||||
|
||||
$data = array();
|
||||
|
||||
foreach($r as $product){
|
||||
$stock = $this->stockRepository->findBy(['product_id'=>$product->getId()]);
|
||||
foreach ($r as $product) {
|
||||
$stock = $this->stockRepository->findBy(['product_id' => $product->getId()]);
|
||||
|
||||
if($stock){
|
||||
foreach($stock as $s){
|
||||
if ($stock) {
|
||||
foreach ($stock as $s) {
|
||||
$warehouse = $s->getWarehouse();
|
||||
$warehouseName = $warehouse->getName();
|
||||
|
||||
$data[$product->getId().$warehouseName] = [
|
||||
$data[$product->getId() . $warehouseName] = [
|
||||
'gtin' => $product->getGtin(),
|
||||
'stock' => $s->getInstock(),
|
||||
'warehouse' => $this->arrLager[$warehouseName] ?? 'Lager ' . $warehouseName
|
||||
];
|
||||
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
$data[$product->getId()] = [
|
||||
'gtin' => $product->getGtin(),
|
||||
'stock' => 0,
|
||||
'warehouse' => 0
|
||||
];
|
||||
|
||||
$this->logger->info('No stock for product '.$product->getId());
|
||||
$this->logger->info('No stock for product ' . $product->getId());
|
||||
}
|
||||
// dump($data);
|
||||
// dump($data);
|
||||
|
||||
}
|
||||
|
||||
@@ -86,22 +88,20 @@ class Jtl
|
||||
public function createExportFile($data): void
|
||||
{
|
||||
try {
|
||||
$writer = Writer::createFromPath(getcwd().'/jtl/cds-export.csv', 'w+');
|
||||
$bytes = $writer->insertAll(new \ArrayIterator($data));
|
||||
$writer = Writer::createFromPath(getcwd() . '/jtl/cds-export.csv', 'w+');
|
||||
$bytes = $writer->insertAll(new ArrayIterator($data));
|
||||
|
||||
if($bytes){
|
||||
$this->logger->info('Exported '.$bytes.' bytes');
|
||||
if ($bytes) {
|
||||
$this->logger->info('Exported ' . $bytes . ' bytes');
|
||||
|
||||
$FTP = new Ftp();
|
||||
$FTP->uploadFile(getcwd().'/jtl/cds-export.csv');
|
||||
$FTP->uploadFile(getcwd() . '/jtl/cds-export.csv');
|
||||
}
|
||||
|
||||
}catch (\Exception $e){
|
||||
} catch (Exception $e) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Helper;
|
||||
|
||||
use Symfony\Component\Notifier\Chatter;
|
||||
use Symfony\Component\Notifier\ChatterInterface;
|
||||
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
|
||||
use Symfony\Component\Notifier\Message\ChatMessage;
|
||||
@@ -16,7 +15,7 @@ class Slack
|
||||
$this->chatter = $chatter;
|
||||
}
|
||||
|
||||
public function sendMessage(String $message): void
|
||||
public function sendMessage(string $message): void
|
||||
{
|
||||
$message = (new ChatMessage($message))
|
||||
->transport('slack');
|
||||
|
||||
Reference in New Issue
Block a user