-- =============================================
-- Author:        (C) Vitacard
-- Create date: 2014-11-01
-- Description:    Загрзка накладных
-- =============================================
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[STOR].[sp_PrepareImportInvoice]') AND type in (N'P', N'PC'))
DROP PROCEDURE [STOR].[sp_PrepareImportInvoice]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [STOR].[sp_PrepareImportInvoice] 
    @UserID uniqueidentifier
AS
BEGIN
	
	declare @ImpInv int
	select top 1 @ImpInv = e.EntityID from VCLib.T_ENTITIES e where e.EntName = 'IMPORT_INVOICE'

    ALTER TABLE idb.STORAGE_INVOICE ADD ID uniqueidentifier
    ALTER TABLE idb.STORAGE_INVOICE ADD InvoiceID uniqueidentifier

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

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

END