-- =============================================
-- Author:        (C) Vitacard
-- Create date: 2015-12-15
-- Description:    Загрзка контрактов
-- =============================================
DROP PROCEDURE IF EXISTS stor.sp_PrepareImportContract;

CREATE PROCEDURE stor.sp_PrepareImportContract
(
	"UserID" uuid
)
    language plpgsql
as
$$
Begin 
  
    ALTER TABLE idb.SUPPLIER_CONTRACT ADD ID uuid;
    alter table idb.SUPPLIER_CONTRACT add id_farm integer;
	
    delete from STOR.T_IMPORT_CONTRACT ic
        using STOR.T_IMPORT_ELEMENTS ie
    where ie.ImportElementsID = ic.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_CONTRACT' limit 1);
    
End;
$$;