USE [EnVisage]
GO



ALTER PROCEDURE [dbo].[sp_AddBulkPeopleResource] (
@FirstName nvarchar(250),
@LastName  nvarchar(250),
@Title		nvarchar(200),
@CreditNumber nvarchar(100),
@StartDate datetime=null,
@EndDate	datetime=null,
@JobCode	nvarchar(25),
@EmployeeID nvarchar(100),
@ProcessID	nvarchar(100),
@RecordNumber	int

) AS
	BEGIN
	
		BEGIN TRANSACTION

			declare @isActive  bit =1;
			declare @defaultUofMTypeCode int =5
			declare @defaultGLTypeCode int=6;
			
			declare @defaultECTypeForPeopleResource int =1
			declare @defaultECUseTypeForPeopleResource int =1
			declare @defaultECCGEFXForPeopleResource nvarchar(10) ='CG'
		--	declare @EndDate datetime
		--using title and creditnumber determine if we have matching records to get an EC
			declare @ECID uniqueidentifier 
			declare @CreditDepertmentID uniqueidentifier
			declare @EXID uniqueidentifier
			-- get credit department id
			select @CreditDepertmentID=Id from CreditDepartment where CreditNumber=@CreditNumber
			--get expenditure id based on title
			select @EXID=Id from Expenditure where Jobcode=@JobCode
			if (@EXID is null)
			select @EXID=Id from Expenditure where Name=@Title
			
			
			
			if not @EXID is null
			begin
				select @ECID=Id FROM ExpenditureCategory WHERE ExpenditureId=@EXID AND CreditId=@CreditDepertmentID
			end
			--add new EC??
			if @EXID is null and not @CreditDepertmentID is null
			begin
				select @EXID=NEWID();
				insert into Expenditure (Id, Name,JobCode) values(@EXID,@Title,@JobCode)

			end
			IF @ECID IS null and not @EXID is null and not @CreditDepertmentID is null
			begin
				declare @UofM uniqueidentifier
				declare @GLid uniqueidentifier
				select top 1 @UofM=convert(uniqueidentifier,Value) from SystemSettings where Type=@defaultUofMTypeCode
				select top 1 @GLid=convert(uniqueidentifier,Value) from SystemSettings where Type=@defaultGLTypeCode

				select @ECID =NEWID();
				insert into ExpenditureCategory(Id,ExpenditureId,GLId,UOMId,CreditId,Type,UseType,CGEFX,WksSubjectToFee,SystemAttributeOne,SystemAttributeTwo,Name) values
				(@ECID,@EXID,@GLid,@UofM,@CreditDepertmentID,@defaultECTypeForPeopleResource,@defaultECUseTypeForPeopleResource,
				@defaultECCGEFXForPeopleResource,null,null,null,@title)
			end
			else if not @ECID is null
			begin
				update ExpenditureCategory set Name=@title where Id=@ECID
			end
			 if @CreditDepertmentID  is null
			 begin
				insert into supt_ImportErrors (ProcessID,RecordNbr,Message,DateTimeProcessed) values (@ProcessID,@RecordNumber,'NO CreditCode for "'+@CreditNumber+'"  Record skipped',GETDATE())
				goto exit_commit
			end
			--if we have a credit department id and expenditure id we can get 
			--excpediture category id used to tie to the people resourse
			if @ECID is null
			begin
				insert into supt_ImportErrors (ProcessID,RecordNbr,Message,DateTimeProcessed) values (@ProcessID,@RecordNumber,'NO Expenditure category found for Expenditure:"'+@title+'" Credit Code:"'+@CreditNumber+'"  Record skipped',GETDATE())
				goto exit_commit
			end
			--see if we have an existing people resource record.
			--first search is by first name and employeeid, second is by first and last name
			declare @CurPeopleResourceRecID uniqueidentifier 
			select @CurPeopleResourceRecID= Id FROM PeopleResource where FirstName=@FirstName and EmployeeID=@EmployeeID



			if @CurPeopleResourceRecID is null
			begin
				select @CurPeopleResourceRecID= Id  FROM PeopleResource where FirstName=@FirstName and LastName=@LastName
			end 

		

			-- if we do not have an existing record create a new one
			if @CurPeopleResourceRecID is null
			begin

				if not isnull(@EndDate,'')=''
				begin
					declare @nbrDays int=0
					SELECT @nbrDays= DATEDIFF(day,GETDATE(),@EndDate)
					if (@nbrDays <= 0 )
						set @isActive=0
				end
				if isnull(@EndDate,'')=''
					set @EndDate=DATEADD (YEAR , 50 , getdate() )
				if isnull(@StartDate,'')=''
					set @StartDate=getdate()

				--inserting a new record, the start date should be today, the end date will be today +50 years.
				insert into PeopleResource ( FirstName,LastName,IsActiveEmployee,ExpenditureCategoryId,StartDate,EndDate,EmployeeID) values (@FirstName,@LastName,@isActive,@ECID,@StartDate,@EndDate,@EmployeeID)
			end
			--we have an existing record determine if we have to set the start/end dates based on isActive value
			else
			begin
				declare @curECID uniqueidentifier
				declare @StartDateCur datetime  = GetDate();
				declare @EndDateCur datetime  = GetDate();
				declare @isActiveCur bit
				select @curECID=ExpenditureCategoryId,@isActiveCur=IsActiveEmployee,@StartDateCur=StartDate,@EndDateCur=EndDate FROM PeopleResource where FirstName=@FirstName and LastName=@LastName
				if @isActiveCur <> @isActive
				begin
					if @isActive = 0
					begin
						if isnull(@EndDate ,'') = ''
						set @EndDate=getdate();
					end
					else
					begin
						if @StartDate is null
							set @StartDate=DATEADD(DAY,-1,getdate());
						if isnull(@EndDate ,'') = ''
							set @EndDate=DATEADD (YEAR , 50 , getdate() );
					end
				end 
				else 
				begin
					set @StartDate=@StartDateCur
					set @EndDate=@EndDateCur
				end
				update PeopleResource set StartDate=@StartDate,EndDate=@EndDate, IsActiveEmployee=@isActive, ExpenditureCategoryId=@ECID ,LastName=@LastName, EmployeeID=@EmployeeID where ID=@CurPeopleResourceRecID
			  end 

	exit_commit:
		COMMIT TRANSACTION 
 	END






GO


