USE [EnVisage]
GO

/****** Object:  View [dbo].[VW_HolidayAllocation]    Script Date: 2/16/2016 11:18:29 AM ******/
if exists (select 1 from sys.views where name like 'VW_HolidayAllocation')
begin
	DROP VIEW [dbo].[VW_HolidayAllocation]
END
GO

/****** Object:  View [dbo].[VW_HolidayAllocation]    Script Date: 2/16/2016 11:18:29 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/* Update Dependent views and procs */
CREATE VIEW [dbo].[VW_HolidayAllocation]
AS
SELECT H.Name as HolidayName, PR.Id as PeopleResourceId, HA.*
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
GO
