222 lines
6.9 KiB
PHP
222 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace Bx\Discount;
|
|
|
|
use Bitrix\Main\Loader;
|
|
use CSite;
|
|
|
|
class Tools
|
|
{
|
|
public static function isSaleDiscountEditPage()
|
|
{
|
|
$isAdminSection = defined('ADMIN_SECTION') && ADMIN_SECTION;
|
|
|
|
if(!$isAdminSection) {
|
|
return false;
|
|
}
|
|
|
|
return CSite::InDir('/bitrix/admin/sale_discount_edit.php')
|
|
// || CSite::InDir('/bitrix/admin/sale_discount.php') // деактивация
|
|
|| CSite::InDir('/bitrix/admin/cat_discount_edit.php')
|
|
|| CSite::InDir('/bitrix/admin/cat_discount.php');
|
|
}
|
|
|
|
public static function isValidRequestData()
|
|
{
|
|
return isset($_POST['PRODUCT_TYPE']);
|
|
}
|
|
|
|
public static function isEnable()
|
|
{
|
|
if(defined('SALE_DISCOUNT_MIGRATE_MODE') && SALE_DISCOUNT_MIGRATE_MODE){
|
|
return false;
|
|
}
|
|
return discountApp()->config('ENABLE');
|
|
}
|
|
|
|
public static function getProductTypeArray($discountID)
|
|
{
|
|
$arProductType = [];
|
|
$arProductType['PRODUCT_TYPE'] = '0';
|
|
$arProductType['MIN_COUNT_PROD'] = 0;
|
|
|
|
if(empty($discountID) || ($discountID < 0)){
|
|
return $arProductType;
|
|
}
|
|
|
|
$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;
|
|
}
|
|
|
|
public static function setTicketValueFromDiscount($elementId)
|
|
{
|
|
$includeModule = Loader::includeModule('catalog')
|
|
&& Loader::includeModule('iblock');
|
|
if(!$includeModule) {
|
|
return false;
|
|
}
|
|
|
|
$iBlockId = discountApp()->config('CATALOG_IBLOCK_ID');
|
|
$ticketPropId = discountApp()->config('TICKET_PROP_ID');
|
|
$map = discountApp()->config('MAP')->toArray();
|
|
|
|
$rawDiscountList = \CCatalogDiscount::GetDiscount(
|
|
$elementId,
|
|
$iBlockId,
|
|
[], //CATALOG_GROUP_ID
|
|
[2],
|
|
'N', // renewal
|
|
's1',
|
|
false
|
|
);
|
|
$discountList = [];
|
|
foreach ($rawDiscountList as $i => $discount){
|
|
$discountId = $discount['ID'];
|
|
$arProductType = static::getProductTypeArray($discountId);
|
|
$discountList[$discountId]['PAN_SETTINGS'] = $arProductType;
|
|
}
|
|
|
|
// pre($discountList, 0);
|
|
|
|
$iterator = \CIBlockElement::GetPropertyValues(
|
|
$iBlockId,
|
|
['ID' => $elementId],
|
|
false,
|
|
['ID' => $ticketPropId]
|
|
);
|
|
$ticketPropValueList = [];
|
|
$arTicketPropValue = $iterator->Fetch();
|
|
if($arTicketPropValue && $arTicketPropValue[$ticketPropId]){
|
|
$ticketPropValueList = $arTicketPropValue[$ticketPropId];
|
|
}
|
|
sort($ticketPropValueList);
|
|
$newTicketValue = array_diff($ticketPropValueList, $map);
|
|
|
|
foreach ($discountList as $discountId => $discount){
|
|
$ticketType = $discount['PAN_SETTINGS']['PRODUCT_TYPE'];
|
|
if($ticketType && $map[$ticketType]){
|
|
$newTicketValue[] = $map[$ticketType];
|
|
}
|
|
}
|
|
sort($newTicketValue);
|
|
|
|
if($newTicketValue != $ticketPropValueList) {
|
|
\CIBlockElement::SetPropertyValuesEx($elementId, $iBlockId, [
|
|
$ticketPropId => $newTicketValue?:false
|
|
]);
|
|
|
|
if(discountApp()->config('UPDATE_ELEMENT_INDEX')){
|
|
\Bitrix\Iblock\PropertyIndex\Manager::updateElementIndex($iBlockId, $elementId);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static function getElementList($lastId = 0)
|
|
{
|
|
$includeModule = Loader::includeModule('iblock');
|
|
if(!$includeModule) {
|
|
return [];
|
|
}
|
|
|
|
$iBlockId = discountApp()->config('CATALOG_IBLOCK_ID');
|
|
|
|
$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));
|
|
}
|
|
} |