CREATE PROCEDURE DBO.OnlineCheckInDeposit @AReferenceNumber INT, @AGuestNumber INT, @AWareTextOverwrite VARCHAR(100), @ATotalPrice MONEY AS
SET NOCOUNT ON

/*
  Created by Kent Johansen 

  Changes:
    KJ 20090618: Bug: Fix for "Only checkin deposit if arriving today"
    KJ 20090616: Bug: Don't checkin deposit if no received deposit on folio.
    KJ 20090203: Bug: Removed "WITH LOG". 
    KJ 20080104: Bug: Fix for "Do not complete posteings if any transactions exists"
    KJ 20071128: Bug: Fix for "Wrong Ware used for checkin"
*/

DECLARE @UserId INT
DECLARE @FolioNumber INT
DECLARE @WareNumber INT
DECLARE @Payment INT
DECLARE @CountryId INT
DECLARE @Department CHAR(1)
DECLARE @GlobalTransactionNumber1 INT
DECLARE @OnlineWarenumber INT
DECLARE @OnlinePayment INT
DECLARE @ResArrivalDate CHAR(8)
DECLARE @SystemDate CHAR(8)
DECLARE @CurDateStr CHAR(8)

SELECT @OnlineWarenumber = Keyvalue FROM Hotelsetup WITH (NOLOCK) Where Searchkey = 'PicassoOnlinePosteringWarenumber'
IF ISNULL(@OnlineWareNumber, 0) = 0
  RAISERROR('Online entry ware number is empty. Set value in HotelSet - Basc - Online', 15, 1)

SELECT @OnlinePayment = Payment FROM Ware WITH (NOLOCK) Where WareNumber = @OnlineWareNumber

SELECT @ResArrivalDate = ArrivalDate FROM Reservations WITH (NOLOCK) WHERE ReferenceNumber = @AReferenceNumber AND GuestNumber = @AGuestNumber
SELECT @CurDateStr = SystemDate FROM System WITH (NOLOCK) WHERE Number = 1

IF @OnlinePayment = 12 AND (@ResArrivalDate = @CurDateStr)
BEGIN
  SELECT TOP 1 @WareNumber = WareNumber FROM Ware WITH (NOLOCK) WHERE Payment = 10

  IF ISNULL(@WareNumber, 0) > 0
  BEGIN
    SELECT @Payment = Payment FROM Ware WITH (NOLOCK) WHERE WareNumber = @WareNumber
    IF @Payment = 10 -- Received Deposit
    BEGIN
      SELECT @UserId = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'PicassoOnlinePosteringUserIdNumber'
      IF ISNULL(@UserId, 0) <= 0
        SELECT TOP 1 @UserId = UserIDNumber  FROM Personel WITH (NOLOCK)

      SELECT @FolioNumber = FolioNumber FROM Folios WITH (NOLOCK) WHERE ReferenceNumber = @AReferenceNumber AND GuestNumber = ISNULL(@AGuestNumber, 0)

      DECLARE @cnt1 INT
      SET @cnt1 = 0

      IF @FolioNumber > 0
      BEGIN
        SELECT @cnt1 = COUNT(*) FROM Transactions T WITH (NOLOCK)  WHERE FolioNumber = @FolioNumber AND Payment = 10
 
        IF @cnt1 > 0
          RAISERROR('Error in storedprocedure <<OnlinePostering>>: This SP is called multiple times', 15, 1)
      END

      SELECT @CountryId = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'CountryCode'
      IF ISNULL(@CountryId, 0) = 0
        SET @CountryId = 1
    
      SELECT @Department = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'PicassoOnlinePosteringDepartment'
      IF ISNULL(@Department, '0') = '0'
        SELECT TOP 1 @Department = DepartmentNumber FROM Departments WITH (NOLOCK) 

      EXEC GetNextNumber 'GLOBALTRANSACTIONIDENTITY', 1, @GlobalTransactionNumber1 OUTPUT

      DECLARE @P1 INT
      SET @P1=NULL
      EXEC DBO.Postering NULL, 0, 1, @AReferenceNumber, @AGuestNumber, 0, @UserId, @FolioNumber, 0, 0, 0, 0, @AWareTextOverwrite, 1, @ATotalPrice, @CountryId, 0, 0, 0, 0, @Department, @WareNumber, 10, @GlobalTransactionNumber1, 0, 0, '', @P1 OUTPUT
    END
  END
END