USE [EnVisage]
GO

if exists (select 1 from sys.views where name like 'VW_HolidayAllocation')
begin
	DROP VIEW [dbo].[VW_HolidayAllocation]
END
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE VIEW [dbo].[VW_HolidayAllocation]
AS
WITH allocations as (SELECT PR.Id as PeopleResourceId, PR.ExpenditureCategoryId, PR.TeamId, PR.WorkWeekId, HA.WeekEndingDate, 
	Min(cast(HA.Sunday as decimal)) as Sunday, Min(cast(HA.Monday as decimal)) as Monday, Min(cast(HA.Tuesday as decimal)) as Tuesday, Min(cast(HA.Wednesday as decimal)) as Wednesday, 
	Min(cast(HA.Thursday as decimal)) as Thursday, Min(cast(HA.Friday as decimal)) as Friday, Min(cast(HA.Saturday as decimal)) as Saturday
FROM HolidayAllocation AS HA
	inner join Holiday H on HA.HolidayId = H.Id
	left join Holiday2Team H2T on H.CompanyImpact = 0 and H.Id = H2T.HolidayId
	left join Holiday2PeopleResource H2PR on H.CompanyImpact = 0 and H.Id = H2PR.HolidayId
	left join Holiday2ExpenditureCategory H2E on H.CompanyImpact = 0 and H.Id = H2E.HolidayId
	left join PeopleResource PR on 
		(H.CompanyImpact = 1) -- all resources in case Company Impact is set to 'All Resources'
		OR (H.CompanyImpact = 0 AND H.IsInclude = 1 AND (PR.Id = H2PR.ResourceId OR PR.TeamId = H2T.TeamId OR PR.ExpenditureCategoryId = H2E.ExpenditureCategoryId)) -- resources from 'Resources' dropdown and resources of teams from 'Teams' dropdown and resources of expenditures from 'Expenditures' dropdown if company impact is 'Some Resources' and Include mode
		OR (H.CompanyImpact = 0 AND H.IsInclude = 0 AND PR.Id <> H2PR.ResourceId AND PR.TeamId <> H2T.TeamId AND PR.ExpenditureCategoryId <> H2E.ExpenditureCategoryId) -- resources from 'Resources' dropdown and resources of teams from 'Teams' dropdown and resources of expenditures from 'Expenditures' dropdown if company impact is 'Some Resources' and Exclude mode
where H.WorkingDays = 0
group by PR.Id, PR.ExpenditureCategoryId, PR.TeamId, PR.WorkWeekId, HA.WeekEndingDate)
select PeopleResourceId, ExpenditureCategoryId, TeamId, HA.WeekEndingDate,
	ISNULL (case (cast (Wk.Sunday as decimal)+cast (Wk.Monday as decimal)+cast (Wk.Tuesday as decimal)+cast (Wk.Wednesday as decimal)+cast (Wk.Thursday as decimal)+cast (Wk.Friday as decimal)+cast (Wk.Saturday as decimal))
		when 0 then 0 
		else ((cast (Wk.Sunday as decimal) * HA.Sunday)+(cast (Wk.Monday as decimal) * HA.Monday)+(cast(Wk.Tuesday as decimal) * HA.Tuesday)+(cast(Wk.Wednesday as decimal) * HA.Wednesday)+(cast(Wk.Thursday as decimal) * HA.Thursday)+(cast(Wk.Friday as decimal) * HA.Friday)+(cast(Wk.Saturday as decimal) * HA.Saturday)) / (cast(Wk.Sunday as decimal)+cast(Wk.Monday as decimal)+cast(Wk.Tuesday as decimal)+cast(Wk.Wednesday as decimal)+cast(Wk.Thursday as decimal)+cast(Wk.Friday as decimal)+cast(Wk.Saturday as decimal))
	end, 0) as AdjustmentKoeff
from allocations HA
	left join ExpenditureCategory EC on HA.ExpenditureCategoryId = EC.Id
	left join UOM on UOM.Id = EC.UOMId
	left join WorkWeek Wk on Wk.Id = HA.WorkWeekId
GO
