CREATE PROCEDURE tmp_AddRule
	@structId int,
	@isAvailable bit,
	@isRequired bit
AS
DECLARE @productId int = (SELECT TOP 1 Id FROM Product WITH(NOLOCK) WHERE ProductDictCode = 'KASKO' AND State = 2 and IsDeleted = 0),
		@condition varchar(2000) = CONCAT(
			'DiKaskoConstructorExternalFunctionsInvoker.',
			IIF(@isAvailable IS NOT NULL, 'IsConditionAllowedByBank', 'IsConditionRequiredByBank'),
			'(Deal,', @structId, ')'),
		@ruleId int;

SELECT @ruleId = Id FROM [Rule] WITH(NOLOCK) WHERE Condition = @condition AND Product_Id=@productId

IF @ruleId IS NULL
BEGIN 
	INSERT INTO [Rule] (Condition, EntityId, AreaId, CreateDate, State, DictionaryDate,	IsDeleted, Product_Id, InitializationType)
	VALUES (@condition, NEXT VALUE FOR SQ_Rule, 1,	GETDATE(), 2, GETDATE(), 0, @productId, 'FirstInitialization')
	
	SELECT @ruleId = SCOPE_IDENTITY()
END

IF NOT EXISTS (SELECT 1 FROM ConditionSetting WITH(NOLOCK) WHERE structId = @structId and Rule_Id = @ruleId)
BEGIN
	INSERT INTO ConditionSetting (StructId, 
									IsDefault, IsRequired,
									IsAvailable, [Check], Uncheck,
									EntityId, AreaId,
									CreateDate, State, DictionaryDate,
									IsDeleted, Rule_Id)
	VALUES (@structId,	--StructId
						NULL, @isRequired,	--IsDefault, IsRequired,
						@isAvailable, NULL, NULL,	--IsAvailable, [Check], Uncheck,
						NEXT VALUE FOR SQ_ConditionSetting, 1,	--EntityId, AreaId,
						GETDATE(), 2, GETDATE(),	--CreateDate, State, DictionaryDate,
						0, @ruleId)	--IsDeleted, Rule_Id
END

IF NOT EXISTS (SELECT 1 FROM StructIdForRule WITH(NOLOCK) WHERE structId = @structId and Rule_Id = @ruleId)
BEGIN
	INSERT INTO StructIdForRule (StructId,
									EntityId, AreaId,
									CreateDate, State, DictionaryDate,
									IsDeleted, Rule_Id)
	VALUES (@structId,	--StructId
						NEXT VALUE FOR SQ_StructIdForRule, 1,	--EntityId, AreaId,
						GETDATE(), 2, GETDATE(),	--CreateDate, State, DictionaryDate,
						0, @ruleId)	--IsDeleted, Rule_Id
END
GO

EXEC tmp_AddRule 5012, 0, NULL
EXEC tmp_AddRule 5013, 0, NULL
EXEC tmp_AddRule 5551, 0, NULL
EXEC tmp_AddRule 5014, 0, NULL
EXEC tmp_AddRule 5018, 0, NULL
EXEC tmp_AddRule 5019, 0, NULL
EXEC tmp_AddRule 5020, 0, NULL
EXEC tmp_AddRule 5021, 0, NULL
EXEC tmp_AddRule 5004, 0, NULL
EXEC tmp_AddRule 5006, 0, NULL
EXEC tmp_AddRule 5005, 0, NULL
EXEC tmp_AddRule 5011, 0, NULL
EXEC tmp_AddRule 5016, 0, NULL
EXEC tmp_AddRule 5031, 0, NULL
EXEC tmp_AddRule 5029, 0, NULL
EXEC tmp_AddRule 5036, 0, NULL
EXEC tmp_AddRule 5484, 0, NULL
EXEC tmp_AddRule 5485, 0, NULL
EXEC tmp_AddRule 5486, 0, NULL

EXEC tmp_AddRule 5012, NULL, 1
EXEC tmp_AddRule 5013, NULL, 1
EXEC tmp_AddRule 5551, NULL, 1
EXEC tmp_AddRule 5014, NULL, 1
EXEC tmp_AddRule 5018, NULL, 1
EXEC tmp_AddRule 5019, NULL, 1
EXEC tmp_AddRule 5020, NULL, 1
EXEC tmp_AddRule 5021, NULL, 1
EXEC tmp_AddRule 5004, NULL, 1
EXEC tmp_AddRule 5006, NULL, 1
EXEC tmp_AddRule 5005, NULL, 1
EXEC tmp_AddRule 5011, NULL, 1
EXEC tmp_AddRule 5016, NULL, 1
EXEC tmp_AddRule 5031, NULL, 1
EXEC tmp_AddRule 5029, NULL, 1
EXEC tmp_AddRule 5036, NULL, 1
EXEC tmp_AddRule 5484, NULL, 1
EXEC tmp_AddRule 5485, NULL, 1
EXEC tmp_AddRule 5486, NULL, 1

DROP PROCEDURE tmp_AddRule
GO


Расскажи как устанавливается параметр isAvailable