IF (NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
            WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'pal_SalesStatus'))
BEGIN
	CREATE TABLE [dbo].[pal_SalesStatus] (
		[recId]  INT IDENTITY (1, 1) NOT NULL,
		[status] VARCHAR (50) NULL,
		CONSTRAINT [PK_pal_SalesStatus] PRIMARY KEY CLUSTERED ([recId] ASC)
	);

	SET IDENTITY_INSERT [dbo].[pal_SalesStatus] ON 
	INSERT [dbo].[pal_SalesStatus] ([recId], [status]) VALUES (1, N'Draft')
	INSERT [dbo].[pal_SalesStatus] ([recId], [status]) VALUES (2, N'Awaiting Acceptance')
	INSERT [dbo].[pal_SalesStatus] ([recId], [status]) VALUES (3, N'Accepted')
	INSERT [dbo].[pal_SalesStatus] ([recId], [status]) VALUES (4, N'Declined')
	INSERT [dbo].[pal_SalesStatus] ([recId], [status]) VALUES (5, N'Unpaid')
	INSERT [dbo].[pal_SalesStatus] ([recId], [status]) VALUES (6, N'Partially Paid')
	INSERT [dbo].[pal_SalesStatus] ([recId], [status]) VALUES (7, N'Paid')
	INSERT [dbo].[pal_SalesStatus] ([recId], [status]) VALUES (8, N'Overdue')
	INSERT [dbo].[pal_SalesStatus] ([recId], [status]) VALUES (9, N'Unallocated')
	INSERT [dbo].[pal_SalesStatus] ([recId], [status]) VALUES (10, N'Allocated')
	SET IDENTITY_INSERT [dbo].[pal_SalesStatus] OFF

END

IF NOT EXISTS(
    SELECT * FROM sys.columns 
    WHERE Object_ID = Object_ID(N'pal_Sales') AND Name = N'isDraft')
BEGIN
    ALTER TABLE pal_Sales ADD isDraft BIT
END

IF NOT EXISTS(
    SELECT * FROM sys.columns 
    WHERE Object_ID = Object_ID(N'pal_Sales') AND Name = N'emailSent')
BEGIN
    ALTER TABLE pal_Sales ADD emailSent BIT
END

IF NOT EXISTS(
    SELECT * FROM sys.columns 
    WHERE Object_ID = Object_ID(N'pal_Sales') AND Name = N'statusId')
BEGIN
    ALTER TABLE pal_Sales ADD statusId INT
END

IF NOT EXISTS(
    SELECT * FROM sys.columns 
    WHERE Object_ID = Object_ID(N'pal_Sales') AND Name = N'updateDate')
BEGIN
    ALTER TABLE pal_Sales ADD updateDate DATETIME
END

IF NOT EXISTS(
    SELECT * FROM sys.columns 
    WHERE Object_ID = Object_ID(N'pal_Sales') AND Name = N'updateUserId')
BEGIN
    ALTER TABLE pal_Sales ADD updateUserId BIT
END


IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'[dbo].[sp_GetSalesData]') 
                   and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
    DROP PROCEDURE [dbo].[sp_GetSalesData]
END