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/Products/ |
Upload File : |
<?php namespace Wbs24\Ozonapinew\Products; use Bitrix\Main\Localization\Loc; use Wbs24\Ozonapinew\{ Main, Wrappers, PackageRatio }; /** * Класс, который: * 1) Ищет товары, пригодные к выгрузке остатков и цен по фильтрам торговых инфоблоков * 2) Записывает такие товары в таблицу */ class Cache { protected const OPTION_PRODUCT_OFFER_ID = 'skuPropertyInOzonForProducts'; protected const OPTION_SUB_PRODUCT_OFFER_ID = 'skuPropertyInOzonForProductOffers'; public function __construct($objects = []) { $this->main = $objects['Main'] ?? new Main(); $this->moduleId = $this->main->getModuleId(); $this->wrappers = new Wrappers($objects); $this->suffix = strtoupper($this->moduleId); $this->ProductsFilter = $objects['ProductsFilter'] ?? new Filter($objects); $this->ProductsProperties = $objects['ProductsProperties'] ?? new Properties($objects); $this->CacheStack = $objects['CacheStack'] ?? new Cache\Stack($objects); $this->PackageRatio = $objects['PackageRatio'] ?? new PackageRatio($objects); $this->optionProductOfferId = $this->wrappers->Option->get( $this->moduleId, self::OPTION_PRODUCT_OFFER_ID ); $this->optionSubProductOfferId = $this->wrappers->Option->get( $this->moduleId, self::OPTION_SUB_PRODUCT_OFFER_ID ); } public function deleteCache($accountIndex, $entityType) { $this->CacheStack->clearProducts($accountIndex, $entityType); } public function launch($param) { $iblockType = $param['iblockType']; switch ($iblockType) { case 'IBLOCK': $this->startIblockProcess($param); break; case 'OFFER_IBLOCK': $this->startOfferIblockProcess($param); break; } } protected function startIblockProcess($param) { $param['articleProperty'] = strtoupper($this->optionProductOfferId); if ( !$param['accountIndex'] || !$param['iblockType'] || !$param['iblockId'] || !$param['entityType'] || !$param['articleProperty'] ) return; // Получить filter и select $response = $this->getSelectAndFilter($param); $select = $response['select']; $filter = $response['filter']; // Произвести поиск и сохранение в таблицу if ($select && $filter) { $param['select'] = $select; $param['filter'] = $filter; $this->searchAndSaveAllowedProducts($param); } } protected function startOfferIblockProcess($param) { $param['articleProperty'] = strtoupper($this->optionSubProductOfferId); if ( !$param['accountIndex'] || !$param['iblockType'] || !$param['iblockId'] || !$param['entityType'] || !$param['articleProperty'] || !$param['parentIblockId'] ) return; // Получить id родительских товаров из таблицы $parentProducts = $this->CacheStack->getParentProducts($param); if (!$parentProducts) return; $param['parentProductIds'] = $this->getParentProductIds($parentProducts); // Получить свойство привязки к родительскому товару $offersLinkPropertyCode = $this->ProductsProperties->getOffersLinkPropertyCode($param['iblockId'], $param['parentIblockId']); if (!$offersLinkPropertyCode) return; $param['offersLinkPropertyCode'] = $offersLinkPropertyCode; $param['useProductPacakgePropForOffer'] = $this->PackageRatio->checkToUseProductPacakgePropForOffer(); $param['packageRatioProductPropCode'] = $this->PackageRatio->getPackageRatioProductPropCode(); // Получить filter и select $response = $this->getSelectAndFilter($param); $select = $response['select']; $filter = $response['filter']; // Произвести поиск и сохранение в таблицу if ($select && $filter) { $param['select'] = $select; $param['filter'] = $filter; $this->searchAndSaveAllowedProducts($param); } } protected function getSelectAndFilter($param) { $response = []; [ 'iblockType' => $iblockType, 'iblockId' => $iblockId, 'articleProperty' => $articleProperty, 'offersLinkPropertyCode' => $offersLinkPropertyCode, 'useProductPacakgePropForOffer' => $useProductPacakgePropForOffer, 'packageRatioProductPropCode' => $packageRatioProductPropCode, ] = $param; // SELECT $select = $this->getSelect( $articleProperty, $iblockId, $iblockType, $offersLinkPropertyCode ); if ($select) { if ($iblockType == 'IBLOCK') { $select = $this->PackageRatio->getSelectForProduct($select); } else { if ($useProductPacakgePropForOffer) { $select[] = 'PROPERTY_'.$offersLinkPropertyCode.'.PROPERTY_'.$packageRatioProductPropCode; } else { $select = $this->PackageRatio->getSelectForOffer($select); } } } // FILTER $filter = $this->getFilter($iblockId); if ($select && $filter) { $response['select'] = $select; $response['filter'] = $filter; } return $response; } protected function searchAndSaveAllowedProducts($param) { $saveData = []; $entityType = $param['entityType']; // Номер аккаунта $accountIndex = $param['accountIndex']; // Тип инфоблока $iblockType = $param['iblockType']; // Cвойство, в котором хранится Артикул OZON $articleProperty = $param['articleProperty']; // Информация об инфоблоках $iblockId = $param['iblockId']; $parentIblockId = $param['parentIblockId']; // Корневые id товаров $parentProductIds = $param['parentProductIds']; // Свойство привязки к корневым товарам $offersLinkPropertyCode = $param['offersLinkPropertyCode']; // Использовать ли родительское свойство коэффициента упаковки для офферов $useProductPacakgePropForOffer = $param['useProductPacakgePropForOffer']; // Свойство корневого товара с кодом коэффициента упаковки $packageRatioProductPropCode = $param['packageRatioProductPropCode']; $filter = $param['filter']; $select = $param['select']; $result = $this->wrappers->CIBlockElement->GetList( [], $filter, false, false, $select ); while ($product = $result->GetNext()) { $offerId = $this->getOfferId($articleProperty, $product); if (!$offerId) continue; $packageRatioValue = $this->getPackageRatioValue($iblockType, $product, [ 'offersLinkPropertyCode' => $offersLinkPropertyCode, 'useProductPacakgePropForOffer' => $useProductPacakgePropForOffer, 'packageRatioProductPropCode' => $packageRatioProductPropCode ]); // Проверить, входит ли оффер в родительскую выгрузку товаров $validateProduct = true; if ($iblockType == 'OFFER_IBLOCK') { $parentProductId = $product['PROPERTY_'.$offersLinkPropertyCode.'_VALUE']; if ($parentProductId) { $validateProduct = in_array($parentProductId, $parentProductIds) ? true : false ; } } $saveData = [ 'accountIndex' => $accountIndex, 'offerId' => $offerId, 'productId' => $product['ID'], 'parentProductId' => $parentProductId, 'type' => $product['TYPE'], 'iblockId' => $product['IBLOCK_ID'], 'parentIblockId' => $parentIblockId, 'packageRatioValue' => $packageRatioValue, 'entityType' => $entityType, ]; if ($validateProduct) $this->CacheStack->setProduct($saveData); } } protected function getPackageRatioValue($iblockType, $product, $offerNeedData) { switch ($iblockType) { case 'IBLOCK': $packageRatioValue = $this->PackageRatio->getPropertyValueForProduct($product); break; case 'OFFER_IBLOCK': $offersLinkPropertyCode = $offerNeedData['offersLinkPropertyCode']; $useProductPacakgePropForOffer = $offerNeedData['useProductPacakgePropForOffer']; $packageRatioProductPropCode = $offerNeedData['packageRatioProductPropCode']; if ($useProductPacakgePropForOffer) { $packageRatioCode = 'PROPERTY_'.$offersLinkPropertyCode.'_PROPERTY_'.$packageRatioProductPropCode.'_VALUE'; $packageRatioValue = $product[$packageRatioCode]; if (!$packageRatioValue) $packageRatioValue = 1; } else { $packageRatioValue = $this->PackageRatio->getPropertyValueForOffer($product); } break; } return $packageRatioValue; } protected function getOfferId($articleProperty, $product) { $offerId = ''; switch ($articleProperty) { case 'ID': $offerId = $product['ID']; break; case 'XML_ID': $offerId = $product['XML_ID']; break; default: $offerId = $product['PROPERTY_'.$articleProperty.'_VALUE']; break; } return $offerId; } protected function getParentProductIds($allowedParentProducts) { $allowedParentProductIds = []; foreach ($allowedParentProducts as $product) { $allowedParentProductIds[] = $product['product_id']; } return $allowedParentProductIds; } protected function getSelect($artnumber, $iblockId, $iblockType, $offersLinkPropertyCode) { $select = []; switch ($artnumber) { case 'ID': $select[] = 'ID'; break; case 'XML_ID': $select[] = 'XML_ID'; break; default: $code = $artnumber; $propertyExists = $this->checkIblockPropertyExists($code, $iblockId); if ($propertyExists) { $select[] = 'PROPERTY_'.$code; } break; } if ($select) { $select[] = 'IBLOCK_ID'; $select[] = 'TYPE'; in_array('ID', $select) ?: $select[] = 'ID'; } if ($select && $iblockType == 'OFFER_IBLOCK') { $select[] = 'PROPERTY_'.$offersLinkPropertyCode; } return $select; } protected function getFilter($iblockId) { $filterJson = $this->wrappers->Option->get($this->moduleId, 'filter_iblock_'.$iblockId); if ($filterJson) { $filter = $this->ProductsFilter->prepare($filterJson, $iblockId); } else { $filter = ['IBLOCK_ID' => $iblockId, 'ACTIVE' => 'Y']; } return $filter; } protected function checkIblockPropertyExists($propertyCode, $iblockId) { $propertyCodes = $this->getAllowedProperties($iblockId); return array_key_exists($propertyCode, $propertyCodes); } protected function getAllowedProperties($iblockId) { $allowedTypes = ['S', 'L']; $properties = []; $allProperties = $this->getAllProperties($iblockId); foreach ($allProperties as $code => $property) { if (!in_array($property['PROPERTY_TYPE'], $allowedTypes)) continue; $properties[strtoupper($code)] = $property['NAME']; } return $properties; } protected function getAllProperties($iblockId) { if (isset($this->allProperties[$iblockId])) return $this->allProperties[$iblockId]; $properties = []; $res = $this->wrappers->CIBlockProperty->GetList([], [ 'IBLOCK_ID' => $iblockId, ]); while ($property = $res->Fetch()) { $properties[$property['CODE']] = $property; } $this->allProperties[$iblockId] = $properties; return $properties; } }