-- =============================================
-- Author:        (C) Vitacard
-- Create date: 2014-11-01
-- Description:    Загрзка накладных
-- =============================================
DROP PROCEDURE IF EXISTS stor.sp_PrepareImportInvoice;

CREATE PROCEDURE stor.sp_PrepareImportInvoice
(
	"UserID" uuid
)
    language plpgsql
as
$$
Begin 
	
    ALTER TABLE IDB.STORAGE_INVOICE ADD ID uuid;
    ALTER TABLE IDB.STORAGE_INVOICE ADD InvoiceID uuid;

    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;
$$;