CREATE PROCEDURE [dbo].[OnlinePosteringExt] @AWareNumber INT, @AHotelCustomerNumber INT, @AFolioNumber INT, @AAmount MONEY, @APieces INT, @AWareTextOverwrite VARCHAR(150) = '', @ATransNrShowOnBill INT OUTPUT, @AUserIdNumber INT = -1,
  @AOnlinePaymentId VARCHAR(100) = '', @AOnlineTransactionId VARCHAR(100) = '' AS
SET NOCOUNT ON

/*
  Created by Kent Johansen december 2012
  Description:
    Handle posting of:
      - Ware: payment = 0
      - Cash: payment = 3
      - Creditcard: payment = 4

  Changes:
    KJ 20200904: Ext: Added Payment and TransationId for use with OnlinePaymentModule
    KJ 20190928: Bug: Fix for "Error calling postering if CashDesk is enabled". Case #CR113714
  KJ 20150707: Added @AUserIdNumber 
--nb20140721 FolioIdentity discontinued
   KJ 20140611: Ext: CashDesk per hotel. Case #CR073269
    
*/

DECLARE @UseCashDesk BIT
DECLARE @CashDesk INT
DECLARE @Payment INT
DECLARE @tmpWareText VARCHAR(100)
DECLARE @DoContinue BIT
DECLARE @HotelNumber INT
DECLARE @UserId INT
DECLARE @FK_Department INT
DECLARE @GlobalTransactionNumber INT
DECLARE @DummyOriginalGlobalTransactionNumber INT
DECLARE @Iden INT

SET @DoContinue = 1

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

SELECT @AHotelCustomerNumber = CustomerNumber, @HotelNumber = HotelNumber 
FROM Customer WITH (NOLOCK)
WHERE CustomerNumber = @AHotelCustomerNumber
AND (HotelNumber >= 0 OR LocalHotel = 1)
AND ISNULL(Customer.MarkAsDeleted, 0) = 0

IF ISNULL(@AHotelCustomerNumber, 0) <= 0
BEGIN
  SET @DoContinue = 0
  RAISERROR('OnlinePosteringExt: @AHotelCustomerNumber not valid.', 15, 1)
END

SELECT @AWareNumber = WareNumber
FROM Ware WITH (NOLOCK) 
WHERE WareNumber = @AWareNumber
AND ISNULL(Ware.MarkAsDeleted, 0) <= 0
AND DBO.IsWareInHotelList(Ware.WareNumber, CAST(@HotelNumber AS VARCHAR), 1)= 1

IF ISNULL(@AWareNumber, 0) <= 0
BEGIN
  SET @DoContinue = 0
  RAISERROR('OnlinePosteringExt: @AWareNumber not valid.', 15, 1)
END

SELECT @Payment = Payment, @tmpWareText = WareName 
FROM Ware WITH (NOLOCK) WHERE WareNumber = @AWareNumber
SET @Payment = ISNULL(@Payment, -1)

IF ISNULL(@AWareTextOverwrite, '') = ''
  SET @AWareTextOverwrite = @tmpWareText

IF @DoContinue = 1
BEGIN
--  SELECT @UseCashDesk = CONVERT(BIT, KeyValue) FROM HotelSetup WITH (NOLOCK) WHERE Searchkey = 'FiscalPrinterSweden'
  SELECT @UseCashDesk = dbo.GetHotelSetupKeyValue ('FiscalPrinterSweden', @AHotelCustomerNumber)
  SET @UseCashDesk = ISNULL(@UseCashDesk, 0)

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

  IF ISNULL(@AUserIdNumber, -1) <= 0
  BEGIN
    SELECT @UserId = Keyvalue FROM Hotelsetup WITH (NOLOCK) WHERE Searchkey = 'PicassoOnlinePosteringUserIdNumber'
    IF @UserId <= 0
      SELECT TOP 1 @UserId = UserIDNumber FROM Personel WITH (Nolock)
  END
  ELSE
    SET @UserId = @AUserIdNumber

  IF ISNULL(@GlobalTransactionNumber, 0) <= 0
    EXEC GetNextNumber 'GLOBALTRANSACTIONIDENTITY', 1, @GlobalTransactionNumber OUTPUT

  IF @Payment = 0 -- Ware
  BEGIN
    IF @UseCashDesk = 1    
      EXEC Postering 0, 0, 1, 0, 0, 0, @UserId, @AFolioNumber, 0, 0, 0, 0, @AWareTextOverwrite, @APieces, @AAmount, -1, 0, 0, 0, 0, @FK_Department, @AWareNumber, @Payment, @GlobalTransactionNumber, 0, 0, '', @ATransNrShowOnBill OUTPUT, @AHotelCustomerNumber,
        -1, -1, 0, 0, 0, 0, 0, 0, @DummyOriginalGlobalTransactionNumber OUTPUT, @CashDesk
    ELSE
      EXEC Postering 0, 0, 1, 0, 0, 0, @UserId, @AFolioNumber, 0, 0, 0, 0, @AWareTextOverwrite, @APieces, @AAmount, -1, 0, 0, 0, 0, @FK_Department, @AWareNumber, @Payment, @GlobalTransactionNumber, 0, 0, '', @ATransNrShowOnBill OUTPUT, @AHotelCustomerNumber
  END
  ELSE IF @Payment = 3 OR @Payment = 4 -- Cash Creditcard
  BEGIN
    IF @UseCashDesk = 1
      EXEC Postering 0, 0, 1, 0, 0, 0, @UserId, @AFolioNumber, 0, -1, 0, @AFolioNumber, @AWareTextOverwrite, @APieces, @AAmount, -1, 0, 0, 0, 0, @FK_Department, @AWareNumber, @Payment, @GlobalTransactionNumber, 0, 0, '', @ATransNrShowOnBill OUTPUT, @AHotelCustomerNumber,
        -1, -1, 0, 0, 0, 0, 0, 0, @DummyOriginalGlobalTransactionNumber OUTPUT, @CashDesk
    ELSE
      EXEC Postering 0, 0, 1, 0, 0, 0, @UserId, @AFolioNumber, 0, -1, 0, @AFolioNumber, @AWareTextOverwrite, @APieces, @AAmount, -1, 0, 0, 0, 0, @FK_Department, @AWareNumber, @Payment, @GlobalTransactionNumber, 0, 0, '', @ATransNrShowOnBill OUTPUT, @AHotelCustomerNumber

    IF (ISNULL(@AOnlinePaymentId, '') <> '' OR ISNULL(@AOnlineTransactionId, '') <> '') AND @ATransNrShowOnBill > 0
    BEGIN
      INSERT INTO OnlinePayment (OnlinePaymentId, OnlineTransactionId) VALUES
        (@AOnlinePaymentId, @AOnlineTransactionId)
      SELECT @Iden = @@Identity
      INSERT INTO TransactionOnlinePaymentXtab (FK_TransactionNumber, FK_OnlinePaymentId) VALUES
        (@ATransNrShowOnBill, @Iden)
    END
  END
  ELSE
    RAISERROR('OnlinePosteringExt: Payment not supported.', 15, 1)
END