USE [EnVisage]
GO

ALTER FUNCTION [dbo].[resourceAvailability_f](@StartDate datetime, @EndDate datetime, @Per char(1), @AvailableHours float, @DisregardNPTime bit)

RETURNS @restable TABLE
   (FirstName  nvarchar(1000),
    LastName   nvarchar(1000),
    ExpenditureCategory       nvarchar(800),
    AvailableHours       float,
    AllocatedHours       float,
    NonProjectTimeHours        float,
	ResourceAvailability       float,
    TeamName	nvarchar(400),
	TeamId	varchar(36),
	ExpenditureCategoryId varchar(36)
    )
AS
BEGIN
 if (@Per = 1)
  begin
	INSERT INTO @restable
	select vw.FirstName, vw.LastName, vw.Name as ExpenditureCategory, 
	sum(vw.AvailableHours) as AvailableHours, sum(vw.AllocatedHours) as AllocatedHours, 
	sum(vw.NonProjectTimeHours ) as NonProjectTimeHours,
	sum(vw.AvailableHours)-sum(vw.AllocatedHours)-sum(vw.NonProjectTimeHours) as ResourceAvailability,
	vw.TeamName, vw.teamId, vw.expId
	From(
		select 
		pr.Id,
		ec2.Id as expId,
		t.Id as teamId,
		pr.FirstName,
		pr.LastName,
		ec2.Name, 
		t.Name as TeamName,
		fc.EndDate, 

		((select UOM.UOMValue
		from UOM, ExpenditureCategory ec
		where UOM.Id=ec.UOMId and ec.Id=pr.ExpenditureCategoryId)
		) as AvailableHours, 

		ISNULL((select SUM (pa.Quantity)
		From PeopleResourceAllocation pa
			inner join Scenario s on s.Id = pa.ScenarioId and s.Status = 1
		Where pa.PeopleResourceId=pr.Id and pa.TeamId = pr.TeamId and pa.WeekEndingDate = fc.EndDate), 0) as AllocatedHours,

		ISNULL((select SUM (pt.HoursOff)
		From NonProjectTimeAllocation pt
		Where pt.PeopleResourceId=pr.Id and pt.WeekEndingDate = fc.EndDate), 0) as NonProjectTimeHours
		
		from FiscalCalendar fc
			join VW_TeamResource pr on fc.StartDate >= pr.TeamStartDate and (pr.TeamEndDate is null or fc.EndDate <= pr.TeamEndDate) --(not(fc.StartDate > pr.EndDate) and not(fc.EndDate < pr.StartDate))
			join ExpenditureCategory ec2 on ec2.Id=pr.ExpenditureCategoryId
			join Team t on t.Id = pr.TeamId 
		where  fc.StartDate >= (select fc1.StartDate from FiscalCalendar fc1 where fc1.StartDate <= @StartDate and fc1.EndDate >= @StartDate and fc1.Type = 0)
		and fc.EndDate <= (select fc1.EndDate from FiscalCalendar fc1 where fc1.StartDate <= @EndDate and fc1.EndDate >= @EndDate and fc1.Type = 0)
		and fc.Type = 0 and
		not pr.Id in (
			select sub.Id from
				(select pr.Id,
				((select UOM.UOMValue * count(fc.Id)
				from UOM, ExpenditureCategory ec, PeopleResource pr1
				where UOM.Id=ec.UOMId and ec.Id=pr.ExpenditureCategoryId and pr1.Id = pr.Id)
				-
				ISNULL((select SUM (pa.Quantity)
				From PeopleResourceAllocation pa
					inner join Scenario s on s.Id = pa.ScenarioId and s.Status = 1
				Where pa.PeopleResourceId=pr.Id and pa.TeamId = pr.TeamId and pa.WeekEndingDate >= min(fc.EndDate) and pa.WeekEndingDate <= max(fc.EndDate)), 0)
				-
				case when @DisregardNPTime = 0 then
				ISNULL((select SUM (pt.HoursOff)
				From NonProjectTimeAllocation pt
				Where pt.PeopleResourceId=pr.Id and pt.WeekEndingDate >= min(fc.EndDate) and pt.WeekEndingDate <= max(fc.EndDate)), 0) else 0 end --as TrainingHours,
				) as AvailableHours

				from FiscalCalendar fc
					join VW_TeamResource pr on fc.StartDate >= pr.TeamStartDate and (pr.TeamEndDate is null or fc.EndDate <= pr.TeamEndDate)
				where  
				fc.EndDate >= cast((cast(datepart(YEAR, (select fc1.StartDate from FiscalCalendar fc1 where fc1.StartDate <= @StartDate and fc1.EndDate >= @StartDate and fc1.Type = 0)) as varchar(4)) + '-'+ cast(datepart(MONTH, (select fc1.StartDate from FiscalCalendar fc1 where fc1.StartDate <= @StartDate and fc1.EndDate >= @StartDate and fc1.Type = 0)) as varchar(4)) + '-01') as datetime) and
				fc.EndDate <= dateadd(day, -1, dateadd(Month, 1, cast((cast(datepart(YEAR, (select fc1.EndDate from FiscalCalendar fc1 where fc1.StartDate <= @EndDate and fc1.EndDate >= @EndDate and fc1.Type = 0)) as varchar(4)) + '-' + cast(datepart(MONTH, (select fc1.EndDate from FiscalCalendar fc1 where fc1.StartDate <= @EndDate and fc1.EndDate >= @EndDate and fc1.Type = 0)) as varchar(4)) + '-01') as datetime))) and fc.Type = 0
				group by datepart(MONTH,fc.EndDate), datepart(YEAR,fc.EndDate), pr.Id, pr.TeamId, pr.ExpenditureCategoryId, pr.TeamStartDate, pr.TeamEndDate
			)sub where sub.AvailableHours < @AvailableHours	
		)
	) as vw 
	group by vw.Id, vw.expId, vw.teamId, vw.FirstName, vw.LastName, vw.Name, vw.TeamName--, vw.EndDate
  end
  else if(@Per= 2)
  begin
	INSERT INTO @restable

	select 
	vw.FirstName, vw.LastName, vw.Name as ExpenditureCategory, 
	sum(vw.AvailableHours) as AvailableHours, sum(vw.AllocatedHours) as AllocatedHours, 
	sum(vw.NonProjectTimeHours ) as NonProjectTimeHours,
	sum(vw.AvailableHours)-sum(vw.AllocatedHours)-sum(vw.NonProjectTimeHours) as ResourceAvailability,
	vw.TeamName, vw.teamId, vw.expId
	From(
		select 
		pr.Id,
		ec2.Id as expId,
		t.Id as teamId,
		pr.FirstName,
		pr.LastName,
		ec2.Name, 
		t.Name as TeamName,
		fc.EndDate, 

		((select UOM.UOMValue
		from UOM, ExpenditureCategory ec
		where UOM.Id=ec.UOMId and ec.Id=pr.ExpenditureCategoryId)
		) as AvailableHours, 

		ISNULL((select SUM (pa.Quantity)
		From PeopleResourceAllocation pa
			inner join Scenario s on s.Id = pa.ScenarioId and s.Status = 1
		Where pa.PeopleResourceId=pr.Id and pa.TeamId = pr.TeamId and pa.WeekEndingDate = fc.EndDate), 0) as AllocatedHours,

		ISNULL((select SUM (pt.HoursOff)
		From NonProjectTimeAllocation pt
		Where pt.PeopleResourceId=pr.Id and pt.WeekEndingDate = fc.EndDate), 0) as NonProjectTimeHours

		from FiscalCalendar fc
			join VW_TeamResource pr on fc.StartDate >= pr.TeamStartDate and (pr.TeamEndDate is null or fc.EndDate <= pr.TeamEndDate) --(not(fc.StartDate > pr.EndDate) and not(fc.EndDate < pr.StartDate))
			join ExpenditureCategory ec2 on ec2.Id=pr.ExpenditureCategoryId
			join Team t on t.Id = pr.TeamId 
		where  fc.StartDate >= (select fc1.StartDate from FiscalCalendar fc1 where fc1.StartDate <= @StartDate and fc1.EndDate >= @StartDate and fc1.Type = 0)
		and fc.EndDate <= (select fc1.EndDate from FiscalCalendar fc1 where fc1.StartDate <= @EndDate and fc1.EndDate >= @EndDate and fc1.Type = 0)
		and fc.Type = 0	 and
		not pr.Id in (
			select sub.Id from
				(select 
				pr.Id,
				((select UOM.UOMValue 
				from UOM, ExpenditureCategory ec, PeopleResource pr1
				where UOM.Id=ec.UOMId and ec.Id=pr.ExpenditureCategoryId and pr1.Id = pr.Id)
				-
				ISNULL((select SUM (pa.Quantity)
				From PeopleResourceAllocation pa
					inner join Scenario s on s.Id = pa.ScenarioId and s.Status = 1
				Where pa.PeopleResourceId=pr.Id and pa.TeamId = pr.TeamId and pa.WeekEndingDate = fc.EndDate), 0)
				-
				case when @DisregardNPTime = 0 then
				ISNULL((select SUM (pt.HoursOff)
				From NonProjectTimeAllocation pt
				Where pt.PeopleResourceId=pr.Id and pt.WeekEndingDate =fc.EndDate), 0) else 0 end --as TrainingHours,
				) as AvailableHours

				from FiscalCalendar fc
					join VW_TeamResource pr on fc.StartDate >=pr.TeamStartDate and (pr.TeamEndDate is null or fc.EndDate <= pr.TeamEndDate)
				where  fc.StartDate >= (select fc1.StartDate from FiscalCalendar fc1 where fc1.StartDate <= @StartDate and fc1.EndDate >= @StartDate and fc1.Type = 0)
				and fc.EndDate <= (select fc1.EndDate from FiscalCalendar fc1 where fc1.StartDate <= @EndDate and fc1.EndDate >= @EndDate and fc1.Type = 0)
				and fc.Type = 0	
			)sub where sub.AvailableHours < @AvailableHours	
		)
	) as vw 
	group by vw.Id, vw.expId, vw.teamId, vw.FirstName, vw.LastName, vw.Name, vw.TeamName--, vw.EndDate
 end
 else
	begin
    INSERT INTO @restable

	select 
	vw.FirstName, vw.LastName, vw.Name as ExpenditureCategory, 
	sum(vw.AvailableHours) as AvailableHours, sum(vw.AllocatedHours) as AllocatedHours, 
	sum(vw.NonProjectTimeHours ) as NonProjectTimeHours,
	sum(vw.AvailableHours)-sum(vw.AllocatedHours)-sum(vw.NonProjectTimeHours ) as ResourceAvailability,
	vw.TeamName, vw.teamId, vw.expId

	From(

	select 
		pr.Id,
		ec2.Id as expId,
		t.Id as teamId,
		pr.FirstName,
		pr.LastName,
		ec2.Name, 
		t.Name as TeamName,
		fc.EndDate, 

		((select UOM.UOMValue
		from UOM, ExpenditureCategory ec
		where UOM.Id=ec.UOMId and ec.Id=pr.ExpenditureCategoryId)
		--*((cast( ww.Monday as int) + cast(ww.Tuesday as int) +  cast(ww.Wednesday as int) + cast(ww.Thursday as int) + cast(ww.Friday as int) + cast(ww.Saturday as int) + cast(ww.Sunday as int)) /5.0)
		) as AvailableHours, 

		ISNULL((select SUM (pa.Quantity)
		From PeopleResourceAllocation pa
			inner join Scenario s on s.Id = pa.ScenarioId and s.Status = 1
		Where pa.PeopleResourceId=pr.Id and pa.TeamId = pr.TeamId and pa.WeekEndingDate = fc.EndDate), 0) as AllocatedHours,

		ISNULL((select SUM (pt.HoursOff)
		From NonProjectTimeAllocation pt
		Where pt.PeopleResourceId=pr.Id and pt.WeekEndingDate = fc.EndDate), 0) as NonProjectTimeHours

		from FiscalCalendar fc
			join VW_TeamResource pr on fc.StartDate >= pr.TeamStartDate and (pr.TeamEndDate is null or fc.EndDate <= pr.TeamEndDate) --(not(fc.StartDate > pr.EndDate) and not(fc.EndDate < pr.StartDate))
			join ExpenditureCategory ec2 on ec2.Id=pr.ExpenditureCategoryId
			join Team t on t.Id = pr.TeamId 
		where fc.StartDate >= (select fc1.StartDate from FiscalCalendar fc1 where fc1.StartDate <= @StartDate and fc1.EndDate >= @StartDate and fc1.Type = 0)
		and fc.EndDate <= (select fc1.EndDate from FiscalCalendar fc1 where fc1.StartDate <= @EndDate and fc1.EndDate >= @EndDate and fc1.Type = 0)
		and fc.Type = 0	
	) as vw 
group by vw.Id, vw.expId, vw.teamId, vw.FirstName, vw.LastName, vw.Name, vw.TeamName, vw.AvailableHours--, vw.EndDate
having 
	((@DisregardNPTime = 0 and @AvailableHours <= (sum(vw.AvailableHours)-sum(vw.AllocatedHours)-sum(vw.NonProjectTimeHours ))) or 
	 (@DisregardNPTime = 1 and @AvailableHours <= (sum(vw.AvailableHours) - sum(vw.AllocatedHours))))
end
RETURN  
END 

GO


