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

This commit is contained in:
Marko
2023-12-15 17:51:24 +01:00
parent 5d9db25d73
commit 2747d53c50
5 changed files with 162 additions and 40 deletions

View File

@@ -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;
}
}