CREATE PROCEDURE dbo.MisStoreExtrasAndCalcDiscount @OrderBookBudgetCalcDiscount BIT, @DiscountAsNettoPrice BIT, @ChargeDiscountAsProduct BIT, @DiscountVatValueWare FLOAT, @Room_HallType TINYINT, @ReferenceNumber INT, @FolioNumber INT, @DateStamp CHAR(8), 
  @SegmentNumber INT, @PointOfSaleNumber INT, @AGuestTypeNumber INT, @APurposeCodeNumber INT, @RoomTypeNumber INT, @CampaignCode INT, @ProjectCode INT, @StaffID INT, @OwnerReservation BIT, @DiscountAccountNumber INT, @ADiscountMISValueType INT, @ADiscountWareNumber INT, @ASplitBillNumber INT,
  @ACountryNumber INT AS
SET NOCOUNT ON

/*
  KJ 20250409: Menu ware allowed even if splitdoes not add up if UseForResidualAmount is set
  KJ 20241113: Bug: Fix for "VAT on Discount not correct when charge discount as product"
  KJ 20241009: Ext: Added From/To Time
  KJ 20240404: Bug: Fix for "Division by zero"
  KJ 20231218: Ext: Added Guesttype, purposeCode and country
  KJ 20230721: Ext: Discount extras and discount per ware
  KJ 20230412: Valuetype wrong for discount if ruuning charge discount as product
  SG 20210224: New fields: BookedWareCount, ActualWareCount
  SG 20200609: Get status from reservation instead of parameter
  SG 20200514: Added @WareType and @WareNumber to MIS
  SG 20191209: MenuWare: Prices * Pieces - when not in percent
  SG 20191128: MenuWare: Prices * Pieces
  SG 20181025: HotelCustomerNumber from Arrangement in order to get Mixed Resources correct
  KJ 20150105: Bug: Fix for "Error when post of more than one pieces of a menu ware not in percent". Case #CR081859
  KJ 20140624: Ext: Menu ware split in whole amount. Req #1561
  SG 20130129, CR 051930: No discount was calculated when VAT=0
  OF 20110913: New for extras and calculating of discount
*/

DECLARE @VatValueWareText varchar(5)

DECLARE @VatValueIndex0 FLOAT
DECLARE @VatValueIndex1 FLOAT
DECLARE @VatValueIndex2 FLOAT
DECLARE @VatValueIndex3 FLOAT
DECLARE @VatValueIndex4 FLOAT
DECLARE @VatValueIndex5 FLOAT

DECLARE @Price MONEY
DECLARE @VatOfPrice MONEY
DECLARE @AccountNumber INT
DECLARE @DepartmentNumber smallint
DECLARE @MisType INT
DECLARE @WarePayment SMALLINT
DECLARE @WareGroupNumber INT
DECLARE @WareNumber INT
DECLARE @WareType INT
DECLARE @WareName VARCHAR(50)
DECLARE @GuestNumber INT
DECLARE @ValueTypeExtra INT
DECLARE @HandleAsMenuWare BIT
DECLARE @MenuVatValueNumber INT
DECLARE @MenuProcentage MONEY
DECLARE	@MenuWareResultAccountNumber INT
DECLARE @MenuWarePrice FLOAT
DECLARE @MenuWarePriceVat FLOAT
DECLARE @MenuVatValueWare FLOAT
DECLARE @InPercent BIT
DECLARE @MenuWarePercent FLOAT

DECLARE @AllDiscountRate FLOAT
DECLARE @DiscountRate FLOAT
DECLARE @DiscountIsFixedAmount BIT
DECLARE @AddressType INT
DECLARE @tmpPrice MONEY
DECLARE @DiscountPrice MONEY
DECLARE @DiscountPriceVat MONEY
DECLARE @Text VARCHAR(100)
DECLARE @ResultPriceVat MONEY
DECLARE @Pieces INT
DECLARE @HotelCustomerNumberFromArr INT
DECLARE @Status SMALLINT
DECLARE @UsedAccountNumber INT
DECLARE @UsedValueType INT
DECLARE @UsedWareNumber INT

DECLARE @LFromTime DATETIME
DECLARE @LToTime DATETIME

DECLARE @TotalPrice MONEY
DECLARE @IsResidual BIT
DECLARE @ResidualNeeded BIT

SET @VatValueIndex0 = 0

SELECT @VatValueWareText = KeyValue FROM HotelSetUp WITH (NOLOCK) WHERE SearchKey='VATValue1'
IF (CHARINDEX(',', @VatValueWareText) > 0 )
  SET @VatValueWareText = STUFF(@VatValueWareText, CHARINDEX(',', @VatValueWareText), 1, '.')
SET @VatValueIndex1 = CONVERT(FLOAT, @VatValueWareText) 

SELECT @VatValueWareText = KeyValue FROM HotelSetUp WITH (NOLOCK) WHERE SearchKey='VATValue2'
if (CHARINDEX(',', @VatValueWareText) > 0)
  SET @VatValueWareText = STUFF(@VatValueWareText, CHARINDEX(',', @VatValueWareText), 1, '.')
SET @VatValueIndex2 = CONVERT(FLOAT, @VatValueWareText) 

SELECT @VatValueWareText = KeyValue FROM HotelSetUp WITH (NOLOCK) WHERE SearchKey='VATValue3'
if (CHARINDEX(',', @VatValueWareText) > 0)
  SET @VatValueWareText = STUFF(@VatValueWareText, CHARINDEX(',', @VatValueWareText), 1, '.')
SET @VatValueIndex3 = CONVERT(FLOAT, @VatValueWareText) 

SELECT @VatValueWareText = KeyValue FROM HotelSetUp WITH (NOLOCK) WHERE SearchKey='VATValue4'
if (CHARINDEX(',', @VatValueWareText) > 0)
  SET @VatValueWareText = STUFF(@VatValueWareText, CHARINDEX(',', @VatValueWareText), 1, '.')
SET @VatValueIndex4 = CONVERT(FLOAT, @VatValueWareText) 

SELECT @VatValueWareText = KeyValue FROM HotelSetUp WITH (NOLOCK) WHERE SearchKey='VATValue5'
if (CHARINDEX(',', @VatValueWareText) > 0)
  SET @VatValueWareText = STUFF(@VatValueWareText, CHARINDEX(',', @VatValueWareText), 1, '.')
SET @VatValueIndex5 = CONVERT(FLOAT, @VatValueWareText) 

IF ISNULL(@VatValueIndex1,0) = 0 SET @VatValueIndex1 = 0
IF ISNULL(@VatValueIndex2,0) = 0 SET @VatValueIndex2 = 0
IF ISNULL(@VatValueIndex3,0) = 0 SET @VatValueIndex3 = 0
IF ISNULL(@VatValueIndex4,0) = 0 SET @VatValueIndex4 = 0
IF ISNULL(@VatValueIndex5,0) = 0 SET @VatValueIndex5 = 0

DECLARE @BudgetExtra INT
SELECT @BudgetExtra = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'BudgetExtra'
SET @BudgetExtra = ISNULL(@BudgetExtra, 1)

DECLARE ExtrasCur CURSOR FAST_FORWARD READ_ONLY FOR
  SELECT ISNULL(ArrangementLines.Pieces * ArrangementLines.Price,0) as Total,
    ISNULL(
      CASE 
        WHEN Accounts.VATValueNumber = 0 THEN 
	  (ArrangementLines.Pieces * ArrangementLines.Price * ((1-(1/CONVERT(FLOAT, ((@VatValueIndex0/100)+1))))))  
	WHEN Accounts.VATValueNumber = 1 THEN
	  (ArrangementLines.Pieces * ArrangementLines.Price * ((1-(1/CONVERT(FLOAT, ((@VatValueIndex1/100)+1))))))  
	WHEN Accounts.VATValueNumber = 2 THEN
	  (ArrangementLines.Pieces * ArrangementLines.Price * ((1-(1/CONVERT(FLOAT, ((@VatValueIndex2/100)+1))))))  
	WHEN Accounts.VATValueNumber = 3 THEN
	  (ArrangementLines.Pieces * ArrangementLines.Price * ((1-(1/CONVERT(FLOAT, ((@VatValueIndex3/100)+1))))))  
	WHEN Accounts.VATValueNumber = 4 THEN
	  (ArrangementLines.Pieces * ArrangementLines.Price * ((1-(1/CONVERT(FLOAT, ((@VatValueIndex4/100)+1))))))  
	WHEN Accounts.VATValueNumber = 5 THEN
	  (ArrangementLines.Pieces * ArrangementLines.Price * ((1-(1/CONVERT(FLOAT, ((@VatValueIndex5/100)+1))))))  
	END, 0) as TotalVat,
	Accounts.AccountNumber,
	CASE WHEN ArrangementLines.FK_ExDepartment IS NULL THEN 
    ISNULL((SELECT TOP 1 PK_Department FROM Departments d WITH (NOLOCK)
    INNER JOIN ReservationMain r WITH (NOLOCK) ON d.HotelNumber = r.HotelNumber AND AutoCharge = 1 AND ISNULL(MarkAsDeleted, 0) = 0 AND ReferenceNumber = @ReferenceNumber), -1)
  ELSE
    ArrangementLines.FK_ExDepartment
  END AS DepartmentNumber,
  ISNULL(WareGroups.WareType, -1),
  ISNULL(WareGroups.Payment, -1),
  WareGroups.SortNumber AS WareGroupNumber,
  Ware.WareNumber,
  Ware.Type AS WareType,
  WareName,
  ArrangementReservationsXtab.GuestNumber,
  ArrangementLines.Pieces,
  isnull(Customer.CustomerNumber,-1) as HotelCustomerNumber,
  r.Status,
  CASE WHEN ISDATE(ArrangementLines.Time) = 1 THEN CONVERT(DATETIME, ArrangementLines.Time, 108) ELSE 0 END, CASE WHEN ISDATE(ArrangementLines.DepartureTime) = 1 THEN CONVERT(DATETIME, ArrangementLines.DepartureTime, 108) ELSE 0 END
  FROM ArrangementLines WITH (NOLOCK) 
  INNER JOIN ArrangementReservationsXtab WITH (NOLOCK) on ArrangementReservationsXtab.ArrangementLineNumber = ArrangementLines.ArrangementLineNumber 
    AND ReferenceNumber = @ReferenceNumber
  INNER JOIN Ware WITH (NOLOCK) ON ArrangementLines.WareNumber = Ware.WareNumber
  INNER JOIN WareGroups WITH (NOLOCK) ON WareGroups.WaregroupNumber = Ware.WareGroupNumber 
  INNER JOIN Accounts WITH (NOLOCK) ON Accounts.AccountNumber = Ware.ResultAccountNumber
  LEFT OUTER JOIN ArrangementRoomXTab WITH (NOLOCK) ON ArrangementLines.ArrangementLineNumber = ArrangementRoomXTab.arrangementLinenumber
  INNER JOIN Departments WITH (NOLOCK) ON Departments.PK_Department = ArrangementLines.FK_ExDepartment
  INNER JOIN Customer WITH (NOLOCK) ON Customer.HotelNumber = Departments.HotelNumber
    AND Customer.HotelNumber >= 0
    AND Customer.CustomerTypeNumber = 7
    AND ISNULL(Customer.MarkAsDeleted, 0) = 0
  INNER JOIN Reservations r ON ArrangementReservationsXtab.ReferenceNumber = r.ReferenceNumber 
    AND ArrangementReservationsXtab.GuestNumber = r.GuestNumber
  WHERE ((@DateStamp = '00000000') OR (ArrangementLines.Date = @DateStamp and arrangementroomxtab.arrangementLinenumber IS NULL))
  AND ((@BudgetExtra = 0) OR (not ArrangementLines.FK_ExDepartment is null))
  AND ISNULL(ArrangementLines.PacketItemNumber, 0) = 0
OPEN ExtrasCur 

FETCH NEXT FROM ExtrasCur INTO @Price, @VatOfPrice, @AccountNumber, @DepartmentNumber, @MisType, @WarePayment, @WaregroupNumber, @WareNumber, @WareType, @WareName, @GuestNumber, @Pieces, @HotelCustomerNumberFromArr, @Status, @LFromTime, @LToTime

WHILE(@@FETCH_STATUS <> -1) 
BEGIN 
  --print '@HotelCustomerNumberFromArr=' + convert(varchar, @HotelCustomerNumberFromArr) + ', @Price=' + convert(varchar, @Price)
  SET @ValueTypeExtra = 0
  IF @MisType = 1
    SET @ValueTypeExtra = 5
  ELSE IF @MisType = 2
    SET @ValueTypeExtra = 4
  ELSE IF @MisType = 0
  BEGIN
    IF @WarePayment = 7 -- ManuelRoom
      SET @ValueTypeExtra = 1	
    ELSE IF @WarePayment = 8 -- ManuelHalls
      SET @ValueTypeExtra = 2
  END

  IF @OrderBookBudgetCalcDiscount = 1
  BEGIN
    SET @AllDiscountRate = 0
    EXEC FindFolioNumber @ReferenceNumber, @GuestNumber, 0, @WareNumber, @ASplitbillNumber, 0, @FolioNumber OUTPUT, @AddressType OUTPUT
    EXEC GetDiscountRateWare @ReferenceNumber, @GuestNumber, @WareNumber, @AddressType, @AllDiscountRate OUTPUT, @DiscountIsFixedAmount OUTPUT
  END

  EXEC WareIsMenuWare @WareNumber, @Price, @Pieces, @HandleAsMenuWare OUTPUT

  IF @HandleAsMenuWare = 1
  BEGIN
    --Insert the menuWare (BookedWareCnount)
    INSERT INTO MISTABLE (Room_HallType, ReferenceNumber, GuestNumber, FolioNumber, DateStamp, SegmentNumber, PointOfSaleNumber, FK_GuestTypeNumber, FK_PurposeCodeNumber, RoomTypeNumber, BookedAdult, BookedChildren1, BookedChildren2, BookedNumberOfRooms, BookedPrice, BookedVatOfPrice, BookedPackage, BookedVatOfPackage, ActualAdult, ActualChildren1, ActualChildren2, ActualNumberOfRooms, ActualPrice, ActualVatOfPrice, ActualPackage, ActualVatOfPackage, ValueType, HotelCustomerNumber, ReservationStatus, CampaignCode, ProjectCode, StaffID, WareGroupNumber, AccountNumber, DepartmentNumber, Text, OwnerReservation,
      WareType, WareNumber, BookedWareCount, ActualWareCount, FK_CountryNumber, FromTime, ToTime)
    VALUES (@Room_HallType, @referencenumber, @GuestNumber, -1, @DateStamp, @SegmentNumber, @PointOfSaleNumber, @AGuestTypeNumber, @APurposeCodeNumber, @RoomTypeNumber, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @ValueTypeExtra, @HotelCustomerNumberFromArr, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @DiscountAccountNumber, @DepartmentNumber, @WareName, @OwnerReservation,
      @WareType, @WareNumber, @Pieces, 0, @ACountryNumber, @LFromTime, @LToTime)

    -- get the sub menuwares
    SET @TotalPrice = 0
    DECLARE db_menuware_cur CURSOR STATIC FOR
      SELECT mw.Procentage, Accounts.VatValueNumber, w.ResultAccountNumber, wg.SortNumber AS WaregroupNumber, ISNUll(MenuWare.InPercent, 0), w.WareNumber, w.WareName, mw.UseForResidualAmount
      FROM Ware w WITH (NOLOCK)          
      INNER JOIN MenuWareXTab mw WITH (NOLOCK) ON w.WareNumber = mw.WareNumber 
        AND mw.MenuNumber = @WareNumber
      INNER JOIN WareGroups wg WITH (NOLOCK) ON wg.WareGroupNumber = w.WareGroupNumber
      INNER JOIN ACCOUNTS WITH (NOLOCK) ON w.ResultAccountNumber = Accounts.AccountNumber
      INNER JOIN MenuWare WITH (NOLOCK) ON MenuWare.FK_MenuNumber = mw.MenuNumber
    OPEN db_menuware_cur

    FETCH NEXT FROM db_menuware_cur INTO @MenuProcentage, @MenuVatValueNumber, @MenuWareResultAccountNumber, @WareGroupNumber, @InPercent, @WareNumber, @WareName, @IsResidual
    WHILE @@FETCH_STATUS = 0
    BEGIN
      IF @MenuProcentage > 0 AND @InPercent = 0
        SET @TotalPrice = @TotalPrice + (@MenuProcentage * @Pieces)

      FETCH NEXT FROM db_menuware_cur INTO @MenuProcentage, @MenuVatValueNumber, @MenuWareResultAccountNumber, @WareGroupNumber, @InPercent, @WareNumber, @WareName, @IsResidual
    END

    IF (ISNULL(@InPercent, 0) = 0) AND (@TotalPrice <> @Price)
      SET @ResidualNeeded = 1
    ELSE
      SET @ResidualNeeded = 0

    FETCH FIRST FROM db_menuware_cur INTO @MenuProcentage, @MenuVatValueNumber, @MenuWareResultAccountNumber, @WareGroupNumber, @InPercent, @WareNumber, @WareName, @IsResidual
    WHILE @@FETCH_STATUS = 0
    BEGIN
      EXEC GetVatvalueFromVATValueNumber @MenuVatValueNumber, @MenuVatValueWare OUTPUT
      IF @InPercent = 1
        SET @MenuWarePrice = ((@Price*@MenuProcentage)/100.0) 
      ELSE
      BEGIN
        SET @MenuWarePrice = @MenuProcentage * @Pieces

        IF @ResidualNeeded = 1 AND @IsResidual = 1
        BEGIN
          SET @MenuWarePrice = @MenuWarePrice + @Price - @TotalPrice
          SET @ResidualNeeded = 0
        END

        IF @Price <> 0
          SET @MenuWarePercent = (@MenuProcentage/@Price)*100
        ELSE
          SET @MenuWarePercent = 0
      END

      IF (@OrderBookBudgetCalcDiscount = 1) AND (@AllDiscountRate > 0)
      BEGIN
        IF @DiscountAsNettoPrice = 0
        BEGIN
          IF @DiscountIsFixedAmount = 1
            SET @DiscountPrice = ROUND(@AllDiscountRate*(@MenuWarePercent * @Pieces)/100,2)*-1
          ELSE
            SET @DiscountPrice = ROUND((@MenuWarePrice*(@AllDiscountRate/100)),2)*-1
          SET @DiscountPriceVat = (@DiscountPrice*(1-(1/((@DiscountVatValueWare/100)+1))))
			  
          IF @ChargeDiscountAsProduct = 0
          BEGIN
            SET @UsedAccountNumber = @DiscountAccountNumber
            SET @UsedValueType = @ADiscountMISValueType 
            SET @UsedWareNumber = @ADiscountWareNumber
          END
          ELSE
          BEGIN
            SET @UsedAccountNumber = @MenuWareResultAccountNumber
            SET @UsedValueType = @ValueTypeExtra 
            SET @UsedWareNumber = @WareNumber
            SELECT @DiscountVatValueWare = ISNULL(dbo.GetVatPercent(Accounts.VatValueNumber), 0)
            FROM Ware WITH (NOLOCK)
            INNER JOIN Accounts WITH (NOLOCK) ON Accounts.AccountNumber = Ware.ResultAccountNumber
            WHERE Ware.WareNumber = @WareNumber
          END

          INSERT INTO MISTABLE (Room_HallType, ReferenceNumber, GuestNumber, FolioNumber, DateStamp, SegmentNumber, PointOfSaleNumber, FK_GuestTypeNumber, FK_PurposeCodeNumber, RoomTypeNumber, BookedAdult, BookedChildren1, BookedChildren2, BookedNumberOfRooms, BookedPrice, BookedVatOfPrice, BookedPackage, BookedVatOfPackage, ActualAdult, ActualChildren1, ActualChildren2, ActualNumberOfRooms, ActualPrice, ActualVatOfPrice, ActualPackage, ActualVatOfPackage, ValueType, HotelCustomerNumber, ReservationStatus, CampaignCode, ProjectCode, StaffID, WareGroupNumber, AccountNumber, DepartmentNumber, Text, OwnerReservation,
            WareType, WareNumber, BookedWareCount, ActualWareCount, FK_CountryNumber, FromTime, ToTime) 
          VALUES (@Room_HallType, @referencenumber, @GuestNumber, -1, @DateStamp, @SegmentNumber, @PointOfSaleNumber, @AGuestTypeNumber, @APurposeCodeNumber, @RoomTypeNumber, 0, 0, 0, 0, @DiscountPrice, @DiscountPriceVat, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @ValueTypeExtra, @HotelCustomerNumberFromArr, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @UsedAccountNumber, @DepartmentNumber, @WareName, @OwnerReservation,
            @WareType, @UsedWareNumber, @Pieces, 0, @ACountryNumber, @LFromTime, @LToTime) 
        END
        ELSE
        BEGIN
          IF @DiscountIsFixedAmount = 1
            SET @MenuWarePrice = @MenuWarePrice - (ROUND(@AllDiscountRate*@Pieces*@MenuWarePercent/100,2))
          ELSE
            SET @MenuWarePrice = @MenuWarePrice - (ROUND((@MenuWarePrice*(@AllDiscountRate/100)),2))
        END
      END   

      SET @MenuWarePriceVat = (@MenuWarePrice*(1-(1/((@DiscountVatValueWare/100)+1))))

      INSERT INTO MISTABLE (Room_HallType, ReferenceNumber, GuestNumber, FolioNumber, DateStamp, SegmentNumber, PointOfSaleNumber, FK_GuestTypeNumber, FK_PurposeCodeNumber, RoomTypeNumber, BookedAdult, BookedChildren1, BookedChildren2, BookedNumberOfRooms, BookedPrice, BookedVatOfPrice, BookedPackage, BookedVatOfPackage, ActualAdult, ActualChildren1, ActualChildren2, ActualNumberOfRooms, ActualPrice, ActualVatOfPrice, ActualPackage, ActualVatOfPackage, ValueType, HotelCustomerNumber, ReservationStatus, CampaignCode, ProjectCode, StaffID, WareGroupNumber, AccountNumber, DepartmentNumber, Text, OwnerReservation,
        WareType, WareNumber, BookedWareCount, ActualWareCount, FK_CountryNumber, FromTime, ToTime) 
      VALUES (@Room_HallType, @referencenumber, @GuestNumber, -1, @DateStamp, @SegmentNumber, @PointOfSaleNumber, @AGuestTypeNumber, @APurposeCodeNumber, @RoomTypeNumber, 0, 0, 0, 0, @MenuWarePrice, @MenuWarePriceVat, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @ValueTypeExtra, @HotelCustomerNumberFromArr, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @MenuWareResultAccountNumber, @DepartmentNumber, @WareName, @OwnerReservation,
        @WareType, @WareNumber, @Pieces, 0, @ACountryNumber, @LFromTime, @LToTime)

      --BookedWareCount: Both the menuware and the manuparts are stored with a count  
      FETCH NEXT FROM db_menuware_cur INTO @MenuProcentage, @MenuVatValueNumber, @MenuWareResultAccountNumber, @WareGroupNumber, @InPercent, @WareNumber, @WareName, @IsResidual
    END  
    CLOSE db_menuware_cur
    DEALLOCATE db_menuware_cur
  END
  ELSE
  BEGIN
    -- Calc Discount
    IF @OrderBookBudgetCalcDiscount = 1 AND @AllDiscountRate > 0
    BEGIN
      IF @DiscountAsNettoPrice=0
      BEGIN
        IF @DiscountIsFixedAmount = 1
          SET @DiscountPrice = @AllDiscountRate * @Pieces
        ELSE
          SET @DiscountPrice = ROUND((@Price*(@AllDiscountRate/100)),2)*-1
        SET @DiscountPriceVat = (@DiscountPrice*(1-(1/((@DiscountVatValueWare/100)+1))))

        IF @ChargeDiscountAsProduct = 0
        BEGIN
          SET @UsedAccountNumber = @DiscountAccountNumber
          SET @UsedValueType = @ADiscountMISValueType 
          SET @UsedWareNumber = @ADiscountWareNumber
        END
        ELSE
        BEGIN
          SET @UsedAccountNumber = @AccountNumber
          SET @UsedValueType = @ValueTypeExtra 
          SET @UsedWareNumber = @WareNumber
        END

        INSERT INTO MISTABLE (Room_HallType, ReferenceNumber, GuestNumber, FolioNumber, DateStamp, SegmentNumber, PointOfSaleNumber, FK_GuestTypeNumber, FK_PurposeCodeNumber, RoomTypeNumber, BookedAdult, BookedChildren1, BookedChildren2, BookedNumberOfRooms, BookedPrice, BookedVatOfPrice, BookedPackage, BookedVatOfPackage, ActualAdult, ActualChildren1, ActualChildren2, ActualNumberOfRooms, ActualPrice, ActualVatOfPrice, ActualPackage, ActualVatOfPackage, ValueType, HotelCustomerNumber, ReservationStatus, CampaignCode, ProjectCode, StaffID, WareGroupNumber, AccountNumber, DepartmentNumber, Text, OwnerReservation,
          WareType, WareNumber, BookedWareCount, ActualWareCount, FK_CountryNumber, FromTime, ToTime) 
        VALUES (@Room_HallType, @referencenumber, @GuestNumber, -1, @DateStamp, @SegmentNumber, @PointOfSaleNumber, @AGuestTypeNumber, @APurposeCodeNumber, @RoomTypeNumber, 0, 0, 0, 0, @DiscountPrice, @DiscountPriceVat, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @ValueTypeExtra, @HotelCustomerNumberFromArr, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @UsedAccountNumber, @DepartmentNumber, @WareName, @OwnerReservation,
          @WareType, @UsedWareNumber, 0, 0, @ACountryNumber, @LFromTime, @LToTime) 
      END
      ELSE
      BEGIN
        IF @DiscountIsFixedAmount = 1
          SET @Price = @Price- (@AllDiscountRate * @Pieces)
        ELSE
          SET @Price = @Price-(ROUND((@Price*(@AllDiscountRate/100)),2))
      END
    END  
	  -- Calc Discount END

    INSERT INTO MISTABLE (Room_HallType, ReferenceNumber, GuestNumber, FolioNumber, DateStamp, SegmentNumber, PointOfSaleNumber, FK_GuestTypeNumber, FK_PurposeCodeNumber, RoomTypeNumber, BookedAdult, BookedChildren1, BookedChildren2, BookedNumberOfRooms, BookedPrice, BookedVatOfPrice, BookedPackage, BookedVatOfPackage, ActualAdult, ActualChildren1, ActualChildren2, ActualNumberOfRooms, ActualPrice, ActualVatOfPrice, ActualPackage, ActualVatOfPackage, ValueType, HotelCustomerNumber, ReservationStatus, CampaignCode, ProjectCode, StaffID, WareGroupNumber, AccountNumber, DepartmentNumber, Text, OwnerReservation,
      WareType, WareNumber, BookedWareCount, ActualWareCount, FK_CountryNumber, FromTime, ToTime) 
    VALUES (@Room_HallType, @referencenumber, @GuestNumber, -1, @DateStamp, @SegmentNumber, @PointOfSaleNumber, @AGuestTypeNumber, @APurposeCodeNumber, @RoomTypeNumber, 0, 0, 0, 0, @Price, @VatOfPrice, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @ValueTypeExtra, @HotelCustomerNumberFromArr, @Status, @CampaignCode, @ProjectCode, @StaffID, @WareGroupNumber, @AccountNumber, @DepartmentNumber, @WareName, @OwnerReservation,
      @WareType, @WareNumber, @Pieces, 0, @ACountryNumber, @LFromTime, @LToTime) 
  END

  FETCH NEXT FROM ExtrasCur INTO @Price, @VatOfPrice, @AccountNumber, @DepartmentNumber, @MisType, @WarePayment, @WaregroupNumber, @WareNumber, @WareType, @WareName, @GuestNumber, @Pieces, @HotelCustomerNumberFromArr, @Status, @LFromTime, @LToTime
END
CLOSE ExtrasCur 
DEALLOCATE ExtrasCur