CREATE PROCEDURE DBO.CopyMonthToPeriod @ProductNumber INT, @Sourceday DATETIME, @DestStartDate DATETIME, @DestEndDate DATETIME
AS
BEGIN
	SET NOCOUNT ON

	/* 20220131/RN: Created */
	DECLARE @DateStamp VARCHAR(8)
	DECLARE @RoomtypeNumber INT
	DECLARE @AdultPrice1 MONEY
	DECLARE @AdultPrice2 MONEY
	DECLARE @AdultPrice3 MONEY
	DECLARE @AdultPrice4 MONEY
	DECLARE @AdultPrice5 MONEY
	DECLARE @AdultPrice6 MONEY
	DECLARE @AdultPrice7 MONEY
	DECLARE @AdultPrice8 MONEY
	DECLARE @AdultPrice9 MONEY
	DECLARE @Children1Price1 MONEY
	DECLARE @Children1Price2 MONEY
	DECLARE @Children1Price3 MONEY
	DECLARE @Children1Price4 MONEY
	DECLARE @Children1Price5 MONEY
	DECLARE @Children1Price6 MONEY
	DECLARE @Children1Price7 MONEY
	DECLARE @Children1Price8 MONEY
	DECLARE @Children1Price9 MONEY
	DECLARE @Children2Price1 MONEY
	DECLARE @Children2Price2 MONEY
	DECLARE @Children2Price3 MONEY
	DECLARE @Children2Price4 MONEY
	DECLARE @Children2Price5 MONEY
	DECLARE @Children2Price6 MONEY
	DECLARE @Children2Price7 MONEY
	DECLARE @Children2Price8 MONEY
	DECLARE @Children2Price9 MONEY
	DECLARE @AdultArrivalAddon SMALLMONEY
	DECLARE @AdultStayAddon SMALLMONEY
	DECLARE @AdultDepartureAddon SMALLMONEY
	DECLARE @Children1ArrivalAddon SMALLMONEY
	DECLARE @Children1StayAddon SMALLMONEY
	DECLARE @Children1DepartureAddon SMALLMONEY
	DECLARE @Children2ArrivalAddon SMALLMONEY
	DECLARE @Children2StayAddon SMALLMONEY
	DECLARE @Children2DepartureAddon SMALLMONEY
	DECLARE @AdultArrivalAddon2 SMALLMONEY
	DECLARE @AdultStayAddon2 SMALLMONEY
	DECLARE @AdultDepartureAddon2 SMALLMONEY
	DECLARE @Children1ArrivalAddon2 SMALLMONEY
	DECLARE @Children1StayAddon2 SMALLMONEY
	DECLARE @Children1DepartureAddon2 SMALLMONEY
	DECLARE @Children2ArrivalAddon2 SMALLMONEY
	DECLARE @Children2StayAddon2 SMALLMONEY
	DECLARE @Children2DepartureAddon2 SMALLMONEY
	DECLARE @AdultArrivalAddon3 SMALLMONEY
	DECLARE @AdultStayAddon3 SMALLMONEY
	DECLARE @AdultDepartureAddon3 SMALLMONEY
	DECLARE @Children1ArrivalAddon3 SMALLMONEY
	DECLARE @Children1StayAddon3 SMALLMONEY
	DECLARE @Children1DepartureAddon3 SMALLMONEY
	DECLARE @Children2ArrivalAddon3 SMALLMONEY
	DECLARE @Children2StayAddon3 SMALLMONEY
	DECLARE @Children2DepartureAddon3 SMALLMONEY
	DECLARE @AdultArrivalAddon4 SMALLMONEY
	DECLARE @AdultStayAddon4 SMALLMONEY
	DECLARE @AdultDepartureAddon4 SMALLMONEY
	DECLARE @Children1ArrivalAddon4 SMALLMONEY
	DECLARE @Children1StayAddon4 SMALLMONEY
	DECLARE @Children1DepartureAddon4 SMALLMONEY
	DECLARE @Children2ArrivalAddon4 SMALLMONEY
	DECLARE @Children2StayAddon4 SMALLMONEY
	DECLARE @Children2DepartureAddon4 SMALLMONEY
	DECLARE @AdultArrivalAddon5 SMALLMONEY
	DECLARE @AdultStayAddon5 SMALLMONEY
	DECLARE @AdultDepartureAddon5 SMALLMONEY
	DECLARE @Children1ArrivalAddon5 SMALLMONEY
	DECLARE @Children1StayAddon5 SMALLMONEY
	DECLARE @Children1DepartureAddon5 SMALLMONEY
	DECLARE @Children2ArrivalAddon5 SMALLMONEY
	DECLARE @Children2StayAddon5 SMALLMONEY
	DECLARE @Children2DepartureAddon5 SMALLMONEY
	DECLARE @AdultArrivalAddon6 SMALLMONEY
	DECLARE @AdultStayAddon6 SMALLMONEY
	DECLARE @AdultDepartureAddon6 SMALLMONEY
	DECLARE @Children1ArrivalAddon6 SMALLMONEY
	DECLARE @Children1StayAddon6 SMALLMONEY
	DECLARE @Children1DepartureAddon6 SMALLMONEY
	DECLARE @Children2ArrivalAddon6 SMALLMONEY
	DECLARE @Children2StayAddon6 SMALLMONEY
	DECLARE @Children2DepartureAddon6 SMALLMONEY
	DECLARE @AdultArrivalAddon7 SMALLMONEY
	DECLARE @AdultStayAddon7 SMALLMONEY
	DECLARE @AdultDepartureAddon7 SMALLMONEY
	DECLARE @Children1ArrivalAddon7 SMALLMONEY
	DECLARE @Children1StayAddon7 SMALLMONEY
	DECLARE @Children1DepartureAddon7 SMALLMONEY
	DECLARE @Children2ArrivalAddon7 SMALLMONEY
	DECLARE @Children2StayAddon7 SMALLMONEY
	DECLARE @Children2DepartureAddon7 SMALLMONEY
	DECLARE @AdultArrivalAddon8 SMALLMONEY
	DECLARE @AdultStayAddon8 SMALLMONEY
	DECLARE @AdultDepartureAddon8 SMALLMONEY
	DECLARE @Children1ArrivalAddon8 SMALLMONEY
	DECLARE @Children1StayAddon8 SMALLMONEY
	DECLARE @Children1DepartureAddon8 SMALLMONEY
	DECLARE @Children2ArrivalAddon8 SMALLMONEY
	DECLARE @Children2StayAddon8 SMALLMONEY
	DECLARE @Children2DepartureAddon8 SMALLMONEY
	DECLARE @AdultArrivalAddon9 SMALLMONEY
	DECLARE @AdultStayAddon9 SMALLMONEY
	DECLARE @AdultDepartureAddon9 SMALLMONEY
	DECLARE @Children1ArrivalAddon9 SMALLMONEY
	DECLARE @Children1StayAddon9 SMALLMONEY
	DECLARE @Children1DepartureAddon9 SMALLMONEY
	DECLARE @Children2ArrivalAddon9 SMALLMONEY
	DECLARE @Children2StayAddon9 SMALLMONEY
	DECLARE @Children2DepartureAddon9 SMALLMONEY
	DECLARE @ArrivalPacketNumber INT
	DECLARE @StayPacketNumber INT
	DECLARE @DeparturePacketNumber INT
	DECLARE @PackageType INT
	DECLARE @PacketNumber INT

	DECLARE @FormatedSourceFirstDay CHAR(10)
	DECLARE @FormatedSourceLastDay CHAR(10)
	DECLARE @CurrentDate CHAR(10)
	DECLARE @EndDate CHAR(10)
	DECLARE @InsertDate CHAR(8)
	DECLARE @SourceFirstday DATETIME
	DECLARE @FirstSourceDateDT DATETIME
	DECLARE @LastSourceDateDT DATETIME
	DECLARE @NextSourceDateDT DATETIME
	DECLARE @CurrentDateDT DATETIME
	DECLARE @EndDateDT DATETIME

	DECLARE @IsError BIT
	DECLARE @ErrorCount INT
	DECLARE @DestMonth INT
	DECLARE @DestYear INT

	DECLARE @FK_ProductNumberDiscounted INT

	SELECT @IsError = 0

	SELECT @SourceFirstday = DATEADD(DAY, 1, EOMONTH(@Sourceday, - 1));

	--PRINT '@SourceFirstDay: ' + CONVERT(VARCHAR(8), @SourceFirstDay, 112)

	SELECT @FormatedSourceFirstDay = CONVERT(CHAR(10), @Sourceday, 112)

	SELECT @FormatedSourceLastDay = CONVERT(CHAR(10), EOMONTH(@Sourceday), 112)

	SELECT @CurrentDate = CONVERT(VARCHAR(8), DATEADD(DAY, 1, EOMONTH(@DestStartDate, - 1)), 112)

	SELECT @EndDate = CONVERT(VARCHAR(8), EOMONTH(@DestEndDate), 112)

	SELECT @FirstSourceDateDT = @Sourceday

	SELECT @LastSourceDateDT = EOMONTH(@Sourceday)

	/*
	PRINT '@FormatedSourceFirstDay: ' + CONVERT(VARCHAR(8), @FormatedSourceFirstDay, 112)
	PRINT '@FormatedSourceLastDay: ' + CONVERT(VARCHAR(8), @FormatedSourceLastDay, 112)
	PRINT '@CurrentDate: ' + CONVERT(VARCHAR(8), @CurrentDate, 112)
	PRINT '@EndDate: ' + CONVERT(VARCHAR(8), @EndDate, 112)
	PRINT '@FirstSourceDT: ' + CONVERT(VARCHAR(8), @firstSourceDateDT, 112)
	PRINT '@LastSourceDT: ' + CONVERT(VARCHAR(8), @LastSourceDateDT, 112)
	*/
	DELETE
	FROM ProductPrice
	WHERE (1 = 1)
		AND ProductNumber = @ProductNumber
		AND DateStamp >= CONVERT(CHAR(8), @DestStartDate, 112)
		AND DateStamp <= CONVERT(CHAR(8), @DestEndDate, 112)

	CREATE TABLE #PackageInfo (View_DateStamp DATETIME, View_Id INT, View_FK_ProductNumber INT, View_PacketType INT)

	INSERT INTO #PackageInfo
	EXEC ProductPricePacketInfo @FormatedSourceFirstDay, @FormatedSourceLastday, @ProductNumber

	SELECT ProductNumber, DateStamp, RoomtypeNumber, AdultPrice1, AdultPrice2, AdultPrice3, AdultPrice4, AdultPrice5, AdultPrice6, AdultPrice7, AdultPrice8, AdultPrice9, Children1Price1, Children1Price2, Children1Price3, Children1Price4, Children1Price5, Children1Price6, Children1Price7, Children1Price8, Children1Price9, Children2Price1, Children2Price2, Children2Price3, Children2Price4, Children2Price5, Children2Price6, Children2Price7, Children2Price8, Children2Price9, AdultArrivalAddon, AdultStayAddon, AdultDepartureAddon, Children1ArrivalAddon, Children1StayAddon, Children1DepartureAddon, Children2ArrivalAddon, Children2StayAddon, Children2DepartureAddon, AdultArrivalAddon2, AdultStayAddon2, AdultDepartureAddon2, Children1ArrivalAddon2, Children1StayAddon2, Children1DepartureAddon2, Children2ArrivalAddon2, Children2StayAddon2, Children2DepartureAddon2, AdultArrivalAddon3, AdultStayAddon3, AdultDepartureAddon3, Children1ArrivalAddon3, Children1StayAddon3, Children1DepartureAddon3, Children2ArrivalAddon3, Children2StayAddon3, 
		Children2DepartureAddon3, AdultArrivalAddon4, AdultStayAddon4, AdultDepartureAddon4, Children1ArrivalAddon4, Children1StayAddon4, Children1DepartureAddon4, Children2ArrivalAddon4, Children2StayAddon4, Children2DepartureAddon4, AdultArrivalAddon5, AdultStayAddon5, AdultDepartureAddon5, Children1ArrivalAddon5, Children1StayAddon5, Children1DepartureAddon5, Children2ArrivalAddon5, Children2StayAddon5, Children2DepartureAddon5, AdultArrivalAddon6, AdultStayAddon6, AdultDepartureAddon6, Children1ArrivalAddon6, Children1StayAddon6, Children1DepartureAddon6, Children2ArrivalAddon6, Children2StayAddon6, Children2DepartureAddon6, AdultArrivalAddon7, AdultStayAddon7, AdultDepartureAddon7, Children1ArrivalAddon7, Children1StayAddon7, Children1DepartureAddon7, Children2ArrivalAddon7, Children2StayAddon7, Children2DepartureAddon7, AdultArrivalAddon8, AdultStayAddon8, AdultDepartureAddon8, Children1ArrivalAddon8, Children1StayAddon8, Children1DepartureAddon8, Children2ArrivalAddon8, Children2StayAddon8, Children2DepartureAddon8, 
		AdultArrivalAddon9, AdultStayAddon9, AdultDepartureAddon9, Children1ArrivalAddon9, Children1StayAddon9, Children1DepartureAddon9, Children2ArrivalAddon9, Children2StayAddon9, Children2DepartureAddon9, (
			SELECT TOP 1 ProductPricePacket.FK_PacketNumber
			FROM #PackageInfo WITH (NOLOCK)
			INNER JOIN ProductPricePacket WITH (NOLOCK) ON ProductPricePacket.Id = #PackageInfo.View_Id
			INNER JOIN Packet WITH (NOLOCK) ON Packet.PacketNumber = ProductPricePacket.FK_PacketNumber
			WHERE FK_ProductNumber = ProductPrice.ProductNumber
				AND #PackageInfo.View_DateStamp = ProductPrice.DateStamp
				AND ISNULL(ProductPricePacket.FK_PacketNumber, 0) > 0
				AND PacketType = 0
			ORDER BY Id DESC
			) AS ArrivalPacketNumber, (
			SELECT TOP 1 ProductPricePacket.FK_PacketNumber
			FROM #PackageInfo WITH (NOLOCK)
			INNER JOIN ProductPricePacket WITH (NOLOCK) ON ProductPricePacket.Id = #PackageInfo.View_Id
			INNER JOIN Packet WITH (NOLOCK) ON Packet.PacketNumber = ProductPricePacket.FK_PacketNumber
			WHERE FK_ProductNumber = ProductPrice.ProductNumber
				AND #PackageInfo.View_DateStamp = ProductPrice.DateStamp
				AND ISNULL(ProductPricePacket.FK_PacketNumber, 0) > 0
				AND PacketType = 1
			ORDER BY Id DESC
			) AS StayPacketNumber, (
			SELECT TOP 1 ProductPricePacket.FK_PacketNumber
			FROM #PackageInfo WITH (NOLOCK)
			INNER JOIN ProductPricePacket WITH (NOLOCK) ON ProductPricePacket.Id = #PackageInfo.View_Id
			INNER JOIN Packet WITH (NOLOCK) ON Packet.PacketNumber = ProductPricePacket.FK_PacketNumber
			WHERE FK_ProductNumber = ProductPrice.ProductNumber
				AND #PackageInfo.View_DateStamp = ProductPrice.DateStamp
				AND ISNULL(ProductPricePacket.FK_PacketNumber, 0) > 0
				AND PacketType = 2
			ORDER BY Id DESC
			) AS DeparturePacketNumber
	INTO #TempCopyMonthProductPrices
	FROM ProductPrice WITH (NOLOCK)
	WHERE DateStamp >= @FormatedSourceFirstday
		AND DateStamp <= @FormatedSourceLastday
		AND ProductNumber = @ProductNumber

	--SELECT * FROM #TempCopyMonthProductPrices

	SELECT @NextSourceDateDT = @FirstSourceDateDT
	SELECT @EndDateDT = @DestEndDate


	WHILE @NextSourceDateDT <= @LastSourceDateDT
	BEGIN
		SELECT @CurrentDateDT = DATEADD(DAY, 1, EOMONTH(@DestStartDate, - 1))
  	    SELECT @CurrentDateDT = DATEADD(MONTH, -1, @CurrentDateDT)
		SELECT @DestMonth = MONTH(@CurrentDateDT)
		SELECT @DestYear = YEAR(@CurrentDateDT)

		--PRINT '@CurrentDateDT starts with: ' + CONVERT(VARCHAR(8), @CurrentDateDT, 112)
		--PRINT '@NextSourceDateDT: ' + CONVERT(VARCHAR(8), @NextSourceDateDT,112) + ' @AdultPrice1: ' + CONVERT(VARCHAR(255), @AdultPrice1)

		WHILE (@CurrentDateDT <= @EndDateDT)
		BEGIN
			--PRINT 'In while loop 2...'
			IF @DestMonth = 12
			BEGIN
				SELECT @DestYear = @DestYear + 1
				SELECT @DestMonth = 1
			END
			ELSE
				SELECT @DestMonth = @DestMonth + 1
			--PRINT 'Day: ' + CONVERT(VARCHAR(2), DAY(@NextSourceDateDT)) + ' month: ' + CONVERT(VARCHAR(2), @DestMonth) + ' year: ' + CONVERT(VARCHAR(4), @DestYear)
			IF ((DAY(@NextSourceDateDT) = 31) AND (@DestMonth IN (4,6,9,11)))
			BEGIN
				--PRINT 'Day is 31 and month in (4,6,9,11), continuing...'
				CONTINUE
			END
			ELSE IF ((@DestMonth = 2) AND (DAY(@NextSourceDateDT) IN (29,30,31)))
			BEGIN
				--PRINT 'Month is 2 and day is > 28, continuing...'
				CONTINUE
			END
            ELSE
			BEGIN
			  SELECT @CurrentDateDT = DATEFROMPARTS(@DestYear, @DestMonth, DAY(@NextSourceDateDT))
			  --SELECT @CurrentDateDT = DATEADD(MONTH, 1, @CurrentDateDT)
			  --PRINT '@CurrentDateDT: ' + CONVERT(VARCHAR(8), @CurrentDateDT, 112)

			  IF @CurrentDateDT <= @EndDateDT
			  BEGIN
  				SELECT @InsertDate = CONVERT(CHAR(8), DATEFROMPARTS(YEAR(@CurrentDateDT), MONTH(@CurrentDateDT), DAY(@CurrentDateDT)), 112)
				--PRINT 'Inserting values for @InsertDate: ' + @InsertDate + ' Roomtype: ' + CONVERT(VARCHAR(10), @Roomtypenumber) + '@CurrentDateDT: ' + CONVERT(VARCHAR(8), @CurrentDateDT, 112)

			    DECLARE TmpCopyMonthCur CURSOR FAST_FORWARD READ_ONLY FOR
				SELECT ProductNumber, DateStamp, RoomtypeNumber, AdultPrice1, AdultPrice2, AdultPrice3, AdultPrice4, AdultPrice5, AdultPrice6, AdultPrice7, AdultPrice8, AdultPrice9, 
				    Children1Price1, Children1Price2, Children1Price3, Children1Price4, Children1Price5, Children1Price6, Children1Price7, Children1Price8, Children1Price9, 
					Children2Price1, Children2Price2, Children2Price3, Children2Price4, Children2Price5, Children2Price6, Children2Price7, Children2Price8, Children2Price9, 
					AdultArrivalAddon, AdultStayAddon, AdultDepartureAddon, 
					Children1ArrivalAddon, Children1StayAddon, Children1DepartureAddon, Children2ArrivalAddon, Children2StayAddon, Children2DepartureAddon, 
					AdultArrivalAddon2, AdultStayAddon2, AdultDepartureAddon2, Children1ArrivalAddon2, Children1StayAddon2, Children1DepartureAddon2, Children2ArrivalAddon2, Children2StayAddon2, Children2DepartureAddon2, 
					AdultArrivalAddon3, AdultStayAddon3, AdultDepartureAddon3, Children1ArrivalAddon3, Children1StayAddon3, Children1DepartureAddon3, Children2ArrivalAddon3, Children2StayAddon3, Children2DepartureAddon3, 
					AdultArrivalAddon4, AdultStayAddon4, AdultDepartureAddon4, Children1ArrivalAddon4, Children1StayAddon4, Children1DepartureAddon4, Children2ArrivalAddon4, Children2StayAddon4, Children2DepartureAddon4, 
					AdultArrivalAddon5, AdultStayAddon5, AdultDepartureAddon5, Children1ArrivalAddon5, Children1StayAddon5, Children1DepartureAddon5, Children2ArrivalAddon5, Children2StayAddon5, Children2DepartureAddon5, 
					AdultArrivalAddon6, AdultStayAddon6, AdultDepartureAddon6, Children1ArrivalAddon6, Children1StayAddon6, Children1DepartureAddon6, Children2ArrivalAddon6, Children2StayAddon6, Children2DepartureAddon6, 
					AdultArrivalAddon7, AdultStayAddon7, AdultDepartureAddon7, Children1ArrivalAddon7, Children1StayAddon7, Children1DepartureAddon7, Children2ArrivalAddon7, Children2StayAddon7, Children2DepartureAddon7, 
					AdultArrivalAddon8, AdultStayAddon8, AdultDepartureAddon8, Children1ArrivalAddon8, Children1StayAddon8, Children1DepartureAddon8, Children2ArrivalAddon8, Children2StayAddon8, Children2DepartureAddon8, 
					AdultArrivalAddon9, AdultStayAddon9, AdultDepartureAddon9, Children1ArrivalAddon9, Children1StayAddon9, Children1DepartureAddon9, Children2ArrivalAddon9, Children2StayAddon9, Children2DepartureAddon9, 
					ArrivalPacketNumber, StayPacketNumber, DeparturePacketNumber
				FROM #TempCopyMonthProductPrices
				WHERE DateStamp = @NextSourceDateDT
				ORDER BY #TempCopyMonthProductPrices.RoomtypeNumber
				OPEN TmpCopyMonthCur
				FETCH NEXT FROM TmpCopyMonthCur INTO @ProductNumber, @DateStamp, @RoomtypeNumber , 
					@AdultPrice1 , @AdultPrice2, @AdultPrice3, @AdultPrice4, @AdultPrice5, @AdultPrice6, @AdultPrice7, @AdultPrice8, @AdultPrice9, 
					@Children1Price1, @Children1Price2, @Children1Price3, @Children1Price4, @Children1Price5, @Children1Price6, @Children1Price7, @Children1Price8, @Children1Price9, 
					@Children2Price1, @Children2Price2, @Children2Price3, @Children2Price4, @Children2Price5, @Children2Price6, @Children2Price7, @Children2Price8, @Children2Price9, 
					@AdultArrivalAddon, @AdultStayAddon, @AdultDepartureAddon, @Children1ArrivalAddon, @Children1StayAddon, @Children1DepartureAddon, 
					@Children2ArrivalAddon, @Children2StayAddon, @Children2DepartureAddon, 
					@AdultArrivalAddon2, @AdultStayAddon2, @AdultDepartureAddon2, @Children1ArrivalAddon2, @Children1StayAddon2, @Children1DepartureAddon2, @Children2ArrivalAddon2, @Children2StayAddon2, @Children2DepartureAddon2, 
					@AdultArrivalAddon3, @AdultStayAddon3, @AdultDepartureAddon3, @Children1ArrivalAddon3, @Children1StayAddon3, @Children1DepartureAddon3, @Children2ArrivalAddon3, 
					@Children2StayAddon3, @Children2DepartureAddon3, @AdultArrivalAddon4, @AdultStayAddon4, @AdultDepartureAddon4, @Children1ArrivalAddon4, @Children1StayAddon4, @Children1DepartureAddon4, @Children2ArrivalAddon4, @Children2StayAddon4, @Children2DepartureAddon4, 
					@AdultArrivalAddon5, @AdultStayAddon5, @AdultDepartureAddon5, @Children1ArrivalAddon5, @Children1StayAddon5, @Children1DepartureAddon5, @Children2ArrivalAddon5, @Children2StayAddon5, @Children2DepartureAddon5, 
					@AdultArrivalAddon6, @AdultStayAddon6, @AdultDepartureAddon6, @Children1ArrivalAddon6, @Children1StayAddon6, @Children1DepartureAddon6, @Children2ArrivalAddon6, @Children2StayAddon6, @Children2DepartureAddon6, 
					@AdultArrivalAddon7, @AdultStayAddon7, @AdultDepartureAddon7, @Children1ArrivalAddon7, @Children1StayAddon7, @Children1DepartureAddon7, @Children2ArrivalAddon7, @Children2StayAddon7, @Children2DepartureAddon7, 
					@AdultArrivalAddon8, @AdultStayAddon8, @AdultDepartureAddon8, @Children1ArrivalAddon8, @Children1StayAddon8, @Children1DepartureAddon8, @Children2ArrivalAddon8, @Children2StayAddon8, @Children2DepartureAddon8, 
					@AdultArrivalAddon9, @AdultStayAddon9, @AdultDepartureAddon9, @Children1ArrivalAddon9, @Children1StayAddon9, @Children1DepartureAddon9, @Children2ArrivalAddon9, @Children2StayAddon9, @Children2DepartureAddon9, 
					@ArrivalPacketNumber, @StayPacketNumber, @DeparturePacketNumber

				WHILE (@@FETCH_STATUS <> -1)
				BEGIN
					--PRINT 'Inserting values for roomtype: ' + CONVERT(VARCHAR(255), @RoomTypeNumber)
					INSERT INTO ProductPrice (
						ProductNumber, DateStamp, RoomtypeNumber, AdultPrice1, AdultPrice2, AdultPrice3, AdultPrice4, AdultPrice5, AdultPrice6, AdultPrice7, AdultPrice8, AdultPrice9, Children1Price1, Children1Price2, Children1Price3, Children1Price4, Children1Price5, Children1Price6, Children1Price7, Children1Price8, Children1Price9, Children2Price1, Children2Price2, Children2Price3, Children2Price4, Children2Price5, Children2Price6, Children2Price7, Children2Price8, Children2Price9, AdultArrivalAddon, AdultStayAddon, AdultDepartureAddon, Children1ArrivalAddon, Children1StayAddon, Children1DepartureAddon, Children2ArrivalAddon, Children2StayAddon, Children2DepartureAddon, AdultArrivalAddon2, AdultStayAddon2, AdultDepartureAddon2, Children1ArrivalAddon2, Children1StayAddon2, Children1DepartureAddon2, Children2ArrivalAddon2, Children2StayAddon2, Children2DepartureAddon2, AdultArrivalAddon3, AdultStayAddon3, AdultDepartureAddon3, Children1ArrivalAddon3, Children1StayAddon3, Children1DepartureAddon3, Children2ArrivalAddon3, Children2StayAddon3, 
						Children2DepartureAddon3, AdultArrivalAddon4, AdultStayAddon4, AdultDepartureAddon4, Children1ArrivalAddon4, Children1StayAddon4, Children1DepartureAddon4, Children2ArrivalAddon4, Children2StayAddon4, Children2DepartureAddon4, AdultArrivalAddon5, AdultStayAddon5, AdultDepartureAddon5, Children1ArrivalAddon5, Children1StayAddon5, Children1DepartureAddon5, Children2ArrivalAddon5, Children2StayAddon5, Children2DepartureAddon5, AdultArrivalAddon6, AdultStayAddon6, AdultDepartureAddon6, Children1ArrivalAddon6, Children1StayAddon6, Children1DepartureAddon6, Children2ArrivalAddon6, Children2StayAddon6, Children2DepartureAddon6, AdultArrivalAddon7, AdultStayAddon7, AdultDepartureAddon7, Children1ArrivalAddon7, Children1StayAddon7, Children1DepartureAddon7, Children2ArrivalAddon7, Children2StayAddon7, Children2DepartureAddon7, AdultArrivalAddon8, AdultStayAddon8, AdultDepartureAddon8, Children1ArrivalAddon8, Children1StayAddon8, Children1DepartureAddon8, Children2ArrivalAddon8, Children2StayAddon8, Children2DepartureAddon8, 
						AdultArrivalAddon9, AdultStayAddon9, AdultDepartureAddon9, Children1ArrivalAddon9, Children1StayAddon9, Children1DepartureAddon9, Children2ArrivalAddon9, Children2StayAddon9, Children2DepartureAddon9
						)
					VALUES (
						@ProductNumber, @InsertDate, @RoomtypeNumber, @AdultPrice1, @AdultPrice2, @AdultPrice3, @AdultPrice4, @AdultPrice5, @AdultPrice6, @AdultPrice7, @AdultPrice8, @AdultPrice9, @Children1Price1, @Children1Price2, @Children1Price3, @Children1Price4, @Children1Price5, @Children1Price6, @Children1Price7, @Children1Price8, @Children1Price9, @Children2Price1, @Children2Price2, @Children2Price3, @Children2Price4, @Children2Price5, @Children2Price6, @Children2Price7, @Children2Price8, @Children2Price9, @AdultArrivalAddon, @AdultStayAddon, @AdultDepartureAddon, @Children1ArrivalAddon, @Children1StayAddon, @Children1DepartureAddon, @Children2ArrivalAddon, @Children2StayAddon, @Children2DepartureAddon, @AdultArrivalAddon2, @AdultStayAddon2, @AdultDepartureAddon2, @Children1ArrivalAddon2, @Children1StayAddon2, @Children1DepartureAddon2, @Children2ArrivalAddon2, @Children2StayAddon2, @Children2DepartureAddon2, @AdultArrivalAddon3, @AdultStayAddon3, @AdultDepartureAddon3, @Children1ArrivalAddon3, @Children1StayAddon3, @Children1DepartureAddon3, 
						@Children2ArrivalAddon3, @Children2StayAddon3, @Children2DepartureAddon3, @AdultArrivalAddon4, @AdultStayAddon4, @AdultDepartureAddon4, @Children1ArrivalAddon4, @Children1StayAddon4, @Children1DepartureAddon4, @Children2ArrivalAddon4, @Children2StayAddon4, @Children2DepartureAddon4, @AdultArrivalAddon5, @AdultStayAddon5, @AdultDepartureAddon5, @Children1ArrivalAddon5, @Children1StayAddon5, @Children1DepartureAddon5, @Children2ArrivalAddon5, @Children2StayAddon5, @Children2DepartureAddon5, @AdultArrivalAddon6, @AdultStayAddon6, @AdultDepartureAddon6, @Children1ArrivalAddon6, @Children1StayAddon6, @Children1DepartureAddon6, @Children2ArrivalAddon6, @Children2StayAddon6, @Children2DepartureAddon6, @AdultArrivalAddon7, @AdultStayAddon7, @AdultDepartureAddon7, @Children1ArrivalAddon7, @Children1StayAddon7, @Children1DepartureAddon7, @Children2ArrivalAddon7, @Children2StayAddon7, @Children2DepartureAddon7, @AdultArrivalAddon8, @AdultStayAddon8, @AdultDepartureAddon8, @Children1ArrivalAddon8, @Children1StayAddon8, 
						@Children1DepartureAddon8, @Children2ArrivalAddon8, @Children2StayAddon8, @Children2DepartureAddon8, @AdultArrivalAddon9, @AdultStayAddon9, @AdultDepartureAddon9, @Children1ArrivalAddon9, @Children1StayAddon9, @Children1DepartureAddon9, @Children2ArrivalAddon9, @Children2StayAddon9, @Children2DepartureAddon9
						)

					INSERT INTO ProductPricePacket (FK_ProductNumber, PacketType, StartDate, EndDate, FK_PacketNumber)
					VALUES (@ProductNumber, 0, @InsertDate, @InsertDate, ISNULL(@ArrivalPacketNumber, - 1))

					INSERT INTO ProductPricePacket (FK_ProductNumber, PacketType, StartDate, EndDate, FK_PacketNumber)
					VALUES (@ProductNumber, 1, @InsertDate, @InsertDate, ISNULL(@StayPacketNumber, - 1))

					INSERT INTO ProductPricePacket (FK_ProductNumber, PacketType, StartDate, EndDate, FK_PacketNumber)
					VALUES (@ProductNumber, 2, @InsertDate, @InsertDate, ISNULL(@DeparturePacketNumber, - 1))

                    DECLARE PacketCur CURSOR FAST_FORWARD READ_ONLY FOR
                      SELECT FK_ProductNumberDiscounted
                      FROM ProductProductDiscountXTab WITH (NOLOCK)
                      WHERE FK_ProductNumber = @ProductNumber

                    OPEN PacketCur
                    FETCH NEXT FROM PacketCur INTO @FK_ProductNumberDiscounted
                    WHILE (@@FETCH_STATUS <> -1)
                    BEGIN
                      SET @PackageType = 0 
                      WHILE @PackageType <= 2
                      BEGIN
                        SET @PacketNumber = -1

                        SELECT TOP 1 @PacketNumber = FK_PacketNumber
                        FROM ProductPricePacket WITH (NOLOCK)
                        WHERE StartDate <= @DateStamp 
                          AND EndDate >= @DateStamp 
                          AND FK_ProductNumber = @FK_ProductNumberDiscounted 
                          AND PacketType = @PackageType
                        ORDER BY Id DESC
 
                        IF ISNULL(@FK_ProductNumberDiscounted, 0) > 0
                          INSERT INTO ProductPricePacket (FK_ProductNumber, PacketType, StartDate, EndDate, FK_PacketNumber) VALUES (@FK_ProductNumberDiscounted, @PackageType, @InsertDate, @InsertDate, ISNULL(@PacketNumber, -1))
 
                        SET @PackageType = @PackageType + 1  
                      END
                      
					  UPDATE Products
                      SET ProductEndDate = Convert(CHAR(8), @DestEndDate, 112)
                      WHERE ProductNumber = @FK_ProductNumberDiscounted

                      FETCH NEXT FROM PacketCur INTO @FK_ProductNumberDiscounted
                    END
                    CLOSE PacketCur
                    DEALLOCATE PacketCur

					--PRINT 'Values inserted.'
					FETCH NEXT FROM TmpCopyMonthCur INTO @ProductNumber, @DateStamp, @RoomtypeNumber , 
						@AdultPrice1 , @AdultPrice2, @AdultPrice3, @AdultPrice4, @AdultPrice5, @AdultPrice6, @AdultPrice7, @AdultPrice8, @AdultPrice9, 
						@Children1Price1, @Children1Price2, @Children1Price3, @Children1Price4, @Children1Price5, @Children1Price6, @Children1Price7, @Children1Price8, @Children1Price9, 
						@Children2Price1, @Children2Price2, @Children2Price3, @Children2Price4, @Children2Price5, @Children2Price6, @Children2Price7, @Children2Price8, @Children2Price9, 
						@AdultArrivalAddon, @AdultStayAddon, @AdultDepartureAddon, @Children1ArrivalAddon, @Children1StayAddon, @Children1DepartureAddon, 
						@Children2ArrivalAddon, @Children2StayAddon, @Children2DepartureAddon, 
						@AdultArrivalAddon2, @AdultStayAddon2, @AdultDepartureAddon2, @Children1ArrivalAddon2, @Children1StayAddon2, @Children1DepartureAddon2, @Children2ArrivalAddon2, @Children2StayAddon2, @Children2DepartureAddon2, 
						@AdultArrivalAddon3, @AdultStayAddon3, @AdultDepartureAddon3, @Children1ArrivalAddon3, @Children1StayAddon3, @Children1DepartureAddon3, @Children2ArrivalAddon3, 
						@Children2StayAddon3, @Children2DepartureAddon3, @AdultArrivalAddon4, @AdultStayAddon4, @AdultDepartureAddon4, @Children1ArrivalAddon4, @Children1StayAddon4, @Children1DepartureAddon4, @Children2ArrivalAddon4, @Children2StayAddon4, @Children2DepartureAddon4, 
						@AdultArrivalAddon5, @AdultStayAddon5, @AdultDepartureAddon5, @Children1ArrivalAddon5, @Children1StayAddon5, @Children1DepartureAddon5, @Children2ArrivalAddon5, @Children2StayAddon5, @Children2DepartureAddon5, 
						@AdultArrivalAddon6, @AdultStayAddon6, @AdultDepartureAddon6, @Children1ArrivalAddon6, @Children1StayAddon6, @Children1DepartureAddon6, @Children2ArrivalAddon6, @Children2StayAddon6, @Children2DepartureAddon6, 
						@AdultArrivalAddon7, @AdultStayAddon7, @AdultDepartureAddon7, @Children1ArrivalAddon7, @Children1StayAddon7, @Children1DepartureAddon7, @Children2ArrivalAddon7, @Children2StayAddon7, @Children2DepartureAddon7, 
						@AdultArrivalAddon8, @AdultStayAddon8, @AdultDepartureAddon8, @Children1ArrivalAddon8, @Children1StayAddon8, @Children1DepartureAddon8, @Children2ArrivalAddon8, @Children2StayAddon8, @Children2DepartureAddon8, 
						@AdultArrivalAddon9, @AdultStayAddon9, @AdultDepartureAddon9, @Children1ArrivalAddon9, @Children1StayAddon9, @Children1DepartureAddon9, @Children2ArrivalAddon9, @Children2StayAddon9, @Children2DepartureAddon9, 
						@ArrivalPacketNumber, @StayPacketNumber, @DeparturePacketNumber
				END
				CLOSE TmpCopyMonthCur
				DEALLOCATE TmpCopyMonthCur
			  END
			  ELSE
			  BEGIN
			    PRINT '@CurrentDateDT beyond @EndDateDT: ' + CONVERT(VARCHAR(8), @CurrentDateDT, 112) + ' - ' + CONVERT(VARCHAR(8), @EndDateDT, 112)
			  END
			END
	    END -- WHILE (@CurrentDateDT <= @EndDateDT)

		SELECT @NextSourceDateDT = DATEADD(DAY, 1, @NextSourceDateDT)

	END -- WHILE @NextSourceDateDT <= @LastSourceDateDT
  DROP TABLE #PackageInfo
  DROP TABLE #TempCopyMonthProductPrices
END

