CREATE procedure [dbo].[MisStoreLogiAndCalcDiscount] 
	@Room_HallType TinyInt,
	@OrderBookBudgetCalcDiscount BIT, 
	@DiscountAsNettoPrice BIT, --prices are stored with discount
	@ChargeDiscountAsProduct BIT,
	@DiscountVatValueWare FLOAT,  --VAT rate for the discount
	@SplitBillNumber INT,
	@IsFixedPriceProduct BIT,
	@tmpUpdateType TinyInt, 
	@tmpSameDayAsLogi bit,
	@ValueType tinyint,
	@BookedNumberOfRooms int, 
	@TotalAdults int, 
	@TotalChildren1 int, 
	@TotalChildren2 int,
	@ReferenceNumber int, 
	@GuestNumber INT,
	@FolioNumber int,
	@SystemDateStr char(8), 
	@CurrentDateStr char(8),
	@CurrentDateStrDeparturePackage char(8),
	@SegmentNumber int, 
	@PointOfSaleNumber int, 
	@RoomTypeNumber int, 
	@HotelCustomerNumber int, 
	@Status smallint, 
	@CampaignCode int, 
	@ProjectCode int, 
	@StaffID int, 
	@WareGroupNumber int,
	@WareNumber int,
	@OwnerReservation bit,
	@DepartmentNumber int, 
	@Pwln int,
	@AccountNumber INT,
	@DiscountAccountNumber INT,
	@TotalPrice MONEY,
	@VatOfPrice MONEY,
	@PackagePrice MONEY,
	@PackageVatOfPrice MONEY,
	@ProductNumber int
AS
SET NOCOUNT ON

/*
   Changes:
	SG 20160902: Don't calculate discount when @DiscountAsNettoPrice
	SG 20130129, CR 051930: No discount was calculated when VAT=0
	OF 20120907: Bug: Discount was added back in time from reservations.
	OF 20120601: New. For storing logi data in mis and calculating discount.
*/

DECLARE @AllDiscountRate FLOAT
DECLARE @DiscountRate FLOAT
DECLARE @AddressType INT
DECLARE @tmpPrice MONEY
DECLARE @DiscountPrice MONEY
DECLARE @DiscountPriceVat MONEY


-- Calc Discount
SET @AllDiscountRate = 0
SET @DiscountRate = 0
SET @TotalPrice = @TotalPrice - @PackagePrice
SET @VatOfPrice = @VatOfPrice - @PackageVatOfPrice

IF (@OrderBookBudgetCalcDiscount = 1) AND (@TotalPrice <> 0) AND (@CurrentDateStr >= @SystemDateStr)
BEGIN
	EXEC FindFolioNumber @ReferenceNumber, @GuestNumber, 0, @WareNumber, @SplitbillNumber, 0, @FolioNumber OUTPUT, @AddressType OUTPUT

	EXEC GetDiscountRate @ReferenceNumber, @GuestNumber, @AddressType, 0, @AllDiscountRate OUTPUT
	IF @Room_HallType = 0
	BEGIN
		-- ROOM
		EXEC GetDiscountRate @ReferenceNumber, @GuestNumber, @AddressType, 1, @DiscountRate OUTPUT
	END
	ELSE
	BEGIN
		-- HALL
		IF @IsFixedPriceProduct = 1
			EXEC GetDiscountRate @ReferenceNumber, @GuestNumber, @AddressType, 2, @DiscountRate OUTPUT
		ELSE
			EXEC GetDiscountRate @ReferenceNumber, @GuestNumber, @AddressType, 3, @DiscountRate OUTPUT
	END

	SET @tmpPrice = @TotalPrice
	IF @AllDiscountRate>0
	BEGIN
		IF @DiscountAsNettoPrice=0
		BEGIN
			SET @DiscountPrice = Round((@TotalPrice*(@AllDiscountRate/100)),2)*-1
			IF @ChargeDiscountAsProduct = 0
			BEGIN
				SET @DiscountPriceVat = (@DiscountPrice*(1-(1/((@DiscountVatValueWare/100)+1))))
				if (@DiscountPrice <> 0)
					EXEC UpdateMISTable @SystemDateStr, @Room_HallType, @ReferenceNumber, @GuestNumber, -1, @tmpUpdateType, @tmpSameDayAsLogi, 0, 0, 0, 0, @DiscountPrice, @DiscountPriceVat, 0, 0, @CurrentDateStr, @CurrentDateStrDeparturePackage, @SegmentNumber, @PointOfSaleNumber, @RoomTypeNumber, @ValueType, @HotelCustomerNumber, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @DiscountAccountNumber, @DepartmentNumber, -1, 0, '', @OwnerReservation, @ProductNumber
			END
			ELSE
			BEGIN
				SET @DiscountPriceVat = (@DiscountPrice*(1-(1/((@DiscountVatValueWare/100)+1))))
				if (@DiscountPrice <> 0)
					EXEC UpdateMISTable @SystemDateStr, @Room_HallType, @ReferenceNumber, @GuestNumber, -1, @tmpUpdateType, @tmpSameDayAsLogi, 0, 0, 0, 0, @DiscountPrice, @DiscountPriceVat, 0, 0, @CurrentDateStr, @CurrentDateStrDeparturePackage, @SegmentNumber, @PointOfSaleNumber, @RoomTypeNumber, @ValueType, @HotelCustomerNumber, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @AccountNumber, @DepartmentNumber, -1, 0, '', @OwnerReservation, @ProductNumber
			END
		END
		ELSE
			SET @TotalPrice = @TotalPrice-(Round((@tmpPrice*(@AllDiscountRate/100)),2))
	END

	IF @DiscountRate>0
	BEGIN
		IF @DiscountAsNettoPrice=0
		BEGIN
			SET @DiscountPrice = Round((@TotalPrice*(@DiscountRate/100)),2)*-1

			IF @ChargeDiscountAsProduct = 0
			BEGIN
				SET @DiscountPriceVat = (@DiscountPrice*(1-(1/((@DiscountVatValueWare/100)+1))))
				if (@DiscountPrice <> 0)
					EXEC UpdateMISTable @SystemDateStr, @Room_HallType, @ReferenceNumber, @GuestNumber, -1, @tmpUpdateType, @tmpSameDayAsLogi, 0, 0, 0, 0, @DiscountPrice, @DiscountPriceVat, 0, 0, @CurrentDateStr, @CurrentDateStrDeparturePackage, @SegmentNumber, @PointOfSaleNumber, @RoomTypeNumber, @ValueType, @HotelCustomerNumber, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @DiscountAccountNumber, @DepartmentNumber, -1, 0, '', @OwnerReservation, @ProductNumber
			END
			ELSE
			BEGIN
				SET @DiscountPriceVat = (@DiscountPrice*(1-(1/((@DiscountVatValueWare/100)+1))))
				if (@DiscountPrice <> 0)
					EXEC UpdateMISTable @SystemDateStr, @Room_HallType, @ReferenceNumber, @GuestNumber, -1, @tmpUpdateType, @tmpSameDayAsLogi, 0, 0, 0, 0, @DiscountPrice, @DiscountPriceVat, 0, 0, @CurrentDateStr, @CurrentDateStrDeparturePackage, @SegmentNumber, @PointOfSaleNumber, @RoomTypeNumber, @ValueType, @HotelCustomerNumber, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @AccountNumber, @DepartmentNumber, -1, 0, '', @OwnerReservation, @ProductNumber
			END
		END
		--ELSE
		 --don't calculate discount	
			--SET @TotalPrice = @TotalPrice-(Round((@tmpPrice*(@DiscountRate/100)),2))
	END
END
-- Calc Discount END


SET @TotalPrice = @TotalPrice + @PackagePrice
SET @VatOfPrice = @VatOfPrice + @PackageVatOfPrice

if (@TotalPrice <> 0 or @TotalAdults > 0 or @TotalChildren1 > 0 or @TotalChildren2 > 0)
	EXEC UpdateMISTable @SystemDateStr, @Room_HallType, @ReferenceNumber, @GuestNumber, -1, @tmpUpdateType, @tmpSameDayAsLogi, @BookedNumberOfRooms, @TotalAdults, @TotalChildren1, @TotalChildren2, @TotalPrice, @VatOfPrice, 0, 0, @CurrentDateStr, @CurrentDateStrDeparturePackage, @SegmentNumber, @PointOfSaleNumber, @RoomTypeNumber, @ValueType, @HotelCustomerNumber, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @AccountNumber, @DepartmentNumber, @Pwln, 0, '', @OwnerReservation, @ProductNumber