if not exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}[{objectQualifier}tblNaDevEventsEarlyBirdDiscount]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
 begin

	CREATE TABLE {databaseOwner}[{objectQualifier}tblNaDevEventsEarlyBirdDiscount](
		[Id] [int] IDENTITY(1,1) NOT NULL,
		[EventId] [int] NOT NULL,
		[StartWindow] [int] not null,
		[EndWindow] [int] not null,
		[Discount] [money] not null
	 CONSTRAINT [PK_{objectQualifier}tblNaDevEventsEarlyBirdDiscount] PRIMARY KEY CLUSTERED 
	(
		[Id] ASC
	)
	)
 end
GO

if not exists (SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME ='FK_{objectQualifier}tblNaDevEventsEarlyBirdDiscount_tblNaDevEvents')

ALTER TABLE {databaseOwner}[{objectQualifier}tblNaDevEventsEarlyBirdDiscount]  WITH CHECK ADD  CONSTRAINT [FK_{objectQualifier}tblNaDevEventsEarlyBirdDiscount_tblNaDevEvents] FOREIGN KEY([EventId])
REFERENCES {databaseOwner}[{objectQualifier}tblNaDevEvents] ([eventId])
ON DELETE CASCADE

GO

if not exists (select column_name from INFORMATION_SCHEMA.columns where table_name = '{objectQualifier}tblNaDevEventPriceTier' and column_name = 'ExpiresNumberOfDaysBeforeEventsStarts')
 begin
   alter table {databaseOwner}[{objectQualifier}tblNaDevEventPriceTier] add ExpiresNumberOfDaysBeforeEventsStarts int null
 end
GO

alter procedure {databaseOwner}{objectQualifier}usp_nadev_Event_Price_Tier_Save
(
 @priceId int,
 @eventId int,
 @TierName nvarchar(255),
 @costPerPlace money,
 @dnnRoleId int,
 @sortOrder int,
 @additionalAttendeeCost money,
 @QtyRequired int,
 @ReduceAll bit,
 @ExpiresNumberOfDaysBeforeEventsStarts int
)

as

set nocount on
  
 if exists(select null from {databaseOwner}[{objectQualifier}tblNaDevEventPriceTier] where priceId = @priceId)
	begin
		update {databaseOwner}[{objectQualifier}tblNaDevEventPriceTier] set eventId = @eventId,
																			TierName = @TierName,
																			costPerPlace = @costPerPlace,
																			dnnRoleId = @dnnRoleId,
																			sortOrder = @sortOrder,
																			additionalAttendeeCost = @additionalAttendeeCost,
																			QtyRequired = @QtyRequired,
																			ReduceAll = @ReduceAll,
																			ExpiresNumberOfDaysBeforeEventsStarts = @ExpiresNumberOfDaysBeforeEventsStarts
																																					  										 
																			where priceId = @priceId
																			return @@identity
			end 
else
	insert into {databaseOwner}[{objectQualifier}tblNaDevEventPriceTier] (eventId,TierName,costPerPlace,dnnRoleId,sortOrder,additionalAttendeeCost,QtyRequired,ReduceAll,ExpiresNumberOfDaysBeforeEventsStarts)

	values (@eventId,@TierName,@costPerPlace,@dnnRoleId,@sortOrder,@additionalAttendeeCost,@QtyRequired,@ReduceAll,@ExpiresNumberOfDaysBeforeEventsStarts)
	
	SELECT SCOPE_IDENTITY()

GO

if not exists (select column_name from INFORMATION_SCHEMA.columns where table_name = '{objectQualifier}tblNaDevEventsUsersSignedUpToEvent' and column_name = 'sentOverDuePaymentRemindEmail')
 begin
   alter table {databaseOwner}[{objectQualifier}tblNaDevEventsUsersSignedUpToEvent] add sentOverDuePaymentRemindEmail bit null default 0
 end
GO

alter procedure {databaseOwner}{objectQualifier}usp_UpdateUserSignUpToEvent
(
 @bookingId int,
 @eventId int,
 @moduleId int,
 @userId int,
 @eventStartDate datetime,
 @eventEndDate datetime,
 @additionalInformation ntext,
 @billingAddressId int,
 @numberOfPlaces int,
 @dateBookingMade datetime,
 @paymentStatusId int,
 @tabModuleId int,
 @userSignUpAnswers ntext,
 @totalCost money,
 @numberOfChildPlaces int,
 @discountAmount money,
 @couponCode nvarchar(50),
 @tax money,
 @sentRemindEmail bit,
 @numberOfAttendeesString nvarchar(max),
 @isWaitList bit,
 @BookingSurcharge money,
 @HeartLandTransactionInfo nvarchar(max),
 @sentOverDuePaymentRemindEmail bit
)

as

set nocount on
  
update {databaseOwner}[{objectQualifier}tblNaDevEventsUsersSignedUpToEvent] set   eventId = @eventId,
																				  eventStartDate = @eventStartDate,
																				  eventEndDate = @eventEndDate,
																				  additionalInformation = @additionalInformation,
																				  billingAddressId = @billingAddressId,
																				  numberOfPlaces = @numberOfPlaces,
																				  userId=@userId,
																				  moduleId =@moduleId,
																				  dateBookingMade = @dateBookingMade,
																				  paymentStatusId = @paymentStatusId,
																				  tabModuleId = @tabModuleId,
																				  userSignUpAnswers = @userSignUpAnswers,
																				  totalCost = @totalCost,
																				  numberOfChildPlaces = @numberOfChildPlaces,
																				  discountAmount =@discountAmount,
																				  couponcode = @couponcode,
																				  tax =@tax,
																				  sentRemindEmail =@sentRemindEmail,
																				  numberOfAttendeesString =@numberOfAttendeesString,
																				  isWaitList = @isWaitList,
																				  BookingSurcharge = @BookingSurcharge,
																				  HeartLandTransactionInfo = @HeartLandTransactionInfo,										 
																			      sentOverDuePaymentRemindEmail = @sentOverDuePaymentRemindEmail
																				  where bookingId = @bookingId												

GO