Files
2025-08-02 16:30:27 +02:00

144 lines
6.2 KiB
PHP

<?php
namespace KupShop\InventoroBundle\EventSubscriber;
use KupShop\AdminBundle\Util\ActivityLog;
use KupShop\InventoroBundle\Util\InventoroUtil;
use KupShop\KupShopBundle\Config;
use KupShop\KupShopBundle\Event\CronEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CronSubscriber implements EventSubscriberInterface
{
/** @var InventoroUtil */
private $inventoroUtil;
public function __construct(InventoroUtil $inventoroUtil)
{
$this->inventoroUtil = $inventoroUtil;
}
public static function getSubscribedEvents()
{
if (isDevelopment()) {
return [];
}
if (empty(Config::get()['Modules']['inventoro'])) {
return [];
}
return [
CronEvent::RUN_EXPENSIVE => [
['sendInventoroProducts', 200],
['sendInventoroTransactions', 100],
],
];
}
public function sendInventoroProducts()
{
$from = date('Y-m-d 00:00:00', time() - 2 * 24 * 60 * 60); // -2 days
$products = $this->inventoroUtil->getProducts($from);
if (!empty($products)) {
$response = $this->inventoroUtil->sendProducts($products);
$errors = array_column($response, 'error');
$errors = array_filter($errors);
if (!empty($errors)) {
$error = reset($errors);
addActivityLog(ActivityLog::SEVERITY_ERROR, ActivityLog::TYPE_SYNC,
'Inventoro - Selhalo odeslání produktů - '.$error, $errors);
}
if (!empty($response)) {
$response = array_column($response, 'response');
$response = array_map('json_encode', $response);
addActivityLog(ActivityLog::SEVERITY_SUCCESS, ActivityLog::TYPE_SYNC,
'Inventoro - Úspešné odeslání produktů - '.count($products).' produktů', $response);
}
}
}
public function sendInventoroTransactions()
{
$from = date('Y-m-d 00:00:00', time() - 2 * 24 * 60 * 60); // -2 days
$transactions = $this->inventoroUtil->getTransactions($from);
if (!empty($transactions)) {
$response = $this->inventoroUtil->sendTransactions($transactions);
$errors = array_column($response, 'error');
$errors = array_filter($errors);
if (!empty($errors)) {
$error = reset($errors);
addActivityLog(ActivityLog::SEVERITY_ERROR, ActivityLog::TYPE_SYNC,
'Inventoro - Selhalo odeslání transakcí - '.$error, $errors);
}
if (!empty($response)) {
$response = array_column($response, 'response');
$response = array_map('json_encode', $response);
addActivityLog(ActivityLog::SEVERITY_SUCCESS, ActivityLog::TYPE_SYNC,
'Inventoro - Úspešné odeslání transakcí - '.count($transactions).' transakcí', $response);
}
}
$transactions = $this->inventoroUtil->getTransactions($from, null, true); // storno
if (!empty($transactions)) {
$response = $this->inventoroUtil->sendTransactions($transactions);
$errors = array_column($response, 'error');
$errors = array_filter($errors);
if (!empty($errors)) {
$error = reset($errors);
addActivityLog(ActivityLog::SEVERITY_ERROR, ActivityLog::TYPE_SYNC,
'Inventoro - Selhalo odeslání storno transakcí - '.$error, $errors);
}
if (!empty($response)) {
$response = array_column($response, 'response');
$response = array_map('json_encode', $response);
addActivityLog(ActivityLog::SEVERITY_SUCCESS, ActivityLog::TYPE_SYNC,
'Inventoro - Úspešné odeslání storno transakcí - '.count($transactions).' transakcí', $response);
}
}
if (!empty(Config::get()['Modules']['inventoro']['sendSuppliersTransactions'])) {
$suppliersTransactions = $this->inventoroUtil->getSuppliersTransactions($from);
$products = $suppliersTransactions['products'] ?? null;
$transactions = $suppliersTransactions['transactions'] ?? null;
if (!empty($transactions)) {
if (!empty($products)) {
$response = $this->inventoroUtil->sendProducts($products);
$errors = array_column($response, 'error');
$errors = array_filter($errors);
if (!empty($errors)) {
$error = reset($errors);
addActivityLog(ActivityLog::SEVERITY_ERROR, ActivityLog::TYPE_SYNC,
'Inventoro - Selhalo odeslání produktů (naskladneni) - '.$error, $errors);
}
if (!empty($response)) {
$response = array_column($response, 'response');
$response = array_map('json_encode', $response);
addActivityLog(ActivityLog::SEVERITY_SUCCESS, ActivityLog::TYPE_SYNC,
'Inventoro - Úspešné odeslání produktů (naskladneni) - '.count($products).' produktů', $response);
}
}
$response = $this->inventoroUtil->sendTransactions($transactions);
$errors = array_column($response, 'error');
$errors = array_filter($errors);
if (!empty($errors)) {
$error = reset($errors);
addActivityLog(ActivityLog::SEVERITY_ERROR, ActivityLog::TYPE_SYNC,
'Inventoro - Selhalo odeslání transakcí (naskladneni) - '.$error, $errors);
}
if (!empty($response)) {
$response = array_column($response, 'response');
$response = array_map('json_encode', $response);
addActivityLog(ActivityLog::SEVERITY_SUCCESS, ActivityLog::TYPE_SYNC,
'Inventoro - Úspešné odeslání transakcí (naskladneni) - '.count($transactions).' transakcí', $response);
}
}
}
}
}