



CREATE PROCEDURE DBO.GetSaldoOnFolio @ReferenceNumber int, @GuestNumber int, @AddressTypeNumber int, @Saldo money  OUTPUT AS
SET NOCOUNT ON
-- Sidst rettet af Klaus Stenå d. 16/7-2001

Declare @FolioNumber int
Declare @SubTotal money

Declare @I smallint
Declare @NumberOfRepeats SmallInt

SET @SubTotal = 0

if  @AddressTypeNumber = -1
  SET @NumberOfRepeats = 4
else
  SET @NumberOfRepeats = 1

SET @I = 1

While (@I<=@NumberOfRepeats)
begin
  SET @FolioNumber = -1

  if @NumberOfRepeats=4
   SET @AddressTypeNumber = @I 

  SELECT @FolioNumber = FolioNumber
  FROM FOLIOS WITH (NOLOCK) INNER JOIN RESERVATIONADDRESS WITH (NOLOCK) ON 
      FOLIOS.ReservationAddressNumber = RESERVATIONADDRESS.AddressNumber
       INNER JOIN RESERVATIONADDRESSXTAB WITH (NOLOCK) ON 
      RESERVATIONADDRESS.AddressNumber = RESERVATIONADDRESSXTAB.ADDRESSNUMBER
  WHERE ReservationAddressXTab.ReferenceNumber=@ReferenceNumber and ReservationAddressXTab.GuestNumber=@GuestNumber and  ReservationAddress.AddressType = @AddressTypeNumber
 
  if @FolioNumber >-1
  begin
    exec GetTransactions @FolioNumber, -1, 0, 0, 0, @Saldo  OUTPUT
    SET @SubTotal = @SubTotal + @Saldo
  end

  SET @I = @I+1
end

SET @Saldo= @SubTotal

