This commit is contained in:
2026-02-05 16:22:55 +03:00
parent 1adbe9591c
commit 3a75beee32
18 changed files with 319 additions and 335 deletions
+112 -69
View File
@@ -31,111 +31,151 @@ class Tools
if(defined('SALE_DISCOUNT_MIGRATE_MODE') && SALE_DISCOUNT_MIGRATE_MODE){
return false;
}
return discountApp()->config('ENABLE');
return discountApp()->config('enable', false);
}
public static function getProductTypeArray($discountID)
/**
* @param $discountId
* @return Entity\DiscountEntity
*/
public static function getDiscountEntity($discountId)
{
$arProductType = [];
$arProductType['PRODUCT_TYPE'] = '0';
$arProductType['MIN_COUNT_PROD'] = 0;
$discountFactory = discountApp()->factory()->main()->getDiscountFactory();
$discountEntity = $discountFactory->getEntityById($discountId);
if(empty($discountID) || ($discountID < 0)){
return $arProductType;
if(!$discountEntity){
return $discountFactory->getEntityByResult([]);
}
$depsLoaded = Loader::includeModule('iblock');
if(!$depsLoaded) {
return $arProductType;
}
$iBlockId = discountApp()->config('DISCOUNT_TYPE_IBLOCK_ID');
if(!$iBlockId){
return $arProductType;
}
$res_element = \CIBlockElement::GetList(
['SORT' => 'ASC'],
['IBLOCK_ID' => $iBlockId, '=NAME' => $discountID],
false,
false,
['ID', 'NAME', 'PROPERTY_TYPE', 'PROPERTY_MIN_COUNT_PROD']
);
$res_element_count = $res_element->SelectedRowsCount();
if($res_element_count){
$obElement = $res_element->GetNext(true, false);
$arProductType['PRODUCT_TYPE'] = (string)$obElement['PROPERTY_TYPE_VALUE'];
$arProductType['MIN_COUNT_PROD'] = (int)$obElement['PROPERTY_MIN_COUNT_PROD_VALUE'];
}
return $arProductType;
return $discountEntity;
}
public static function setTicketValueFromDiscount($elementId)
{
$includeModule = Loader::includeModule('catalog')
&& Loader::includeModule('iblock');
&& Loader::includeModule('iblock')
&& Loader::includeModule('sale');
if(!$includeModule) {
return false;
}
$iBlockId = discountApp()->config('CATALOG_IBLOCK_ID');
$ticketPropId = discountApp()->config('TICKET_PROP_ID');
$iBlockId = discountApp()->config('catalog_iblock_id');
$map = discountApp()->config('MAP')->toArray();
$rawDiscountList = \CCatalogDiscount::GetDiscount(
$discountList = \CCatalogDiscount::GetDiscount(
$elementId,
$iBlockId,
[], //CATALOG_GROUP_ID
[], //catalog_group_id_list
[2],
'N', // renewal
's1',
false
);
$discountList = [];
foreach ($rawDiscountList as $i => $discount){
$discountId = $discount['ID'];
$arProductType = static::getProductTypeArray($discountId);
$discountList[$discountId]['PAN_SETTINGS'] = $arProductType;
if(!is_array($discountList)){
$discountList = [];
}
// pre($discountList, 0);
$iterator = \CIBlockElement::GetPropertyValues(
$iBlockId,
['ID' => $elementId],
false,
['ID' => $ticketPropId]
);
$ticketPropValueList = [];
$arTicketPropValue = $iterator->Fetch();
if($arTicketPropValue && $arTicketPropValue[$ticketPropId]){
$ticketPropValueList = $arTicketPropValue[$ticketPropId];
$af = app()->factory()->iBlock()->getEntityById($iBlockId);
$propertyFactory = $af->getPropertyFactory();
$elementFactory = $af->getElementFactory();
$elementEntity = $elementFactory->getEntityById($elementId);
if(!$elementEntity){
return false;
}
sort($ticketPropValueList);
$newTicketValue = array_diff($ticketPropValueList, $map);
$pvc = $elementEntity->getPropertyValueCollection();
$currentPropertyValueList = [];
foreach ($map as $type => $typeMap){
foreach ($typeMap as $propertyCode => $propertyValue) {
static::prepareMapPropertyCodeAndValue($propertyCode, $propertyValue);
foreach ($discountList as $discountId => $discount){
$ticketType = $discount['PAN_SETTINGS']['PRODUCT_TYPE'];
if($ticketType && $map[$ticketType]){
$newTicketValue[] = $map[$ticketType];
$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();
}
}
}
sort($newTicketValue);
$currentPropertyValueList = collect($currentPropertyValueList)->sort()->toArray();
pre($currentPropertyValueList);
if($newTicketValue != $ticketPropValueList) {
\CIBlockElement::SetPropertyValuesEx($elementId, $iBlockId, [
$ticketPropId => $newTicketValue?:false
]);
$newPropertyValueList = $currentPropertyValueList;
foreach ($map as $type => $typeMap){
foreach ($typeMap as $propertyCode => $propertyValue) {
static::prepareMapPropertyCodeAndValue($propertyCode, $propertyValue);
if(discountApp()->config('UPDATE_ELEMENT_INDEX')){
$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');
@@ -143,7 +183,10 @@ class Tools
return [];
}
$iBlockId = discountApp()->config('CATALOG_IBLOCK_ID');
$iBlockId = discountApp()->config('catalog_iblock_id', 0);
if(!$iBlockId) {
return [];
}
$rs = \CIBlockElement::GetList(
['ID' => 'ASC'],
@@ -155,7 +198,7 @@ class Tools
],
false,
[
'nTopCount' => discountApp()->config('PAGE_SIZE', 50),
'nTopCount' => discountApp()->config('page_size', 50),
],
['ID', 'IBLOCK_ID']
);