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/cvetdv.ru/bitrix/modules/wbs24.ozonapinew/lib/Agents/ |
Upload File : |
<?php namespace Wbs24\Ozonapinew\Agents; use Wbs24\Ozonapinew\Agents; use Wbs24\Ozonapinew\Stocks\Update as StocksUpdate; use Wbs24\Ozonapinew\Products\Cache as ProductsCache; use Wbs24\Ozonapinew\Settings; class Stocks extends Agents { protected $main; protected $moduleId; protected $wrappers; protected $entityType = 'stocks'; protected $postfixAgentNames = [ '::updateStocksAgent();', ]; protected $lockOption = 'agents_stocks_lock'; public function setAccountIndex(int $accountIndex) { $dependencies = $this->getDependencies($accountIndex); $this->setDependences([], $dependencies, $accountIndex); } public function setDependences($objects = [], $dependencies, $accountIndex) { $this->StocksUpdate = $objects['StocksUpdate'] ?? new StocksUpdate($dependencies); $this->ProductsCache = $objects['ProductsCache'] ?? new ProductsCache($dependencies); $this->Settings = $objects['Settings'] ?? new Settings(); } public function updateAgents($accounts = [1]) { foreach ($accounts as $accountIndex) { $this->wrappers->Option->setPrefix($accountIndex); $autoUpdateStocksInterval = $this->wrappers->Option->get($this->moduleId, 'autoUpdateStocksInterval'); $apiKey = $this->wrappers->Option->get($this->moduleId, 'apiKey'); $marketplaceWarehouseName = $this->wrappers->Option->get( $this->moduleId, 'marketplaceWarehouseName' ); if (!$this->agentCheckOn()) continue; if (empty($apiKey)) continue; if ( empty($autoUpdateStocksInterval) || !is_numeric($autoUpdateStocksInterval) ) continue; if ( empty($marketplaceWarehouseName) ) continue; foreach ($this->postfixAgentNames as $postfixAgentName) { if ($accountIndex > 1) { $postfixAgentName = str_replace("()", "(${accountIndex})", $postfixAgentName); } $this->wrappers->Option->set($this->moduleId, $this->lockOption, 'N'); $this->updateAgent($postfixAgentName); } } } public function updateAgent($postfixAgentName) { $agentName = $this->getNameAgent($postfixAgentName); $interval = intval($this->wrappers->Option->get($this->moduleId, "autoUpdateStocksInterval")); $this->addAgent($agentName, $interval); } public static function setAllowedProductsAgent(int $accountIndex = 1, $key = 0): string { $agentsStocksObject = new Stocks(); $agentsStocksObject->setAccountIndex($accountIndex); $response = $agentsStocksObject->getIblockIdAndOfferIblockId($key); $iblockId = $response['iblockId']; $parentIblockId = $response['parentIblockId']; if ($iblockId) { if ($key == 0) { $agentsStocksObject->ProductsCache->deleteCache( $accountIndex, $agentsStocksObject->entityType ); } $agentsStocksObject->ProductsCache->launch([ 'accountIndex' => $accountIndex, 'iblockType' => $parentIblockId ? 'OFFER_IBLOCK' : 'IBLOCK' , 'iblockId' => $iblockId, 'parentIblockId' => $parentIblockId ?: false, 'entityType' => $agentsStocksObject->entityType, ]); } else { self::updateStocksStepAgent($accountIndex); return ''; } $key++; return '\\'.__CLASS__.'::'.__FUNCTION__.'('.$accountIndex.', '.$key.');'; } protected function getIblockIdAndOfferIblockId($key) { $iblockId = false; $parentIblockId = false; $siteId = $this->wrappers->Option->get($this->moduleId, 'siteId'); $allIblocks = $this->Settings->getAllInfoBlocks($siteId); $iblocks = $allIblocks['iblocks'] ?? []; $iblocksToOfferIblocks = $allIblocks['iblocks_to_trade_iblocks'] ?? []; $offerIblocks = array_values($iblocksToOfferIblocks); $iblocks = array_merge($iblocks, $offerIblocks); $iblockId = $iblocks[$key] ?? false; if ($iblockId) { $parentIblockId = array_search($iblockId, $iblocksToOfferIblocks); } return [ 'iblockId' => $iblockId, 'parentIblockId' => $parentIblockId, ]; } public static function updateStocksAgent(int $accountIndex = 1): string { $agentsStocksObject = new Stocks(); $agentsStocksObject->setAccountIndex($accountIndex); if (!$agentsStocksObject->areAgentsLocked()) { $agentsStocksObject->setAgentsLock(); $agentName = $agentsStocksObject->getNameAgent('::setAllowedProductsAgent('.$accountIndex.', 0);'); $agentsStocksObject->addAgent($agentName, 1); } return '\\'.__CLASS__.'::'.__FUNCTION__.'('.$accountIndex.');'; } public static function updateStocksStepAgent($accountIndex = 1, $lastId = '') { $agentsStocksObject = new Stocks(); $agentsStocksObject->setAccountIndex($accountIndex); $newLastId = $agentsStocksObject->StocksUpdate->updateStocksOnMarketplace($lastId); if ($newLastId) { $agentName = $agentsStocksObject->getNameAgent('::'.__FUNCTION__.'('.$accountIndex.', "'.$newLastId.'");'); $agentsStocksObject->addAgent($agentName, 1); $agentsStocksObject->setAgentsLock(); } else { $agentsStocksObject->unsetAgentsLock(); } return ''; } // вспомогательные функции public function getNameAgent($postfixAgentName): string { return "\\".__CLASS__.$postfixAgentName; } protected function agentCheckOn(): bool { return $this->wrappers->Option->get($this->moduleId, "stocksUpdateFlag") == "Y" ? true : false; } public function setAgentsLock() { $this->wrappers->Option->set($this->moduleId, $this->lockOption, 'Y'); } public function unsetAgentsLock() { $this->wrappers->Option->set($this->moduleId, $this->lockOption, 'N'); } public function areAgentsLocked() { return ($this->wrappers->Option->get($this->moduleId, $this->lockOption) == 'Y'); } }