Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/wbs24.ozonapinew/lib/ |
Upload File : |
<?php namespace Wbs24\Ozonapinew; use Bitrix\Main\SystemException; use Wbs24\Ozonapinew\Api\Fbs; class Agents { use Exception; // trait use Accounts; // trait protected $main; protected $moduleId; protected $wrappers; protected $postfixAgentNames = [ '::getOrdersAgent();', '::ordersCurlExecAgent();', '::cleanOrdersStack();', '::statusesCurlExecAgent(0);', ]; public function __construct($objects = []) { try { $this->main = $objects['Main'] ?? new Main(); $this->moduleId = $this->main->getModuleId(); $this->wrappers = new Wrappers($objects); } catch (SystemException $exception) { $this->exceptionHandler($exception); } } public function updateAgents($accounts = [1]) { // проверка условий, если какие то важные настройки не выбраны, агенты создаваться не будут $this->wrappers->CAgent->RemoveModuleAgents($this->moduleId); foreach ($accounts as $accountIndex) { $this->wrappers->Option->setPrefix($accountIndex); if (!$this->agentCheckOn()) continue; if (empty($this->wrappers->Option->get($this->moduleId, 'apiKey'))) continue; if (empty($this->wrappers->Option->get($this->moduleId, 'clientId'))) continue; if ( empty($this->wrappers->Option->get($this->moduleId, 'scheduler_interval')) || !is_numeric($this->wrappers->Option->get($this->moduleId, 'scheduler_interval')) ) continue; $valuesShouldNotBeNothing = [ 'isHttps', 'siteId', 'personTypeId', 'deliveryServiceId', 'paymentSystemId', 'propertyOfExternalOrderNumber', 'propertyOfShipmentDate', 'skuPropertyInOzonForProducts', 'skuPropertyInOzonForProductOffers', ]; $valueIsNothing = false; foreach ($valuesShouldNotBeNothing as $value) { if ($this->wrappers->Option->get($this->moduleId, $value) == 'nothing') { $valueIsNothing = true; break; } } if ($valueIsNothing) continue; foreach ($this->postfixAgentNames as $postfixAgentName) { if ($accountIndex > 1) { $postfixAgentName = str_replace("(0)", "(0, ${accountIndex})", $postfixAgentName); $postfixAgentName = str_replace("()", "(${accountIndex})", $postfixAgentName); } $this->updateAgent($postfixAgentName); } } } public function updateAgent($postfixAgentName) { $agentName = $this->getNameAgent($postfixAgentName); $launchTime = date("G"); $interval = intval($this->wrappers->Option->get($this->moduleId, "scheduler_interval")); $interval *= 60; if ($interval) { $dateTimestamp = time(); $launchDate = date("d.m.Y" , $dateTimestamp)." ".($launchTime < 10 ? "0" : "").$launchTime.":00:00"; $this->wrappers->CAgent->AddAgent( $agentName, $this->moduleId, "N", $interval, $launchDate, "Y", $launchDate ); } } public function addAgent($agentName, $interval) { if ($interval) { $interval *= 60; $this->wrappers->CAgent->AddAgent( $agentName, $this->moduleId, "N", $interval ); } } public static function getOrdersAgent($accountIndex = 1) { $agents = new Agents(); $dependencies = $agents->getDependencies($accountIndex); $apiObject = new Api($dependencies); $ordersObject = new Orders($dependencies); $ordersStackObject = new OrdersStack($accountIndex); // api request $orders = $apiObject->getAllOrders(); $ordersToTemporaryStack = []; foreach ($orders as $order) { if (!$ordersObject->checkTheOrderForUniqueness($order['posting_number'])) { $ordersToTemporaryStack[] = $order; } } if (!empty($ordersToTemporaryStack)) { $ordersStackObject->addOrdersToStack($ordersToTemporaryStack); } return '\\'.__CLASS__.'::'.__FUNCTION__.'('.$accountIndex.');'; } public static function ordersCurlExecAgent($accountIndex = 1) { try { $agents = new Agents(); $agents->setAccount($accountIndex); $domainName = $agents->getDomainName(); $isHttps = $agents->checkIsHttps(); $url = 'http'.($isHttps ? 's' : '') .'://'.$domainName .'/bitrix/tools/wbs24.ozonapinew/orderscurlexec.php?account_index='.$accountIndex ; $curl = curl_init($url); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_TIMEOUT, 45); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($curl); $error = curl_error($curl); curl_close($curl); if ($error) { $result = 'cURL Error: ' . $error; } } catch (SystemException $exception) { $agents->exceptionHandler($exception); } return '\\'.__CLASS__.'::'.__FUNCTION__.'('.$accountIndex.');'; } public static function cleanOrdersStack($accountIndex = 1) { $agents = new Agents(); $dependencies = $agents->getDependencies($accountIndex); $ordersObject = new Orders($dependencies); $ordersStackObject = new OrdersStack($accountIndex); $flagOfAllOrders = true; $orderIdsToExternalIds = $ordersObject->getOrderIdsToExternalIds($flagOfAllOrders); $externalIds = array_values($orderIdsToExternalIds); $ordersStackObject->cleanStack($externalIds); return '\\'.__CLASS__.'::'.__FUNCTION__.'('.$accountIndex.');'; } public static function statusesCurlExecAgent($lastOrderId = 0, $accountIndex = 1) { try { $agents = new Agents(); $agents->setAccount($accountIndex); $domainName = $agents->getDomainName(); $isHttps = $agents->checkIsHttps(); $url = 'http'.($isHttps ? 's' : '') .'://'.$domainName .'/bitrix/tools/wbs24.ozonapinew/statusescurlexec.php?last_order_id='.$lastOrderId.'&account_index='.$accountIndex ; $curl = curl_init($url); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_TIMEOUT, 45); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($curl); $error = curl_error($curl); curl_close($curl); if ($error) { $result = 'cURL Error: ' . $error; } if ($result) { if (is_numeric($result)) { $lastOrderId = $result; } else { $agents->createReport('agents_statuses_error_log.txt', 'request: '.$result); } } else { $lastOrderId = 0; } } catch (SystemException $exception) { $agents->exceptionHandler($exception); } return '\\'.__CLASS__.'::'.__FUNCTION__.'('.$lastOrderId.', '.$accountIndex.');'; } public function getDomainName() { $query = \Bitrix\Main\SiteTable::getList(); while ($site = $query->Fetch()) { if ($site['LID'] == $this->wrappers->Option->get($this->moduleId, 'siteId')) { $domainName = $site['SERVER_NAME']; } } return $domainName; } public function checkIsHttps() { return $this->wrappers->Option->get($this->moduleId, "isHttps") == "Y" ? true : false; } protected function getNameAgent($postfixAgentName) { return "\\".__CLASS__.$postfixAgentName; } protected function agentCheckOn() { return $this->wrappers->Option->get($this->moduleId, "scheduler_is_on") == "Y" ? true : false; } }