227 lines
7.9 KiB
Transact-SQL
227 lines
7.9 KiB
Transact-SQL
USE [EnVisage]
|
|
GO
|
|
|
|
ALTER FUNCTION [dbo].[resourceAvailability_f](@StartDate datetime, @EndDate datetime, @Per char(1), @AvailableHours float)
|
|
|
|
RETURNS @restable TABLE
|
|
|
|
(FirstName varchar(100),
|
|
|
|
LastName varchar(100),
|
|
|
|
ExpenditureCategory varchar(100),
|
|
|
|
AvailableHours float,
|
|
|
|
AllocatedHours float,
|
|
|
|
TrainingHours float,
|
|
|
|
VacationHours float,
|
|
|
|
ResourceAvailability float,
|
|
|
|
TeamName varchar(100)
|
|
|
|
)
|
|
|
|
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.TrainingHours) as TrainingHours, sum(vw.VacationHours) as VacationHours,
|
|
sum(vw.AvailableHours)-sum(vw.AllocatedHours)-sum(vw.TrainingHours)-sum(vw.VacationHours) as ResourceAvailability,
|
|
vw.TeamName
|
|
From(
|
|
select
|
|
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.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 TrainingHours,
|
|
|
|
0 as VacationHours
|
|
|
|
from FiscalCalendar fc
|
|
join PeopleResource pr on (not(fc.StartDate > pr.EndDate) and not(fc.EndDate < pr.StartDate))
|
|
join ExpenditureCategory ec2 on ec2.Id=pr.ExpenditureCategoryId
|
|
join WorkWeek ww on ww.Id = pr.WorkWeekId
|
|
join Team t on t.Id = pr.TeamId
|
|
where fc.StartDate >= @StartDate and fc.StartDate <= @EndDate and fc.Type = 0 and
|
|
not pr.Id in (
|
|
select sub.Id from
|
|
(select
|
|
pr.Id,
|
|
--fc.EndDate,
|
|
((select UOM.UOMValue * datediff(WEEK, fc.StartDate, fc.EndDate)
|
|
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.WeekEndingDate >= fc.StartDate 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.StartDate and pt.WeekEndingDate <= fc.EndDate), 0) --as TrainingHours,
|
|
-
|
|
0 --as VacationHours
|
|
) as AvailableHours
|
|
|
|
from FiscalCalendar fc
|
|
join PeopleResource pr on (not(fc.StartDate > pr.EndDate) and not(fc.EndDate < pr.StartDate))
|
|
join ExpenditureCategory ec2 on ec2.Id=pr.ExpenditureCategoryId
|
|
join WorkWeek ww on ww.Id = pr.WorkWeekId
|
|
join Team t on t.Id = pr.TeamId
|
|
where fc.StartDate >= @StartDate and fc.StartDate <= @EndDate and fc.Type = 1
|
|
)sub where sub.AvailableHours < @AvailableHours
|
|
)
|
|
) as vw
|
|
group by 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.TrainingHours) as TrainingHours, sum(vw.VacationHours) as VacationHours,
|
|
sum(vw.AvailableHours)-sum(vw.AllocatedHours)-sum(vw.TrainingHours)-sum(vw.VacationHours) as ResourceAvailability,
|
|
vw.TeamName
|
|
From(
|
|
select
|
|
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.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 TrainingHours,
|
|
|
|
0 as VacationHours
|
|
|
|
from FiscalCalendar fc
|
|
join PeopleResource pr on (not(fc.StartDate > pr.EndDate) and not(fc.EndDate < pr.StartDate))
|
|
join ExpenditureCategory ec2 on ec2.Id=pr.ExpenditureCategoryId
|
|
join WorkWeek ww on ww.Id = pr.WorkWeekId
|
|
join Team t on t.Id = pr.TeamId
|
|
where fc.StartDate >= @StartDate and fc.StartDate <= @EndDate and fc.Type = 0 and
|
|
not pr.Id in (
|
|
select sub.Id from
|
|
(select
|
|
pr.Id,
|
|
((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)
|
|
-
|
|
ISNULL((select SUM (pt.HoursOff)
|
|
From NonProjectTimeAllocation pt
|
|
Where pt.PeopleResourceId=pr.Id and pt.WeekEndingDate = fc.EndDate), 0) --as TrainingHours,
|
|
) as AvailableHours
|
|
|
|
from FiscalCalendar fc
|
|
join PeopleResource pr on (not(fc.StartDate > pr.EndDate) and not(fc.EndDate < pr.StartDate))
|
|
join ExpenditureCategory ec2 on ec2.Id=pr.ExpenditureCategoryId
|
|
join WorkWeek ww on ww.Id = pr.WorkWeekId
|
|
join Team t on t.Id = pr.TeamId
|
|
where fc.StartDate >= @StartDate and fc.StartDate <= @EndDate and fc.Type = 0
|
|
)sub where sub.AvailableHours < @AvailableHours
|
|
)
|
|
) as vw
|
|
group by 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.TrainingHours) as TrainingHours, sum(vw.VacationHours) as VacationHours,
|
|
sum(vw.AvailableHours)-sum(vw.AllocatedHours)-sum(vw.TrainingHours)-sum(vw.VacationHours) as ResourceAvailability,
|
|
vw.TeamName
|
|
|
|
From(
|
|
|
|
select
|
|
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.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 TrainingHours,
|
|
|
|
0 as VacationHours
|
|
|
|
from FiscalCalendar fc
|
|
join PeopleResource pr on (not(fc.StartDate > pr.EndDate) and not(fc.EndDate < pr.StartDate))
|
|
join ExpenditureCategory ec2 on ec2.Id=pr.ExpenditureCategoryId
|
|
join WorkWeek ww on ww.Id = pr.WorkWeekId
|
|
join Team t on t.Id = pr.TeamId
|
|
where fc.StartDate >= @StartDate and fc.StartDate <= @EndDate and fc.Type = 0
|
|
) as vw
|
|
group by vw.FirstName, vw.LastName, vw.Name, vw.TeamName, vw.AvailableHours--, vw.EndDate
|
|
having vw.AvailableHours > @AvailableHours
|
|
end
|
|
RETURN
|
|
END
|
|
|
|
GO
|
|
|
|
|