config('enable', false); } /** * @param $discountId * @return Entity\DiscountEntity */ public static function getDiscountEntity($discountId) { $discountFactory = discountApp()->factory()->main()->getDiscountFactory(); $discountEntity = $discountFactory->getEntityById($discountId); if(!$discountEntity){ return $discountFactory->getEntityByResult([]); } return $discountEntity; } public static function setTicketValueFromDiscount($elementId) { $includeModule = Loader::includeModule('catalog') && Loader::includeModule('iblock') && Loader::includeModule('sale'); if(!$includeModule) { return false; } $iBlockId = discountApp()->config('catalog_iblock_id'); $map = discountApp()->config('MAP')->toArray(); $discountList = \CCatalogDiscount::GetDiscount( $elementId, $iBlockId, [], //catalog_group_id_list [2], 'N', // renewal 's1', false ); if(!is_array($discountList)){ $discountList = []; } $af = app()->factory()->iBlock()->getEntityById($iBlockId); $propertyFactory = $af->getPropertyFactory(); $elementFactory = $af->getElementFactory(); $elementEntity = $elementFactory->getEntityById($elementId); if(!$elementEntity){ return false; } $pvc = $elementEntity->getPropertyValueCollection(); $currentPropertyValueList = []; foreach ($map as $type => $typeMap){ foreach ($typeMap as $propertyCode => $propertyValue) { static::prepareMapPropertyCodeAndValue($propertyCode, $propertyValue); $propertyEntity = $propertyFactory->getEntityByCode($propertyCode); if($propertyEntity?->getBaseType() != 'L'){ continue; } if(!isset($currentPropertyValueList[$propertyEntity->getId()])){ $pve = $pvc->getByCode($propertyCode); $currentValue = $pve->getValue(); $currentValueCollection = collect($currentValue); $currentPropertyValueList[$propertyEntity->getId()] = $currentValueCollection->values()->sort()->toArray(); } } } $currentPropertyValueList = collect($currentPropertyValueList)->sort()->toArray(); pre($currentPropertyValueList); $newPropertyValueList = $currentPropertyValueList; foreach ($map as $type => $typeMap){ foreach ($typeMap as $propertyCode => $propertyValue) { static::prepareMapPropertyCodeAndValue($propertyCode, $propertyValue); $propertyEntity = $propertyFactory->getEntityByCode($propertyCode); if($propertyEntity?->getBaseType() != 'L'){ continue; } /** * @var \Wt\Core\Collection\IBlock\PropertyEnumCollection $valueCollection */ $valueCollection = $propertyEntity->getValueCollection(); $valueEntity = $valueCollection->getByXmlId($propertyValue); $newPropertyValueList[$propertyEntity->getId()] = collect($newPropertyValueList[$propertyEntity->getId()]) ->deleteValue($valueEntity->getId()) ->values()->toArray(); } } foreach ($discountList as $discountId => $discount){ $discountEntity = static::getDiscountEntity($discountId); $type = $discountEntity->getType(); if(isset($map[$type]) && is_array($map[$type])){ $typeMap = $map[$type]; foreach ($typeMap as $propertyCode => $propertyValue) { static::prepareMapPropertyCodeAndValue($propertyCode, $propertyValue); $propertyEntity = $propertyFactory->getEntityByCode($propertyCode); if($propertyEntity?->getBaseType() != 'L'){ continue; } /** * @var \Wt\Core\Collection\IBlock\PropertyEnumCollection $valueCollection */ $valueCollection = $propertyEntity->getValueCollection(); $valueEntity = $valueCollection->getByXmlId($propertyValue); $newPropertyValueList[$propertyEntity->getId()] = collect($newPropertyValueList[$propertyEntity->getId()]) ->push($valueEntity->getId()) ->values()->toArray(); } } } pre($newPropertyValueList); if($newPropertyValueList != $currentPropertyValueList) { pre('set'); foreach ($newPropertyValueList as $propertyId => $propertyValue){ if(is_array($propertyValue) && !$propertyValue){ $newPropertyValueList[$propertyId] = false; } } \CIBlockElement::SetPropertyValuesEx($elementId, $iBlockId, $newPropertyValueList); if(discountApp()->config('update_element_index')){ \Bitrix\Iblock\PropertyIndex\Manager::updateElementIndex($iBlockId, $elementId); } } return true; } public static function prepareMapPropertyCodeAndValue(&$propertyCode, &$propertyValue) { if (is_int($propertyCode)) { [$propertyCode, $propertyValue] = explode('.', $propertyValue); } } public static function getElementList($lastId = 0) { $includeModule = Loader::includeModule('iblock'); if(!$includeModule) { return []; } $iBlockId = discountApp()->config('catalog_iblock_id', 0); if(!$iBlockId) { return []; } $rs = \CIBlockElement::GetList( ['ID' => 'ASC'], [ 'IBLOCK_ID' => $iBlockId, '>ID' => $lastId, 'ACTIVE' => 'Y', 'GLOBAL_ACTIVE' => 'Y' ], false, [ 'nTopCount' => discountApp()->config('page_size', 50), ], ['ID', 'IBLOCK_ID'] ); $elements = []; while($ar = $rs->Fetch()){ $elements[$ar['ID']] = $ar['ID']; } return $elements; } /** * Удаление записей которые ссылаются на отсутствующие элементы * * @return void */ public static function removeOldTypeValuesFromCatalogDiscount() { $includeModule = Loader::includeModule('catalog') && Loader::includeModule('iblock'); if(!$includeModule) { return; } $typeIBlockId = discountApp()->config('DISCOUNT_TYPE_IBLOCK_ID'); $typeAFactory = app()->factory()->iBlock()->getEntityById($typeIBlockId); $typeCollection = $typeAFactory->getElementFactory()->cache([])->getCollection([], [], ['ID', 'NAME']); // \Bitrix\Sale\Internals\DiscountTable::getList() $result = \CCatalogDiscount::GetList(); $list = []; while($ar = $result->Fetch()){ /** * Как пример не брать! Тут скидки дикие! */ $list[$ar['ID']] = $ar; } $removeList = []; foreach ($typeCollection as $entity){ $discountId = $entity->getName(); if(!$list[$discountId]){ /** * remove iblock element - discount type */ $removeList[] = $entity->getId(); $r = \CIBlockElement::Delete($entity->getId()); if(!$r){ global $APPLICATION; pre("ERROR REMOVE {$entity->getId()}:" . $APPLICATION->LAST_ERROR?$APPLICATION->LAST_ERROR->GetString():'last_error_error'); } } } $removeCount = count($removeList); pre("remove {$removeCount} / {$typeCollection->count()} = " . ($typeCollection->count() - $removeCount)); pre('REMOVE ID LIST [TYPES]'); pre($removeList); pre('DISCOUNT IDS ' . count($list)); pre(array_keys($list)); } }