CREATE OR ALTER PROCEDURE tmp_AddTarifficationAttribute
	@tarifficationAttributeName VARCHAR(250), 
	@tarifficationAttributePath VARCHAR(2000),
	@tarifficationAttributeId INT,
	@tarifficationAttributeEntityId INT,
	@componenTarifficationAttributeId INT,
	@componenTarifficationAttributeEntityId INT
AS
BEGIN
DECLARE @tarifficationAttributeTypeGroupVersionId INT = (SELECT MAX(Id) FROM TarifficationAttributeTypeGroupVersion)

IF NOT EXISTS (SELECT * FROM TarifficationAttribute WHERE Id = @tarifficationAttributeId and IsDeleted = 0)
BEGIN

	ALTER TABLE [dbo].TarifficationAttribute DISABLE TRIGGER [TarifficationAttribute_ON_SYNC_INSERT]

		INSERT INTO TarifficationAttribute
		(
			[Id],
			[EntityId],
			[Name],
			[Path],
			[TarifficationAttributeTypeCode],
			[TarifficationAttributeTypeGroupVersionId],
			[AreaId],
			[CreateDate],
			[State],
			[DictionaryDate],
			[IsDeleted],
			[Product_Id]
		)
		VALUES(
			@tarifficationAttributeId,
			@tarifficationAttributeEntityId,
			@tarifficationAttributeName,
			@tarifficationAttributePath,
			'DealAttribute',
			@tarifficationAttributeTypeGroupVersionId,
			1,
			GETDATE(),
			2,
			GETDATE(),
			0,
			14);

	ALTER TABLE [dbo].TarifficationAttribute ENABLE TRIGGER [TarifficationAttribute_ON_SYNC_INSERT]

END


IF NOT EXISTS (
SELECT 1 
FROM   [dbo].[ComponentTarifficationAttribute]
WHERE  [Id] = @componenTarifficationAttributeId
)
BEGIN
	IF EXISTS (
		SELECT 1
		FROM   [sys].[triggers]
		WHERE  [Name] = 'ComponentTarifficationAttribute_ON_SYNC_INSERT'
	) 
		DISABLE TRIGGER [dbo].[ComponentTarifficationAttribute_ON_SYNC_INSERT] 
		ON              [dbo].[ComponentTarifficationAttribute];

		IF NOT EXISTS (SELECT TOP 1 * FROM ComponentTarifficationAttribute WHERE Id = @componenTarifficationAttributeId AND EntityId = @componenTarifficationAttributeEntityId)
		BEGIN
				INSERT INTO ComponentTarifficationAttribute(
					[Id],
					[EntityId],
					[AreaId],
					[CreateDate],
					[State],
					[DictionaryDate],
					[TarifficationAttribute_Id],
					[IsDeleted]
				)
				VALUES(
					@componenTarifficationAttributeId,
					@componenTarifficationAttributeEntityId,
					1,
					GETDATE(),
					2,
					GETDATE(),
					@TarifficationAttributeId,
					0
				)
		END

		IF EXISTS (
			SELECT 1 
			FROM   [sys].[triggers]
			WHERE  [Name] = 'ComponentTarifficationAttribute_ON_SYNC_INSERT'
		) 
			ENABLE TRIGGER [dbo].[ComponentTarifficationAttribute_ON_SYNC_INSERT]
			ON             [dbo].[ComponentTarifficationAttribute];
END

DECLARE @flCoefficientId INT = (SELECT TOP 1 Id FROM Coefficient WHERE Name = 'K_Correction_FL')
DECLARE @ulCoefficientId INT = (SELECT TOP 1 Id FROM Coefficient WHERE Name = 'K_Correction_UL')
DECLARE @flCalculationComponentCoefficientId INT = (SELECT TOP 1 Id FROM CalculationComponentCoefficient WHERE Coefficient_Id = @flCoefficientId)
DECLARE @ulCalculationComponentCoefficientId INT = (SELECT TOP 1 Id FROM CalculationComponentCoefficient WHERE Coefficient_Id = @ulCoefficientId)

IF NOT EXISTS (SELECT TOP 1 * FROM CalculationComponentCoefficientComponentTarifficationAttribute WHERE CalculationComponentCoefficient_Id = @flCalculationComponentCoefficientId
AND ComponentTarifficationAttribute_Id = @componenTarifficationAttributeId)
BEGIN
	INSERT INTO CalculationComponentCoefficientComponentTarifficationAttribute
		(CalculationComponentCoefficient_Id, ComponentTarifficationAttribute_Id)
	VALUES
		(@flCalculationComponentCoefficientId, @componenTarifficationAttributeId)
END

IF NOT EXISTS (SELECT TOP 1 * FROM CalculationComponentCoefficientComponentTarifficationAttribute WHERE CalculationComponentCoefficient_Id = @ulCalculationComponentCoefficientId
AND ComponentTarifficationAttribute_Id = @componenTarifficationAttributeId)
BEGIN
	INSERT INTO CalculationComponentCoefficientComponentTarifficationAttribute
		(CalculationComponentCoefficient_Id, ComponentTarifficationAttribute_Id)
	VALUES
		(@ulCalculationComponentCoefficientId, @componenTarifficationAttributeId)
END
END
GO
-- Описание колонки в таблице коэфф, 'Deal.GetCashbookId()', @tarifficationAttributeId INT, tarifficationAttributeEntityId, componenTarifficationAttributeId, componenTarifficationAttributeEntityId
EXEC tmp_AddTarifficationAttribute 'CashbookId', 'Deal.GetCashbookId()', 2485, 2484, 6198, 6144
EXEC tmp_AddTarifficationAttribute 'Новое авто', 'Deal.IsNewAuto()', 2486, 2485, 6199, 6145
EXEC tmp_AddTarifficationAttribute 'Марка авто', 'Deal.InsuranceObjects.Auto.AutoBrandName', 2487, 2486, 6200, 6146
EXEC tmp_AddTarifficationAttribute 'Модель авто', 'Deal.InsuranceObjects.Auto.AutoModelName', 2488, 2487, 6201, 6147
GO
DROP PROCEDURE tmp_AddTarifficationAttribute
GO