CREATE PROCEDURE [dbo].[OnlineTerminalLogin] @AReferencenumber INT, @AIncomMail VARCHAR(100), @CustomerNumber INT OUTPUT, 
@Firstname VARCHAR(100) OUTPUT, @Lastname VARCHAR(100) OUTPUT, @Address VARCHAR(100) OUTPUT, @City VARCHAR(100) OUTPUT, @Zipcode VARCHAR(50) OUTPUT, 
@Email  VARCHAR(100) OUTPUT, @AllowPaymentNoCard BIT OUTPUT, @Refnumber INT OUTPUT, @UsePaymentForCustomer BIT OUTPUT, 
@AGuestNumber INT=0, @AGuestNumberFound INT OUTPUT
AS SET NOCOUNT ON

/*
	Created by Troels Buch Dec 2007
	
	Changes -	
	KS20160822: Changes merged: TB20140918 + TB20140924 with TB20141027
	TB20141027: Added: AND ISNULL(LINK, 0) > 0
	TB20140924: Moved check for @CustomerNumber if login with email
	TB20140918: Added @AGuestNumberFound
	TB20140918: Added @AGuestNumber INT=0
	TB20130207: Added check on Payment for excisting customers - IF @UsePaymentOnTerminal = 1 AND @CustomerNumber =0
	TB20121119: Changed Set of @UsePaymentForCustomer
	TB20121108: Check reservation number in altres and voucher
	TB20090331: Added @Refnumber
	TB20090324: Added @AIncomMail	
	
	
	NOTE:
	@AGuestNumber = Guestnumber from namelist
*/

--DECLARE @CustomerNumber INT
--DECLARE @Zipcode VARCHAR(50)
--DECLARE @Email  VARCHAR(100)
DECLARE @AllowPaymentNoCardTmp INT

--print '@CustomerNumber'
--print @CustomerNumber

-- Check reservation number in altres and voucher
DECLARE @RefnrTmp INT
SELECT @RefnrTmp = referencenumber FROM reservations WITH (NOLOCK) WHERE referencenumber = @AReferencenumber OR Altresno = CONVERT(VARCHAR(50), @AReferencenumber) OR Voucher = CONVERT(VARCHAR(50), @AReferencenumber)
--SET @AReferencenumber = @RefnrTmp ---- KS: Så vil resnr jo være > 0 selv om man ikke på forhånd angiver det?!

--print '@CustomerNumber'
--print @CustomerNumber

SELECT @CustomerNumber=Link FROM reservationaddress WITH (NOLOCK)
INNER JOIN reservationaddressxtab WITH (NOLOCK)
ON reservationaddress.addressnumber = reservationaddressxtab.addressnumber
WHERE referencenumber = @AReferencenumber AND ISNULL(LINK, 0) > 0

SET @CustomerNumber = IsNULL(@CustomerNumber, 0)


--print '@CustomerNumber'
--print @CustomerNumber

/*
IF @CustomerNumber = 0
BEGIN
  SELECT TOP 1 @CustomerNumber=Link FROM reservationaddress WITH (NOLOCK)
  INNER JOIN reservationaddressxtab WITH (NOLOCK)
  ON reservationaddress.addressnumber = reservationaddressxtab.addressnumber
  WHERE Email = @AIncomMail ORDER BY Referencenumber DESC
END

SET @CustomerNumber = IsNULL(@CustomerNumber, 0)*/

--print '@CustomerNumber 2'
--print @CustomerNumber
/*
IF @CustomerNumber = 0
BEGIN
  SELECT CustomerNumber, ZipcodeOverwrite  FROM Customer (NOLOCK)
  WHERE CustomerNumber = @CustomerNumber
END*/



IF @AReferencenumber <> 0
BEGIN
  SELECT @Firstname=Firstname,  @Lastname=Lastname, @Address=Address1, @City=City, @Zipcode=Zipcode, @Email=Email, @Refnumber = @AReferencenumber, @AGuestNumberFound=@AGuestNumber
  FROM reservationaddress WITH (NOLOCK)
  INNER JOIN reservationaddressxtab WITH (NOLOCK)
  ON reservationaddress.addressnumber = reservationaddressxtab.addressnumber
  WHERE referencenumber = @AReferencenumber
  AND ADDRESSTYPE = 1 AND GUESTNUMBER=@AGuestNumber
  --print 'N1'
END
ELSE
BEGIN
  /*SELECT TOP 1 @Firstname=Firstname,  @Lastname=Lastname, @Address=Address1, @City=City, @Zipcode=Zipcode, @Email=Email, @Refnumber = reservationaddressxtab.Referencenumber, @AGuestNumberFound=Reservations.GuestNumber 
  FROM reservationaddress (NOLOCK)
  INNER JOIN reservationaddressxtab (NOLOCK) ON reservationaddress.addressnumber = reservationaddressxtab.addressnumber
  INNER JOIN Reservationroomlinextab (NOLOCK) ON Reservationroomlinextab.referencenumber = reservationaddressxtab.referencenumber
  INNER JOIN ReservationRoomline (NOLOCK) ON ReservationRoomline.linenumber = Reservationroomlinextab.linenumber
  INNER JOIN Reservations (NOLOCK) ON Reservations.Referencenumber = Reservationroomlinextab.Referencenumber 
  WHERE email = @AIncomMail AND Status <6 ORDER BY reservationaddressxtab.Referencenumber DESC*/
  --print 'N2'
  
  --SELECT TOP 1 Firstname,  Lastname ,Address1, City, Zipcode, Email, reservationaddressxtab.Referencenumber, reservationaddressxtab.GUESTNUMBER
  SELECT TOP 1 @Firstname=Firstname,  @Lastname=Lastname, @Address=Address1, @City=City, @Zipcode=Zipcode, @Email=Email, @Refnumber = reservationaddressxtab.Referencenumber, @AGuestNumberFound=reservationaddressxtab.GuestNumber 
  FROM reservationaddress WITH (NOLOCK)
  INNER JOIN reservationaddressxtab WITH (NOLOCK) ON reservationaddress.addressnumber = reservationaddressxtab.addressnumber  
  where EMAIL =@AIncomMail ORDER BY reservationaddressxtab.Referencenumber DESC  
  
  IF @CustomerNumber = 0
  BEGIN
    SELECT @CustomerNumber=Link FROM reservationaddress WITH (NOLOCK)
    INNER JOIN reservationaddressxtab WITH (NOLOCK)
    ON reservationaddress.addressnumber = reservationaddressxtab.addressnumber
    WHERE referencenumber = @Refnumber
  END

  SET @CustomerNumber = IsNULL(@CustomerNumber, 0)
END

SELECT @AllowPaymentNoCardTmp = COUNT(*) FROM Customer WITH (NOLOCK) INNER JOIN Ware WITH (NOLOCK) ON Customer.Paymenttypewarenumber = Ware.Warenumber
WHERE Ware.Payment IN (1,2) AND Customer.Customernumber = @CustomerNumber

--print '@CustomerNumber 2'
--print @CustomerNumber

--print '@Refnumber'
--print @Refnumber
-- OLD
--SELECT @UsePaymentForCustomer = PaymentAtCheckin FROM Customer (NOLOCK) 
--WHERE Customer.Customernumber = @CustomerNumber --AND customer.CustomerTypeNumber >1
select @UsePaymentForCustomer = IsNull(CUSTOMER.PaymentAtCheckin, 0) from reservations (nolock) 
inner join reservationroomlinextab WITH (nolock) on reservations.referencenumber = reservationroomlinextab.referencenumber  
inner join reservationroomline WITH (nolock) on reservationroomlinextab.linenumber = reservationroomline.linenumber 
inner join reservationaddressxtab WITH (nolock) on reservationaddressxtab.referencenumber =  reservations.referencenumber
inner join reservationaddress WITH (nolock) on reservationaddressxtab.addressnumber = reservationaddress.addressnumber
left join CUSTOMER WITH (NOLOCK) on CustomerNumber = link
Where reservations.referencenumber = @Refnumber AND ADDRESSTYPE >1

--print '@UsePaymentForCustomer'
--print @UsePaymentForCustomer

-- Check payment for reservations without customer
--DECLARE @UsePaymentForExcistCustomer BIT
--SET @UsePaymentForExcistCustomer = 0
DECLARE @PaymentType INT
SET @PaymentType = 0
DECLARE @UsePaymentOnTerminal BIT
SELECT @UsePaymentOnTerminal = keyvalue FROM HOTELSETUP WITH (nolock) WHERE SEARCHKEY = 'onlineterminalpaymentbeforecheckin'

--print 'Cust3'
--print @CustomerNumber
IF @UsePaymentOnTerminal = 1 AND @CustomerNumber =0
BEGIN
	--print 'ja'
	SELECT @PaymentType = Ware.Payment FROM RESERVATIONS WITH (NOLOCK)
	INNER JOIN Ware WITH (nolock) ON RESERVATIONS.methodofpayment = Ware.Warenumber 
	WHERE ReferenceNumber = @Refnumber
	
	--print '@PaymentType'
	--print @PaymentType
	
	IF @PaymentType = 3
	BEGIN		
		SET @UsePaymentForCustomer = 1		
	END
	ELSE
	BEGIN
		SET @UsePaymentForCustomer = 0
	END
END

IF @AllowPaymentNoCardTmp  > 0
   SET   @AllowPaymentNoCard = 1
ELSE
   SET   @AllowPaymentNoCard = 0

