-- 1.0.32 Power.TechnologicalConnection
-- Доля горсетей в оплате.
alter table TCContractOutgoings drop column CompanyPercent
alter table TCContractOutgoings add CompanyPart decimal(18, 2)
GO
alter table TCContracts add CompanyPercent int
GO
alter table ConsumingObjects add IsConnected bit
alter table ConsumingObjects add RightHolderID int
GO
update ConsumingObjects set RightHolderID = LegalEntities.LegalEntityID
	FROM LegalEntities inner join Individuals on LegalEntities.LegalEntityID = Individuals.IndividualID and LegalEntities.IsIndividual = 1 
	where Individuals.Name = ConsumingObjects.RightHolder
GO
update ConsumingObjects set RightHolderID = LegalEntities.LegalEntityID
	FROM LegalEntities inner join Организация on LegalEntities.LegalEntityID = Организация.Код_организации and LegalEntities.IsIndividual = 0 
	where Организация.Наименование = ConsumingObjects.RightHolder
GO
ALTER TABLE ConsumingObjects
	ADD FOREIGN KEY (RightHolderID)
	REFERENCES LegalEntities(LegalEntityID)