CREATE FUNCTION dbo.PaymentIdentification (@InvoiceNumber int, @CustomerNumber varchar(20), @HotelCreditorNumber varchar(10), @PhoneNumber varchar(20), @Invoice_ReferenceNumber int, @GuestNumber int, @BillNumber int, @HotelNumber int, @ReferenceBillInvoiceNumber int, @SwitchCustomerRBI int) 
RETURNS Varchar(30) AS
/* 
	type = 1  InvoiceNumber, CustomerNumber, HotelCreditorNumber
	type = 2  PhoneNumber, BillNumber, HotelCreditorNumber
	Type = 3  Invoice_ReferenceNumber , Guestnumber, HotelNumber ,HotelCreditorNumber
	Type = 4  CustomerNumber, ReferenceBillInvoiceNumber, HotelCreditorNumber, Hotelnumber, SwitchCustomerRBI
 */
BEGIN
	DECLARE
		@IdentificationNumber varchar(30),
		@InvoiceN0 varchar(25),
		@tmp varchar(25),
		@Paymenttype varchar(50)

	Set @Paymenttype = (Select Keyvalue from hotelsetup WITH (nolock) where searchkey='InvoicePaymentIdentification')

/*
0=<None>
1='Customer- & BillNumber. Length 21' = type 4
2='Customer- & InvoiceNumber. Length 21' = type 4
3='Customer- & ReferenceNumber. Length 21' = type 4
4='Customer- & BillNumber. Length 15/16' = type 2
5='Phone- & BillNumber. Length 15/16' = type
6='ReferenceNumber. Length 15/16' = type 3
7='Bill- & CustomerNumber. Length 21' = type 4
8='Invoice- & CustomerNumber. Length 21' = type 4
9='Reference- & CustomerNumber. Length 21' = type 4
10='Customer-,Invoice- & CreditorNumber. Length 19' = type 1

*/

	/* type 1 (19 cifre)*/
	if ((@Paymenttype=10)) and (@InvoiceNumber is not null) and (@CustomerNumber is not null) and (@HotelCreditorNumber is not null)
	Begin
		Select @InvoiceN0 = REPLICATE('0', 9-Len(cast(@invoiceNumber as Varchar(9)))) + cast(@invoiceNumber as Varchar(9))
		Set @IdentificationNumber = @InvoiceN0 + Replicate('0',6-Len(@CustomerNumber)) + @CustomerNumber
		Set @IdentificationNumber = @IdentificationNumber + Replicate('0',3-len(@HotelCreditorNumber)) + @HotelCreditorNumber
		Set @IdentificationNumber = dbo.MaesureIdentification(@IdentificationNumber)
	end
	/* type 2 */
	if (@Paymenttype in (4,5)) and ((@PhoneNumber is not null) or (@CustomerNumber is not null)) and (@BillNumber is not null) and (@HotelCreditorNumber is not null)
	Begin
		if @PhoneNumber is null
			Set @tmp = Replace(@CustomerNumber,' ','')
		else
			Set @tmp = Replace(@PhoneNumber,' ','')
		set @IdentificationNumber = Replicate('0',8-Len(@tmp)) + @tmp
		if len(@HotelCreditorNumber) = 7 
			Set @tmp = Replicate('0',7-Len(Cast(@BillNumber as Varchar))) + Cast(@BillNumber as Varchar)	
		else
			Set @tmp = Replicate('0',6-Len(Cast(@BillNumber as Varchar))) + Cast(@BillNumber as Varchar)
		Set @IdentificationNumber = @IdentificationNumber + @tmp
		Set @IdentificationNumber = Replace(@IdentificationNumber, ' ', '0')
		Set @IdentificationNumber = dbo.MaesureIdentification(@IdentificationNumber)
	end
	/* type 3 */
	if (@Paymenttype = 6) and (@Invoice_ReferenceNumber  is not null) and (@Guestnumber is not null) and (@Hotelnumber is not null) and (@HotelCreditorNumber is not null)
	Begin
		if @Invoice_ReferenceNumber >= 0 
		Begin
			Set @IdentificationNumber = Substring(cast(@HotelNumber as varchar(2)),1,2)
			if @GuestNumber >= 0
			Begin
				Set @IdentificationNumber = @IdentificationNumber + '0'
				if len(@HotelCreditorNumber) = 7
					Set @tmp = Replicate('0',10-(len(Cast(@Invoice_ReferenceNumber as Varchar)))) + Cast(@Invoice_ReferenceNumber as Varchar) + Replicate('0',3-(len(Cast(@GuestNumber as Varchar)))) + Cast(@GuestNumber as Varchar)
				else
					Set @tmp = Replicate('0',9-(len(Cast(@Invoice_ReferenceNumber as Varchar)))) + Cast(@Invoice_ReferenceNumber as Varchar) + Replicate('0',3-(len(Cast(@GuestNumber as Varchar)))) + Cast(@GuestNumber as Varchar)
			end
			else
			Begin
				Set @IdentificationNumber = @IdentificationNumber + '1'
				if len(@HotelCreditorNumber) = 7
					Set @tmp = Replicate('0',13-(len(Cast(@Invoice_ReferenceNumber as Varchar)))) + Cast(@Invoice_ReferenceNumber as Varchar) 
				else
					Set @tmp = Replicate('0',12-(len(Cast(@Invoice_ReferenceNumber as Varchar)))) + Cast(@Invoice_ReferenceNumber as Varchar) 
			end
			set @tmp = Replace(@tmp, ' ', '0')
			Set @IdentificationNumber = @IdentificationNumber + @tmp
			Set @IdentificationNumber = dbo.MaesureIdentification(@IdentificationNumber)
		end
	end
	/* type 4 (21 cifre)*/
	if (@Paymenttype in (1,2,3,7,8,9)) and (@CustomerNumber is not null) and (@ReferenceBillInvoiceNumber is not null) and  (@HotelCreditorNumber is not null) and (@Hotelnumber is not null) and (@SwitchCustomerRBI is not null)
	Begin
		if @ReferenceBillInvoiceNumber > 0 
		Begin
			Set @tmp = Cast(@Hotelnumber as Varchar(20))
			Set @IdentificationNumber = substring(@HotelCreditorNumber,1,6)
			if len(@IdentificationNumber) < 6
				Set @IdentificationNumber = Replicate('0', 6-len(@IdentificationNumber)) + @IdentificationNumber
			if Len(@tmp) < 2
				Set @IdentificationNumber = @IdentificationNumber + '0' + @tmp
			else
				Set @IdentificationNumber = @IdentificationNumber + @tmp

			if @SwitchCustomerRBI = 1
			Begin
				Set @IdentificationNumber = @IdentificationNumber + Replicate('0', 6-len(Cast(@ReferenceBillInvoiceNumber as varchar(6)))) + Cast(@ReferenceBillInvoiceNumber as Varchar(6))
				Set @IdentificationNumber = @IdentificationNumber + Replicate('0', 6-len(@CustomerNumber)) + @CustomerNumber
			end
			else
			Begin
				Set @IdentificationNumber = @IdentificationNumber + Replicate('0', 6-len(@CustomerNumber)) + @CustomerNumber
				Set @IdentificationNumber = @IdentificationNumber + Replicate('0', 6-len(Cast(@ReferenceBillInvoiceNumber as varchar(6)))) + Cast(@ReferenceBillInvoiceNumber as Varchar(6))
			end
			Set @IdentificationNumber = Replace(@IdentificationNumber, ' ', '0')
			Set @IdentificationNumber = dbo.MaesureIdentification(@IdentificationNumber)
		end
	end 
	RETURN @IdentificationNumber
END