-- изменяем таблицу балансовой принадлежности объектов
delete from PowerObjectOwners
GO
declare @table_name nvarchar(50)
declare @strSQL nvarchar(1024)

set @table_name=N'PowerObjectOwners'
set @strSQL=''
select @strSQL = @strSQL + ' ALTER TABLE ' + @table_name + ' DROP CONSTRAINT '+ object_name(FK_Tab.id) 
from sysobjects FK_Tab 
where (xtype = 'F' OR xtype = 'pk') AND object_id(@table_name)=parent_obj 

if @strSQL<>''  exec (@strSQL)

GO
alter table PowerObjectOwners drop column ForCompany
alter table PowerObjectOwners drop column ApplicationID
alter table PowerObjectOwners add LegalEntityID int
alter table PowerObjectOwners alter column LegalEntityID int not null
alter table PowerObjectOwners add ConsumingObjectID int
alter table PowerObjectOwners alter column ConsumingObjectID int not null
GO
alter table PowerObjectOwners add PRIMARY KEY (ConsumingObjectID, ItemNum)
GO
ALTER TABLE PowerObjectOwners
	ADD FOREIGN KEY (ConsumingObjectID)
	REFERENCES ConsumingObjects(ID)
	ON DELETE CASCADE
        ON UPDATE CASCADE

ALTER TABLE PowerObjectOwners
	ADD FOREIGN KEY (RefID)
	REFERENCES Объект(Код_объекта)
	ON DELETE CASCADE

ALTER TABLE PowerObjectOwners
	ADD FOREIGN KEY (LegalEntityID)
	REFERENCES LegalEntities(LegalEntityID)
GO
-- добавляем поля к Актам
EXEC sp_rename 'TCActs.Сable', 'AppIsCablePresser', 'COLUMN'
GO
alter table TCActs add ProjectNumber		nvarchar(50)
alter table TCActs add ProjectLegalEntityID 	int 
alter table TCActs add ProjectDate 		datetime

alter table TCActs add WorksStartDate 		datetime
alter table TCActs add WorksEndDate 		datetime
alter table TCActs add WorksLegalEntityID	int 
alter table TCActs add WorksContractNumber	nvarchar(50)

alter table TCActs add DelineationDate	datetime
alter table TCActs add DelineationSigningCompanyDate	datetime
alter table TCActs add DelineationSigningApplicantDate	datetime
alter table TCActs add DelineationSigningThirdDate	datetime
alter table TCActs add DelineationThirdLegalEntityID	int
alter table TCActs add DelineationExecutorID		int

alter table TCActs add UnitInspectionJobDate	datetime
alter table TCActs add UnitInspectionDate	datetime
alter table TCActs add UnitInspectionExecutor	nvarchar(200)

alter table TCActs add CompanyCommissioningActNumber	nvarchar(50)
alter table TCActs add CompanyCommissioningDate		datetime

alter table TCActs add SpecFulfilmentNoteNumber		nvarchar(50)
alter table TCActs add SpecFulfilmentNoteDate		datetime
alter table TCActs add SpecFulfilmentExecutorID		int
alter table TCActs add SpecFulfilmentNotePerformanceDate datetime

alter table TCActs add ActualConnectionApplicationFilingDate	datetime
alter table TCActs add ActualConnectionDate			datetime
alter table TCActs add ActualConnectionExecutorID		int

alter table TCActs add TCFulfilmentActNumber	nvarchar(50)
alter table TCActs add TCFulfilmentActDate	datetime
alter table TCActs add TCFulfilmentActSigningCompanyDate	datetime
alter table TCActs add TCFulfilmentActSigningApplicantDate	datetime

alter table TCActs add SupplyApplicationFilingDate	datetime

alter table TCActs add PermitionActNumber nvarchar(50)
alter table TCActs add PermitionActDate datetime

alter table TCActs add SupplyDate	datetime
alter table TCActs add SupplyExecutorID int
GO

alter table TCActs add foreign key(ProjectLegalEntityID) references LegalEntities(LegalEntityID)
alter table TCActs add foreign key(WorksLegalEntityID) references LegalEntities(LegalEntityID)
alter table TCActs add foreign key(DelineationThirdLegalEntityID) references LegalEntities(LegalEntityID)
alter table TCActs add foreign key(DelineationExecutorID) references Персонал(Код_работника)
alter table TCActs add foreign key(SpecFulfilmentExecutorID) references Персонал(Код_работника)
alter table TCActs add foreign key(ActualConnectionExecutorID) references Персонал(Код_работника)
alter table TCActs add foreign key(SupplyExecutorID) references Персонал(Код_работника)
GO
-- добавляем хранение пользовательских настроек
alter table Users add Settings image
