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, not used
	@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 20210309: Added BookedWareCount
  SG 20210224: Added @NumberOfRooms (BookedWareCount)
	SG 20200514: Added @WareType and @WareNumber
	SG 20200207: Don't subtract package before discount. Calculate discount when @DiscountAsNettoPrice.
    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
DECLARE @WareType Integer
DECLARE @WareName varchar(50)

-- Calc Discount
SET @AllDiscountRate = 0
SET @DiscountRate = 0
--SET @TotalPrice = @TotalPrice - @PackagePrice
--SET @VatOfPrice = @VatOfPrice - @PackageVatOfPrice
-- Not taken HotelSetup "PackageSplitAfterDiscount" into account. 
-- When set, there should not be calculated discount on packages, but the discount on packages should be transferred to stay

SELECT @WareType = Type, @WareName = WareName FROM Ware with (nolock) Where WareNumber = @WareNumber

IF (@OrderBookBudgetCalcDiscount = 1) AND (@TotalPrice <> 0) AND (@CurrentDateStr >= @SystemDateStr)
BEGIN
    Declare @FolioNumber int --not used
    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
    Declare @UsedAccountNumber int
    IF @ChargeDiscountAsProduct = 0
    BEGIN
       Set @UsedAccountNumber = @DiscountAccountNumber
    END ELSE
    BEGIN
       Set @UsedAccountNumber = @AccountNumber
    END
    IF @DiscountAsNettoPrice=0
    BEGIN
        IF @AllDiscountRate > 0
        BEGIN
            SET @DiscountPrice = Round((@TotalPrice*(@AllDiscountRate/100)),2) * -1 --Discount, negative
            if (@DiscountPrice <> 0)
            BEGIN
                SET @DiscountPriceVat = (@DiscountPrice*(1-(1/((@DiscountVatValueWare/100)+1))))
                EXEC UpdateMISTable @SystemDateStr, @Room_HallType, @ReferenceNumber, @GuestNumber, @tmpUpdateType, @tmpSameDayAsLogi, 0, 0, 0, 0, @DiscountPrice, @DiscountPriceVat, 0, 0, @CurrentDateStr, @CurrentDateStrDeparturePackage, @SegmentNumber, @PointOfSaleNumber, @RoomTypeNumber, @ValueType, @HotelCustomerNumber, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @UsedAccountNumber, @DepartmentNumber, -1, 0, @WareName, @OwnerReservation, @ProductNumber,
				                    @WareType, @WareNumber, 0
            END
        END

        IF @DiscountRate > 0
        BEGIN
            SET @DiscountPrice = Round((@TotalPrice*(@DiscountRate/100)),2)*-1
            if (@DiscountPrice <> 0)
            BEGIN
                SET @DiscountPriceVat = (@DiscountPrice*(1-(1/((@DiscountVatValueWare/100)+1))))
                EXEC UpdateMISTable @SystemDateStr, @Room_HallType, @ReferenceNumber, @GuestNumber, @tmpUpdateType, @tmpSameDayAsLogi, 
                                    0, 0, 0, 0, 
                                    @DiscountPrice, @DiscountPriceVat, 0, 0, @CurrentDateStr, @CurrentDateStrDeparturePackage, 
                                    @SegmentNumber, @PointOfSaleNumber, @RoomTypeNumber, @ValueType, @HotelCustomerNumber, 
                                    @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @UsedAccountNumber, 
                                    @DepartmentNumber, -1, 0, @WareName, @OwnerReservation, @ProductNumber,
				                            @WareType, @WareNumber, 0
            END
        END
    End ELSE
    BEGIN
        SET @TotalPrice = @TotalPrice - (Round((@tmpPrice*((@AllDiscountRate + @DiscountRate)/100)),2))
    END
END
-- Calc Discount END

--SET @TotalPrice = @TotalPrice + @PackagePrice
--SET @VatOfPrice = @VatOfPrice + @PackageVatOfPrice
--print 'ReferenceNumber=' + convert(varchar, @ReferenceNumber) + ', DateStamp=' + @CurrentDateStr + ', WareNumber=' + convert(varchar, @wareNumber) + ', @NumberOfRooms=' + convert(varchar, @NumberOfRooms) + ', @BookedNumberOfRooms=' + convert(varchar, @BookedNumberOfRooms)
--Store total before discount

DECLARE @piecesOrNoOfRooms int
SELECT @piecesOrNoOfRooms = Al.Pieces
FROM Reservations R (Nolock)
    left OUTER JOIN 
    ArrangementReservationsXtab X (Nolock)
    ON R.ReferenceNumber = X.ReferenceNumber 
    AND R.GuestNumber = X.GuestNumber
      INNER JOIN 
      ArrangementLines Al (Nolock)
      ON X.ArrangementLineNumber = Al.ArrangementLineNumber
  WHERE R.ReferenceNumber = @referenceNumber
    AND Al.WareNumber = @wareNumber
    AND Al.date = @currentDateStr

Set @PiecesOrNoOfRooms = ISNULL(@PiecesOrNoOfRooms, @BookedNumberOfRooms)

if (@TotalPrice <> 0 or @TotalAdults > 0 or @TotalChildren1 > 0 or @TotalChildren2 > 0 or @PiecesOrNoOfRooms > 0)
    EXEC UpdateMISTable @SystemDateStr, @Room_HallType, @ReferenceNumber, @GuestNumber, @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, @WareName, @OwnerReservation, @ProductNumber,
	                      @WareType, @WareNumber, @PiecesOrNoOfRooms