-- =============================================
-- Author:        (C) Vitacard
-- Create date: 2015-12-17
-- Description:   Удаление загруженных накладных
-- =============================================
DROP PROCEDURE IF EXISTS stor.sp_DeleteImportInvoice;

CREATE PROCEDURE stor.sp_DeleteImportInvoice
(
	"UserID" uuid
)
    language plpgsql
as
$$
Begin 
	
	delete from STOR.T_IMPORT_INVOICE ii
		using STOR.T_IMPORT_ELEMENTS ie
	where ie.ImportElementsID = ii.ImportElementsID and ie.Users = "UserID";

	delete from STOR.T_IMPORT_ELEMENTS  where Users = "UserID" and type_ = (select e.EntityID from VCLib.T_ENTITIES e where e.EntName = 'IMPORT_INVOICE' limit 1);

End;
$$;