-- =============================================
-- Author:        (C) Vitacard
-- Create date: 2015-12-15
-- Description:    Загрзка контрактов
-- =============================================
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[STOR].[sp_PrepareImportContract]') AND type in (N'P', N'PC'))
DROP PROCEDURE [STOR].[sp_PrepareImportContract]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [STOR].[sp_PrepareImportContract] 
    @UserID uniqueidentifier
AS
BEGIN
    ALTER TABLE idb.SUPPLIER_CONTRACT ADD ID uniqueidentifier
    alter table idb.SUPPLIER_CONTRACT add id_farm int
	
	declare @IdEnt int
	
	select top 1 @IdEnt = e.EntityID from VCLib.T_ENTITIES e where e.EntName = 'IMPORT_CONTRACT'

    delete ic
    from [STOR].T_IMPORT_CONTRACT ic
        join [STOR].T_IMPORT_ELEMENTS ie on ie.ImportElementsID = ic.ImportElementsID
    where ie.Users = @UserID

    delete [STOR].T_IMPORT_ELEMENTS  where Users = @UserID and type_ = @IdEnt

END