--DigitalForm Old Record Update
INSERT INTO [sStudent].[tTempDigitalFormScopeHistory]
Select * from [sStudent].[tDigitalFormScope]

create table #tmpclass
(
	Id int identity,
	DigitalFormId int,
	SourceTypeId smallint,
	SourceId bigint
)

Insert into #tmpClass
select * from sStudent.tDigitalFormScope 
where DigitalFormId IN (select Id from [sStudent].[tDigitalForm]) AND SourceTypeId IN (2016,2017)

DECLARE @lCount int,@lId int
declare @lSourceTypeId int,@lDigitalFormId int,@lSourceId bigint
SELECT @lCount =  COUNT(Id) FROM #tmpClass

create table #tmpDivision
(
	Id int identity,
	DigitalFormId int,
	SourceId bigint,
	SourceTypeId smallint
)

WHILE @lCount > 0 
 	BEGIN
		SET @lSourceTypeId = (Select SourcetypeId FROM #tmpClass where Id = @lCount)
		SET @lDigitalFormId = (Select DigitalFormId FROM #tmpClass where Id = @lCount)
		SET @lSourceId = (Select SourceId FROM #tmpClass where Id = @lCount)

		IF(@lSourceTypeId=2017)
		BEGIN
		Insert into #tmpDivision
		select @lDigitalFormId, Id AS SourceId, 2016 from sSetup.tClassMaster where DepartmentId = @lSourceId
		END

		ELSE
		BEGIN
			Insert into #tmpDivision
			select DigitalFormId,SourceId,SourceTypeId from #tmpClass where SourceID = @lSourceId
		END
		SET @lCount = @lCount - 1
	END	


DECLARE @lCount1 int,@lId1 int
declare @lDigitalFormId1 int,@lSourceId1 bigint
SELECT @lCount1 =  COUNT(Id) FROM #tmpDivision

WHILE @lCount1 > 0 
BEGIN
	SET @lDigitalFormId1 = (Select DigitalFormId FROM #tmpClass where Id = @lCount1)
	SET @lSourceId1 = (Select SourceId FROM #tmpClass where Id = @lCount1)

	Insert Into sStudent.tDigitalFormScope 
	Select @lDigitalFormId1,2005,Id AS SourceId from sSetup.tDivisionMaster where ClassId = @lSourceId1

	SET @lCount1 = @lCount1 - 1
END

delete from sStudent.tDigitalFormScope where SourceTypeId IN (2016,2017)
drop table  #tmpDivision
drop table #tmpclass


--Update Employee EUID
UPDATE sHrm.tEmployeeMaster
SET EUId = Id
WHERE EUId IS NULL


--for AnuualIncome Label Change for Old Record

UPDATE sAdmission.tAdmissionCustomSearch
SET ColumnName = Replace(ColumnName,'FatherIncome','FatherAnnualIncome')

UPDATE sAdmission.tAdmissionCustomSearch
SET ColumnName = Replace(ColumnName,'MotherIncome','MotherAnnualIncome')

UPDATE sAdmission.tAdmissionCustomSearch
SET ColumnName = Replace(ColumnName,'RelativeIncome','RelativeAnnualIncome')


UPDATE sStudent.tStudentCustomSearch
SET ColumnName = Replace(ColumnName,'FatherIncome','FatherAnnualIncome')

UPDATE sStudent.tStudentCustomSearch
SET ColumnName = Replace(ColumnName,'MotherIncome','MotherAnnualIncome')

UPDATE sStudent.tStudentCustomSearch
SET ColumnName = Replace(ColumnName,'RelativeIncome','RelativeAnnualIncome')
