CREATE PROCEDURE dbo.OnlineRedeemGiftToken @AGiftTokenNumber VARCHAR(50), @AHotelCustomerNumber INT, @AAmount MONEY, @AGlobalGiftTokenSystem BIT = 0,
  @APayment INT  AS
SET NOCOUNT ON

/*
  Created by Kent Johansen september 2012
  Used by redeem and cancel gift card in global gift card system.
  Possible values for @APayment is: 
    15: For redeem 
    16: For cancel
  
  Prepared to be used for online redeem of gift card. Some coments made where some changes are needed to get it to work to redeem online.
  Changes:
    KJ 20200330: Ext: Added CashDesk when making post. Case #CR185366 + CR185454
    KJ 20191120: Bug: Fix for "Post is made on wrong folionumber if bonus card". Case #CR174101
    nb20140721 FolioIdentity discontinued
  
*/

DECLARE @FK_Department INT
DECLARE @WareNumberPayment INT
DECLARE @DoContinue BIT
DECLARE @UseCashDesk BIT
DECLARE @CashDesk INT

SET @DoContinue = 1

SELECT @FK_Department = dbo.GetHotelSetupKeyValue('PicassoOnlinePosteringDepartment', @AHotelCustomerNumber)
IF ISNULL(@FK_Department, 0) <= 0
BEGIN
  SET @DoContinue = 0
  RAISERROR('Error in storedprocedure <<OnlineRedeemGiftToken>>: @FK_Department not valid.', 15, 1)
END

SELECT @WareNumberPayment = dbo.GetHotelSetupKeyValue ('OnlineInhousePaymentWareNumber', @AHotelCustomerNumber)
IF ISNULL(@WareNumberPayment, 0) <= 0 AND ISNULL(@AGlobalGiftTokenSystem, 0) = 0
BEGIN
  SET @DoContinue = 0
  RAISERROR('Error in storedprocedure <<OnlineRedeemGiftToken>>: Online Inhouse Payment Ware <= 0.', 15, 1)
END
  
DECLARE @AddressNumber INT
DECLARE @FolioNumber INT
DECLARE @OnlineUserId INT
DECLARE @WareText VARCHAR(100)
DECLARE @WareTextPayment VARCHAR(100)
DECLARE @WareNumber INT
DECLARE @tmpWareNumber INT
DECLARE @GlobalTransactionNumber INT
DECLARE @TransNrShowOnBill INT
DECLARE @OriginalGlobalTransactionNumber INT
DECLARE @cnt INT
DECLARE @tmpGtt INT
DECLARE @tmpFolioNumber INT
DECLARE @tmpGiftTokenNumber VARCHAR(50)

SELECT @cnt = COUNT(*) 
FROM GiftTokenType WITH (NOLOCK)
WHERE Online = 1

IF ISNULL(@cnt, 0) = 0
BEGIN
  SET @DoContinue = 0
  RAISERROR('Error in storedprocedure <<OnlineRedeemGiftToken>>: No Online Gift certificate types.', 15, 1)
END

SELECT @tmpGtt = gtt.Id, @tmpGiftTokenNumber = gt.GiftTokenNumber2
FROM GiftTokenType gtt WITH(NOLOCK)
INNER JOIN GiftTokenGiftTokenTypeXTab xtab WITH (NOLOCK) ON xtab.FK_GiftTokenTypeId = gtt.Id
INNER JOIN GiftToken gt WITH (NOLOCK) ON gt.Id = xtab.FK_GiftTokenId
  AND gt.GiftTokenNumber = @AGiftTokenNumber
  
SELECT @WareText = WareName, @WareNumber = WareNumber 
FROM Ware WITH (NOLOCK)
WHERE FK_GiftTokenTypeId = @tmpGtt
AND Payment = @APayment
AND ISNULL(MarkAsDeleted, 0) = 0

IF ISNULL(@WareNumber, 0) <= 0
BEGIN
  SET @DoContinue = 0
  RAISERROR('Error in storedprocedure <<OnlineRedeemGiftToken>>: No gift cert redeem ware for selected gift certificate type', 15, 1)
END

-- Redeem Gift certificate allowed on selected hotel?
SELECT @tmpWareNumber = Ware.WareNumber
FROM Ware WITH (NOLOCK)
INNER JOIN WareDepartmentsXtab WITH (NOLOCK) ON WareDepartmentsXtab.WareNumber = Ware.WareNumber
  AND WareDepartmentsXtab.Type = 1 -- Cashier department
INNER JOIN Departments WITH (NOLOCK) ON Departments.PK_Department = WareDepartmentsXtab.FK_Department
  AND ISNULL(Departments.MarkAsDeleted, 0) = 0
INNER JOIN Customer WITH (NOLOCK) ON Customer.HotelNumber = Departments.HotelNumber
  AND Customer.CustomerNumber = @AHotelCustomerNumber
  AND Customer.CustomerTypeNumber = 7
  AND ISNULL(Customer.MarkAsDeleted, 0) = 0
WHERE Ware.WareNumber = @WareNumber

IF ISNULL(@tmpWareNumber, 0) <= 0
BEGIN
  SET @DoContinue = 0
  RAISERROR('Error in storedprocedure <<OnlineRedeemGiftToken>>: Gift Certificate redeem ware not allowed on selected hotel', 15, 1)  
END  
  
-- Payment ware allowed on selected hotel?
IF ISNULL(@AGlobalGiftTokenSystem, 0) = 0
BEGIN
  SELECT @tmpWareNumber = Ware.WareNumber
  FROM Ware WITH (NOLOCK)
  INNER JOIN WareDepartmentsXtab WITH (NOLOCK) ON WareDepartmentsXtab.WareNumber = Ware.WareNumber
    AND WareDepartmentsXtab.Type = 1 -- Cashier department
  INNER JOIN Departments WITH (NOLOCK) ON Departments.PK_Department = WareDepartmentsXtab.FK_Department
    AND ISNULL(Departments.MarkAsDeleted, 0) = 0
  INNER JOIN Customer WITH (NOLOCK) ON Customer.HotelNumber = Departments.HotelNumber
    AND Customer.CustomerNumber = @AHotelCustomerNumber
    AND Customer.CustomerTypeNumber = 7
    AND ISNULL(Customer.MarkAsDeleted, 0) = 0
  WHERE Ware.WareNumber = @WareNumberPayment

  IF ISNULL(@tmpWareNumber, 0) <= 0
  BEGIN
    SET @DoContinue = 0
    RAISERROR('Error in storedprocedure <<OnlineRedeemGiftToken>>: Online Inhouse Payment ware not allowed on selected hotel', 15, 1)  
  END
END  

IF @DoContinue = 1
BEGIN
  SET @tmpFolioNumber = -1
  IF ISNULL(@AGlobalGiftTokenSystem, 0) = 1
  BEGIN
    SELECT @tmpFolioNumber = FolioNumber FROM FOLIOS WITH (NOLOCK)
    INNER JOIN ReservationAddress WITH (NOLOCK) ON ReservationAddress.AddressNumber = Folios.ReservationAddressNumber
      AND Link = @AHotelCustomerNumber
    WHERE OpenFolio=1 
    AND HotelCustomerNumber = @AHotelCustomerNumber 
    AND ISNULL(Folios.ReferenceNumber, 0) = 0
    
    IF @tmpFolioNumber <= 0
    BEGIN
      SET @AddressNumber = 0
      EXEC UpdateReservationAddress @AHotelCustomerNumber, 7, @AddressNumber OUTPUT -- Reservation address is created here.
    
      /*
      EXEC GetNextNumber 'FolioIdentity', 1, @FolioNumber OUTPUT

      INSERT INTO Folios (FolioNumber, GuestNumber, ReservationAddressNumber, OpenFolio, Note, HotelCustomerNumber)
      VALUES (@FolioNumber, 0, @AddressNumber, 1, '', @AHotelCustomerNumber)
      */--nb20140721 FolioIndentity discontinued
      IF @AddressNumber > 0
      BEGIN
        INSERT INTO Folios (GuestNumber, ReservationAddressNumber, OpenFolio, Note, HotelCustomerNumber)
        VALUES (0, @AddressNumber, 1, '', @AHotelCustomerNumber)
        SELECT @FolioNumber = SCOPE_IDENTITY()--@@IDENTITY
      END

    END
    ELSE
      SET @FolioNumber = @tmpFolioNumber
  END
  ELSE
  BEGIN
/*
    EXEC ReservationAddressCreate @AFirstName, @ALastName, @AAddress1, @AAddress2, @AAddress3, @ACountryCode, @AZipCode, @ACity, @AEmail, @AOrderId, @AddressNumber OUTPUT
*/ -- hvad skal der ske ved online redeem gavekort????????????
--Man må vel formode at gavekort skal indløses mod folio til den reservation eller lign. som brugeren netop er ved at bestille online.
-- så folio skal sikkert findes via Folios tabellen, eller?
    /*
    EXEC GetNextNumber 'FolioIdentity', 1, @FolioNumber OUTPUT

    INSERT INTO Folios (FolioNumber, GuestNumber, ReservationAddressNumber, OpenFolio, Note, HotelCustomerNumber)
    VALUES (@FolioNumber, 0, @AddressNumber, 1, '', @AHotelCustomerNumber)
    */--nb20140721 FolioIndentity discontinued
    IF @AddressNumber > 0
    BEGIN
      INSERT INTO Folios (GuestNumber, ReservationAddressNumber, OpenFolio, Note, HotelCustomerNumber)
      VALUES (0, @AddressNumber, 1, '', @AHotelCustomerNumber)
      SELECT @FolioNumber = SCOPE_IDENTITY()--@@IDENTITY
    END
  END

  SELECT @UseCashDesk = dbo.GetHotelSetupKeyValue ('FiscalPrinterSweden', @AHotelCustomerNumber)
  SET @UseCashDesk = ISNULL(@UseCashDesk, 0)

  IF @UseCashDesk = 1
    EXEC GetCashDeskForOnline @AHotelCustomerNumber, @CashDesk OUTPUT

  -- Poster gavekort
  SELECT @OnlineUserId = Keyvalue FROM HotelSetup WITH (NOLOCK) WHERE Searchkey = 'PicassoOnlinePosteringUserIdNumber'
  IF @OnlineUserId <= 0
    SELECT TOP 1 @OnlineUserId = UserIDNumber FROM Personel WITH (NOLOCK)

  EXEC GetNextNumber 'GLOBALTRANSACTIONIDENTITY', 1, @GlobalTransactionNumber OUTPUT

  EXEC Postering 0, 0, 1, 0, 0, 0, @OnlineUserId, @FolioNumber, 0, -1, 0, @FolioNumber, @WareText, 1, @AAmount, -1, 0, 0, 0, 0, @FK_Department, @WareNumber, @APayment, @GlobalTransactionNumber, 0, 0, @AGiftTokenNumber, @TransNrShowOnBill OUTPUT, @AHotelCustomerNumber, NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, @OriginalGlobalTransactionNumber OUTPUT, @CashDesk

  IF ISNULL(@AGlobalGiftTokenSystem, 0) > 0
  BEGIN
    UPDATE GiftToken 
    SET GiftTokenNumber2 = @tmpGiftTokenNumber 
    WHERE GiftTokenNumber = @AGiftTokenNumber
    AND Status = 2
  END

  IF ISNULL(@AGlobalGiftTokenSystem, 0) = 0
  BEGIN
    -- Poster betaling
    SELECT @WareTextPayment = WareName
    FROM Ware WITH (NOLOCK)
    WHERE WareNumber = @WareNumberPayment
    AND ISNULL(MarkAsDeleted, 0) = 0

/*
    SET @GlobalTransactionNumber = @GlobalTransactionNumber + 1
    SELECT @Payment = Payment FROM Ware WITH (NOLOCK) WHERE WareNumber = @WareNumberPayment
    EXEC Postering 0, 0, 1, 0, 0, 0, @OnlineUserId, @FolioNumber, 0, -1, 0, @FolioNumber, @WareTextPayment, 1, @AAmount, -1, 0, 0, 0, 0, @FK_Department, @WareNumberPayment, @Payment, @GlobalTransactionNumber, 0, 0, '', @TransNrShowOnBill OUTPUT, @AHotelCustomerNumber, NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, @OriginalGlobalTransactionNumber OUTPUT, NULL
    --Create Bill and checkout
    EXEC OnlineCreateBillAndCheckout @FolioNumber, @AHotelCustomerNumber
*/  -- find ud af hvad der skal ske ved online indløsning af gavekort. Folio skal jo i hvert fald ikke lukkes. 
--Man må vel formode at gavekort skal indløses mod folio til den reservation eller lign. som brugeren netop er ved at bestille online.


  END  
END