From 2747d53c5047712e15126db927702d00e6a3d2bb Mon Sep 17 00:00:00 2001 From: Marko <52066939+HeX87@users.noreply.github.com> Date: Fri, 15 Dec 2023 17:51:24 +0100 Subject: [PATCH] add fixed export --- .env | 2 +- src/Command/HiltesImportCommand.php | 1 - src/EventSubscriber/SlackNotifySubscriber.php | 59 ++++++++--- src/Helper/Jtl.php | 42 ++++---- tests/Helper/HiltesImportTest.php | 98 +++++++++++++++++++ 5 files changed, 162 insertions(+), 40 deletions(-) create mode 100644 tests/Helper/HiltesImportTest.php diff --git a/.env b/.env index 42a14ca..4d398a9 100644 --- a/.env +++ b/.env @@ -15,7 +15,7 @@ # https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration ###> symfony/framework-bundle ### -APP_ENV=dev +APP_ENV=prod APP_SECRET=e5a2fe31ff8ce325266d52632a0ba5df ###< symfony/framework-bundle ### diff --git a/src/Command/HiltesImportCommand.php b/src/Command/HiltesImportCommand.php index 15096ad..b7dabea 100644 --- a/src/Command/HiltesImportCommand.php +++ b/src/Command/HiltesImportCommand.php @@ -60,7 +60,6 @@ class HiltesImportCommand extends Command */ $hiltesImport = new HiltesImport($this->productRepository, $this->warehouseRepository, $this->stockRepository, $this->logger, $rootPath); - $prodIds = $hiltesImport->startImport($delta); if (isset($r['error'])) { diff --git a/src/EventSubscriber/SlackNotifySubscriber.php b/src/EventSubscriber/SlackNotifySubscriber.php index 209fc64..43b7984 100644 --- a/src/EventSubscriber/SlackNotifySubscriber.php +++ b/src/EventSubscriber/SlackNotifySubscriber.php @@ -4,7 +4,9 @@ namespace App\EventSubscriber; use ApiPlatform\Symfony\EventListener\EventPriorities; use App\Entity\Order; -use App\Helper\Slack; +use App\Repository\ProductRepository; +use App\Repository\StockRepository; +use App\Repository\WarehouseRepository; use Maknz\Slack\Client; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Request; @@ -18,8 +20,11 @@ class SlackNotifySubscriber implements EventSubscriberInterface * @var Slack */ private $slackWebhookUrl; + private $productRepository; + private $stockRepository; + private $warehouseRepository; - public function __construct(string $slackWebhookUrl) + public function __construct(string $slackWebhookUrl, ProductRepository $productRepository, StockRepository $stockRepository, WarehouseRepository $warehouseRepository) { $this->slackWebhookUrl = $slackWebhookUrl; } @@ -38,13 +43,7 @@ class SlackNotifySubscriber implements EventSubscriberInterface */ public function sendSlack(ViewEvent $event): void { - $slack = new Client($this->slackWebhookUrl, [ - 'username' => 'CDS-Notify', - 'channel' => '#online-verkäufe', - 'link_names' => true, - 'icon' => ':robot_face:', - 'allow_markdown' => true, - ]); + $order = $event->getControllerResult(); $method = $event->getRequest()->getMethod(); @@ -54,19 +53,51 @@ class SlackNotifySubscriber implements EventSubscriberInterface return; } - $msg = "Bestellung {$order->getOrderId()}: \n"; foreach ($order->getData()[0]['positions'] as $item) { if ($item['menge'] > 0 && strlen($item['sku']) > 0) { + + $warehouse = $this->getWarehouseByGtin($item['sku']); + + $slack = new Client($this->slackWebhookUrl, [ + 'username' => 'CDS-Notify', + 'channel' => '#online-verkäufe' . '_' . $warehouse, + 'link_names' => true, + 'icon' => ':robot_face:', + 'allow_markdown' => true, + ]); + + + $msg = "Bestellung {$order->getOrderId()}: \n"; $msg .= $item['name']; $msg .= ' ' . $item['sku']; $msg .= ' Menge: ' . $item['menge'] . "\n"; + + if ($order->getStatus() == 1) { + $slack->from('CDS-Notify') + ->send($msg); + } } } - if ($order->getStatus() == 1) { - $slack->from('CDS-Notify') - ->send($msg); - } + } + + private function getWarehouseByGtin(string $gtin) + { + $product = $this->productRepository->findOneBy(['gtin' => $gtin]); + + if ($product) { + $stock = $this->stockRepository->findOneBy(['product_id' => $product->getId()]); + + if ($stock) { + $warehouse = $this->warehouseRepository->findOneBy(['id' => $stock->getWarehouseId()]); + return $warehouse->getName(); + } + } + + return null; + + } + } \ No newline at end of file diff --git a/src/Helper/Jtl.php b/src/Helper/Jtl.php index 104a086..8bfac7a 100644 --- a/src/Helper/Jtl.php +++ b/src/Helper/Jtl.php @@ -69,34 +69,28 @@ class Jtl if ($warehouse) { foreach ($warehouse as $w) { - $stock = $this->stockRepository->findBy(['warehouse' => $w]); + $warehousesName = $w->getName(); - if ($stock) { - foreach ($stock as $s) { - $warehouse = $s->getWarehouse(); - $warehouseName = $warehouse->getName(); + foreach ($products as $p) { + $stock = $this->stockRepository->findOneBy(['product_id' => $p->getId(), 'warehouse' => $w]); - if (isset($products[$s->getProductId()])) { - $data[$s->getProductId() . $warehouseName] = [ - 'gtin' => $products[$s->getProductId()]->getGtin(), - 'stock' => $s->getInstock(), - 'warehouse' => $this->arrLager[$warehouseName] ?? 'Lager_' . $warehouseName, - 'lager' => '01' - ]; - - } else { - $this->logger->info('No product for stock ' . $s->getProductId()); - } + if ($stock) { + $data[$p->getId() . $warehousesName] = [ + 'gtin' => $p->getGtin(), + 'stock' => $stock->getInstock(), + 'warehouse' => $this->arrLager[$warehousesName] ?? 'Lager_' . $warehousesName, + 'lager' => '01' + ]; + } else { + //Wenn Product nicht gefunden werden kann, dann setzte Bestand auf Null + $this->logger->info('No stock for warehouse ' . $w->getName()); + } + + if ($p->getGtin() == '0193128415976') { + dump($data[$p->getId() . $warehousesName]); } - } 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()); } + } } diff --git a/tests/Helper/HiltesImportTest.php b/tests/Helper/HiltesImportTest.php new file mode 100644 index 0000000..4203bc6 --- /dev/null +++ b/tests/Helper/HiltesImportTest.php @@ -0,0 +1,98 @@ +productRepository = $this->createMock(ProductRepository::class); + $this->warehouseRepository = $this->createMock(WarehouseRepository::class); + $this->stockRepository = $this->createMock(StockRepository::class); + $this->logger = $this->createMock(LoggerInterface::class); + $this->rootPath = '/'; + + $this->hiltesImport = new HiltesImport( + $this->productRepository, + $this->warehouseRepository, + $this->stockRepository, + $this->logger, + $this->rootPath + ); + } + + public function testStartImportWithNoFiles() + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('No Files to Import'); + + $this->hiltesImport->startImport(); + } + + public function testStartImportWithFiles() + { + // Mock the getFiles method to return true + $hiltesImport = $this->getMockBuilder(HiltesImport::class) + ->setConstructorArgs([ + $this->productRepository, + $this->warehouseRepository, + $this->stockRepository, + $this->logger, + $this->rootPath + ]) + ->onlyMethods(['getFiles']) + ->getMock(); + + $hiltesImport->method('getFiles')->willReturn(true); + + $this->assertNull($hiltesImport->startImport()); + } + + public function testGetFilesWithNoResults() + { + $this->assertFalse($this->hiltesImport->getFiles()); + } + + public function testSaveDataWithNoWarehouse() + { + $this->logger->expects($this->once()) + ->method('error') + ->with('No Warehouse'); + + $this->assertFalse($this->hiltesImport->saveData(['product1', '100'])); + } + + public function testSaveDataWithValidData() + { + $warehouse = new Warehouse(); + $warehouse->setName('Warehouse1'); + + $product = new Product(); + $product->setGtin('product1'); + + $this->warehouseRepository->method('findOneBy')->willReturn($warehouse); + $this->productRepository->method('findOneBy')->willReturn($product); + + $this->stockRepository->expects($this->once()) + ->method('save'); + + $this->assertTrue($this->hiltesImport->saveData(['product1', '100', '', 'Warehouse1'])); + } +} \ No newline at end of file