


declare @uid as varchar(100)
declare @eid as varchar

-- parametro general para items por listaprecio

select @uid=uid , @eid=eid from seg_usuario where id=1

--select * from gen_config where modulo='documentos'


if not exists(select * from gen_config where modulo='documentos' and config ='cotizarListaPrecio')
begin 
INSERT INTO [gen_config]
           ([uid]
           ,[eid]
           ,[id_usuario_modifico]
           ,[id_usuario_creo]
           ,[fechaModificacion]
           ,[fechaCreacion]
           ,[active]
           ,[config]
           ,[config_codigo]
           ,[modulo]
           ,[valor]
           ,[descripcion])
     VALUES
           (@uid
           ,@eid
           ,1
           ,1
           ,GETDATE()
           ,GETDATE()
           ,1
           ,'cotizarListaPrecio'
           ,''
           ,'documentos'
           ,'0'
           ,'')

end 

--intermedia sucursal-listaprecio

if not exists(select * FROM INFORMATION_SCHEMA.TABLES  where Table_Name='ter_sucursal_listaprecio')
BEGIN
EXECUTE [_ini_creartabla] 'ter_sucursal_listaprecio',1
END

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='ter_sucursal_listaprecio' and column_name = 'id_sucursal' )
begin
alter table [ter_sucursal_listaprecio] add [id_sucursal] int not null default 0
end
if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='ter_sucursal_listaprecio' and column_name = 'id_listaprecio' )
begin
alter table [ter_sucursal_listaprecio] add [id_listaprecio] int not null default 0
end

IF NOT  EXISTS (SELECT * FROM sys.foreign_keys WHERE parent_object_id = OBJECT_ID(N'[dbo].[ter_sucursal_listaprecio]') AND object_id = OBJECT_ID(N'[dbo].[FK_ter_sucursal_listaprecio_id_listaprecio]')  )
begin

ALTER TABLE [ter_sucursal_listaprecio] ADD CONSTRAINT
	FK_ter_sucursal_listaprecio_id_listaprecio FOREIGN KEY
	([id_listaprecio]) REFERENCES [cat_listaPrecio]
	(id) ON UPDATE  NO ACTION ON DELETE  NO ACTION 
end


IF NOT  EXISTS (SELECT * FROM sys.foreign_keys WHERE parent_object_id = OBJECT_ID(N'[dbo].[ter_sucursal_listaprecio]') AND object_id = OBJECT_ID(N'[dbo].[FK_ter_sucursal_listaprecio_id_sucursal]')  )
begin

 ALTER TABLE [ter_sucursal_listaprecio] ADD CONSTRAINT
	FK_ter_sucursal_listaprecio_id_sucursal FOREIGN KEY
	([id_sucursal]) REFERENCES [ter_sucursal]
	(id) ON UPDATE  NO ACTION ON DELETE  NO ACTION 
	
end	

--cat_listaprecio

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='cat_listaprecio' and column_name = 'esPrivado' )
begin
alter table [cat_listaprecio] add [esPrivado] bit not null default 0
end

-- itemdocumento

if not exists(select * FROM INFORMATION_SCHEMA.COLUMNS  where Table_Name='doc_itemdocumento' and column_name = 'id_listaprecio' )
begin
alter table [doc_itemdocumento] add [id_listaprecio] int not null default 0
end


IF NOT  EXISTS (SELECT * FROM sys.foreign_keys WHERE parent_object_id = OBJECT_ID(N'[dbo].[doc_itemDocumento]') AND object_id = OBJECT_ID(N'[dbo].[FK_doc_itemDocumento_id_listaprecio]')  )
begin
 ALTER TABLE [doc_itemDocumento] ADD CONSTRAINT
	FK_doc_itemDocumento_id_listaprecio FOREIGN KEY
	([id_listaprecio]) REFERENCES [cat_listaPrecio]
	(id) ON UPDATE  NO ACTION ON DELETE  NO ACTION 
	
end 