increase speed

This commit is contained in:
Marko
2024-03-20 17:32:52 +01:00
parent 060b481319
commit df4d62bfdf
15 changed files with 1907 additions and 777 deletions

View File

@@ -8,6 +8,8 @@ use App\Repository\ProductRepository;
use App\Repository\StockRepository;
use App\Repository\WarehouseRepository;
use Psr\Log\LoggerInterface;
use Sentry\CheckIn;
use Sentry\CheckInStatus;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
@@ -15,6 +17,7 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpKernel\KernelInterface;
use function Sentry\captureCheckIn;
#[AsCommand(
name: 'hiltes:import',
@@ -49,6 +52,18 @@ class HiltesImportCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): int
{
$time_start = microtime(true);
// 🟡 Notify Sentry your job is running:
$checkInId = captureCheckIn(
slug: 'bestandsabgleich-delta',
status: CheckInStatus::inProgress()
);
$this->logger->error('Start Hiltes Import');
dump($checkInId);
$io = new SymfonyStyle($input, $output);
$io->success('Start Hiltes Import');
@@ -65,6 +80,14 @@ class HiltesImportCommand extends Command
if (isset($r['error'])) {
$io->error($r['text']);
$this->logger->error($r['text']);
// 🔴 Notify Sentry your job has failed:
captureCheckIn(
slug: 'bestandsabgleich-delta',
status: CheckInStatus::error(),
checkInId: $checkInId,
);
return Command::FAILURE;
} else {
$io->info('Start Hiltes Push JTL');
@@ -81,6 +104,14 @@ class HiltesImportCommand extends Command
$execution_time = ($time_end - $time_start) / 60;
// 🟢 Notify Sentry your job has completed successfully:
captureCheckIn(
slug: 'bestandsabgleich-delta',
status: CheckInStatus::ok(),
duration: $execution_time,
checkInId: $checkInId
);
$io->success('Done.' . PHP_EOL . 'Execution time: ' . $execution_time . ' minutes');
return Command::SUCCESS;
}

View File

@@ -28,9 +28,8 @@ class Order
#[ApiProperty(identifier: false)]
private ?int $id = null;
#[ORM\Column(length: 255,unique: true)]
#[ORM\Column(length: 255, unique: true)]
#[ApiProperty(identifier: true)]
private ?string $orderId = null;
#[ORM\Column]
@@ -42,12 +41,6 @@ class Order
#[ORM\Column(name: 'change_date', type: Types::DATETIME_MUTABLE, nullable: true, options: ['default' => 'CURRENT_TIMESTAMP'])]
private ?DateTimeInterface $changeDate = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $han = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $marke = null;
public function getId(): ?int
{
return $this->id;
@@ -100,28 +93,4 @@ class Order
return $this;
}
public function getHan(): ?string
{
return $this->han;
}
public function setHan(?string $han): static
{
$this->han = $han;
return $this;
}
public function getMarke(): ?string
{
return $this->marke;
}
public function setMarke(?string $marke): static
{
$this->marke = $marke;
return $this;
}
}

View File

@@ -7,7 +7,9 @@ use App\Entity\Order;
use App\Repository\ProductRepository;
use App\Repository\StockRepository;
use App\Repository\WarehouseRepository;
use Maknz\Slack\Client;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
@@ -24,12 +26,14 @@ class SlackNotifySubscriber implements EventSubscriberInterface
private $stockRepository;
private $warehouseRepository;
public function __construct(string $slackWebhookUrl, ProductRepository $productRepository, StockRepository $stockRepository, WarehouseRepository $warehouseRepository)
{
$this->slackWebhookUrl = $slackWebhookUrl;
$this->productRepository = $productRepository;
$this->stockRepository = $stockRepository;
$this->warehouseRepository = $warehouseRepository;
}
public static function getSubscribedEvents(): array
@@ -54,28 +58,37 @@ class SlackNotifySubscriber implements EventSubscriberInterface
if (!$order instanceof Order || Request::METHOD_POST !== $method) {
return;
}
if ($order->getStatus() == 1) {
foreach ($order->getData()[0]['positions'] as $item) {
if ($item['menge'] > 0) {
foreach ($order->getData()[0]['positions'] as $item) {
if ($item['menge'] > 0) {
$warehouse = $this->getWarehouseByGtin($item['gtin']);
$warehouses = $this->getWarehouseByGtin($item['gtin']);
$slack = new Client($this->slackWebhookUrl, [
'username' => 'CDS-Notify',
'channel' => '#online_verkäufe' . '_' . $warehouse,
'link_names' => true,
'icon' => ':robot_face:',
'allow_markdown' => true,
]);
if ($warehouses == false) {
continue;
}
$msg = "Bestellung {$order->getOrderId()}: \n";
$msg .= $item['marke'] . ' - ';
$msg .= $item['name'];
$msg .= ' - ' . $item['han'];
$msg .= ' * - ' . $item['menge'] . "* \n";
foreach ($warehouses as $warehouse) {
if ($order->getStatus() == 1) {
$slack->from('CDS-Notify')->send($msg);
$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['marke'] . ' - ';
$msg .= $item['name'];
$msg .= ' - ' . $item['han'];
$msg .= ' * - ' . $item['menge'] . "* \n";
$slack->from('CDS-Notify')->send($msg);
}
}
}
}
@@ -83,26 +96,34 @@ class SlackNotifySubscriber implements EventSubscriberInterface
/**
* @param string $gtin
* @return string|null
* @return array
*/
private function getWarehouseByGtin(string $gtin)
{
if ($gtin == null) {
return false;
}
$warehouse = [];
$product = $this->productRepository->findOneBy(['gtin' => $gtin]);
if ($product) {
$stock = $this->stockRepository->findOneBy(['product_id' => $product->getId()]);
$stock = $this->stockRepository->findBy(['product_id' => $product->getId()]);
if ($stock) {
$warehouse = $this->warehouseRepository->findOneBy(['id' => $stock->getWarehouse()->getId()]);
return $warehouse->getName();
$prio = 0;
foreach ($stock as $item) {
$w = $this->warehouseRepository->findOneBy(['id' => $item->getWarehouse()->getId()]);
if ($w->getPrio() > $prio) {
$warehouse[] = $w->getName();
$prio = $w->getPrio();
}
}
}
}
return null;
return $warehouse;
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Helper;
use App\Entity\Order;
use App\Repository\OrderRepository;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
@@ -21,7 +22,7 @@ class Hiltes
protected $hiltesApiUrl;
public function __construct(private OrderRepository $orderRepository)
public function __construct(private OrderRepository $orderRepository, private LoggerInterface $logger)
{
$this->fsObject = new Filesystem();
$this->current_dir_path = getcwd();
@@ -31,19 +32,6 @@ class Hiltes
$this->hiltesPass = $_ENV['HILTES_API_PASS'];
}
// public function import()
// {
//
// $finder = new Finder();
// $finder->files()->in($this->current_dir_path . "/hiltes/h2c/");
//
// foreach ($finder as $file) {
// $contents = $file->getContents();
//
// //var_dump($contents);
// }
//
// }
/**
* @param string $data
@@ -100,7 +88,12 @@ class Hiltes
#dump($tA);
#*** Umkonvertieren des Datumstings *****************
$tA['orderdate'] = gmdate('Y-m-d\TH:i:s.v\Z', $tA['orderdate'] ? strtotime($tA['orderdate']) : time());
if (!empty($tA['salesdate'])) {
$tA['orderdate'] = gmdate('Y-m-d\TH:i:s.v\Z', $tA['salesdate'] ? strtotime($tA['salesdate']) : time());
} else {
$tA['orderdate'] = gmdate('Y-m-d\TH:i:s.v\Z', time());
}
#****
$arr = array(
'SalesDate' => $tA['orderdate'],
@@ -112,9 +105,6 @@ class Hiltes
);
#*** Items ***************
if (!empty($tA['positions']) && is_array($tA['positions'])) {
# Noch nötig
# "CalculatedSellingPrice" => 0, #X Kalkulierter Verkaufspreis
# "AchievedSalesPrice" => 0, #X Erzielter Verkaufspreis
$tSalNr = array();
foreach ($tA['positions'] as $v) {
if ($v['type'] == 'versandposition' || $v['sku'] == '') {
@@ -163,7 +153,7 @@ class Hiltes
}
}
#*** Kundne *******************************
#*** Kunde *******************************
$arr['CustomerList'][] = array(
"CustomerNumber" => $tA['kundenummer'], # Fake Daten
"Surname" => $tA['name'],#"string",
@@ -232,13 +222,19 @@ class Hiltes
);
$r = $this->sendToHiltes($url, $param);
dump($r);
if ($r === false) {
$this->logger->error('Login Error ' . $r . __LINE__);
return false;
}
$t = json_decode($r, true);
if ($t['Success']) {
$this->hiltesApiHash = $t['Data'];
return true;
} else {
dump($r);
dump('Login Error ' . __LINE__);
$this->logger->error('Login Error ' . __LINE__);
return false;
}
}
@@ -378,6 +374,13 @@ class Hiltes
//dump($param);
$r = $this->sendToHiltes($url, $param, true);
dump($r);
if ($r === false) {
dump('Send Order Error ' . __LINE__);
return false;
}
$t = json_decode($r, true);
if ($t['Success']) {
return true;