UPDATE pal_Module 
SET isActive = 1, [control] = 'module/calendar.ascx'
WHERE module = 'Calendar'



--create calendar tables
IF  NOT EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[pal_Calendar]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[pal_Calendar](
		[recId] [int] IDENTITY(1,1) NOT NULL,
		[name] [varchar](20) NULL,
		[description] [varchar](50) NULL,
		[interval] [int] NULL,
		[firstDayOfWeek] [varchar](10) NULL,
		[defaultView] [varchar](10) NULL,
		[isDoubleBook] [bit] NULL,
		[isAccessRestricted] [bit] NULL,
		[dateSaved] [datetime] NULL,
		[userIdSaved] [int] NULL,
		[dateUpdated] [datetime] NULL,
		[userIdUpdated] [int] NULL,
		[sequence] [int] NULL,
		[isActive] [bit] NULL,
	 CONSTRAINT [PK_pal_Calendar] PRIMARY KEY CLUSTERED 
	(
		[recId] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

END

IF  NOT EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[pal_CalendarActiveTime]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[pal_CalendarActiveTime](
		[recId] [int] IDENTITY(1,1) NOT NULL,
		[calendarId] [int] NULL,
		[dayOfWeek] [varchar](10) NULL,
		[start] [time](7) NULL,
		[end] [time](7) NULL,
	 CONSTRAINT [PK_pal_CalendarActiveTime] PRIMARY KEY CLUSTERED 
	(
		[recId] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

END


IF  NOT EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[pal_CalendarEvent]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[pal_CalendarEvent](
		[recId] [int] IDENTITY(1,1) NOT NULL,
		[calendarId] [int] NULL,
		[calendarEventTypeId] [int] NULL,
		[subject] [varchar](50) NULL,
		[users] [varchar](500) NULL,
		[start] [datetime] NULL,
		[end] [datetime] NULL,
		[recurrenceRule] [varchar](500) NULL,
		[recurrenceParentId] [int] NULL,
		[isEmailReminder] [bit] NULL,
		[emailReminderMin] [int] NULL,
		[calendarRoomId] [int] NULL,
		[calendarRoomOther] [varchar](500) NULL,
		[dateSaved] [datetime] NULL,
		[userIdSaved] [int] NULL,
		[dateUpdated] [datetime] NULL,
		[userIdUpdated] [int] NULL,
		[customerId] [int] NULL,
	 CONSTRAINT [PK_pal_CalendarEvent] PRIMARY KEY CLUSTERED 
	(
		[recId] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

END


IF  NOT EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[pal_CalendarEventModule]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[pal_CalendarEventModule](
		[recId] [int] IDENTITY(1,1) NOT NULL,
		[calendarId] [int] NULL,
		[moduleId] [int] NULL,
		[entityId] [int] NULL,
	 CONSTRAINT [PK_pal_CalendarEventModule] PRIMARY KEY CLUSTERED 
	(
		[recId] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

END


IF  NOT EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[pal_CalendarEventType]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[pal_CalendarEventType](
		[recId] [int] IDENTITY(1,1) NOT NULL,
		[name] [varchar](100) NULL,
		[colour] [varchar](50) NULL,
		[icon] [varchar](500) NULL,
		[sequence] [int] NULL,
		[isActive] [bit] NULL,
	 CONSTRAINT [PK_pal_CalendarEventType] PRIMARY KEY CLUSTERED 
	(
		[recId] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

END


IF  NOT EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[pal_CalendarModule]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[pal_CalendarModule](
		[recId] [int] IDENTITY(1,1) NOT NULL,
		[calendarId] [int] NULL,
		[moduleId] [int] NULL,
		[minItems] [int] NULL,
		[maxItems] [int] NULL,
		[sequence] [int] NULL,
		[isActive] [bit] NULL,
	 CONSTRAINT [PK_pal_CalendarModule] PRIMARY KEY CLUSTERED 
	(
		[recId] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

END


IF  NOT EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[pal_CalendarRoom]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[pal_CalendarRoom](
		[recId] [int] IDENTITY(1,1) NOT NULL,
		[calendarId] [int] NULL,
		[calendarEventTypes] [varchar](500) NULL,
		[name] [varchar](500) NULL,
		[sequence] [int] NULL,
		[isActive] [bit] NULL,
	 CONSTRAINT [PK_pal_CalendarRoom] PRIMARY KEY CLUSTERED 
	(
		[recId] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

END


IF  NOT EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[pal_CalendarUser]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[pal_CalendarUser](
		[recId] [int] IDENTITY(1,1) NOT NULL,
		[calendarId] [int] NULL,
		[userId] [int] NULL,
		[isViewOnly] [bit] NULL,
	 CONSTRAINT [PK_pal_CalendarUser] PRIMARY KEY CLUSTERED 
	(
		[recId] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

END



--insert event types
TRUNCATE Table pal_CalendarEventType
SET IDENTITY_INSERT [dbo].[pal_CalendarEventType] ON 
INSERT [dbo].[pal_CalendarEventType] ([recId], [name], [colour], [icon], [sequence], [isActive]) VALUES (1, N'Event', N'#74C957', N'', 1, 1)
INSERT [dbo].[pal_CalendarEventType] ([recId], [name], [colour], [icon], [sequence], [isActive]) VALUES (2, N'Leave', N'#7D8FB3', N'', 2, 1)
INSERT [dbo].[pal_CalendarEventType] ([recId], [name], [colour], [icon], [sequence], [isActive]) VALUES (3, N'Sick Leave', N'#CC3333', N'', 3, 1)
INSERT [dbo].[pal_CalendarEventType] ([recId], [name], [colour], [icon], [sequence], [isActive]) VALUES (4, N'Public Holiday', N'#B15BC7', N'', 4, 1)
INSERT [dbo].[pal_CalendarEventType] ([recId], [name], [colour], [icon], [sequence], [isActive]) VALUES (5, N'Birthday', N'#DD4477', N'', 5, 1)
INSERT [dbo].[pal_CalendarEventType] ([recId], [name], [colour], [icon], [sequence], [isActive]) VALUES (6, N'Anniversary', N'#D6AE00', N'', 6, 1)
INSERT [dbo].[pal_CalendarEventType] ([recId], [name], [colour], [icon], [sequence], [isActive]) VALUES (7, N'Travel', N'#B08B59', N'', 7, 1)
SET IDENTITY_INSERT [dbo].[pal_CalendarEventType] OFF